#!/usr/libexec/platform-python -s
# *****************************************************************************
# MLZ Tango client tool
# Copyright (c) 2015-2019 by the authors, see LICENSE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Module authors:
#   Georg Brandl <g.brandl@fz-juelich.de>
#
# *****************************************************************************

# pylint: disable=invalid-name

import argparse
import sys
from os import path

# Add import path for inplace usage
sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..')))

from quango.qt import QApplication, QHBoxLayout, QMainWindow, QVBoxLayout, \
    QWidget

# pylint: disable=wrong-import-position
from quango.mlzgui import MLZGuiPanel

parser = argparse.ArgumentParser(description='''\
Shows GUI for Tango devices conforming to MLZ standard interfaces.

You can pass any number of Tango device names, the GUIs will be stacked
vertically. Pass a single "+" argument to begin a new stack.

If all device names have a common prefix (a/ or a/b/), you can pass this
with the -p option and omit it from the other arguments.
''')
parser.add_argument('--prefix', '-p')
parser.add_argument('--minimal', '-m', action='store_true')
parser.add_argument('device', nargs='+')

args = parser.parse_args()
pfx = args.prefix or ''
if pfx and not pfx.endswith('/'):
    pfx += '/'

app = QApplication(sys.argv)
window = QMainWindow()
mainwidget = QWidget()
mainlayout = QHBoxLayout()
vlayout = QVBoxLayout()

for dev in args.device:
    if dev == '+':
        vlayout.addStretch()
        mainlayout.addLayout(vlayout)
        vlayout = QVBoxLayout()
        continue
    panel = MLZGuiPanel.create(window, pfx + dev, minimal=args.minimal)
    panel.setName(dev)
    vlayout.addWidget(panel)

vlayout.addStretch()
mainlayout.addLayout(vlayout)
mainwidget.setLayout(mainlayout)
window.setWindowTitle('Quango')
window.setCentralWidget(mainwidget)
window.show()
app.exec_()
