summaryrefslogtreecommitdiff
path: root/item_delegates
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
parentb4aebf2d137b892f53503b13785ecae900062ff7 (diff)
New upstream version 2.0.0+repackupstream/2.0.0+repack
Diffstat (limited to 'item_delegates')
-rw-r--r--item_delegates/CallsignDelegate.cpp31
-rw-r--r--item_delegates/CallsignDelegate.hpp30
-rw-r--r--item_delegates/CandidateKeyFilter.cpp89
-rw-r--r--item_delegates/CandidateKeyFilter.hpp41
-rw-r--r--item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp67
-rw-r--r--item_delegates/ForeignKeyDelegate.cpp62
-rw-r--r--item_delegates/ForeignKeyDelegate.hpp43
-rw-r--r--item_delegates/MaidenheadLocatorDelegate.cpp30
-rw-r--r--item_delegates/MaidenheadLocatorDelegate.hpp30
-rw-r--r--item_delegates/item_delegates.pri12
10 files changed, 435 insertions, 0 deletions
diff --git a/item_delegates/CallsignDelegate.cpp b/item_delegates/CallsignDelegate.cpp
new file mode 100644
index 00000000..d3b9864c
--- /dev/null
+++ b/item_delegates/CallsignDelegate.cpp
@@ -0,0 +1,31 @@
+#include "CallsignDelegate.hpp"
+
+#include <QLineEdit>
+
+#include "validators/CallsignValidator.hpp"
+
+CallsignDelegate::CallsignDelegate (QObject * parent)
+ : QStyledItemDelegate {parent}
+ , validator_ {new CallsignValidator}
+{
+}
+
+QWidget * CallsignDelegate::createEditor (QWidget * parent, QStyleOptionViewItem const&
+ , QModelIndex const&) const
+{
+ auto * editor = new QLineEdit {parent};
+ editor->setFrame (false);
+ editor->setValidator (validator_.data ());
+ return editor;
+}
+
+void CallsignDelegate::setEditorData (QWidget * editor, QModelIndex const& index) const
+{
+ static_cast<QLineEdit *> (editor)->setText (index.model ()->data (index, Qt::EditRole).toString ());
+}
+
+void CallsignDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const
+{
+ model->setData (index, static_cast<QLineEdit *> (editor)->text (), Qt::EditRole);
+}
+
diff --git a/item_delegates/CallsignDelegate.hpp b/item_delegates/CallsignDelegate.hpp
new file mode 100644
index 00000000..b5bdce1c
--- /dev/null
+++ b/item_delegates/CallsignDelegate.hpp
@@ -0,0 +1,30 @@
+#ifndef CALLSIGN_DELEGATE_HPP_
+#define CALLSIGN_DELEGATE_HPP_
+
+#include <QStyledItemDelegate>
+
+class QValidator;
+
+//
+// Class CallsignDelegate
+//
+// Item delegate for editing a callsign
+//
+class CallsignDelegate final
+ : public QStyledItemDelegate
+{
+public:
+ explicit CallsignDelegate (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;
+ void updateEditorGeometry (QWidget * editor, QStyleOptionViewItem const& option, QModelIndex const&) const override
+ {
+ editor->setGeometry (option.rect);
+ }
+
+private:
+ QScopedPointer<QValidator> validator_;
+};
+
+#endif
diff --git a/item_delegates/CandidateKeyFilter.cpp b/item_delegates/CandidateKeyFilter.cpp
new file mode 100644
index 00000000..306e4102
--- /dev/null
+++ b/item_delegates/CandidateKeyFilter.cpp
@@ -0,0 +1,89 @@
+#include "CandidateKeyFilter.hpp"
+
+#include <QModelIndex>
+#include <QAbstractItemModel>
+
+#include "pimpl_impl.hpp"
+
+class CandidateKeyFilter::impl final
+{
+public:
+ explicit impl (int referenced_key_column
+ , int referenced_key_role
+ , QAbstractItemModel const * referencing_model
+ , int referencing_key_column
+ , int referencing_key_role)
+ : referencing_ {referencing_model}
+ , referencing_key_column_ {referencing_key_column}
+ , referencing_key_role_ {referencing_key_role}
+ , referenced_key_column_ {referenced_key_column}
+ , referenced_key_role_ {referenced_key_role}
+ {
+ }
+
+ QAbstractItemModel const * referencing_;
+ int referencing_key_column_;
+ int referencing_key_role_;
+ int referenced_key_column_;
+ int referenced_key_role_;
+ QModelIndex active_key_;
+};
+
+CandidateKeyFilter::CandidateKeyFilter (QAbstractItemModel const * referenced_model
+ , int referenced_key_column
+ , QObject * parent
+ , int referenced_key_role)
+ : QSortFilterProxyModel {parent}
+ , m_ {referenced_key_column, referenced_key_role, nullptr, 0, Qt::EditRole}
+{
+ setSourceModel (const_cast<QAbstractItemModel *> (referenced_model));
+}
+
+CandidateKeyFilter::CandidateKeyFilter (QAbstractItemModel const * referenced_model
+ , QAbstractItemModel const * referencing_model
+ , int referenced_key_column
+ , int referencing_key_column
+ , QObject * parent
+ , int referenced_key_role
+ , int referencing_key_role)
+ : QSortFilterProxyModel {parent}
+ , m_ {referenced_key_column, referenced_key_role, referencing_model, referencing_key_column, referencing_key_role}
+{
+ setSourceModel (const_cast<QAbstractItemModel *> (referenced_model));
+}
+
+CandidateKeyFilter::~CandidateKeyFilter ()
+{
+}
+
+void CandidateKeyFilter::set_active_key (QModelIndex const& index)
+{
+ if (m_->referencing_)
+ {
+ if (index.isValid () )
+ {
+ Q_ASSERT (index.column () == m_->referencing_key_column_);
+ m_->active_key_ = index;
+ }
+ invalidateFilter ();
+ }
+}
+
+bool CandidateKeyFilter::filterAcceptsRow (int candidate_row, QModelIndex const& candidate_parent) const
+{
+ if (!m_->referencing_) // many to many passes all
+ {
+ return true;
+ }
+
+ auto candidate_key = sourceModel ()->index (candidate_row, m_->referenced_key_column_, candidate_parent).data (m_->referenced_key_role_);
+
+ // Include the current key.
+ if (m_->active_key_.isValid () && candidate_key == m_->active_key_.data (m_->referencing_key_role_))
+ {
+ return true;
+ }
+
+ // Filter out any candidates already in the referencing key rows.
+ return m_->referencing_->match (m_->referencing_->index (0, m_->referencing_key_column_), m_->referencing_key_role_, candidate_key, 1, Qt::MatchExactly).isEmpty ();
+}
diff --git a/item_delegates/CandidateKeyFilter.hpp b/item_delegates/CandidateKeyFilter.hpp
new file mode 100644
index 00000000..27f1892b
--- /dev/null
+++ b/item_delegates/CandidateKeyFilter.hpp
@@ -0,0 +1,41 @@
+#ifndef CANDIDATE_KEY_FILTER_HPP_
+#define CANDIDATE_KEY_FILTER_HPP_
+
+#include <QSortFilterProxyModel>
+#include <QModelIndex>
+
+#include "pimpl_h.hpp"
+
+class QAbstractItemModel;
+
+class CandidateKeyFilter final
+ : public QSortFilterProxyModel
+{
+public:
+ explicit CandidateKeyFilter (QAbstractItemModel const * referenced_model
+ , int referenced_key_column
+ , QObject * parent = nullptr
+ , int referenced_key_role = Qt::EditRole);
+ explicit CandidateKeyFilter (QAbstractItemModel const * referenced_model
+ , QAbstractItemModel const * referencing_model
+ , int referenced_key_column
+ , int referencing_key_column
+ , QObject * parent = nullptr
+ , int referenced_key_role = Qt::EditRole
+ , int referencing_key_role = Qt::EditRole);
+ ~CandidateKeyFilter ();
+
+ // this key is not to be filtered, usually because we want to allow
+ // it since we are editing the row that contains it this it is valid
+ // even though it is in use
+ void set_active_key (QModelIndex const& index = QModelIndex {});
+
+protected:
+ bool filterAcceptsRow (int candidate_row, QModelIndex const& candidate_parent) const override;
+
+private:
+ class impl;
+ pimpl<impl> m_;
+};
+
+#endif
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
diff --git a/item_delegates/ForeignKeyDelegate.cpp b/item_delegates/ForeignKeyDelegate.cpp
new file mode 100644
index 00000000..20473816
--- /dev/null
+++ b/item_delegates/ForeignKeyDelegate.cpp
@@ -0,0 +1,62 @@
+#include "ForeignKeyDelegate.hpp"
+
+#include <QApplication>
+#include <QComboBox>
+#include <QFontMetrics>
+#include <QSize>
+#include <QStyleOptionViewItem>
+#include "CandidateKeyFilter.hpp"
+
+ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referenced_model
+ , int referenced_key_column
+ , QObject * parent
+ , int referenced_key_role)
+ : QStyledItemDelegate {parent}
+ , candidate_key_filter_ {new CandidateKeyFilter {referenced_model, referenced_key_column, nullptr, referenced_key_role}}
+{
+}
+
+ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referenced_model
+ , QAbstractItemModel const * referencing_model
+ , int referenced_key_column
+ , int referencing_key_column
+ , QObject * parent
+ , int referenced_key_role
+ , int referencing_key_role)
+ : QStyledItemDelegate {parent}
+ , candidate_key_filter_ {new CandidateKeyFilter {referenced_model, referencing_model, referenced_key_column, referencing_key_column, nullptr, referenced_key_role, referencing_key_role}}
+{
+}
+
+ForeignKeyDelegate::~ForeignKeyDelegate ()
+{
+}
+
+QWidget * ForeignKeyDelegate::createEditor (QWidget * parent
+ , QStyleOptionViewItem const& /* option */
+ , QModelIndex const& index) const
+{
+ auto editor = new QComboBox {parent};
+ editor->setFrame (false);
+ candidate_key_filter_->set_active_key (index);
+ editor->setModel (candidate_key_filter_.data ());
+ editor->setSizeAdjustPolicy (QComboBox::AdjustToContents);
+ return editor;
+}
+
+QSize ForeignKeyDelegate::sizeHint (QStyleOptionViewItem const& option, QModelIndex const& index) const
+{
+ auto size_hint = QStyledItemDelegate::sizeHint (option, index);
+ QFontMetrics metrics {option.font};
+ QStyleOptionComboBox combo_box_option;
+ combo_box_option.rect = option.rect;
+ combo_box_option.state = option.state | QStyle::State_Enabled;
+ for (auto row = 0; row < candidate_key_filter_->rowCount (); ++row)
+ {
+ size_hint = size_hint.expandedTo (qApp->style ()->sizeFromContents (QStyle::CT_ComboBox
+ , &combo_box_option
+ , {metrics.width (candidate_key_filter_->data (candidate_key_filter_->index (row, 0)).toString ())
+ , metrics.height ()}));
+ }
+ return size_hint;
+}
diff --git a/item_delegates/ForeignKeyDelegate.hpp b/item_delegates/ForeignKeyDelegate.hpp
new file mode 100644
index 00000000..4f58f460
--- /dev/null
+++ b/item_delegates/ForeignKeyDelegate.hpp
@@ -0,0 +1,43 @@
+#ifndef FOREIGN_KEY_DELEGATE_HPP_
+#define FOREIGN_KEY_DELEGATE_HPP_
+
+#include <QStyledItemDelegate>
+#include <QScopedPointer>
+
+class CandidateKeyFilter;
+
+//
+// Class ForeignKeyDelegate
+//
+// Item delegate for editing a foreign key item in a one or many
+// to one relationship. A QComboBox is used as an item delegate
+// for the edit role.
+//
+class ForeignKeyDelegate final
+ : public QStyledItemDelegate
+{
+public:
+ // many to many relationship
+ explicit ForeignKeyDelegate (QAbstractItemModel const * referenced_model
+ , int referenced_key_column
+ , QObject * parent = nullptr
+ , int referenced_key_role = Qt::EditRole);
+
+ // one to many (referenced to referencing) relationship
+ explicit ForeignKeyDelegate (QAbstractItemModel const * referenced_model
+ , QAbstractItemModel const * referencing_model
+ , int referenced_key_column
+ , int referencing_key_column
+ , QObject * parent = nullptr
+ , int referenced_key_role = Qt::EditRole
+ , int referencing_key_role = Qt::EditRole);
+ ~ForeignKeyDelegate ();
+
+private:
+ QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override;
+ QSize sizeHint (QStyleOptionViewItem const&, QModelIndex const&) const override;
+
+ QScopedPointer<CandidateKeyFilter> candidate_key_filter_;
+};
+
+#endif
diff --git a/item_delegates/MaidenheadLocatorDelegate.cpp b/item_delegates/MaidenheadLocatorDelegate.cpp
new file mode 100644
index 00000000..7768f2a8
--- /dev/null
+++ b/item_delegates/MaidenheadLocatorDelegate.cpp
@@ -0,0 +1,30 @@
+#include "MaidenheadLocatorDelegate.hpp"
+
+#include <QLineEdit>
+
+#include "validators/MaidenheadLocatorValidator.hpp"
+
+MaidenheadLocatorDelegate::MaidenheadLocatorDelegate (QObject * parent)
+ : QStyledItemDelegate {parent}
+ , validator_ {new MaidenheadLocatorValidator}
+{
+}
+
+QWidget * MaidenheadLocatorDelegate::createEditor (QWidget * parent, QStyleOptionViewItem const&
+ , QModelIndex const&) const
+{
+ auto * editor = new QLineEdit {parent};
+ editor->setFrame (false);
+ editor->setValidator (validator_.data ());
+ return editor;
+}
+
+void MaidenheadLocatorDelegate::setEditorData (QWidget * editor, QModelIndex const& index) const
+{
+ static_cast<QLineEdit *> (editor)->setText (index.model ()->data (index, Qt::EditRole).toString ());
+}
+
+void MaidenheadLocatorDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, QModelIndex const& index) const
+{
+ model->setData (index, static_cast<QLineEdit *> (editor)->text (), Qt::EditRole);
+}
diff --git a/item_delegates/MaidenheadLocatorDelegate.hpp b/item_delegates/MaidenheadLocatorDelegate.hpp
new file mode 100644
index 00000000..39627523
--- /dev/null
+++ b/item_delegates/MaidenheadLocatorDelegate.hpp
@@ -0,0 +1,30 @@
+#ifndef MAIDENHEAD_LOCATOR_DELEGATE_HPP_
+#define MAIDENHEAD_LOCATOR_DELEGATE_HPP_
+
+#include <QStyledItemDelegate>
+
+class QValidator;
+
+//
+// Class MaidenheadLocatorDelegate
+//
+// Item delegate for editing a Maidenhead locator
+//
+class MaidenheadLocatorDelegate final
+ : public QStyledItemDelegate
+{
+public:
+ explicit MaidenheadLocatorDelegate (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;
+ void updateEditorGeometry (QWidget * editor, QStyleOptionViewItem const& option, QModelIndex const&) const override
+ {
+ editor->setGeometry (option.rect);
+ }
+
+private:
+ QScopedPointer<QValidator> validator_;
+};
+
+#endif
diff --git a/item_delegates/item_delegates.pri b/item_delegates/item_delegates.pri
new file mode 100644
index 00000000..153ad427
--- /dev/null
+++ b/item_delegates/item_delegates.pri
@@ -0,0 +1,12 @@
+SOURCES += \
+ item_delegates/ForeignKeyDelegate.cpp \
+ item_delegates/FrequencyItemDelegate.cpp \
+ item_delegates/CallsignDelegate.cpp \
+ item_delegates/MaidenheadLocatorItemDelegate.cpp
+
+HEADERS += \
+ item_delegates/ForeignKeyDelegate.hpp \
+ item_delegates/FrequencyItemDelegate.hpp \
+ item_delegates/CallsignDelegate.hpp \
+ item_delegates/MaidenheadLocatorDelegate.hpp \
+ item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp