Changeset 8896:5f8604be2581 in orange


Ignore:
Timestamp:
09/02/11 15:00:08 (3 years ago)
Author:
Noughmad <Noughmad@…>
Branch:
default
Convert:
e0b4abf88aec09e15a992b58ed0f2490d83d243d
Message:

Unify all selection modes into a single button with a popup menu.

Location:
orange/OrangeWidgets
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/plot/owplot.py

    r8883 r8896  
    460460    discPalette = deprecated_attribute("discPalette", "discrete_palette") 
    461461     
    462     def __setattr__(self, name, value): 
    463         unisetattr(self, name, value, QGraphicsView) 
    464          
    465462    def scrollContentsBy(self, dx, dy): 
    466463        # This is overriden here to prevent scrolling with mouse and keyboard 
  • orange/OrangeWidgets/plot/owplotgui.py

    r8735 r8896  
    3131from owconstants import * 
    3232 
    33 from PyQt4.QtGui import QWidget, QToolButton, QGroupBox, QVBoxLayout, QHBoxLayout, QIcon 
    34 from PyQt4.QtCore import Qt, pyqtSignal, qDebug, QObject, SIGNAL 
     33from PyQt4.QtGui import QWidget, QToolButton, QGroupBox, QVBoxLayout, QHBoxLayout, QIcon, QMenu, QAction 
     34from PyQt4.QtCore import Qt, pyqtSignal, qDebug, QObject, SIGNAL, SLOT 
    3535 
    3636 
     
    137137        if enabled and self._clicked_button: 
    138138            self._clicked_button.click() 
    139                      
    140 class OWButton(QToolButton): 
    141     ''' 
    142         A custom tool button that can set and attribute or call a function when clicked.  
    143          
    144         :param plot: The object whose attributes will be modified 
    145         :type plot: :obj:`.OWPlot` 
    146     ''' 
    147     def __init__(self, plot, attr_name, attr_value, callback, parent): 
    148         QToolButton.__init__(self, parent) 
    149         self.setMinimumSize(30, 30) 
    150         self.plot = plot 
     139             
     140class OWAction(QAction): 
     141    ''' 
     142      A :obj:.QAction with convenience methods for calling a callback or setting an attribute of the plot.  
     143    ''' 
     144    def __init__(self, plot, icon_name=None, attr_name='', attr_value=None, callback=None, parent=None): 
     145        QAction.__init__(self, parent) 
     146         
    151147        if type(callback) == str: 
    152148            callback = getattr(plot, callback, None) 
    153149        if callback: 
    154             QObject.connect(self, SIGNAL("clicked(bool)"), callback) 
     150            QObject.connect(self, SIGNAL("triggered(bool)"), callback) 
    155151        if attr_name: 
     152            self._plot = plot 
    156153            self.attr_name = attr_name 
    157154            self.attr_value = attr_value 
    158             QObject.connect(self, SIGNAL("clicked(bool)"), self.set_attribute) 
    159          
     155            QObject.connect(self, SIGNAL("triggered(bool)"), self.set_attribute) 
     156        if icon_name: 
     157            self.setIcon(QIcon(os.path.dirname(__file__) + "/../icons/" + icon_name + '.png')) 
     158 
    160159    def set_attribute(self, clicked): 
    161         setattr(self.plot, self.attr_name, self.attr_value) 
     160        setattr(self._plot, self.attr_name, self.attr_value) 
     161         
     162                     
     163class OWButton(QToolButton): 
     164    ''' 
     165        A custom tool button which signal when its down state changes 
     166    ''' 
     167    def __init__(self, action=None, parent=None): 
     168        QToolButton.__init__(self, parent) 
     169        self.setMinimumSize(30, 30) 
     170        if action: 
     171            self.setDefaultAction(action) 
    162172         
    163173    def setDown(self, down): 
    164174        self.emit(SIGNAL("downChanged(bool)"), down) 
    165175        QToolButton.setDown(self, down) 
    166                      
     176     
    167177class OWPlotGUI: 
    168178    ''' 
     
    265275        StateButtonsEnd, 
    266276        Spacing, 
    267         StateButtonsBegin, 
    268             SelectionOne, 
    269             SelectionAdd,  
    270             SelectionRemove, 
    271         StateButtonsEnd, 
    272         Spacing, 
    273277        SendSelection, 
    274278        ClearSelection 
     
    279283        Pan : ('Pan', 'state', PANNING, None, 'Dlg_pan_hand'), 
    280284        Select : ('Select', 'state', SELECT, None, 'Dlg_arrow'), 
    281         SelectionAdd : ('Add to selection', 'selection_behavior', SELECTION_ADD, None, ''), 
    282         SelectionRemove : ('Remove from selection', 'selection_behavior', SELECTION_REMOVE, None, ''), 
    283         SelectionToggle : ('Toggle selection', 'selection_behavior', SELECTION_TOGGLE, None, ''), 
    284         SelectionOne : ('Replace selection', 'selection_behavior', SELECTION_REPLACE, None, ''), 
     285        SelectionAdd : ('Add to selection', 'selection_behavior', SELECTION_ADD, None, 'Dlg_select_add'), 
     286        SelectionRemove : ('Remove from selection', 'selection_behavior', SELECTION_REMOVE, None, 'Dlg_select_remove'), 
     287        SelectionToggle : ('Toggle selection', 'selection_behavior', SELECTION_TOGGLE, None, 'Dlg_select_toggle'), 
     288        SelectionOne : ('Replace selection', 'selection_behavior', SELECTION_REPLACE, None, 'Dlg_arrow'), 
    285289        SendSelection : ('Send selection', None, None, 'send_selection', 'Dlg_send'), 
    286290        ClearSelection : ('Clear selection', None, None, 'clear_selection', 'Dlg_clear'), 
     
    404408        return box 
    405409         
    406     def tool_button(self, id, widget): 
    407         ''' 
    408             Creates an :obj:`.OWButton` and adds it to the parent ``widget``.  
    409         ''' 
     410    def _expand_id(self, id): 
    410411        if type(id) == int: 
    411412            name, attr_name, attr_value, callback, icon_name = self._buttons[id] 
    412413        elif len(id) == 4: 
    413414            name, attr_name, attr_value, callback, icon_name = id 
     415            id = -1 
    414416        else: 
    415417            id, name, attr_name, attr_value, callback, icon_name = id 
    416         b = OWButton(self._plot, attr_name, attr_value, callback, widget) 
     418        return id, name, attr_name, attr_value, callback, icon_name 
     419         
     420    def tool_button(self, id, widget): 
     421        ''' 
     422            Creates an :obj:`.OWButton` and adds it to the parent ``widget``.  
     423        ''' 
     424        id, name, attr_name, attr_value, callback, icon_name = self._expand_id(id) 
     425        if id == OWPlotGUI.Select: 
     426            b = self.menu_button(self.Select, [self.SelectionOne, self.SelectionAdd, self.SelectionRemove, self.SelectionToggle], widget) 
     427        else: 
     428            b = OWButton(OWAction(self._plot, icon_name, attr_name, attr_value, callback), widget) 
    417429        b.setToolTip(name) 
    418         b.setIcon(QIcon(os.path.dirname(__file__) + "/../icons/" + icon_name + '.png')) 
    419430        if widget.layout() is not None: 
    420431            widget.layout().addWidget(b) 
     432        return b 
     433         
     434    def menu_button(self, main_action_id, ids, widget): 
     435        ''' 
     436            Creates an :obj:`.OWButton` with a popup-menu and adds it to the parent ``widget``.  
     437        ''' 
     438        id, name, attr_name, attr_value, callback, icon_name = self._expand_id(main_action_id) 
     439        b = OWButton(parent=widget) 
     440        m = QMenu(b) 
     441        b.setMenu(m) 
     442        QObject.connect(m, SIGNAL("triggered(QAction*)"), b, SLOT("setDefaultAction(QAction*)")) 
     443        QObject.connect(m, SIGNAL("triggered(QAction*)"), b.click) 
     444 
     445        if main_action_id: 
     446            main_action = OWAction(self._plot, icon_name, attr_name, attr_value, callback, parent=b) 
     447            QObject.connect(m, SIGNAL("triggered(QAction*)"), main_action, SLOT("trigger()")) 
     448         
     449        for id in ids: 
     450            id, name, attr_name, attr_value, callback, icon_name = self._expand_id(id) 
     451            m.addAction(OWAction(self._plot, icon_name, attr_name, attr_value, callback, parent=m)) 
     452             
     453        if m.actions(): 
     454            b.setDefaultAction(m.actions()[0]) 
     455        elif main_action_id: 
     456            b.setDefaultAction(main_action) 
     457             
     458         
     459        b.setPopupMode(QToolButton.MenuButtonPopup) 
     460        b.setMinimumSize(40, 30) 
    421461        return b 
    422462         
     
    444484    def zoom_select_toolbar(self, widget, text = 'Zoom / Select', orientation = Qt.Horizontal, buttons = default_zoom_select_buttons): 
    445485        t = self.toolbar(widget, text, orientation, buttons) 
    446         t.groups[self.SelectionOne].setEnabled(t.buttons[self.Select].isDown()) 
    447         QObject.connect(t.buttons[self.Select], SIGNAL("downChanged(bool)"), t.groups[self.SelectionOne].setEnabled) 
    448486        t.buttons[self.Select].click() 
    449         t.buttons[self.SelectionOne].click() 
    450487        return t     
    451488         
Note: See TracChangeset for help on using the changeset viewer.