Ignore:
Timestamp:
01/08/13 17:20:05 (16 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Changed the WidgetToolBox save/restoreState format.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/OrangeCanvas/application/widgettoolbox.py

    r11243 r11254  
    88""" 
    99 
    10 from ast import literal_eval 
    1110import logging 
    1211 
     
    1615) 
    1716 
    18 from PyQt4.QtCore import Qt, QModelIndex, QSize 
    19  
    20 from PyQt4.QtCore import QObject, QEvent, QMimeData 
     17from PyQt4.QtCore import ( 
     18    Qt, QObject, QModelIndex, QSize, QEvent, QMimeData, QByteArray, 
     19    QDataStream, QIODevice 
     20) 
     21 
    2122from PyQt4.QtCore import  pyqtSignal as Signal, pyqtProperty as Property 
    2223 
     
    265266 
    266267        """ 
    267         version = 1 
    268  
    269         tabs = [] 
    270         for i in range(self.count()): 
    271             tab_action = self.tabAction(i) 
    272             name = unicode(tab_action.text()) 
    273             state = tab_action.isChecked() 
    274             tabs.append((name, state)) 
    275  
    276         return str((version, tabs)) 
     268        version = 2 
     269 
     270        actions = map(self.tabAction, range(self.count())) 
     271        expanded = [action for action in actions if action.isChecked()] 
     272        expanded = [action.text() for action in expanded] 
     273 
     274        byte_array = QByteArray() 
     275        stream = QDataStream(byte_array, QIODevice.WriteOnly) 
     276        stream.writeInt(version) 
     277        stream.writeQStringList(expanded) 
     278 
     279        return byte_array 
    277280 
    278281    def restoreState(self, state): 
    279         """Restore the toolbox from `state`. 
    280         """ 
    281         try: 
    282             version, tabs = literal_eval(str(state)) 
    283         except (ValueError, SyntaxError, TypeError): 
    284             return False 
    285  
    286         if version != 1: 
    287             return False 
    288  
    289         state = dict(tabs) 
    290  
    291         for i in range(self.count()): 
    292             tab_action = self.tabAction(i) 
    293             name = unicode(tab_action.text()) 
    294             checked = state.get(name, tab_action.isChecked()) 
    295             if checked != tab_action.isChecked(): 
    296                 tab_action.trigger() 
     282        """Restore the toolbox from a :class:`QByteArray` `state`. 
     283 
     284        .. note:: The toolbox should already be populated for the state 
     285                  changes to take effect. 
     286 
     287        """ 
     288        # In version 1 of saved state the state was saved in 
     289        # a simple dict repr string. 
     290        if isinstance(state, QByteArray): 
     291            stream = QDataStream(state, QIODevice.ReadOnly) 
     292            version = stream.readInt() 
     293            if version == 2: 
     294                expanded = stream.readQStringList() 
     295                for action in map(self.tabAction, range(self.count())): 
     296                    if (action.text() in expanded) != action.isChecked(): 
     297                        action.trigger() 
     298 
     299                return True 
     300        return False 
    297301 
    298302    def setModel(self, model): 
Note: See TracChangeset for help on using the changeset viewer.