Exemple #1
0
    def edit_cell ( self, parent, row, column ):
        """ Returns the editor widget to use for editing a specified cell's
            value.
        """
        self.cell_row = self.data_row_for( row )

        # Get the editor factory to use. If none, exit (read-only cell):
        editor_factory = self.grid_adapter.get_editor( self.cell_row, column )
        if editor_factory is None:
            return None

        # Create the requested type of editor from the editor factory:
        # Note: We save the editor reference so that the editor doesn't get
        # garbage collected too soon.
        self.cell_column = column
        object, name     = self.grid_adapter.get_alias(  self.cell_row, column )
        editor           = editor_factory.simple_editor(
                               self.ui, object, name, '' ).set(
                               item        = self.item,
                               object_name = '' )

        # Tell the editor to actually build the editing widget:
        editor.prepare( control_adapter( parent ) )

        # Make sure that the editor is a control (and not a layout):
        self._editor = editor
        control      = editor.control
        if not isinstance( control, QWidget ):
            layout  = control
            control = QWidget( parent )
            control.setLayout( layout )
            layout.setContentsMargins( 5, 0, 5, 0 )

        control._editor = editor

        # Adjust the row height of the grid row to fit the editor control:
        height = control.height()
        if height > self.height:
            if (self.height - height) <= 4:
                # If the difference is not too great, then we will just adjust
                # the grid height to accomodate it:
                self.row_height( height )
            else:
                # Otherwise, just adjust the height of this row temporarily:
                control._row = row
                self.control.verticalHeader().resizeSection( row, height )

        # Resize the grid column width to fit the editor if necessary:
        width        = control.width()
        header       = self.control.horizontalHeader()
        column_width = header.sectionSize( column )
        if width > column_width:
            control._column = column
            control._width  = column_width
            header.resizeSection( column, width )

        # Return the editing widget as the result:
        return control
class StatusScrollArea(QScrollArea):
    def __init__(self):
        super(StatusScrollArea, self).__init__()

        self._width = 0
        self._group_count = 0

        self._pan_pos = None

        self.__create_ui()
        self.__init_ui()

    def __create_ui(self):
        self.container = QWidget()
        self.container_layout = QHBoxLayout()

        self.container.setLayout(self.container_layout)
        self.setWidget(self.container)

    def __init_ui(self):
        self.container.setFixedHeight(TOOLBAR_BUTTON_SIZE)
        self.container_layout.setContentsMargins(0, 0, 0, 0)
        self.container_layout.setSpacing(1)
        self.container_layout.setAlignment(Qt.AlignLeft)

        self.setFixedHeight(TOOLBAR_BUTTON_SIZE)
        self.setFocusPolicy(Qt.NoFocus)
        self.setFrameShape(self.NoFrame)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

    def _update_width(self, expand_value):
        self._width += expand_value
        self.container.setFixedWidth(self._width + self._group_count)

    def mousePressEvent(self, event):
        if event.button() == Qt.MidButton:
            QApplication.setOverrideCursor(QCursor(Qt.SizeHorCursor))
            self._pan_pos = event.globalPos()
            event.accept()
        else:
            event.ignore()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.MidButton:
            QApplication.restoreOverrideCursor()
            self._pan_pos = None
            event.accept()
        else:
            event.ignore()

    def mouseMoveEvent(self, event):
        if self._pan_pos:
            h_bar = self.horizontalScrollBar()
            h_bar_pos = h_bar.sliderPosition()
            cursor_pos = event.globalPos()
            cursor_delta = (cursor_pos - self._pan_pos).x()

            h_bar.setValue(h_bar_pos - cursor_delta)
            self._pan_pos = cursor_pos
            event.accept()
        else:
            event.ignore()

    def wheelEvent(self, event):
        if event.orientation() == Qt.Vertical:
            num_degrees = event.delta() / 8
            h_bar = self.horizontalScrollBar()
            h_bar_pos = h_bar.sliderPosition()

            h_bar.setValue(h_bar_pos - num_degrees)
        else:
            super(StatusScrollArea, self).wheelEvent(event)

    def resizeEvent(self, event):
        max_scroll = max(0, self.container.width() - event.size().width())
        self.horizontalScrollBar().setMaximum(max_scroll)

    def add_widget(self, widget):
        # add widget to layout
        self.container_layout.addWidget(widget)

        # connect widget for future update when user interact with it
        widget.toggled.connect(self._update_width)

        # expand widget layout
        self._width += widget.max_length()
        self._group_count += 1
        self.container.setFixedWidth(self._width)