mirror of https://gitlab.com/pamhyr/pamhyr2
View: BC, LC: Factorise time delegate and widget.
parent
602593a6cb
commit
45690690d1
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue