summaryrefslogtreecommitdiff
path: root/item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2018-12-19 22:58:32 +0100
committerChristoph Berg <myon@debian.org>2018-12-19 22:58:32 +0100
commite0737c56c1e384e05064a0c6e1ac46979371041b (patch)
treea0123d694a2d36c43dabdf0a3907392fce11d457 /item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp
parentb4aebf2d137b892f53503b13785ecae900062ff7 (diff)
New upstream version 2.0.0+repackupstream/2.0.0+repack
Diffstat (limited to 'item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp')
-rw-r--r--item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp b/item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp
new file mode 100644
index 00000000..a666ed32
--- /dev/null
+++ b/item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp
@@ -0,0 +1,67 @@
+#ifndef DATE_TIME_AS_SECS_SINCE_EPOCH_DELEGATE_HPP_
+#define DATE_TIME_AS_SECS_SINCE_EPOCH_DELEGATE_HPP_
+
+#include <memory>
+#include <QStyledItemDelegate>
+#include <QVariant>
+#include <QLocale>
+#include <QDateTime>
+#include <QAbstractItemModel>
+#include <QDateTimeEdit>
+
+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, Qt::UTC);
+ }
+
+ QString displayText (QVariant const& value, QLocale const& locale) const override
+ {
+ return locale.toString (to_date_time (value), locale.dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
+ }
+
+ QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const& /*option*/, QModelIndex const& /*index*/) const override
+ {
+ std::unique_ptr<QDateTimeEdit> editor {new QDateTimeEdit {parent}};
+ editor->setDisplayFormat (parent->locale ().dateFormat (QLocale::ShortFormat) + " hh:mm:ss");
+ editor->setTimeSpec (Qt::UTC); // needed because it ignores time
+ // spec of the QDateTime that it is
+ // set from
+ return editor.release ();
+ }
+
+ void setEditorData (QWidget * editor, QModelIndex const& index) const override
+ {
+ static_cast<QDateTimeEdit *> (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<QDateTimeEdit *> (editor)->dateTime ()));
+ }
+
+ void updateEditorGeometry (QWidget * editor, QStyleOptionViewItem const& option, QModelIndex const& /*index*/) const override
+ {
+ editor->setGeometry (option.rect);
+ }
+};
+
+#endif