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