class SynthesisQWidget(QWidget):
    """
        Class who manage Synthesis view with Host and Services QWidgets
    """

    def __init__(self, parent=None):
        super(SynthesisQWidget, self).__init__(parent)
        # Fields
        self.host_widget = HostQWidget()
        self.services_widget = ServicesQWidget()
        self.line_search = QLineEdit()
        self.completer = QCompleter()
        self.hint_widget = QWidget()

    def initialize_synthesis(self):
        """
        Initialize Synthesis QWidget

        """

        synthesis_layout = QVBoxLayout()
        synthesis_layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(synthesis_layout)

        # Search widget
        search_widget = self.get_search_widget()
        synthesis_layout.addWidget(search_widget)

        # Host widget
        self.host_widget.initialize()
        self.host_widget.setMaximumHeight(self.width() * 0.5)
        synthesis_layout.addWidget(self.host_widget)

        # Hint Widget
        hint_text = _(
            '<h4>Dahsboard</h4>'
            '<ul><li>At the top of App, '
            'you have a dashboard that summarizes the number of items per state.</li></ul>'
            '<h4>Tabs</h4>'
            '<ul><li><h4>Host Synthesis</h4></li>'
            'Tap in the search bar to view a host and its services.'
            '<li><h4>Problems</h4></li>'
            'The "Problems" tab will show you all problems detected in your backend.'
            '<li><h4>Spy Hosts</h4></li>'
            'A "Spy Host" will keep you regularly informed of his condition. '
            'You will also see problems detected for this host, in the "Spy Hosts" panel.</ul>'
            '<h4>Alignak</h4>'
            '<ul><li>You can see your backend status and daemons if available, '
            'as well as your profile.</li></ul>'
            '<h4>Livestate</h4>'
            '<ul><li>In the livestate, you can see global state of your monitored items.</li></ul>'
            '<h4>Events</h4>'
            '<ul><li>Events will show you informative messages your actions inside App.</li></ul>'
        )
        hint_layout = QVBoxLayout(self.hint_widget)
        hint_label = QLabel(hint_text)
        hint_label.setObjectName('subtitle')
        hint_layout.addWidget(hint_label)
        synthesis_layout.addWidget(self.hint_widget)

        # Services widget
        self.services_widget.initialize()
        synthesis_layout.addWidget(self.services_widget)

        # Align all widgets to Top
        synthesis_layout.setAlignment(Qt.AlignTop)

    def get_search_widget(self):
        """
        Create and return the search QWidget

        :return: search QWidget
        :rtype: QWidget
        """

        widget = QWidget()
        layout = QHBoxLayout()
        layout.setSpacing(0)
        widget.setLayout(layout)

        # Search label
        search_lbl = QLabel(_('Search Host'))
        search_lbl.setObjectName('bordertitle')
        search_lbl.setFixedHeight(25)
        search_lbl.setToolTip(_('Search Host'))
        layout.addWidget(search_lbl)

        # QLineEdit
        self.line_search.setFixedHeight(search_lbl.height())
        layout.addWidget(self.line_search)
        self.create_line_search([])

        return widget

    def create_line_search(self, hostnames_list):
        """
        Add all hosts to QLineEdit and set QCompleter

        :param hostnames_list: list of host names
        :type hostnames_list: list
        """

        # Get QStringListModel
        model = self.completer.model()
        if not model:
            model = QStringListModel()

        model.setStringList(hostnames_list)

        # Configure QCompleter from model
        self.completer.setFilterMode(Qt.MatchContains)
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setModel(model)
        self.completer.popup().setObjectName('popup')

        # Add completer to QLineEdit
        self.line_search.setCompleter(self.completer)
        self.line_search.setPlaceholderText(_('Type a host name to display its data'))
        self.line_search.setToolTip(_('Type a host name to display its data'))

    def update_synthesis(self, host, services, not_spied):
        """
        Update Synthesis QWidget with given host and services

        :param host: host item
        :type host: alignak_app.items.host.Host
        :param services: list of services attached to host
        :type services: list
        :param not_spied: define if host is spied or not
        :type not_spied: bool
        """

        self.host_widget.spy_btn.setEnabled(not_spied)

        if host:
            logger.info('Display %s in synthesis view', host.name)
            # Update Qwidgets
            self.host_widget.update_host(host)
            self.services_widget.update_widget(services)
            self.hint_widget.hide()
            self.host_widget.show()
            self.services_widget.show()

            # If the service element does not have the same ID as the host, reset to None
            if self.services_widget.service_data_widget.service_item:
                if self.services_widget.service_data_widget.service_item.data['host'] != \
                        self.host_widget.host_item.item_id:
                    self.services_widget.service_data_widget.service_item = None

        else:
            self.host_widget.hide()
            self.services_widget.hide()
            self.hint_widget.show()
