From 45690690d1c4e1312ada25fd0b6e44cacef0a92d Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Mon, 11 Sep 2023 15:49:10 +0200 Subject: [PATCH] View: BC, LC: Factorise time delegate and widget. --- src/View/BoundaryCondition/Edit/Table.py | 99 --------------------- src/View/BoundaryCondition/Edit/Window.py | 5 +- src/View/LateralContribution/Edit/Table.py | 98 -------------------- src/View/LateralContribution/Edit/Window.py | 5 +- src/View/Tools/PamhyrDelegate.py | 70 +++++++++++++++ src/View/Tools/PamhyrWidget.py | 92 +++++++++++++++++++ 6 files changed, 168 insertions(+), 201 deletions(-) create mode 100644 src/View/Tools/PamhyrDelegate.py create mode 100644 src/View/Tools/PamhyrWidget.py diff --git a/src/View/BoundaryCondition/Edit/Table.py b/src/View/BoundaryCondition/Edit/Table.py index 8850ec46..35360400 100644 --- a/src/View/BoundaryCondition/Edit/Table.py +++ b/src/View/BoundaryCondition/Edit/Table.py @@ -54,105 +54,6 @@ _translate = QCoreApplication.translate logger = logging.getLogger() -class ExtendedTimeEdit(AWidget): - def __init__(self, parent=None): - super(ExtendedTimeEdit, self).__init__( - ui="extendedTimeEdit", - parent=parent - ) - self.parent = parent - - self.spinBox_days = self.find(QSpinBox, "spinBox_days") - self.timeEdit = self.find(QTimeEdit, "timeEdit") - - def set_time(self, time): - days = 0 - stime = time - - # if ',' in time, time format is 'DD days, HH:MM:SS', - # otherelse is 'HH:MM:SS' - if "," in time: - s = time.strip().split(" ") - days = int(s[0]) - stime = s[-1] - - qtime = QTime.fromString( - stime, - "h:mm:ss" - ) - self.spinBox_days.setValue(days) - self.timeEdit.setTime(qtime) - - def get_time(self): - days = self.spinBox_days.value() - time = self.timeEdit.time().toPyTime() - secs = ( - (time.hour * 3600) + - (time.minute * 60) + - time.second - ) - - return timedelta(days=days, seconds=secs) - -class ExtendedDateTimeEdit(AWidget): - def __init__(self, parent=None): - super(ExtendedDateTimeEdit, self).__init__( - ui="extendedDateTimeEdit", - parent=parent - ) - self.parent = parent - - self.dateTimeEdit = self.find(QDateTimeEdit, "dateTimeEdit") - - def set_time(self, time): - qdatetime = QDateTime.fromString( - time, - "yyyy-MM-dd hh:mm:ss" - ) - self.dateTimeEdit.setDateTime(qdatetime) - - def get_time(self): - time = self.dateTimeEdit.dateTime().toPyDateTime() - return time - -class ExTimeDelegate(QItemDelegate): - def __init__(self, data=None, mode="time", parent=None): - super(ExTimeDelegate, self).__init__(parent) - - self._data = data - self._mode = mode - - def createEditor(self, parent, option, index): - if self._mode == "time": - self.editor = ExtendedTimeEdit(parent=parent) - else: - self.editor = ExtendedDateTimeEdit(parent=parent) - value = index.data(Qt.DisplayRole) - self.editor.set_time(value) - logger.debug(str(value)) - return self.editor - - def setModelData(self, editor, model, index): - time = editor.get_time() - if self._mode == "time": - model.setData(index, int(time.total_seconds())) - else: - logger.debug(str(time.timestamp())) - model.setData(index, int(time.timestamp())) - editor.close() - editor.deleteLater() - - def updateEditorGeometry(self, editor, option, index): - r = QRect(option.rect) - if self.editor.windowFlags() & Qt.Popup and editor.parent() is not None: - r.setTopLeft(self.editor.parent().mapToGlobal(r.topLeft())) - editor.setGeometry(r) - - @pyqtSlot() - def currentItemChanged(self): - self.commitData.emit(self.sender()) - - class TableModel(PamhyrTableModel): def data(self, index, role): if role == Qt.TextAlignmentRole: diff --git a/src/View/BoundaryCondition/Edit/Window.py b/src/View/BoundaryCondition/Edit/Window.py index 180eb11b..e1e6a27f 100644 --- a/src/View/BoundaryCondition/Edit/Window.py +++ b/src/View/BoundaryCondition/Edit/Window.py @@ -22,6 +22,7 @@ from tools import timer, trace from View.ASubWindow import ASubMainWindow, AWidget from View.ListedSubWindow import ListedSubWindow +from View.Tools.PamhyrDelegate import PamhyrExTimeDelegate from PyQt5.QtGui import ( QKeySequence, @@ -46,7 +47,7 @@ from View.Plot.navigation_toolbar_2qt import PamHyrNavigationToolbar2QT from View.BoundaryCondition.translate import long_types from View.BoundaryCondition.Edit.translate import table_headers from View.BoundaryCondition.Edit.UndoCommand import SetMetaDataCommand -from View.BoundaryCondition.Edit.Table import TableModel, ExTimeDelegate +from View.BoundaryCondition.Edit.Table import TableModel from View.BoundaryCondition.Edit.Plot import Plot _translate = QCoreApplication.translate @@ -150,7 +151,7 @@ class EditBoundaryConditionWindow(ASubMainWindow, ListedSubWindow): for h in self._data.header: headers[h] = table_headers[h] - self._delegate_time = ExTimeDelegate( + self._delegate_time = PamhyrExTimeDelegate( data = self._data, mode = self._study.time_system, parent = self diff --git a/src/View/LateralContribution/Edit/Table.py b/src/View/LateralContribution/Edit/Table.py index 034c914d..2fff0895 100644 --- a/src/View/LateralContribution/Edit/Table.py +++ b/src/View/LateralContribution/Edit/Table.py @@ -52,104 +52,6 @@ _translate = QCoreApplication.translate logger = logging.getLogger() -class ExtendedTimeEdit(AWidget): - def __init__(self, parent=None): - super(ExtendedTimeEdit, self).__init__( - ui="extendedTimeEdit", - parent=parent - ) - self.parent = parent - - self.spinBox_days = self.find(QSpinBox, "spinBox_days") - self.timeEdit = self.find(QTimeEdit, "timeEdit") - - def set_time(self, time): - days = 0 - stime = time - - # if ',' in time, time format is 'DD days, HH:MM:SS', - # otherelse is 'HH:MM:SS' - if "," in time: - s = time.strip().split(" ") - days = int(s[0]) - stime = s[-1] - - qtime = QTime.fromString( - stime, - "h:mm:ss" - ) - self.spinBox_days.setValue(days) - self.timeEdit.setTime(qtime) - - def get_time(self): - days = self.spinBox_days.value() - time = self.timeEdit.time().toPyTime() - secs = ( - (time.hour * 3600) + - (time.minute * 60) + - time.second - ) - - return timedelta(days=days, seconds=secs) - -class ExtendedDateTimeEdit(AWidget): - def __init__(self, parent=None): - super(ExtendedDateTimeEdit, self).__init__( - ui="extendedDateTimeEdit", - parent=parent - ) - self.parent = parent - - self.dateTimeEdit = self.find(QDateTimeEdit, "dateTimeEdit") - - def set_time(self, time): - qdatetime = QDateTime.fromString( - time, - "yyyy-MM-dd hh:mm:ss" - ) - self.dateTimeEdit.setDateTime(qdatetime) - - def get_time(self): - time = self.dateTimeEdit.dateTime().toPyDateTime() - return time - -class ExTimeDelegate(QItemDelegate): - def __init__(self, data=None, mode="time", parent=None): - super(ExTimeDelegate, self).__init__(parent) - - self._data = data - self._mode = mode - - def createEditor(self, parent, option, index): - if self._mode == "time": - self.editor = ExtendedTimeEdit(parent=parent) - else: - self.editor = ExtendedDateTimeEdit(parent=parent) - value = index.data(Qt.DisplayRole) - self.editor.set_time(value) - logger.debug(str(value)) - return self.editor - - def setModelData(self, editor, model, index): - time = editor.get_time() - if self._mode == "time": - model.setData(index, int(time.total_seconds())) - else: - logger.debug(str(time.timestamp())) - model.setData(index, int(time.timestamp())) - editor.close() - editor.deleteLater() - - def updateEditorGeometry(self, editor, option, index): - r = QRect(option.rect) - if self.editor.windowFlags() & Qt.Popup and editor.parent() is not None: - r.setTopLeft(self.editor.parent().mapToGlobal(r.topLeft())) - editor.setGeometry(r) - - @pyqtSlot() - def currentItemChanged(self): - self.commitData.emit(self.sender()) - class TableModel(PamhyrTableModel): def data(self, index, role): diff --git a/src/View/LateralContribution/Edit/Window.py b/src/View/LateralContribution/Edit/Window.py index 0fd6720c..f91f99a3 100644 --- a/src/View/LateralContribution/Edit/Window.py +++ b/src/View/LateralContribution/Edit/Window.py @@ -20,6 +20,7 @@ from tools import timer, trace from View.ASubWindow import ASubMainWindow from View.ListedSubWindow import ListedSubWindow +from View.Tools.PamhyrDelegate import PamhyrExTimeDelegate from PyQt5.QtGui import ( QKeySequence, @@ -41,7 +42,7 @@ from View.Plot.navigation_toolbar_2qt import PamHyrNavigationToolbar2QT from View.LateralContribution.translate import long_types from View.LateralContribution.Edit.translate import table_headers -from View.LateralContribution.Edit.Table import TableModel, ExTimeDelegate +from View.LateralContribution.Edit.Table import TableModel from View.LateralContribution.Edit.Plot import Plot _translate = QCoreApplication.translate @@ -90,7 +91,7 @@ class EditLateralContributionWindow(ASubMainWindow, ListedSubWindow): for h in self._data.header: headers[h] = table_headers[h] - self._delegate_time = ExTimeDelegate( + self._delegate_time = PamhyrExTimeDelegate( data = self._data, mode = self._study.time_system, parent = self diff --git a/src/View/Tools/PamhyrDelegate.py b/src/View/Tools/PamhyrDelegate.py new file mode 100644 index 00000000..c5e97da3 --- /dev/null +++ b/src/View/Tools/PamhyrDelegate.py @@ -0,0 +1,70 @@ +# PamhyrDelegate.py -- Pamhyr +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +import logging + +from PyQt5.QtCore import ( + Qt, QRect, QTime, QDateTime, pyqtSlot, +) + +from PyQt5.QtWidgets import ( + QSpinBox, QTimeEdit, QDateTimeEdit, QItemDelegate, +) + +from View.Tools.PamhyrWidget import ( + PamhyrExtendedTimeEdit, PamhyrExtendedDateTimeEdit +) + +logger = logging.getLogger() + +class PamhyrExTimeDelegate(QItemDelegate): + def __init__(self, data=None, mode="time", parent=None): + super(PamhyrExTimeDelegate, self).__init__(parent) + + self._data = data + self._mode = mode + + def createEditor(self, parent, option, index): + if self._mode == "time": + self.editor = PamhyrExtendedTimeEdit(parent=parent) + else: + self.editor = PamhyrExtendedDateTimeEdit(parent=parent) + value = index.data(Qt.DisplayRole) + self.editor.set_time(value) + logger.debug(str(value)) + return self.editor + + def setModelData(self, editor, model, index): + time = editor.get_time() + if self._mode == "time": + model.setData(index, int(time.total_seconds())) + else: + logger.debug(str(time.timestamp())) + model.setData(index, int(time.timestamp())) + editor.close() + editor.deleteLater() + + def updateEditorGeometry(self, editor, option, index): + r = QRect(option.rect) + if self.editor.windowFlags() & Qt.Popup and editor.parent() is not None: + r.setTopLeft(self.editor.parent().mapToGlobal(r.topLeft())) + editor.setGeometry(r) + + @pyqtSlot() + def currentItemChanged(self): + self.commitData.emit(self.sender()) diff --git a/src/View/Tools/PamhyrWidget.py b/src/View/Tools/PamhyrWidget.py new file mode 100644 index 00000000..60e06174 --- /dev/null +++ b/src/View/Tools/PamhyrWidget.py @@ -0,0 +1,92 @@ +# PamhyrWidget.py -- Pamhyr +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +import logging + +from PyQt5.QtCore import ( + QModelIndex, QRect, QTime, QDateTime, +) + +from PyQt5.QtWidgets import ( + QSpinBox, QTimeEdit, QDateTimeEdit, QItemDelegate, +) + +from View.ASubWindow import AWidget + +logger = logging.getLogger() + +class PamhyrExtendedTimeEdit(AWidget): + def __init__(self, parent=None): + super(PamhyrExtendedTimeEdit, self).__init__( + ui="extendedTimeEdit", + parent=parent + ) + self.parent = parent + + self.spinBox_days = self.find(QSpinBox, "spinBox_days") + self.timeEdit = self.find(QTimeEdit, "timeEdit") + + def set_time(self, time): + days = 0 + stime = time + + # if ',' in time, time format is 'DD days, HH:MM:SS', + # otherelse is 'HH:MM:SS' + if "," in time: + s = time.strip().split(" ") + days = int(s[0]) + stime = s[-1] + + qtime = QTime.fromString( + stime, + "h:mm:ss" + ) + self.spinBox_days.setValue(days) + self.timeEdit.setTime(qtime) + + def get_time(self): + days = self.spinBox_days.value() + time = self.timeEdit.time().toPyTime() + secs = ( + (time.hour * 3600) + + (time.minute * 60) + + time.second + ) + + return timedelta(days=days, seconds=secs) + +class PamhyrExtendedDateTimeEdit(AWidget): + def __init__(self, parent=None): + super(ExtendedDateTimeEdit, self).__init__( + ui="extendedDateTimeEdit", + parent=parent + ) + self.parent = parent + + self.dateTimeEdit = self.find(QDateTimeEdit, "dateTimeEdit") + + def set_time(self, time): + qdatetime = QDateTime.fromString( + time, + "yyyy-MM-dd hh:mm:ss" + ) + self.dateTimeEdit.setDateTime(qdatetime) + + def get_time(self): + time = self.dateTimeEdit.dateTime().toPyDateTime() + return time -- GitLab