Example #1
0
class WServerInformation(QWidget, Logger.ClassLogger):
    """
    Widget for the global server information
    """
    def __init__(self, parent=None):
        """
        Constructs WServerInformation widget 

        @param parent: 
        @type parent:
        """
        QWidget.__init__(self, parent)
        self.parent = parent
        self.name = self.tr("Miscellaneous")
        self.createWidgets()
        self.createActions()
        self.createToolbar()
        self.deactivate()

    def createWidgets(self):
        """
        QtWidgets creation
        """
        self.dockToolbar = QToolBar(self)
        self.dockToolbar.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.dockToolbarReset = QToolBar(self)
        self.dockToolbarReset.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbarReset.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.dockToolbarGen = QToolBar(self)
        self.dockToolbarGen.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbarGen.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.refreshBox = QGroupBox("Refresh")
        self.refreshBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutRefreshBox = QHBoxLayout()
        layoutRefreshBox.addWidget(self.dockToolbar)
        layoutRefreshBox.setContentsMargins(0, 0, 0, 0)
        self.refreshBox.setLayout(layoutRefreshBox)

        self.resetBox = QGroupBox("Reset")
        self.resetBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutResetBox = QHBoxLayout()
        layoutResetBox.addWidget(self.dockToolbarReset)
        layoutResetBox.setContentsMargins(0, 0, 0, 0)
        self.resetBox.setLayout(layoutResetBox)

        self.genBox = QGroupBox("Prepare")
        self.genBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutGenBox = QHBoxLayout()
        layoutGenBox.addWidget(self.dockToolbarGen)
        layoutGenBox.setContentsMargins(0, 0, 0, 0)
        self.genBox.setLayout(layoutGenBox)

        layoutToolbars = QHBoxLayout()
        layoutToolbars.addWidget(self.refreshBox)
        layoutToolbars.addWidget(self.genBox)
        layoutToolbars.addWidget(self.resetBox)
        layoutToolbars.addStretch(1)

        layoutFinal = QHBoxLayout()
        layoutLeft = QVBoxLayout()

        layoutRight = QVBoxLayout()
        layoutRight.addLayout(layoutToolbars)

        self.diskUsageBox = QGroupBox("Disk Usage")
        self.nbSizeLogsOnDiskLabel = QLabel("0")
        self.nbSizeTmpOnDiskLabel = QLabel("0")
        self.nbSizeArchivesOnDiskLabel = QLabel("0")
        self.nbSizeAdpOnDiskLabel = QLabel("0")
        self.nbSizeLibOnDiskLabel = QLabel("0")
        self.nbSizeBakOnDiskLabel = QLabel("0")
        self.nbSizeTestsOnDiskLabel = QLabel("0")
        layout2 = QFormLayout()
        layout2.addRow(QLabel("Logs"), self.nbSizeLogsOnDiskLabel)
        layout2.addRow(QLabel("Tmp"), self.nbSizeTmpOnDiskLabel)
        layout2.addRow(QLabel("Archives"), self.nbSizeArchivesOnDiskLabel)
        layout2.addRow(QLabel("Tests"), self.nbSizeTestsOnDiskLabel)
        layout2.addRow(QLabel("Adapters"), self.nbSizeAdpOnDiskLabel)
        layout2.addRow(QLabel("Libraries"), self.nbSizeLibOnDiskLabel)
        layout2.addRow(QLabel("Backups"), self.nbSizeBakOnDiskLabel)
        self.diskUsageBox.setLayout(layout2)

        layoutGrid = QGridLayout()
        layoutGrid.addWidget(self.diskUsageBox, 0, 0)
        layoutRight.addLayout(layoutGrid)
        layoutRight.addStretch(1)

        self.informations = QTreeWidget(self)
        self.informations.setVerticalScrollMode(
            QAbstractItemView.ScrollPerPixel)
        self.informations.setIndentation(10)
        self.labels = [self.tr("Key"), self.tr("Value")]
        self.informations.setHeaderLabels(self.labels)

        layoutLeft.addWidget(self.informations)
        layoutFinal.addLayout(layoutLeft)
        layoutFinal.addLayout(layoutRight)

        self.setLayout(layoutFinal)

    def createActions(self):
        """
        Actions defined:
         * generate the cache for the documentation
         * generate all packages
         * refresh statistics
         * refresh the context of the server
         * check the syntax of adapters
         * reset statistics
        """
        self.genCacheHelpAction = QtHelper.createAction(
            self,
            "&Generate\nDocumentations",
            self.genCacheHelp,
            tip='Generate the cache for the documentation',
            icon=QIcon(":/generate-doc.png"))
        self.genTarAdaptersAction = QtHelper.createAction(
            self,
            "&Package\nAdapters",
            self.genPackageAdapters,
            tip='Generate adapters packages',
            icon=QIcon(":/generate-tar.png"))
        self.genTarLibrariesAction = QtHelper.createAction(
            self,
            "&Package\nLibraries",
            self.genPackageLibraries,
            tip='Generate libraries packages',
            icon=QIcon(":/generate-tar.png"))
        self.genTarSamplesAction = QtHelper.createAction(
            self,
            "&Package\nSamples",
            self.genPackageSamples,
            tip='Generate samples packages',
            icon=QIcon(":/generate-tar.png"))

        self.refreshAction = QtHelper.createAction(
            self,
            "&Usages",
            self.refreshUsages,
            tip='Refresh Usages',
            tooltip='Refresh usages',
            icon=QIcon(":/refresh-statistics.png"))
        self.refreshCtxAction = QtHelper.createAction(
            self,
            "&Session",
            self.refreshCtx,
            tip='Refresh server context',
            tooltip='Refresh server context',
            icon=QIcon(":/act-refresh.png"))

        self.resetAction = QtHelper.createAction(
            self,
            "&Reset\nStatistics",
            self.resetStats,
            tip='Reset all statistics',
            icon=QIcon(":/reset-counter.png"))
        self.unlockAllTestsAction = QtHelper.createAction(
            self,
            "&Unlock\nTests",
            self.unlockTests,
            tip='Unlock all files',
            icon=QIcon(":/unlock.png"))

        self.unlockAllAdaptersAction = QtHelper.createAction(
            self,
            "&Unlock\nAdapters",
            self.unlockAdapters,
            tip='Unlock all files',
            icon=QIcon(":/unlock.png"))

        self.unlockAllLibrariesAction = QtHelper.createAction(
            self,
            "&Unlock\nLibraries",
            self.unlockLibraries,
            tip='Unlock all files',
            icon=QIcon(":/unlock.png"))

    def createToolbar(self):
        """
        Toolbar creation
        """
        self.dockToolbar.setObjectName("Misc toolbar")
        self.dockToolbar.addAction(self.refreshCtxAction)
        self.dockToolbar.addAction(self.refreshAction)
        self.dockToolbar.setIconSize(QSize(16, 16))

        self.dockToolbarGen.setObjectName("Generate toolbar")
        self.dockToolbarGen.addAction(self.genCacheHelpAction)
        self.dockToolbarGen.addAction(self.genTarAdaptersAction)
        self.dockToolbarGen.addAction(self.genTarLibrariesAction)
        self.dockToolbarGen.addAction(self.genTarSamplesAction)
        self.dockToolbarGen.setIconSize(QSize(16, 16))

        self.dockToolbarReset.setObjectName("Reset toolbar")
        self.dockToolbarReset.addAction(self.resetAction)
        self.dockToolbarReset.addAction(self.unlockAllTestsAction)
        self.dockToolbarReset.addAction(self.unlockAllAdaptersAction)
        self.dockToolbarReset.addAction(self.unlockAllLibrariesAction)
        self.dockToolbarReset.setIconSize(QSize(16, 16))

    def unlockTests(self):
        """
        Unlock all files
        """
        RCI.instance().unlockTests()

    def unlockAdapters(self):
        """
        Unlock all files
        """
        RCI.instance().unlockAdapters()

    def unlockLibraries(self):
        """
        Unlock all files
        """
        RCI.instance().unlockLibraries()

    def genPackageAdapters(self):
        """
        Generate all tar packages
        """
        RCI.instance().buildAdapters()

    def genPackageLibraries(self):
        """
        Generate all tar packages
        """
        RCI.instance().buildLibraries()

    def genPackageSamples(self):
        """
        Generate all tar packages
        """
        RCI.instance().buildSamples()

    def resetStats(self):
        """
        Reset statistic manually
        """
        reply = QMessageBox.question(self, "Reset statistics",
                                     "Are you sure ?",
                                     QMessageBox.Yes | QMessageBox.No)
        if reply == QMessageBox.Yes:
            RCI.instance().resetTestsMetrics()

    def refreshCtx(self):
        """
        Call the server to refresh context of the server
        """
        RCI.instance().sessionContext()

    def refreshUsages(self):
        """
        Call the server to refresh statistics of the server
        """
        RCI.instance().systemUsages()

    def genCacheHelp(self):
        """
        Call the server to generate the cache documentation
        """
        reply = QMessageBox.question(self, "Generate cache", "Are you sure ?",
                                     QMessageBox.Yes | QMessageBox.No)
        if reply == QMessageBox.Yes:
            RCI.instance().buildDocumentations()

    def active(self):
        """
        Enables QTreeWidget
        """
        self.diskUsageBox.setEnabled(True)
        self.informations.setEnabled(True)
        self.genCacheHelpAction.setEnabled(True)
        self.resetAction.setEnabled(True)
        self.unlockAllTestsAction.setEnabled(True)
        self.unlockAllAdaptersAction.setEnabled(True)
        self.unlockAllLibrariesAction.setEnabled(True)

    def deactivate(self):
        """
        Clears QTreeWidget and disables it
        """
        self.diskUsageBox.setEnabled(False)

        self.informations.clear()
        self.informations.setEnabled(False)
        self.genCacheHelpAction.setEnabled(False)
        self.resetAction.setEnabled(False)
        self.unlockAllTestsAction.setEnabled(False)
        self.unlockAllAdaptersAction.setEnabled(False)
        self.unlockAllLibrariesAction.setEnabled(False)

        self.nbSizeLogsOnDiskLabel.setText("0")
        self.nbSizeTmpOnDiskLabel.setText("0")
        self.nbSizeArchivesOnDiskLabel.setText("0")
        self.nbSizeAdpOnDiskLabel.setText("0")
        self.nbSizeLibOnDiskLabel.setText("0")
        self.nbSizeBakOnDiskLabel.setText("0")
        self.nbSizeTestsOnDiskLabel.setText("0")

    def cleanContext(self):
        """
        Clear the context
        Removes all items
        """
        self.informations.clear()

    def loadStats(self, data):
        """
        Load statistics
        """

        self.nbSizeLogsOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-logs'])))
        self.nbSizeTmpOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-tmp'])))

        self.nbSizeArchivesOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-testresults'])))
        self.nbSizeAdpOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-adapters'])))
        self.nbSizeLibOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-libraries'])))
        self.nbSizeBakOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-backups'])))
        self.nbSizeTestsOnDiskLabel.setText(
            str(QtHelper.bytes2human(data['disk-usage-tests'])))

    def loadData(self, data):
        """
        Load all config keys

        @param data: 
        @type data:
        """
        if isinstance(data, dict):
            data = [data]
        for param in data:
            probeItem = ParamItem(param=param, parent=self.informations)

        # resize columns
        for i in xrange(len(self.labels) - 1):
            self.informations.resizeColumnToContents(i)
