summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaine2020-06-15 16:02:41 +0530
committerBlaine2020-06-15 16:02:41 +0530
commitf31b832f7807f1568e128d826ea45712714d125a (patch)
tree20533eab1a64ea2cc53ba14f97ae9d5c35472e32
parentca0705ddfc08affa6b63e3616e2e6783026ca349 (diff)
downloadChemical-PFD-f31b832f7807f1568e128d826ea45712714d125a.tar.gz
Chemical-PFD-f31b832f7807f1568e128d826ea45712714d125a.tar.bz2
Chemical-PFD-f31b832f7807f1568e128d826ea45712714d125a.zip
add dynamic stream table list
-rw-r--r--src/main/python/main.py2
-rw-r--r--src/main/python/shapes/line.py5
-rw-r--r--src/main/python/shapes/shapes.py1
-rw-r--r--src/main/python/utils/canvas.py20
-rw-r--r--src/main/python/utils/graphics.py5
-rw-r--r--src/main/python/utils/streamTable.py106
6 files changed, 75 insertions, 64 deletions
diff --git a/src/main/python/main.py b/src/main/python/main.py
index 1869067..42b4e48 100644
--- a/src/main/python/main.py
+++ b/src/main/python/main.py
@@ -143,7 +143,7 @@ class appWindow(QMainWindow):
def closeEvent(self, event):
#save alert on window close
if len(self.activeFiles) and not dialogs.saveEvent(self):
- event.ignore()
+ event.ignore()
else:
event.accept()
self.writeSettings()
diff --git a/src/main/python/shapes/line.py b/src/main/python/shapes/line.py
index 516f34d..bbeab30 100644
--- a/src/main/python/shapes/line.py
+++ b/src/main/python/shapes/line.py
@@ -964,7 +964,10 @@ class Line(QGraphicsPathItem):
changeArrowFlag = contextMenu.addAction(str)
action = contextMenu.exec_(event.screenPos())
if action == addLableAction:
- self.label.append(LineLabel(event.scenePos(), self))
+ newLabel = LineLabel(event.scenePos(), self)
+ self.label.append(newLabel)
+ self.scene().labelAdded.emit(newLabel)
+
if action == changeArrowFlag:
if str == "Hide Arrow":
self.arrowFlag =False
diff --git a/src/main/python/shapes/shapes.py b/src/main/python/shapes/shapes.py
index 6d06cba..b4c53c5 100644
--- a/src/main/python/shapes/shapes.py
+++ b/src/main/python/shapes/shapes.py
@@ -530,6 +530,7 @@ class NodeItem(QGraphicsSvgItem):
action = contextMenu.exec_(event.screenPos())
if action == addLabelAction:
self.label = ItemLabel(event.scenePos(), self)
+ self.scene().labelAdded.emit(self.label)
def __getstate__(self):
return {
diff --git a/src/main/python/utils/canvas.py b/src/main/python/utils/canvas.py
index 4d1fda4..f8d71d6 100644
--- a/src/main/python/utils/canvas.py
+++ b/src/main/python/utils/canvas.py
@@ -30,7 +30,8 @@ class canvas(customView):
# when we will draw items on this, this might be changed if QGraphicScene is subclassed.
#set layout and background color
- self.painter = customScene()
+ self.painter = customScene()
+ self.painter.labelAdded.connect(self.updateStreamTable)
self.painter.setBackgroundBrush(QBrush(Qt.white)) #set white background
self.setScene(self.painter)
@@ -41,17 +42,19 @@ class canvas(customView):
self.customContextMenuRequested.connect(self.sideViewContextMenu)
def addStreamTable(self, pos=QPointF(0, 0), table=None):
- self.streamTable = table if table else streamTable(5, 5, canvas=self)
+ self.streamTable = table if table else streamTable(self.labelItems, canvas=self)
- self.streamTableRect = moveRect(-10, -10, 10, 10)
+ self.streamTableRect = moveRect()
self.streamTableRect.setFlags(moveRect.ItemIsMovable |
moveRect.ItemIsSelectable)
self.streamTableProxy = QGraphicsProxyWidget(self.streamTableRect)
self.streamTableProxy.setWidget(self.streamTable)
self.painter.addItem(self.streamTableRect)
self.streamTableRect.setPos(pos)
- # proxy = self.painter.addWidget(self.streamTable)
- # proxy.setPos(pos)
+
+ def updateStreamTable(self, item):
+ if self.streamTable:
+ self.streamTable.model.insertRow(name=item.toPlainText())
def sideViewContextMenu(self, pos):
self.parentFileWindow.sideViewContextMenu(self.mapTo(self.parentFileWindow, pos))
@@ -94,7 +97,12 @@ class canvas(customView):
# generator to filter out certain items
for i in self.painter.items():
yield i
-
+
+ @property
+ def labelItems(self):
+ for i in self.items:
+ if isinstance(i, (shapes.ItemLabel, shapes.LineLabel)):
+ yield i
@property
def canvasSize(self):
return self._canvasSize
diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py
index c799217..3e1d193 100644
--- a/src/main/python/utils/graphics.py
+++ b/src/main/python/utils/graphics.py
@@ -1,4 +1,4 @@
-from PyQt5.QtCore import Qt, QPointF
+from PyQt5.QtCore import Qt, QPointF, pyqtSignal
from PyQt5.QtGui import QPen, QKeySequence
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QGraphicsItem, QUndoStack, QAction, QUndoView
@@ -11,6 +11,7 @@ class customView(QGraphicsView):
"""
Defines custom QGraphicsView with zoom features and drag-drop accept event, overriding wheel event
"""
+
def __init__(self, scene = None, parent=None):
if scene is not None: #overloaded constructor
super(customView, self).__init__(scene, parent)
@@ -83,6 +84,8 @@ class customScene(QGraphicsScene):
"""
Extends QGraphicsScene with undo-redo functionality
"""
+ labelAdded = pyqtSignal(shapes.QGraphicsItem)
+
def __init__(self, *args, parent=None):
super(customScene, self).__init__(*args, parent=parent)
diff --git a/src/main/python/utils/streamTable.py b/src/main/python/utils/streamTable.py
index b8b492d..b7dffa9 100644
--- a/src/main/python/utils/streamTable.py
+++ b/src/main/python/utils/streamTable.py
@@ -1,22 +1,21 @@
-from PyQt5.QtCore import Qt, QSize, QRectF, QPoint, QAbstractTableModel, pyqtSignal
+from PyQt5.QtCore import Qt, QSize, QRect, QPoint, QAbstractTableModel, pyqtSignal, QModelIndex
from PyQt5.QtGui import QBrush, QPen, QColor
from PyQt5.QtWidgets import QTableView, QMenu, QGraphicsRectItem, QInputDialog, QStyledItemDelegate
from collections import defaultdict
class streamTableModel(QAbstractTableModel):
- layoutAboutToBeChanged = pyqtSignal()
- layoutChanged = pyqtSignal()
+ updateEvent = pyqtSignal()
def __init__(self, parent, list, header, *args):
super(streamTableModel, self).__init__(parent, *args)
self.list = list
self.header = header
- def rowCount(self, parent):
+ def rowCount(self, parent=None):
return len(self.list)
- def columnCount(self, parent):
+ def columnCount(self, parent=None):
return len(self.list[0])
def data(self, index, role):
@@ -26,6 +25,29 @@ class streamTableModel(QAbstractTableModel):
return None
return self.list[index.row()][index.column()]
+ def setData(self, index, value, role):
+ if not index.isValid():
+ return False
+ elif role != Qt.EditRole:
+ return False
+ self.list[index.row()][index.column()] = value
+ return True
+
+ def insertColumn(self, int):
+ self.beginInsertColumns(QModelIndex(), int, int)
+ for item in self.list:
+ item.insert(int, 0)
+ self.header.insert(int, "newVal")
+ self.endInsertColumns()
+ self.updateEvent.emit()
+
+ def insertRow(self, int=None, name="Name"):
+ int = int if int else self.rowCount()+1
+ self.beginInsertRows(QModelIndex(), int, int)
+ self.list.insert(int, [name] + [0 for _ in range(self.columnCount()-1)])
+ self.endInsertRows()
+ self.updateEvent.emit()
+
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
@@ -33,39 +55,34 @@ class streamTableModel(QAbstractTableModel):
def flags(self, index):
return (super(streamTableModel, self).flags(index) | Qt.ItemIsEditable)
- # def sort(self, col, order):
- # """sort table by given column number col"""
- # self.layoutAboutToBeChanged.emit()
- # self.list = sorted(self.list, lambda x: x[col])
- # if order == Qt.DescendingOrder:
- # self.list.reverse()
- # self.layoutChanged.emit()
-#
class streamTable(QTableView):
- def __init__(self, int, int_, canvas=None, parent=None):
+ def __init__(self, itemLabels=[], canvas=None, parent=None):
super(streamTable, self).__init__(parent=parent)
self.canvas = canvas
+ self.items = itemLabels
list = []
- for i in range(int):
- list.append([f'name {i+1}']+[0 for _ in range(int_ - 1)])
+ for i, item in enumerate(itemLabels):
+ list.append([item.toPlainText()]+[0 for _ in range(5)])
header = ["name", "val1", "val2", "val3", "val4", "val5"]
self.model = streamTableModel(self, list, header)
+ self.setShowGrid(False)
+ self.verticalHeader().hide()
self.setModel(self.model)
- # self.setSortingEnabled(True)
self.borderThickness = defaultdict(lambda: 1)
-
+ self.model.updateEvent.connect(self.refresh)
+
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
point = event.pos()
- col = self.getCol(point.x())
- row = self.getRow(point.y())
+ # col = self.getCol(point.x())
+ index = self.indexAt(point)
menu = QMenu("Context Menu", self)
- menu.addAction("Change bottom border thickness", lambda x=row: self.changeRowBorder(x))
- menu.addAction("Change right border thickness", lambda x=col: self.changeColBorder(x))
- menu.addAction("Insert Column to right", lambda x=col: self.insertColRight(col))
- menu.addAction("Insert Row to bottom", lambda x=row: self.insertRowBottom(row))
+ menu.addAction("Change bottom border thickness", lambda x=index.row(): self.changeRowBorder(x))
+ menu.addAction("Change right border thickness", lambda x=index.column(): self.changeColBorder(x))
+ menu.addAction("Insert Column to right", lambda x=index.column(): self.insertColRight(x))
+ menu.addAction("Insert Row to bottom", lambda x=index.row(): self.insertRowBottom(x))
menu.exec_(self.mapToGlobal(point)+ QPoint(20, 25))
event.accept()
return super(streamTable, self).mousePressEvent(event)
@@ -79,51 +96,30 @@ class streamTable(QTableView):
newWidth, bool = QInputDialog.getInt(self, "Change Vertical Border Width", "Enter new Width in pixels", self.borderThickness[-col], 0, 10, step=1)
if bool:
self.setItemDelegateForColumn(col, drawBorderDelegate(self, newWidth, False))
-
- def getRow(self, point):
- h = self.horizontalHeader().height()
- i = -1
- while (h < point):
- h += self.rowHeight(i)
- i += 1
- return i
-
- def getCol(self, point):
- w = self.verticalHeader().width()
- i = -1
- while (w < point):
- w += self.columnWidth(i)
- i += 1
- return i
def insertRowBottom(self, row):
- self.insertRow(row + 1)
- self.resize(self.sizeHint())
+ self.model.insertRow(row + 1)
def insertColRight(self, col):
- self.insertColumn(col + 1)
- self.resize(self.sizeHint())
+ self.model.insertColumn(col + 1)
+
+ def refresh(self):
+ self.resizeHandler()
def resizeHandler(self):
self.resize(self.sizeHint())
def sizeHint(self):
- w = self.verticalHeader().width() + 4
- for i in range(self.model.columnCount(self)):
- w += self.columnWidth(i)
- h = self.horizontalHeader().height() + 4
- for i in range(self.model.rowCount(self)):
- h += self.rowHeight(i)
- return QSize(w, h)
+ return self.rect().size()
def rect(self):
w = self.verticalHeader().width() + 4
- for i in range(self.model.columnCount(self)):
+ for i in range(self.model.columnCount()):
w += self.columnWidth(i)
h = self.horizontalHeader().height() + 4
- for i in range(self.model.rowCount(self)):
+ for i in range(self.model.rowCount()):
h += self.rowHeight(i)
- return QRectF(0, 0, w, h)
+ return QRect(0, 0, w, h)
def cellEntered(self, row, column):
print(row, column)
@@ -146,5 +142,5 @@ class drawBorderDelegate(QStyledItemDelegate):
class moveRect(QGraphicsRectItem):
def __init__(self, *args):
- super(moveRect, self).__init__(*args)
+ super(moveRect, self).__init__(-10, -10, 10, 10)
self.setBrush(QBrush(QColor(0, 0, 0, 120))) \ No newline at end of file