~dcaro/clicompanion/fix-1035073.2

« back to all changes in this revision

Viewing changes to clicompanionlib/view.py

  • Committer: david.caro.estevez at gmail
  • Date: 2011-12-31 12:29:41 UTC
  • mfrom: (94.2.3 fix-909893)
  • Revision ID: david.caro.estevez@gmail.com-20111231122941-irwc7k7uwjos16ro
Merged from p:~dcaro/clicompanion/fix-909893, for bugs: lp:748888, lp:909893

Show diffs side-by-side

added added

removed removed

Lines of Context:
323
323
        ## instantiate tabs
324
324
        tabs = clicompanionlib.tabs.Tabs()
325
325
        ## instantiate controller.Actions, where all the button actions are
326
 
        actions = clicompanionlib.controller.Actions()
 
326
        self.actions = clicompanionlib.controller.Actions()
327
327
        ## instantiate 'File' and 'Help' Drop Down Menu [menus_buttons.py]
328
328
        bar = clicompanionlib.menus_buttons.FileMenu()
329
 
        menu_bar = bar.the_menu(actions, self.notebook, self.liststore)
 
329
        menu_bar = bar.the_menu(self.actions, self.notebook, self.liststore)
330
330
        
331
331
 
332
332
        ## get row of a selection
333
333
        def mark_selected(self, treeselection):
334
334
            global ROW
335
 
            (model, pathlist)=treeselection.get_selected_rows()
 
335
            (model, pathlist) = treeselection.get_selected_rows()
336
336
            ROW = pathlist
337
337
            
338
338
            
339
339
        ## double click to run a command    
340
340
        def treeview_clicked(widget, event):
341
341
            if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS:
342
 
                actions.run_command(self, self.notebook, self.liststore)
 
342
                self.actions.run_command(self, self.notebook, self.liststore)
343
343
 
344
344
        ## press enter to run a command                   
345
345
        def treeview_button(widget, event):
347
347
            #print keyname ##debug
348
348
            if event.type == gtk.gdk.KEY_PRESS:
349
349
                if keyname == 'RETURN':
350
 
                    actions.run_command(self, self.notebook, self.liststore)
 
350
                    self.actions.run_command(self, self.notebook, self.liststore)
351
351
                    
352
352
                    
353
353
 
367
367
        search_label = gtk.Label(_("Search:"))
368
368
        search_label.set_alignment(xalign=-1, yalign=0)
369
369
        self.search_box = gtk.Entry()
370
 
        self.search_box.connect("changed", actions._filter_commands, self.liststore, self.treeview)
 
370
        self.search_box.connect("changed", self.actions._filter_commands, self.liststore, self.treeview)
371
371
        ## search box tooltip
372
372
        self.search_box.set_tooltip_text(_("Search your list of commands"))
373
373
        ## Set the search box sensitive OFF at program start, because
405
405
        
406
406
        global button_box
407
407
        ## buttons at bottom of main window [menus_buttons.py]
408
 
        button_box = bar.buttons(actions, 10, gtk.BUTTONBOX_END, self.notebook, self.liststore)
 
408
        button_box = bar.buttons(self.actions, 10, gtk.BUTTONBOX_END, self.notebook, self.liststore)
409
409
 
410
410
        ## vbox for search, notebook, buttonbar
411
411
        vbox = gtk.VBox()
423
423
        self.window.connect("key-press-event", self.key_clicked)
424
424
        add_tab_button.connect("clicked", tabs.add_tab, self.notebook)
425
425
        ## right click menu event capture
426
 
        self.treeview.connect ("button_press_event", bar.right_click, actions, self.treeview, self.notebook, self.liststore)
 
426
        self.treeview.connect ("button_press_event", bar.right_click, self.actions, self.treeview, self.notebook, self.liststore)
 
427
 
 
428
        # Allow enable drag and drop of rows including row move
 
429
        self.treeview.enable_model_drag_source( gtk.gdk.BUTTON1_MASK,
 
430
                                                TARGETS,
 
431
                                                gtk.gdk.ACTION_DEFAULT |
 
432
                                                gtk.gdk.ACTION_COPY)
 
433
        self.treeview.enable_model_drag_dest(TARGETS,
 
434
                                                gtk.gdk.ACTION_DEFAULT)
 