Beispiel #2
0
class DictionaryDialog(QDialog):
    def setupUi(self):
        self.setWindowTitle('Dictionary')

        layout = QGridLayout(self)
        self.setLayout(layout)

        self.edit = QLineEdit(self)
        self.edit.textChanged.connect(self.onTextChanged)
        self.edit.returnPressed.connect(self.onReturnPressed)

        searchButton = QPushButton('&Search', self)
        searchButton.clicked.connect(self.onSearchButtonClicked)

        addButton = QPushButton('Add', self)
        addButton.clicked.connect(self.onAddButtonClicked)

        header = ('Short', 'Long', "Translate")
        self.table = QTableWidget(self)
        self.table.setColumnCount(3)
        self.table.setHorizontalHeaderLabels(header)
        self.table.setColumnWidth(0, 50)
        self.table.setColumnWidth(1, 200)

        self.edit.setCompleter(
            QCompleter(self.updateTable(self.dictionary.words)))

        layout.addWidget(self.edit, 0, 0)
        layout.addWidget(searchButton, 0, 1)
        layout.addWidget(addButton, 0, 2)
        layout.addWidget(self.table, 1, 0, 1, 3)

    def onSearchButtonClicked(self):
        pass

    def onAddButtonClicked(self):
        pass

    def onTextChanged(self, arg):
        self.updateTable(self.dictionary.compare(arg))

    def onReturnPressed(self):
        if (self.table.rowCount() == 0):
            if (QMessageBox.question(
                    self, "Add",
                    "Not Found\nAdd new one?") == QMessageBox.Yes):
                self.createWordDialog(Word('%s,,' % self.edit.text()))

    def createWordDialog(self, word=Word('')):
        dialog = QDialog(self)
        layout = QGridLayout(self)

        dialog.setLayout(layout)
        shortEdit = QLineEdit(word.short)
        longEdit = QLineEdit(word.long)
        explainEdit = QLineEdit(word.explain)
        okButton = QPushButton('OK')
        okButton.clicked.connect(dialog.accept)
        cancelButton = QPushButton('Cancel')
        cancelButton.clicked.connect(dialog.reject)

        layout.addWidget(QLabel('Short'), 0, 0)
        layout.addWidget(shortEdit, 0, 1)
        layout.addWidget(QLabel('Long'), 1, 0)
        layout.addWidget(longEdit, 1, 1)
        layout.addWidget(QLabel('Translate'), 2, 0)
        layout.addWidget(explainEdit, 2, 1)
        layout.addWidget(okButton, 3, 0)
        layout.addWidget(cancelButton, 3, 1)

        dialog.exec()
        if (dialog.result() == QDialog.Accepted):
            word = Word(
                '%s,%s,%s' %
                (shortEdit.text(), longEdit.text(), explainEdit.text()))
            self.dictionary.append(word)
            self.dictionary.save()
            self.updateTable(self.dictionary.words)

    def updateTable(self, words):
        results = []
        self.table.setRowCount(len(words))
        i = 0
        for word in words:
            self.table.setItem(i, 0, QTableWidgetItem(word.short))
            self.table.setItem(i, 1, QTableWidgetItem(word.long))
            self.table.setItem(i, 2, QTableWidgetItem(word.explain))
            i += 1
            results.append(word.short)
        return results

    def __init__(self, parent=None):
        super(QDialog, self).__init__()
        self.dictionary = Dictionary()
        self.dictionary.load()
        self.setupUi()
