def __init__(self, parent, text, model):
        from pyvmmonitor_qt.qt.QtWidgets import QLabel
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
        from pyvmmonitor_qt.qt_utils import add_expanding_spacer_to_layout
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        QWidget.__init__(self, parent)
        self._in_expected_ui_change = 0
        self._in_expected_data_change = 0
        self._model = model

        self.on_value_changed = Callback()

        self._layout = QHBoxLayout(self)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)

        self._label = QLabel(self)
        self._label.setText(text)
        self._label.setFixedWidth(WIDTH_LABEL)

        self._line_edit = QLineEdit(self)
        self._line_edit.textChanged.connect(self._on_line_edit_changed)
        self._line_edit.setFixedWidth(WIDTH_VALUE)

        self._layout.addWidget(self._label)
        add_expanding_spacer_to_layout(self._layout)
        self._layout.addWidget(self._line_edit)
        self._model.register_modified(self._on_model_changed)
        self._update_line_edit()
    def __init__(self, parent, text, model):
        from pyvmmonitor_qt.qt.QtWidgets import QLabel
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
        from pyvmmonitor_qt.qt_utils import add_expanding_spacer_to_layout
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        QWidget.__init__(self, parent)
        self._in_expected_ui_change = 0
        self._in_expected_data_change = 0
        self._model = model

        self.on_value_changed = Callback()

        self._layout = QHBoxLayout(self)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)

        self._label = QLabel(self)
        self._label.setText(text)
        self._label.setFixedWidth(WIDTH_LABEL)

        self._line_edit = QLineEdit(self)
        self._line_edit.textChanged.connect(self._on_line_edit_changed)
        self._line_edit.setFixedWidth(WIDTH_VALUE)

        self._layout.addWidget(self._label)
        add_expanding_spacer_to_layout(self._layout)
        self._layout.addWidget(self._line_edit)
        self._model.register_modified(self._on_model_changed)
        self._update_line_edit()
示例#3
0
def test_count_children(qtapi):
    from pyvmmonitor_qt.qt.QtWidgets import QWidget
    qwidget = QWidget()
    qwidget2 = QWidget(qwidget)
    qwidget3 = QWidget(qwidget2)
    qwidget4 = QWidget(qwidget2)
    qtapi.add_widget(qwidget)
    assert count_widget_children(qwidget) == 3
    def create_widget(self, parent, layout='vertical', margin=0):
        '''
        :param parent:
        :param layout: 'vertical' or 'horizontal'
        '''
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        self._widget = QWidget(parent)

        self._layout = self.create_layout(layout, margin)
        self._widget.setLayout(self._layout)
示例#5
0
def create_right_aligned_toolbar(parent):

    '''
    Creates a toolbar with an expanding widget in the beginning.
    '''
    from pyvmmonitor_qt.qt.QtWidgets import QToolBar
    from pyvmmonitor_qt.qt.QtWidgets import QWidget
    from pyvmmonitor_qt.qt.QtWidgets import QSizePolicy

    toolbar = QToolBar(parent)
    spacer = QWidget(toolbar)
    spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    toolbar.addWidget(spacer)
    return toolbar
示例#6
0
def create_right_aligned_toolbar(parent):

    '''
    Creates a toolbar with an expanding widget in the beginning.
    '''
    from pyvmmonitor_qt.qt.QtWidgets import QToolBar
    from pyvmmonitor_qt.qt.QtWidgets import QWidget
    from pyvmmonitor_qt.qt.QtWidgets import QSizePolicy

    toolbar = QToolBar(parent)
    spacer = QWidget(toolbar)
    spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    toolbar.addWidget(spacer)
    return toolbar
