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