update view color-coding code
[sface.git] / sface / sliceview.py
1 import datetime
2 import os
3 import pickle
4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6
7 from sfa.util.record import SfaRecord, SliceRecord, AuthorityRecord
8 from sface.config import config
9 from sface.sfidata import SfiData
10
11 NAME_COLUMN = 0
12 MEMBERSHIP_STATUS_COLUMN = 1
13
14 # maximum length of a name to display before clipping
15 NAME_MAX_LEN = 48
16
17 slice_status = { "in": "Selected",
18                 "out": "Not Selected"}
19
20 color_status = { "in": QColor.fromRgb(0, 250, 250), }
21
22 class SliceNameDelegate(QStyledItemDelegate):
23     def __init__(self, parent):
24         QStyledItemDelegate.__init__(self, parent)
25
26     def displayText(self, value, locale):
27         data = str(QStyledItemDelegate.displayText(self, value, locale))
28         if (len(data)>NAME_MAX_LEN):
29             data = data[:(NAME_MAX_LEN-3)] + "..."
30         return QString(data)
31
32     def paint(self, painter, option, index):
33         model = index.model()
34         data = str(self.displayText(index.data(), QLocale()))
35         status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
36         status_data = status_index.data().toString()
37
38         fm = QFontMetrics(option.font)
39         rect = QRect(option.rect)
40
41         rect.setHeight(rect.height() - 2)
42         rect.setWidth(fm.width(QString(data)) + 6)
43         rect.setX(rect.x() + 5)
44         rect.setY(rect.y() - 1)
45
46         textRect = QRect(option.rect)
47         textRect.setWidth(fm.width(QString(data)) + 6)
48         textRect.setX(rect.x())
49
50         x, y, h, w = rect.x(), rect.y(), rect.height(), rect.width()
51
52         path = QPainterPath()
53         path.addRoundedRect(x - 1, y + 1, w, h, 4, 4)
54
55         painter.save()
56         painter.setRenderHint(QPainter.Antialiasing)
57
58         if option.state & QStyle.State_Selected:
59             painter.fillRect(option.rect, option.palette.color(QPalette.Active, QPalette.Highlight))
60
61         color = None
62         for x in slice_status.keys():
63             if (slice_status[x] == status_data) and (x in color_status):
64                 color = color_status[x]
65
66         if color != None:
67             painter.fillPath(path, color)
68         painter.setPen(QColor.fromRgb(0, 0, 0))
69         painter.drawText(textRect, Qt.AlignVCenter, QString(data))
70
71         painter.restore()
72
73 class SliceView(QTableView):
74     def __init__(self, parent=None):
75         QTableView.__init__(self, parent)
76
77         self.setSelectionBehavior(QAbstractItemView.SelectRows)
78         self.setSelectionMode(QAbstractItemView.SingleSelection)
79         self.setAlternatingRowColors(True)
80         self.setAttribute(Qt.WA_MacShowFocusRect, 0)
81         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
82         self.setToolTip("Double click on a row to change its status.")
83
84         self.setItemDelegateForColumn(0, SliceNameDelegate(self))
85
86     def keyPressEvent(self, event):
87         if (event.key() == Qt.Key_Space):
88             self.toggleSelection()
89         else:
90             QTableView.keyPressEvent(self, event)
91
92     def mouseDoubleClickEvent(self, event):
93         self.toggleSelection()
94
95     def toggleSelection(self):
96         index = self.currentIndex()
97         model = index.model()
98         status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
99         status_data = status_index.data().toString()
100         node_index = model.index(index.row(), NAME_COLUMN, index.parent())
101         node_data = node_index.data().toString()
102
103         if status_data == slice_status['in']:
104             model.setData(status_index, QString(slice_status['out']))
105         else:
106             model.setData(status_index, QString(slice_status['in']))
107
108         model.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), node_index, node_index)
109
110     def currentChanged(self, current, previous):
111         model = current.model()
112         node_index = model.index(current.row(), 0, current.parent())
113         node_data = node_index.data().toString()
114
115 class SliceModel(QStandardItemModel):
116     def __init__(self, rows=0, columns=4, parent=None):
117          QStandardItemModel.__init__(self, rows, columns, parent)
118
119     def updateModel(self):
120         self.clear()
121
122         slice_names = SfiData().getAuthorityHrns(type="slice")
123
124         rootItem = self.invisibleRootItem()
125
126         for name in slice_names:
127             rootItem.appendRow([self.readOnlyItem(name),
128                                 self.readOnlyItem(slice_status["out"])])
129
130         headers = QStringList() << "Slice Name" << "Status"
131         self.setHorizontalHeaderLabels(headers)
132
133     def readOnlyItem(self, x):
134         item = QStandardItem(QString(x))
135         item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
136         return item
137
138     def getSelectedSlices(self):
139         slices = []
140         item = self.invisibleRootItem()
141         children = item.rowCount()
142         for row in range(0, children):
143             childName = str(item.child(row, NAME_COLUMN).data(Qt.DisplayRole).toString())
144             childStatus = str(item.child(row, MEMBERSHIP_STATUS_COLUMN).data(Qt.DisplayRole).toString())
145
146             if (childStatus == slice_status['in']):
147                 slices.append(childName)
148
149         return slices
150