diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b3a4e010..3147d7c98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,6 +282,7 @@ set (wsjt_qt_CXXSRCS item_delegates/MaidenheadLocatorDelegate.cpp item_delegates/FrequencyDelegate.cpp item_delegates/FrequencyDeltaDelegate.cpp + item_delegates/SQLiteDateTimeDelegate.cpp models/CabrilloLog.cpp logbook/AD1CCty.cpp logbook/WorkedBefore.cpp diff --git a/item_delegates/SQLiteDateTimeDelegate.cpp b/item_delegates/SQLiteDateTimeDelegate.cpp new file mode 100644 index 000000000..1cf8ad262 --- /dev/null +++ b/item_delegates/SQLiteDateTimeDelegate.cpp @@ -0,0 +1,40 @@ +#include "SQLiteDateTimeDelegate.hpp" + +#include +#include +#include + +SQLiteDateTimeDelegate::SQLiteDateTimeDelegate (QObject * parent) + : QStyledItemDelegate {parent} +{ +} + +QWidget * SQLiteDateTimeDelegate::createEditor (QWidget * parent, QStyleOptionViewItem const& + , QModelIndex const&) const +{ + auto * editor = new QDateTimeEdit {parent}; + editor->setCalendarPopup (true); + editor->setDisplayFormat (QLocale {}.dateFormat (QLocale::ShortFormat) + " hh:mm:ss"); + editor->setFrame (false); + return editor; +} + +void SQLiteDateTimeDelegate::setEditorData (QWidget * editor, QModelIndex const& index) const +{ + auto const& value = index.model ()->data (index, Qt::EditRole); + if (value.isValid () && !value.isNull ()) + { + static_cast (editor)->setDateTime (QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull, Qt::UTC)); + } +} + +void SQLiteDateTimeDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const +{ + QVariant data; + auto const& value = static_cast (editor)->dateTime (); + if (value.isValid () && !value.isNull ()) + { + data = value.toMSecsSinceEpoch () / 1000ull; + } + model->setData (index, data, Qt::EditRole); +} diff --git a/item_delegates/SQLiteDateTimeDelegate.hpp b/item_delegates/SQLiteDateTimeDelegate.hpp new file mode 100644 index 000000000..183b26d73 --- /dev/null +++ b/item_delegates/SQLiteDateTimeDelegate.hpp @@ -0,0 +1,23 @@ +#ifndef SQLITE_DATE_TIME_DELEGATE_HPP_ +#define SQLITE_DATE_TIME_DELEGATE_HPP_ + +#include + +// +// Class SQLiteDateTimeDelegte +// +// Item delegate for editing a date and time stored as milliseconds +// since the Unix epoch and displayed or edited as a QDateTime +// showing UTC +// +class SQLiteDateTimeDelegate final + : public QStyledItemDelegate +{ +public: + explicit SQLiteDateTimeDelegate (QObject * parent = nullptr); + QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override; + void setEditorData (QWidget * editor, QModelIndex const&) const override; + void setModelData (QWidget * editor, QAbstractItemModel *, QModelIndex const&) const override; +}; + +#endif