pylint and autopep8 all python files

This commit is contained in:
Allen Winter
2022-07-08 17:34:42 -04:00
parent 4afc6b62b2
commit 6e5cfd70d1
17 changed files with 258 additions and 187 deletions

View File

@@ -9,6 +9,8 @@
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#
# pylint: disable=missing-module-docstring,missing-function-docstring,missing-class-docstring
from PyKDDockWidgetsQt6 import KDDockWidgets
from PySide6 import QtCore, QtWidgets, QtGui
@@ -17,33 +19,41 @@ from MyWidget1 import MyWidget1
from MyWidget2 import MyWidget2
from MyWidget3 import MyWidget3
def newMyWidget(parent = None):
def newMyWidget(parent=None):
randomNumber = QtCore.QRandomGenerator.global_().bounded(0, 100) + 1
if (randomNumber < 50):
if (randomNumber < 33):
if randomNumber < 50:
if randomNumber < 33:
return MyWidget1(parent)
else:
return MyWidget3(parent)
else:
return MyWidget2(parent)
return MyWidget3(parent)
return MyWidget2(parent)
class MyMainWindow(KDDockWidgets.MainWindow):
s_count = 0
s_menuCount = 0
def __init__(self, uniqueName, options = KDDockWidgets.MainWindowOption_None, dockWidget0IsNonClosable = False, nonDockableDockWidget9 = False, restoreIsRelative = False, maxSizeForDockWidget8 = False, affinityName = "", parent = None):
def __init__(self,
uniqueName,
options=KDDockWidgets.MainWindowOption_None,
dockWidget0IsNonClosable=False,
nonDockableDockWidget9=False,
restoreIsRelative=False,
maxSizeForDockWidget8=False,
affinityName="",
parent=None):
super().__init__(uniqueName, options, parent)
self.m_dockWidget0IsNonClosable = dockWidget0IsNonClosable
self.m_dockWidget9IsNonDockable = nonDockableDockWidget9
self.m_restoreIsRelative = restoreIsRelative
self.m_maxSizeForDockWidget8 = maxSizeForDockWidget8
self.m_dockwidgets = []
self.dockWidget0IsNonClosable = dockWidget0IsNonClosable
self.dockWidget9IsNonDockable = nonDockableDockWidget9
self.restoreIsRelative = restoreIsRelative
self.maxSizeForDockWidget8 = maxSizeForDockWidget8
self.dockwidgets = []
menubar = self.menuBar()
fileMenu = QtWidgets.QMenu("File")
self.m_toggleMenu = QtWidgets.QMenu("Toggle")
self.toggleMenu = QtWidgets.QMenu("Toggle")
menubar.addMenu(fileMenu)
menubar.addMenu(self.m_toggleMenu)
menubar.addMenu(self.toggleMenu)
newAction = fileMenu.addAction("New DockWidget")
newAction.triggered.connect(self._newDockWidget)
@@ -63,101 +73,104 @@ class MyMainWindow(KDDockWidgets.MainWindow):
quitAction = fileMenu.addAction("Quit")
quitAction.triggered.connect(QtWidgets.QApplication.instance().quit)
self.setAffinities([ affinityName ])
self.setAffinities([affinityName])
self.createDockWidgets()
def _newDockWidget(self):
MyMainWindow.s_menuCount += 1
w = newMyWidget(self)
w.setGeometry(100, 100, 400, 400)
dock = KDDockWidgets.DockWidget("new dock %d"%(MyMainWindow.s_menuCount))
dock = KDDockWidgets.DockWidget("new dock %d" % (MyMainWindow.s_menuCount))
dock.setWidget(w)
dock.resize(600, 600)
dock.show()
self.m_dockwidgets.append(dock)
self.dockwidgets.append(dock)
# pylint: disable=no-self-use
def _saveLayout(self):
#saver = KDDockWidgets.LayoutSaver()
#result = saver.saveToFile("mylayout.json")
#print("Saving layout to disk. Result=", result)
print("Not available")
# pylint: disable=no-self-use
def _restoreLayout(self):
#options = KDDockWidgets.RestoreOption_None
#if self.m_restoreIsRelative:
# if self.restoreIsRelative:
# options |= KDDockWidgets.RestoreOption_RelativeToMainWindow
#saver = KDDockWidgets.LayoutSaver(options)
#saver.restoreFromFile("mylayout.json")
# saver.restoreFromFile("mylayout.json")
print("Not available")
def _closeAll(self):
for dw in self.m_dockwidgets:
dw.close()
for widget in self.dockwidgets:
widget.close()
def createDockWidgets(self):
if self.m_dockWidget9IsNonDockable:
if self.dockWidget9IsNonDockable:
numDockWidgets = 10
else:
numDockWidgets = 9
# numDockWidgets = 2
# Create 9 KDDockWidget::DockWidget and the respective widgets they're hosting (MyWidget instances)
for i in range(numDockWidgets):
self.m_dockwidgets.append(self.newDockWidget())
for _ in range(numDockWidgets):
self.dockwidgets.append(self.newDockWidget())
# MainWindow::addDockWidget() attaches a dock widget to the main window:
initialOpts = KDDockWidgets.InitialOption(KDDockWidgets.InitialVisibilityOption.StartHidden, QtCore.QSize(500, 500))
self.addDockWidget(self.m_dockwidgets[0], KDDockWidgets.Location_OnBottom, None, initialOpts)
initialOpts = KDDockWidgets.InitialOption(KDDockWidgets.InitialVisibilityOption.StartHidden,
QtCore.QSize(500, 500))
self.addDockWidget(self.dockwidgets[0], KDDockWidgets.Location_OnBottom, None, initialOpts)
# Here, for finer granularity we specify right of dockwidgets[0]:
self.addDockWidget(self.m_dockwidgets[1], KDDockWidgets.Location_OnRight, self.m_dockwidgets[0])
self.addDockWidget(self.m_dockwidgets[2], KDDockWidgets.Location_OnLeft)
self.addDockWidget(self.m_dockwidgets[3], KDDockWidgets.Location_OnBottom)
self.addDockWidget(self.m_dockwidgets[4], KDDockWidgets.Location_OnBottom)
self.addDockWidget(self.dockwidgets[1], KDDockWidgets.Location_OnRight, self.dockwidgets[0])
self.addDockWidget(self.dockwidgets[2], KDDockWidgets.Location_OnLeft)
self.addDockWidget(self.dockwidgets[3], KDDockWidgets.Location_OnBottom)
self.addDockWidget(self.dockwidgets[4], KDDockWidgets.Location_OnBottom)
# Tab two dock widgets together
self.m_dockwidgets[3].addDockWidgetAsTab(self.m_dockwidgets[5])
self.dockwidgets[3].addDockWidgetAsTab(self.dockwidgets[5])
# 6 is floating, as it wasn't added to the main window via MainWindow::addDockWidget().
# and we tab 7 with it.
self.m_dockwidgets[6].addDockWidgetAsTab(self.m_dockwidgets[7])
self.dockwidgets[6].addDockWidgetAsTab(self.dockwidgets[7])
# Floating windows also support nesting, here we add 8 to the bottom of the group
self.m_dockwidgets[6].addDockWidgetToContainingWindow(self.m_dockwidgets[8], KDDockWidgets.Location_OnBottom)
self.dockwidgets[6].addDockWidgetToContainingWindow(self.dockwidgets[8], KDDockWidgets.Location_OnBottom)
floatingWindow = self.m_dockwidgets[6].window()
floatingWindow = self.dockwidgets[6].window()
floatingWindow.move(100, 100)
def newDockWidget(self):
# Passing options is optional, we just want to illustrate Option_NotClosable here
options = KDDockWidgets.DockWidget.Option_None
if (MyMainWindow.s_count == 0) and self.m_dockWidget0IsNonClosable:
if (MyMainWindow.s_count == 0) and self.dockWidget0IsNonClosable:
options |= KDDockWidgets.DockWidget.Option_NotClosable
if (MyMainWindow.s_count == 9) and self.m_dockWidget9IsNonDockable:
if (MyMainWindow.s_count == 9) and self.dockWidget9IsNonDockable:
options |= KDDockWidgets.DockWidget.Option_NotDockable
dock = KDDockWidgets.DockWidget("DockWidget #%d"%(MyMainWindow.s_count), options)
dock.setAffinities(self.affinities()); # optional, just to show the feature. Pass -mi to the example to see incompatible dock widgets
dock = KDDockWidgets.DockWidget("DockWidget #%d" % (MyMainWindow.s_count), options)
# optional, just to show the feature. Pass -mi to the example to see incompatible dock widgets
dock.setAffinities(self.affinities())
if MyMainWindow.s_count == 1:
dock.setIcon(QtGui.QIcon.fromTheme("mail-message"))
myWidget = newMyWidget(self)
if (MyMainWindow.s_count == 8) and self.m_maxSizeForDockWidget8:
if (MyMainWindow.s_count == 8) and self.maxSizeForDockWidget8:
# Set a maximum size on dock #8
myWidget.setMaximumSize(200, 200)
dock.setWidget(myWidget)
if dock.options() & KDDockWidgets.DockWidget.Option_NotDockable:
dock.setTitle("DockWidget #%d (%s)" %(MyMainWindow.s_count, "non dockable"))
dock.setTitle("DockWidget #%d (%s)" % (MyMainWindow.s_count, "non dockable"))
else:
dock.setTitle("DockWidget #%d"%(MyMainWindow.s_count))
dock.setTitle("DockWidget #%d" % (MyMainWindow.s_count))
dock.resize(600, 600)
self.m_toggleMenu.addAction(dock.toggleAction())
self.toggleMenu.addAction(dock.toggleAction())
MyMainWindow.s_count += 1
return dock

