QScintilla/EditorButtonsWidget.py

changeset 5411
a163fbbf2bea
parent 5407
f833f89571b8
child 5412
db5a520f69d3
equal deleted inserted replaced
5409:c6f4a6f0d051 5411:a163fbbf2bea
8 editor actions. 8 editor actions.
9 """ 9 """
10 10
11 from __future__ import unicode_literals 11 from __future__ import unicode_literals
12 12
13 from PyQt5.QtCore import pyqtSlot 13 from PyQt5.QtCore import pyqtSlot, Qt
14 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame, QMenu 14 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame, QMenu, \
15 QSizePolicy, QScrollArea
15 16
16 import UI.PixmapCache 17 import UI.PixmapCache
17 18
18 from . import MarkupProviders 19 from . import MarkupProviders
19 20
34 """ 35 """
35 super(EditorButtonsWidget, self).__init__(parent) 36 super(EditorButtonsWidget, self).__init__(parent)
36 37
37 margin = 2 38 margin = 2
38 spacing = 3 39 spacing = 3
39 self.__layout = QVBoxLayout(self) 40
40 self.__layout.setContentsMargins(margin, margin, margin, margin) 41 self.__buttonsWidget = QWidget(self)
42
43 self.__layout = QVBoxLayout(self.__buttonsWidget)
44 self.__layout.setContentsMargins(0, 0, 0, 0)
41 self.__layout.setSpacing(spacing) 45 self.__layout.setSpacing(spacing)
42 46
43 self.__provider = None 47 self.__provider = None
44 48
45 self.__editor = editor 49 self.__editor = editor
49 self.__editor.selectionChanged.connect(self.__editorSelectionChanged) 53 self.__editor.selectionChanged.connect(self.__editorSelectionChanged)
50 54
51 self.__createButtons() 55 self.__createButtons()
52 56
53 self.__layout.addStretch() 57 self.__layout.addStretch()
58
59 self.__outerLayout = QVBoxLayout(self)
60 self.__outerLayout.setContentsMargins(margin, margin, margin, margin)
61 self.__outerLayout.setSpacing(spacing)
62 self.__outerLayout.setAlignment(Qt.AlignHCenter)
63
64 self.__upButton = QToolButton(self)
65 self.__upButton.setArrowType(Qt.UpArrow)
66 self.__upButton.setSizePolicy(
67 QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
68 self.__upButton.setAutoRepeat(True)
69
70 self.__scroller = QScrollArea(self)
71 self.__scroller.setWidget(self.__buttonsWidget)
72 self.__scroller.setSizePolicy(
73 QSizePolicy.Minimum, QSizePolicy.Expanding)
74 self.__scroller.setFrameShape(QFrame.NoFrame)
75 self.__scroller.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
76 self.__scroller.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
77 self.__scroller.setWidgetResizable(False)
78
79 self.__downButton = QToolButton(self)
80 self.__downButton.setArrowType(Qt.DownArrow)
81 self.__downButton.setSizePolicy(
82 QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
83 self.__downButton.setAutoRepeat(True)
84
85 self.__outerLayout.addWidget(self.__upButton)
86 self.__outerLayout.addWidget(self.__scroller)
87 self.__outerLayout.addWidget(self.__downButton)
88
89 self.__upButton.clicked.connect(self.__slideUp)
90 self.__downButton.clicked.connect(self.__slideDown)
91
54 self.setMaximumWidth( 92 self.setMaximumWidth(
55 self.__buttons["bold"].sizeHint().width() + 2 * margin) 93 self.__buttons["bold"].sizeHint().width() + 2 * margin)
56 94
57 self.__updateButtonStates() 95 self.__updateButtonStates()
96
97 #######################################################################
98 ## Methods below implement some event handlers
99 #######################################################################
100
101 def show(self):
102 """
103 Public slot to show the widget.
104 """
105 super(EditorButtonsWidget, self).show()
106 self.__enableScrollerButtons()
107
108 def resizeEvent(self, evt):
109 """
110 Protected method to handle resize events.
111
112 @param evt reference to the resize event (QResizeEvent)
113 """
114 self.__enableScrollerButtons()
115 super(EditorButtonsWidget, self).resizeEvent(evt)
116
117 #######################################################################
118 ## Methods below implement scroller related functions
119 #######################################################################
120
121 def __enableScrollerButtons(self):
122 """
123 Private method to set the enabled state of the scroll buttons.
124 """
125 scrollBar = self.__scroller.verticalScrollBar()
126 self.__upButton.setEnabled(scrollBar.value() > 0)
127 self.__downButton.setEnabled(scrollBar.value() < scrollBar.maximum())
128
129 def __slideUp(self):
130 """
131 Private slot to move the widget upwards, i.e. show contents to the
132 bottom.
133 """
134 self.__slide(True)
135
136 def __slideDown(self):
137 """
138 Private slot to move the widget downwards, i.e. show contents to
139 the top.
140 """
141 self.__slide(False)
142
143 def __slide(self, up):
144 """
145 Private method to move the sliding widget.
146
147 @param up flag indicating to move upwards (boolean)
148 """
149 scrollBar = self.__scroller.verticalScrollBar()
150 stepSize = scrollBar.singleStep()
151 if up:
152 stepSize = -stepSize
153 newValue = scrollBar.value() + stepSize
154 if newValue < 0:
155 newValue = 0
156 elif newValue > scrollBar.maximum():
157 newValue = scrollBar.maximum()
158 scrollBar.setValue(newValue)
159 self.__enableScrollerButtons()
160
161 #######################################################################
162 ## Methods below implement the format button functions
163 #######################################################################
58 164
59 def __createButtons(self): 165 def __createButtons(self):
60 """ 166 """
61 Private slot to create the various tool buttons. 167 Private slot to create the various tool buttons.
62 """ 168 """
114 @param toolTip text for the tool tip 220 @param toolTip text for the tool tip
115 @type str 221 @type str
116 @return generated button 222 @return generated button
117 @rtype QToolButton 223 @rtype QToolButton
118 """ 224 """
119 button = QToolButton(self) 225 button = QToolButton(self.__buttonsWidget)
120 button.setIcon(UI.PixmapCache.getIcon(iconName)) 226 button.setIcon(UI.PixmapCache.getIcon(iconName))
121 button.setToolTip(toolTip) 227 button.setToolTip(toolTip)
122 button.clicked.connect(lambda: self.__formatClicked(format)) 228 button.clicked.connect(lambda: self.__formatClicked(format))
123 self.__layout.addWidget(button) 229 self.__layout.addWidget(button)
124 self.__buttons[format] = button 230 self.__buttons[format] = button
127 233
128 def __addSeparator(self): 234 def __addSeparator(self):
129 """ 235 """
130 Private method to add a separator line. 236 Private method to add a separator line.
131 """ 237 """
132 line = QFrame(self) 238 line = QFrame(self.__buttonsWidget)
133 line.setLineWidth(2) 239 line.setLineWidth(2)
134 if isinstance(self.__layout, QVBoxLayout): 240 if isinstance(self.__layout, QVBoxLayout):
135 line.setFrameShape(QFrame.HLine) 241 line.setFrameShape(QFrame.HLine)
136 else: 242 else:
137 line.setFrameShape(QFrame.VLine) 243 line.setFrameShape(QFrame.VLine)
167 act.setData("header{0}".format(level)) 273 act.setData("header{0}".format(level))
168 274
169 self.__buttons["code"].setEnabled(self.__provider.hasCode()) 275 self.__buttons["code"].setEnabled(self.__provider.hasCode())
170 self.__buttons["codeBlock"].setEnabled( 276 self.__buttons["codeBlock"].setEnabled(
171 self.__provider.hasCodeBlock()) 277 self.__provider.hasCodeBlock())
278
279 self.__buttons["bulletedList"].setEnabled(
280 self.__provider.hasBulletedList())
281 self.__buttons["numberedList"].setEnabled(
282 self.__provider.hasNumberedList())
172 283
173 self.__editorSelectionChanged() 284 self.__editorSelectionChanged()
174 285
175 # TODO: make this configurable 286 # TODO: make this configurable
176 self.setVisible(self.__provider.kind() != "none") 287 self.setVisible(self.__provider.kind() != "none")
204 self.__provider.hyperlink(self.__editor) 315 self.__provider.hyperlink(self.__editor)
205 elif format == "line": 316 elif format == "line":
206 self.__provider.line(self.__editor) 317 self.__provider.line(self.__editor)
207 elif format == "image": 318 elif format == "image":
208 self.__provider.image(self.__editor) 319 self.__provider.image(self.__editor)
320 elif format == "bulletedList":
321 self.__provider.bulletedList(self.__editor)
322 elif format == "numberedList":
323 self.__provider.numberedList(self.__editor)
209 324
210 def __headerMenuTriggered(self, act): 325 def __headerMenuTriggered(self, act):
211 """ 326 """
212 Private method handling the selection of a header menu entry. 327 Private method handling the selection of a header menu entry.
213 328

eric ide

mercurial