Ensure all model proxy caches are flushed before access

This fixes a defect where station detail changes are not saved.

The Qt sort and filter proxy models utilize an item cache that must be
flushed by  callig submit() before  accessing the underlying  model if
the proxy model has been used for updates.

Also  separated  the   item  model  candidate  key   filter  from  the
implementation  internals of  the foreign  key item  delegate so  that
candidate key filtered models can be used directly as view models.

Make the insert new station details band combo box use a candidate key
filtered item model to avoid constraint violations. Constraint is zero
or one station records per band.

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@5161 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
Bill Somerville 2015-04-06 01:57:47 +00:00
parent 868ce60f7e
commit f7b0d3904a
10 changed files with 1260 additions and 1181 deletions

View File

@ -184,6 +184,7 @@ set (wsjt_qt_CXXSRCS
StationList.cpp
FrequencyLineEdit.cpp
FrequencyItemDelegate.cpp
CandidateKeyFilter.cpp
ForeignKeyDelegate.cpp
LiveFrequencyValidator.cpp
GetUserId.cpp

70
CandidateKeyFilter.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "CandidateKeyFilter.hpp"
#include <QModelIndex>
#include <QAbstractItemModel>
#include "pimpl_impl.hpp"
class CandidateKeyFilter::impl final
{
public:
explicit impl (QAbstractItemModel const * referencing_model
, int referencing_key_column
, int referenced_key_column
, int referencing_key_role
, int referenced_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 * referencing_model
, QAbstractItemModel * referenced_model
, int referencing_key_column
, int referenced_key_column
, int referencing_key_role
, int referenced_key_role)
: QSortFilterProxyModel {nullptr} // ForeignKeyDelegate owns us
, m_ {referencing_model, referencing_key_column, referenced_key_column, referencing_key_role, referenced_key_role}
{
setSourceModel (referenced_model);
}
CandidateKeyFilter::~CandidateKeyFilter ()
{
}
void CandidateKeyFilter::set_active_key (QModelIndex const& index)
{
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
{
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 ();
}

36
CandidateKeyFilter.hpp Normal file
View File

@ -0,0 +1,36 @@
#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 * referencing_model
, QAbstractItemModel * referenced_model
, int referencing_key_column = 0
, int referenced_key_column = 0
, int referencing_key_role = Qt::EditRole
, int referenced_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

View File

@ -155,12 +155,14 @@
#include <QFontDialog>
#include <QColorDialog>
#include <QSerialPortInfo>
#include <QScopedPointer>
#include <QDebug>
#include "qt_helpers.hpp"
#include "SettingsGroup.hpp"
#include "FrequencyLineEdit.hpp"
#include "FrequencyItemDelegate.hpp"
#include "CandidateKeyFilter.hpp"
#include "ForeignKeyDelegate.hpp"
#include "TransceiverFactory.hpp"
#include "Transceiver.hpp"
@ -240,13 +242,13 @@ class StationDialog final
: public QDialog
{
public:
explicit StationDialog (Bands * bands, QWidget * parent = nullptr)
explicit StationDialog (StationList const * stations, Bands * bands, QWidget * parent = nullptr)
: QDialog {parent}
, bands_ {bands}
, filtered_bands_ {new CandidateKeyFilter {stations, bands}}
{
setWindowTitle (QApplication::applicationName () + " - " + tr ("Add Station"));
band_.setModel (bands_);
band_.setModel (filtered_bands_.data ());
auto form_layout = new QFormLayout ();
form_layout->addRow (tr ("&Band:"), &band_);
@ -273,8 +275,14 @@ public:
return {band_.currentText (), delta_.frequency_delta (), description_.text ()};
}
int exec () override
{
filtered_bands_->set_active_key ();
return QDialog::exec ();
}
private:
Bands * bands_;
QScopedPointer<CandidateKeyFilter> filtered_bands_;
QComboBox band_;
FrequencyDeltaLineEdit delta_;
@ -724,7 +732,7 @@ Configuration::impl::impl (Configuration * self, QSettings * settings, QWidget *
, stations_ {&bands_}
, next_stations_ {&bands_}
, frequency_dialog_ {new FrequencyDialog {this}}
, station_dialog_ {new StationDialog {&bands_, this}}
, station_dialog_ {new StationDialog {&next_stations_, &bands_, this}}
, rig_active_ {false}
, have_rig_ {false}
, rig_changed_ {false}
@ -922,7 +930,7 @@ Configuration::impl::impl (Configuration * self, QSettings * settings, QWidget *
ui_->stations_table_view->setModel (&next_stations_);
ui_->stations_table_view->sortByColumn (0, Qt::AscendingOrder);
ui_->stations_table_view->setColumnWidth (1, 150);
ui_->stations_table_view->setItemDelegateForColumn (0, new ForeignKeyDelegate {&next_stations_, &bands_, 0, this});
ui_->stations_table_view->setItemDelegateForColumn (0, new ForeignKeyDelegate {&next_stations_, &bands_, 0, 0, this});
ui_->stations_table_view->setItemDelegateForColumn (1, new FrequencyDeltaItemDelegate {this});
station_delete_action_ = new QAction {tr ("&Delete"), ui_->stations_table_view};

View File

@ -1,77 +1,32 @@
#include "ForeignKeyDelegate.hpp"
#include <QComboBox>
#include <QSortFilterProxyModel>
class CandidateKeyFilter final
: public QSortFilterProxyModel
{
public:
explicit CandidateKeyFilter (QAbstractItemModel const * referencing_model
, QAbstractItemModel * referenced_model
, int referenced_key_column
, int referencing_key_role
, int referenced_key_role)
: QSortFilterProxyModel {nullptr} // ForeignKeyDelegate owns us
, referencing_ {referencing_model}
, referencing_key_role_ {referencing_key_role}
, referenced_key_column_ {referenced_key_column}
, referenced_key_role_ {referenced_key_role}
{
setSourceModel (referenced_model);
}
void set_active_key (QModelIndex const& index)
{
active_key_ = index;
invalidateFilter ();
}
protected:
bool filterAcceptsRow (int candidate_row, QModelIndex const& candidate_parent) const override
{
auto candidate_key = sourceModel ()->index (candidate_row, referenced_key_column_, candidate_parent).data (referenced_key_role_);
// Include the current key.
if (candidate_key == active_key_.data (referencing_key_role_))
{
return true;
}
// Filter out any candidates already in the referencing key rows.
return referencing_->match (referencing_->index (0, active_key_.column ()), referencing_key_role_, candidate_key, 1, Qt::MatchExactly).isEmpty ();
}
private:
QAbstractItemModel const * referencing_;
int referencing_key_role_;
int referenced_key_column_;
int referenced_key_role_;
QModelIndex active_key_;
};
ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referencing_model
, QAbstractItemModel * referenced_model
, int referenced_key_column
, QObject * parent
, int referencing_key_role
, int referenced_key_role)
: QStyledItemDelegate {parent}
, candidate_key_filter_ {new CandidateKeyFilter {referencing_model, referenced_model, referenced_key_column, referencing_key_role, referenced_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 ());
return editor;
}
#include "ForeignKeyDelegate.hpp"
#include <QComboBox>
#include "CandidateKeyFilter.hpp"
ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referencing_model
, QAbstractItemModel * referenced_model
, int referencing_key_column
, int referenced_key_column
, QObject * parent
, int referencing_key_role
, int referenced_key_role)
: QStyledItemDelegate {parent}
, candidate_key_filter_ {new CandidateKeyFilter {referencing_model, referenced_model, referencing_key_column, referenced_key_column, referencing_key_role, referenced_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 ());
return editor;
}

View File

@ -1,34 +1,35 @@
#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:
explicit ForeignKeyDelegate (QAbstractItemModel const * referencing_model
, QAbstractItemModel * referenced_model
, int referenced_key_column = 0
, QObject * parent = nullptr
, int referencing_key_role = Qt::EditRole
, int referenced_key_role = Qt::EditRole);
~ForeignKeyDelegate ();
QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override;
private:
QScopedPointer<CandidateKeyFilter> candidate_key_filter_;
};
#endif
#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:
explicit ForeignKeyDelegate (QAbstractItemModel const * referencing_model
, QAbstractItemModel * referenced_model
, int referencing_key_column = 0
, int referenced_key_column = 0
, QObject * parent = nullptr
, int referencing_key_role = Qt::EditRole
, int referenced_key_role = Qt::EditRole);
~ForeignKeyDelegate ();
QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override;
private:
QScopedPointer<CandidateKeyFilter> candidate_key_filter_;
};
#endif

View File

@ -1,358 +1,359 @@
#include "FrequencyList.hpp"
#include <utility>
#include <QAbstractTableModel>
#include <QString>
#include <QList>
#include <QListIterator>
#include <QVector>
#include <QStringList>
#include <QMimeData>
#include <QDataStream>
#include <QByteArray>
#include <QDebug>
#include "pimpl_impl.hpp"
class FrequencyList::impl final
: public QAbstractTableModel
{
public:
impl (Frequencies frequencies, QObject * parent)
: QAbstractTableModel {parent}
, frequencies_ {frequencies}
{
}
Frequencies const& frequencies () const {return frequencies_;}
void assign (Frequencies);
QModelIndex add (Frequency);
protected:
// Implement the QAbstractTableModel interface
int rowCount (QModelIndex const& parent = QModelIndex {}) const override;
int columnCount (QModelIndex const& parent = QModelIndex {}) const override;
Qt::ItemFlags flags (QModelIndex const& = QModelIndex {}) const override;
QVariant data (QModelIndex const&, int role = Qt::DisplayRole) const override;
bool setData (QModelIndex const&, QVariant const& value, int role = Qt::EditRole) override;
QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override;
bool removeRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override;
bool insertRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override;
QStringList mimeTypes () const override;
QMimeData * mimeData (QModelIndexList const&) const override;
private:
static int constexpr num_cols {2};
static auto constexpr mime_type ="application/wsjt.Frequencies";
Frequencies frequencies_;
};
FrequencyList::FrequencyList (QObject * parent)
: FrequencyList {{}, parent}
{
}
FrequencyList::FrequencyList (Frequencies frequencies, QObject * parent)
: QSortFilterProxyModel {parent}
, m_ {frequencies, parent}
{
// setDynamicSortFilter (true);
setSourceModel (&*m_);
setSortRole (SortRole);
}
FrequencyList::~FrequencyList ()
{
}
FrequencyList& FrequencyList::operator = (Frequencies frequencies)
{
m_->assign (frequencies);
return *this;
}
auto FrequencyList::frequencies () const -> Frequencies
{
return m_->frequencies ();
}
QModelIndex FrequencyList::add (Frequency f)
{
return mapFromSource (m_->add (f));
}
bool FrequencyList::remove (Frequency f)
{
auto row = m_->frequencies ().indexOf (f);
if (0 > row)
{
return false;
}
return m_->removeRow (row);
}
namespace
{
bool row_is_higher (QModelIndex const& lhs, QModelIndex const& rhs)
{
return lhs.row () > rhs.row ();
}
}
bool FrequencyList::removeDisjointRows (QModelIndexList rows)
{
bool result {true};
// We must work with source model indexes because we don't want row
// removes to invalidate model indexes we haven't yet processed. We
// achieve that by processing them in decending row order.
for (int r = 0; r < rows.size (); ++r)
{
rows[r] = mapToSource (rows[r]);
}
// reverse sort by row
qSort (rows.begin (), rows.end (), row_is_higher);
Q_FOREACH (auto index, rows)
{
if (result && !m_->removeRow (index.row ()))
{
result = false;
}
}
return result;
}
void FrequencyList::impl::assign (Frequencies frequencies)
{
beginResetModel ();
std::swap (frequencies_, frequencies);
endResetModel ();
}
QModelIndex FrequencyList::impl::add (Frequency f)
{
// Any Frequency that isn't in the list may be added
if (!frequencies_.contains (f))
{
auto row = frequencies_.size ();
beginInsertRows (QModelIndex {}, row, row);
frequencies_.append (f);
endInsertRows ();
return index (row, 0);
}
return QModelIndex {};
}
int FrequencyList::impl::rowCount (QModelIndex const& parent) const
{
return parent.isValid () ? 0 : frequencies_.size ();
}
int FrequencyList::impl::columnCount (QModelIndex const& parent) const
{
return parent.isValid () ? 0 : num_cols;
}
Qt::ItemFlags FrequencyList::impl::flags (QModelIndex const& index) const
{
auto result = QAbstractTableModel::flags (index) | Qt::ItemIsDropEnabled;
auto row = index.row ();
auto column = index.column ();
if (index.isValid ()
&& row < frequencies_.size ()
&& column < num_cols)
{
switch (column)
{
case 0:
result |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
break;
case 1:
result |= Qt::ItemIsDragEnabled;
break;
}
}
return result;
}
QVariant FrequencyList::impl::data (QModelIndex const& index, int role) const
{
QVariant item;
auto row = index.row ();
auto column = index.column ();
if (index.isValid ()
&& row < frequencies_.size ()
&& column < num_cols)
{
auto frequency = frequencies_.at (row);
switch (column)
{
case 0:
switch (role)
{
case SortRole:
case Qt::DisplayRole:
case Qt::EditRole:
case Qt::AccessibleTextRole:
item = frequency;
break;
case Qt::ToolTipRole:
case Qt::AccessibleDescriptionRole:
item = tr ("Frequency");
break;
case Qt::TextAlignmentRole:
item = Qt::AlignRight + Qt::AlignVCenter;
break;
}
break;
case 1:
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
case Qt::AccessibleTextRole:
item = static_cast<double> (frequency / 1.e6);
break;
case SortRole: // use the underlying Frequency value
item = frequency;
break;
case Qt::ToolTipRole:
case Qt::AccessibleDescriptionRole:
item = tr ("Frequency MHz");
break;
case Qt::TextAlignmentRole:
item = Qt::AlignRight + Qt::AlignVCenter;
break;
}
break;
}
}
return item;
}
bool FrequencyList::impl::setData (QModelIndex const& model_index, QVariant const& value, int role)
{
bool changed {false};
auto row = model_index.row ();
if (model_index.isValid ()
&& Qt::EditRole == role
&& row < frequencies_.size ()
&& 0 == model_index.column ()
&& value.canConvert<Frequency> ())
{
auto frequency = value.value<Frequency> ();
auto original_frequency = frequencies_.at (row);
if (frequency != original_frequency)
{
frequencies_.replace (row, frequency);
Q_EMIT dataChanged (model_index, index (model_index.row (), 1), QVector<int> {} << role);
}
changed = true;
}
return changed;
}
QVariant FrequencyList::impl::headerData (int section, Qt::Orientation orientation, int role) const
{
QVariant header;
if (Qt::DisplayRole == role
&& Qt::Horizontal == orientation
&& section < num_cols)
{
switch (section)
{
case 0: header = tr ("Frequency"); break;
case 1: header = tr ("Frequency (MHz)"); break;
}
}
else
{
header = QAbstractTableModel::headerData (section, orientation, role);
}
return header;
}
bool FrequencyList::impl::removeRows (int row, int count, QModelIndex const& parent)
{
if (0 < count && (row + count) <= rowCount (parent))
{
beginRemoveRows (parent, row, row + count - 1);
for (auto r = 0; r < count; ++r)
{
frequencies_.removeAt (row);
}
endRemoveRows ();
return true;
}
return false;
}
bool FrequencyList::impl::insertRows (int row, int count, QModelIndex const& parent)
{
if (0 < count)
{
beginInsertRows (parent, row, row + count - 1);
for (auto r = 0; r < count; ++r)
{
frequencies_.insert (row, Frequency {});
}
endInsertRows ();
return true;
}
return false;
}
QStringList FrequencyList::impl::mimeTypes () const
{
QStringList types;
types << mime_type;
return types;
}
QMimeData * FrequencyList::impl::mimeData (QModelIndexList const& items) const
{
QMimeData * mime_data = new QMimeData {};
QByteArray encoded_data;
QDataStream stream {&encoded_data, QIODevice::WriteOnly};
Q_FOREACH (auto const& item, items)
{
if (item.isValid ())
{
stream << QString {data (item, Qt::DisplayRole).toString ()};
}
}
mime_data->setData (mime_type, encoded_data);
return mime_data;
}
#include "FrequencyList.hpp"
#include <utility>
#include <QAbstractTableModel>
#include <QString>
#include <QList>
#include <QListIterator>
#include <QVector>
#include <QStringList>
#include <QMimeData>
#include <QDataStream>
#include <QByteArray>
#include <QDebug>
#include "pimpl_impl.hpp"
class FrequencyList::impl final
: public QAbstractTableModel
{
public:
impl (Frequencies frequencies, QObject * parent)
: QAbstractTableModel {parent}
, frequencies_ {frequencies}
{
}
Frequencies const& frequencies () const {return frequencies_;}
void assign (Frequencies);
QModelIndex add (Frequency);
protected:
// Implement the QAbstractTableModel interface
int rowCount (QModelIndex const& parent = QModelIndex {}) const override;
int columnCount (QModelIndex const& parent = QModelIndex {}) const override;
Qt::ItemFlags flags (QModelIndex const& = QModelIndex {}) const override;
QVariant data (QModelIndex const&, int role = Qt::DisplayRole) const override;
bool setData (QModelIndex const&, QVariant const& value, int role = Qt::EditRole) override;
QVariant headerData (int section, Qt::Orientation, int = Qt::DisplayRole) const override;
bool removeRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override;
bool insertRows (int row, int count, QModelIndex const& parent = QModelIndex {}) override;
QStringList mimeTypes () const override;
QMimeData * mimeData (QModelIndexList const&) const override;
private:
static int constexpr num_cols {2};
static auto constexpr mime_type ="application/wsjt.Frequencies";
Frequencies frequencies_;
};
FrequencyList::FrequencyList (QObject * parent)
: FrequencyList {{}, parent}
{
}
FrequencyList::FrequencyList (Frequencies frequencies, QObject * parent)
: QSortFilterProxyModel {parent}
, m_ {frequencies, parent}
{
// setDynamicSortFilter (true);
setSourceModel (&*m_);
setSortRole (SortRole);
}
FrequencyList::~FrequencyList ()
{
}
FrequencyList& FrequencyList::operator = (Frequencies frequencies)
{
m_->assign (frequencies);
return *this;
}
auto FrequencyList::frequencies () -> Frequencies
{
submit ();
return m_->frequencies ();
}
QModelIndex FrequencyList::add (Frequency f)
{
return mapFromSource (m_->add (f));
}
bool FrequencyList::remove (Frequency f)
{
auto row = m_->frequencies ().indexOf (f);
if (0 > row)
{
return false;
}
return m_->removeRow (row);
}
namespace
{
bool row_is_higher (QModelIndex const& lhs, QModelIndex const& rhs)
{
return lhs.row () > rhs.row ();
}
}
bool FrequencyList::removeDisjointRows (QModelIndexList rows)
{
bool result {true};
// We must work with source model indexes because we don't want row
// removes to invalidate model indexes we haven't yet processed. We
// achieve that by processing them in decending row order.
for (int r = 0; r < rows.size (); ++r)
{
rows[r] = mapToSource (rows[r]);
}
// reverse sort by row
qSort (rows.begin (), rows.end (), row_is_higher);
Q_FOREACH (auto index, rows)
{
if (result && !m_->removeRow (index.row ()))
{
result = false;
}
}
return result;
}
void FrequencyList::impl::assign (Frequencies frequencies)
{
beginResetModel ();
std::swap (frequencies_, frequencies);
endResetModel ();
}
QModelIndex FrequencyList::impl::add (Frequency f)
{
// Any Frequency that isn't in the list may be added
if (!frequencies_.contains (f))
{
auto row = frequencies_.size ();
beginInsertRows (QModelIndex {}, row, row);
frequencies_.append (f);
endInsertRows ();
return index (row, 0);
}
return QModelIndex {};
}
int FrequencyList::impl::rowCount (QModelIndex const& parent) const
{
return parent.isValid () ? 0 : frequencies_.size ();
}
int FrequencyList::impl::columnCount (QModelIndex const& parent) const
{
return parent.isValid () ? 0 : num_cols;
}
Qt::ItemFlags FrequencyList::impl::flags (QModelIndex const& index) const
{
auto result = QAbstractTableModel::flags (index) | Qt::ItemIsDropEnabled;
auto row = index.row ();
auto column = index.column ();
if (index.isValid ()
&& row < frequencies_.size ()
&& column < num_cols)
{
switch (column)
{
case 0:
result |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
break;
case 1:
result |= Qt::ItemIsDragEnabled;
break;
}
}
return result;
}
QVariant FrequencyList::impl::data (QModelIndex const& index, int role) const
{
QVariant item;
auto row = index.row ();
auto column = index.column ();
if (index.isValid ()
&& row < frequencies_.size ()
&& column < num_cols)
{
auto frequency = frequencies_.at (row);
switch (column)
{
case 0:
switch (role)
{
case SortRole:
case Qt::DisplayRole:
case Qt::EditRole:
case Qt::AccessibleTextRole:
item = frequency;
break;
case Qt::ToolTipRole:
case Qt::AccessibleDescriptionRole:
item = tr ("Frequency");
break;
case Qt::TextAlignmentRole:
item = Qt::AlignRight + Qt::AlignVCenter;
break;
}
break;
case 1:
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
case Qt::AccessibleTextRole:
item = static_cast<double> (frequency / 1.e6);
break;
case SortRole: // use the underlying Frequency value
item = frequency;
break;
case Qt::ToolTipRole:
case Qt::AccessibleDescriptionRole:
item = tr ("Frequency MHz");
break;
case Qt::TextAlignmentRole:
item = Qt::AlignRight + Qt::AlignVCenter;
break;
}
break;
}
}
return item;
}
bool FrequencyList::impl::setData (QModelIndex const& model_index, QVariant const& value, int role)
{
bool changed {false};
auto row = model_index.row ();
if (model_index.isValid ()
&& Qt::EditRole == role
&& row < frequencies_.size ()
&& 0 == model_index.column ()
&& value.canConvert<Frequency> ())
{
auto frequency = value.value<Frequency> ();
auto original_frequency = frequencies_.at (row);
if (frequency != original_frequency)
{
frequencies_.replace (row, frequency);
Q_EMIT dataChanged (model_index, index (model_index.row (), 1), QVector<int> {} << role);
}
changed = true;
}
return changed;
}
QVariant FrequencyList::impl::headerData (int section, Qt::Orientation orientation, int role) const
{
QVariant header;
if (Qt::DisplayRole == role
&& Qt::Horizontal == orientation
&& section < num_cols)
{
switch (section)
{
case 0: header = tr ("Frequency"); break;
case 1: header = tr ("Frequency (MHz)"); break;
}
}
else
{
header = QAbstractTableModel::headerData (section, orientation, role);
}
return header;
}
bool FrequencyList::impl::removeRows (int row, int count, QModelIndex const& parent)
{
if (0 < count && (row + count) <= rowCount (parent))
{
beginRemoveRows (parent, row, row + count - 1);
for (auto r = 0; r < count; ++r)
{
frequencies_.removeAt (row);
}
endRemoveRows ();
return true;
}
return false;
}
bool FrequencyList::impl::insertRows (int row, int count, QModelIndex const& parent)
{
if (0 < count)
{
beginInsertRows (parent, row, row + count - 1);
for (auto r = 0; r < count; ++r)
{
frequencies_.insert (row, Frequency {});
}
endInsertRows ();
return true;
}
return false;
}
QStringList FrequencyList::impl::mimeTypes () const
{
QStringList types;
types << mime_type;
return types;
}
QMimeData * FrequencyList::impl::mimeData (QModelIndexList const& items) const
{
QMimeData * mime_data = new QMimeData {};
QByteArray encoded_data;
QDataStream stream {&encoded_data, QIODevice::WriteOnly};
Q_FOREACH (auto const& item, items)
{
if (item.isValid ())
{
stream << QString {data (item, Qt::DisplayRole).toString ()};
}
}
mime_data->setData (mime_type, encoded_data);
return mime_data;
}

