mirror of
https://github.com/saitohirga/WSJT-X.git
synced 2024-11-21 03:21:58 -05:00
Item delegate for SQLite date and time fields
This commit is contained in:
parent
d5269c9002
commit
b6b8271a6d
@ -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
|
||||
|
40
item_delegates/SQLiteDateTimeDelegate.cpp
Normal file
40
item_delegates/SQLiteDateTimeDelegate.cpp
Normal 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);
|
||||
}
|
23
item_delegates/SQLiteDateTimeDelegate.hpp
Normal file
23
item_delegates/SQLiteDateTimeDelegate.hpp
Normal 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
|
Loading…
Reference in New Issue
Block a user