435
 
 
436
        self.treeview.connect ("drag_data_get", self.drag_data_get_event)
 
437
        self.treeview.connect ("drag_data_received", self.drag_data_received_event)
427
438
 
428
439
 
429
440
        #self.vte.grab_focus()
430
441
        self.window.show_all()
431
442
        return
432
443
 
 
444
    def drag_data_get_event(self, treeview, context, selection, target_id, 
 
445
                            etime):
 
446
        """
 
447
        Executed on dragging
 
448
        """
 
449
        treeselection = treeview.get_selection()
 
450
        model, iter = treeselection.get_selected()
 
451
        data = model.get(iter, 0, 1, 2)
 
452
        selection.set(selection.target, 8, '\t'.join(data))
 
453
 
 
454
    def drag_data_received_event(self, treeview, context, x, y, selection, info,
 
455
                            etime):
 
456
        """
 
457
        Executed when dropping.
 
458
        """
 
459
        global CMNDS
 
460
        model = treeview.get_model()
 
461
        for data in selection.data.split('\n'):
 
462
            # if we got an empty line skip it
 
463
            if not data.replace('\r',''): continue
 
464
            # format the incoming string
 
465
            orig = data.replace('\r','').split('\t',2)
 
466
            orig = tuple([ fld.strip() for fld in orig ])
 
467
            # fill the empty fields
 
468
            if len(orig) < 3: orig = orig + ('',)*(3-len(orig))
 
469
            # if the element already exists delete it (dragged from clicompanion)
 
470
            olditer = self.find_iter_by_tuple(orig, model)
 
471
            if olditer: del model[olditer]
 
472
 
 
473
            drop_info = treeview.get_dest_row_at_pos(x, y)
 
474
            if drop_info:
 
475
                path, position = drop_info
 
476
                iter = model.get_iter(path)
 
477
                dest = tuple(model.get(iter, 0, 1, 2))
 
478
                if (position == gtk.TREE_VIEW_DROP_BEFORE
 
479
                        or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE):
 
480
                    model.insert_before(iter, orig)
 
481
                    self.drag_cmnd(orig, dest, before=True)
 
482
                else:
 
483
                    model.insert_after(iter, orig)
 
484
                    self.drag_cmnd(orig, dest, before=False)
 
485
            else:
 
486
                if len(model) > 0:
 
487
                    iter = model[-1].iter
 
488
                    model.insert_after(iter, orig)
 
489
                else:
 
490
                    model.insert(0, orig)
 
491
                    return
 
492
                dest = tuple(model.get(iter, 0, 1, 2))
 
493
                self.drag_cmnd(orig, dest, before=False)
 
494
            if context.action == gtk.gdk.ACTION_MOVE:
 
495
                context.finish(True, True, etime)
 
496
        self.actions.save_cmnds()
 
497
        
 
498
    def find_iter_by_tuple(self, data, model):
 
499
        for row in model:
 
500
            if tuple(model.get(row.iter, 0, 1, 2)) == data:
 
501
                return row.iter
 
502
        return None
 
503
    
 
504
    def drag_cmnd(self, orig, dest, before=True):
 
505
        """
 
506
        Sync the CMNDS array with the drag and drop of the treeview.
 
507
        """
 
508
        global CMNDS
 
509
        i = j = None
 
510
        pos = 0
 
511
        for cmnd in CMNDS:
 
512
            if cmnd == orig: 
 
513
                i = pos
 
514
            elif cmnd == dest: 
 
515
                j = pos
 
516
            pos += 1
 
517
        ## both from clicompanion
 
518
        if i != None and j != None:
 
519
            cmnd = CMNDS.pop(i)
 
520
            if before and i<=j:
 
521
                CMNDS.insert(j-1, cmnd)
 
522
            elif before and i>j:
 
523
                CMNDS.insert(j, cmnd)
 
524
            elif i<=j:
 
525
                CMNDS.insert(j, cmnd)
 
526
            else:
 
527
                CMNDS.insert(j+1, cmnd)
 
528
        ## origin unknown
 
529
        elif j != None:
 
530
            cmnd = orig
 
531
            if before:
 
532
                CMNDS.insert(j, cmnd)
 
533
            else:
 
534
                CMNDS.insert(j+1, cmnd)
 
535
    
 
536
 
433
537
    def main(self):
434
538
        try:
435
539
            gtk.main()