View File

@ -1,59 +1,59 @@
#ifndef FREQUENCY_LIST_HPP__
#define FREQUENCY_LIST_HPP__
#include "pimpl_h.hpp"
#include <QSortFilterProxyModel>
#include "Radio.hpp"
//
// Class FrequencyList
//
// Encapsulates a collection of frequencies. The implementation is a
// table containing the list of Frequency type elements which is
// editable and a second column which is an immutable double
// representation of the corresponding Frequency item scaled to
// mega-Hertz.
//
// The list is ordered.
//
// Responsibilities
//
// Stores internally a list of unique frequencies. Provides methods
// to add and delete list elements.
//
// Collaborations
//
// Implements the QSortFilterProxyModel interface for a list of spot
// frequencies.
//
class FrequencyList final
: public QSortFilterProxyModel
{
public:
using Frequency = Radio::Frequency;
using Frequencies = Radio::Frequencies;
explicit FrequencyList (QObject * parent = nullptr);
explicit FrequencyList (Frequencies, QObject * parent = nullptr);
~FrequencyList ();
// Load and store contents
FrequencyList& operator = (Frequencies);
Frequencies frequencies () const;
// Model API
QModelIndex add (Frequency);
bool remove (Frequency);
bool removeDisjointRows (QModelIndexList);
// Custom roles.
static int constexpr SortRole = Qt::UserRole;
private:
class impl;
pimpl<impl> m_;
};
#endif
#ifndef FREQUENCY_LIST_HPP__
#define FREQUENCY_LIST_HPP__
#include "pimpl_h.hpp"
#include <QSortFilterProxyModel>
#include "Radio.hpp"
//
// Class FrequencyList
//
// Encapsulates a collection of frequencies. The implementation is a
// table containing the list of Frequency type elements which is
// editable and a second column which is an immutable double
// representation of the corresponding Frequency item scaled to
// mega-Hertz.
//
// The list is ordered.
//
// Responsibilities
//
// Stores internally a list of unique frequencies. Provides methods
// to add and delete list elements.
//
// Collaborations
//
// Implements the QSortFilterProxyModel interface for a list of spot
// frequencies.
//
class FrequencyList final
: public QSortFilterProxyModel
{
public:
using Frequency = Radio::Frequency;
using Frequencies = Radio::Frequencies;
explicit FrequencyList (QObject * parent = nullptr);
explicit FrequencyList (Frequencies, QObject * parent = nullptr);
~FrequencyList ();
// Load and store contents
FrequencyList& operator = (Frequencies);
Frequencies frequencies ();
// Model API
QModelIndex add (Frequency);
bool remove (Frequency);
bool removeDisjointRows (QModelIndexList);
// Custom roles.
static int constexpr SortRole = Qt::UserRole;
private:
class impl;
pimpl<impl> m_;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,96 +1,102 @@
#ifndef STATION_LIST_HPP__
#define STATION_LIST_HPP__
#include <QSortFilterProxyModel>
#include <QString>
#include <QList>
#include "pimpl_h.hpp"
#include "Radio.hpp"
class Bands;
//
// Class StationList
//
// Encapsulates information about a collection of unique operating
// stations per band. The implementation is a table model with the
// first column being the unique (within the table rows) band name
// and, the second the frequency offset for transverter usage and,
// the third the antenna description. All are editable.
//
// Responsibilities
//
// Stores internally an unordered table of bands.
//
// If an ordered representaion is required then wrapping with an
// appropriate proxy model is sufficient
// e.g. QSortFilterProxyModel. A custom SortRole role is provided for
// the band name column which returns a numeric value (Bands lower
// frequency limit) which gives a strict frequency ordering by band.
//
// Collaborations
//
// Implements the QAbstractTableModel interface for a grid of bands
// with offset frequencies and antenna descriptions.
//
// Uses the QAbstractItemModel interface of the bands model to lookup
// band information.
//
class StationList final
: public QSortFilterProxyModel
{
public:
using Frequency = Radio::Frequency;
using FrequencyDelta = Radio::FrequencyDelta;
//
// Struct Station
//
// Aggregation of fields that describe a radio station on a band.
//
struct Station
{
QString band_name_;
FrequencyDelta offset_;
QString antenna_description_;
};
using Stations = QList<Station>;
explicit StationList (Bands const * bands, QObject * parent = nullptr);
explicit StationList (Bands const * bands, Stations, QObject * parent = nullptr);
~StationList ();
// Load and store contents.
StationList& operator = (Stations);
Stations stations () const;
//
// Model API
//
QModelIndex add (Station); // Add a new Station
bool remove (Station); // Remove a Station
bool removeDisjointRows (QModelIndexList); // Remove one or more stations
FrequencyDelta offset (Frequency) const; // Return the offset to be used for a Frequency
// Custom sort role.
static int constexpr SortRole = Qt::UserRole;
private:
class impl;
pimpl<impl> m_;
};
// Station equivalence is based on band name alone.
inline
bool operator == (StationList::Station const& lhs, StationList::Station const& rhs)
{
return lhs.band_name_ == rhs.band_name_;
}
Q_DECLARE_METATYPE (StationList::Station);
Q_DECLARE_METATYPE (StationList::Stations);
#endif
#ifndef STATION_LIST_HPP__
#define STATION_LIST_HPP__
#include <QSortFilterProxyModel>
#include <QString>
#include <QList>
#include "pimpl_h.hpp"
#include "Radio.hpp"
class Bands;
//
// Class StationList
//
// Encapsulates information about a collection of unique operating
// stations per band. The implementation is a table model with the
// first column being the unique (within the table rows) band name
// and, the second the frequency offset for transverter usage and,
// the third the antenna description. All are editable.
//
// Responsibilities
//
// Stores internally an unordered table of bands.
//
// If an ordered representaion is required then wrapping with an
// appropriate proxy model is sufficient
// e.g. QSortFilterProxyModel. A custom SortRole role is provided for
// the band name column which returns a numeric value (Bands lower
// frequency limit) which gives a strict frequency ordering by band.
//
// Collaborations
//
// Implements the QAbstractTableModel interface for a grid of bands
// with offset frequencies and antenna descriptions.
//
// Uses the QAbstractItemModel interface of the bands model to lookup
// band information.
//
class StationList final
: public QSortFilterProxyModel
{
public:
using Frequency = Radio::Frequency;
using FrequencyDelta = Radio::FrequencyDelta;
//
// Struct Station
//
// Aggregation of fields that describe a radio station on a band.
//
struct Station
{
QString band_name_;
FrequencyDelta offset_;
QString antenna_description_;
};
using Stations = QList<Station>;
explicit StationList (Bands const * bands, QObject * parent = nullptr);
explicit StationList (Bands const * bands, Stations, QObject * parent = nullptr);
~StationList ();
// Load and store contents.
StationList& operator = (Stations);
Stations stations ();
//
// Model API
//
QModelIndex add (Station); // Add a new Station
bool remove (Station); // Remove a Station
bool removeDisjointRows (QModelIndexList); // Remove one or more stations
FrequencyDelta offset (Frequency) const; // Return the offset to be used for a Frequency
// Custom sort role.
static int constexpr SortRole = Qt::UserRole;
private:
class impl;
pimpl<impl> m_;
};
// Station equivalence
inline
bool operator == (StationList::Station const& lhs, StationList::Station const& rhs)
{
return lhs.band_name_ == rhs.band_name_
&& lhs.offset_ == rhs.offset_
&& lhs.antenna_description_ == rhs.antenna_description_;
}
Q_DECLARE_METATYPE (StationList::Station);
Q_DECLARE_METATYPE (StationList::Stations);
#if !defined (QT_NO_DEBUG_STREAM)
QDebug operator << (QDebug debug, StationList::Station const&);
#endif
#endif