Example #2
0
class RawView(QWidget, Logger.ClassLogger):
    """
    Raw view widget
    """
    def __init__(self,
                 parent,
                 data,
                 toCsv=False,
                 toHtml=False,
                 toXml=False,
                 toPrinter=False,
                 toTxt=False,
                 toPdf=False):
        """
        Raw view widget

        @param parent: 
        @type parent:
        """
        QWidget.__init__(self, parent)
        self.parent = parent
        self.__data = data

        self.toCsv = toCsv
        self.toXml = toXml
        self.toCsv = toCsv
        self.toHtml = toHtml
        self.toPrinter = toPrinter
        self.toTxt = toTxt
        self.toPdf = toPdf

        self.createActions()
        self.createWidgets()
        self.createToolbars()
        self.createConnections()

    def createWidgets(self):
        """
        Create qt widgets
        """
        # prepare menu
        self.toolbar = QToolBar(self)
        self.toolbar.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.toolbarPlugins = QToolBar(self)
        self.toolbarPlugins.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.toolbarPlugins.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.pluginsBox = QGroupBox("Plugins")
        self.pluginsBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutPlugins = QHBoxLayout()
        layoutPlugins.addWidget(self.toolbarPlugins)
        layoutPlugins.setContentsMargins(0, 0, 0, 0)
        self.pluginsBox.setLayout(layoutPlugins)
        self.pluginsBox.hide()

        self.exportBox = QGroupBox("Exports")
        self.exportBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutExports = QHBoxLayout()
        layoutExports.addWidget(self.toolbar)
        layoutExports.setContentsMargins(0, 0, 0, 0)
        self.exportBox.setLayout(layoutExports)

        layout = QVBoxLayout()

        if self.toXml:
            self.txtEdit = QtHelper.RawXmlEditor(parent=self)
            self.txtEdit.setText(self.__data)
            # self.txtEdit.setUtf8(True)
            self.txtEdit.setFont(QFont("Courier", 9))
        else:
            self.txtEdit = QtHelper.RawEditor(parent=self)
            self.txtEdit.setTabStopWidth(10)
            self.txtEdit.setText(self.__data)
            self.txtEdit.setFont(QFont("Courier", 9))

        self.txtEdit.setMinimumWidth(650)
        self.txtEdit.setMinimumHeight(400)

        self.delGroup = QGroupBox("Remove line")
        self.delTG = QCheckBox("TESTGLOBAL")
        self.delTP = QCheckBox("TESTPLAN")
        self.delTS = QCheckBox("TESTSUITE")
        self.delTU = QCheckBox("TESTUNIT")
        self.delTA = QCheckBox("TESTABSTRACT")
        self.delTC = QCheckBox("TESTCASE")
        self.delSTP = QCheckBox("STEP")

        layoutDel = QHBoxLayout()
        layoutDel.addWidget(self.delTG)
        layoutDel.addWidget(self.delTP)
        layoutDel.addWidget(self.delTS)
        layoutDel.addWidget(self.delTU)
        layoutDel.addWidget(self.delTA)
        layoutDel.addWidget(self.delTC)
        layoutDel.addWidget(self.delSTP)
        self.delGroup.setLayout(layoutDel)

        if self.toXml: self.delGroup.setEnabled(False)

        layoutToolbars = QHBoxLayout()
        layoutToolbars.addWidget(self.exportBox)
        layoutToolbars.addWidget(self.pluginsBox)
        layoutToolbars.addStretch(1)
        layoutToolbars.setContentsMargins(5, 0, 0, 0)

        layout.addLayout(layoutToolbars)
        layout.addWidget(self.delGroup)
        layout.addWidget(self.txtEdit)
        if not self.toXml:
            self.rawFind = QtHelper.RawFind(parent=self,
                                            editor=self.txtEdit,
                                            buttonNext=True)
            layout.addWidget(self.rawFind)

        self.setLayout(layout)

    def createConnections(self):
        """
        All qt connections
        """
        self.delTG.stateChanged.connect(self.onRemoveLines)
        self.delTP.stateChanged.connect(self.onRemoveLines)
        self.delTS.stateChanged.connect(self.onRemoveLines)
        self.delTU.stateChanged.connect(self.onRemoveLines)
        self.delTA.stateChanged.connect(self.onRemoveLines)
        self.delTC.stateChanged.connect(self.onRemoveLines)
        self.delSTP.stateChanged.connect(self.onRemoveLines)

    def createActions(self):
        """
        Qt Actions
        """
        self.saveCsvAction = QtHelper.createAction(self,
                                                   "&To CSV",
                                                   self.saveCsv,
                                                   tip='Save to CSV file',
                                                   icon=QIcon(":/csv.png"))
        self.saveTxtAction = QtHelper.createAction(
            self,
            "&To TXT",
            self.saveTxt,
            tip='Save to TXT file',
            icon=QIcon(":/file-txt.png"))
        self.saveHtmlAction = QtHelper.createAction(self,
                                                    "&To HTML",
                                                    self.saveHtml,
                                                    tip='Save to HTML file',
                                                    icon=QIcon(":/web.png"))
        self.savePdfAction = QtHelper.createAction(self,
                                                   "&To PDF",
                                                   self.savePdf,
                                                   tip='Save to PDF file',
                                                   icon=QIcon(":/to_pdf.png"))
        self.saveXmlAction = QtHelper.createAction(self,
                                                   "&To XML",
                                                   self.saveXml,
                                                   tip='Save to XML file',
                                                   icon=QIcon(":/xml.png"))
        self.toPrinterAction = QtHelper.createAction(
            self,
            "&To Printer",
            self.savePrinter,
            tip='Print',
            icon=QIcon(":/printer.png"))

    def createToolbars(self):
        """
        Toolbar creation
        """
        self.toolbar.setObjectName("Export toolbar")
        if self.toCsv: self.toolbar.addAction(self.saveCsvAction)
        if self.toTxt: self.toolbar.addAction(self.saveTxtAction)
        if self.toHtml: self.toolbar.addAction(self.saveHtmlAction)
        if self.toPdf: self.toolbar.addAction(self.savePdfAction)
        if self.toXml: self.toolbar.addAction(self.saveXmlAction)
        if self.toPrinter: self.toolbar.addAction(self.toPrinterAction)
        self.toolbar.setIconSize(QSize(16, 16))

    def registerPlugin(self, pluginAction):
        """
        Register plugin
        """
        self.toolbarPlugins.addAction(pluginAction)
        self.toolbarPlugins.setIconSize(QSize(16, 16))
        self.pluginsBox.show()

    def onRemoveLines(self):
        """
        Called to remove lines
        """
        ret = []
        for l in self.__data.splitlines():
            if self.delTG.checkState() and l.startswith("TESTGLOBAL"):
                continue
            elif self.delTP.checkState() and l.startswith("TESTPLAN"):
                continue
            elif self.delTS.checkState() and l.startswith("TESTSUITE"):
                continue
            elif self.delTU.checkState() and l.startswith("TESTUNIT"):
                continue
            elif self.delTA.checkState() and l.startswith("TESTABSTRACT"):
                continue
            elif self.delTC.checkState() and l.startswith("TESTCASE"):
                continue
            elif self.delSTP.checkState() and l.startswith("STEP"):
                continue
            else:
                ret.append(l)
        self.txtEdit.setText("\n".join(ret))
        del ret

    def savePrinter(self):
        """
        Save to printer
        """
        printer = QPrinter()
        dialog = QPrintDialog(printer, self)
        dialog.setWindowTitle("Print")

        if dialog.exec_() != QDialog.Accepted:
            return

        if QtHelper.IS_QT5:  # new in v18
            self.fileName = printer
            self.txtEdit.page().toHtml(self.__toPrinter)
        else:
            doc = QTextDocument()
            doc.setPlainText(self.txtEdit.text())
            doc.print_(printer)

    def __toPrinter(self, html):
        """
        New in v18
        Callback from QWebpage
        """
        textEdit = QTextEdit(self)
        textEdit.setHtml(html)
        textEdit.print(self.fileName)
        textEdit.deleteLater()

        self.fileName = None

    def saveCsv(self):
        """
        Save to csv file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save CSV file", "", "CSV file (*.csv);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            try:
                f = open(_filename, 'w')
                f.write(self.txtEdit.toPlainText())
                f.close()
            except Exception as e:
                self.error('unable to save report file as txt: %s' % str(e))

    def saveTxt(self):
        """
        Save to txt file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save TXT file", "", "TXT file (*.txt);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            try:
                f = open(_filename, 'w')
                f.write(self.txtEdit.toPlainText())
                f.close()
            except Exception as e:
                self.error('unable to save report file as txt: %s' % str(e))

    def saveXml(self):
        """
        Save to xml file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save XML file", "", "XML file (*.xml);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            try:
                with codecs.open(_filename, "w", "utf-8") as f:
                    f.write(self.txtEdit.text())
            except Exception as e:
                self.error('unable to save design file as xml: %s' % str(e))

    def saveHtml(self):
        """
        Save to html file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save HTML file", "", "HTML file (*.html);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            try:
                f = open(_filename, 'w')
                f.write(self.txtEdit.toHtml())
                f.close()
            except Exception as e:
                self.error('unable to save report file as html: %s' % str(e))

    def savePdf(self):
        """
        Save pdf file
        """
        fileName = QFileDialog.getSaveFileName(
            self, 'Save to PDF', "", "PDF file (*.pdf);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            printer = QPrinter(QPrinter.HighResolution)
            printer.setPageSize(QPrinter.A4)
            printer.setColorMode(QPrinter.Color)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(_filename)

            doc = QTextDocument()
            if self.toXml:
                doc.setPlainText(self.txtEdit.text())
            else:
                doc.setHtml(self.txtEdit.toHtml())
            doc.print_(printer)
class RawView(QWidget, Logger.ClassLogger):
    """
    Raw view widget
    """
    def __init__(self,
                 parent,
                 data,
                 toCsv=False,
                 toHtml=False,
                 toXml=False,
                 toPrinter=False,
                 toTxt=False,
                 toPdf=False):
        """
        Raw view widget

        @param parent: 
        @type parent:
        """
        QWidget.__init__(self, parent)
        self.parent = parent
        self.__data = data
        self.toXml = toXml
        self.toCsv = toCsv
        self.toHtml = toHtml
        self.toPrinter = toPrinter
        self.toTxt = toTxt
        self.toPdf = toPdf

        self.fileName = None

        self.createWidgets()
        self.createActions()
        self.createToolbars()

    def createWidgets(self):
        """
        Create qt widgets
        """
        # prepare menu
        self.toolbar = QToolBar(self)
        self.toolbar.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.toolbarPlugins = QToolBar(self)
        self.toolbarPlugins.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.toolbarPlugins.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.pluginsBox = QGroupBox("Plugins")
        self.pluginsBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutPlugins = QHBoxLayout()
        layoutPlugins.addWidget(self.toolbarPlugins)
        layoutPlugins.setContentsMargins(0, 0, 0, 0)
        self.pluginsBox.setLayout(layoutPlugins)
        self.pluginsBox.hide()

        self.exportBox = QGroupBox("Exports")
        self.exportBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutExports = QHBoxLayout()
        layoutExports.addWidget(self.toolbar)
        layoutExports.setContentsMargins(0, 0, 0, 0)
        self.exportBox.setLayout(layoutExports)
        self.exportBox.setMaximumHeight(70)

        layout = QVBoxLayout()

        if self.toXml:
            self.txtEdit = QtHelper.RawXmlEditor(parent=self)
            self.txtEdit.setText(self.__data)
            # self.txtEdit.setUtf8(False)
            self.txtEdit.setFont(QFont("Courier", 9))
        else:
            self.txtEdit = QWebView(parent=self)
            # convert to qbyte array to support qt5
            tmp_ = QByteArray()
            tmp_.append(self.__data)

            self.txtEdit.setContent(tmp_, "text/html; charset=utf-8")

        layoutToolbars = QHBoxLayout()
        layoutToolbars.addWidget(self.exportBox)
        layoutToolbars.addWidget(self.pluginsBox)
        layoutToolbars.addStretch(1)
        layoutToolbars.setContentsMargins(5, 0, 0, 0)

        layout.addLayout(layoutToolbars)
        layout.addWidget(self.txtEdit)

        self.setLayout(layout)

    def createToolbars(self):
        """
        Toolbar creation
        """
        self.toolbar.setObjectName("Export toolbar")
        if self.toTxt: self.toolbar.addAction(self.saveTxtAction)
        if self.toHtml: self.toolbar.addAction(self.saveHtmlAction)
        if self.toPdf: self.toolbar.addAction(self.savePdfAction)
        if self.toXml: self.toolbar.addAction(self.saveXmlAction)
        if self.toPrinter: self.toolbar.addAction(self.toPrinterAction)
        self.toolbar.setIconSize(QSize(16, 16))

    def registerPlugin(self, pluginAction):
        """
        Register plugin in toolbar
        """
        self.toolbarPlugins.addAction(pluginAction)
        self.toolbarPlugins.setIconSize(QSize(16, 16))
        self.pluginsBox.show()

    def createActions(self):
        """
        Qt Actions
        """
        self.saveTxtAction = QtHelper.createAction(
            self,
            "&To TXT",
            self.saveTxt,
            tip='Save to TXT file',
            icon=QIcon(":/file-txt.png"))
        self.saveHtmlAction = QtHelper.createAction(self,
                                                    "&To HTML",
                                                    self.saveHtml,
                                                    tip='Save to HTML file',
                                                    icon=QIcon(":/web.png"))
        self.savePdfAction = QtHelper.createAction(self,
                                                   "&To PDF",
                                                   self.savePdf,
                                                   tip='Save to PDF file',
                                                   icon=QIcon(":/to_pdf.png"))
        self.saveXmlAction = QtHelper.createAction(self,
                                                   "&To XML",
                                                   self.saveXml,
                                                   tip='Save to XML file',
                                                   icon=QIcon(":/xml.png"))
        self.toPrinterAction = QtHelper.createAction(
            self,
            "&To Printer",
            self.savePrinter,
            tip='Print',
            icon=QIcon(":/printer.png"))

    def savePrinter(self):
        """
        Save to printer
        """
        printer = QPrinter()
        dialog = QPrintDialog(printer, self)
        dialog.setWindowTitle("Print")

        if dialog.exec_() != QDialog.Accepted:
            return

        if QtHelper.IS_QT5:  # new in v18
            self.fileName = printer
            self.txtEdit.page().toHtml(self.__toPrinter)
        else:
            self.txtEdit.print_(printer)

    def __toPrinter(self, html):
        """
        New in v18
        Callback from QWebpage
        """
        textEdit = QTextEdit(self)
        textEdit.setHtml(html)
        textEdit.print(self.fileName)
        textEdit.deleteLater()

        self.fileName = None

    def saveTxt(self):
        """
        Save to txt file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save TXT file", "", "TXT file (*.txt);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            if QtHelper.IS_QT5:  # new in v18
                self.fileName = _filename
                self.txtEdit.page().toPlainText(self.__toPlainText)
            else:
                frame = self.txtEdit.page().mainFrame()
                try:
                    with codecs.open(_filename, "w", "utf-8") as f:
                        f.write(frame.toPlainText())
                except Exception as e:
                    self.error('unable to save report file as txt: %s' %
                               str(e))

    def __toPlainText(self, text):
        """
        New in v18
        Callback from QWebpage
        """
        if self.fileName is None:
            return

        try:
            with codecs.open(self.fileName, "w", "utf-8") as f:
                f.write(text)
        except Exception as e:
            self.error('unable to save report file as txt: %s' % str(e))

        self.fileName = None

    def saveXml(self):
        """
        Save xml file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save XML file", "", "XML file (*.xml);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            try:
                with codecs.open(_filename, "w", "utf-8") as f:
                    f.write(self.txtEdit.text())
            except Exception as e:
                self.error('unable to save report file as xml: %s' % str(e))

    def saveHtml(self):
        """
        Save to html file
        """
        fileName = QFileDialog.getSaveFileName(
            self, "Save HTML file", "", "HTML file (*.html);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            if QtHelper.IS_QT5:  # new in v18
                self.fileName = _filename
                self.txtEdit.page().toHtml(self.__toHtml)
            else:
                frame = self.txtEdit.page().mainFrame()
                try:
                    with codecs.open(_filename, "w", "utf-8") as f:
                        f.write(frame.toHtml())
                except Exception as e:
                    self.error('unable to save report file as html: %s' %
                               str(e))

    def __toHtml(self, html):
        """
        New in v18
        Callback from QWebpage
        """
        if self.fileName is None:
            return

        try:
            with codecs.open(self.fileName, "w", "utf-8") as f:
                f.write(html)
        except Exception as e:
            self.error('unable to save report file as html: %s' % str(e))

        self.fileName = None

    def savePdf(self):
        """
        Save to pdf file
        """
        fileName = QFileDialog.getSaveFileName(
            self, 'Save to PDF', "", "PDF file (*.pdf);;All Files (*.*)")

        # new in v17.1
        if QtHelper.IS_QT5:
            _filename, _type = fileName
        else:
            _filename = fileName
        # end of new

        if _filename:
            if QtHelper.IS_QT5:  # new in v18
                self.fileName = _filename
                self.txtEdit.page().printToPdf(self.__toPdf)
            else:
                printer = QPrinter(QPrinter.HighResolution)
                printer.setPageSize(QPrinter.A4)
                printer.setColorMode(QPrinter.Color)
                printer.setOutputFormat(QPrinter.PdfFormat)
                printer.setOutputFileName(_filename)

                if isinstance(self.txtEdit, QWebView):
                    self.txtEdit.print_(printer)
                else:
                    doc = QTextDocument()
                    if self.toXml:
                        doc.setPlainText(self.txtEdit.text())
                    else:
                        doc.setHtml(self.txtEdit.toHtml())
                    doc.print_(printer)

    def __toPdf(self, pdf):
        """
        New in v18
        Callback from QWebpage
        """
        if self.fileName is None:
            return

        try:
            with codecs.open(self.fileName, "wb") as f:
                f.write(pdf)
        except Exception as e:
            self.error('unable to save report file as pdf: %s' % str(e))

        self.fileName = None
Example #4
0
class ImmatriculationSCoopViewWidget(FWidget):

    def __init__(self, parent, dmd=None, *args, **kwargs):
        super(ImmatriculationSCoopViewWidget, self).__init__(
            parent=parent, *args, **kwargs)

        self.parent = parent
        self.parentWidget().set_window_title("FORMULAIRE D’IMMATRICULATION")
        self.dmd = dmd
        self.scoop = self.dmd.scoop
        self.name_declarant_field = QLineEdit()
        self.name_declarant_field.setPlaceholderText("M. / Mme")
        self.name_declarant_field.setMaximumWidth(600)

        self.procuration_field = QLineEdit()
        self.procuration_field.setPlaceholderText(
            "Réf.de la Procuration le cas échéant")
        self.procuration_field.setMaximumWidth(600)
        self.quality_box = QComboBox()
        self.quality_box.setMaximumWidth(600)
        self.quality_box.currentIndexChanged.connect(self.change_select)
        self.qualities_list = get_qualities()
        for index, value in enumerate(self.qualities_list):
            self.quality_box.addItem(
                "{}".format(self.qualities_list.get(value).upper()), value)

        self.type_box = QComboBox()
        self.type_box.setMaximumWidth(600)
        self.type_lists = Immatriculation.TYPES
        for index, value in enumerate(self.type_lists):
            print(value)
            self.type_box.addItem("{}".format(value[1], index))
        self.tel_declarant_field = IntLineEdit()
        self.tel_declarant_field.setInputMask('## ## ## ##')
        self.tel_declarant_field.setMaximumWidth(600)
        self.btn = Button_save("Sauvegarder")
        self.btn.setMaximumWidth(600)
        self.btn.clicked.connect(self.save)

        declarant_formbox = QFormLayout()
        declarant_formbox.addRow(FLabel("<strong>Type de d'immatriculation *: </strong>"), self.type_box)
        declarant_formbox.addRow(FLabel("<strong>Nom et prénom du declarant *: </strong>"), self.name_declarant_field)
        declarant_formbox.addRow(FLabel("<strong>En qualité de *: </strong>"), self.quality_box)
        declarant_formbox.addRow(FLabel("<strong>Procuration *: </strong>"), self.procuration_field)
        declarant_formbox.addRow(FLabel("<strong>Numéro tel. du declarant *: </strong>"), self.tel_declarant_field)
        declarant_formbox.addRow(FLabel(""), self.btn)
        self.declarantGroupBox = QGroupBox("Info. du déclarant de la {} *".format(self.scoop.denomination))
        self.declarantGroupBox.setStyleSheet(CSS_CENTER)
        self.declarantGroupBox.setLayout(declarant_formbox)
        vbox = QVBoxLayout()
        # vbox.addWidget(self.infoGroupBox)
        vbox.addWidget(self.declarantGroupBox)
        # vbox.addLayout(editbox)
        self.setLayout(vbox)

    def change_select(self):
        self.qlt_select = self.quality_box.itemData(
            self.quality_box.currentIndex())

        self.procuration_field.setEnabled(False)
        if self.qlt_select == Immatriculation.TP:
            self.procuration_field.setEnabled(True)
            # if check_is_empty(self.procuration_field):
            #     return False

    def is_not_valide(self):
        # print(check_is_empty(self.name_declarant_field))
        if self.quality_box.itemData(self.quality_box.currentIndex()) == Immatriculation.TP:
            if check_is_empty(self.procuration_field) or check_is_empty(self.tel_declarant_field):
                return True
        return check_is_empty(self.name_declarant_field) or check_is_empty(self.tel_declarant_field)

    def save(self):
        if self.is_not_valide():
            return False

        imma = Immatriculation()
        imma.scoop = self.scoop
        imma.typ_imm = self.type_lists[self.type_box.currentIndex()][0]
        imma.name_declarant = self.name_declarant_field.text()
        imma.quality = self.quality_box.itemData(self.quality_box.currentIndex())
        imma.procuration = self.procuration_field.text()
        imma.tel_declarant = self.tel_declarant_field.text()
        imma.save_ident()
        self.dmd.status = self.dmd.ENDPROCCES
        self.dmd.save_()
        self.parent.change_context(ResgistrationManagerWidget)
Example #5
0
class WTestConfig(Document.WDocument):
    """
    Test config widget
    """
    def __init__(self,
                 parent=None,
                 path=None,
                 filename=None,
                 extension=None,
                 nonameId=None,
                 remoteFile=False,
                 repoDest=None,
                 project=0,
                 isLocked=False):
        """
        Constructs WScript widget

        @param parent: 
        @type parent: 

        @param path: 
        @type path: 

        @param filename: 
        @type filename: 

        @param extension: 
        @type extension: 

        @param nonameId: 
        @type nonameId: 

        @param remoteFile: is remote
        @type remoteFile: boolean
        """
        Document.WDocument.__init__(self, parent, path, filename, extension,
                                    nonameId, remoteFile, repoDest, project,
                                    isLocked)

        self.tc = TestParameters.ParametersQWidget(self, forTestConfig=True)

        # new in v17
        defaultTimeout = Settings.instance().readValue(
            key='TestProperties/default-timeout')

        _defaults_params = []
        try:
            defaultInputs = Settings.instance().readValue(
                key='TestProperties/default-inputs')
            _defaults_params = json.loads(defaultInputs)
        except Exception as e:
            self.error("bad default inputs provided: %s - %s" %
                       (e, defaultInputs))
        # end of new

        self.dataModel = FileModelTestConfig.DataModel(
            timeout=defaultTimeout, parameters=_defaults_params)

        self.createWidgets()
        self.createToolbar()
        self.createConnections()

    def createWidgets(self):
        """
        QtWidgets creation
        """
        self.dockToolbar = QToolBar(self)
        self.dockToolbar.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.dockToolbarClipboard = QToolBar(self)
        self.dockToolbarClipboard.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbarClipboard.setToolButtonStyle(
            Qt.ToolButtonTextUnderIcon)

        self.clipBox = QGroupBox("Clipboard")
        self.clipBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutClipBox = QHBoxLayout()
        layoutClipBox.addWidget(self.dockToolbarClipboard)
        layoutClipBox.setContentsMargins(0, 0, 0, 0)
        self.clipBox.setLayout(layoutClipBox)

        self.paramsBox = QGroupBox("Parameters")
        self.paramsBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutParamBox = QHBoxLayout()
        layoutParamBox.addWidget(self.dockToolbar)
        layoutParamBox.setContentsMargins(0, 0, 0, 0)
        self.paramsBox.setLayout(layoutParamBox)

        layoutToolbars = QHBoxLayout()
        layoutToolbars.addWidget(self.paramsBox)
        layoutToolbars.addWidget(self.clipBox)
        layoutToolbars.addStretch(1)
        layoutToolbars.setContentsMargins(5, 0, 0, 0)

        title = QLabel("Test Config:")
        title.setStyleSheet("QLabel { padding-left: 2px; padding-top: 2px }")
        font = QFont()
        font.setBold(True)
        title.setFont(font)

        layout = QVBoxLayout()
        layout.addWidget(title)
        layout.addLayout(layoutToolbars)
        layout.addWidget(self.tc)
        layout.setContentsMargins(0, 0, 0, 0)

        self.setLayout(layout)

    def createConnections(self):
        """
        QtSignals connection
        """
        self.tc.table().DataChanged.connect(self.setModify)

    def createToolbar(self):
        """
        Toolbar creation
            
        ||------|------|||
        || Open | Save |||
        ||------|------|||
        """
        self.dockToolbar.setObjectName("Test Config toolbar")
        self.dockToolbar.addAction(self.tc.table().addAction)
        self.dockToolbar.addAction(self.tc.table().delAction)
        self.dockToolbar.addAction(self.tc.table().clearAction)
        self.dockToolbar.addAction(self.tc.table().openAction)
        self.dockToolbar.addSeparator()
        self.dockToolbar.addAction(self.tc.table().colorsAction)
        self.dockToolbar.setIconSize(QSize(16, 16))

        self.dockToolbarClipboard.setObjectName("Clipboard toolbar")
        self.dockToolbarClipboard.addAction(self.tc.table().copyAction)
        self.dockToolbarClipboard.addAction(self.tc.table().pasteAction)
        self.dockToolbarClipboard.addAction(self.tc.table().undoAction)
        self.dockToolbarClipboard.setIconSize(QSize(16, 16))

    def defaultLoad(self):
        """
        Default load
        """
        self.setReadOnly(readOnly=False)
        self.tc.table().loadData(
            data=self.dataModel.properties['properties']['parameters'])

    def load(self, content=None):
        """
        Open file and contruct the data model from file or directly with data

        @param content:
        @type content:
        """
        if content is not None:
            res = self.dataModel.load(rawData=content)
        else:
            absPath = '%s/%s.%s' % (self.path, self.filename, self.extension)
            res = self.dataModel.load(absPath=absPath)
        if res:
            self.setReadOnly(readOnly=False)
            self.tc.table().loadData(
                data=self.dataModel.properties['properties']['parameters'])
        return res

    def write(self, force=False, fromExport=False):
        """
        Write data to file, data are compressed 

        @param force:
        @type force: boolean

        @param fromExport:
        @type fromExport: boolean
        """
        if not force:
            if not self.isModified():
                return False

        saved = self.dataModel.write(
            absPath='%s/%s.%s' % (self.path, self.filename, self.extension))
        if saved:
            if not fromExport:
                self.setUnmodify()
            return True
        else:
            self.path = None
            return None

    def getraw_encoded(self):
        """
        Return raw data encoded in base64
        """
        return self.dataModel.getRaw()
Example #6
0
class CommandWindow (QMainWindow):
    
    # Constructs a new CommandWindow
    def __init__(self, parent = None):
        
        QMainWindow.__init__(self, parent)
        self.main_widget = QWidget()
        
        # Builds all groups
        self.build_connection_group()        
        self.build_source_windows()
        self.build_buttons_group()
        self.build_commands_group()
        self.build_log_group()
        
        # Initially the client is disconnected. Consequently, no boards
        # are available
        self.sources_group.setVisible(False)
        
        self.main_layout = QGridLayout()
        self.main_layout.setSpacing(10)
        
        # Groups all components in the main widget
        self.main_layout.addWidget(self.connect_group, 0, 0, 1, 3)
        self.main_layout.addWidget(self.commands_group, 1, 0, 1, 5)
        self.main_layout.addWidget(self.sources_group, 2, 0, 1, 5)
        self.main_layout.addWidget(self.log_group, 3, 0, 1, 5)
        
        self.main_widget.setLayout(self.main_layout)
        
        self.statusBar().showMessage("Client started.")
        self.setCentralWidget(self.main_widget)
        self.setWindowTitle('Commands Window')
        self.setFixedSize(QSize(720, 330))
        
        # Constructs the objects responsible to send and receive packets 
        # from PROSAC
        self.requester_controller = Requester(self)
        
        self.is_closed = 0
    
    # Sets a requester in the case it was not instantiated 
    def setRequester(self, r):
        self.requester_controller = r
        self.requester_controller.window_controller = self
    
    # Builds text boxes, labels and buttons required to establish a connection with PROSAC 
    def build_connection_group (self):
        
        self.connect_group = QGroupBox(parent =  self.main_widget)
        self.connect_group.setTitle('Connection')
        self.connect_group.setStyleSheet("QGroupBox { border: 1px solid gray; border-radius: 3px;  margin-top: 0.5em; } QGroupBox::title {  subcontrol-origin: margin; left: 10px; padding: 0 3px 0 3px;}")
        
        self.connect_label = QLabel("IP Address")
        self.connect_line = QLineEdit("10.0.6.65")
        self.connect_line.setFixedWidth(100)
        
        self.connect_button = QPushButton("Connect")
        self.connect_button.clicked.connect(self.connection_handler)
        
        self.port_label = QLabel("Port")
        self.port_line = QLineEdit("4000")
        self.port_line.setFixedWidth(50)
        self.led_connected = QLed(parent = self.main_widget)
        self.led_connected.turnOff()
        
        self.connect_layout = QHBoxLayout()
        
        self.connect_layout.addWidget(self.connect_label)
        self.connect_layout.addWidget(self.connect_line)
        self.connect_layout.addWidget(self.port_label)
        self.connect_layout.addWidget(self.port_line)
        self.connect_layout.addWidget(self.led_connected)
        self.connect_layout.addWidget(self.connect_button)
        
        self.connect_group.setLayout(self.connect_layout)
    
    # Builds components to show last received and sent messages
    def build_log_group (self): 
        
        self.log_group = QGroupBox(parent =  self.main_widget)
        self.log_group.setTitle('Messages')
        self.log_group.setStyleSheet("QGroupBox { border: 1px solid gray; border-radius: 3px;  margin-top: 0.5em; } QGroupBox::title {  subcontrol-origin: margin; left: 10px; padding: 0 3px 0 3px;}")
        
        self.label_sent = QLabel("Last packet sent: ")
        self.label_received = QLabel("Last packet received: ")
        
        self.log_layout = QVBoxLayout()
        self.log_layout.addWidget(self.label_received)
        self.log_layout.addWidget(self.label_sent)
        
        self.log_group.setLayout(self.log_layout)
    
    # Adds as many buttons as the number of connected power supplies.
    def build_buttons_group (self):
        
        self.sources_group = QGroupBox(parent =  self.main_widget)
        self.sources_group.setTitle('Power supply')
        self.sources_group.setStyleSheet("QGroupBox { border: 1px solid gray; border-radius: 3px;  margin-top: 0.5em; } QGroupBox::title {  subcontrol-origin: margin; left: 10px; padding: 0 3px 0 3px;}")
    
        self.sources_layout = QHBoxLayout()
    
        for button in self.board_buttons_list:
            self.sources_layout.addWidget(button)
            
        self.sources_group.setLayout(self.sources_layout)
        
    # Builds all command buttons.
    # 0x00 stands for a normal reading command, 0x01, a ADJUST command, 0x05, a acknowledge message,
    # 0xE0 is used to ENABLE CYCLE and 0xE1, to abort it.
    def build_commands_group (self):
        
        self.commands_group = QGroupBox(parent =  self.main_widget)
        self.commands_group.setTitle('Commands')
        self.commands_group.setStyleSheet("QGroupBox { border: 1px solid gray; border-radius: 3px;  margin-top: 0.5em; } QGroupBox::title {  subcontrol-origin: margin; left: 10px; padding: 0 3px 0 3px;}")
    
        self.commands_layout = QHBoxLayout()
    
        self.button_normal = QPushButton("0x00")
        self.commands_layout.addWidget(self.button_normal)
        self.button_normal.setEnabled(False)
        # Connects clicked signal to the handler
        self.button_normal.clicked.connect(self.normal_handler)
        
        self.button_adjust = QPushButton("0x01")
        self.commands_layout.addWidget(self.button_adjust)
        self.button_adjust.setEnabled(False)
        self.button_adjust.clicked.connect(self.adjust_handler)
        
        self.button_confirm = QPushButton("0x05")
        self.commands_layout.addWidget(self.button_confirm)
        self.button_confirm.setEnabled(False)
        self.button_confirm.clicked.connect(self.confirm_handler)
        
        self.button_cycle_enable = QPushButton("0xE0")
        self.commands_layout.addWidget(self.button_cycle_enable)
        self.button_cycle_enable.setEnabled(False)
        self.button_cycle_enable.clicked.connect(self.cycle_enable_handler)
        
        self.button_cycle_abort = QPushButton("0xE1")
        self.commands_layout.addWidget(self.button_cycle_abort)
        self.button_cycle_abort.setEnabled(False)
        self.button_cycle_abort.clicked.connect(self.cycle_abort_handler)
        
        self.commands_group.setLayout(self.commands_layout)
    
    # Returns how many power suppliers are connected
    def get_boards_count(self):
        
        return len(self.board_source_windows_list)
    
    # Returns how many power suppliers are connected
    def get_boards_list(self):
        
        return self.board_source_windows_list
    
    # Resets lists
    def build_source_windows(self):
        
        # board_source_windows_list is a list object which contains all references
        # to the SourceWindow objects. board_buttons_list contains the references to
        # the buttons which open the respective window 
        
        self.board_source_windows_list = list()
        self.board_buttons_list = list()
        
        # Support for a pre-built configuration (command IDENT not necessary)
        #for i in board_list:
        #    self.board_source_windows_list.append(SourceWindow(parent = self, board = i))
        #    button = QPushButton(i.getTitle())
        #    button.clicked.connect (self.source_handler)
        #    self.board_buttons_list.append(button)

    # Adds a button to the board_buttons_list and associates it to a SourceWindow object
    def add_board_source_group(self, id):
               
        self.board_source_windows_list.append(SourceWindow(parent = self, board = Board(title='Board #%d' % id, color = QColor(randint(0, 255), randint(0, 255), randint(0, 255) ), order = id)))
        button = QPushButton('Board #%d' % id)
        button.clicked.connect (self.source_handler)
        self.board_buttons_list.append(button)
        
        self.sources_layout.addWidget(button)
    
    # Resets and deletes board_source_windows_list and board_buttons_list
    def clear_source_group (self):
        
        # Resets entire source group. Clears buttons and layout
        for b in self.board_buttons_list:
            b.deleteLater()
            #self.sources_layout.removeWidget(b)
            
        self.board_buttons_list = []  # Clears button list
        
        for b in self.board_source_windows_list:
            
            # if useCanvas is True, close canvas. Such operation must be made explicitly,
            # considering that matplotlib does not handle it.
            if b.useCanvas:
                b.canvas.close()
                     
            b.close()
        
        self.board_source_windows_list = []
        
    # Updates buttons in the case of a disconnection
    def set_disconnected_state (self):
               
        self.sources_group.setVisible(False)
        
        self.button_normal.setEnabled(False)
        self.button_adjust.setEnabled(False)
        self.button_cycle_enable.setEnabled(False)
        self.button_cycle_abort.setEnabled(False)
        self.button_confirm.setEnabled(False)
        
        self.led_connected.turnOff()
                
        self.connect_button.setText("Connect")
    
    # Updates buttons in the case of a connection    
    def set_connected_state (self):
        
        self.sources_group.setVisible(True)
        
        self.button_normal.setEnabled(True)
        self.button_adjust.setEnabled(True)
        self.button_cycle_enable.setEnabled(True)
        self.button_cycle_abort.setEnabled(False)
        self.button_confirm.setEnabled(True)
        
        self.led_connected.turnOn()
        
        self.connect_button.setText("Disconnect")
        
    def set_requester_controller(self, requester):
        
        self.requester_controller = requester

    ####### HANDLERS section #######
    
    # Handles a connection / disconnection request
    def connection_handler(self):
        
        if not self.requester_controller.isConnected :
        
            port_n = int(self.port_line.text())
            
            self.led_connected.turnAlmostOn()
            
            self.statusBar().clearMessage()
            
            self.statusBar().showMessage('Trying to connect to %s through port %d' % (self.connect_line.text(), port_n))
            
            self.update()
            
            # Tries to connect to the PROSAC
            connected, message = self.requester_controller.connect(ip_address = self.connect_line.text(), port = port_n)
            
            self.statusBar().showMessage('%s' % message)
                    
            if connected:
                
                self.set_connected_state()
                                            
                # Request information about how many boards are connected (IDENT command)
                self.requester_controller.execute_command(0x02)
                self.requester_controller.execute_command(0x03)
                
                
                self.requester_controller.can_ask = True
            
            # If connection request was not successful, reset all lists 
            # and boards
            else: 
                
                self.requester_controller.isConnected = False
                self.clear_source_group()
                self.set_disconnected_state()
        
        # Disconnection request: reset all lists and updates buttons
        else :
            
            self.requester_controller.disconnect()
            
            self.clear_source_group()
            
            self.set_disconnected_state()
                        
            self.statusBar().showMessage('Client disconnected.')            
            
    # Sends a 0x00 command
    def normal_handler (self):
    
        self.requester_controller.execute_command(0x00)
    
    # Sends a 0x01 command
    def adjust_handler(self):
        
        self.requester_controller.execute_command(0x01)
    
    # Sends a 0x05 command
    def confirm_handler(self):
        
        self.requester_controller.execute_command(0x05)

    # Sends a 0xE0 command and updates states of buttons
    def cycle_enable_handler(self):
        
        self.requester_controller.execute_command(0xE0)
        
        self.button_normal.setEnabled(False)
        self.button_adjust.setEnabled(False)
        self.button_cycle_enable.setEnabled(False)
        self.button_cycle_abort.setEnabled(True)
        self.button_confirm.setEnabled(False)
    
    # Sends a 0xE1 command and updates states of buttons
    def cycle_abort_handler(self):
        
        self.requester_controller.execute_command(0xE1)
        
        self.button_normal.setEnabled(True)
        self.button_adjust.setEnabled(True)
        self.button_cycle_enable.setEnabled(True)
        self.button_cycle_abort.setEnabled(False)
        self.button_confirm.setEnabled(True)
        
    # Handles push-button event and shows the associated SourceWindow object 
    def source_handler(self):
        
        sender = self.sender()
        j = 0
        
        for i in self.board_buttons_list:
            
            if sender == i:
                break
            
            j = j + 1
            
        self.board_source_windows_list[j].show()
        
        self.statusBar().showMessage(sender.text() + ' was pressed')
    
    # Update flag in order to reader thread to acknowledge it 
    def closeEvent(self, *args, **kwargs):
        
        self.is_closed = 1
        
        return QMainWindow.closeEvent(self, *args, **kwargs)
    
    # Updates all SourceWindow objects 
    def paintEvent(self, *args, **kwargs):
        
        for b in self.board_source_windows_list:
            b.update()
        
        return QMainWindow.paintEvent(self, *args, **kwargs)
Example #7
0
class PropertyEditor(QWidget):
    # Signals
    insertionModeStarted = pyqtSignal(str, str, str)
    insertionModeEnded = pyqtSignal()
    insertionPropertiesChanged = pyqtSignal(object)
    editPropertiesChanged = pyqtSignal(object)
    checkboxStateChangedSignal = pyqtSignal(str, object)
    _optionsCheckboxStateChangedSignal = pyqtSignal(str, object)

    def setAnnotationScene(self, annotationScene):
        self._scene = annotationScene

    def setOptionStateChangedSignalSlot(self, checkboxStateChangedSignalSlot):
        if checkboxStateChangedSignalSlot is not None:
            self.checkboxStateChangedSignal.connect(
                checkboxStateChangedSignalSlot)
        self._optionsCheckboxStateChangedSignal.connect(
            self.onInserterButtonOptionChanged)

    # set enable status for the checkbox group which is attached to inserterButton
    def enableCheckboxGroup(self, inserterButtonName, enabled=True):
        widgetsInfo = self._widgets_dict.get(inserterButtonName, None)
        if not widgetsInfo: return
        if widgetsInfo[1]:
            # this button has an attachedCheckboxGroup
            attachedCheckboxGroupWidget = widgetsInfo[1]
            attachedCheckboxGroupWidget.enableAll(enabled)

    # set check status for one specific checkbox which is attached to inserterButton
    def setCheckedCheckbox(self,
                           inserterButtonName,
                           checkBoxName,
                           checked=True):
        widgetsInfo = self._widgets_dict.get(inserterButtonName, None)
        # print "inserterButtonName {} _widgets_dict {} checkBoxName {} checked {} ...".format(inserterButtonName, self._widgets_dict, checkBoxName, checked)
        if not widgetsInfo: return
        if widgetsInfo[1]:
            # this button has an attachedCheckboxGroup
            attachedCheckboxGroupWidget = widgetsInfo[1]
            attachedCheckboxGroupWidget.setCheckedCheckbox(
                checkBoxName, checked)

    # set check status for some specific checkboxs which are attached to inserterButton
    def setCheckedCheckboxs(self,
                            inserterButtonName,
                            checkBoxNamesList,
                            checked=True):
        widgetsInfo = self._widgets_dict.get(inserterButtonName, None)
        if not widgetsInfo: return
        if widgetsInfo[1]:
            # this button has an attachedCheckboxGroup
            attachedCheckboxGroupWidget = widgetsInfo[1]
            attachedCheckboxGroupWidget.setCheckedCheckboxs(
                checkBoxNamesList, checked)

    # get names list of all checkboxs in the checkbox group which is attached to inserterButton
    def getCheckboxsName(self, inserterButtonName, enabled=True):
        widgetsInfo = self._widgets_dict.get(inserterButtonName, None)
        if not widgetsInfo: return
        if widgetsInfo[1]:
            # this button has an attachedCheckboxGroup
            attachedCheckboxGroupWidget = widgetsInfo[1]
            return attachedCheckboxGroupWidget.get_checkboxs_name()
        return None

    def __init__(self, config, idName, displayName, groupBoxName, parent=None):
        QWidget.__init__(self, parent)
        self._inserters_modelitems = {
        }  # dict. { inserter_class_name : AnnotationModelItem, ...}. Note that, inserter_class_name is not always equal to inserter_button_name, for those buttons with attached options.
        self._inserters_guiitems = {
        }  # dict. { inserter_class_name : (inserter_creator_method, inserted_item_creator_method) }
        self._idName = idName
        self._groupBoxName = groupBoxName
        self._displayName = displayName
        self._scene = None
        self._current_toinsert_inserterClassName = None
        self._current_is_insert_mode = False
        self._widgets_dict = {
        }  # dict. { inserter_button_name :[buttonWidget, buttonAttachedOptionsWidget, buttonName], ....}

        self._setupGUI(self._groupBoxName)

        # Add label classes from config
        for buttonConfig in config:
            self.addButton(buttonConfig)

        # print "self._inserters_guiitems = {}".format(self._inserters_guiitems)

    def onModelChanged(self, new_model):
        pass

    def addButton(self, buttonConfig):
        # LOG.info("addLabelClass with buttonConfig {} ...".format(buttonConfig))
        # Check label configuration
        if 'attributes' not in buttonConfig:
            raise ImproperlyConfigured("Label with no 'attributes' dict found")

        inserter_creator_method = buttonConfig['inserter']
        inserted_item_creator_method = buttonConfig['item']

        attrs = buttonConfig['attributes']
        # LOG.info("buttonConfig['attributes'] {} type {} ...".format(buttonConfig['attributes'], type(buttonConfig['attributes'])))
        if config.METADATA_LABELCLASS_TOKEN not in attrs:
            raise ImproperlyConfigured(
                "Labels must have an attribute config.METADATA_LABELCLASS_TOKEN"
            )
        label_class = attrs[config.METADATA_LABELCLASS_TOKEN]
        # LOG.info("buttonConfig['attributes'][config.METADATA_LABELCLASS_TOKEN] {} type {} ...".format(attrs[config.METADATA_LABELCLASS_TOKEN], type(attrs[config.METADATA_LABELCLASS_TOKEN])))
        if label_class in self._inserters_modelitems:
            raise ImproperlyConfigured(
                "Label with class '%s' defined more than once" % label_class)

        # Add INSERTER button
        displaytext = attrs['displaytext']
        buttonName = label_class
        button = QPushButton(displaytext, self)

        optionInfo = attrs.get('optioninfo', None)
        # print "button {}: option {}".format(buttonName, optionInfo)
        buttonOptionsWidget = None
        buttonDisplayColor = None
        tmp = [
            o.get(default_config.METADATA_DISPLAYCOLOR_TOKEN, None)
            for o in optionInfo['option']
            if o.get(config.METADATA_IS_DEFAULT_TOKEN, False)
        ][0] if optionInfo else None
        buttonDisplayColor = tmp if tmp else optionInfo['option'][0].get(
            default_config.METADATA_DISPLAYCOLOR_TOKEN,
            None) if optionInfo else attrs.get(
                default_config.METADATA_DISPLAYCOLOR_TOKEN, None)

        # LOG.info(u"buttonConfig['attributes'] = {}, displaytext = {}, displayColor = {}".format(attrs, displaytext, buttonDisplayColor))

        # ==== zx add @ 20161114 to display button with color configured by user ====
        txtColor = None
        if buttonDisplayColor is not None:
            qtColor, hexColor = utils.getColorDesc(buttonDisplayColor)
            rgba = utils.hexColorStrToRGBA(hexColor)
            distance = math.sqrt((rgba[0] - 255)**2 + (rgba[1] - 255)**2 +
                                 (rgba[2] - 255)**2)
            txtColor = '#000000' if distance > config.GUI_COLOR_TAG_TEXT_BLACKWHITE_TOGGLE_THRESHOLD else '#ffffff'
            buttonDisplayColor = hexColor[0:8]
            # LOG.info(u"buttonDisplayColor = {} txtColor = {}, qtColor = {} hexColor = {}".format(buttonDisplayColor, txtColor, qtColor, hexColor ))
            # print (u"buttonDisplayColor = {} txtColor = {}, qtColor = {} hexColor = {}".format(buttonDisplayColor, txtColor, qtColor, hexColor ))

        # print "button {} buttonDisplayColor {} ...".format(buttonName, buttonDisplayColor)
        utils.set_qobj_stylesheet(
            button,
            'QPushButton',
            widgetBackgroundColor=None,
            widgetTextColor=None,
            widgetBackgroundColorWhenChecked=buttonDisplayColor,
            widgetTextColorWhenChecked=txtColor)
        # ========================== zx add end ============================

        button.clicked.connect(bind(self.onClassButtonPressed, label_class))
        # Add hotkey
        if 'hotkey' in buttonConfig:
            hotkey = QShortcut(QKeySequence(buttonConfig['hotkey']), self)
            hotkey.activated.connect(button.click)
            self._class_shortcuts[label_class] = hotkey
            # print "{} is set hotkey {} {}".format(label_class, buttonConfig['hotkey'], hotkey)

        if optionInfo:
            optionListName = optionInfo['name']
            optionListText = optionInfo['displaytext']
            option = optionInfo['option']
            buttonOptionsWidget = AttachedCheckboxGroupWidget(
                buttonName,
                optionListName,
                optionListText,
                True,
                option,
                self._optionsCheckboxStateChangedSignal,
                parent=None)

            isDefaultOption = False
            for o in option:
                new_class = o.get('tag', None)
                if new_class:
                    # Add prototype mdoelItem for insertion
                    mi = {config.METADATA_LABELCLASS_TOKEN: new_class}
                    mi['displaytext'] = o.get('displaytext', new_class)

                    self._inserters_modelitems[
                        new_class] = AnnotationModelItem(mi)
                    self._inserters_guiitems[new_class] = (
                        inserter_creator_method, inserted_item_creator_method)
                    # print "addButton.....self._inserters_guiitems[{}] = {}".format(new_class, (inserter_creator_method, inserted_item_creator_method))
                    for key, val in o.iteritems():
                        if key != 'tag':
                            self._inserters_modelitems[new_class][key] = val

        else:
            attrs = buttonConfig['attributes']

            # Add prototype mdoelItem for insertion
            mi = {config.METADATA_LABELCLASS_TOKEN: label_class}
            mi['displaytext'] = attrs.get('displaytext', label_class)

            self._inserters_modelitems[label_class] = AnnotationModelItem(mi)
            self._inserters_guiitems[label_class] = (
                inserter_creator_method, inserted_item_creator_method)

            # update their values
            for key, val in attrs.iteritems():
                self._inserters_modelitems[label_class][key] = val
                # LOG.info("self._inserters_modelitems[{}][{}] = {}".format(label_class, key, val))

        self._widgets_dict[label_class] = [button, buttonOptionsWidget]
        # print "self._widgets_dict [ {} ] = {}".format(label_class, button)

        button.setCheckable(True)

        utils.set_qobj_stylesheet(
            self._widgets_dict[label_class][0],
            'QPushButton',
            widgetBackgroundColor=None,
            widgetTextColor=None,
            widgetBackgroundColorWhenChecked=buttonDisplayColor,
            widgetTextColorWhenChecked=txtColor)

        if buttonOptionsWidget:
            # self._layout.addWidget(button)
            self._inserterButtonGroup_layout.addWidget(button)
            self._layout.addWidget(buttonOptionsWidget)
        else:
            self._inserterButtonGroup_layout.addWidget(button)

    def onClassButtonPressed(self, pressedButtonName):
        # print "onClassButtonPressed ... button {} isChecked !".format(pressedButtonName)
        inserterClassName = pressedButtonName

        if pressedButtonName not in self._widgets_dict.keys():
            # check whether passed-in pressedButtonName argument is an checkbox option name
            for kk, vv in self._widgets_dict.iteritems():
                buttonOptionsWidget = vv[1] if vv else None
                if buttonOptionsWidget:
                    optionsName = buttonOptionsWidget.get_checkboxs_name()
                    if pressedButtonName in optionsName:
                        pressedButtonName = kk
                        # print "pressedButtonName ============ {}".format(kk)

        if self._widgets_dict[pressedButtonName][0].isChecked():
            if not self._scene._image_item:
                self._widgets_dict[pressedButtonName][0].setChecked(False)
                return

            checkedOption = None
            if self._widgets_dict[pressedButtonName][1]:
                buttonOptionsWidget = self._widgets_dict[pressedButtonName][1]
                checkedOptionsNameList = buttonOptionsWidget.get_checked_widgets_name(
                )
                if not checkedOptionsNameList:
                    checkedOptionsNameList = [
                        buttonOptionsWidget._defaultCheckboxName
                    ]
                    buttonOptionsWidget.setCheckedCheckbox(
                        buttonOptionsWidget._defaultCheckboxName)

                buttonOptionsWidget.enableAll()
                checkedOptionName = checkedOptionsNameList[0]

                orgClsNames = [
                    i for i in buttonOptionsWidget.get_checkboxs_name()
                    if i in self._inserters_modelitems.keys()
                ]
                if (not orgClsNames):
                    raise RuntimeError(
                        "There are no or more than one inserters")
                inserterClassName = checkedOptionName

            if ((not self._scene._labeltool._enableAutoConnectLabelMode)
                    and (self._scene._selectedDisplayItemsList is not None)
                    and (len(self._scene._selectedDisplayItemsList) == 1)):
                parentModelItem = self._scene._selectedDisplayItemsList[0][
                    annotationscene.
                    SELECTED_DISPLAYED_ITEMS_LIST_MODELITEM_INDEX]
                clsNameOfChild = inserterClassName
                if ((clsNameOfChild == config.ANNOTATION_PERSONBIKE_TOKEN) or
                    (clsNameOfChild == config.ANNOTATION_PEDESTRAIN_TOKEN) or
                    (clsNameOfChild == config.ANNOTATION_VEHICLE_TOKEN)):
                    self._scene._selectedDisplayItemsList = []
                    parentModelItem = None

            elif (self._scene._sceneViewMode == config.OBJ_VIEW_MODE) and (
                    self._scene._objViewModeTopModelItem is not None):
                parentModelItem = self._scene._objViewModeTopModelItem
            else:
                parentModelItem = None

            LOG.info(
                "onClassButtonPressed ... self._scene._labeltool._enableAutoConnectLabelMode = {} parentModelItem = {}"
                .format(self._scene._labeltool._enableAutoConnectLabelMode,
                        parentModelItem))
            if not self._scene._labeltool._enableAutoConnectLabelMode:

                clsNameOfChild = inserterClassName
                isValid, rectBoundary = self._scene.checkModelItemValidity(
                    clsNameOfChild,
                    None,
                    parentModelItem,
                    self._scene._image_item,
                    enablePopupMsgBox=True,
                    enableEmitStatusMsg=False,
                    enableCountingThisModelItem=True)

                if not isValid:
                    LOG.info("enter hhhhhhhhhhhhhhh....")

                    # --------------------------------------
                    # added by zx @ 2017-02-08
                    # to exit all inserters among all pannels
                    # --------------------------------------
                    for i in self._scene._labeltool.propertyeditors():
                        if i:
                            i.setCheckedAll(False)
                    # self._scene._labeltool.exitInsertMode()
                    self._scene.deselectAllItems()
                    # --------------------------------------

                    return

            LOG.info("onClassButtonPressed ... call startInsertionMode...")

            self.startInsertionMode(pressedButtonName, inserterClassName)

        else:

            LOG.info("onClassButtonPressed ... call endInsertionMode...")
            self.endInsertionMode()

        return

    def startInsertionMode(self, pressedButtonName, inserterClassName):
        self.endInsertionMode(False)
        LOG.info(
            "Starting insertion mode for {} .. self._inserters_modelitems[{}]={} "
            .format(inserterClassName, inserterClassName,
                    self._inserters_modelitems[inserterClassName]))

        for lc, buttonAndOption in self._widgets_dict.items():
            buttonAndOption[0].setChecked(lc == pressedButtonName)
            if buttonAndOption[1]:
                # print ("startInsertionMode .. setchecked for {} option {} enabled = {} ".format(lc, buttonAndOption[1], (lc == pressedButtonName)))
                buttonAndOption[1].enableAll(lc == pressedButtonName)

            LOG.info(
                "startInsertionMode .. setchecked for {} button checked = {} ".
                format(lc, lc == inserterClassName))
            # print ("startInsertionMode .. setchecked for {} button {} checked = {} ".format(lc, buttonAndOption[0], lc == inserterClassName))

        self._current_toinsert_inserterClassName = inserterClassName
        # print "==== startInsertionMode set _current_is_insert_mode False ..."
        self._current_is_insert_mode = True

        LOG.info(
            u"startInsertionMode .. emit insertionModeStarted(pannel = {}, inserter = {})... "
            .format(self._idName, inserterClassName))

        self.insertionModeStarted.emit(self._idName, inserterClassName,
                                       inserterClassName)

    def endInsertionMode(self, uncheck_buttons=True):
        if uncheck_buttons:
            self.setCheckedAll(False)

        LOG.info(
            u"endInsertionMode... PropertyEditor {} endInsertionMode(uncheck_buttons = {})"
            .format(self._displayName, uncheck_buttons))
        # print (u"endInsertionMode... PropertyEditor {} endInsertionMode(uncheck_buttons = {})".format(self._displayName, uncheck_buttons))
        self._current_is_insert_mode = False
        self.insertionModeEnded.emit()

    def enableAll(self, enabled=True):
        for v, buttonAndOption in self._widgets_dict.items():
            buttonAndOption[0].setEnabled(enabled)

    def setCheckedAll(self, checked=True):
        for v, buttonAndOption in self._widgets_dict.items():
            buttonAndOption[0].setChecked(checked)

    def getChecked(self):
        buttonname = None
        for buttonName, buttonWidgets in self._widgets_dict.iteritems():
            if buttonWidgets[0].isChecked():
                return buttonName
        return buttonname

    def setChecked(self, buttonName, checked=True):
        buttonWidget = self._widgets_dict.get(buttonName, None)
        if buttonWidget is not None:
            buttonWidget[0].setChecked(checked)

    def enable(self, buttonName, enabled=True):
        buttonWidget = self._widgets_dict.get(buttonName, None)
        if buttonWidget is not None:
            buttonWidget[0].setEnabled(enabled)

    def markEditButtons(self, buttonNamesList):
        for lc, buttonAndOption in self._widgets_dict.items():
            # buttonAndOption[0].setFlat(lc not in buttonNamesList)
            buttonAndOption[0].setChecked(lc in buttonNamesList)

    def updateCurrentInserter(self, inserterClassName):
        self._current_toinsert_inserterClassName = inserterClassName
        return

    def isInsertingMode(self):
        return self._current_is_insert_mode

    def currentToInsertItemProperty(self):
        return self._inserters_modelitems.get(
            self._current_toinsert_inserterClassName,
            {}) if self._current_toinsert_inserterClassName else {}

    # return [inserter0ClassName, inserter1ClassName, ...]
    def getSupportedInserters(self):
        return self._inserters_modelitems.keys()

    def startEditMode(self, model_items):
        # If we're in insertion mode, ignore empty edit requests
        if self._current_is_insert_mode and len(model_items) == 0:
            return

        self.endInsertionMode()
        LOG.info("Starting edit mode for model_items: {} ".format(model_items))
        self._current_toinsert_inserterClassName = None
        # print "==== startEditMode set _current_is_insert_mode False ..."
        self._current_is_insert_mode = False

        labelClasses = set([
            item[config.METADATA_LABELCLASS_TOKEN] for item in model_items
            if config.METADATA_LABELCLASS_TOKEN in item
        ])
        self.markEditButtons(labelClasses)

    def _setupGUI(self, _groupBoxName):
        self._widgets_dict = {}
        self._class_shortcuts = {}

        # Label class buttons
        qss = "QGroupBox::title {subcontrol-origin: margin; subcontrol-position: top left; padding: 0 3px; color : red; font-weight:bold; }"
        self._inserterButtonGroupbox = QGroupBox(_groupBoxName, self)
        self._inserterButtonGroupbox.setStyleSheet(qss)
        self._inserterButtonGroup_layout = FloatingLayout()
        self._inserterButtonGroupbox.setLayout(
            self._inserterButtonGroup_layout)

        # Global widget
        self._layout = MyVBoxLayout()
        self.setLayout(self._layout)
        self._layout.addWidget(self._inserterButtonGroupbox, 0)
        self._layout.addStretch(1)

    def onInserterButtonOptionChanged(self, _inserterButtonName,
                                      checkboxsInfo):
        # print "onInserterButtonOptionChanged {}: inserterButton {} checkboxinfo {}".format(self._idName, _inserterButtonName, checkboxsInfo)
        if not checkboxsInfo:
            return
        if len(checkboxsInfo) != 2:
            return

        inserterButtonName = str(_inserterButtonName)

        checkedWidgetsAttrDescDict = checkboxsInfo[0]
        allCheckboxsNameList = checkboxsInfo[1]

        if len(checkedWidgetsAttrDescDict) != 1:
            return

        checkedOptionName = checkedWidgetsAttrDescDict.keys()[0]

        if (self._current_toinsert_inserterClassName == checkedOptionName):
            return

        checkedOptionDisplayText = checkedWidgetsAttrDescDict.values()[0][
            attrArea.checkedWidgetsAttrDescList_checkedWidgetText_idx]
        checkedOptionDisplayColor = checkedWidgetsAttrDescDict.values()[0][
            attrArea.checkedWidgetsAttrDescList_checkedWidgetColor_idx]

        # change inserter button displaycolor
        # print "--- self._current_is_insert_mode = {} self._widgets_dict[{}][0].is_checked() = {} ...".format(self._current_is_insert_mode, inserterButtonName, self._widgets_dict[inserterButtonName][0].isChecked())
        utils.set_qobj_stylesheet(
            self._widgets_dict[inserterButtonName][0],
            'QPushButton',
            widgetBackgroundColor=None,
            widgetTextColor=None,
            widgetBackgroundColorWhenChecked=checkedOptionDisplayColor,
            widgetTextColorWhenChecked=None)

        if not self._current_is_insert_mode or self._scene._sceneViewMode == config.OBJ_VIEW_MODE:
            parentModelItem = None

            if self._scene._sceneViewMode == config.OBJ_VIEW_MODE:
                # print "_sceneViewMode = {} _objViewModeTopModelItem = {}".format(self._scene._sceneViewMode, self._scene._objViewModeTopModelItem)
                parentModelItem = self._scene._objViewModeTopModelItem

            if not parentModelItem:
                # get current scene's parent modelitem
                if len(self._scene._selectedDisplayItemsList) > 0:
                    parentModelItem = self._scene._selectedDisplayItemsList[0][
                        annotationscene.
                        SELECTED_DISPLAYED_ITEMS_LIST_MODELITEM_INDEX]
                    # print "_selectedDisplayItemsList = {}".format(self._scene._selectedDisplayItemsList)

            if not parentModelItem:
                parentModelItem = self._scene._labeltool._current_image

            # print ("checkedOptionName = {} parentModelItem = {}".format(checkedOptionName, GET_ANNOTATION(parentModelItem)))
            # print ("allCheckboxsNameList = {}".format((allCheckboxsNameList)))

            # find all modelItems which has class which in is the option list of changed inserter option but not has not that class of option
            foundNum = 0
            foundListList = []
            for a in allCheckboxsNameList:
                foundList = model.findModelItemsWithSpecificClassNameFromRoot(
                    a, parentModelItem, 5)
                # print "find {} => {}".format(a, foundList)
                foundNum += len(foundList)  # if a != checkedOptionName else 0
                maxNum = 1
                if foundNum > maxNum:
                    print "Error: there are {} items of {} in current image! We can only modify at least {} item once!".format(
                        foundNum, allCheckboxsNameList, maxNum)
                    foundListList = []
                    break
                if a != checkedOptionName:
                    foundListList += foundList

            # print ("foundListList = {}".format((foundListList)))
            # update found modelitems' class field to current selected option
            if foundListList:
                for found in foundListList:
                    if not found: continue
                    modelItem = found[0]
                    iterativeLevelNum = found[1]
                    # print "ModelItem is changed from {} => {}!".format(modelItem.get_label_name(), checkedOptionName)
                    modelItem.update({'class': checkedOptionName},
                                     needEmitDatachanged=True)
                    sceneItem = self._scene.modelItem_to_baseItem(modelItem)

                    # print u"checkedOptionDisplayText = {} checkedOptionDisplayColor = {}".format(checkedOptionDisplayText, checkedOptionDisplayColor)
                    sceneItem.updateInfo(checkedOptionDisplayText,
                                         checkedOptionDisplayColor)

                    # newModelItem = copy.deepcopy(oldModelItem)
                    # self._scene.deleteItems(self, [oldModelItem], recursiuve=True)
        else:

            # switch to new inserter as per current selected inserter button type
            # print "---------------- _current_is_insert_mode = {} ...".format(self._current_is_insert_mode)
            # print "call onInsertionModeStarted with {} {}...".format(checkedOptionName, inserterButtonName)
            self.updateCurrentInserter(checkedOptionName)
            self._scene.onInsertionModeStarted(self._idName, checkedOptionName,
                                               inserterButtonName)

        return

    def onInsertionModeStarted(self, _classNameToInsert,
                               _buttonNameOfInserter):
        buttonNameOfInserter = str(_buttonNameOfInserter)
        self.setChecked(buttonNameOfInserter, True)
        self._current_is_insert_mode = True
        self.updateCurrentInserter(str(_classNameToInsert))
        return
Example #8
0
File: ui.py Project: Xifax/ransukan
class GUI(QWidget):

    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)

        self.create_ui_components()
        self.compose_ui()

        # Initializing: window composition, it's contents and event handlers
        self.init_composition()
        self.init_contents()
        self.init_actions()

        self.on_start()

    def create_ui_components(self):
        """
        Create layouts and qt controls.
        """
        self.layout = QGridLayout()

        self.kanjiGroup = QGroupBox()
        self.kanjiLayout = QGridLayout()

        # Kanji ui group
        self.day, self.week, self.month, self.year = \
        QLabel(KANJI), QLabel(KANJI), QLabel(KANJI), QLabel(KANJI)

        self.dayLabel, self.weekLabel, self.monthLabel, self.yearLabel = \
        QLabel('<b>Day</b>'), QLabel('<b>Week</b>'), \
        QLabel('<b>Month</b>'), QLabel('<b>Year</b>')

        # Main layout
        self.showAbout = QPushButton('A&bout')
        # DB controls (top)
        self.showDB, self.availableDB, self.changeDB = \
        QPushButton('&Change DB (active:)'), QComboBox(), QPushButton('&Remap')
        # General controls (bottom)
        self.getAll, self.showStats, self.quitApp, self.authGen, self.methodCombo = \
        QPushButton('&Get all'), QPushButton('&Stats'), QPushButton('&Quit'), \
        QPushButton('&Auth'), QComboBox()
        # Notifications
        self.progressBar = QProgressBar()
        self.statusMessage = QLabel()
        # About
        self.aboutBox = QMessageBox()

    def compose_ui(self):
        """
        Fill layouts and groups, initialize filters.
        """
        self.kanjiLayout.addWidget(self.day, 0, 0)
        self.kanjiLayout.addWidget(self.week, 0, 1)
        self.kanjiLayout.addWidget(self.dayLabel, 1, 0)
        self.kanjiLayout.addWidget(self.weekLabel, 1, 1)

        self.kanjiLayout.addWidget(self.month, 2, 0)
        self.kanjiLayout.addWidget(self.year, 2, 1)
        self.kanjiLayout.addWidget(self.monthLabel, 3, 0)
        self.kanjiLayout.addWidget(self.yearLabel, 3, 1)

        self.kanjiGroup.setLayout(self.kanjiLayout)

        self.layout.addWidget(self.showDB, 0, 0, 1, 2)
        self.layout.addWidget(self.availableDB, 1, 0)
        self.layout.addWidget(self.changeDB, 1, 1)
        self.layout.addWidget(self.kanjiGroup, 2, 0, 1, 2)
        self.layout.addWidget(self.getAll, 3, 0)
        self.layout.addWidget(self.showStats, 3, 1)
        self.layout.addWidget(self.methodCombo, 4, 0)
        self.layout.addWidget(self.authGen, 4, 1)
        #self.layout.addWidget(self.quitApp, 5, 0, 1, 2)
        self.layout.addWidget(self.quitApp, 5, 0)
        self.layout.addWidget(self.showAbout, 5, 1)
        self.layout.addWidget(self.progressBar, 6, 0, 1, 2)
        self.layout.addWidget(self.statusMessage, 7, 0, 1, 2)

        self.setLayout(self.layout)

        self.eFilter = LabelEventFilter()

    def on_start(self):
        """
        Additional procedures run on application start.
        """
        # Let's initialize even some stuff!
        self.al = None
        self.auth_thread = None
        self.init_backend()

        choose_db(str(self.availableDB.currentText()))
        self.showDB.setText("&Change DB (active: %s)" % self.availableDB.currentText())

        self.stats = StatsUI(self.al, self)

    def init_composition(self):
        """
        Window composition and general params.
        """
        self.setWindowTitle(NAME + ' ' + __version__)
        desktop = QApplication.desktop()
        self.setGeometry((desktop.width() - WIDTH)/2,
                        (desktop.height() - HEIGHT)/2, WIDTH, HEIGHT)

    def init_contents(self):
        """
        Setting up qt controls.
        """
        self.changeDB.hide()
        self.availableDB.hide()
        self.availableDB.addItems(dbs.keys())

        self.kanjiGroup.setAlignment(Qt.AlignCenter)
        self.kanjiGroup.setStyleSheet("QGroupBox { border: 1px solid gray; border-radius: 3px; }")

        self.day.setAlignment(Qt.AlignCenter)
        self.week.setAlignment(Qt.AlignCenter)
        self.month.setAlignment(Qt.AlignCenter)
        self.year.setAlignment(Qt.AlignCenter)
        self.dayLabel.setAlignment(Qt.AlignCenter)
        self.weekLabel.setAlignment(Qt.AlignCenter)
        self.monthLabel.setAlignment(Qt.AlignCenter)
        self.yearLabel.setAlignment(Qt.AlignCenter)

        self.day.setFont(QFont(PRETTY_FONT, KANJI_SIZE))
        self.week.setFont(QFont(PRETTY_FONT, KANJI_SIZE))
        self.month.setFont(QFont(PRETTY_FONT, KANJI_SIZE))
        self.year.setFont(QFont(PRETTY_FONT, KANJI_SIZE))

        self.methodCombo.addItems(RandomMess.algs.keys())
        self.methodCombo.setCurrentIndex(1)

        self.statusMessage.setAlignment(Qt.AlignCenter)
        self.statusMessage.hide()
        self.statusMessage.setMaximumHeight(MESSAGE_HEIGHT)
        self.statusMessage.setStyleSheet(WARNING_STYLE)

        self.progressBar.setMaximum(0)
        self.progressBar.setMaximumHeight(PROGRESS_HEIGHT)
        self.progressBar.hide()

        QToolTip.setFont(QFont(PRETTY_FONT, TOOLTIP_FONT_SIZE))

        self.getAll.setToolTip('Randomly select all 4 kanji')
        self.methodCombo.setToolTip('Choose algorithm for randomness')
        self.authGen.setToolTip('Authorize on remote RNG services')
        self.showStats.setToolTip('Show/hide dialog with comprehensive statistics')
        self.quitApp.setToolTip('Close application')
        self.showDB.setToolTip('Show/hide available databases')
        self.availableDB.setToolTip('Available kanji frequency charts db')
        self.changeDB.setToolTip('Pick new kanji from currently selected db')

        # About dialog
        self.aboutBox.layout().itemAt(1).widget().setAlignment(Qt.AlignLeft)

        self.aboutBox.setTextFormat(Qt.RichText)
        self.aboutBox.setText('Version:\t<b>' + __version__ + '</b><br/>Python:\t<b>' + platform.python_version() + '</b>' +
                                '<br/>Platform:\t<b>' + platform.system() + ' ' + platform.release() + '</b>' +
                                '<br/>Author:\t<b>' + __author__ + '</b>' + app_about)
        self.aboutBox.setWindowTitle('About ' + app_name)
        self.aboutBox.setIconPixmap(QPixmap(paths['icon']))

    def init_actions(self):
        """
        Binding events/handlers.
        """
        self.showDB.clicked.connect(self.show_available_db)
        self.changeDB.clicked.connect(self.change_db)

        self.quitApp.clicked.connect(self.close)
        self.getAll.clicked.connect(self.get_all)
        self.authGen.clicked.connect(self.auth_task)
        self.showStats.clicked.connect(self.show_stats)
        self.methodCombo.currentIndexChanged.connect(self.update_alg)

        self.showAbout.clicked.connect(self.app_help)

        # Mouse events for labels
        self.day.setAttribute(Qt.WA_Hover, True)
        self.week.setAttribute(Qt.WA_Hover, True)
        self.month.setAttribute(Qt.WA_Hover, True)
        self.year.setAttribute(Qt.WA_Hover, True)
        self.day.installEventFilter(self.eFilter)
        self.week.installEventFilter(self.eFilter)
        self.month.installEventFilter(self.eFilter)
        self.year.installEventFilter(self.eFilter)

    ##### actions #####

    def show_stats(self):
        if self.stats.isVisible():
            self.stats.hide()
        else:
            self.stats.show()

    def show_available_db(self):
        if self.availableDB.isVisible():
            self.availableDB.hide()
            self.changeDB.hide()
        else:
            self.availableDB.show()
            self.changeDB.show()

    def change_db(self):
        try:
            choose_db(str(self.availableDB.currentText()))
            self.availableDB.hide()
            self.changeDB.hide()
            self.show_message_then_hide("DB successfully remaped!", False)
            self.showDB.setText("&Change DB (active: %s)" % self.availableDB.currentText())

            self.stats.update_stat_info()
            self.stats.refresh_plot()
        except NoDbException as e:
            self.show_message_then_hide(e.message)

    def get_all(self):
        self.random_kanji_task = RandomKanjiTask(self.al)
        self.random_kanji_task.done.connect(self.update_kanji)
        self.show_progress('Selecting kanji...')
        self.random_kanji_task.start()

    def update_kanji(self, results):
        if results['success']:
            kanji_set = results['kanji_set']
            for_a_day = kanji_set.pop()
            for_a_week = kanji_set.pop()
            for_a_month = kanji_set.pop()
            for_a_year = kanji_set.pop()

            self.day.setText(for_a_day.character)
            self.dayLabel.setText('<b>Day:</b> ' + str(for_a_day.frequency) + ' | '
                                        + str(for_a_day.dominance) + '%')
            self.week.setText(for_a_week.character)
            self.weekLabel.setText('<b>Week:</b> ' + str(for_a_week.frequency) + ' | '
                                        + str(for_a_week.dominance) + '%')
            self.month.setText(for_a_month.character)
            self.monthLabel.setText('<b>Month:</b> ' + str(for_a_month.frequency) + ' | '
                                        + str(for_a_month.dominance) + '%')
            self.year.setText(for_a_year.character)
            self.yearLabel.setText('<b>Year:</b> ' + str(for_a_year.frequency) + ' | '
                                        + str(for_a_year.dominance) + '%')

            self.kanji_tooltip(self.day)
            self.kanji_tooltip(self.week)
            self.kanji_tooltip(self.month)
            self.kanji_tooltip(self.year)

            if self.stats.isVisible():
                self.stats.update_stat_info()
                self.stats.refresh_plot()

            self.hide_message()
        else:
            self.show_message_then_hide(results['message'])

        self.hide_progress()

    def pretty_font(self):
        pass

    def update_alg(self):
        self.al.set_active(str(self.methodCombo.currentText()))

    def init_backend(self):
        self.al = RandomMess()
        self.update_alg()

    def auth_task(self):
        self.auth_thread = AuthorizationTask(self.al)
        self.auth_thread.done.connect(self.auth_complete)
        #self.auth_thread.run()
        # IT DOESN't work on windows as it should!
        self.auth_thread.start()
        self.show_progress('Authorizing on RNG services...')

    def auth_complete(self, success):
        self.hide_message()
        self.hide_progress()
        if success:
            self.show_message_then_hide("Successfully authenticated!", False)
        else:
            self.show_message_then_hide("Sorry, could not authenticate.")

    def show_message_then_hide(self, message, error=True):
        if error:
            self.statusMessage.setStyleSheet(WARNING_STYLE)
        else:
            self.statusMessage.setStyleSheet(NOTE_STYLE)

        self.statusMessage.setText(message)
        self.statusMessage.show()
        QTimer.singleShot(MESSAGE_TIMEOUT, self.hide_message)

    def show_progress(self, message):
        self.statusMessage.setStyleSheet(NOTE_STYLE)
        self.statusMessage.setText(message)
        self.statusMessage.show()
        self.progressBar.show()

    def hide_message(self):
        self.statusMessage.setText('')
        self.statusMessage.hide()

    def hide_progress(self):
        self.progressBar.hide()

    def toggle_kanji_info(self, label, info):
        label.setToolTip(info.info())

    def kanji_tooltip(self, label):
        found = JDIC.search(label.text())
        if found:
            label.setToolTip(found.info())
        else:
            label.setToolTip('No such kanji in kanjidic2!')

    def kanji_info(self, kanji):
        pass

    def app_help(self):
        self.aboutBox.show()

        #### Utility events ####

    def resizeEvent(self, QResizeEvent):
        self.updateStatsPosition()
        self.updateStatsSize()

    def moveEvent(self, QMoveEvent):
        self.updateStatsPosition()
        self.updateStatsSize()

    def updateStatsPosition(self):
        self.stats.move(self.x() + self.width() + 20, self.y())

    def updateStatsSize(self):
        self.stats.resize(QSize(self.stats.width(), self.height()))
Example #9
0
class GUI(QWidget):
    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)

        self.create_ui_components()
        self.compose_ui()

        # Initializing: window composition, it's contents and event handlers
        self.init_composition()
        self.init_contents()
        self.init_actions()

        self.on_start()

    def create_ui_components(self):
        """
        Create layouts and qt controls.
        """
        self.layout = QGridLayout()

        self.kanjiGroup = QGroupBox()
        self.kanjiLayout = QGridLayout()

        # Kanji ui group
        self.day, self.week, self.month, self.year = \
        QLabel(KANJI), QLabel(KANJI), QLabel(KANJI), QLabel(KANJI)

        self.dayLabel, self.weekLabel, self.monthLabel, self.yearLabel = \
        QLabel('<b>Day</b>'), QLabel('<b>Week</b>'), \
        QLabel('<b>Month</b>'), QLabel('<b>Year</b>')

        # Main layout
        self.showAbout = QPushButton('A&bout')
        # DB controls (top)
        self.showDB, self.availableDB, self.changeDB = \
        QPushButton('&Change DB (active:)'), QComboBox(), QPushButton('&Remap')
        # General controls (bottom)
        self.getAll, self.showStats, self.quitApp, self.authGen, self.methodCombo = \
        QPushButton('&Get all'), QPushButton('&Stats'), QPushButton('&Quit'), \
        QPushButton('&Auth'), QComboBox()
        # Notifications
        self.progressBar = QProgressBar()
        self.statusMessage = QLabel()
        # About
        self.aboutBox = QMessageBox()

    def compose_ui(self):
        """
        Fill layouts and groups, initialize filters.
        """
        self.kanjiLayout.addWidget(self.day, 0, 0)
        self.kanjiLayout.addWidget(self.week, 0, 1)
        self.kanjiLayout.addWidget(self.dayLabel, 1, 0)
        self.kanjiLayout.addWidget(self.weekLabel, 1, 1)

        self.kanjiLayout.addWidget(self.month, 2, 0)
        self.kanjiLayout.addWidget(self.year, 2, 1)
        self.kanjiLayout.addWidget(self.monthLabel, 3, 0)
        self.kanjiLayout.addWidget(self.yearLabel, 3, 1)

        self.kanjiGroup.setLayout(self.kanjiLayout)

        self.layout.addWidget(self.showDB, 0, 0, 1, 2)
        self.layout.addWidget(self.availableDB, 1, 0)
        self.layout.addWidget(self.changeDB, 1, 1)
        self.layout.addWidget(self.kanjiGroup, 2, 0, 1, 2)
        self.layout.addWidget(self.getAll, 3, 0)
        self.layout.addWidget(self.showStats, 3, 1)
        self.layout.addWidget(self.methodCombo, 4, 0)
        self.layout.addWidget(self.authGen, 4, 1)
        #self.layout.addWidget(self.quitApp, 5, 0, 1, 2)
        self.layout.addWidget(self.quitApp, 5, 0)
        self.layout.addWidget(self.showAbout, 5, 1)
        self.layout.addWidget(self.progressBar, 6, 0, 1, 2)
        self.layout.addWidget(self.statusMessage, 7, 0, 1, 2)

        self.setLayout(self.layout)

        self.eFilter = LabelEventFilter()

    def on_start(self):
        """
        Additional procedures run on application start.
        """
        # Let's initialize even some stuff!
        self.al = None
        self.auth_thread = None
        self.init_backend()

        choose_db(str(self.availableDB.currentText()))
        self.showDB.setText("&Change DB (active: %s)" %
                            self.availableDB.currentText())

        self.stats = StatsUI(self.al, self)

    def init_composition(self):
        """
        Window composition and general params.
        """
        self.setWindowTitle(NAME + ' ' + __version__)
        desktop = QApplication.desktop()
        self.setGeometry((desktop.width() - WIDTH) / 2,
                         (desktop.height() - HEIGHT) / 2, WIDTH, HEIGHT)

    def init_contents(self):
        """
        Setting up qt controls.
        """
        self.changeDB.hide()
        self.availableDB.hide()
        self.availableDB.addItems(dbs.keys())

        self.kanjiGroup.setAlignment(Qt.AlignCenter)
        self.kanjiGroup.setStyleSheet(
            "QGroupBox { border: 1px solid gray; border-radius: 3px; }")

        self.day.setAlignment(Qt.AlignCenter)
        self.week.setAlignment(Qt.AlignCenter)
        self.month.setAlignment(Qt.AlignCenter)
        self.year.setAlignment(Qt.AlignCenter)
        self.dayLabel.setAlignment(Qt.AlignCenter)
        self.weekLabel.setAlignment(Qt.AlignCenter)
        self.monthLabel.setAlignment(Qt.AlignCenter)
        self.yearLabel.setAlignment(Qt.AlignCenter)

        self.day.setFont(QFont(PRETTY_FONT, KANJI_SIZE))
        self.week.setFont(QFont(PRETTY_FONT, KANJI_SIZE))
        self.month.setFont(QFont(PRETTY_FONT, KANJI_SIZE))
        self.year.setFont(QFont(PRETTY_FONT, KANJI_SIZE))

        self.methodCombo.addItems(RandomMess.algs.keys())
        self.methodCombo.setCurrentIndex(1)

        self.statusMessage.setAlignment(Qt.AlignCenter)
        self.statusMessage.hide()
        self.statusMessage.setMaximumHeight(MESSAGE_HEIGHT)
        self.statusMessage.setStyleSheet(WARNING_STYLE)

        self.progressBar.setMaximum(0)
        self.progressBar.setMaximumHeight(PROGRESS_HEIGHT)
        self.progressBar.hide()

        QToolTip.setFont(QFont(PRETTY_FONT, TOOLTIP_FONT_SIZE))

        self.getAll.setToolTip('Randomly select all 4 kanji')
        self.methodCombo.setToolTip('Choose algorithm for randomness')
        self.authGen.setToolTip('Authorize on remote RNG services')
        self.showStats.setToolTip(
            'Show/hide dialog with comprehensive statistics')
        self.quitApp.setToolTip('Close application')
        self.showDB.setToolTip('Show/hide available databases')
        self.availableDB.setToolTip('Available kanji frequency charts db')
        self.changeDB.setToolTip('Pick new kanji from currently selected db')

        # About dialog
        self.aboutBox.layout().itemAt(1).widget().setAlignment(Qt.AlignLeft)

        self.aboutBox.setTextFormat(Qt.RichText)
        self.aboutBox.setText('Version:\t<b>' + __version__ +
                              '</b><br/>Python:\t<b>' +
                              platform.python_version() + '</b>' +
                              '<br/>Platform:\t<b>' + platform.system() + ' ' +
                              platform.release() + '</b>' +
                              '<br/>Author:\t<b>' + __author__ + '</b>' +
                              app_about)
        self.aboutBox.setWindowTitle('About ' + app_name)
        self.aboutBox.setIconPixmap(QPixmap(paths['icon']))

    def init_actions(self):
        """
        Binding events/handlers.
        """
        self.showDB.clicked.connect(self.show_available_db)
        self.changeDB.clicked.connect(self.change_db)

        self.quitApp.clicked.connect(self.close)
        self.getAll.clicked.connect(self.get_all)
        self.authGen.clicked.connect(self.auth_task)
        self.showStats.clicked.connect(self.show_stats)
        self.methodCombo.currentIndexChanged.connect(self.update_alg)

        self.showAbout.clicked.connect(self.app_help)

        # Mouse events for labels
        self.day.setAttribute(Qt.WA_Hover, True)
        self.week.setAttribute(Qt.WA_Hover, True)
        self.month.setAttribute(Qt.WA_Hover, True)
        self.year.setAttribute(Qt.WA_Hover, True)
        self.day.installEventFilter(self.eFilter)
        self.week.installEventFilter(self.eFilter)
        self.month.installEventFilter(self.eFilter)
        self.year.installEventFilter(self.eFilter)

    ##### actions #####

    def show_stats(self):
        if self.stats.isVisible():
            self.stats.hide()
        else:
            self.stats.show()

    def show_available_db(self):
        if self.availableDB.isVisible():
            self.availableDB.hide()
            self.changeDB.hide()
        else:
            self.availableDB.show()
            self.changeDB.show()

    def change_db(self):
        try:
            choose_db(str(self.availableDB.currentText()))
            self.availableDB.hide()
            self.changeDB.hide()
            self.show_message_then_hide("DB successfully remaped!", False)
            self.showDB.setText("&Change DB (active: %s)" %
                                self.availableDB.currentText())

            self.stats.update_stat_info()
            self.stats.refresh_plot()
        except NoDbException as e:
            self.show_message_then_hide(e.message)

    def get_all(self):
        self.random_kanji_task = RandomKanjiTask(self.al)
        self.random_kanji_task.done.connect(self.update_kanji)
        self.show_progress('Selecting kanji...')
        self.random_kanji_task.start()

    def update_kanji(self, results):
        if results['success']:
            kanji_set = results['kanji_set']
            for_a_day = kanji_set.pop()
            for_a_week = kanji_set.pop()
            for_a_month = kanji_set.pop()
            for_a_year = kanji_set.pop()

            self.day.setText(for_a_day.character)
            self.dayLabel.setText('<b>Day:</b> ' + str(for_a_day.frequency) +
                                  ' | ' + str(for_a_day.dominance) + '%')
            self.week.setText(for_a_week.character)
            self.weekLabel.setText('<b>Week:</b> ' +
                                   str(for_a_week.frequency) + ' | ' +
                                   str(for_a_week.dominance) + '%')
            self.month.setText(for_a_month.character)
            self.monthLabel.setText('<b>Month:</b> ' +
                                    str(for_a_month.frequency) + ' | ' +
                                    str(for_a_month.dominance) + '%')
            self.year.setText(for_a_year.character)
            self.yearLabel.setText('<b>Year:</b> ' +
                                   str(for_a_year.frequency) + ' | ' +
                                   str(for_a_year.dominance) + '%')

            self.kanji_tooltip(self.day)
            self.kanji_tooltip(self.week)
            self.kanji_tooltip(self.month)
            self.kanji_tooltip(self.year)

            if self.stats.isVisible():
                self.stats.update_stat_info()
                self.stats.refresh_plot()

            self.hide_message()
        else:
            self.show_message_then_hide(results['message'])

        self.hide_progress()

    def pretty_font(self):
        pass

    def update_alg(self):
        self.al.set_active(str(self.methodCombo.currentText()))

    def init_backend(self):
        self.al = RandomMess()
        self.update_alg()

    def auth_task(self):
        self.auth_thread = AuthorizationTask(self.al)
        self.auth_thread.done.connect(self.auth_complete)
        #self.auth_thread.run()
        # IT DOESN't work on windows as it should!
        self.auth_thread.start()
        self.show_progress('Authorizing on RNG services...')

    def auth_complete(self, success):
        self.hide_message()
        self.hide_progress()
        if success:
            self.show_message_then_hide("Successfully authenticated!", False)
        else:
            self.show_message_then_hide("Sorry, could not authenticate.")

    def show_message_then_hide(self, message, error=True):
        if error:
            self.statusMessage.setStyleSheet(WARNING_STYLE)
        else:
            self.statusMessage.setStyleSheet(NOTE_STYLE)

        self.statusMessage.setText(message)
        self.statusMessage.show()
        QTimer.singleShot(MESSAGE_TIMEOUT, self.hide_message)

    def show_progress(self, message):
        self.statusMessage.setStyleSheet(NOTE_STYLE)
        self.statusMessage.setText(message)
        self.statusMessage.show()
        self.progressBar.show()

    def hide_message(self):
        self.statusMessage.setText('')
        self.statusMessage.hide()

    def hide_progress(self):
        self.progressBar.hide()

    def toggle_kanji_info(self, label, info):
        label.setToolTip(info.info())

    def kanji_tooltip(self, label):
        found = JDIC.search(label.text())
        if found:
            label.setToolTip(found.info())
        else:
            label.setToolTip('No such kanji in kanjidic2!')

    def kanji_info(self, kanji):
        pass

    def app_help(self):
        self.aboutBox.show()

        #### Utility events ####

    def resizeEvent(self, QResizeEvent):
        self.updateStatsPosition()
        self.updateStatsSize()

    def moveEvent(self, QMoveEvent):
        self.updateStatsPosition()
        self.updateStatsSize()

    def updateStatsPosition(self):
        self.stats.move(self.x() + self.width() + 20, self.y())

    def updateStatsSize(self):
        self.stats.resize(QSize(self.stats.width(), self.height()))