示例#7
0
    def __init__(self, shortcuts_manager):
        '''
        :param IShortcutsManager shortcuts_manager:
        '''
        QWidget.__init__(self)
        from pyvmmonitor_qt.qt.QtWidgets import QTreeView
        self._shortcuts = QTreeView(self)

        from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
        self._vlayout = QVBoxLayout(self)
        self._vlayout.addWidget(self._shortcuts)
        self.setLayout(self._vlayout)

        from pyvmmonitor_qt.tree.pythonic_tree_view import PythonicQTreeView
        self._pythonic_tree = PythonicQTreeView(self._shortcuts)
        self._pythonic_tree.columns = ['Action', 'Shortcut']
    def __init__(self, shortcuts_manager):
        '''
        :param IShortcutsManager shortcuts_manager:
        '''
        QWidget.__init__(self)
        from pyvmmonitor_qt.qt.QtWidgets import QTreeView
        self._shortcuts = QTreeView(self)

        from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
        self._vlayout = QVBoxLayout(self)
        self._vlayout.addWidget(self._shortcuts)
        self.setLayout(self._vlayout)

        from pyvmmonitor_qt.tree.pythonic_tree_view import PythonicQTreeView
        self._pythonic_tree = PythonicQTreeView(self._shortcuts)
        self._pythonic_tree.columns = ['Action', 'Shortcut']
示例#9
0
    def __init__(self):
        QWidget.__init__(self)

        self._layout = QVBoxLayout(self)
        self._layout.setSpacing(0)

        self._tabbar = QTabBar(self)
        self._tabbar.currentChanged.connect(self._tab_changed)
        self._layout.addWidget(self._tabbar)

        self._stack = QStackedWidget(self)
        self._layout.addWidget(self._stack)

        self._widgets = []
        self._buttons = []
        self.setLayout(self._layout)
        # Called with: on_remove_requested(index)
        self.on_remove_requested = Callback()
示例#10
0
    def mouseMoveEvent(self, ev):
        if self._last_pos is not None:
            pos = ev.pos()
            pos = pos.x(), pos.y()
            self._last_pos = pos
            self._on_mouse_pos(pos)
            return

        return QWidget.mouseMoveEvent(self, ev)
    def __init__(self):
        QWidget.__init__(self)

        self._layout = QVBoxLayout(self)
        self._layout.setSpacing(0)

        self._tabbar = QTabBar(self)
        self._tabbar.currentChanged.connect(self._tab_changed)
        self._layout.addWidget(self._tabbar)

        self._stack = QStackedWidget(self)
        self._layout.addWidget(self._stack)

        self._widgets = []
        self._buttons = []
        self.setLayout(self._layout)
        # Called with: on_remove_requested(index)
        self.on_remove_requested = Callback()
示例#12
0
    def mousePressEvent(self, ev):
        from pyvmmonitor_qt.qt.QtCore import Qt
        if ev.button() == Qt.LeftButton:
            pos = ev.pos()
            pos = pos.x(), pos.y()
            self._last_pos = pos
            self._on_mouse_pos(pos)
            return

        return QWidget.mousePressEvent(self, ev)
示例#13
0
    def create_widget(self, parent, layout='vertical', margin=0):
        '''
        :param parent:
        :param layout: 'vertical' or 'horizontal'
        '''
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        self._widget = QWidget(parent)

        self._layout = self.create_layout(layout, margin)
        self._widget.setLayout(self._layout)