class SynthesisQWidget(QWidget):
    """
        Class who manage Synthesis view with Host and Services QWidgets
    """
    def __init__(self, parent=None):
        super(SynthesisQWidget, self).__init__(parent)
        # Fields
        self.host_widget = HostQWidget()
        self.services_widget = ServicesQWidget()
        self.line_search = QLineEdit()
        self.completer = QCompleter()
        self.hint_widget = QWidget()

    def initialize_synthesis(self):
        """
        Initialize Synthesis QWidget

        """

        synthesis_layout = QVBoxLayout()
        synthesis_layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(synthesis_layout)

        # Search widget
        search_widget = self.get_search_widget()
        synthesis_layout.addWidget(search_widget)

        # Host widget
        self.host_widget.initialize()
        self.host_widget.setMaximumHeight(self.width() * 0.5)
        synthesis_layout.addWidget(self.host_widget)

        # Hint Widget
        hint_text = _(
            '<h4>Dahsboard</h4>'
            '<ul><li>At the top of App, '
            'you have a dashboard that summarizes the number of items per state.</li></ul>'
            '<h4>Tabs</h4>'
            '<ul><li><h4>Host Synthesis</h4></li>'
            'Tap in the search bar to view a host and its services.'
            '<li><h4>Problems</h4></li>'
            'The "Problems" tab will show you all problems detected in your backend.'
            '<li><h4>Spy Hosts</h4></li>'
            'A "Spy Host" will keep you regularly informed of his condition. '
            'You will also see problems detected for this host, in the "Spy Hosts" panel.</ul>'
            '<h4>Alignak</h4>'
            '<ul><li>You can see your backend status and daemons if available, '
            'as well as your profile.</li></ul>'
            '<h4>Livestate</h4>'
            '<ul><li>In the livestate, you can see global state of your monitored items.</li></ul>'
            '<h4>Events</h4>'
            '<ul><li>Events will show you informative messages your actions inside App.</li></ul>'
        )
        hint_layout = QVBoxLayout(self.hint_widget)
        hint_label = QLabel(hint_text)
        hint_label.setObjectName('subtitle')
        hint_layout.addWidget(hint_label)
        synthesis_layout.addWidget(self.hint_widget)

        # Services widget
        self.services_widget.initialize()
        synthesis_layout.addWidget(self.services_widget)

        # Align all widgets to Top
        synthesis_layout.setAlignment(Qt.AlignTop)

    def get_search_widget(self):
        """
        Create and return the search QWidget

        :return: search QWidget
        :rtype: QWidget
        """

        widget = QWidget()
        layout = QHBoxLayout()
        layout.setSpacing(0)
        widget.setLayout(layout)

        # Search label
        search_lbl = QLabel(_('Search Host'))
        search_lbl.setObjectName('bordertitle')
        search_lbl.setFixedHeight(25)
        search_lbl.setToolTip(_('Search Host'))
        layout.addWidget(search_lbl)

        # QLineEdit
        self.line_search.setFixedHeight(search_lbl.height())
        layout.addWidget(self.line_search)
        self.create_line_search([])

        return widget

    def create_line_search(self, hostnames_list):
        """
        Add all hosts to QLineEdit and set QCompleter

        :param hostnames_list: list of host names
        :type hostnames_list: list
        """

        # Get QStringListModel
        model = self.completer.model()
        if not model:
            model = QStringListModel()

        model.setStringList(hostnames_list)

        # Configure QCompleter from model
        self.completer.setFilterMode(Qt.MatchContains)
        self.completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.completer.setModel(model)
        self.completer.popup().setObjectName('popup')

        # Add completer to QLineEdit
        self.line_search.setCompleter(self.completer)
        self.line_search.setPlaceholderText(
            _('Type a host name to display its data'))
        self.line_search.setToolTip(_('Type a host name to display its data'))

    def update_synthesis(self, host, services, not_spied):
        """
        Update Synthesis QWidget with given host and services

        :param host: host item
        :type host: alignak_app.items.host.Host
        :param services: list of services attached to host
        :type services: list
        :param not_spied: define if host is spied or not
        :type not_spied: bool
        """

        self.host_widget.spy_btn.setEnabled(not_spied)

        if host:
            logger.info('Display %s in synthesis view', host.name)
            # Update Qwidgets
            self.host_widget.update_host(host)
            self.services_widget.update_widget(services)
            self.hint_widget.hide()
            self.host_widget.show()
            self.services_widget.show()

            # If the service element does not have the same ID as the host, reset to None
            if self.services_widget.service_data_widget.service_item:
                if self.services_widget.service_data_widget.service_item.data['host'] != \
                        self.host_widget.host_item.item_id:
                    self.services_widget.service_data_widget.service_item = None

        else:
            self.host_widget.hide()
            self.services_widget.hide()
            self.hint_widget.show()
