~sir-rainbow/+junk/scribes-on-win

« back to all changes in this revision

Viewing changes to plugins/TemplateEditor/LanguageTreeView.py

  • Committer: goldenmyst
  • Date: 2007-09-25 17:15:52 UTC
  • Revision ID: goldenmyst@goldenmyst-desktop-20070925171552-mvrhxdd39iibs0sr
New branch. New Trigger Management System. New Trigger API. New Plugin Management System. Fix for bug triggered by PyGTK+ version 2.11 or better.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright © 2007 Lateef Alabi-Oki
 
3
#
 
4
# This file is part of Scribes.
 
5
#
 
6
# Scribes is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# Scribes is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with Scribes; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 
19
# USA
 
20
 
 
21
"""
 
22
This module documents a class that defines the behavior of the template
 
23
editor's language treeview.
 
24
 
 
25
@author: Lateef Alabi-Oki
 
26
@organization: The Scribes Project
 
27
@copyright: Copyright © 2007 Lateef Alabi-Oki
 
28
@license: GNU GPLv2 or Later
 
29
@contact: mystilleef@gmail.com
 
30
"""
 
31
 
 
32
class TemplateLanguageTreeView(object):
 
33
        """
 
34
        This class creates an object that defines the behavior of the
 
35
        template editor's language treeview.
 
36
        """
 
37
 
 
38
        def __init__(self, manager, editor):
 
39
                """
 
40
                Initialize object.
 
41
 
 
42
                @param self: Reference to the TemplateLanguageTreeView instance.
 
43
                @type self: A TemplateLanguageTreeView object.
 
44
 
 
45
                @param manager: Reference to the TemplateManager instance.
 
46
                @type manager: A TemplateManager object.
 
47
 
 
48
                @param editor: Reference to the text editor.
 
49
                @type editor: An Editor object.
 
50
                """
 
51
                self.__init_attributes(manager, editor)
 
52
                self.__set_properties()
 
53
                self.__populate_model()
 
54
                self.__select_language()
 
55
                self.__signal_id_1 = manager.connect("destroy", self.__destroy_cb)
 
56
                self.__signal_id_2 = self.__editor.connect("loaded-document", self.__generic_cb)
 
57
                self.__signal_id_3 = self.__editor.connect("renamed-document", self.__generic_cb)
 
58
                self.__signal_id_4 = manager.connect("show", self.__show_cb)
 
59
                self.__signal_id_5 = self.__treeview.connect("cursor-changed", self.__cursor_changed_cb)
 
60
                self.__signal_id_6 = manager.connect("imported-language", self.__imported_language_cb)
 
61
                self.__signal_id_7 = self.__treeview.connect("drag-data-get", self.__drag_data_get_cb)
 
62
                self.__signal_id_8 = self.__treeview.connect("drag-data-received", self.__drag_data_received_cb)
 
63
 
 
64
        def __init_attributes(self, manager, editor):
 
65
                """
 
66
                Initialize data attributes.
 
67
 
 
68
                @param self: Reference to the TemplateLanguageTreeView instance.
 
69
                @type self: A TemplateLanguageTreeView object.
 
70
 
 
71
                @param manager: Reference to the TemplateManager instance.
 
72
                @type manager: A TemplateManager object.
 
73
 
 
74
                @param editor: Reference to the text editor.
 
75
                @type editor: An Editor object.
 
76
                """
 
77
                self.__manager = manager
 
78
                self.__editor = editor
 
79
                self.__treeview = manager.glade.get_widget("LanguageTreeView")
 
80
                self.__is_first_time = True
 
81
                self.__model = self.__create_model()
 
82
                self.__renderer = self.__create_renderer()
 
83
                self.__column = self.__create_column()
 
84
                from gtksourceview import SourceLanguagesManager
 
85
                language_list = SourceLanguagesManager().get_available_languages()
 
86
                self.__languages = [name.get_id() for name in language_list]
 
87
                self.__signal_id_1 = self.__signal_id_2 = self.__signal_id_3 = None
 