class EditorWidget(QWidget, Logger.ClassLogger):
    """
    Widget editor for python
    """
    def __init__(self,
                 editorId,
                 title,
                 parent,
                 activePyLexer=True,
                 activePropertiesLexer=False,
                 wrappingText=False,
                 toolbar=True):
        """
        Contructs EditorWidget

        @param parent: 
        @type parent:
        """
        QWidget.__init__(self, parent)
        self.toolbar = toolbar
        self.title = title
        self.editor = PyEditor(editorId, parent, activePyLexer,
                               activePropertiesLexer, wrappingText)
        self.createWidgets()
        self.createToolbars()

    def getEditor(self):
        """
        Return editor
        """
        return self.editor

    def createWidgets(self):
        """
        Create qt widget
        """
        self.dockToolbarClipboard = QToolBar(self)
        self.dockToolbarClipboard.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbarClipboard.setToolButtonStyle(
            Qt.ToolButtonTextUnderIcon)

        self.dockToolbarText = QToolBar(self)
        self.dockToolbarText.setStyleSheet(
            "QToolBar { border: 0px }")  # remove 3D border
        self.dockToolbarText.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.clipBox = QGroupBox("Clipboard")
        self.clipBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutClipBox = QHBoxLayout()
        layoutClipBox.addWidget(self.dockToolbarClipboard)
        layoutClipBox.setContentsMargins(0, 0, 0, 0)
        self.clipBox.setLayout(layoutClipBox)

        self.textBox = QGroupBox("Formatting")
        self.textBox.setStyleSheet("""
                                           QGroupBox { font: normal; border: 1px solid silver; border-radius: 2px; } 
                                           QGroupBox { padding-bottom: 10px; background-color: #FAFAFA; } 
                                           QGroupBox::title { subcontrol-position: bottom center;}
                                       """)
        layoutTextBox = QHBoxLayout()
        layoutTextBox.addWidget(self.dockToolbarText)
        layoutTextBox.setContentsMargins(0, 0, 0, 0)
        self.textBox.setLayout(layoutTextBox)

        layoutToolbars = QHBoxLayout()
        layoutToolbars.addWidget(self.textBox)
        layoutToolbars.addWidget(self.clipBox)
        layoutToolbars.addStretch(1)
        layoutToolbars.setContentsMargins(0, 0, 0, 0)

        title = QLabel(self.title)
        title.setStyleSheet("QLabel { padding-left: 1px; padding-top: 2px }")
        font = QFont()
        font.setBold(True)
        title.setFont(font)

        layout = QVBoxLayout()
        layout.addWidget(title)
        if self.toolbar:
            layout.addLayout(layoutToolbars)
        layout.addWidget(self.editor)
        layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(layout)

    def createToolbars(self):
        """
        Create qt toolbars
        """
        self.dockToolbarClipboard.addAction(self.editor.copyAction)
        self.dockToolbarClipboard.addAction(self.editor.cutAction)
        self.dockToolbarClipboard.addAction(self.editor.pasteAction)
        self.dockToolbarClipboard.addSeparator()
        self.dockToolbarClipboard.addAction(self.editor.undoAction)
        self.dockToolbarClipboard.addAction(self.editor.redoAction)
        self.dockToolbarClipboard.setIconSize(QSize(16, 16))

        self.dockToolbarText.addAction(self.editor.foldAllAction)
        self.dockToolbarText.addSeparator()
        self.dockToolbarText.addAction(self.editor.commentAction)
        self.dockToolbarText.addAction(self.editor.uncommentAction)
        self.dockToolbarText.setIconSize(QSize(16, 16))
