|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a widget containing various buttons for accessing |
|
8 editor actions. |
|
9 """ |
|
10 |
|
11 from __future__ import unicode_literals |
|
12 |
|
13 from PyQt5.QtCore import pyqtSlot |
|
14 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton |
|
15 |
|
16 import UI.PixmapCache |
|
17 |
|
18 from . import MarkupProviders |
|
19 |
|
20 |
|
21 class EditorButtonsWidget(QWidget): |
|
22 """ |
|
23 Class implementing a widget containing various buttons for accessing |
|
24 editor actions. |
|
25 """ |
|
26 def __init__(self, editor, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param editor reference to the editor |
|
31 @type Editor |
|
32 @param parent reference to the parent widget |
|
33 @type QWidget |
|
34 """ |
|
35 super(EditorButtonsWidget, self).__init__(parent) |
|
36 |
|
37 self.__layout = QVBoxLayout(self) |
|
38 self.__layout.setContentsMargins(1, 1, 1, 1) |
|
39 self.__layout.setSpacing(1) |
|
40 |
|
41 self.__provider = None |
|
42 |
|
43 self.__editor = editor |
|
44 self.__editor.languageChanged.connect(self.__updateButtonStates) |
|
45 self.__editor.editorSaved.connect(self.__updateButtonStates) |
|
46 self.__editor.editorRenamed.connect(self.__updateButtonStates) |
|
47 |
|
48 self.__createButtons() |
|
49 |
|
50 self.__layout.addStretch() |
|
51 self.setMaximumWidth(self.__buttons["bold"].sizeHint().width() + 2) |
|
52 |
|
53 self.__updateButtonStates() |
|
54 |
|
55 def __createButtons(self): |
|
56 """ |
|
57 Private slot to create the various tool buttons. |
|
58 """ |
|
59 self.__buttons = {} |
|
60 |
|
61 button = QToolButton(self) |
|
62 button.setIcon(UI.PixmapCache.getIcon("formatTextBold.png")) |
|
63 button.clicked.connect(lambda: self.__formatClicked("bold")) |
|
64 self.__layout.addWidget(button) |
|
65 self.__buttons["bold"] = button |
|
66 |
|
67 button = QToolButton(self) |
|
68 button.setIcon(UI.PixmapCache.getIcon("formatTextItalic.png")) |
|
69 button.clicked.connect(lambda: self.__formatClicked("italic")) |
|
70 self.__layout.addWidget(button) |
|
71 self.__buttons["italic"] = button |
|
72 |
|
73 button = QToolButton(self) |
|
74 button.setIcon(UI.PixmapCache.getIcon("formatTextStrikethrough.png")) |
|
75 button.clicked.connect(lambda: self.__formatClicked("strikethrough")) |
|
76 self.__layout.addWidget(button) |
|
77 self.__buttons["strikethrough"] = button |
|
78 |
|
79 @pyqtSlot() |
|
80 def __updateButtonStates(self): |
|
81 """ |
|
82 Private slot to change tzhe button states. |
|
83 """ |
|
84 self.__provider = MarkupProviders.getMarkupProvider(self.__editor) |
|
85 |
|
86 self.__buttons["bold"].setEnabled(self.__provider.hasBold()) |
|
87 self.__buttons["italic"].setEnabled(self.__provider.hasItalic()) |
|
88 self.__buttons["strikethrough"].setEnabled( |
|
89 self.__provider.hasStrikethrough()) |
|
90 |
|
91 def __formatClicked(self, format): |
|
92 """ |
|
93 Private slot to handle a format button being clicked. |
|
94 |
|
95 @param format format type of the button |
|
96 @type str |
|
97 """ |
|
98 if format == "bold": |
|
99 self.__provider.bold(self.__editor) |
|
100 elif format == "italic": |
|
101 self.__provider.italic(self.__editor) |
|
102 elif format == "strikethrough": |
|
103 self.__provider.strikethrough(self.__editor) |