88
                self.__signal_id_4 = self.__signal_id_5 = self.__signal_id_6 = None
 
89
                self.__signal_id_7 = self.__signal_id_8 = None
 
90
                return
 
91
 
 
92
        def __set_properties(self):
 
93
                """
 
94
                Set the language view's default properties.
 
95
 
 
96
                @param self: Reference to the TemplateEditorLanguageView instance.
 
97
                @type self: A TemplateEditorLanguageView object.
 
98
                """
 
99
                from gtk.gdk import ACTION_DEFAULT, BUTTON1_MASK
 
100
                from gtk import TARGET_SAME_APP
 
101
                self.__treeview.enable_model_drag_source(BUTTON1_MASK, [("STRING", 0, 123)], ACTION_DEFAULT)
 
102
                self.__treeview.enable_model_drag_dest([("STRING", TARGET_SAME_APP, 124)], ACTION_DEFAULT)
 
103
#               self.__treeview.set_property("rules-hint", True)
 
104
#               self.__treeview.set_property("search-column", 0)
 
105
#               self.__treeview.set_property("headers-clickable", True)
 
106
                self.__treeview.append_column(self.__column)
 
107
                self.__treeview.set_model(self.__model)
 
108
                self.__column.clicked()
 
109
                return
 
110
 
 
111
        def __create_model(self):
 
112
                """
 
113
                Create the model for the template editor's language view.
 
114
 
 
115
                @param self: Reference to the TemplateEditorLanguageView instance.
 
116
                @type self: A TemplateEditorLanguageView object.
 
117
 
 
118
                @return: A model for the language view.
 
119
                @rtype: A gtk.ListStore object.
 
120
                """
 
121
                from gtk import ListStore
 
122
                model = ListStore(str, str)
 
123
                return model
 
124
 
 
125
        def __create_column(self):
 
126
                """
 
127
                Create the column for the template editor's language view.
 
128
 
 
129
                @param self: Reference to the TemplateEditorLanguageView instance.
 
130
                @type self: A TemplateEditorLanguageView object.
 
131
 
 
132
                @return: A column for the language view.
 
133
                @rtype: A gtk.TreeViewColumn object.
 
134
                """
 
135
                from gtk import TreeViewColumn, TREE_VIEW_COLUMN_GROW_ONLY
 
136
                from gtk import SORT_ASCENDING
 
137
                from i18n import msg0001
 
138
                column = TreeViewColumn(msg0001, self.__renderer, text=0)
 
139
                column.set_property("expand", False)
 
140
                column.set_property("sizing", TREE_VIEW_COLUMN_GROW_ONLY)
 
141
                column.set_property("clickable", True)
 
142
                column.set_sort_column_id(0)
 
143
                column.set_property("sort-indicator", True)
 
144
                column.set_property("sort-order", SORT_ASCENDING)
 
145
                return column
 
146
 
 
147
        def __create_renderer(self):
 
148
                """
 
149
                Create the renderer for the language view's column
 
150
 
 
151
                @param self: Reference to the TemplateEditorLanguageView instance.
 
152
                @type self: A TemplateEditorLanguageView object.
 
153
 
 
154
                @return: A renderer for the language view.
 
155
                @rtype: A gtk.CellRendererText object.
 
156
                """
 
157
                from gtk import CellRendererText
 
158
                renderer = CellRendererText()
 
159
                return renderer
 
160
 
 
161
        def __populate_model(self):
 
162
                """
 
163
                Populate the language view's model.
 
164
 
 
165
                @param self: Reference to the TemplateLanguageTreeView instance.
 
166
                @type self: A TemplateLanguageTreeView object.
 
167
                """
 
168
                from gtksourceview import SourceLanguagesManager
 
169
                language_list = SourceLanguagesManager().get_available_languages()
 
170
                language_names = [(name.get_name(), name.get_id()) for name in language_list]
 
171
                self.__model.append(["General", "General"])
 
172
                for name, id in language_names:
 
173
                        self.__model.append([name, id])
 
