Item delegate for SQLite date and time fields

This commit is contained in:
Bill Somerville 2019-12-08 18:36:04 +00:00
parent d5269c9002
commit b6b8271a6d
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
3 changed files with 64 additions and 0 deletions

View File

@ -282,6 +282,7 @@ set (wsjt_qt_CXXSRCS
item_delegates/MaidenheadLocatorDelegate.cpp item_delegates/MaidenheadLocatorDelegate.cpp
item_delegates/FrequencyDelegate.cpp item_delegates/FrequencyDelegate.cpp
item_delegates/FrequencyDeltaDelegate.cpp item_delegates/FrequencyDeltaDelegate.cpp
item_delegates/SQLiteDateTimeDelegate.cpp
models/CabrilloLog.cpp models/CabrilloLog.cpp
logbook/AD1CCty.cpp logbook/AD1CCty.cpp
logbook/WorkedBefore.cpp logbook/WorkedBefore.cpp

View File

@ -0,0 +1,40 @@
#include "SQLiteDateTimeDelegate.hpp"
#include <QDateTimeEdit>
#include <QDateTime>
#include <QLocale>
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<QDateTimeEdit *> (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<QDateTimeEdit *> (editor)->dateTime ();
if (value.isValid () && !value.isNull ())
{
data = value.toMSecsSinceEpoch () / 1000ull;
}
model->setData (index, data, Qt::EditRole);
}

View File

@ -0,0 +1,23 @@
#ifndef SQLITE_DATE_TIME_DELEGATE_HPP_
#define SQLITE_DATE_TIME_DELEGATE_HPP_
#include <QStyledItemDelegate>
//
// 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