示例#14
0
    def _create_find_control(self):
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        from pyvmmonitor_qt.qt.QtWidgets import QPushButton
        from pyvmmonitor_qt.qt.QtWidgets import QMenu
        from pyvmmonitor_qt.qt.QtWidgets import QAction
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout

        control = QWidget(self)

        self.line_edit = QLineEdit()
        self.next_button = QPushButton('&Next')
        self.next_button.setFixedWidth(self.button_size)
        self.prev_button = QPushButton('&Prev')
        self.prev_button.setFixedWidth(self.button_size)
        self.options_button = QPushButton('&Options')
        self.options_button.setFixedWidth(self.button_size)

        options_menu = QMenu(self)
        self.case_action = QAction('Match &case', options_menu)
        self.case_action.setCheckable(True)
        self.word_action = QAction('Match words', options_menu)
        self.word_action.setCheckable(True)
        self.wrap_action = QAction('Wrap search', options_menu)
        self.wrap_action.setCheckable(True)
        self.wrap_action.setChecked(True)
        options_menu.addAction(self.case_action)
        options_menu.addAction(self.word_action)
        options_menu.addAction(self.wrap_action)
        self.options_button.setMenu(options_menu)

        layout = QHBoxLayout()
        layout.addWidget(self.line_edit)
        layout.addWidget(self.next_button)
        layout.addWidget(self.prev_button)
        layout.addWidget(self.options_button)
        layout.addStretch(2)
        layout.setContentsMargins(0, 0, 0, 0)

        control.setLayout(layout)
        return control
    def __init__(self, parent, model, layout='vertical'):
        '''
        :param QWidget parent:
        :param ChooseColorModel model:
        '''
        QWidget.__init__(self, parent)
        self._in_expected_ui_change = 0
        self._in_expected_data_change = 0
        if layout == 'vertical':
            from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
            layout = QVBoxLayout()
        elif layout == 'horizontal':
            from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
            layout = QHBoxLayout()
        else:
            raise AssertionError('Expected "vertical" or "horizontal" layout.')

        self._layout = layout
        self.setLayout(self._layout)
        assert model is not None
        self._model = model

        self._create_label_widgets()

        self._widget_0.on_value_changed.register(self._update_w0)
        self._layout.addWidget(self._widget_0)

        if hasattr(self, '_widget_1'):
            self._widget_1.on_value_changed.register(self._update_w1)
            self._layout.addWidget(self._widget_1)

        if hasattr(self, '_widget_2'):
            self._widget_2.on_value_changed.register(self._update_w2)
            self._layout.addWidget(self._widget_2)

        if hasattr(self, '_widget_3'):
            self._widget_3.on_value_changed.register(self._update_w3)
            self._layout.addWidget(self._widget_3)

        self._update_widgets()
        model.register_modified(self._on_model_changed)