174
                return
 
175
 
 
176
        def __select_language(self, language=None):
 
177
                """
 
178
                Select a row corresponding to the language in the tree view.
 
179
 
 
180
                @param self: Reference to the TemplateLanguageTreeView instance.
 
181
                @type self: A TemplateLanguageTreeView object.
 
182
                """
 
183
                from operator import eq, not_
 
184
                if not_(language):
 
185
                        language_id = self.__get_language()
 
186
                else:
 
187
                        language_id = language
 
188
                model = self.__model
 
189
                iterator = model.get_iter_first()
 
190
                language = model.get_value(iterator, 1)
 
191
                if eq(language, language_id):
 
192
                        selection = self.__treeview.get_selection()
 
193
                        selection.select_iter(iterator)
 
194
                        path = model.get_path(iterator)
 
195
                        self.__treeview.scroll_to_cell(path, self.__treeview.get_column(0), True, 0.5, 0.0)
 
196
                else:
 
197
                        while True:
 
198
                                iterator = model.iter_next(iterator)
 
199
                                if iterator:
 
200
                                        language = model.get_value(iterator, 1)
 
201
                                        if eq(language, language_id):
 
202
                                                selection = self.__treeview.get_selection()
 
203
                                                selection.select_iter(iterator)
 
204
                                                path = model.get_path(iterator)
 
205
                                                self.__treeview.scroll_to_cell(path, self.__treeview.get_column(0), True, 0.5, 0.0)
 
206
                                                break
 
207
                                else:
 
208
                                        break
 
209
                return
 
210
 
 
211
        def __get_language(self):
 
212
                """
 
213
                Get the current language of the file in the text editor.
 
214
 
 
215
                @param self: Reference to the TemplateLanguageTreeView instance.
 
216
                @type self: A TemplateLanguageTreeView object.
 
217
 
 
218
                @return: Return a language or "General"
 
219
                @rtype: A String object.
 
220
                """
 
221
                language = self.__editor.language
 
222
                from operator import not_
 
223
                if not_(language): return "General"
 
224
                return language.get_id()
 
225
 
 
226
        def __get_templates(self, language):
 
227
                templates = []
 
228
                from Metadata import open_template_database
 
229
                from Metadata import close_template_database
 
230
                from operator import eq, not_, ne
 
231
                same_prefix = lambda x: x.startswith(language)
 
232
                remove_language = lambda x: ne(x, language)
 
233
                similar_prefix = filter(remove_language, filter(same_prefix, self.__languages))
 
234
                database = open_template_database()
 
235
                for key in database.keys():
 
236
                        skip = False
 
237
                        for value in similar_prefix:
 
238
                                if key.startswith(value):
 
239
                                        skip = True
 
240
                                        break
 
241
                        if skip: continue
 
242
                        if key.startswith(language):
 
243
                                boolean = True
 
244
                                trigger = key.replace(language, "")
 
245
                                description = database[key][0]
 
246
                                template = database[key][1]
 
247
                                templates.append((language, trigger, description, template))
 
248
                close_template_database(database)
 
249
                return templates
 
250
 
 
251
        def __get_drop_row(self, path):
 
252
                """
 
253
                Get the language of the row drag and drop occurred.
 
254
 
 
255
                @param self: Reference to the TemplateEditorLanguageView instance.
 
256
                @type self: A TemplateEditorLanguageView object.
 
257
 
 
258
                @param path: The row a drag and drop occurred.
 
259
                @type path: A Tuple object.
 
260
 
 
261
                @return: The language identifier of the row a drag and drop occurred.
 
262
                @rtype: A String object.
 
263
                """
 
264
                model = self.__treeview.get_model()
 
265
                iterator = model.get_iter(path)
 
266
                language_id = model.get_value(iterator, 1)
 
267
                return language_id
 
268
 
 
269
        def __create_new_templates(self, templates, language):
 