View File

@@ -9,18 +9,23 @@
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#
import PyKDDockWidgetsQt6
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
from PySide6 import QtWidgets, QtGui, QtCore
# pylint: disable=too-few-public-methods
class MyWidget(QtWidgets.QWidget):
s_images = {}
def __init__(self, backgroundFile, logoFile, parent = None):
def __init__(self, backgroundFile, logoFile, parent=None):
super().__init__(parent)
self.m_background = self._lookupImage(backgroundFile)
self.m_logo = self._lookupImage(logoFile)
self.background = self._lookupImage(backgroundFile)
self.logo = self._lookupImage(logoFile)
# pylint: disable=no-self-use
def _lookupImage(self, imageName):
if imageName == "":
return None
@@ -29,23 +34,23 @@ class MyWidget(QtWidgets.QWidget):
MyWidget.s_images[imageName] = QtGui.QImage(imageName)
return MyWidget.s_images[imageName]
def drawLogo(self, p):
if not self.m_logo:
if not self.logo:
return
ratio = self.m_logo.height() / (self.m_logo.width() * 1.0)
ratio = self.logo.height() / (self.logo.width() * 1.0)
maxWidth = int(0.80 * self.size().width())
maxHeight = int(0.80 * self.size().height())
proposedHeight = int(maxWidth * ratio)
if (proposedHeight <= maxHeight):
if proposedHeight <= maxHeight:
width = maxWidth
else:
width = int(maxHeight / ratio)
height = int(width * ratio)
targetLogoRect = QtCore.QRect(0,0, width, height)
targetLogoRect.moveCenter(self.rect().center() + QtCore.QPoint(0, -int(self.size().height() * 0.00)))
p.drawImage(targetLogoRect, self.m_logo, self.m_logo.rect());
targetLogoRect = QtCore.QRect(0, 0, width, height)
targetLogoRect.moveCenter(self.rect().center(
) + QtCore.QPoint(0, -int(self.size().height() * 0.00)))
p.drawImage(targetLogoRect, self.logo, self.logo.rect())

