fix navigation keys in node view
[sface.git] / sface / screens / mainscreen.py
index b20ae51..96a9b06 100644 (file)
@@ -7,7 +7,7 @@ from PyQt4.QtGui import *
 #from sfa.util.rspecHelper import RSpec
 from sfa.rspecs.rspec_parser import parse_rspec
 from sface.config import config
-from sface.sfirenew import SfiRenewer
+from sface.sfirenew import RenewWindow
 from sface.sfiprocess import SfiProcess
 from sface.screens.sfascreen import SfaScreen
 
@@ -31,6 +31,9 @@ NODE_STATUS_COLUMN = 1
 MEMBERSHIP_STATUS_COLUMN = 2
 KIND_COLUMN = 3
 
+# maximum length of a name to display before clipping
+NAME_MAX_LEN = 48
+
 def itemType(index):
     if index.parent().parent().isValid():
         return "tag"
@@ -51,7 +54,16 @@ class NodeView(QTreeView):
         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
         self.setToolTip("Double click on a row to change its status.  Right click on a host to add a tag.")
 
+    def keyPressEvent(self, event):
+        if (event.key() == Qt.Key_Space):
+            self.toggleSelection()
+        else:
+            QTreeView.keyPressEvent(self, event)
+
     def mouseDoubleClickEvent(self, event):
+        self.toggleSelection()
+
+    def toggleSelection(self):
         index = self.currentIndex()
         model = index.model()
         status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
@@ -124,7 +136,7 @@ class NodeView(QTreeView):
                         tagstring = QString("%s: %s" % (tagname, value))
                         tagItem = QStandardItem(tagstring)
                         status = QStandardItem(QString(tag_status['add']))
-                        nodeItem.appendRow([tagItem, status])
+                        nodeItem.appendRow([tagItem, QStandardItem(QString("")), status])
 
             elif status_data in (node_status['out'], node_status['remove']):
                 QMessageBox.warning(self, "Not selected", "Can only add tags to selected nodes")
@@ -144,15 +156,21 @@ class NodeNameDelegate(QStyledItemDelegate):
     def __init__(self, parent):
         QStyledItemDelegate.__init__(self, parent)
 
+    def displayText(self, value, locale):
+        data = str(QStyledItemDelegate.displayText(self, value, locale))
+        if (len(data)>NAME_MAX_LEN):
+            data = data[:(NAME_MAX_LEN-3)] + "..."
+        return QString(data)
+
     def paint(self, painter, option, index):
         model = index.model()
+        data = str(self.displayText(index.data(), QLocale()))
         status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
         status_data = status_index.data().toString()
 
         fm = QFontMetrics(option.font)
         rect = QRect(option.rect)
 
-        data = index.data().toString()
         rect.setHeight(rect.height() - 2)
         rect.setWidth(fm.width(QString(data)) + 6)
         rect.setX(rect.x() + 5)
@@ -348,8 +366,16 @@ class SliceWidget(QWidget):
         self.updateView()
 
     def submitFinished(self):
-        self.setStatus("<font color='green'>Slice data submitted.</font>")
-        QTimer.singleShot(1000, self.refresh)
+        faultString = self.process.getFaultString()
+        if not faultString:
+            self.setStatus("<font color='green'>Slice data submitted.</font>")
+        else:
+            self.setStatus("<font color='red'>Slice submit failed: %s</font>" % (faultString))
+
+        # no need to do that anymore
+        # QTimer.singleShot(1000, self.refresh)
+        self.updateView()
+        self.parent().signalAll("rspecUpdated")
 
     def refreshFinished(self):
         self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
@@ -452,19 +478,7 @@ class SliceWidget(QWidget):
 
     def renew(self):
         dlg = RenewWindow(parent=self)
-        if (dlg.exec_() == QDialog.Accepted):
-            self.setStatus("Renewing Slice.")
-
-            self.renewProcess = SfiRenewer(config.getSlice(), dlg.get_new_expiration(), self)
-            self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
-
-    def renewFinished(self):
-        if self.renewProcess.statusMsg:
-            self.setStatus("Renew " + self.renewProcess.status + ": " + self.renewProcess.statusMsg)
-        else:
-            self.setStatus("Renew " + self.renewProcess.status)
-        self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
-        self.renewProcess = None
+        dlg.exec_()
 
     def refresh(self):
         if not config.getSlice():
@@ -478,7 +492,7 @@ class SliceWidget(QWidget):
         self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished)
         self.connect(self.process, SIGNAL('finished()'), self.refreshFinished)
 
-        self.process.getRSpecFromSM()
+        self.process.retrieveRspec()
         self.setStatus("Refreshing slice data. This will take some time...")
 
     def updateView(self):
@@ -562,44 +576,6 @@ class SliceWidget(QWidget):
     def nodeSelectionChanged(self, hostname):
         self.parent().nodeSelectionChanged(hostname)
 
-class RenewWindow(QDialog):
-    def __init__(self, parent=None):
-        super(RenewWindow, self).__init__(parent)
-        self.setWindowTitle("Renew Slivers")
-
-        self.duration = QComboBox()
-
-        self.expirations = []
-
-        durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") )
-
-        now = datetime.datetime.utcnow()
-        for (weeks, desc) in durations:
-            exp = now + datetime.timedelta(days = weeks * 7)
-            desc = desc + " " + exp.strftime("%Y-%m-%d %H:%M:%S")
-            self.expirations.append(exp)
-            self.duration.addItem(desc)
-
-        self.duration.setCurrentIndex(0)
-
-        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
-        buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
-
-        layout = QVBoxLayout()
-        layout.addWidget(self.duration)
-        layout.addWidget(buttonBox)
-        self.setLayout(layout)
-
-        self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
-        self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
-
-    def accept(self):
-        QDialog.accept(self)
-
-    def get_new_expiration(self):
-        index = self.duration.currentIndex()
-        return self.expirations[index]
-
 class MainScreen(SfaScreen):
     def __init__(self, parent):
         SfaScreen.__init__(self, parent)