Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for use PyQt6 #25

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions modules/ext/alchemical_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
"""
Qt data models that bind to SQLAlchemy queries
"""
from PyQt5.Qt import Qt, QVariant
from PyQt5.QtSql import QSqlTableModel
from PyQt5.QtWidgets import QMessageBox
try:
from PyQt6.QtCore import Qt, QVariant
from PyQt6.QtSql import QSqlTableModel
from PyQt6.QtWidgets import QMessageBox
except ImportError as err:
from PyQt5.Qt import Qt, QVariant
from PyQt5.QtSql import QSqlTableModel
from PyQt5.QtWidgets import QMessageBox
print(f"alchemical_model.py: ImportError {err=}, {type(err)=}")
except Exception as err:
print(f"alchemical_model.py: Unexpected {err=}, {type(err)=}")
raise


class SqlAlchemyTableModel(QSqlTableModel):
Expand Down Expand Up @@ -47,7 +56,7 @@ def __init__(self, session, entity, columns, parent=None):
self.refresh()

def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole:
return QVariant(self.fields[col][0])
return QVariant()

Expand All @@ -69,7 +78,7 @@ def refresh(self):
if self.sort is not None:
order, col = self.sort
col = self.fields[col][1]
if order == Qt.DescendingOrder:
if order == Qt.SortOrder.DescendingOrder:
col = col.desc()
else:
col = None
Expand All @@ -84,7 +93,7 @@ def refresh(self):
self.layoutChanged.emit()

def flags(self, index):
_flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
_flags = Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable

if self.sort is not None:
order, col = self.sort
Expand All @@ -99,12 +108,11 @@ def flags(self, index):
return _flags

def supportedDropActions(self):
return Qt.MoveAction
return Qt.DropAction.MoveAction

def dropMimeData(self, data, action, row, col, parent):
if action != Qt.MoveAction:
if action != Qt.DropAction.MoveAction:
return

return False

def rowCount(self, parent=None):
Expand All @@ -117,7 +125,7 @@ def data(self, index, role):
if not index.isValid():
return QVariant()

elif role not in (Qt.DisplayRole, Qt.EditRole):
elif role not in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole):
return QVariant()

row = self.results[index.row()]
Expand Down
20 changes: 12 additions & 8 deletions modules/gui/dialogsqltable.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import os
import sys

from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt, QTimer
try:
from PyQt6 import QtGui, QtWidgets
from PyQt6.QtCore import Qt, QTimer
except ImportError:
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt, QTimer

from modules.ext.alchemical_model import SqlAlchemyTableModel

Expand All @@ -47,11 +51,11 @@ def __init__(self, session, table, model, parent=None):

self.setModal(True)
self.buttonBox = QtWidgets.QDialogButtonBox()
self.buttonBox.setOrientation(Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Ok |
QtWidgets.QDialogButtonBox.Cancel)
self.buttonBox.setOrientation(Qt.Orientation.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.StandardButton.Ok |
QtWidgets.QDialogButtonBox.StandardButton.Cancel)
self.boxLayout = QtWidgets.QBoxLayout(
QtWidgets.QBoxLayout.TopToBottom, self)
QtWidgets.QBoxLayout.Direction.TopToBottom, self)
self.gridLayout = QtWidgets.QGridLayout()
self.setWindowTitle(self.tr("Item"))
try:
Expand Down Expand Up @@ -203,7 +207,7 @@ def get_values(session, table, model, test=None, parent=None):
if test:
QTimer.singleShot(500, dialog.accept)
result = dialog.exec()
return dialog.values(), result == QtWidgets.QDialog.Accepted
return dialog.values(), result == QtWidgets.QDialog.DialogCode.Accepted

@staticmethod
def edit_values(session, table, model, idEdit, test=None, parent=None):
Expand All @@ -226,7 +230,7 @@ def edit_values(session, table, model, idEdit, test=None, parent=None):
if test:
QTimer.singleShot(500, dialog.accept)
result = dialog.exec()
return dialog.values(), result == QtWidgets.QDialog.Accepted
return dialog.values(), result == QtWidgets.QDialog.DialogCode.Accepted

@classmethod
def my_table_model(cls, model, session):
Expand Down
49 changes: 29 additions & 20 deletions modules/gui/mainwindow.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
# -*- coding: utf-8 -*-


from PyQt5 import QtCore, QtWidgets
try:
from PyQt6 import QtCore, QtWidgets
from PyQt6.QtWidgets import QSizePolicy
from PyQt6.QtGui import QAction
except ImportError as err:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QSizePolicy, QAction
print(f"mainwindow.py: ImportError {err=}, {type(err)=}")
except Exception as err:
print(f"mainwindow.py: Unexpected {err=}, {type(err)=}")
raise


