Skip to content

Commit

Permalink
Refactor actions and prepare support for action dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
don4get committed Aug 18, 2020
1 parent c848893 commit 97d4557
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 104 deletions.
159 changes: 94 additions & 65 deletions nodedge/editor_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import logging
import os
from typing import Optional, cast
from typing import Callable, Optional, Union, cast

from PySide2.QtCore import QPoint, QSettings, QSize, Qt
from PySide2.QtGui import QClipboard, QCloseEvent, QGuiApplication, QKeySequence
Expand Down Expand Up @@ -140,70 +140,64 @@ def createActions(self) -> None:
"""
Create basic `File` and `Edit` actions.
"""
self.newAct: QAction = QAction("&New", self)
self.newAct.setShortcut(QKeySequence("Ctrl+N"))
self.newAct.setStatusTip("Create new Nodedge graph")
self.newAct.triggered.connect(self.newFile)

self.openAct = QAction("&Open", self)
self.openAct.setShortcut(QKeySequence("Ctrl+O"))
self.openAct.setStatusTip("Open file")
self.openAct.triggered.connect(self.openFile)

self.saveAct = QAction("&Save", self)
self.saveAct.setShortcut(QKeySequence("Ctrl+S"))
self.saveAct.setStatusTip("Save file")
self.saveAct.triggered.connect(self.saveFile)

self.saveAsAct = QAction("Save &As", self)
self.saveAsAct.setShortcut(QKeySequence("Ctrl+Shift+S"))
self.saveAsAct.setStatusTip("Save file as...")
self.saveAsAct.triggered.connect(self.saveFileAs)

self.quitAct = QAction("&Quit", self)
self.quitAct.setShortcut(QKeySequence("Ctrl+Q"))
self.quitAct.setStatusTip("Exit application")
self.quitAct.triggered.connect(self.quit)

self.undoAct = QAction("&Undo", self)
self.undoAct.setShortcut(QKeySequence("Ctrl+Z"))
self.undoAct.setStatusTip("Undo last operation")
self.undoAct.triggered.connect(self.undo)

self.redoAct = QAction("&Redo", self)
self.redoAct.setShortcut(QKeySequence("Ctrl+Shift+Z"))
self.redoAct.setStatusTip("Redo last operation")
self.redoAct.triggered.connect(self.redo)

self.cutAct = QAction("C&ut", self)
self.cutAct.setShortcut(QKeySequence("Ctrl+X"))
self.cutAct.setStatusTip("Cut selected items")
self.cutAct.triggered.connect(self.cut)

self.copyAct = QAction("&Copy", self)
self.copyAct.setShortcut(QKeySequence("Ctrl+C"))
self.copyAct.setStatusTip("Copy selected items")
self.copyAct.triggered.connect(self.copy)

self.pasteAct = QAction("&Paste", self)
self.pasteAct.setShortcut(QKeySequence.Paste)
self.pasteAct.setStatusTip("Paste selected items")
self.pasteAct.triggered.connect(self.paste)

self.deleteAct = QAction("&Delete", self)
self.deleteAct.setShortcut(QKeySequence("Del"))
self.deleteAct.setStatusTip("Delete selected items")
self.deleteAct.triggered.connect(self.delete)

self.fitInViewAct = QAction("Fit in view", self)
self.fitInViewAct.setShortcut(QKeySequence(Qt.Key_Space))
self.fitInViewAct.setStatusTip("Fit content in view")
self.fitInViewAct.triggered.connect(self.onFitInView)

self.generateCodeAct = QAction("Generate code", self)
self.generateCodeAct.setShortcut(QKeySequence("Ctrl+G"))
self.generateCodeAct.setStatusTip("Generate python code")
self.generateCodeAct.triggered.connect(self.onGenerateCode)

self.newAct = self.createAction(
"&New", self.newFile, "Create new Nodedge graph", QKeySequence("Ctrl+N")
)

self.openAct = self.createAction(
"&Open", self.openFile, "Open file", QKeySequence("Ctrl+O")
)

self.saveAct = self.createAction(
"&Save", self.saveFile, "Save file", QKeySequence("Ctrl+S")
)

