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

Revision 1353, 18.4 kB (checked in by kop-labs, 6 years ago)

border fix

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