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