diff options
author | Blaine | 2020-05-16 18:24:07 +0530 |
---|---|---|
committer | Blaine | 2020-05-16 18:24:07 +0530 |
commit | 051af95251fd0f87457322d9bd80ca99309c6c56 (patch) | |
tree | 49c2e4d019dba9c398628f1107baff8d4623a5fe /src | |
parent | 7df3f6ab2aadbc178173a26a299c9a809aaa35f4 (diff) | |
download | Chemical-PFD-051af95251fd0f87457322d9bd80ca99309c6c56.tar.gz Chemical-PFD-051af95251fd0f87457322d9bd80ca99309c6c56.tar.bz2 Chemical-PFD-051af95251fd0f87457322d9bd80ca99309c6c56.zip |
minor clean up and comments
Diffstat (limited to 'src')
-rw-r--r-- | src/main/python/main.py | 4 | ||||
-rw-r--r-- | src/main/python/utils/fileWindow.py | 3 | ||||
-rw-r--r-- | src/main/python/utils/graphics.py | 32 | ||||
-rw-r--r-- | src/main/python/utils/toolbar.py | 105 |
4 files changed, 74 insertions, 70 deletions
diff --git a/src/main/python/main.py b/src/main/python/main.py index ef52784..7d72bae 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -144,9 +144,7 @@ class appWindow(QMainWindow): @property def activeFiles(self): - for i in self.mdi.subWindowList(): - if i.tabCount: - yield i + return [i for i in self.mdi.subWindowList() if i.tabCount] @property def count(self): diff --git a/src/main/python/utils/fileWindow.py b/src/main/python/utils/fileWindow.py index 4bd4ceb..e549568 100644 --- a/src/main/python/utils/fileWindow.py +++ b/src/main/python/utils/fileWindow.py @@ -187,8 +187,7 @@ class fileWindow(QMdiSubWindow): @property def tabList(self): #returns a list of tabs in the given window - for i in range(self.tabCount): - yield self.tabber.widget(i) + return [self.tabber.widget(i) for i in range(self.tabCount)] @property def tabCount(self): diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index dce664a..0fe0030 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -5,7 +5,7 @@ from PyQt5 import QtWidgets class customView(QGraphicsView): """ - Defines custom QGraphicsView with zoom features, overriding wheel event + 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 @@ -13,22 +13,28 @@ class customView(QGraphicsView): else: super(customView, self).__init__(parent) self._zoom = 1 - self.setDragMode(True) - self.setAcceptDrops(True) + self.setDragMode(True) #sets pannable using mouse + self.setAcceptDrops(True) #sets ability to accept drops + #following four functions are required to be overridden for drag-drop functionality def dragEnterEvent(self, QDragEnterEvent): + #defines acceptable drop items if QDragEnterEvent.mimeData().hasText(): QDragEnterEvent.acceptProposedAction() def dragMoveEvent(self, QDragMoveEvent): + #defines acceptable drop items if QDragMoveEvent.mimeData().hasText(): QDragMoveEvent.acceptProposedAction() def dragLeaveEvent(self, QDragLeaveEvent): + #accept any drag leave event, avoid unnecessary logging QDragLeaveEvent.accept() def dropEvent(self, QDropEvent): + #defines item drop, fetches text, creates corresponding QGraphicItem and adds it to scene if QDropEvent.mimeData().hasText(): + #QDropEvent.mimeData().text() defines intended drop item, the pos values define position graphic = getattr(QtWidgets, QDropEvent.mimeData().text())(QDropEvent.pos().x()-150, QDropEvent.pos().y()-150, 300, 300) graphic.setPen(QPen(Qt.black, 2)) graphic.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable) @@ -63,19 +69,9 @@ class customView(QGraphicsView): self.scale(self.zoom / temp, self.zoom / temp) class customScene(QGraphicsScene): - def dragEnterEvent(self, e): - e.acceptProposedAction() - - def dropEvent(self, e): - # find item at these coordinates - item = self.itemAt(e.scenePos()) - if item.setAcceptDrops == True: - # pass on event to item at the coordinates - try: - item.dropEvent(e) - except RuntimeError: - pass #This will supress a Runtime Error generated when dropping into a widget with no MyProxy - - def dragMoveEvent(self, e): - e.acceptProposedAction() + """ + re-implement QGraphicsScene for future functionality + hint: QUndoFramework + """ + pass
\ No newline at end of file diff --git a/src/main/python/utils/toolbar.py b/src/main/python/utils/toolbar.py index 6f276df..ec3b6a8 100644 --- a/src/main/python/utils/toolbar.py +++ b/src/main/python/utils/toolbar.py @@ -9,78 +9,80 @@ from .data import toolbarItems from .funcs import grouper from .layout import flowLayout - -resourceManager = ApplicationContext() +resourceManager = ApplicationContext() #Used to load images, mainly toolbar icons class toolbar(QDockWidget): - toolbuttonClicked = pyqtSignal(dict) + """ + Defines the right side toolbar, using QDockWidget. + """ + toolbuttonClicked = pyqtSignal(dict) #signal for any object button pressed def __init__(self, parent = None): super(toolbar, self).__init__(parent) - self.toolbarButtonDict = dict() - self.toolbarItems(toolbarItems.keys()) + self.toolbarButtonDict = dict() #initializes empty dict to store toolbar buttons + self.toolbarItems(toolbarItems.keys()) #creates all necessary buttons self.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable) + #mainly used to disable closeability of QDockWidget + #declare main widget and layout self.widget = QWidget(self) self.layout = QBoxLayout(QBoxLayout.TopToBottom, self.widget) self.setAllowedAreas(Qt.AllDockWidgetAreas) - self.searchBox = QLineEdit(self.widget) + self.searchBox = QLineEdit(self.widget) #search box to search through componenets + #connect signal to filter slot, add searchbar to toolbar self.searchBox.textChanged.connect(self.searchQuery) self.layout.addWidget(self.searchBox, alignment=Qt.AlignHCenter) + #create a scrollable area to house all buttons self.diagArea = QScrollArea(self) self.layout.addWidget(self.diagArea) - self.diagAreaWidget = QWidget() + self.diagAreaWidget = QWidget(self.diagArea) #inner widget for scroll area + #custom layout for inner widget self.diagAreaLayout = flowLayout(self.diagAreaWidget) - - self.setWidget(self.widget) + + # self.diagArea.setWidget() #set inner widget to scroll area + self.setWidget(self.widget) #set main widget to dockwidget def clearLayout(self): + # used to clear all items from toolbar, by parenting it to the toolbar instead + # this works because changing parents moves widgets to be the child of the new + # parent, setting it to none, would have qt delete them to free memory for i in reversed(range(self.diagAreaLayout.count())): + # since changing parent would effect indexing, its important to go in reverse self.diagAreaLayout.itemAt(i).widget().setParent(self) def populateToolbar(self, list): - self.clearLayout() + #called everytime the button box needs to be updated(incase of a filter) + self.clearLayout() #clears layout for item in list: self.diagAreaLayout.addWidget(self.toolbarButtonDict[item]) - self.diagArea.setWidget(self.diagAreaWidget) def searchQuery(self): # shorten toolbaritems list with search items # self.populateToolbar() # populate with toolbar items - text = self.searchBox.text() + text = self.searchBox.text() #get text if text == '': - self.populateToolbar(self.toolbarItemList) + self.populateToolbar(self.toolbarItemList) # restore everything on empty string else: + # use regex to search filter through button list and add the remainder to toolbar self.populateToolbar(filter(lambda x: search(text, x, IGNORECASE), self.toolbarItemList)) def resize(self): - parent = self.parentWidget() - if parent.dockWidgetArea in [Qt.TopDockWidgetArea, Qt.BottomDockWidgetArea] and not self.isFloating(): - self.layout.setDirection(QBoxLayout.LeftToRight) - self.setFixedHeight(.12*parent.height()) - self.setFixedWidth(parent.width()) - self.diagAreaWidget.setFixedHeight(.12*parent.height() - 45) - else: - self.layout.setDirection(QBoxLayout.TopToBottom) - self.setFixedWidth(.12*parent.width()) - self.setFixedHeight(self.height()) - self.diagAreaWidget.setFixedWidth(.12*parent.width() - 45) - - def resizeEvent(self, event): - parent = self.parentWidget() - self.layout.setDirection(QBoxLayout.TopToBottom) - self.setFixedWidth(.12*parent.width()) - self.setFixedHeight(self.height()) - width = .12*parent.width() - 45 - self.diagAreaWidget.setFixedWidth(width) + # called when main window resizes, overloading resizeEvent caused issues. + parent = self.parentWidget() #used to get parent dimensions + self.layout.setDirection(QBoxLayout.TopToBottom) # here so that a horizontal toolbar can be implemented later + self.setFixedWidth(.12*parent.width()) #12% of parent width + self.setFixedHeight(self.height()) #span available height + width = .12*parent.width() #12% of parent width + self.diagAreaWidget.setFixedWidth(width) #set inner widget width + # the following line, sets the required height for the current width, so that blank space doesnt occur self.diagAreaWidget.setFixedHeight(self.diagAreaLayout.heightForWidth(width)) - return super(toolbar, self).resizeEvent(event) def toolbarItems(self, items): + #helper functions to create required buttons for item in items: obj = toolbarItems[item] button = toolbarButton(self, obj) @@ -89,39 +91,48 @@ class toolbar(QDockWidget): @property def toolbarItemList(self): + #generator to iterate over all buttons for i in self.toolbarButtonDict.keys(): yield i class toolbarButton(QToolButton): - + """ + Custom buttons for components that implements drag and drop functionality + item -> dict from toolbarItems dict, had 4 properties, name, object, icon and default arguments. + """ def __init__(self, parent = None, item = None): super(toolbarButton, self).__init__(parent) + #uses fbs resource manager to get icons self.setIcon(QIcon(resourceManager.get_resource(f'toolbar/{item["icon"]}'))) - self.setIconSize(QSize(40, 40)) - self.dragStartPosition = 0 - self.itemObject = item['object'] - self.setText(item["name"]) - self.setToolTip(item["name"]) + self.setIconSize(QSize(40, 40)) #unecessary but left for future references + self.dragStartPosition = None #intialize value for drag event + self.itemObject = item['object'] #refer current item object, to handle drag mime + self.setText(item["name"]) #button text + self.setToolTip(item["name"]) #button tooltip def mousePressEvent(self, event): + #check if button was pressed or there was a drag intent super(toolbarButton, self).mousePressEvent(event) if event.button() == Qt.LeftButton: - self.dragStartPosition = event.pos() + self.dragStartPosition = event.pos() #set dragstart position def mouseMoveEvent(self, event): + #handles drag if not (event.buttons() and Qt.LeftButton): - return + return #ignore if left click is not held if (event.pos() - self.dragStartPosition).manhattanLength() < QApplication.startDragDistance(): - return + return #check if mouse was dragged enough, manhattan length is a rough and quick method in qt - drag = QDrag(self) - mimeData = QMimeData() - mimeData.setText(self.itemObject) - drag.setMimeData(mimeData) - drag.exec(Qt.CopyAction) + drag = QDrag(self) #create drag object + mimeData = QMimeData() #create drag mime + mimeData.setText(self.itemObject) # set mime value for view to accept + drag.setMimeData(mimeData) # attach mime to drag + drag.exec(Qt.CopyAction) #execute drag def sizeHint(self): + #defines button size return self.minimumSizeHint() def minimumSizeHint(self): + #defines button size return QSize(30, 30)
\ No newline at end of file |