View File

@@ -9,21 +9,20 @@
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#
import PyKDDockWidgetsQt6
from PySide6 import QtWidgets, QtGui
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
from PySide6 import QtGui
from MyWidget import MyWidget
class MyWidget1(MyWidget):
def __init__(self, parent = None):
def __init__(self, parent=None):
super().__init__(":/assets/triangles.png", ":/assets/KDAB_bubble_white.png", parent)
def paintEvent(self, ev):
def paintEvent(self, event):
del event # unsed at this time
p = QtGui.QPainter(self)
p.fillRect(self.rect(), QtGui.QColor(0xCC, 0xCC, 0xCC))
p.drawImage(self.m_background.rect(), self.m_background, self.m_background.rect())
p.drawImage(self.background.rect(),
self.background, self.background.rect())
self.drawLogo(p)

View File

@@ -9,19 +9,19 @@
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#
import PyKDDockWidgetsQt6
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
from PySide6 import QtWidgets, QtGui, QtCore
from PySide6 import QtGui, QtCore
from MyWidget import MyWidget
class MyWidget2(MyWidget):
def __init__(self, parent = None):
def __init__(self, parent=None):
super().__init__("", ":/assets/KDAB_bubble_blue.png", parent)
def paintEvent(self, ev):
def paintEvent(self, event):
del event # unsed at this time
p = QtGui.QPainter(self)
p.fillRect(self.rect(), QtCore.Qt.white);
p.fillRect(self.rect(), QtCore.Qt.white)
self.drawLogo(p)

