Add some item delagate classes useful for database fields

This commit is contained in:
Bill Somerville 2018-11-12 02:00:35 +00:00
parent 21bb985d65
commit 97431d8154
7 changed files with 189 additions and 2 deletions

View File

@ -266,6 +266,8 @@ set (wsjt_qt_CXXSRCS
widgets/DecodeHighlightingListView.cpp
models/FoxLog.cpp
widgets/FoxLogWindow.cpp
item_delegates/CallsignDelegate.cpp
item_delegates/MaidenheadLocatorDelegate.cpp
)
set (wsjt_qtmm_CXXSRCS

View File

@ -0,0 +1,30 @@
#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);
}

View File

@ -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

View File

@ -0,0 +1,60 @@
#ifndef DATE_TIME_AS_SECS_SINCE_EPOCH_ITEM_DELEGATE_HPP_
#define DATE_TIME_AS_SECS_SINCE_EPOCH_ITEM_DELEGATE_HPP_
#include <QStyledItemDelegate>
#include <QVariant>
#include <QDateTime>
#include <QAbstractItemModel>
#include <QDateTimeEdit>
class DateTimeAsSecsSinceEpochItemDelegate final
: public QStyledItemDelegate
{
public:
DateTimeAsSecsSinceEpochItemDelegate (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<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

View File

@ -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);
}

View File

@ -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

View File

@ -1,7 +1,12 @@
SOURCES += \
item_delegates/ForeignKeyDelegate.cpp \
item_delegates/FrequencyItemDelegate.cpp
item_delegates/FrequencyItemDelegate.cpp \
item_delegates/CallsignDelegate.cpp \
item_delegates/MaidenheadLocatorItemDelegate.cpp
HEADERS += \
item_delegates/ForeignKeyDelegate.hpp \
item_delegates/FrequencyItemDelegate.hpp
item_delegates/FrequencyItemDelegate.hpp \
item_delegates/CallsignDelegate.hpp \
item_delegates/MaidenheadLocatorDelegate.hpp \
item_delegates/DateTimeAsSecsSinceEpochDelegate.hpp