class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setObjectName("MainWindow")
self.resize(785, 769)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
self.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(2)
sizePolicy.setVerticalStretch(2)
sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
Expand All @@ -26,20 +35,20 @@ def __init__(self):
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SizeConstraint.SetNoConstraint)
self.horizontalLayout.setContentsMargins(0, 0, -1, -1)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SizeConstraint.SetNoConstraint)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.label_user = QtWidgets.QLabel(self.centralwidget)
self.label_user.setAlignment(QtCore.Qt.AlignCenter)
self.label_user.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_user.setObjectName("label_user")
self.verticalLayout.addWidget(self.label_user)
self.tableView_user = QtWidgets.QTableView(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.tableView_user.sizePolicy().hasHeightForWidth())
Expand Down Expand Up @@ -69,29 +78,29 @@ def __init__(self):
self.menuEvaluation = QtWidgets.QMenu(self.menubar)
self.menuEvaluation.setObjectName("menuEvaluation")
self.setMenuBar(self.menubar)
self.actionExit = QtWidgets.QAction(self)
self.actionExit = QAction(self)
self.actionExit.setObjectName("actionExit")
self.actionOpen = QtWidgets.QAction(self)
self.actionOpen = QAction(self)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(self)
self.actionSave = QAction(self)
self.actionSave.setObjectName("actionSave")
self.actionNew = QtWidgets.QAction(self)
self.actionNew = QAction(self)
self.actionNew.setObjectName("actionNew")
self.actionInfo = QtWidgets.QAction(self)
self.actionInfo = QAction(self)
self.actionInfo.setObjectName("actionInfo")
self.actionOverview = QtWidgets.QAction(self)
self.actionOverview = QAction(self)
self.actionOverview.setObjectName("actionOverview")
self.actionPrintPreview = QtWidgets.QAction(self)
self.actionPrintPreview = QAction(self)
self.actionPrintPreview.setObjectName("actionPrintPreview")
self.actionLoad_file = QtWidgets.QAction(self)
self.actionLoad_file = QAction(self)
self.actionLoad_file.setObjectName("actionLoad_file")
self.actionSave_file_as = QtWidgets.QAction(self)
self.actionSave_file_as = QAction(self)
self.actionSave_file_as.setObjectName("actionSave_file_as")
self.actionCreateCertificates = QtWidgets.QAction(self)
self.actionCreateCertificates = QAction(self)
self.actionCreateCertificates.setObjectName("actionCreateCertificates")
self.actionXLSX_Export = QtWidgets.QAction(self)
self.actionXLSX_Export = QAction(self)
self.actionXLSX_Export.setObjectName("actionXLSX_Export")
self.actionCreateAddress = QtWidgets.QAction(self)
self.actionCreateAddress = QAction(self)
self.actionCreateAddress.setObjectName("actionCreateAddress")
self.menuFile.addAction(self.actionExit)
self.menuInfo.addAction(self.actionInfo)
Expand Down
5 changes: 4 additions & 1 deletion modules/gui/printdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import os
import sys

from PyQt5 import QtGui, QtPrintSupport, QtWidgets
try:
from PyQt6 import QtGui, QtPrintSupport, QtWidgets
except ImportError:
from PyQt5 import QtGui, QtPrintSupport, QtWidgets


class DlgPrint(QtWidgets.QDialog):
Expand Down
36 changes: 25 additions & 11 deletions modules/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,25 @@
import sys
from os.path import join

from PyQt5 import QtGui, QtWidgets
from PyQt5.Qt import PYQT_VERSION_STR
from PyQt5.QtCore import QDir, QLocale, QObject, Qt, QTimer, QTranslator
from PyQt5.QtWidgets import (QDockWidget, QFileDialog, QMessageBox,
QPushButton, QTableView, QVBoxLayout, QWidget)
try:
from PyQt6 import QtGui, QtWidgets
from PyQt6.QtCore import QDir, QLocale, QObject, Qt, QTimer, QTranslator, PYQT_VERSION_STR
from PyQt6.QtWidgets import (QDockWidget, QFileDialog, QMessageBox,
QPushButton, QTableView, QVBoxLayout, QWidget)
LeftDockWidgetArea = Qt.DockWidgetArea.LeftDockWidgetArea
RightDockWidgetArea = Qt.DockWidgetArea.RightDockWidgetArea
except ImportError as err:
from PyQt5 import QtGui, QtWidgets
from PyQt5.Qt import PYQT_VERSION_STR
from PyQt5.QtCore import QDir, QLocale, QObject, Qt, QTimer, QTranslator
from PyQt5.QtWidgets import (QDockWidget, QFileDialog, QMessageBox,
QPushButton, QTableView, QVBoxLayout, QWidget)
LeftDockWidgetArea, RightDockWidgetArea = Qt.LeftDockWidgetArea, Qt.RightDockWidgetArea
print(f"main.py: ImportError {err=}, {type(err)=}")
except Exception as err:
print(f"main.py: Unexpected {err=}, {type(err)=}")
raise

from sqlalchemy import create_engine, orm

from modules import VERSION_STR, model, writexlsx
Expand Down Expand Up @@ -406,7 +420,7 @@ def on_overview(self, test=None):

