#ifndef DATE_TIME_AS_SECS_SINCE_EPOCH_DELEGATE_HPP_ #define DATE_TIME_AS_SECS_SINCE_EPOCH_DELEGATE_HPP_ #include #include #include #include #include class DateTimeAsSecsSinceEpochDelegate final : public QStyledItemDelegate { public: DateTimeAsSecsSinceEpochDelegate (QObject * parent = nullptr) : QStyledItemDelegate {parent} { } static QVariant to_secs_since_epoch (QDateTime const& date_time) { return date_time.toMSecsSinceEpoch () / 1000ull; } static QDateTime to_date_time (QModelIndex const& index, int role = Qt::DisplayRole) { return to_date_time (index.model ()->data (index, role)); } static QDateTime to_date_time (QVariant const& value) { return QDateTime::fromMSecsSinceEpoch (value.toULongLong () * 1000ull); } QString displayText (QVariant const& value, QLocale const& locale) const override { return locale.toString (to_date_time (value), QLocale::ShortFormat); } QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const& /*option*/, QModelIndex const& /*index*/) const override { return new QDateTimeEdit {parent}; } void setEditorData (QWidget * editor, QModelIndex const& index) const override { static_cast (editor)->setDateTime (to_date_time (index, Qt::EditRole)); } void setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const override { model->setData (index, to_secs_since_epoch (static_cast (editor)->dateTime ())); } void updateEditorGeometry (QWidget * editor, QStyleOptionViewItem const& option, QModelIndex const& /*index*/) const override { editor->setGeometry (option.rect); } }; #endif