270
                """
 
271
                Convert old template information to new one.
 
272
 
 
273
                @param self: Reference to the LanguageTreeView instance.
 
274
                @type self: A LanguageTreeView object.
 
275
 
 
276
                @param templates: Reference to the templates information.
 
277
                @type templates: A List object.
 
278
 
 
279
                @param language: Language category to convert templates to.
 
280
                @type language: A String object.
 
281
                """
 
282
                def new_template(data):
 
283
                        key, description, template, old_language = data
 
284
                        return language, key[len(language):], description, template
 
285
                templates = map(new_template, templates)
 
286
                return templates
 
287
 
 
288
        def __destroy_cb(self, manager):
 
289
                """
 
290
                Handles callback when the "destroy" signal is emitted.
 
291
 
 
292
                @param self: Reference to the TemplateLanguageTreeView instance.
 
293
                @type self: A TemplateLanguageTreeView object.
 
294
 
 
295
                @param manager: Reference to the TemplateManager instance.
 
296
                @type manager: A TemplateManager object.
 
297
                """
 
298
                from SCRIBES.utils import delete_attributes, disconnect_signal
 
299
                disconnect_signal(self.__signal_id_1, manager)
 
300
                disconnect_signal(self.__signal_id_2, self.__editor)
 
301
                disconnect_signal(self.__signal_id_3, self.__editor)
 
302
                disconnect_signal(self.__signal_id_4, manager)
 
303
                disconnect_signal(self.__signal_id_5, self.__treeview)
 
304
                disconnect_signal(self.__signal_id_6, manager)
 
305
                disconnect_signal(self.__signal_id_7, self.__treeview)
 
306
                disconnect_signal(self.__signal_id_8, self.__treeview)
 
307
                self.__model.clear()
 
308
                self.__treeview.destroy()
 
309
                delete_attributes(self)
 
310
                self = None
 
311
                del self
 
312
                return
 
313
 
 
314
        def __cursor_changed_cb(self, *args):
 
315
                """
 
316
                Handles callback when the "cursor-changed" signal is emitted.
 
317
 
 
318
                @param self: Reference to the TemplateLanguageTreeView instance.
 
319
                @type self: A TemplateLanguageTreeView object.
 
320
                """
 
321
                try:
 
322
                        selection = self.__treeview.get_selection()
 
323
                        model, iterator = selection.get_selected()
 
324
                        language_id = model.get_value(iterator, 1)
 
325
                        self.__manager.emit("language-selected", language_id)
 
326
                except TypeError:
 
327
                        pass
 
328
                return
 
329
 
 
330
        def __generic_cb(self, *args):
 
331
                """
 
332
                A generic callback handler to select a language.
 
333
 
 
334
                @param self: Reference to the TemplateLanguageTreeView instance.
 
335
                @type self: A TemplateLanguageTreeView object.
 
336
                """
 
337
                self.__select_language()
 
338
                return
 
339
 
 
340
        def __show_cb(self, *args):
 
341
                """
 
342
                Handles callback when the "show" signal is emitted.
 
343
 
 
344
                @param self: Reference to the TemplateLanguageTreeView instance.
 
345
                @type self: A TemplateLanguageTreeView object.
 
346
                """
 
347
                self.__treeview.grab_focus()
 
348
                from SCRIBES.utils import select_row
 
349
                select_row(self.__treeview)
 
350
                return
 
351
 
 
352
        def __imported_language_cb(self, manager, language):
 
353
                """
 
354
                Handles callback when the "imported-language" signal is emitted.
 
355
 
 
356
                @param self: Reference to the TemplateLanguageTreeView instance.
 
357
                @type self: A TemplateLanguageTreeView object.
 
358
 
 
359
                @param manager: Reference to the TemplateManager instance.
 
360
                @type manager: A TemplateManager object.
 
361
 
 
362
                @param language: A language category
 
363
                @type language: A String object.
 
364
                """
 
365
                self.__select_language(language)
 
366
                # No error occurred. I'm just using the signal to make the
 
367
                # template editor window sensitive.
 
368
                manager.emit("import-error")
 
369
                from SCRIBES.utils import select_row
 
