|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from collections import defaultdict |
| 4 | + |
| 5 | +from Qt import QtWidgets, QtCore, QtGui |
| 6 | + |
| 7 | +from ..constants import URN_SCHEME |
| 8 | + |
| 9 | + |
| 10 | +class NodesGridDelagate(QtWidgets.QStyledItemDelegate): |
| 11 | + |
| 12 | + def paint(self, painter, option, index): |
| 13 | + """ |
| 14 | + Args: |
| 15 | + painter (QtGui.QPainter): |
| 16 | + option (QtGui.QStyleOptionViewItem): |
| 17 | + index (QtCore.QModelIndex): |
| 18 | + """ |
| 19 | + if index.column() != 0: |
| 20 | + super(NodesGridDelagate, self).paint(painter, option, index) |
| 21 | + return |
| 22 | + |
| 23 | + model = index.model().sourceModel() |
| 24 | + item = model.item(index.row(), index.column()) |
| 25 | + |
| 26 | + sub_margin = 2 |
| 27 | + radius = 5 |
| 28 | + |
| 29 | + base_rect = QtCore.QRectF( |
| 30 | + option.rect.x() + sub_margin, |
| 31 | + option.rect.y() + sub_margin, |
| 32 | + option.rect.width() - (sub_margin * 2), |
| 33 | + option.rect.height() - (sub_margin * 2) |
| 34 | + ) |
| 35 | + |
| 36 | + painter.save() |
| 37 | + painter.setRenderHint(QtGui.QPainter.Antialiasing, True) |
| 38 | + |
| 39 | + # background. |
| 40 | + bg_color = option.palette.window().color() |
| 41 | + pen_color = option.palette.midlight().color().lighter(120) |
| 42 | + if option.state & QtWidgets.QStyle.State_Selected: |
| 43 | + bg_color = bg_color.lighter(120) |
| 44 | + pen_color = pen_color.lighter(160) |
| 45 | + |
| 46 | + pen = QtGui.QPen(pen_color, 3.0) |
| 47 | + pen.setCapStyle(QtCore.Qt.RoundCap) |
| 48 | + painter.setPen(pen) |
| 49 | + painter.setBrush(QtGui.QBrush(bg_color)) |
| 50 | + painter.drawRoundRect(base_rect, |
| 51 | + int(base_rect.height()/radius), |
| 52 | + int(base_rect.width()/radius)) |
| 53 | + |
| 54 | + pen_color = option.palette.midlight().color().darker(130) |
| 55 | + if option.state & QtWidgets.QStyle.State_Selected: |
| 56 | + pen_color = option.palette.highlight().color() |
| 57 | + pen = QtGui.QPen(pen_color, 1.0) |
| 58 | + pen.setCapStyle(QtCore.Qt.RoundCap) |
| 59 | + painter.setPen(pen) |
| 60 | + painter.setBrush(QtCore.Qt.NoBrush) |
| 61 | + |
| 62 | + sub_margin = 6 |
| 63 | + sub_rect = QtCore.QRectF( |
| 64 | + base_rect.x() + sub_margin, |
| 65 | + base_rect.y() + sub_margin, |
| 66 | + base_rect.width() - (sub_margin * 2), |
| 67 | + base_rect.height() - (sub_margin * 2) |
| 68 | + ) |
| 69 | + painter.drawRoundRect(sub_rect, |
| 70 | + int(sub_rect.height() / radius), |
| 71 | + int(sub_rect.width() / radius)) |
| 72 | + |
| 73 | + painter.setBrush(QtGui.QBrush(pen_color)) |
| 74 | + edge_size = 2, sub_rect.height() - 6 |
| 75 | + left_x = sub_rect.left() |
| 76 | + right_x = sub_rect.right() - edge_size[0] |
| 77 | + pos_y = sub_rect.center().y() - (edge_size[1] / 2) |
| 78 | + |
| 79 | + for pos_x in [left_x, right_x]: |
| 80 | + painter.drawRect(QtCore.QRectF( |
| 81 | + pos_x, pos_y, edge_size[0], edge_size[1] |
| 82 | + )) |
| 83 | + |
| 84 | + # painter.setPen(QtCore.Qt.NoPen) |
| 85 | + painter.setBrush(QtGui.QBrush(bg_color)) |
| 86 | + dot_size = 4 |
| 87 | + left_x = sub_rect.left() - 1 |
| 88 | + right_x = sub_rect.right() - (dot_size - 1) |
| 89 | + pos_y = sub_rect.center().y() - (dot_size / 2) |
| 90 | + for pos_x in [left_x, right_x]: |
| 91 | + painter.drawEllipse(QtCore.QRectF( |
| 92 | + pos_x, pos_y, dot_size, dot_size |
| 93 | + )) |
| 94 | + pos_x -= dot_size + 2 |
| 95 | + |
| 96 | + # text |
| 97 | + pen_color = option.palette.text().color() |
| 98 | + pen = QtGui.QPen(pen_color, 0.5) |
| 99 | + pen.setCapStyle(QtCore.Qt.RoundCap) |
| 100 | + painter.setPen(pen) |
| 101 | + |
| 102 | + font = painter.font() |
| 103 | + font_metrics = QtGui.QFontMetrics(font) |
| 104 | + font_width = font_metrics.width(item.text().replace(' ', '_')) |
| 105 | + font_height = font_metrics.height() |
| 106 | + text_rect = QtCore.QRectF( |
| 107 | + sub_rect.center().x() - (font_width / 2), |
| 108 | + sub_rect.center().y() - (font_height * 0.55), |
| 109 | + font_width, font_height) |
| 110 | + painter.drawText(text_rect, item.text()) |
| 111 | + painter.restore() |
| 112 | + |
| 113 | + |
| 114 | +class NodesGridProxyModel(QtCore.QSortFilterProxyModel): |
| 115 | + |
| 116 | + def __init__(self, parent=None): |
| 117 | + super(NodesGridProxyModel, self).__init__(parent) |
| 118 | + |
| 119 | + def mimeData(self, indexes): |
| 120 | + node_ids = ['node:{}'.format(i.data(QtCore.Qt.ToolTipRole)) |
| 121 | + for i in indexes] |
| 122 | + node_urn = URN_SCHEME + ';'.join(node_ids) |
| 123 | + mime_data = super(NodesGridProxyModel, self).mimeData(indexes) |
| 124 | + mime_data.setUrls([node_urn]) |
| 125 | + return mime_data |
| 126 | + |
| 127 | + |
| 128 | +class NodesGridView(QtWidgets.QListView): |
| 129 | + |
| 130 | + def __init__(self, parent=None): |
| 131 | + super(NodesGridView, self).__init__(parent) |
| 132 | + self.setSelectionMode(self.ExtendedSelection) |
| 133 | + self.setUniformItemSizes(True) |
| 134 | + self.setResizeMode(self.Adjust) |
| 135 | + self.setViewMode(self.IconMode) |
| 136 | + self.setDragDropMode(self.DragOnly) |
| 137 | + self.setDragEnabled(True) |
| 138 | + self.setMinimumSize(450, 300) |
| 139 | + self.setSpacing(4) |
| 140 | + |
| 141 | + model = QtGui.QStandardItemModel() |
| 142 | + proxy_model = NodesGridProxyModel() |
| 143 | + proxy_model.setSourceModel(model) |
| 144 | + self.setModel(proxy_model) |
| 145 | + self.setItemDelegate(NodesGridDelagate(self)) |
| 146 | + |
| 147 | + def clear(self): |
| 148 | + self.model().sourceMode().clear() |
| 149 | + |
| 150 | + def add_item(self, label, tooltip=''): |
| 151 | + item = QtGui.QStandardItem(label) |
| 152 | + item.setSizeHint(QtCore.QSize(130, 40)) |
| 153 | + item.setToolTip(tooltip) |
| 154 | + model = self.model().sourceModel() |
| 155 | + model.appendRow(item) |
| 156 | + |
| 157 | + |
| 158 | +class NodesPaletteWidget(QtWidgets.QWidget): |
| 159 | + |
| 160 | + def __init__(self, parent=None, node_graph=None): |
| 161 | + super(NodesPaletteWidget, self).__init__(parent) |
| 162 | + self.setWindowTitle('Nodes') |
| 163 | + |
| 164 | + self._category_tabs = {} |
| 165 | + self._custom_labels = {} |
| 166 | + self._factory = node_graph.node_factory if node_graph else None |
| 167 | + |
| 168 | + self._tab_widget = QtWidgets.QTabWidget() |
| 169 | + self._tab_widget.setMovable(True) |
| 170 | + |
| 171 | + layout = QtWidgets.QVBoxLayout(self) |
| 172 | + layout.addWidget(self._tab_widget) |
| 173 | + |
| 174 | + self._build_ui() |
| 175 | + |
| 176 | + def __repr__(self): |
| 177 | + return '<{} object at {}>'.format( |
| 178 | + self.__class__.__name__, hex(id(self)) |
| 179 | + ) |
| 180 | + |
| 181 | + def _build_ui(self): |
| 182 | + """ |
| 183 | + populate the ui |
| 184 | + """ |
| 185 | + categories = set() |
| 186 | + node_types = defaultdict(list) |
| 187 | + for name, node_ids in self._factory.names.items(): |
| 188 | + for nid in node_ids: |
| 189 | + category = '.'.join(nid.split('.')[:-1]) |
| 190 | + categories.add(category) |
| 191 | + node_types[category].append((nid, name)) |
| 192 | + |
| 193 | + for category, nodes_list in node_types.items(): |
| 194 | + grid_view = self._add_category_tab(category) |
| 195 | + for node_id, node_name in nodes_list: |
| 196 | + grid_view.add_item(node_name, node_id) |
| 197 | + |
| 198 | + def _set_node_factory(self, factory): |
| 199 | + """ |
| 200 | + Set current node factory. |
| 201 | +
|
| 202 | + Args: |
| 203 | + factory (NodeFactory): node factory. |
| 204 | + """ |
| 205 | + self._factory = factory |
| 206 | + |
| 207 | + def _add_category_tab(self, category): |
| 208 | + """ |
| 209 | + Adds a new tab to the node palette widget. |
| 210 | +
|
| 211 | + Args: |
| 212 | + category (str): node identifier category eg. ``"nodes.widgets"`` |
| 213 | +
|
| 214 | + Returns: |
| 215 | + NodesGridView: nodes grid view widget. |
| 216 | + """ |
| 217 | + if category not in self._category_tabs: |
| 218 | + grid_widget = NodesGridView(self) |
| 219 | + self._tab_widget.addTab(grid_widget, category) |
| 220 | + self._category_tabs[category] = grid_widget |
| 221 | + return self._category_tabs[category] |
| 222 | + |
| 223 | + def set_category_label(self, category, label): |
| 224 | + """ |
| 225 | + Override tab label for a node category tab. |
| 226 | +
|
| 227 | + Args: |
| 228 | + category (str): node identifier category eg. ``"nodes.widgets"`` |
| 229 | + label (str): custom display label. eg. ``"Node Widgets"`` |
| 230 | + """ |
| 231 | + if label in self._custom_labels.values(): |
| 232 | + labels = {v: k for k, v in self._custom_labels.items()} |
| 233 | + raise ValueError('label "{}" already in use for "{}"' |
| 234 | + .format(label, labels[label])) |
| 235 | + previous_label = self._custom_labels.get(category, '') |
| 236 | + for idx in range(self._tab_widget.count()): |
| 237 | + tab_text = self._tab_widget.tabText(idx) |
| 238 | + if tab_text in [category, previous_label]: |
| 239 | + self._tab_widget.setTabText(idx, label) |
| 240 | + break |
| 241 | + self._custom_labels[category] = label |
| 242 | + |
| 243 | + def update(self): |
| 244 | + """ |
| 245 | + Update and refresh the node palette widget. |
| 246 | + """ |
| 247 | + self._build_tree() |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
0 commit comments