示例#16
0
    def _create_find_control(self):
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        from pyvmmonitor_qt.qt.QtWidgets import QPushButton
        from pyvmmonitor_qt.qt.QtWidgets import QMenu
        from pyvmmonitor_qt.qt.QtWidgets import QAction
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout

        control = QWidget(self)

        self.line_edit = QLineEdit()
        self.next_button = QPushButton('&Next')
        self.next_button.setFixedWidth(self.button_size)
        self.prev_button = QPushButton('&Prev')
        self.prev_button.setFixedWidth(self.button_size)
        self.options_button = QPushButton('&Options')
        self.options_button.setFixedWidth(self.button_size)

        options_menu = QMenu(self)
        self.case_action = QAction('Match &case', options_menu)
        self.case_action.setCheckable(True)
        self.word_action = QAction('Match words', options_menu)
        self.word_action.setCheckable(True)
        self.wrap_action = QAction('Wrap search', options_menu)
        self.wrap_action.setCheckable(True)
        self.wrap_action.setChecked(True)
        options_menu.addAction(self.case_action)
        options_menu.addAction(self.word_action)
        options_menu.addAction(self.wrap_action)
        self.options_button.setMenu(options_menu)

        layout = QHBoxLayout()
        layout.addWidget(self.line_edit)
        layout.addWidget(self.next_button)
        layout.addWidget(self.prev_button)
        layout.addWidget(self.options_button)
        layout.addStretch(2)
        layout.setContentsMargins(0, 0, 0, 0)

        control.setLayout(layout)
        return control
    def __init__(self, parent, model, layout='vertical'):
        '''
        :param QWidget parent:
        :param ChooseColorModel model:
        '''
        QWidget.__init__(self, parent)
        self._in_expected_ui_change = 0
        self._in_expected_data_change = 0
        if layout == 'vertical':
            from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
            layout = QVBoxLayout()
        elif layout == 'horizontal':
            from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
            layout = QHBoxLayout()
        else:
            raise AssertionError('Expected "vertical" or "horizontal" layout.')

        self._layout = layout
        self.setLayout(self._layout)
        assert model is not None
        self._model = model

        self._create_label_widgets()

        self._widget_0.on_value_changed.register(self._update_w0)
        self._layout.addWidget(self._widget_0)

        if hasattr(self, '_widget_1'):
            self._widget_1.on_value_changed.register(self._update_w1)
            self._layout.addWidget(self._widget_1)

        if hasattr(self, '_widget_2'):
            self._widget_2.on_value_changed.register(self._update_w2)
            self._layout.addWidget(self._widget_2)

        if hasattr(self, '_widget_3'):
            self._widget_3.on_value_changed.register(self._update_w3)
            self._layout.addWidget(self._widget_3)

        self._update_widgets()
        model.register_modified(self._on_model_changed)
    def __init__(self, parent, text, gradient_stops=None, limits=(0, 100)):
        from pyvmmonitor_qt.qt.QtWidgets import QLabel
        from pyvmmonitor_qt.qt_gradient_slider import QGradientSlider
        from pyvmmonitor_qt.qt.QtWidgets import QSpinBox
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
        QWidget.__init__(self, parent)

        self.on_value_changed = Callback()

        self._layout = QHBoxLayout(self)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)

        self._label = QLabel(self)
        self._label.setText(text)
        self._label.setFixedWidth(WIDTH_LABEL)

        self._limits = limits

        self._slider = QGradientSlider(self)
        self._slider.min_value = limits[0]
        self._slider.max_value = limits[1]
        self._slider.on_value.register(self._on_gradient_value_changed)

        self._slider.setSizePolicy(QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        self._slider.setFixedHeight(20)

        self._spin_box = QSpinBox(self)
        self._spin_box.setFixedWidth(WIDTH_VALUE)
        self._spin_box.setMinimum(limits[0])
        self._spin_box.setMaximum(limits[1])
        self._spin_box.valueChanged.connect(self._on_spin_value_changed)

        self._layout.addWidget(self._label)
        self._layout.addWidget(self._slider)
        self._layout.addWidget(self._spin_box)

        self.set_gradient_stops(gradient_stops)
    def __init__(self, parent, text, gradient_stops=None, limits=(0, 100)):
        from pyvmmonitor_qt.qt.QtWidgets import QLabel
        from pyvmmonitor_qt.qt_gradient_slider import QGradientSlider
        from pyvmmonitor_qt.qt.QtWidgets import QSpinBox
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
        QWidget.__init__(self, parent)

        self.on_value_changed = Callback()

        self._layout = QHBoxLayout(self)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)

        self._label = QLabel(self)
        self._label.setText(text)
        self._label.setFixedWidth(WIDTH_LABEL)

        self._limits = limits

        self._slider = QGradientSlider(self)
        self._slider.min_value = limits[0]
        self._slider.max_value = limits[1]
        self._slider.on_value.register(self._on_gradient_value_changed)

        self._slider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self._slider.setFixedHeight(20)

        self._spin_box = QSpinBox(self)
        self._spin_box.setFixedWidth(WIDTH_VALUE)
        self._spin_box.setMinimum(limits[0])
        self._spin_box.setMaximum(limits[1])
        self._spin_box.valueChanged.connect(self._on_spin_value_changed)

        self._layout.addWidget(self._label)
        self._layout.addWidget(self._slider)
        self._layout.addWidget(self._spin_box)

        self.set_gradient_stops(gradient_stops)
    def __init__(self, pythonic_tree_view=None):
        QWidget.__init__(self)
        from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
        self._vbox = QVBoxLayout(self)

        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        self._edit_text_filter = QLineEdit(self)
        self._vbox.addWidget(self._edit_text_filter)

        if pythonic_tree_view is None:
            from pyvmmonitor_qt.tree.pythonic_tree_view import PythonicQTreeView
            from pyvmmonitor_qt.qt.QtWidgets import QTreeView
            tree = QTreeView(self)
            pythonic_tree_view = PythonicQTreeView(tree)
        else:
            pythonic_tree_view.tree.setParent(self)

        self._vbox.addWidget(pythonic_tree_view.tree)
        self.setLayout(self._vbox)
        self._pythonic_tree_view = pythonic_tree_view

        self._edit_text_filter.textChanged.connect(self._on_filter_text_changed)
    def __init__(self, pythonic_tree_view=None):
        QWidget.__init__(self)
        from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
        self._vbox = QVBoxLayout(self)

        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        self._edit_text_filter = QLineEdit(self)
        self._vbox.addWidget(self._edit_text_filter)

        if pythonic_tree_view is None:
            from pyvmmonitor_qt.tree.pythonic_tree_view import PythonicQTreeView
            from pyvmmonitor_qt.qt.QtWidgets import QTreeView
            tree = QTreeView(self)
            pythonic_tree_view = PythonicQTreeView(tree)
        else:
            pythonic_tree_view.tree.setParent(self)

        self._vbox.addWidget(pythonic_tree_view.tree)
        self.setLayout(self._vbox)
        self._pythonic_tree_view = pythonic_tree_view

        self._edit_text_filter.textChanged.connect(
            self._on_filter_text_changed)
