QScintilla/EditorButtonsWidget.py

Sat, 07 Jan 2017 19:35:40 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 07 Jan 2017 19:35:40 +0100
changeset 5404
6b19ad5470a3
parent 5402
ce21a78a5fcf
child 5407
f833f89571b8
permissions
-rw-r--r--

Continued implementing a format button bar and provider classes for various markup languages.

5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de>
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a widget containing various buttons for accessing
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 editor actions.
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from __future__ import unicode_literals
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt5.QtCore import pyqtSlot
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
14 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame, QMenu
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import UI.PixmapCache
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from . import MarkupProviders
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 class EditorButtonsWidget(QWidget):
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 Class implementing a widget containing various buttons for accessing
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 editor actions.
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 def __init__(self, editor, parent=None):
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Constructor
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 @param editor reference to the editor
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 @type Editor
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @param parent reference to the parent widget
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @type QWidget
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 super(EditorButtonsWidget, self).__init__(parent)
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
37 margin = 2
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.__layout = QVBoxLayout(self)
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
39 self.__layout.setContentsMargins(margin, margin, margin, margin)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
40 self.__layout.setSpacing(2)
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.__provider = None
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.__editor = editor
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.__editor.languageChanged.connect(self.__updateButtonStates)
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.__editor.editorSaved.connect(self.__updateButtonStates)
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.__editor.editorRenamed.connect(self.__updateButtonStates)
5404
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
48 self.__editor.selectionChanged.connect(self.__editorSelectionChanged)
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 self.__createButtons()
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.__layout.addStretch()
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
53 self.setMaximumWidth(
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
54 self.__buttons["bold"].sizeHint().width() + 2 * margin)
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.__updateButtonStates()
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 def __createButtons(self):
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 Private slot to create the various tool buttons.
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.__buttons = {}
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
63 self.__separators = []
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
64 self.__headerMenu = QMenu()
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
66 self.__addButton("bold", "formatTextBold.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
67 self.__addButton("italic", "formatTextItalic.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
68 self.__addButton("strikethrough", "formatTextStrikethrough.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
69 self.__addSeparator()
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
70 self.__addButton("header1", "formatTextHeader1.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
71 self.__addButton("header2", "formatTextHeader2.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
72 self.__addButton("header3", "formatTextHeader3.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
73 button = self.__addButton("header", "formatTextHeader.png")
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
74 button.setPopupMode(QToolButton.InstantPopup)
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
75 button.setMenu(self.__headerMenu)
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
76 self.__addSeparator()
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
77 self.__addButton("code", "formatTextInlineCode.png")
5404
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
78 self.__addButton("codeBlock", "formatTextCodeBlock.png")
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
79 self.__addSeparator()
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
80 self.__addButton("hyperlink", "formatTextHyperlink.png")
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
81 self.__addButton("line", "formatTextHorizontalLine.png")
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
82
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
83 self.__headerMenu.triggered.connect(self.__headerMenuTriggered)
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
84
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
85 def __addButton(self, format, iconName):
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
86 """
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
87 Private method to add a format button.
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
89 @param format unique name of the format
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
90 @type str
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
91 @param iconName name of the icon for the button
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
92 @type str
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
93 @return generated button
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
94 @rtype QToolButton
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
95 """
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 button = QToolButton(self)
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
97 button.setIcon(UI.PixmapCache.getIcon(iconName))
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
98 button.clicked.connect(lambda: self.__formatClicked(format))
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 self.__layout.addWidget(button)
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
100 self.__buttons[format] = button
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
102 return button
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
103
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
104 def __addSeparator(self):
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
105 """
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
106 Private method to add a separator line.
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
107 """
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
108 line = QFrame(self)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
109 line.setLineWidth(2)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
110 if isinstance(self.__layout, QVBoxLayout):
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
111 line.setFrameShape(QFrame.HLine)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
112 else:
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
113 line.setFrameShape(QFrame.VLine)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
114 line.setFrameShadow(QFrame.Sunken)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
115
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
116 self.__layout.addWidget(line)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
117 self.__separators.append(line)
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 @pyqtSlot()
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 def __updateButtonStates(self):
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 """
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
122 Private slot to change the button states.
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 """
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
124 provider = MarkupProviders.getMarkupProvider(self.__editor)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
125 if self.__provider is None or \
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
126 provider.kind() != self.__provider.kind():
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
127 self.__provider = provider
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
128
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
129 self.__buttons["bold"].setEnabled(self.__provider.hasBold())
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
130 self.__buttons["italic"].setEnabled(self.__provider.hasItalic())
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
131 self.__buttons["strikethrough"].setEnabled(
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
132 self.__provider.hasStrikethrough())
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
133
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
134 headerLevels = self.__provider.headerLevels()
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
135 self.__buttons["header1"].setEnabled(headerLevels >= 1)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
136 self.__buttons["header2"].setEnabled(headerLevels >= 2)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
137 self.__buttons["header3"].setEnabled(headerLevels >= 3)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
138 self.__buttons["header"].setEnabled(headerLevels > 3)
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
139 self.__headerMenu.clear()
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
140 for level in range(1, headerLevels + 1):
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
141 act = self.__headerMenu.addAction(
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
142 self.tr("Level {0}").format(level))
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
143 act.setData("header{0}".format(level))
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
144
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
145 self.__buttons["code"].setEnabled(self.__provider.hasCode())
5404
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
146 self.__buttons["codeBlock"].setEnabled(
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
147 self.__provider.hasCodeBlock())
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
148
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
149 self.__editorSelectionChanged()
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
150
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
151 # TODO: make this configurable
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
152 self.setVisible(self.__provider.kind() != "none")
5394
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 def __formatClicked(self, format):
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 Private slot to handle a format button being clicked.
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 @param format format type of the button
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 @type str
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 if format == "bold":
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self.__provider.bold(self.__editor)
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 elif format == "italic":
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.__provider.italic(self.__editor)
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 elif format == "strikethrough":
b2c6179184f6 Started implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 self.__provider.strikethrough(self.__editor)
5398
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
167 elif format.startswith("header"):
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
168 try:
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
169 level = int(format[-1])
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
170 self.__provider.header(self.__editor, level)
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
171 except ValueError:
1f4509cf8f35 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5394
diff changeset
172 pass
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
173 elif format == "code":
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
174 self.__provider.code(self.__editor)
5404
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
175 elif format == "codeBlock":
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
176 self.__provider.codeBlock(self.__editor)
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
177 elif format == "hyperlink":
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
178 self.__provider.hyperlink(self.__editor)
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
179 elif format == "line":
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
180 self.__provider.line(self.__editor)
5402
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
181
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
182 def __headerMenuTriggered(self, act):
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
183 """
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
184 Private method handling the selection of a header menu entry.
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
185
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
186 @param act action of the headers menu that was triggered
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
187 @type QAction
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
188 """
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
189 format = act.data()
ce21a78a5fcf Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5398
diff changeset
190 self.__formatClicked(format)
5404
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
191
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
192 def __editorSelectionChanged(self):
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
193 """
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
194 Private slot to handle a change of the editor's selection.
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
195 """
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
196 hasSelection = self.__editor.hasSelectedText()
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
197 if self.__provider:
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
198 self.__buttons["hyperlink"].setEnabled(
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
199 self.__provider.hasHyperlink() and not hasSelection)
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
200 self.__buttons["line"].setEnabled(
6b19ad5470a3 Continued implementing a format button bar and provider classes for various markup languages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5402
diff changeset
201 self.__provider.hasLine() and not hasSelection)

eric ide

mercurial