root/branch/k0p/umitInterfaceEditor/Tools.py @ 1426

Revision 1426, 21.3 kB (checked in by kop-labs, 6 years ago)

Delete Dependences

Line 
1# Copyright (C) 2005 Insecure.Com LLC.
2#
3# Author: Luis A. Bastiao Silva <luis.kop@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18import gtk
19
20
21import os
22from higwidgets.higboxes import HIGVBox, HIGHBox
23from higwidgets.higbuttons import HIGMixButton, HIGButton
24from higwidgets.higscrollers import HIGScrolledWindow
25from higwidgets.higtables import HIGTable
26from higwidgets.higlabels import HIGEntryLabel
27from higwidgets.higentries import HIGTextEntry
28
29from umitCore.Logging import log
30from umitCore.I18N import _
31
32
33from umitCore.Paths import Path
34pixmaps_dir = Path.pixmaps_dir
35import sys
36#sys.path.append("selectborder")
37
38from umitInterfaceEditor.selectborder.WrapperWidgets import NotebookLabel, SpecialHBox
39from umitInterfaceEditor.PageNotebook import BoxEditable, CommandPageNotebook
40from umitInterfaceEditor.Command import Command, TwiceCommand, command_manager
41
42
43'''
44This is a tool of the left frame of UIE
45It do a part of the interface, and this tools is packed on the UIE Right frame
46
47'''
48
49
50TARGET_STRING = 0
51TARGET_ROOTWIN = 1
52
53
54target = [
55    ('STRING', 0, TARGET_STRING),
56    ('text/plain', 0, TARGET_STRING),
57    ('application/x-rootwin-drop', 0, TARGET_ROOTWIN)
58]
59
60
61class ToolDesign(HIGScrolledWindow):
62    def __init__(self, only_text=True):
63        HIGScrolledWindow.__init__(self)
64       
65        self._box = HIGVBox()
66        vp = gtk.Viewport()
67
68        self._box.set_border_width(6)
69        if only_text:
70            self._draw_buttons()
71            self._pack()
72        #ELSE == Algo com icons; ETC!
73       
74        vp.add(self._box)
75        vp.set_shadow_type(gtk.SHADOW_NONE)
76        self.add(vp)
77    def update_toolbar(self):
78        pass
79   
80    def _draw_buttons(self):
81        self.button_list = HIGButton('Option List')
82        img = gtk.Image()
83        img_dir =  os.path.join(pixmaps_dir, 'combo.png')
84        img.set_from_file(img_dir)
85        self.button_list.set_image(img) 
86       
87
88        self.button_list.drag_source_set(gtk.gdk.BUTTON1_MASK ,
89                          target, gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
90        self.button_list.connect('drag_data_get', self._drag_option_list)       
91       
92       
93        self.button_label = HIGButton('Label')
94        img = gtk.Image()
95        img_dir =  os.path.join(pixmaps_dir, 'label.png')
96        img.set_from_file(img_dir)
97        self.button_label.set_image(img)
98
99        self.button_label.drag_source_set(gtk.gdk.BUTTON1_MASK ,
100                          target, gtk.gdk.ACTION_COPY | gtk.gdk.ACTION_MOVE)
101        self.button_label.connect('drag_data_get', self.source_drag_data_get)
102
103        self.button_check = HIGButton('Option Check')
104        img = gtk.Image()
105        img_dir =  os.path.join(pixmaps_dir, 'checkbutton.png')
106        img.set_from_file(img_dir)
107        self.button_check.set_image(img)
108        self.button_path = HIGButton('Choose Path')
109        self.button_text_entry = HIGButton('String')
110        img = gtk.Image()
111        img_dir =  os.path.join(pixmaps_dir, 'entry.png')
112        img.set_from_file(img_dir)
113        self.button_text_entry.set_image(img)
114        self.button_float = HIGButton('Float Spin')
115        img = gtk.Image()
116        img_dir =  os.path.join(pixmaps_dir, 'spinbutton.png')
117        img.set_from_file(img_dir)
118        self.button_float.set_image(img)       
119       
120        self.button_level = HIGButton('Level Spin')
121        img = gtk.Image()
122        img_dir =  os.path.join(pixmaps_dir, 'spinbutton.png')
123        img.set_from_file(img_dir)
124        self.button_level.set_image(img)       
125        self.button_interface = HIGButton('Interface')
126        self.button_integer = HIGButton('Integer Spin ')
127        img = gtk.Image()
128        img_dir =  os.path.join(pixmaps_dir, 'spinbutton.png')
129        img.set_from_file(img_dir)
130        self.button_integer.set_image(img)     
131    def source_drag_data_get(self, btn, context, selection_data, info, time):
132        selection_data.set(selection_data.target, 8, "Label")
133    def _drag_option_list(self, btn, context, selection_data, info, time):
134        selection_data.set(selection_data.target, 8, "_widget_option_list")
135
136
137    def _pack(self):
138        box = self._box
139        box.pack_start(self.button_list, False, False)
140        box.pack_start(self.button_label, False, False)
141        #box.pack_start(self.button_check, False, False)
142        #box.pack_start(self.button_path, False, False)
143        #box.pack_start(self.button_float, False, False)
144        #box.pack_start(self.button_integer, False, False)
145        #box.pack_start(self.button_level, False, False)
146       
147       
148
149class ToolBarInterface(gtk.EventBox):
150    def __init__(self):
151        gtk.EventBox.__init__(self)
152        self._profilecore = None 
153        self.box_editable = None 
154        self._optionlist = None 
155        self.notebook = None
156        self.ui_manager = gtk.UIManager()
157        self.main_action_group = gtk.ActionGroup('MainActionGroup')
158        self.main_actions = [ \
159
160            ('Move up', gtk.STOCK_GO_UP, _('_Move up'), None, _('Move up'),
161             self._move_item_up),
162            ('Move down', gtk.STOCK_GO_DOWN, _('_Move down'), None,
163             _('Move down'),
164             self._move_item_down),
165            ('Move Section Left', gtk.STOCK_GO_BACK, _('_Move Section Left'), 
166             None, _('Move Section Left'), self._move_section_left),
167            ('Move Section Right',gtk.STOCK_GO_FORWARD, _('_Move Section Right'), 
168             None, _('Move Section Right'), self._move_section_right),
169            ('Add Section', gtk.STOCK_ADD, _('_Add Section'), None,
170             _('Add Section'), self._add_section),
171            ('Remove Section', gtk.STOCK_REMOVE, _('_Remove Section'), 
172             None, _('Remove Section'), self._remove_section),
173            ('Add Place', gtk.STOCK_ADD, _('Add Place'), 
174             None, _('Add Place'), self._add_place),
175            ('Remove Option', gtk.STOCK_DELETE, _('Remove Option'), 
176             None, _('Remove Option'), self._remove_option),
177
178        ]
179        self.ui_info = """
180        <toolbar>
181            <toolitem action='Move up'/>
182            <toolitem action='Move down'/>
183            <toolitem action='Add Place'/>
184            <toolitem action='Remove Option'/>   
185            <separator/>
186            <toolitem action='Move Section Left'/>
187            <toolitem action='Move Section Right'/>
188            <toolitem action='Add Section'/>
189            <toolitem action='Remove Section'/>
190            <separator/>
191     
192        </toolbar>
193        """
194       
195       
196
197        self.main_action_group.add_actions(self.main_actions)
198        self.main_accel_group = gtk.AccelGroup()
199        for action in self.main_action_group.list_actions():
200           
201            action.set_accel_group(self.main_accel_group)
202            action.connect_accelerator()
203
204        #ENABLE = FALSE
205        self.main_action_group.get_action('Move up').set_sensitive(False)
206        self.main_action_group.get_action('Move down').set_sensitive(False)
207
208        #END ENABLE
209        self.ui_manager.insert_action_group(self.main_action_group, 0)
210        #try:
211        self.ui_manager.add_ui_from_string(self.ui_info)
212        self.toolbar = self.ui_manager.get_widget('/toolbar')
213        self.toolbar.set_style(gtk.TOOLBAR_ICONS)
214        self.toolbar.set_orientation(gtk.ORIENTATION_VERTICAL)
215       
216        #self.add(self.toolbar)
217    def set_profilecore(self, profilecore):
218        self._profilecore = profilecore
219    def get_toolbar(self):
220        return self.toolbar
221    def set_optionlist(self, optionlist):
222        self._optionlist = optionlist
223    def enable(self,item, value):
224        self.main_action_group.get_action(item).set_sensitive(value)
225    def _remove_option(self, widget):
226        self.box_editable.delete_on_item(self.box_editable._old_selected)
227    def _add_section(self, widget):
228        '''
229        Add a new section
230        '''
231
232        page =  BoxEditable('New_Section', self._profilecore, self._optionlist,
233                        self.notebook, True)
234        page.set_profile_core(self._profilecore)
235        cmd = CommandPageNotebook(self.notebook,page, -1, self._profilecore, True)
236        command_manager.add_command(cmd)
237    def _remove_section(self, widget):
238        '''
239        Remove section
240        '''
241        self.notebook.delete_section()
242    def _add_place(self,widget):
243        self.box_editable.add_voidplace(-1)
244    def _move_item_up(self, widget):   
245        self.box_editable.move_item_up()
246    def _move_item_down(self, widget):   
247        self.box_editable.move_item_down()   
248    def _move_section_left(self, widget):
249        self.notebook.move_left()
250    def _move_section_right(self, widget):
251        self.notebook.move_right()
252    def set_box_editable(self, boxeditable):
253        self.box_editable = boxeditable
254    def set_notebook(self, notebook):
255        self.notebook = notebook
256       
257    def update_toolbar(self):
258        boxeditable = self.box_editable
259        self.enable('Move up', boxeditable.can_move_up())
260        self.enable('Move down', boxeditable.can_move_down())
261        self.enable('Move Section Left', self.notebook.can_move_left())
262        self.enable('Move Section Right', self.notebook.can_move_right())
263       
264
265    def np(self, widget):
266        pass
267   
268
269class CommandChangeLabel(TwiceCommand,Command):
270    def __init__(self, widget, text, profilecore, state):
271        TwiceCommand.__init__(self, state)
272        Command.__init__(self, _('Change Label'))
273        self._profilecore = profilecore
274        self._widget = widget
275        self._text = text
276        self._old_text = None
277        self._is_section = False
278   
279    def _identify(self, text):
280        selected = self._widget
281        if isinstance(selected, NotebookLabel):
282            log.debug('update NotebookLabel')
283            selected.set_text(text)
284            self._is_section = True
285           
286           
287            return 
288        childs = selected.get_children()
289        child_label = childs[0]
290        if isinstance(child_label, gtk.HBox):
291            log.debug('update OptionCheckIcon')
292            #self._button_list.hide()
293            child_label.cbutton.set_label(text)
294        elif isinstance(child_label, gtk.EventBox):
295            log.debug('update OptionList ')
296            #self._button_list.show()
297            other = child_label.get_children()[0]
298            other.set_label(text)
299    def _renama_profilecore(self, text1, text2):
300        if self._is_section :
301            self._profilecore.rename_section(text1, text2)
302        else: 
303            section = self._widget.get_profileoption().get_section()
304            self._profilecore.rename_option(section, text1, text2)
305    def _rename(self):
306
307        self._identify(self._text)
308        self._old_text = self._widget.get_name()
309        #Update ProfileCore: (section name)
310        self._renama_profilecore(self._old_text, self._text)
311
312    _execute_1 = _rename
313   
314    def _unrename(self):
315        self._identify(self._old_text)
316        #Update ProfileCore: (section name)
317        self._renama_profilecore(self._text, self._old_text)
318       
319
320    _execute_2 = _unrename
321import gobject
322from OptionManager import OptionList
323## Testing at devel
324#from os.path import split, join
325
326#from umitCore.Paths import Path
327#Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
328##END DEV TEST
329#options = Path.options
330
331class CommandUpdateOptionList(TwiceCommand, Command):
332    def __init__(self, widget, list_new, list_old, profilecore,state):
333        TwiceCommand.__init__(self, state)
334        Command.__init__(self, _('Update OptionList'))
335        self._widget = widget
336        self._new_list = list_new
337        self._old_list = list_old
338        self._profilecore = profilecore
339       
340    def _update_list(self, list):
341        #Refresh Widget
342        combo = self._widget.get_children()[1]
343        combo.list.clear()
344        for i in list :
345            d= {}
346            d['name'] = i 
347            combo.append(d)
348       
349        #Update ProfileOption
350        po = self._widget.get_profileoption()
351        po.set_option_list(self._new_list)
352       
353        #Update ProfileCore
354        self._profilecore.update_option_list(po.get_section(),
355                                             po.get_label(),
356                                             list)
357       
358       
359    def _update_new(self):
360        self._update_list(self._new_list)
361    _execute_1 = _update_new
362    def _update_old(self):
363        self._update_list(self._old_list)
364    _execute_2 = _update_old
365   
366   
367   
368
369
370
371class ListManager(gtk.Dialog):
372    '''
373    ListManager - manage the OptionList
374    Tricks:
375     - To works with the Command Manager it save at __init__ the list
376       of options. After that. Add to CommandManager when clicked 'Ok Button'
377       with the new and old list.
378    '''
379    def __init__(self, name, section, profilecore, widget, title):
380        gtk.Dialog.__init__(self,title)
381        self.set_size_request(450, 300)
382        self._name = name
383        self._section = section
384        self._profilecore = profilecore
385        self._widget = widget
386        self._list = self._profilecore.get_list_opt(self._section, self._name)
387        self._new_list = self._profilecore.get_list_opt(self._section, 
388                                                        self._name)
389        self._create_widgets()
390        self.show()
391        self._load_option_list()
392        self._create_action_area()
393       
394    def _create_widgets(self):
395        self._optionlist = OptionList()
396        label = gtk.Label('Items at the %s ' % self._name)
397        self._box = gtk.HPaned()
398        ol = self._optionlist
399        ol.reload()
400        self.vbox.pack_start(label, False, False, 0)
401        self._box.add(ol)
402        self.vbox.pack_start(self._box)
403        self._box.show_all()
404        self._move_box = HIGVBox()
405        self._add_bt = HIGButton(stock='gtk-add')
406        self._add_bt.connect('clicked', self._on_add_press)
407        self._remove_bt = HIGButton(stock='gtk-remove')
408        self._remove_bt.connect('clicked', self._on_remove_press)
409        #XXX - moves don't work yet: lack the connect
410        self._move_up_bt = HIGButton(stock='gtk-go-up')
411        self._move_down_bt = HIGButton(stock='gtk-go-down')
412        self._move_box.pack_start(self._add_bt, False, False)
413        self._move_box.pack_start(self._remove_bt, False, False)
414        self._move_box.pack_start(self._move_up_bt, False, False)
415        self._move_box.pack_start(self._move_down_bt, False, False)
416        self._create_option_tv()
417
418        self._box.set_position(200)
419        self._box_other = gtk.HPaned()
420        self._box.add(self._box_other)
421        self._box_other.add(self._move_box)
422        self._box_other.add(self._sw)
423        self._move_box.show_all()
424        self.vbox.show_all()
425       
426        label.show()
427       
428    def _create_option_tv(self):
429        self._sw = HIGScrolledWindow()
430       
431       
432        self._model = gtk.TreeStore(gobject.TYPE_STRING)
433        self._tv = gtk.TreeView(self._model)
434        column = gtk.TreeViewColumn()
435        column.set_title('Name')
436        render = gtk.CellRendererText()
437        column.pack_start(render, expand=True)
438        column.add_attribute(render, 'text', 0)
439        self._tv.append_column(column)
440        self._sw.add(self._tv)
441        self._sw.show_all()
442    def _load_option_list(self):
443        list = self._list
444       
445        for i in list : 
446            iter = self._model.insert_before(None, None)
447            self._model.set_value(iter, 0, i)
448    def _create_action_area(self):
449        self._button_ok = HIGButton(stock='gtk-ok')
450        self._button_cancel = HIGButton(stock='gtk-cancel')
451        self._button_cancel.connect('clicked', self._on_cancel_press)
452        self._button_ok.connect('clicked', self._on_ok_press)
453        self.action_area.pack_start(self._button_cancel)
454        self.action_area.pack_start(self._button_ok)
455        self.action_area.show_all()
456
457    def _on_add_press(self, widget):
458        log.debug('<<< Add Option to OptionList')
459        option_selected = self._optionlist.get_selected()
460        iter = self._model.insert_before(None, None)
461        self._model.set_value(iter, 0, option_selected)
462        self._new_list.append(option_selected)
463       
464    def get_selected(self):
465        """
466        Returns the string with name of selected option
467        """
468        try:
469            treeselection = self._tv.get_selection()
470            (model,iter) = treeselection.get_selected() 
471            return model.get_value(iter,0)
472        except:
473            return None           
474           
475    def get_selected_option(self):
476        '''
477        @return: iter and model of option treeview selected
478        '''
479        treeselection = self._tv.get_selection()
480       
481        (model,iter) = treeselection.get_selected()
482        return model, iter   
483   
484    def _on_remove_press(self, widget):
485        log.debug('<<< Remove Option from OptionList')
486        selected = self.get_selected()
487        (model, iter) = self.get_selected_option()
488        if selected!=None:
489            self._new_list.remove(selected)
490            self._model.remove(iter)
491           
492   
493
494   
495   
496    def _on_ok_press(self, widget):
497        # Lists:
498        list2 = self._list
499        list1 = self._new_list
500       
501        cmd = CommandUpdateOptionList(self._widget, list1, list2, self._profilecore, True)
502        command_manager.add_command(cmd)
503        self.destroy()
504       
505    def _on_cancel_press(self, widget):
506        self.destroy()
507   
508   
509
510class Proprieties(HIGScrolledWindow):
511    '''
512   
513    This box should be configurable
514    if widget is of a type all configuration should be change to this type
515   
516    #tricks: option_list have a icon to fill with options
517    and option_check have a list of options in combo to change
518   
519    '''
520   
521    def __init__(self):
522        HIGScrolledWindow.__init__(self)
523        self._boxeditable = None 
524        vp = gtk.Viewport()
525        self._create_widgets()
526        vp.add(self._box)
527        vp.set_shadow_type(gtk.SHADOW_NONE)
528        self.add(vp)
529        self._profilecore = None 
530        self._selected = None 
531   
532    def set_profilecore(self, profilecore):
533        self._profilecore = profilecore
534       
535
536    def _create_widgets(self):
537        '''
538        Create the main entrys of the option
539        '''
540        self._box = HIGVBox()
541       
542       
543        self._table = HIGTable()
544       
545       
546        #Name
547        self._label_name  = HIGEntryLabel(_('Name'))
548        self._entry_name = HIGTextEntry()
549       
550        self._entry_name.connect('activate', self._update_label)
551
552        #Type
553        self._label_type = HIGEntryLabel(_('Type'))     
554        self._combo_type = gtk.combo_box_new_text()
555        self._combo_type.append_text('')
556        self._combo_type.append_text('Option List')
557        self._combo_type.append_text('Option Check')
558        self._combo_type.set_active(0)
559        self._combo_type.connect('changed', self.change_combo)
560       
561        self._label_opt = HIGEntryLabel(_('Option'))
562        self._entry_opt = HIGTextEntry()
563        self._entry_opt.set_sensitive(False)
564       
565        #For option list open a dialog to add/remove options
566        self._button_list = HIGButton('Edit Option List')
567        img = gtk.Image()
568        img_dir =  os.path.join(pixmaps_dir, 'combo.png')
569        img.set_from_file(img_dir)
570        self._button_list.set_image(img)
571        self._button_list.connect('button-press-event', self._button_list_clicked)
572       
573       
574        self._table.attach(self._label_name, 0,1,0, 1)
575        self._table.attach(self._entry_name, 1,2,0,1)
576        self._table.attach(self._label_type, 0,1,1,2)
577        self._table.attach(self._combo_type, 1,2,1, 2)
578
579        self._table.attach(self._button_list, 0,2, 3,4)
580        self._table.attach(self._label_opt, 0,1, 4,5)
581        self._table.attach(self._entry_opt, 1,2,4,5)
582       
583        self._box.pack_start(self._table, False, False)
584    def _button_list_clicked(self, widget, event):
585        section_name = self._boxeditable.get_name()
586        lm = ListManager(self._entry_name.get_text(),section_name, 
587                         self._profilecore, self._selected, _('List of items'))
588    def _update_label(self, widget):
589        #XXX Replace by Command
590        print "Update Label"
591        selected = self._selected
592        cmd = CommandChangeLabel(selected, self._entry_name.get_text(), 
593                                 self._profilecore, True)
594        command_manager.add_command(cmd)
595        #if isinstance(selected, NotebookLabel):
596            #log.debug('update NotebookLabel')
597            #selected.set_text(self._entry_name.get_text())
598            #return
599        #childs = selected.get_children()
600        #child_label = childs[0]
601        #if isinstance(child_label, gtk.HBox):
602            #self._button_list.hide()
603            #child_label.cbutton.set_label(self._entry_name.get_text())
604        #elif isinstance(child_label, gtk.EventBox):
605            #self._button_list.show()
606            #other = child_label.get_children()[0]
607            #other.set_label(self._entry_name.get_text())
608       
609    def change_combo(self,combo):
610        model = combo.get_model()
611        index = combo.get_active()
612        if index:
613            if model[index][0]=='Option List':
614               
615                log.debug('Show Button List ')
616                self._button_list.show()
617               
618            else:
619               
620                log.debug('Hide Button List ')
621                self._button_list.hide()
622        return
623    def show_notebook_label(self):
624        '''
625        show proprieties of notebook label and hide others
626        '''
627        pass
628   
629
630    def show_item(self):
631        pass
632    def hide_item(self):
633        self._label_opt.hide()
634        self._entry_opt.hide()
635        self._button_list.hide()
636        self._combo_type.hide()
637        self._label_type.hide()
638        self._entry_name.hide()
639        self._label_name.hide()
640    def set_notebooklabel(self, selected):
641        self._entry_name.show()
642        self._label_name.show()
643        self._entry_name.set_text(selected.get_text())
644        self._button_list.hide()
645        self._combo_type.hide()
646        self._label_type.hide()
647        self._label_opt.hide()
648        self._entry_opt.hide()
649        self._selected = selected
650    def set_item(self, selected):
651        self._entry_name.show()
652        self._label_name.show()
653
654        self._selected = selected
655        if selected.get_name()!=None:
656            self._entry_name.set_text(selected.get_name())
657        else: 
658            self.hide_item()
659            return
660        childs = selected.get_children()
661        self._combo_type.show()
662        self._label_type.show()
663        child_label = childs[0]
664        if isinstance(child_label, gtk.HBox):
665            #OptionCheck
666            self._label_opt.show()
667            self._entry_opt.show()
668            opt_ = self._profilecore.get_opt_check(self._boxeditable.get_name(),
669                                                  selected.get_name())
670            self._entry_opt.set_text(opt_)
671            self._button_list.hide()
672            child_label.cbutton.set_label(self._entry_name.get_text())
673            self._combo_type.set_active(2)
674            #XXX: Put other widget that sensitible = False with option name
675           
676           
677        elif isinstance(child_label, gtk.EventBox):
678            #OptionList
679            self._button_list.show()
680            other = child_label.get_children()[0]
681            other.set_label(self._entry_name.get_text())
682            self._combo_type.set_active(1)
683        #Disable Combo to change OptionList/OptionChange
684        self._combo_type.set_sensitive(False)
685
686    def set_boxeditable(self, boxeditable):
687        self._boxeditable = boxeditable
688    def update(self):
689        pass
690   
691    def disable_all(self):
692        self._label_opt.hide()
693        self._entry_opt.hide()
694        self._button_list.hide()
695        self._combo_type.hide()
696        self._label_type.hide()
697        self._entry_name.hide()
698        self._label_name.hide()
699    def load_data(self, option):
700        pass 
701    def unload(self,option):
702        pass 
703   
Note: See TracBrowser for help on using the browser.