370
                select_row(self.__treeview)
 
371
                self.__treeview.grab_focus()
 
372
                return
 
373
 
 
374
        def __drag_data_get_cb(self, treeview, context, data, info, time):
 
375
                try:
 
376
                        from Exceptions import NoDataError
 
377
                        selection = treeview.get_selection()
 
378
                        model, iterator = selection.get_selected()
 
379
                        language_id = model.get_value(iterator, 1)
 
380
                        templates = self.__get_templates(language_id)
 
381
                        from operator import not_
 
382
                        if not_(templates): raise NoDataError
 
383
                        from WriteXMLTemplate import create_template_string
 
384
                        string = create_template_string(templates)
 
385
                        data.set(data.target, 8, string)
 
386
                except NoDataError:
 
387
                        pass
 
388
                return True
 
389
 
 
390
        def __drag_data_received_cb(self, treeview, context, x, y, data, info, time):
 
391
                """
 
392
                Handles callback when the "drag-data-received" signal is emitted.
 
393
 
 
394
                @param self: Reference to the TemplateLanguageTreeView instance.
 
395
                @type self: A TemplateLanguageTreeView object.
 
396
 
 
397
                @param treeview: Reference to the TemplateEditorLanguageView.
 
398
                @type treeview: A TemplateEditorLanguageView object.
 
399
 
 
400
                @param context: An object containing data about a drag selection.
 
401
                @type context: A gtk.DragContextData object.
 
402
 
 
403
                @param x: The x-cordinate of the drop.
 
404
                @type x: An Integer object.
 
405
 
 
406
                @param y: The y-cordinate of the drop.
 
407
                @type y: An Integer object.
 
408
 
 
409
                @param selection_data: Data representing the drag selection.
 
410
                @type selection_data: A gtk.SelectionData object.
 
411
 
 
412
                @param info: A unique identification for the text editor.
 
413
                @type info: An Integer object.
 
414
 
 
415
                @param time: The time the drop operation occurred.
 
416
                @type time: An Integer object.
 
417
 
 
418
                @return: True to propagate signals to parent widgets.
 
419
                @type: A Boolean Object.
 
420
                """
 
421
                try:
 
422
                        from Exceptions import InvalidFileError, ValidationError
 
423
                        from Exceptions import NoDataError
 
424
                        path, position = treeview.get_dest_row_at_pos(x, y)
 
425
                        from gtk import TREE_VIEW_DROP_BEFORE, TREE_VIEW_DROP_AFTER
 
426
                        if position in [TREE_VIEW_DROP_BEFORE, TREE_VIEW_DROP_AFTER]: return True
 
427
                        language_id = self.__get_drop_row(path)
 
428
                        string = data.data
 
429
                        from ReadXMLTemplate import get_template_from_string
 
430
                        templates = get_template_from_string(string)
 
431
                        from operator import not_
 
432
                        if not_(templates): raise NoDataError
 
433
                        new_templates = self.__create_new_templates(templates, language_id)
 
434
                        from WriteXMLTemplate import create_template_string
 
435
                        template_string = create_template_string(new_templates)
 
436
                        from ImportTemplate import import_template_from_string
 
437
                        templates = import_template_from_string(template_string)
 
438
                        if not_(templates): raise NoDataError
 
439
                        self.__manager.emit("imported-language", language_id)
 
440
                except InvalidFileError:
 
441
                        from i18n import msg0014
 
442
                        self.__editor.error_dialog.show_message(msg0014, parent_window=self.__dialog)
 
443
                        self.__manager.emit("import-error")
 
444
                except ValidationError:
 
445
                        from i18n import msg0014
 
446
                        self.__editor.error_dialog.show_message(msg0014, parent_window=self.__dialog)
 
447
                        self.__manager.emit("import-error")
 
448
                except NoDataError:
 
449
                        from i18n import msg0015
 
450
                        self.__editor.error_dialog.show_message(msg0015, parent_window=self.__dialog)
 
451
                        self.__manager.emit("import-error")
 
452
                return True