self.saveAsAct = self.createAction(
"Save &As", self.saveFileAs, "Save file as...", QKeySequence("Ctrl+Shift+S")
)

self.quitAct = self.createAction(
"&Quit", self.quit, "Exit application", QKeySequence("Ctrl+Q")
)

self.undoAct = self.createAction(
"&Undo", self.undo, "Undo last operation", QKeySequence("Ctrl+Z")
)

self.redoAct = self.createAction(
"&Redo", self.redo, "Redo last operation", QKeySequence("Ctrl+Shift+Z")
)

self.cutAct = self.createAction(
"C&ut", self.cut, "Cut selected items", QKeySequence("Ctrl+X")
)

self.copyAct = self.createAction(
"&Copy", self.copy, "Copy selected items", QKeySequence("Ctrl+C")
)

self.pasteAct = self.createAction(
"&Paste", self.paste, "Paste selected items", QKeySequence.Paste
)

self.deleteAct = self.createAction(
"&Delete", self.delete, "Delete selected items", QKeySequence("Del")
)

self.fitInViewAct = self.createAction(
"Fit in view",
self.onFitInView,
"Fit content in view",
QKeySequence(Qt.Key_Space),
)

self.generateCodeAct = self.createAction(
"Generate code",
self.onGenerateCode,
"Generate python code",
QKeySequence("Ctrl+G"),
)

# noinspection PyArgumentList, PyAttributeOutsideInit, DuplicatedCode
def createMenus(self) -> None:
Expand Down Expand Up @@ -551,3 +545,38 @@ def onFitInView(self):
def onGenerateCode(self):
if self.currentEditorWidget is not None:
self.currentEditorWidget.graphicsView.graphicsScene.fitInView()

def createAction(
self,
name: str,
callback: Callable,
statusTip: Optional[str] = None,
shortcut: Union[None, str, QKeySequence] = None,
) -> QAction:
"""
Create an action for this window and add it to actions list.
:param name: action's name
:type name: ``str``
:param callback: function to be called when the action is triggered
:type callback: ``Callable``
:param statusTip: Description of the action displayed
at the bottom left of the :class:`~nodedge.editor_window.EditorWindow`.
:type statusTip: Optional[``str``]
:param shortcut: Keyboard shortcut to trigger the action.
:type shortcut: ``Optional[str]``
:return:
"""
act = QAction(name, self)
act.triggered.connect(callback)

if statusTip is not None:
act.setStatusTip(statusTip)
act.setToolTip(statusTip)

if shortcut is not None:
act.setShortcut(QKeySequence(shortcut))

self.addAction(act)