def createDockWindows(self):
dock = QDockWidget(self.tr("Age"), self.ui)
dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dock.setAllowedAreas(LeftDockWidgetArea | RightDockWidgetArea)
ageWidget = QWidget(dock)
layout = QVBoxLayout()

Expand All @@ -421,12 +435,12 @@ def createDockWindows(self):
layout.addWidget(self.ui.pushButton_deleteage)
ageWidget.setLayout(layout)
dock.setWidget(ageWidget)
self.ui.addDockWidget(Qt.RightDockWidgetArea, dock)
self.ui.addDockWidget(RightDockWidgetArea, dock)
self.ui.viewMenu = self.ui.menuBar().addMenu(self.tr("&View"))
self.ui.viewMenu.addAction(dock.toggleViewAction())

dock = QDockWidget(self.tr("Bow"), self.ui)
dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dock.setAllowedAreas(LeftDockWidgetArea | RightDockWidgetArea)
ageWidget = QWidget(dock)
layout = QVBoxLayout()

Expand All @@ -441,11 +455,11 @@ def createDockWindows(self):
layout.addWidget(self.ui.pushButton_deletebow)
ageWidget.setLayout(layout)
dock.setWidget(ageWidget)
self.ui.addDockWidget(Qt.LeftDockWidgetArea, dock)
self.ui.addDockWidget(LeftDockWidgetArea, dock)
self.ui.viewMenu.addAction(dock.toggleViewAction())

dock = QDockWidget(self.tr("Club"), self.ui)
dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
dock.setAllowedAreas(LeftDockWidgetArea | RightDockWidgetArea)
ageWidget = QWidget(dock)
layout = QVBoxLayout()

Expand All @@ -460,7 +474,7 @@ def createDockWindows(self):
layout.addWidget(self.ui.pushButton_deleteclub)
ageWidget.setLayout(layout)
dock.setWidget(ageWidget)
self.ui.addDockWidget(Qt.RightDockWidgetArea, dock)
self.ui.addDockWidget(RightDockWidgetArea, dock)
self.ui.viewMenu.addAction(dock.toggleViewAction())

user_new = functools.partial(self.entry_new, model.User, self.main.model_user)
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ pyqt5
sqlalchemy>=1.4.0
docx-mailmerge
openpyxl
# unit test requirements
pycodestyle
isort
6 changes: 5 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[pycodestyle]
max-line-length = 100
max-line-length = 120

[flake8]
max-line-length = 120

17 changes: 10 additions & 7 deletions tests/test_sqlAlchemyTableModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Archerank2

Copyright (C) <2019,2023> Markus Hackspacher
Copyright (C) <2019-2024> Markus Hackspacher

This file is part of Archerank2.

Expand All @@ -23,7 +23,10 @@

from unittest import TestCase

from PyQt5.Qt import QMetaType, QModelIndex, Qt
try:
from PyQt6.Qt import QMetaType, QModelIndex, Qt
except ImportError:
from PyQt5.Qt import QMetaType, QModelIndex, Qt
from sqlalchemy import create_engine, orm

from modules import model
Expand Down Expand Up @@ -63,7 +66,7 @@ def test_headerData(self):
self.assertEqual(header.canConvert(QMetaType.QString), True)
self.assertEqual(header.value(), 'Lastname')

header = self.model_user.headerData(0, Qt.Vertical, Qt.DisplayRole)
header = self.model_user.headerData(0, Qt.Vertical, Qt.ItemDataRole.DisplayRole)
self.assertEqual(header.value(), None)

def test_setFilter(self):
Expand All @@ -85,7 +88,7 @@ def test_dropMimeData(self):
self.model_user.dropMimeData('name', Qt.MoveAction, 0, 2, index), False)
self.assertEqual(
self.model_user.dropMimeData('a', Qt.DropAction, 0, 2, index), None)
self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'John')
self.assertEqual(self.model_user.data(index, Qt.ItemDataRole.DisplayRole), 'John')

def test_rowCount(self):
self.assertEqual(self.model_user.rowCount(), 0)
Expand All @@ -106,11 +109,11 @@ def test_data(self):
self.model_user.refresh()
index = self.model_user.createIndex(0, 1)
self.assertEqual(index.isValid(), True)
self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'Dow')
self.assertEqual(self.model_user.data(index, Qt.ItemDataRole.DisplayRole), 'Dow')
index = self.model_user.createIndex(0, 2)
self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'John')
self.assertEqual(self.model_user.data(index, Qt.ItemDataRole.DisplayRole), 'John')
self.assertEqual(self.model_user.setData(index, 'Jonny'), True)
self.assertEqual(self.model_user.data(index, Qt.DisplayRole), 'Jonny')
self.assertEqual(self.model_user.data(index, Qt.ItemDataRole.DisplayRole), 'Jonny')

def test_refresh(self):
self.session.add(model.User(name='John', lastname='Dow'))
Expand Down
Loading