View File

@@ -9,24 +9,27 @@
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#
import PyKDDockWidgetsQt6
from PySide6 import QtWidgets, QtGui, QtCore
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
from PySide6 import QtGui, QtCore
from MyWidget import MyWidget
class MyWidget3(MyWidget):
def __init__(self, parent = None):
def __init__(self, parent=None):
super().__init__(":/assets/base.png", ":/assets/KDAB_bubble_fulcolor.png", parent)
self.m_triangle = QtGui.QImage(":/assets/tri.png")
self.triangle = QtGui.QImage(":/assets/tri.png")
def paintEvent(self, ev):
def paintEvent(self, event):
del event # unsed at this time
p = QtGui.QPainter(self)
p.fillRect(self.rect(), QtGui.QColor(0xD5, 0xD5, 0xD5))
p.drawImage(self.m_background.rect(), self.m_background, self.m_background.rect())
targetRect = QtCore.QRect(QtCore.QPoint(self.width() - self.m_triangle.width(), self.height() - self.m_triangle.height()), self.m_triangle.size())
p.drawImage(self.background.rect(),
self.background, self.background.rect())
QtCore.QRect(QtCore.QPoint(self.width() - self.triangle.width(),
self.height() - self.triangle.height()),
self.triangle.size())
self.drawLogo(p)

View File

@@ -3,6 +3,9 @@ Running python example
Generate resource file with:
~# rcc -g python -o rc_assets.py ../../examples/dockwidgets/resources_example.qrc
(on some systems, rcc might be invoked as rcc-qt6)
Make sure you installed kddockwidgets and set PYTHONPATH accordingly.
Run the app:
~# python3 main.py

View File

@@ -9,16 +9,26 @@
# Contact KDAB at <info@kdab.com> for commercial licensing options.
#
from PyKDDockWidgetsQt6 import KDDockWidgets
from MyMainWindow import MyMainWindow
from PySide6 import QtWidgets, QtCore
''' KDDockWidgets example (Qt6) '''
import sys
from MyMainWindow import MyMainWindow
from PySide6 import QtWidgets, QtCore
try:
# pylint: disable=unused-import
import rc_assets
except:
exit("Oops.. rc_assets needs to be generated first.\nPlease run:\n rcc -g python -o rc_assets.py ../../examples/dockwidgets/resources_example.qrc\n(Make sure to use the rcc from the Qt6 version used to generate the bindings!)")
except ImportError:
sys.exit(
'''
Oops.. rc_assets needs to be generated first.
Please run:
rcc -g python -o rc_assets.py ../../examples/dockwidgets/resources_example.qrc
(Make sure to use the rcc from the Qt6 version used to generate the bindings!)
On some systems rcc might be invoked as rcc-qt6.
'''
)
if __name__ == "__main__":
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
@@ -34,5 +44,4 @@ if __name__ == "__main__":
mainWindow.resize(1200, 1200)
mainWindow.show()
app.exec_()
app.exec()