示例#22
0
    def _create_replace_control(self):
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        from pyvmmonitor_qt.qt.QtWidgets import QPushButton
        from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit

        control = QWidget(self)

        self.replace_edit = QLineEdit()
        self.replace_button = QPushButton('&Replace')
        self.replace_button.setFixedWidth(self.button_size)
        self.replace_all_button = QPushButton('Replace &All')
        self.replace_all_button.setFixedWidth(self.button_size)

        layout = QHBoxLayout()
        layout.addWidget(self.replace_edit)
        layout.addWidget(self.replace_button)
        layout.addWidget(self.replace_all_button)
        layout.addStretch(2)
        layout.setContentsMargins(0, 0, 0, 0)

        control.setLayout(layout)
        return control
示例#23
0
class WidgetBuilder(object):
    '''
    Helper to build a widget adding components in a vertical or horizontal layout.

    Use as:

    widget_builder = WidgetBuilder()
    widget_builder.create_widget()
    widget_builder.create_label()
    ...

    widget_builder.widget.show()
    '''
    def __init__(self, widget=None, layout=None):
        self._widget = widget
        self._layout = layout

    def create_widget(self, parent, layout='vertical', margin=0):
        '''
        :param parent:
        :param layout: 'vertical' or 'horizontal'
        '''
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        self._widget = QWidget(parent)

        self._layout = self.create_layout(layout, margin)
        self._widget.setLayout(self._layout)

    @property
    def widget(self):
        return self._widget

    @classmethod
    def create_layout(cls, layout='vertical', margin=0):
        if layout == 'vertical':
            from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
            layout = QVBoxLayout()
        else:
            from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
            layout = QHBoxLayout()

        layout.setContentsMargins(margin, margin, margin, margin)
        return layout

    def add_widget(self, widget, layout=None):
        if layout is None:
            layout = self._layout
        if layout is None:
            raise RuntimeError(
                'layout must be passed at constructor or created in create_widget.'
            )
        layout.addWidget(widget)
        return widget

    def create_label(self, txt='', layout=None):
        from pyvmmonitor_qt.qt.QtWidgets import QLabel
        widget = QLabel(self._widget)
        widget.setText(txt)
        return self.add_widget(widget, layout=layout)

    def create_line_edit(self):
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        widget = QLineEdit(self._widget)
        return self.add_widget(widget)

    def create_text_browser(self, txt='', open_links=False):
        from pyvmmonitor_qt.qt.QtWidgets import QTextBrowser
        from pyvmmonitor_qt.qt.QtCore import Qt
        text_browser = QTextBrowser(self._widget)
        text_browser.setOpenExternalLinks(open_links)
        text_browser.setOpenLinks(open_links)
        text_browser.setContextMenuPolicy(Qt.NoContextMenu)
        text_browser.setText(txt)
        return self.add_widget(text_browser)

    def create_text(self,
                    txt='',
                    read_only=False,
                    line_wrap=True,
                    is_html=True,
                    font=None):
        from pyvmmonitor_qt.qt.QtWidgets import QTextEdit
        widget = QTextEdit(self._widget)
        widget.setReadOnly(read_only)
        if line_wrap:
            widget.setLineWrapMode(QTextEdit.WidgetWidth)
        else:
            widget.setLineWrapMode(QTextEdit.NoWrap)
        if is_html:
            widget.setHtml(txt)
        else:
            widget.setText(txt)

        if font is not None:
            widget.setFont(font)
        return self.add_widget(widget)

    def add_qtree_view(self):
        from pyvmmonitor_qt.qt.QtWidgets import QTreeView
        return self.add_widget(QTreeView(self._widget))

    def create_spacer(self):
        from pyvmmonitor_qt.qt_utils import add_expanding_spacer_to_layout
        return add_expanding_spacer_to_layout(self._layout)

    def create_custom_buttons(self, label_and_callback):
        '''
        i.e.: create_custom_buttons(
        [
            ['Apply', QDialogButtonBox.ApplyRole],
            ['Ok', QDialogButtonBox.Ok],
            ['Cancel', QDialogButtonBox.Cancel],
        ]
        )
        '''
        from pyvmmonitor_qt.qt.QtWidgets import QDialogButtonBox
        roles = iter([
            QDialogButtonBox.ApplyRole, QDialogButtonBox.AcceptRole,
            QDialogButtonBox.ResetRole, QDialogButtonBox.RejectRole
        ])

        bbox = QDialogButtonBox()
        for label, callback in label_and_callback:
            button = bbox.addButton(label, next(roles))
            button.clicked.connect(callback)
        return self.add_widget(bbox)

    def create_buttons(self, show_ok=True, show_cancel=True):
        '''
        Note that clients should connect to the QDialogButtonBox rejected and accepted signals:

        bbox.rejected.connect(self.reject)
        bbox.accepted.connect(self.accept)
        '''
        assert show_ok or show_cancel
        from pyvmmonitor_qt.qt.QtWidgets import QDialogButtonBox

        if show_ok and show_cancel:
            flags = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
        elif show_ok:
            flags = QDialogButtonBox.Ok
        else:
            flags = QDialogButtonBox.Cancel

        bbox = QDialogButtonBox(flags)
        return self.add_widget(bbox)

    def create_close_button(self):
        from pyvmmonitor_qt.qt.QtWidgets import QDialogButtonBox
        flags = QDialogButtonBox.Close

        bbox = QDialogButtonBox(flags)
        return self.add_widget(bbox)
 def __init__(self, parent, model):
     QWidget.__init__(self, parent)
     self._model = model
     model.register_modified(self._on_modified)
 def __init__(self, parent, model):
     QWidget.__init__(self, parent)
     self._model = model
     model.register_modified(self._on_modified)
