X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fscreens%2Fmainscreen.py;h=fa29ec665b2332dfe60f58803010ce6815212019;hp=575d5f9183fa3d2c94ee355d8592c46081e12062;hb=6918ef20652170b47750757550e49358bf6ec1dc;hpb=3e48f23eafc944bf3ed409623f3fa6243a60bf4c;ds=sidebyside
diff --git a/sface/screens/mainscreen.py b/sface/screens/mainscreen.py
index 575d5f9..fa29ec6 100644
--- a/sface/screens/mainscreen.py
+++ b/sface/screens/mainscreen.py
@@ -5,19 +5,99 @@ from PyQt4.QtGui import *
from sfa.util.rspecHelper import RSpec
from sface.sfahelper import *
-from sface.sficonfig import config
+from sface.config import config
from sface.sfiprocess import SfiProcess
from sface.screens.sfascreen import SfaScreen
+already_in_nodes = []
+
+node_status = { "in": "Already Selected",
+ "out": "Not Selected",
+ "add": "To be Added",
+ "remove": "To be Removed"}
+
class NodeView(QTreeView):
def __init__(self, parent):
QTreeView.__init__(self, parent)
+ self.setAnimated(True)
self.setItemsExpandable(True)
self.setRootIsDecorated(True)
self.setAlternatingRowColors(True)
+# self.setSelectionMode(self.MultiSelection)
+ self.setAttribute(Qt.WA_MacShowFocusRect, 0)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
+ def mouseDoubleClickEvent(self, event):
+ index = self.currentIndex()
+ model = index.model()
+ status_index = model.index(index.row(), 2, index.parent())
+ status_data = status_index.data().toString()
+ hostname_index = model.index(index.row(), 1, index.parent())
+ hostname_data = hostname_index.data().toString()
+
+ if status_data == node_status['in']:
+ model.setData(status_index, QString(node_status['remove']))
+ elif status_data == node_status['out']:
+ model.setData(status_index, QString(node_status['add']))
+ elif status_data in (node_status['add'], node_status['remove']):
+ if hostname_data in already_in_nodes: model.setData(status_index, QString(node_status['in']))
+ else: model.setData(status_index, QString(node_status['out']))
+
+ model.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), hostname_index, hostname_index)
+
+
+
+class NodeNameDelegate(QStyledItemDelegate):
+ def __init__(self, parent):
+ QStyledItemDelegate.__init__(self, parent)
+
+ def paint(self, painter, option, index):
+ model = index.model()
+ data = "%s" % index.data().toString()
+ status_index = model.index(index.row(), 2, index.parent())
+ status_data = status_index.data().toString()
+
+ if status_data not in (node_status['in'], node_status['remove'], node_status['add']):
+ # default view
+ QStyledItemDelegate.paint(self, painter, option, index)
+ return
+
+ fm = QFontMetrics(option.font)
+ rect = option.rect
+ rect.setWidth(fm.width(QString(data)) + 8)
+ rect.setHeight(rect.height() - 2)
+ rect.setX(rect.x() + 4)
+ x, y, h, w = rect.x(), rect.y(), rect.height(), rect.width()
+
+ path = QPainterPath()
+ path.addRoundedRect(x, y, w, h, 4, 4)
+
+ painter.save()
+ painter.setRenderHint(QPainter.Antialiasing)
+ painter.drawRoundedRect(rect, 4, 4)
+
+ if status_data == node_status['in']: # already in the slice
+ painter.fillPath(path, QColor.fromRgb(0, 250, 0))
+ painter.setPen(QColor.fromRgb(0, 0, 0))
+ painter.drawText(option.rect, 0, QString(data))
+
+ elif status_data == node_status['add']: # newly added to the slice
+ painter.fillPath(path, QColor.fromRgb(0, 250, 0))
+ painter.setPen(QColor.fromRgb(0, 0, 0))
+ painter.drawText(option.rect, 0, QString(data))
+ painter.drawRect(x + w + 10, y + 3, 10, 10)
+ painter.fillRect(x + w + 10, y + 3, 10, 10, QColor.fromRgb(0, 250, 0))
+
+ elif status_data == node_status['remove']: # removed from the slice
+ painter.fillPath(path, QColor.fromRgb(250, 0, 0))
+ painter.setPen(QColor.fromRgb(0, 0, 0))
+ painter.drawText(option.rect, 0, QString(data))
+ painter.drawRect(x + w + 10, y + 3, 10, 10)
+ painter.fillRect(x + w + 10, y + 3, 10, 10, QColor.fromRgb(250, 0, 0))
+
+ painter.restore()
+
class TreeItem:
def __init__(self, data, parent=None):
self.parentItem = parent
@@ -40,19 +120,46 @@ class TreeItem:
def childCount(self):
return len(self.childItems)
+ def childNumber(self):
+ if self.parentItem:
+ return self.parentItem.childItems.index(self)
+ return 0
+
def columnCount(self):
return len(self.itemData)
def data(self, column):
return self.itemData[column]
- def row(self):
- if (self.parentItem):
- try:
- return self.parentItem.childItems.index(self)
- except ValueError:
- return 0
- return 0
+ def insertChildren(self, position, count, columns):
+ if position < 0 or position > len(self.childItems):
+ return False
+
+ for row in range(count):
+ data = self.data(columns)
+ item = TreeItem(data, self)
+ self.childItems.insert(position, item)
+
+ return True
+
+ def insertColumns(self, position, columns):
+ if position < 0 or position > len(self.itemData):
+ return False
+
+ for column in range(columns):
+ self.itemData.insert(position, QVariant())
+
+ for child in self.childItems:
+ child.insertColumns(position, columns)
+
+ return True
+
+ def setData(self, column, value):
+ if column < 0 or column >= len(self.itemData):
+ return False
+
+ self.itemData[column] = value
+ return True
def parent(self):
return self.parentItem
@@ -69,145 +176,210 @@ class NodeModel(QAbstractItemModel):
self.__initRoot()
def __initRoot(self):
- self.rootItem = TreeItem([QString("Testbed"), QString("Hostname")])
-
- def columnCount(self, parent):
- if parent.isValid():
- return parent.internalPointer().columnCount()
- else:
- return self.rootItem.columnCount()
-
- def data(self, index, role):
- if not index.isValid():
- return QVariant()
+ self.rootItem = TreeItem([QString("Testbed"), QString("Hostname"), QString("Status")])
- if role != Qt.DisplayRole:
- return QVariant()
-
- item = index.internalPointer()
- return item.data(index.column())
+ def getItem(self, index):
+ if index.isValid():
+ item = index.internalPointer()
+ if item: return item
+ return self.rootItem
def headerData(self, section, orientation, role):
- if orientation == Qt.Horizontal and role == Qt.DisplayRole:
+ if orientation == Qt.Horizontal and role in (Qt.DisplayRole, Qt.EditRole):
return self.rootItem.data(section)
return QVariant()
- def flags(self, index):
- if not index.isValid():
- return 0
- return Qt.ItemIsEnabled | Qt.ItemIsSelectable
-
def index(self, row, column, parent):
if not self.hasIndex(row, column, parent):
return QModelIndex()
- parentItem = None
- if not parent.isValid():
- parentItem = self.rootItem
- else:
- parentItem = parent.internalPointer()
-
+ parentItem = self.getItem(parent)
childItem = parentItem.child(row)
if childItem:
return self.createIndex(row, column, childItem)
else:
return QModelIndex()
+ def insertColumns(self, position, columns, parent):
+ self.beginInsertColumns(parent, position, position + columns -1)
+ ret = self.rootItem.insertColumns(position, columns)
+ self.endInsertColumns()
+ return ret
+
+ def insertRows(self, position, rows, parent):
+ parentItem = self.getItem(parent)
+ self.beginInsertRows(parent, position, position + rows -1)
+ ret = parentItem.insertChildren(position, rows, self.rootItem.columnCount())
+ self.endInsertRows()
+ return ret
+
def parent(self, index):
if not index.isValid():
return QModelIndex()
- childItem = index.internalPointer()
+ childItem = self.getItem(index)
parentItem = childItem.parent()
-
- if not parentItem:
- return QModelIndex()
-
if parentItem is self.rootItem:
return QModelIndex()
- return self.createIndex(parentItem.row(), 0, parentItem)
+ return self.createIndex(parentItem.childNumber(), 0, parentItem)
+
+ def rowCount(self, parent=QModelIndex()):
+ parentItem = self.getItem(parent)
+ return parentItem.childCount()
+
+ def columnCount(self, parent=None):
+ return self.rootItem.columnCount()
- def rowCount(self, parent):
- if parent.column() > 0:
+ def data(self, index, role):
+ if not index.isValid():
+ return QVariant()
+
+ if role != Qt.DisplayRole and role != Qt.EditRole:
+ return QVariant()
+
+ item = self.getItem(index)
+ return item.data(index.column())
+
+ def flags(self, index):
+ if not index.isValid():
return 0
+ return Qt.ItemIsEnabled | Qt.ItemIsSelectable# | Qt.ItemIsEditable
- parentItem = None
- if not parent.isValid():
- parentItem = self.rootItem
- else:
- parentItem = parent.internalPointer()
+ def setData(self, index, value, role):
+ if role != Qt.EditRole:
+ return False
+
+ item = self.getItem(index)
+ ret = item.setData(index.column(), value)
+ if ret:
+ self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), index, index)
+ return ret
- return parentItem.childCount()
class SliceWidget(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
- self.nodeView = NodeView(self)
- self.nodeModel = NodeModel(self)
- self.nodeView.setModel(self.nodeModel)
+ self.network_names = []
- refresh = QLabel("Refresh", self)
- refresh.setScaledContents(False)
slicename = QLabel ("Slice : %s"%(config.getSlice() or "None"),self)
slicename.setScaledContents(False)
+ searchlabel = QLabel ("Search: ", self)
+ searchlabel.setScaledContents(False)
+ searchbox = QLineEdit(self)
+ searchbox.setAttribute(Qt.WA_MacShowFocusRect, 0)
+
+ toplayout = QHBoxLayout()
+ toplayout.addWidget(slicename, 0, Qt.AlignLeft)
+ toplayout.addStretch()
+ toplayout.addWidget(searchlabel, 0, Qt.AlignRight)
+ toplayout.addWidget(searchbox, 0, Qt.AlignRight)
+
+ self.nodeView = NodeView(self)
+ self.nodeModel = NodeModel(self)
+ self.filterModel = QSortFilterProxyModel(self) # enable filtering
- hlayout = QHBoxLayout()
- hlayout.addWidget(slicename)
- hlayout.addStretch()
- hlayout.addWidget(refresh)
+ self.nodeNameDelegate = NodeNameDelegate(self)
+
+ refresh = QPushButton("Update Slice Data", self)
+ refresh.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
+ submit = QPushButton("Submit", self)
+ submit.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
+
+ bottomlayout = QHBoxLayout()
+ bottomlayout.addWidget(refresh, 0, Qt.AlignLeft)
+ bottomlayout.addStretch()
+ bottomlayout.addWidget(submit, 0, Qt.AlignRight)
layout = QVBoxLayout()
- layout.addLayout(hlayout)
+ layout.addLayout(toplayout)
layout.addWidget(self.nodeView)
+ layout.addLayout(bottomlayout)
self.setLayout(layout)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
- self.connect(refresh, SIGNAL('linkActivated(QString)'), self.refresh)
+ self.connect(refresh, SIGNAL('clicked()'), self.refresh)
+ self.connect(submit, SIGNAL('clicked()'), self.submit)
+ self.connect(searchbox, SIGNAL('textChanged(QString)'), self.filter)
- rspec_file = os.path.expanduser("~/.sfi/%s.rspec" % config.getSlice())
- if os.path.exists(rspec_file):
- self.updateView()
+ self.updateView()
+
+ def filter(self, filter_string):
+ # for hierarchical models QSortFilterProxyModel applies the
+ # sort recursively. if the parent doesn't match the criteria
+ # we won't be able to match the children. so we need to match
+ # parent (by matching the network_names)
+ networks = ["^%s$" % n for n in self.network_names]
+ filters = networks + [str(filter_string)]
+ self.filterModel.setFilterRegExp(QRegExp('|'.join(filters)))
+
+ def submit(self):
+ self.parent().setStatus("TODO: Submit not implemented yet!", 3000)
def readSliceRSpec(self):
- rspec_file = os.path.expanduser("~/.sfi/%s.rspec" % config.getSlice())
- xml = open(rspec_file).read()
- return xml
+ rspec_file = config.getSliceRSpecFile()
+ if os.path.exists(rspec_file):
+ xml = open(rspec_file).read()
+ return xml
+ return None
- def refresh(self, link=None):
+ def refresh(self):
if not config.getSlice():
- self.parent().setStatus("Slice not set yet!")
+ self.parent().setStatus("Slice not set yet!", timeout=None)
return
self.process = SfiProcess()
outfile = self.process.getRSpecFromSM()
- self.parent().setStatus("Updating slice data. This may take some time...")
+ self.parent().setStatus("Updating slice data. This may take some time...", timeout=None)
self.connect(self.process, SIGNAL('finished()'), self.refreshFinished)
def refreshFinished(self):
del self.process
- self.parent().setStatus("Slice data updated.")
+ self.parent().setStatus("Slice data updated.", timeout=5000)
self.updateView()
def updateView(self):
+ global already_in_nodes
+ already_in_nodes = []
+ self.network_names = []
self.nodeModel.clear()
+
rspec_string = self.readSliceRSpec()
- networks = rspec_get_networks(rspec_string)
+ if not rspec_string:
+ return None
+ networks = rspec_get_networks(rspec_string)
for network in networks:
- networkItem = TreeItem([QString(network), QString("")], self.nodeModel.rootItem)
+ self.network_names.append(network)
+ networkItem = TreeItem([QString(network), QString(""), QString("")], self.nodeModel.rootItem)
all_nodes = rspec_get_nodes_from_network(rspec_string, network)
- for node in all_nodes:
- nodeItem = TreeItem([QString(""), QString(node)], networkItem)
+ sliver_nodes = rspec_get_sliver_nodes_from_network(rspec_string, network)
+ available_nodes = filter(lambda x:x not in sliver_nodes, all_nodes)
+
+ already_in_nodes += sliver_nodes
+
+ for node in sliver_nodes:
+ nodeItem = TreeItem([QString(""), QString("%s" % node), QString(node_status['in'])], networkItem)
+ networkItem.appendChild(nodeItem)
+
+ for node in available_nodes:
+ nodeItem = TreeItem([QString(""), QString(node), QString(node_status['out'])], networkItem)
networkItem.appendChild(nodeItem)
self.nodeModel.rootItem.appendChild(networkItem)
+ self.filterModel.setSourceModel(self.nodeModel)
+ self.filterModel.setFilterKeyColumn(-1)
+ self.filterModel.setDynamicSortFilter(True)
+
+ self.nodeView.setItemDelegateForColumn(1, self.nodeNameDelegate)
+ self.nodeView.setModel(self.filterModel)
self.nodeView.expandAll()
+ self.nodeView.resizeColumnToContents(1)
class MainScreen(SfaScreen):
@@ -215,4 +387,4 @@ class MainScreen(SfaScreen):
SfaScreen.__init__(self, parent)
slice = SliceWidget(self)
- self.init(slice, "Main Window", "PlanetLab Federation GUI")
+ self.init(slice, "Main Window", "OneLab Federation GUI")