X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fscreens%2Fmainscreen.py;h=3ac5520c7b265565b8d93c3cf36f43039d6ea8e7;hp=575d5f9183fa3d2c94ee355d8592c46081e12062;hb=7c3b04b5d6a6674a197fa6bf28bfd7ae5e15170d;hpb=3e48f23eafc944bf3ed409623f3fa6243a60bf4c diff --git a/sface/screens/mainscreen.py b/sface/screens/mainscreen.py index 575d5f9..3ac5520 100644 --- a/sface/screens/mainscreen.py +++ b/sface/screens/mainscreen.py @@ -5,7 +5,7 @@ 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 @@ -13,11 +13,66 @@ 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) +class SelectDelegate(QStyledItemDelegate): + pass + +class NodeNameDelegate(QStyledItemDelegate): + def __init__(self, parent): + QStyledItemDelegate.__init__(self) + + def paint(self, painter, option, index): + model = index.model() + data = "%s" % index.data().toString() + select_index = model.index(index.row(), 2, index.parent()) + select_data = select_index.data().toString() + + if select_data not in ("true", "add", "remove"): # 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 select_data == "true": # 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 select_data == "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 select_data == "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 +95,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 +151,207 @@ 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() - - if role != Qt.DisplayRole: - return QVariant() + self.rootItem = TreeItem([QString("Testbed"), QString("Hostname"), QString("Selected")]) - 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 data(self, index, role): + if not index.isValid(): + return QVariant() + + if role != Qt.DisplayRole and role != Qt.EditRole: + return QVariant() - def rowCount(self, parent): - if parent.column() > 0: + 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) - hlayout = QHBoxLayout() - hlayout.addWidget(slicename) - hlayout.addStretch() - hlayout.addWidget(refresh) + self.nodeView = NodeView(self) + self.nodeModel = NodeModel(self) + self.filterModel = QSortFilterProxyModel(self) # enable filtering + + self.nodeNameDelegate = NodeNameDelegate(self) + self.selectDelegate = SelectDelegate(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): + 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) + + for node in sliver_nodes: + nodeItem = TreeItem([QString(""), QString("%s" % node), QString("true")], networkItem) + networkItem.appendChild(nodeItem) + + for node in available_nodes: + nodeItem = TreeItem([QString(""), QString(node), QString("false")], 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.setItemDelegateForColumn(2, self.selectDelegate) + self.nodeView.setModel(self.filterModel) self.nodeView.expandAll() + self.nodeView.resizeColumnToContents(1) class MainScreen(SfaScreen): @@ -215,4 +359,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")