示例#26
0
 def mouseReleaseEvent(self, ev):
     self._last_pos = None
     return QWidget.mouseReleaseEvent(self, ev)
示例#27
0
class WidgetBuilder(object):
    '''
    Helper to build a widget adding components in a vertical or horizontal layout.

    Use as:

    widget_builder = WidgetBuilder()
    widget_builder.create_widget()
    widget_builder.create_label()
    ...

    widget_builder.widget.show()
    '''

    def __init__(self, widget=None, layout=None):
        self._widget = widget
        self._layout = layout

    def create_widget(self, parent, layout='vertical', margin=0):
        '''
        :param parent:
        :param layout: 'vertical' or 'horizontal'
        '''
        from pyvmmonitor_qt.qt.QtWidgets import QWidget
        self._widget = QWidget(parent)

        self._layout = self.create_layout(layout, margin)
        self._widget.setLayout(self._layout)

    @property
    def widget(self):
        return self._widget

    @classmethod
    def create_layout(cls, layout='vertical', margin=0):
        if layout == 'vertical':
            from pyvmmonitor_qt.qt.QtWidgets import QVBoxLayout
            layout = QVBoxLayout()
        else:
            from pyvmmonitor_qt.qt.QtWidgets import QHBoxLayout
            layout = QHBoxLayout()

        layout.setContentsMargins(margin, margin, margin, margin)
        return layout

    def add_widget(self, widget, layout=None):
        if layout is None:
            layout = self._layout
        if layout is None:
            raise RuntimeError('layout must be passed at constructor or created in create_widget.')
        layout.addWidget(widget)
        return widget

    def create_label(self, txt='', layout=None):
        from pyvmmonitor_qt.qt.QtWidgets import QLabel
        widget = QLabel(self._widget)
        widget.setText(txt)
        return self.add_widget(widget, layout=layout)

    def create_line_edit(self):
        from pyvmmonitor_qt.qt.QtWidgets import QLineEdit
        widget = QLineEdit(self._widget)
        return self.add_widget(widget)

    def create_text_browser(self, txt='', open_links=False):
        from pyvmmonitor_qt.qt.QtWidgets import QTextBrowser
        from pyvmmonitor_qt.qt.QtCore import Qt
        text_browser = QTextBrowser(self._widget)
        text_browser.setOpenExternalLinks(open_links)
        text_browser.setOpenLinks(open_links)
        text_browser.setContextMenuPolicy(Qt.NoContextMenu)
        text_browser.setText(txt)
        return self.add_widget(text_browser)

    def create_text(self, txt='', read_only=False, line_wrap=True, is_html=True, font=None):
        from pyvmmonitor_qt.qt.QtWidgets import QTextEdit
        widget = QTextEdit(self._widget)
        widget.setReadOnly(read_only)
        if line_wrap:
            widget.setLineWrapMode(QTextEdit.WidgetWidth)
        else:
            widget.setLineWrapMode(QTextEdit.NoWrap)
        if is_html:
            widget.setHtml(txt)
        else:
            widget.setText(txt)

        if font is not None:
            widget.setFont(font)
        return self.add_widget(widget)

    def add_qtree_view(self):
        from pyvmmonitor_qt.qt.QtWidgets import QTreeView
        return self.add_widget(QTreeView(self._widget))

    def create_spacer(self):
        from pyvmmonitor_qt.qt_utils import add_expanding_spacer_to_layout
        return add_expanding_spacer_to_layout(self._layout)

    def create_custom_buttons(self, label_and_callback):
        '''
        i.e.: create_custom_buttons(
        [
            ['Apply', QDialogButtonBox.ApplyRole],
            ['Ok', QDialogButtonBox.Ok],
            ['Cancel', QDialogButtonBox.Cancel],
        ]
        )
        '''
        from pyvmmonitor_qt.qt.QtWidgets import QDialogButtonBox
        roles = iter([
            QDialogButtonBox.ApplyRole,
            QDialogButtonBox.AcceptRole,
            QDialogButtonBox.ResetRole,
            QDialogButtonBox.RejectRole
        ])

        bbox = QDialogButtonBox()
        for label, callback in label_and_callback:
            button = bbox.addButton(label, next(roles))
            button.clicked.connect(callback)
        return self.add_widget(bbox)

    def create_buttons(self, show_ok=True, show_cancel=True):
        '''
        Note that clients should connect to the QDialogButtonBox rejected and accepted signals:

        bbox.rejected.connect(self.reject)
        bbox.accepted.connect(self.accept)
        '''
        assert show_ok or show_cancel
        from pyvmmonitor_qt.qt.QtWidgets import QDialogButtonBox

        if show_ok and show_cancel:
            flags = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
        elif show_ok:
            flags = QDialogButtonBox.Ok
        else:
            flags = QDialogButtonBox.Cancel

        bbox = QDialogButtonBox(flags)
        return self.add_widget(bbox)

    def create_close_button(self):
        from pyvmmonitor_qt.qt.QtWidgets import QDialogButtonBox
        flags = QDialogButtonBox.Close

        bbox = QDialogButtonBox(flags)
        return self.add_widget(bbox)