--- a/QScintilla/EditorButtonsWidget.py Tue Jan 10 15:35:08 2017 +0100 +++ b/QScintilla/EditorButtonsWidget.py Tue Jan 10 19:15:17 2017 +0100 @@ -10,8 +10,9 @@ from __future__ import unicode_literals -from PyQt5.QtCore import pyqtSlot -from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame, QMenu +from PyQt5.QtCore import pyqtSlot, Qt +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame, QMenu, \ + QSizePolicy, QScrollArea import UI.PixmapCache @@ -36,8 +37,11 @@ margin = 2 spacing = 3 - self.__layout = QVBoxLayout(self) - self.__layout.setContentsMargins(margin, margin, margin, margin) + + self.__buttonsWidget = QWidget(self) + + self.__layout = QVBoxLayout(self.__buttonsWidget) + self.__layout.setContentsMargins(0, 0, 0, 0) self.__layout.setSpacing(spacing) self.__provider = None @@ -51,10 +55,112 @@ self.__createButtons() self.__layout.addStretch() + + self.__outerLayout = QVBoxLayout(self) + self.__outerLayout.setContentsMargins(margin, margin, margin, margin) + self.__outerLayout.setSpacing(spacing) + self.__outerLayout.setAlignment(Qt.AlignHCenter) + + self.__upButton = QToolButton(self) + self.__upButton.setArrowType(Qt.UpArrow) + self.__upButton.setSizePolicy( + QSizePolicy.MinimumExpanding, QSizePolicy.Minimum) + self.__upButton.setAutoRepeat(True) + + self.__scroller = QScrollArea(self) + self.__scroller.setWidget(self.__buttonsWidget) + self.__scroller.setSizePolicy( + QSizePolicy.Minimum, QSizePolicy.Expanding) + self.__scroller.setFrameShape(QFrame.NoFrame) + self.__scroller.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + self.__scroller.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + self.__scroller.setWidgetResizable(False) + + self.__downButton = QToolButton(self) + self.__downButton.setArrowType(Qt.DownArrow) + self.__downButton.setSizePolicy( + QSizePolicy.MinimumExpanding, QSizePolicy.Minimum) + self.__downButton.setAutoRepeat(True) + + self.__outerLayout.addWidget(self.__upButton) + self.__outerLayout.addWidget(self.__scroller) + self.__outerLayout.addWidget(self.__downButton) + + self.__upButton.clicked.connect(self.__slideUp) + self.__downButton.clicked.connect(self.__slideDown) + self.setMaximumWidth( self.__buttons["bold"].sizeHint().width() + 2 * margin) self.__updateButtonStates() + + ####################################################################### + ## Methods below implement some event handlers + ####################################################################### + + def show(self): + """ + Public slot to show the widget. + """ + super(EditorButtonsWidget, self).show() + self.__enableScrollerButtons() + + def resizeEvent(self, evt): + """ + Protected method to handle resize events. + + @param evt reference to the resize event (QResizeEvent) + """ + self.__enableScrollerButtons() + super(EditorButtonsWidget, self).resizeEvent(evt) + + ####################################################################### + ## Methods below implement scroller related functions + ####################################################################### + + def __enableScrollerButtons(self): + """ + Private method to set the enabled state of the scroll buttons. + """ + scrollBar = self.__scroller.verticalScrollBar() + self.__upButton.setEnabled(scrollBar.value() > 0) + self.__downButton.setEnabled(scrollBar.value() < scrollBar.maximum()) + + def __slideUp(self): + """ + Private slot to move the widget upwards, i.e. show contents to the + bottom. + """ + self.__slide(True) + + def __slideDown(self): + """ + Private slot to move the widget downwards, i.e. show contents to + the top. + """ + self.__slide(False) + + def __slide(self, up): + """ + Private method to move the sliding widget. + + @param up flag indicating to move upwards (boolean) + """ + scrollBar = self.__scroller.verticalScrollBar() + stepSize = scrollBar.singleStep() + if up: + stepSize = -stepSize + newValue = scrollBar.value() + stepSize + if newValue < 0: + newValue = 0 + elif newValue > scrollBar.maximum(): + newValue = scrollBar.maximum() + scrollBar.setValue(newValue) + self.__enableScrollerButtons() + + ####################################################################### + ## Methods below implement the format button functions + ####################################################################### def __createButtons(self): """ @@ -116,7 +222,7 @@ @return generated button @rtype QToolButton """ - button = QToolButton(self) + button = QToolButton(self.__buttonsWidget) button.setIcon(UI.PixmapCache.getIcon(iconName)) button.setToolTip(toolTip) button.clicked.connect(lambda: self.__formatClicked(format)) @@ -129,7 +235,7 @@ """ Private method to add a separator line. """ - line = QFrame(self) + line = QFrame(self.__buttonsWidget) line.setLineWidth(2) if isinstance(self.__layout, QVBoxLayout): line.setFrameShape(QFrame.HLine) @@ -170,6 +276,11 @@ self.__buttons["codeBlock"].setEnabled( self.__provider.hasCodeBlock()) + self.__buttons["bulletedList"].setEnabled( + self.__provider.hasBulletedList()) + self.__buttons["numberedList"].setEnabled( + self.__provider.hasNumberedList()) + self.__editorSelectionChanged() # TODO: make this configurable @@ -206,6 +317,10 @@ self.__provider.line(self.__editor) elif format == "image": self.__provider.image(self.__editor) + elif format == "bulletedList": + self.__provider.bulletedList(self.__editor) + elif format == "numberedList": + self.__provider.numberedList(self.__editor) def __headerMenuTriggered(self, act): """