Beispiel #4
0
class Synthesis(QWidget):
    """
        Class who create the Synthesis QWidget.
    """

    first_display = True

    def __init__(self, parent=None):
        super(Synthesis, self).__init__(parent)
        self.setStyleSheet(get_css())
        # Fields
        self.line_search = QLineEdit()
        self.app_backend = None
        self.action_manager = None
        self.host_synthesis = None
        self.app_widget = AppQWidget()
        self.old_checkbox_states = {}

    def initialize(self, app_backend):
        """
        Create the QWidget with its items and layout.

        :param app_backend: app_backend of alignak.
        :type app_backend: alignak_app.core.backend.AppBackend
        """

        logger.info('Create Synthesis View...')

        # App_backend
        self.app_backend = app_backend
        self.action_manager = ActionManager(app_backend)

        layout = QGridLayout()
        self.setLayout(layout)

        # button
        button = QPushButton('Search / Refresh Host', self)
        button.setObjectName('search')
        button.setFixedHeight(22)
        button.setToolTip('Search Host')
        button.clicked.connect(self.display_host_synthesis)
        layout.addWidget(button, 0, 4, 1, 1)
        layout.setAlignment(button, Qt.AlignTop)

        self.line_search.setFixedHeight(button.height())
        self.line_search.returnPressed.connect(button.click)
        self.line_search.cursorPositionChanged.connect(button.click)
        layout.addWidget(self.line_search, 0, 0, 1, 4)
        layout.setAlignment(self.line_search, Qt.AlignTop)

        self.app_widget.initialize('Host Synthesis View')
        self.app_widget.add_widget(self)
        self.app_widget.setMinimumSize(1300, 750)

        refresh_interval = int(get_app_config('Alignak-App', 'item_interval'))
        if bool(refresh_interval) and refresh_interval > 0:
            logger.debug('Hosts synthesis will be refresh in %ss',
                         str(refresh_interval))
            refresh_interval *= 1000
        else:
            logger.error(
                '"item_interval" option must be greater than 0. Replace by default: 30s'
            )
            refresh_interval = 30000

        refresh_timer = QTimer(self)
        refresh_timer.start(refresh_interval)
        refresh_timer.timeout.connect(self.display_host_synthesis)

    def create_line_search(self):
        """
        Add all hosts to QLineEdit and set QCompleter

        """

        # Create list for QStringModel
        hosts_list = []
        params = {'where': json.dumps({'_is_template': False})}

        all_hosts = self.app_backend.get('host', params, ['name'])

        if all_hosts:
            for host in all_hosts['_items']:
                hosts_list.append(host['name'])

        model = QStringListModel()
        model.setStringList(hosts_list)

        # Create completer from model
        completer = QCompleter()
        completer.setFilterMode(Qt.MatchContains)
        completer.setCaseSensitivity(Qt.CaseInsensitive)
        completer.setModel(model)

        # Add completer to "line edit"
        self.line_search.setCompleter(completer)
        self.line_search.setPlaceholderText(
            'Type a host name to display its data')
        self.line_search.setToolTip('Type a host name to display its data')

    def display_host_synthesis(self):
        """
        Display Synthesis QWidget. Remove and delete HostSynthesis if exists

        """

        if self.isVisible() or self.first_display:
            # If first display, create line search from hosts list
            if self.first_display:
                self.create_line_search()
                self.first_display = False

            host_name = str(self.line_search.text()).rstrip()
            backend_data = None
            old_row = -1

            if host_name:
                backend_data = self.app_backend.get_host_with_services(
                    host_name)

            # Store old data, remove and delete host_synthesis
            if self.host_synthesis:
                # Store old data
                if self.host_synthesis.services_list:
                    old_row = self.host_synthesis.services_list.currentRow()
                if self.host_synthesis.check_boxes:
                    for key in self.host_synthesis.check_boxes:
                        self.old_checkbox_states[key] = \
                            self.host_synthesis.check_boxes[key].isChecked()

                # Remove and delete QWidget
                self.layout().removeWidget(self.host_synthesis)
                self.host_synthesis.deleteLater()
                self.host_synthesis = None

            # Create the new host_synthesis
            self.host_synthesis = HostSynthesis(self.action_manager)
            self.host_synthesis.initialize(backend_data)

            # Restore old data
            if old_row >= 0 and self.host_synthesis.services_list:
                self.host_synthesis.services_list.setCurrentRow(old_row)
            if self.old_checkbox_states and self.host_synthesis.check_boxes:
                for key, checked in self.old_checkbox_states.items():
                    try:
                        self.host_synthesis.check_boxes[key].setChecked(
                            checked)
                    except KeyError as e:
                        logger.warning('Can\'t reapply filter [%s]: %s', e,
                                       checked)

            self.layout().addWidget(self.host_synthesis, 1, 0, 1, 5)