|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Editor Highlighter Associations configuration page. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from pygments.lexers import get_all_lexers |
|
15 |
|
16 from PyQt5.QtCore import Qt, pyqtSlot |
|
17 from PyQt5.QtWidgets import QHeaderView, QTreeWidgetItem |
|
18 |
|
19 from .ConfigurationPageBase import ConfigurationPageBase |
|
20 from .Ui_EditorHighlightersPage import Ui_EditorHighlightersPage |
|
21 |
|
22 import Preferences |
|
23 from Globals import qVersionTuple |
|
24 |
|
25 |
|
26 class EditorHighlightersPage(ConfigurationPageBase, Ui_EditorHighlightersPage): |
|
27 """ |
|
28 Class implementing the Editor Highlighter Associations configuration page. |
|
29 """ |
|
30 def __init__(self, lexers): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param lexers reference to the lexers dictionary |
|
35 """ |
|
36 super(EditorHighlightersPage, self).__init__() |
|
37 self.setupUi(self) |
|
38 self.setObjectName("EditorHighlightersPage") |
|
39 |
|
40 self.editorLexerList.headerItem().setText( |
|
41 self.editorLexerList.columnCount(), "") |
|
42 header = self.editorLexerList.header() |
|
43 if qVersionTuple() >= (5, 0, 0): |
|
44 header.setSectionResizeMode(QHeaderView.ResizeToContents) |
|
45 else: |
|
46 header.setResizeMode(QHeaderView.ResizeToContents) |
|
47 header.setSortIndicator(0, Qt.AscendingOrder) |
|
48 |
|
49 try: |
|
50 self.extsep = os.extsep |
|
51 except AttributeError: |
|
52 self.extsep = "." |
|
53 |
|
54 import QScintilla.Lexers |
|
55 self.extras = ["-----------", self.tr("Alternative")] |
|
56 languages = [''] + sorted(lexers.keys()) + self.extras |
|
57 for lang in languages: |
|
58 self.editorLexerCombo.addItem( |
|
59 QScintilla.Lexers.getLanguageIcon(lang, False), |
|
60 lang) |
|
61 |
|
62 pygmentsLexers = [''] + sorted(l[0] for l in get_all_lexers()) |
|
63 self.pygmentsLexerCombo.addItems(pygmentsLexers) |
|
64 |
|
65 # set initial values |
|
66 lexerAssocs = Preferences.getEditorLexerAssocs() |
|
67 for ext in lexerAssocs: |
|
68 QTreeWidgetItem(self.editorLexerList, [ext, lexerAssocs[ext]]) |
|
69 self.editorLexerList.sortByColumn(0, Qt.AscendingOrder) |
|
70 |
|
71 def save(self): |
|
72 """ |
|
73 Public slot to save the Editor Highlighter Associations configuration. |
|
74 """ |
|
75 lexerAssocs = {} |
|
76 for index in range( |
|
77 self.editorLexerList.topLevelItemCount()): |
|
78 itm = self.editorLexerList.topLevelItem(index) |
|
79 lexerAssocs[itm.text(0)] = itm.text(1) |
|
80 Preferences.setEditorLexerAssocs(lexerAssocs) |
|
81 |
|
82 @pyqtSlot() |
|
83 def on_addLexerButton_clicked(self): |
|
84 """ |
|
85 Private slot to add the lexer association displayed to the list. |
|
86 """ |
|
87 ext = self.editorFileExtEdit.text() |
|
88 if ext.startswith(self.extsep): |
|
89 ext.replace(self.extsep, "") |
|
90 lexer = self.editorLexerCombo.currentText() |
|
91 if lexer in self.extras: |
|
92 pygmentsLexer = self.pygmentsLexerCombo.currentText() |
|
93 if not pygmentsLexer: |
|
94 lexer = pygmentsLexer |
|
95 else: |
|
96 lexer = "Pygments|{0}".format(pygmentsLexer) |
|
97 if ext and lexer: |
|
98 itmList = self.editorLexerList.findItems( |
|
99 ext, Qt.MatchFlags(Qt.MatchExactly), 0) |
|
100 if itmList: |
|
101 index = self.editorLexerList.indexOfTopLevelItem(itmList[0]) |
|
102 itm = self.editorLexerList.takeTopLevelItem(index) |
|
103 # __IGNORE_WARNING__ |
|
104 del itm |
|
105 QTreeWidgetItem(self.editorLexerList, [ext, lexer]) |
|
106 self.editorFileExtEdit.clear() |
|
107 self.editorLexerCombo.setCurrentIndex(0) |
|
108 self.pygmentsLexerCombo.setCurrentIndex(0) |
|
109 self.editorLexerList.sortItems( |
|
110 self.editorLexerList.sortColumn(), |
|
111 self.editorLexerList.header().sortIndicatorOrder()) |
|
112 |
|
113 @pyqtSlot() |
|
114 def on_deleteLexerButton_clicked(self): |
|
115 """ |
|
116 Private slot to delete the currently selected lexer association of the |
|
117 list. |
|
118 """ |
|
119 itmList = self.editorLexerList.selectedItems() |
|
120 if itmList: |
|
121 index = self.editorLexerList.indexOfTopLevelItem(itmList[0]) |
|
122 itm = self.editorLexerList.takeTopLevelItem(index) |
|
123 # __IGNORE_WARNING__ |
|
124 del itm |
|
125 |
|
126 self.editorLexerList.clearSelection() |
|
127 self.editorFileExtEdit.clear() |
|
128 self.editorLexerCombo.setCurrentIndex(0) |
|
129 |
|
130 def on_editorLexerList_itemClicked(self, itm, column): |
|
131 """ |
|
132 Private slot to handle the clicked signal of the lexer association |
|
133 list. |
|
134 |
|
135 @param itm reference to the selecte item (QTreeWidgetItem) |
|
136 @param column column the item was clicked or activated (integer) |
|
137 (ignored) |
|
138 """ |
|
139 if itm is None: |
|
140 self.editorFileExtEdit.clear() |
|
141 self.editorLexerCombo.setCurrentIndex(0) |
|
142 self.pygmentsLexerCombo.setCurrentIndex(0) |
|
143 else: |
|
144 self.editorFileExtEdit.setText(itm.text(0)) |
|
145 lexer = itm.text(1) |
|
146 if lexer.startswith("Pygments|"): |
|
147 pygmentsLexer = lexer.split("|")[1] |
|
148 pygmentsIndex = self.pygmentsLexerCombo.findText(pygmentsLexer) |
|
149 lexer = self.tr("Alternative") |
|
150 else: |
|
151 pygmentsIndex = 0 |
|
152 index = self.editorLexerCombo.findText(lexer) |
|
153 self.editorLexerCombo.setCurrentIndex(index) |
|
154 self.pygmentsLexerCombo.setCurrentIndex(pygmentsIndex) |
|
155 |
|
156 def on_editorLexerList_itemActivated(self, itm, column): |
|
157 """ |
|
158 Private slot to handle the activated signal of the lexer association |
|
159 list. |
|
160 |
|
161 @param itm reference to the selecte item (QTreeWidgetItem) |
|
162 @param column column the item was clicked or activated (integer) |
|
163 (ignored) |
|
164 """ |
|
165 self.on_editorLexerList_itemClicked(itm, column) |
|
166 |
|
167 @pyqtSlot(str) |
|
168 def on_editorLexerCombo_currentIndexChanged(self, text): |
|
169 """ |
|
170 Private slot to handle the selection of a lexer. |
|
171 |
|
172 @param text text of the lexer combo (string) |
|
173 """ |
|
174 if text in self.extras: |
|
175 self.pygmentsLexerCombo.setEnabled(True) |
|
176 self.pygmentsLabel.setEnabled(True) |
|
177 else: |
|
178 self.pygmentsLexerCombo.setEnabled(False) |
|
179 self.pygmentsLabel.setEnabled(False) |
|
180 |
|
181 |
|
182 def create(dlg): |
|
183 """ |
|
184 Module function to create the configuration page. |
|
185 |
|
186 @param dlg reference to the configuration dialog |
|
187 @return reference to the instantiated page (ConfigurationPageBase) |
|
188 """ |
|
189 page = EditorHighlightersPage(dlg.getLexers()) |
|
190 return page |