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

Revision 1393, 20.5 kB (checked in by kop-labs, 6 years ago)

Fixed bug when save OptionList?

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.np),
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.np),
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 _add_section(self, widget):
226        '''
227        Add a new section
228        '''
229
230        page =  BoxEditable('New_Section', self._profilecore, self._optionlist,
231                        self.notebook, True)
232        page.set_profile_core(self._profilecore)
233        cmd = CommandPageNotebook(self.notebook,page, -1, self._profilecore, True)
234        command_manager.add_command(cmd)
235    def _add_place(self,widget):
236        self.box_editable.add_voidplace(-1)
237    def _move_item_up(self, widget):   
238        self.box_editable.move_item_up()
239    def _move_item_down(self, widget):   
240        self.box_editable.move_item_down()   
241    def _move_section_left(self, widget):
242        self.notebook.move_left()
243    def _move_section_right(self, widget):
244        self.notebook.move_right()
245    def set_box_editable(self, boxeditable):
246        self.box_editable = boxeditable
247    def set_notebook(self, notebook):
248        self.notebook = notebook
249       
250    def update_toolbar(self):
251        boxeditable = self.box_editable
252        self.enable('Move up', boxeditable.can_move_up())
253        self.enable('Move down', boxeditable.can_move_down())
254        self.enable('Move Section Left', self.notebook.can_move_left())
255        self.enable('Move Section Right', self.notebook.can_move_right())
256       
257
258    def np(self, widget):
259        pass
260   
261
262class CommandChangeLabel(TwiceCommand,Command):
263    def __init__(self, widget, text, profilecore, state):
264        TwiceCommand.__init__(self, state)
265        Command.__init__(self, _('Change Label'))
266        self._profilecore = profilecore
267        self._widget = widget
268        self._text = text
269        self._old_text = None
270        self._is_section = False
271   
272    def _identify(self, text):
273        selected = self._widget
274        if isinstance(selected, NotebookLabel):
275            log.debug('update NotebookLabel')
276            selected.set_text(text)
277            self._is_section = True
278           
279           
280            return 
281        childs = selected.get_children()
282        child_label = childs[0]
283        if isinstance(child_label, gtk.HBox):
284            log.debug('update OptionCheckIcon')
285            #self._button_list.hide()
286            child_label.cbutton.set_label(text)
287        elif isinstance(child_label, gtk.EventBox):
288            log.debug('update OptionList ')
289            #self._button_list.show()
290            other = child_label.get_children()[0]
291            other.set_label(text)
292    def _renama_profilecore(self, text1, text2):
293        if self._is_section :
294            self._profilecore.rename_section(text1, text2)
295        else: 
296            section = self._widget.get_profileoption().get_section()
297            self._profilecore.rename_option(section, text1, text2)
298    def _rename(self):
299
300        self._identify(self._text)
301        self._old_text = self._widget.get_name()
302        #Update ProfileCore: (section name)
303        self._renama_profilecore(self._old_text, self._text)
304
305    _execute_1 = _rename
306   
307    def _unrename(self):
308        self._identify(self._old_text)
309        #Update ProfileCore: (section name)
310        self._renama_profilecore(self._text, self._old_text)
311       
312
313    _execute_2 = _unrename
314import gobject
315from OptionManager import OptionList
316## Testing at devel
317#from os.path import split, join
318
319#from umitCore.Paths import Path
320#Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
321##END DEV TEST
322#options = Path.options
323
324class CommandUpdateOptionList(TwiceCommand, Command):
325    def __init__(self, widget, list_new, list_old, profilecore,state):
326        TwiceCommand.__init__(self, state)
327        Command.__init__(self, _('Update OptionList'))
328        self._widget = widget
329        self._new_list = list_new
330        self._old_list = list_old
331        self._profilecore = profilecore
332       
333    def _update_list(self, list):
334        #Refresh Widget
335        combo = self._widget.get_children()[1]
336        combo.list.clear()
337        for i in list :
338            d= {}
339            d['name'] = i 
340            combo.append(d)
341       
342        #Update ProfileOption
343        po = self._widget.get_profileoption()
344        po.set_option_list(self._new_list)
345       
346        #Update ProfileCore
347        self._profilecore.update_option_list(po.get_section(),
348                                             po.get_label(),
349                                             list)
350       
351       
352    def _update_new(self):
353        self._update_list(self._new_list)
354    _execute_1 = _update_new
355    def _update_old(self):
356        self._update_list(self._old_list)
357    _execute_2 = _update_old
358   
359   
360   
361
362
363
364class ListManager(gtk.Dialog):
365    '''
366    ListManager - manage the OptionList
367    Tricks:
368     - To works with the Command Manager it save at __init__ the list
369       of options. After that. Add to CommandManager when clicked 'Ok Button'
370       with the new and old list.
371    '''
372    def __init__(self, name, section, profilecore, widget, title):
373        gtk.Dialog.__init__(self,title)
374        self.set_size_request(450, 300)
375        self._name = name
376        self._section = section
377        self._profilecore = profilecore
378        self._widget = widget
379        self._list = self._profilecore.get_list_opt(self._section, self._name)
380        self._new_list = self._profilecore.get_list_opt(self._section, 
381                                                        self._name)
382        self._create_widgets()
383        self.show()
384        self._load_option_list()
385        self._create_action_area()
386       
387    def _create_widgets(self):
388        self._optionlist = OptionList()
389        label = gtk.Label('Items at the %s ' % self._name)
390        self._box = gtk.HPaned()
391        ol = self._optionlist
392        ol.reload()
393        self.vbox.pack_start(label, False, False, 0)
394        self._box.add(ol)
395        self.vbox.pack_start(self._box)
396        self._box.show_all()
397        self._move_box = HIGVBox()
398        self._add_bt = HIGButton(stock='gtk-add')
399        self._add_bt.connect('clicked', self._on_add_press)
400        self._remove_bt = HIGButton(stock='gtk-remove')
401        self._remove_bt.connect('clicked', self._on_remove_press)
402        #XXX - moves don't work yet: lack the connect
403        self._move_up_bt = HIGButton(stock='gtk-go-up')
404        self._move_down_bt = HIGButton(stock='gtk-go-down')
405        self._move_box.pack_start(self._add_bt, False, False)
406        self._move_box.pack_start(self._remove_bt, False, False)
407        self._move_box.pack_start(self._move_up_bt, False, False)
408        self._move_box.pack_start(self._move_down_bt, False, False)
409        self._create_option_tv()
410
411        self._box.set_position(200)
412        self._box_other = gtk.HPaned()
413        self._box.add(self._box_other)
414        self._box_other.add(self._move_box)
415        self._box_other.add(self._sw)
416        self._move_box.show_all()
417        self.vbox.show_all()
418       
419        label.show()
420       
421    def _create_option_tv(self):
422        self._sw = HIGScrolledWindow()
423       
424       
425        self._model = gtk.TreeStore(gobject.TYPE_STRING)
426        self._tv = gtk.TreeView(self._model)
427        column = gtk.TreeViewColumn()
428        column.set_title('Name')
429        render = gtk.CellRendererText()
430        column.pack_start(render, expand=True)
431        column.add_attribute(render, 'text', 0)
432        self._tv.append_column(column)
433        self._sw.add(self._tv)
434        self._sw.show_all()
435    def _load_option_list(self):
436        list = self._list
437       
438        for i in list : 
439            iter = self._model.insert_before(None, None)
440            self._model.set_value(iter, 0, i)
441    def _create_action_area(self):
442        self._button_ok = HIGButton(stock='gtk-ok')
443        self._button_cancel = HIGButton(stock='gtk-cancel')
444        self._button_cancel.connect('clicked', self._on_cancel_press)
445        self._button_ok.connect('clicked', self._on_ok_press)
446        self.action_area.pack_start(self._button_cancel)
447        self.action_area.pack_start(self._button_ok)
448        self.action_area.show_all()
449
450    def _on_add_press(self, widget):
451        log.debug('<<< Add Option to OptionList')
452        option_selected = self._optionlist.get_selected()
453        iter = self._model.insert_before(None, None)
454        self._model.set_value(iter, 0, option_selected)
455        self._new_list.append(option_selected)
456       
457    def get_selected(self):
458        """
459        Returns the string with name of selected option
460        """
461        try:
462            treeselection = self._tv.get_selection()
463            (model,iter) = treeselection.get_selected() 
464            return model.get_value(iter,0)
465        except:
466            return None           
467           
468    def get_selected_option(self):
469        '''
470        @return: iter and model of option treeview selected
471        '''
472        treeselection = self._tv.get_selection()
473       
474        (model,iter) = treeselection.get_selected()
475        return model, iter   
476   
477    def _on_remove_press(self, widget):
478        log.debug('<<< Remove Option from OptionList')
479        selected = self.get_selected()
480        (model, iter) = self.get_selected_option()
481        if selected!=None:
482            self._new_list.remove(selected)
483            self._model.remove(iter)
484           
485   
486
487   
488   
489    def _on_ok_press(self, widget):
490        # Lists:
491        list2 = self._list
492        list1 = self._new_list
493       
494        cmd = CommandUpdateOptionList(self._widget, list1, list2, self._profilecore, True)
495        command_manager.add_command(cmd)
496        self.destroy()
497       
498    def _on_cancel_press(self, widget):
499        self.destroy()
500   
501   
502
503class Proprieties(HIGScrolledWindow):
504    '''
505   
506    This box should be configurable
507    if widget is of a type all configuration should be change to this type
508   
509    #tricks: option_list have a icon to fill with options
510    and option_check have a list of options in combo to change
511   
512    '''
513   
514    def __init__(self):
515        HIGScrolledWindow.__init__(self)
516        self._boxeditable = None 
517        vp = gtk.Viewport()
518        self._create_widgets()
519        vp.add(self._box)
520        vp.set_shadow_type(gtk.SHADOW_NONE)
521        self.add(vp)
522        self._profilecore = None 
523        self._selected = None 
524   
525    def set_profilecore(self, profilecore):
526        self._profilecore = profilecore
527       
528
529    def _create_widgets(self):
530        '''
531        Create the main entrys of the option
532        '''
533        self._box = HIGVBox()
534       
535       
536        self._table = HIGTable()
537       
538       
539        #Name
540        self._label_name  = HIGEntryLabel(_('Name'))
541        self._entry_name = HIGTextEntry()
542       
543        self._entry_name.connect('activate', self._update_label)
544
545        #Type
546        self._label_type = HIGEntryLabel(_('Type'))     
547        self._combo_type = gtk.combo_box_new_text()
548        self._combo_type.append_text('')
549        self._combo_type.append_text('Option List')
550        self._combo_type.append_text('Option Check')
551        self._combo_type.set_active(0)
552        self._combo_type.connect('changed', self.change_combo)
553       
554        #For option list open a dialog to add/remove options
555        self._button_list = HIGButton('Edit Option List')
556        img = gtk.Image()
557        img_dir =  os.path.join(pixmaps_dir, 'combo.png')
558        img.set_from_file(img_dir)
559        self._button_list.set_image(img)
560        self._button_list.connect('button-press-event', self._button_list_clicked)
561       
562       
563        self._table.attach(self._label_name, 0,1,0, 1)
564        self._table.attach(self._entry_name, 1,2,0,1)
565        self._table.attach(self._label_type, 0,1,1,2)
566        self._table.attach(self._combo_type, 1,2,1, 2)
567        self._table.attach(self._button_list, 0,2, 3,4)
568       
569        self._box.pack_start(self._table, False, False)
570    def _button_list_clicked(self, widget, event):
571        section_name = self._boxeditable.get_name()
572        lm = ListManager(self._entry_name.get_text(),section_name, 
573                         self._profilecore, self._selected, _('List of items'))
574    def _update_label(self, widget):
575        #XXX Replace by Command
576        print "Update Label"
577        selected = self._selected
578        cmd = CommandChangeLabel(selected, self._entry_name.get_text(), 
579                                 self._profilecore, True)
580        command_manager.add_command(cmd)
581        #if isinstance(selected, NotebookLabel):
582            #log.debug('update NotebookLabel')
583            #selected.set_text(self._entry_name.get_text())
584            #return
585        #childs = selected.get_children()
586        #child_label = childs[0]
587        #if isinstance(child_label, gtk.HBox):
588            #self._button_list.hide()
589            #child_label.cbutton.set_label(self._entry_name.get_text())
590        #elif isinstance(child_label, gtk.EventBox):
591            #self._button_list.show()
592            #other = child_label.get_children()[0]
593            #other.set_label(self._entry_name.get_text())
594       
595    def change_combo(self,combo):
596        model = combo.get_model()
597        index = combo.get_active()
598        if index:
599            if model[index][0]=='Option List':
600               
601                log.debug('Show Button List ')
602                self._button_list.show()
603               
604            else:
605               
606                log.debug('Hide Button List ')
607                self._button_list.hide()
608        return
609    def show_notebook_label(self):
610        '''
611        show proprieties of notebook label and hide others
612        '''
613        pass
614   
615
616    def show_item(self):
617        pass
618    def hide_item(self):
619        self._button_list.hide()
620        self._combo_type.hide()
621        self._label_type.hide()
622        self._entry_name.hide()
623        self._label_name.hide()
624    def set_notebooklabel(self, selected):
625        self._entry_name.show()
626        self._label_name.show()
627        self._entry_name.set_text(selected.get_text())
628        self._button_list.hide()
629        self._combo_type.hide()
630        self._label_type.hide()
631        self._selected = selected
632    def set_item(self, selected):
633        self._entry_name.show()
634        self._label_name.show()
635        self._selected = selected
636        if selected.get_name()!=None:
637            self._entry_name.set_text(selected.get_name())
638        else: 
639            self.hide_item()
640            return
641        childs = selected.get_children()
642        self._combo_type.show()
643        self._label_type.show()
644        child_label = childs[0]
645        if isinstance(child_label, gtk.HBox):
646            #OptionCheck
647            self._button_list.hide()
648            child_label.cbutton.set_label(self._entry_name.get_text())
649            self._combo_type.set_active(2)
650            #XXX: Put other widget that sensitible = False with option name
651           
652           
653        elif isinstance(child_label, gtk.EventBox):
654            #OptionList
655            self._button_list.show()
656            other = child_label.get_children()[0]
657            other.set_label(self._entry_name.get_text())
658            self._combo_type.set_active(1)
659        #Disable Combo to change OptionList/OptionChange
660        self._combo_type.set_sensitive(False)
661
662    def set_boxeditable(self, boxeditable):
663        self._boxeditable = boxeditable
664    def update(self):
665        pass
666   
667    def disable_all(self):
668        self._button_list.hide()
669        self._combo_type.hide()
670        self._label_type.hide()
671        self._entry_name.hide()
672        self._label_name.hide()
673    def load_data(self, option):
674        pass 
675    def unload(self,option):
676        pass 
677   
Note: See TracBrowser for help on using the browser.