library_tool.py
Go to the documentation of this file.
00001 import tool_utils as tu
00002 from PyQt4.QtGui import *
00003 from PyQt4.QtCore import *
00004 
00005 import os
00006 import glob
00007 import os.path as pt
00008 import cPickle as pk
00009 import shutil as su
00010 import graph_model as gm
00011 import state_machine_tool as smt
00012 
00013 #
00014 # Library nodes saved in ~/.rcommander/library/
00015 #
00016 class LibraryTool(tu.ToolBase):
00017 
00018     def __init__(self, button, rcommander):
00019         tu.ToolBase.__init__(self, rcommander, 'library', 'Library', 'library')
00020         self.button = button
00021         self.rcommander.connect(self.button, SIGNAL('clicked()'), self.activate_cb)
00022 
00023     def _create_tree_model(self):
00024         tree_model = QStandardItemModel()
00025         tree_model.setHorizontalHeaderLabels(['Saved States'])
00026         root_node = tree_model.invisibleRootItem()
00027 
00028         #Traverse directory looking for saved states
00029         state_names = glob.glob(pt.join(self._get_library_home(), '*.state'))
00030         slist_dict = {}
00031         for sfilename in state_names:
00032             sfile = open(sfilename, 'r')
00033             loaded_state = pk.load(sfile)
00034             sfile.close()
00035 
00036             class_name = loaded_state.__class__.__name__
00037             if not slist_dict.has_key(class_name):
00038                 slist_dict[class_name] = {}
00039             slist_dict[class_name][loaded_state.get_name()] = {'file': sfilename, 'state': loaded_state}
00040             #slist_dict[class_name].append([sfilename, loaded_state])
00041 
00042         all_classes = slist_dict.keys()
00043         all_classes.sort()
00044         #print 'all_classes', all_classes
00045         for n in all_classes:
00046             item = QStandardItem(n)
00047             item.setEditable(False)
00048             item.setSelectable(False)
00049             state_names = slist_dict[n].keys()
00050             state_names.sort()
00051             slist_dict[n]['__qstandard_item__'] = item
00052             #print n, slist_dict[n].keys()
00053             for state_name in state_names:
00054                 state_item = QStandardItem(state_name)
00055                 state_item.setEditable(False)
00056                 item.appendRow(state_item)
00057             root_node.appendRow(item)
00058 
00059         return tree_model, root_node, slist_dict
00060 
00061     def edit_cb(self, index):
00062         print 'edit called', index.row()
00063 
00064     def fill_property_box(self, pbox):
00065         #Remove everything from parent container
00066         container = self.rcommander.ui.properties_container
00067         layout = container.layout()
00068         for i in range(layout.count()):
00069             item = layout.itemAt(0)
00070             layout.removeItem(item)
00071         children = container.children()
00072         for c in children[1:]:
00073             try:
00074                 layout.removeWidget(c)
00075             except TypeError, e:
00076                 pass
00077 
00078         layout.invalidate()
00079         container.update()
00080 
00081         #delete layout
00082         clayout = container.layout()
00083         clayout.deleteLater()
00084         QCoreApplication.sendPostedEvents(clayout, QEvent.DeferredDelete)
00085 
00086         #Create a new layout
00087         clayout = QVBoxLayout(container)
00088         clayout.setMargin(0)
00089 
00090         self.tree_view = QTreeView(container)
00091         self.tree_model, root_node, self.slist_dict = self._create_tree_model()
00092         self.tree_view.setModel(self.tree_model)
00093         self.tree_view.expandAll()
00094         #self.tree_view.setEditTriggers(QAbstractItemView.AllEditTriggers)
00095         #self.rcommander.connect(self.tree_view, SIGNAL('edit(QModelIndex)'), self.edit_cb)
00096 
00097         self.button_panel = QWidget(container)
00098         bpanel_layout = QHBoxLayout(self.button_panel)
00099         bpanel_layout.setMargin(0)
00100 
00101         self.delete_button = QPushButton(self.button_panel)
00102         self.delete_button.setText('Delete')
00103         bpanel_layout.addWidget(self.delete_button)
00104         self.rcommander.connect(self.delete_button, SIGNAL('clicked()'), self.delete_cb)
00105 
00106         clayout.addWidget(self.tree_view)
00107         clayout.addWidget(self.button_panel)
00108 
00109         pidx = self.rcommander.ui.node_settings_tabs.indexOf(self.rcommander.ui.properties_container)
00110         self.rcommander.ui.node_settings_tabs.setCurrentIndex(pidx)
00111 
00112     def fill_connections_box(self, pbox):
00113         self.name_input = QLineEdit()
00114         return
00115 
00116     def new_node(self, name = None):
00117         #Get the current selected state, make a clone of it
00118         index = self.tree_view.selectionModel().currentIndex()
00119 
00120         #assume that the correct index is selected always
00121         name_of_state = str(index.data(Qt.DisplayRole).toString())
00122         name_of_class = str(index.parent().data(Qt.DisplayRole).toString())
00123         if name_of_state == '':
00124             return None
00125 
00126         state_file = open(self.slist_dict[name_of_class][name_of_state]['file'], 'r')
00127         state = pk.load(state_file)
00128         state_file.close()
00129         if gm.is_container(state):
00130             state = state.load_and_recreate(self._get_library_home())
00131             if isinstance(state, smt.StateMachineNode):
00132                 curr_document = gm.FSMDocument(state.get_name(), modified=True, real_filename=False)
00133                 state.get_child().set_document(curr_document)
00134 
00135         return state
00136 
00137     def set_node_properties(self, my_node):
00138         pass
00139 
00140     def reset(self):
00141         pass
00142 
00143     def _get_library_home(self):
00144         #create the library if it does not exist
00145         rcommander_home = pt.join(os.getenv("HOME"), '.rcommander')
00146         library_home = pt.join(rcommander_home, 'library')
00147 
00148         #if not pt.exists(rcommander_home):
00149         #    os.mkdir(rcommander_home)
00150 
00151         if not pt.exists(library_home):
00152             os.makedirs(library_home)
00153 
00154         return library_home
00155 
00156     #Special method to this tool, called by rcommander.
00157     def add_to_library(self, state_node):
00158         library_home = self._get_library_home()
00159 
00160         #if there's a node in the library of the same name.
00161         state_fname = pt.join(library_home, "%s.state" % (state_node.get_name()))
00162         i = 0
00163         while pt.exists(state_fname):
00164             state_fname = pt.join(library_home, "%s_%d.state" % (state_node.get_name(), i))
00165             i = i + 1
00166 
00167         #######################################
00168         # Save!
00169         # Special saving for aborted
00170         if gm.is_container(state_node):
00171             state_node.save_child(library_home)
00172             child = state_node.abort_child()
00173         state_file = open(state_fname, 'w')
00174         pk.dump(state_node, state_file)
00175         state_file.close()
00176         if gm.is_container(state_node):
00177             state_node.set_child(child)
00178 
00179         #Recreate everything in the GUI!
00180         self.activate_cb()
00181 
00182         #for state_name in self.states_dict.keys():
00183         #    containerp = is_container(self.states_dict[state_name])
00184         #    if containerp:
00185         #        self.states_dict[state_name].save_child(name)
00186         #        child = self.states_dict[state_name].abort_child()
00187         #    state_fname = pt.join(name, state_name) + '.state'
00188         #    pickle_file = open(state_fname, 'w')
00189         #    #print 'SAVING STATE', state_name, self.states_dict[state_name]
00190         #    pk.dump(self.states_dict[state_name], pickle_file)
00191         #    pickle_file.close()
00192         #    if containerp:
00193         #        #print 'document\'s path was', self.states_dict[state_name].document.get_filename()
00194         #        self.states_dict[state_name].set_child(child)
00195 
00196     def delete_cb(self):
00197         index = self.tree_view.selectionModel().currentIndex()
00198         name_of_state = str(index.data(Qt.DisplayRole).toString())
00199         name_of_class = str(index.parent().data(Qt.DisplayRole).toString())
00200         file_name = self.slist_dict[name_of_class][name_of_state]['file']
00201 
00202         trash_home = pt.join(os.getenv("HOME"), '.Trash')
00203         if not pt.exists(trash_home):
00204             os.mkdir(trash_home)
00205 
00206         state_file = open(file_name, 'r')
00207         state_node = pk.load(state_file)
00208         state_file.close()
00209         su.move(file_name, pt.join(trash_home, pt.split(file_name)[1]))
00210         #Recreate everything in the GUI!
00211         if isinstance(state_node, tu.EmbeddableState):
00212             child_folder_name = pt.join(self._get_library_home(), pt.split(state_node.get_child_document().get_filename())[1])
00213             #child_folder_name = state_node.get_child().get_document().get_filename()
00214             su.move(child_folder_name, pt.join(trash_home, pt.split(child_folder_name)[1]))
00215         self.activate_cb()
00216 
00217 


rcommander
Author(s): Hai Nguyen (haidai@gmail.com)
autogenerated on Thu Nov 28 2013 11:46:34