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

Revision 1358, 18.8 kB (checked in by kop-labs, 6 years ago)

Add section

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