return act
83 changes: 52 additions & 31 deletions nodedge/mdi_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from PySide2.QtGui import QCloseEvent, QIcon, QKeySequence
from PySide2.QtWidgets import (
QAction,
QDialog,
QDockWidget,
QFileDialog,
QMdiArea,
Expand Down Expand Up @@ -142,52 +143,67 @@ def createActions(self) -> None:
"""
super().createActions()

self.closeAct = QAction("Cl&ose", self)
self.closeAct.setStatusTip("Close the active window")
self.closeAct.triggered.connect(self.mdiArea.closeActiveSubWindow)
self.closeAct = self.createAction(
"Cl&ose", self.mdiArea.closeActiveSubWindow, "Close the active window"
)

self.closeAllAct = QAction("Close &All", self)
self.closeAllAct.setStatusTip("Close all the windows")
self.closeAllAct.triggered.connect(self.mdiArea.closeAllSubWindows)
self.closeAllAct = self.createAction(
"Close &All", self.mdiArea.closeAllSubWindows, "Close all the windows"
)

self.tileAct = QAction("&Tile", self)
self.tileAct.setStatusTip("Tile the windows")
self.tileAct.triggered.connect(self.mdiArea.tileSubWindows)
self.tileAct = self.createAction(
"&Tile", self.mdiArea.tileSubWindows, "Tile the windows"
)

self.cascadeAct = QAction("&Cascade", self)
self.cascadeAct.setStatusTip("Cascade the windows")
self.cascadeAct.triggered.connect(self.mdiArea.cascadeSubWindows)
self.cascadeAct = self.createAction(
"&Cascade", self.mdiArea.cascadeSubWindows, "Cascade the windows"
)

self.nextAct = QAction("Ne&xt", self)
self.nextAct.setShortcut(QKeySequence.NextChild)
self.nextAct.setStatusTip("Move the focus to the next window")
self.nextAct.triggered.connect(self.mdiArea.activateNextSubWindow)
self.nextAct = self.createAction(
"Ne&xt",
self.mdiArea.activateNextSubWindow,
"Move the focus to the next window",
QKeySequence.NextChild,
)

# noinspection SpellCheckingInspection
self.previousAct = QAction("Pre&vious", self)
self.previousAct.setShortcut(QKeySequence.PreviousChild)
self.previousAct.setStatusTip("Move the focus to the previous window")
self.previousAct.triggered.connect(self.mdiArea.activatePreviousSubWindow)
self.previousAct = self.createAction(
"Pre&vious",
self.mdiArea.activatePreviousSubWindow,
"Move the focus to the previous window",
QKeySequence.PreviousChild,
)

self.nodeToolbarAct = QAction("&Node toolbar", self)
self.nodeToolbarAct.setShortcut(QKeySequence("ctrl+alt+n"))
self.nodeToolbarAct.setStatusTip("Enable/Disable the node toolbar")
self.nodeToolbarAct.triggered.connect(self.onNodesToolbarTriggered)
self.nodeToolbarAct = self.createAction(
"&Node toolbar",
self.onNodesToolbarTriggered,
"Enable/Disable the node toolbar",
QKeySequence("ctrl+alt+n"),
)

self.nodeToolbarAct.setCheckable(True)
self.nodeToolbarAct.setChecked(True) # self.nodesDock.isVisible()

self.separatorAct = QAction(self)
self.separatorAct.setSeparator(True)

self.aboutAct: QAction = QAction("&About", self)
self.aboutAct.setStatusTip("Show the application's About box")
self.aboutAct.triggered.connect(self.about)
self.aboutAct = self.createAction(
"&About", self.about, "Show the application's About box"
)

self.debugAct = self.createAction(
"&Debug",
self.onDebugSwitched,
"Enable/Disable the debug mode",
QKeySequence("ctrl+alt+shift+d"),
)

self.debugAct = QAction("&Debug", self)
self.debugAct.setShortcut(QKeySequence("ctrl+alt+shift+d"))
self.debugAct.setStatusTip("Enable/Disable the debug mode")
self.debugAct.triggered.connect(self.onDebugSwitched)
self.showDialogActionsAct = self.createAction(
"Show dialog actions",
self.onShowDialogActions,
"Show available actions",
QKeySequence("ctrl+shift+a"),
)

# noinspection PyAttributeOutsideInit
def createToolBars(self) -> None:
Expand Down Expand Up @@ -640,3 +656,8 @@ def showItemsInStatusBar(self, items: List[str]):
def onDebugSwitched(self):
"""Event called when the debug action is triggered."""
pass

def onShowDialogActions(self):
self.__logger.info("")
dialog = QDialog()
dialog.show()
19 changes: 12 additions & 7 deletions nodedge/qss/nodedge_style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,18 @@ GraphicsNodeWidget{
background:palette(mid);
}

QToolBar::separator{
border-right: 1px solid palette(light);
padding:3px;
width: 1px;
height: 30px;
QToolBar::separator {
border-right: 1px solid palette(light);
padding: 3px;
width: 1px;
height: 30px;
}

SceneItemsTableWidget{
min-width: 300px;
SceneItemsTableWidget {
min-width: 300px;
}

QToolTip {
background: palette(light);
color: palette(text);
}
2 changes: 1 addition & 1 deletion requirements_docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ recommonmark
sphinx
sphinx-autodoc-typehints
sphinx-autodoc-types
-e git+https://github.com/readthedocs/sphinx_rtd_theme#egg=sphinx-rtd-theme
sphinx-rtd-theme

0 comments on commit 97d4557

Please sign in to comment.