Example #11
0
def mkGroupBox(title):
    group_box = QGroupBox(title)
    sub_form = QFormLayout(group_box)
    group_box.setStyleSheet(u"QGroupBox {font: bold italic large;}")
    return group_box, sub_form
Example #12
0
class RegistrationViewWidget(FWidget):
    """ Shows the home page  """
    def __init__(self, parent=0, *args, **kwargs):
        super(RegistrationViewWidget, self).__init__(parent=parent,
                                                     *args,
                                                     **kwargs)
        self.parent = parent

        self.parentWidget().set_window_title("FORMULAIRE D’IMMATRICULATION")
        self.title = FLabel("<h3>FORMULAIRE D’IMMATRICULATION</h3>")
        self.office = Office.select().where(Office.id == 1).get()
        self.created_date_field = FormatDate(QDate.currentDate())
        self.created_date_field.setMaximumWidth(130)
        # self.created_date_field.setInputMask('##/##/####')
        self.rue_field = IntLineEdit()
        self.porte_field = IntLineEdit()
        self.tel_field = IntLineEdit()
        self.tel_field.setInputMask('## ## ## ##')
        self.tel2_field = IntLineEdit()
        self.tel2_field.setInputMask('## ## ## ##')
        self.bp_field = IntLineEdit()
        self.email_field = LineEdit()
        self.denomination_field = LineEdit()
        self.commercial_name_field = LineEdit()
        self.declaration_date_field = FormatDate(QDate.currentDate())
        self.declaration_date_field.setMaximumWidth(130)
        self.amount_capital_social_initial = FLabel()
        self.amount_part_social_field = IntLineEdit()
        self.apports_numeraire_field = IntLineEdit()
        self.apports_numeraire_field.textChanged.connect(self.cal_total)
        self.apports_nature_field = IntLineEdit()
        self.apports_nature_field.textChanged.connect(self.cal_total)
        self.apports_industrie_field = IntLineEdit()
        self.apports_industrie_field.textChanged.connect(self.cal_total)

        self.duree_statutaire_field = IntLineEdit()
        self.duree_statutaire_field.setMaximumWidth(80)
        self.spinneret_box = QComboBox()
        self.spinneret_box.setMaximumWidth(800)

        self.activites_box = QComboBox()
        self.activites_box.setMaximumWidth(800)
        self.activites_box.currentIndexChanged.connect(self.sp_change_select)
        self.activities_list = get_activities()
        for index, value in enumerate(self.activities_list):
            self.activites_box.addItem(
                "{}".format(self.activities_list.get(value).upper()), value)
            # if self.store and self.store.name == op.name:
            #     self.box_store.setCurrentIndex(index)

        self.formes_box = QComboBox()
        self.formes_box.setMaximumWidth(800)
        self.formes_list = get_formes()
        for index, value in enumerate(self.formes_list):
            self.formes_box.addItem(
                "{}".format(self.formes_list.get(value).upper()), value)

        self.commune_list = entity_children(self.office.slug_cercle).items()
        self.commune_box = ExtendedComboBox()
        for index, value in enumerate(self.commune_list):
            self.commune_box.addItem("{}".format(value[1].upper()), value[0])
        # self.commune_box.addItems(self.commune_list)
        self.commune_box.setToolTip("commune")
        self.commune_box.currentIndexChanged.connect(self.c_change_select)

        # self.vfq_list = self.get_vfq_list()
        self.vfq_box = ExtendedComboBox()
        self.vfq_list = self.get_vfq_list()
        for index, value in enumerate(self.vfq_list):
            self.vfq_box.addItem("{}".format(self.vfq_list.get(value).upper()),
                                 value)
        self.vfq_box.setToolTip("vfq")

        formbox = QFormLayout()
        formbox.addRow(FLabel(DATE_DEMANTE), self.declaration_date_field)
        formbox.addRow(FLabel("1. " + DENOMINATION_S_SC),
                       self.denomination_field)
        formbox.addRow(FLabel("2. " + NOM_COMMERCIAL),
                       self.commercial_name_field)
        formbox.addRow(FLabel("3. " + DATE_CREATION_SC),
                       self.created_date_field)
        formbox.addRow(FLabel("4. " + ACTIVITES_E), self.activites_box)
        formbox.addRow(FLabel("5. " + FILIERE), self.spinneret_box)
        formbox.addRow(FLabel("6. " + FORME_SC), self.formes_box)
        # Capital Social Initial
        capital_formbox = QFormLayout()
        capital_formbox.addRow(FLabel("7.1. " + MONTANT_PART_S),
                               self.amount_part_social_field)
        capital_formbox.addRow(FLabel("7.2. " + MONTANT_APPORTS_NUM),
                               self.apports_numeraire_field)
        capital_formbox.addRow(FLabel("7.3. " + MONTANT_APPORTS_NAT),
                               self.apports_nature_field)
        capital_formbox.addRow(FLabel("7.4. " + MONTANT_APPORTS_INDU),
                               self.apports_industrie_field)
        self.capitalSGroupBox = QGroupBox("7. " + MONTANT_CAPITAL_SI)
        self.capitalSGroupBox.setLayout(capital_formbox)
        self.capitalSGroupBox.setStyleSheet(CSS)
        # self.capitalSGroupBox.setMaximumWidth(1300)
        # Adresse du siège social

        self.vline = QFrame()
        self.vline.setFrameShape(QFrame.VLine)
        self.vline.setFrameShadow(QFrame.Sunken)

        self.addresGroupBox = QGroupBox("8. " + ADRESSE_SS)
        self.addresGroupBox.setStyleSheet(CSS)
        addres_gribox = QGridLayout()
        addres_gribox.addWidget(FRLabel(CERCLE), 0, 0)
        addres_gribox.addWidget(FLabel(self.office.cercle_name()), 0, 1)
        addres_gribox.addWidget(FRLabel(COMMUNE), 1, 0)
        addres_gribox.addWidget(self.commune_box, 1, 1)
        # addres_gribox.addWidget(self.vline, 0, 3, 2, 5)
        addres_gribox.addWidget(FRLabel(VFQ), 2, 0)
        addres_gribox.addWidget(self.vfq_box, 2, 1)
        addres_gribox.addWidget(FRLabel(RUE), 0, 2)
        addres_gribox.addWidget(self.rue_field, 0, 3)
        addres_gribox.addWidget(FRLabel(PORTE), 1, 2)
        addres_gribox.addWidget(self.porte_field, 1, 3)
        addres_gribox.addWidget(FRLabel(BP), 0, 4)
        addres_gribox.addWidget(self.bp_field, 0, 5)
        addres_gribox.addWidget(FRLabel(EMAIL), 1, 4)
        addres_gribox.addWidget(self.email_field, 1, 5)
        addres_gribox.addWidget(FRLabel(TEL), 2, 2)
        addres_gribox.addWidget(self.tel_field, 2, 3)
        addres_gribox.addWidget(FRLabel(TEL2), 2, 4)
        addres_gribox.addWidget(self.tel2_field, 2, 5)
        # addres_gribox.setColumnStretch(6, 5)
        self.addresGroupBox.setLayout(addres_gribox)
        # self.addresGroupBox.setMaximumWidth(1300)
        # Durée statutaire de la société coopérative
        duree_fbox = QFormLayout()
        duree_fbox.addRow(FLabel("9. " + DUREE_STATUTAIRE_SC),
                          self.duree_statutaire_field)
        butt = Button_save(SAVE)
        butt.clicked.connect(self.save_and_goto_manager)
        butt_and_continous = Button_save(SAVE_AND_CONTINNUES)
        butt_and_continous.clicked.connect(self.save_and_goto_add_member)

        butt_and_continous.setMaximumWidth(300)
        duree_fbox.addRow(FLabel(""), FLabel(""))
        duree_fbox.addRow(butt, butt_and_continous)

        vbox = QVBoxLayout()
        vbox.addLayout(formbox)
        vbox.addWidget(self.capitalSGroupBox)
        vbox.addWidget(self.addresGroupBox)
        vbox.addLayout(duree_fbox)
        self.setLayout(vbox)

    def get_vfq_list(self):
        # c_dic = {}
        co_select = self.commune_box.itemData(self.commune_box.currentIndex())
        return entity_children(co_select)

    def c_change_select(self):
        self.vfq_box.clear()
        self.vfq_list = self.get_vfq_list()
        for index, value in enumerate(self.vfq_list):
            self.vfq_box.addItem("{}".format(self.vfq_list.get(value).upper()),
                                 value)

    def get_spinneret_list(self):
        # c_dic = {}
        r_select = self.activites_box.itemData(
            self.activites_box.currentIndex())
        return get_spinneret_activites(r_select)

    def sp_change_select(self):
        self.spinneret_box.clear()
        self.spinneret_list = self.get_spinneret_list()

        for index, value in enumerate(self.spinneret_list):
            self.spinneret_box.addItem(
                "{}".format(self.spinneret_list.get(value).upper()), value)

    def is_valide(self):
        if check_is_empty(self.denomination_field):
            return False

        if CooperativeCompanie.select().where(
                CooperativeCompanie.denomination ==
                self.denomination_field.text()).exists():
            field_error(
                self.denomination_field,
                "Cette dénomination existe déjà dans la base de données !")
            return False
        if check_is_empty(self.commercial_name_field):
            return False
        if check_is_empty(self.created_date_field):
            return False
        if check_is_empty(self.denomination_field):
            return False
        if check_is_empty(self.apports_numeraire_field):
            return False
        if check_is_empty(self.apports_nature_field):
            return False
        if check_is_empty(self.apports_industrie_field):
            return False
        # if check_is_empty(self.rue_field):
        #     return False
        # if check_is_empty(self.porte_field):
        #     return False
        # print(len(self.tel_field.text()))
        if len(self.tel_field.text()) != 11:
            field_error(self.tel_field, "Numéro requis")
            return False
        # if check_is_empty(self.bp_field):
        #     return False
        # if check_is_empty(self.email_field):
        #     return False
        if check_is_empty(self.duree_statutaire_field):
            return False
        # print(int(self.duree_statutaire_field.text()))
        if int(self.duree_statutaire_field.text()) > 99:
            field_error(
                self.duree_statutaire_field,
                "La durée statutaire ne peut être supérieure à 99 ans")
            return False
        return True

    def save(self):
        if not self.is_valide():
            return
        self.scoop = CooperativeCompanie()
        self.scoop.office = self.office
        self.scoop.created_date = str(self.created_date_field.text())
        self.scoop.denomination = str(self.denomination_field.text())
        self.scoop.commercial_name = str(self.commercial_name_field.text())
        self.scoop.activity = self.activites_box.itemData(
            self.activites_box.currentIndex())
        self.scoop.spinneret = self.spinneret_box.itemData(
            self.spinneret_box.currentIndex())
        self.scoop.forme = self.formes_box.itemData(
            self.formes_box.currentIndex())
        self.scoop.amount_part_social = is_int(
            self.amount_part_social_field.text())
        self.scoop.apports_numeraire = is_int(
            self.apports_numeraire_field.text())
        self.scoop.apports_nature = is_int(self.apports_nature_field.text())
        self.scoop.apports_industrie = is_int(
            self.apports_industrie_field.text())
        self.scoop.region = self.office.slug_region
        self.scoop.cercle = self.office.slug_cercle
        self.scoop.commune = self.commune_box.itemData(
            self.commune_box.currentIndex())
        self.scoop.vfq = self.vfq_box.itemData(self.vfq_box.currentIndex())
        self.scoop.rue = is_int(self.rue_field.text())
        self.scoop.porte = is_int(self.porte_field.text())
        self.scoop.tel = is_int(self.tel_field.text())
        self.scoop.tel2 = is_int(self.tel2_field.text())
        self.scoop.bp = is_int(self.bp_field.text())
        self.scoop.email = self.email_field.text()
        self.scoop.duree_statutaire = is_int(
            self.duree_statutaire_field.text())
        self.scoop.save_()
        check_list = CheckList()
        check_list.save_()
        self.dmd = Demande()
        self.dmd.check_list = check_list
        self.dmd.declaration_date = str(self.declaration_date_field.text())
        self.dmd.scoop = self.scoop
        self.dmd.status = self.dmd.ADDMEMBER
        self.dmd.save_()
        return True

    def save_and_goto_add_member(self):
        if self.save():
            from ui.member_manager import MemberManagerWidget
            self.parent.change_context(MemberManagerWidget, dmd=self.dmd)

    def save_and_goto_manager(self):
        if self.save():
            self.parent.change_context(ResgistrationManagerWidget)

    def cal_total(self):
        total = is_int(self.apports_numeraire_field.text() or 0) + is_int(
            self.apports_nature_field.text() or 0) + is_int(
                self.apports_industrie_field.text() or 0)
        self.capitalSGroupBox.setTitle("7. {} :  {}".format(
            MONTANT_CAPITAL_SI, device_amount(total)))