replaced dummy MDI sub with plots; added Close All to menu; added logo watermark to MDI area; use icons
This commit is contained in:
23
mainwin.py
23
mainwin.py
@ -1,17 +1,30 @@
|
||||
import numpy as np
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtWidgets import QMainWindow, QSplitter
|
||||
from PyQt5.QtGui import QIcon
|
||||
|
||||
from dictlist import DictList
|
||||
from mdi import MDIArea, MDISubWindow
|
||||
from mdi import MDIArea, MDISubPlot
|
||||
|
||||
|
||||
X = np.arange(100) / 10
|
||||
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
|
||||
def __init__(self, title="grum.py", *args, **kwargs):
|
||||
def __init__(self, title="grum", *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.setWindowTitle(title)
|
||||
|
||||
data = {"a": 10, "b": 20, "c": 30, "d": 40}
|
||||
self.setWindowIcon(QIcon("assets/icon.png"))
|
||||
|
||||
data = {
|
||||
"sine": [X, np.sin(X)],
|
||||
"cosine": [X, np.cos(X)],
|
||||
"exp": [X, np.exp(X)],
|
||||
"log": [X, np.log(X+1)]
|
||||
}
|
||||
self.lst = lst = DictList(data)
|
||||
lst.setAlternatingRowColors(True)
|
||||
lst.doubleClicked.connect(self.on_select_list_item)
|
||||
@ -32,14 +45,14 @@ class MainWindow(QMainWindow):
|
||||
key, value = self.lst.get(index)
|
||||
|
||||
#TODO: just a test for dynamic adding
|
||||
self.lst.add("x", 100)
|
||||
self.lst.add("x", [X, X])
|
||||
|
||||
for sub in self.mdi.subWindowList():
|
||||
if sub.windowTitle() == key:
|
||||
self.mdi.setActiveSubWindow(sub)
|
||||
return
|
||||
|
||||
sub = MDISubWindow(key, value)
|
||||
sub = MDISubPlot(key, value)
|
||||
self.mdi.addSubWindow(sub)
|
||||
sub.show()
|
||||
|
||||
|
43
mdi.py
43
mdi.py
@ -1,5 +1,7 @@
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtWidgets import QMdiArea, QMdiSubWindow, QAction
|
||||
from PyQt5.QtWidgets import QTextEdit
|
||||
from PyQt5.QtGui import QIcon, QPixmap, QPainter, QColor
|
||||
from pyqtgraph import PlotWidget
|
||||
|
||||
|
||||
class MDIArea(QMdiArea):
|
||||
@ -7,11 +9,14 @@ class MDIArea(QMdiArea):
|
||||
def __init__(self, bar, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
menu = bar.addMenu("View")
|
||||
menu = bar.addMenu("Windows")
|
||||
menu.addAction("Cascade")
|
||||
menu.addAction("Tiled")
|
||||
menu.addAction("Close All")
|
||||
menu.triggered[QAction].connect(self.on_menu_select)
|
||||
|
||||
self.logo = QPixmap("assets/logo.png")
|
||||
|
||||
|
||||
def on_menu_select(self, p):
|
||||
txt = p.text()
|
||||
@ -19,17 +24,45 @@ class MDIArea(QMdiArea):
|
||||
self.cascadeSubWindows()
|
||||
elif txt == "Tiled":
|
||||
self.tileSubWindows()
|
||||
elif txt == "Close All":
|
||||
self.closeAllSubWindows()
|
||||
|
||||
|
||||
def paintEvent(self, event):
|
||||
# draw the watermark in the MDI area
|
||||
painter = QPainter()
|
||||
painter.begin(self.viewport())
|
||||
|
||||
painter.fillRect(event.rect(), QColor(150, 150, 150))
|
||||
|
||||
logo = self.logo
|
||||
margin = 20
|
||||
painter.drawPixmap(
|
||||
self.width()-logo.width()-margin,
|
||||
self.height()-logo.height()-margin,
|
||||
logo.width(),
|
||||
logo.height(),
|
||||
logo
|
||||
)
|
||||
|
||||
painter.end()
|
||||
|
||||
|
||||
|
||||
class MDISubWindow(QMdiSubWindow):
|
||||
class MDISubPlot(QMdiSubWindow):
|
||||
|
||||
def __init__(self, title, data, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.setWindowTitle(title)
|
||||
|
||||
txt = QTextEdit(str(data))
|
||||
self.setWidget(txt)
|
||||
self.setWindowIcon(QIcon("assets/char.png"))
|
||||
|
||||
# without this, the SubWindow is not removed from the subWindowList
|
||||
self.setAttribute(Qt.WA_DeleteOnClose)
|
||||
|
||||
plt = PlotWidget()
|
||||
plt.plot(*data)
|
||||
self.setWidget(plt)
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user