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

Revision 1410, 20.6 kB (checked in by kop-labs, 6 years ago)

ToolbarInterface? - remove option added

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