summaryrefslogtreecommitdiff
path: root/StationList.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'StationList.cpp')
-rw-r--r--StationList.cpp110
1 files changed, 48 insertions, 62 deletions
diff --git a/StationList.cpp b/StationList.cpp
index 94c09d65..cfd629ed 100644
--- a/StationList.cpp
+++ b/StationList.cpp
@@ -14,33 +14,23 @@
#include <QDataStream>
#include <QByteArray>
#include <QDebug>
+#include <QDebugStateSaver>
#include "pimpl_impl.hpp"
+#include "Radio.hpp"
#include "Bands.hpp"
-
-namespace
-{
- struct init
- {
- init ()
- {
- qRegisterMetaType<StationList::Station> ("Station");
- qRegisterMetaTypeStreamOperators<StationList::Station> ("Station");
- qRegisterMetaType<StationList::Stations> ("Stations");
- qRegisterMetaTypeStreamOperators<StationList::Stations> ("Stations");
- }
- } static_initializer;
-}
+#include "FrequencyList.hpp"
#if !defined (QT_NO_DEBUG_STREAM)
QDebug operator << (QDebug debug, StationList::Station const& station)
{
+ QDebugStateSaver saver {debug};
debug.nospace () << "Station("
<< station.band_name_ << ", "
<< station.offset_ << ", "
<< station.antenna_description_ << ')';
- return debug.space ();
+ return debug;
}
#endif
@@ -70,12 +60,10 @@ public:
{
}
- Stations const& stations () const {return stations_;}
- void assign (Stations);
+ Stations station_list (Stations);
QModelIndex add (Station);
FrequencyDelta offset (Frequency) const;
-protected:
// Implement the QAbstractTableModel interface.
int rowCount (QModelIndex const& parent = QModelIndex {}) const override;
int columnCount (QModelIndex const& parent = QModelIndex {}) const override;
@@ -90,7 +78,6 @@ protected:
QMimeData * mimeData (QModelIndexList const&) const override;
bool dropMimeData (QMimeData const *, Qt::DropAction, int row, int column, QModelIndex const& parent) override;
-private:
// Helper method for band validation.
QModelIndex first_matching_band (QString const& band_name) const
{
@@ -127,15 +114,14 @@ StationList::~StationList ()
{
}
-StationList& StationList::operator = (Stations stations)
+auto StationList::station_list (Stations stations) -> Stations
{
- m_->assign (stations);
- return *this;
+ return m_->station_list (stations);
}
-auto StationList::stations () const -> Stations
+auto StationList::station_list () const -> Stations const&
{
- return m_->stations ();
+ return m_->stations_;
}
QModelIndex StationList::add (Station s)
@@ -145,13 +131,11 @@ QModelIndex StationList::add (Station s)
bool StationList::remove (Station s)
{
- auto row = m_->stations ().indexOf (s);
-
+ auto row = m_->stations_.indexOf (s);
if (0 > row)
{
return false;
}
-
return removeRow (row);
}
@@ -194,11 +178,12 @@ auto StationList::offset (Frequency f) const -> FrequencyDelta
}
-void StationList::impl::assign (Stations stations)
+auto StationList::impl::station_list (Stations stations) -> Stations
{
beginResetModel ();
std::swap (stations_, stations);
endResetModel ();
+ return stations;
}
QModelIndex StationList::impl::add (Station s)
@@ -221,22 +206,19 @@ QModelIndex StationList::impl::add (Station s)
auto StationList::impl::offset (Frequency f) const -> FrequencyDelta
{
// Lookup band for frequency
- auto band_index = bands_->find (f);
- if (band_index.isValid ())
+ auto const& band = bands_->find (f);
+ if (!band.isEmpty ())
{
- auto band_name = band_index.data ().toString ();
-
// Lookup station for band
- for (int i = 0; i < stations ().size (); ++i)
+ for (int i = 0; i < stations_.size (); ++i)
{
- if (stations_[i].band_name_ == band_name)
+ if (stations_[i].band_name_ == band)
{
return stations_[i].offset_;
}
}
}
-
- return 0; // no offset
+ return 0; // no offset
}
int StationList::impl::rowCount (QModelIndex const& parent) const
@@ -260,7 +242,7 @@ Qt::ItemFlags StationList::impl::flags (QModelIndex const& index) const
&& row < stations_.size ()
&& column < num_columns)
{
- if (2 == column)
+ if (description_column == column)
{
result |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
@@ -289,7 +271,7 @@ QVariant StationList::impl::data (QModelIndex const& index, int role) const
{
switch (column)
{
- case 0: // band name
+ case band_column:
switch (role)
{
case SortRole:
@@ -318,19 +300,19 @@ QVariant StationList::impl::data (QModelIndex const& index, int role) const
}
break;
- case 1: // frequency offset
+ case offset_column:
{
auto frequency_offset = stations_.at (row).offset_;
switch (role)
{
+ case SortRole:
+ case Qt::EditRole:
case Qt::AccessibleTextRole:
item = frequency_offset;
break;
- case SortRole:
case Qt::DisplayRole:
- case Qt::EditRole:
- item = frequency_offset;
+ item = Radio::pretty_frequency_MHz_string (frequency_offset) + " MHz";
break;
case Qt::ToolTipRole:
@@ -345,7 +327,7 @@ QVariant StationList::impl::data (QModelIndex const& index, int role) const
}
break;
- case 2: // antenna description
+ case description_column:
switch (role)
{
case SortRole:
@@ -379,9 +361,9 @@ QVariant StationList::impl::headerData (int section, Qt::Orientation orientation
{
switch (section)
{
- case 0: header = tr ("Band"); break;
- case 1: header = tr ("Offset"); break;
- case 2: header = tr ("Antenna Description"); break;
+ case band_column: header = tr ("Band"); break;
+ case offset_column: header = tr ("Offset"); break;
+ case description_column: header = tr ("Antenna Description"); break;
}
}
else
@@ -407,7 +389,7 @@ bool StationList::impl::setData (QModelIndex const& model_index, QVariant const&
switch (model_index.column ())
{
- case 0:
+ case band_column:
{
// Check if band name is valid.
auto band_index = first_matching_band (value.toString ());
@@ -420,15 +402,22 @@ bool StationList::impl::setData (QModelIndex const& model_index, QVariant const&
}
break;
- case 1:
+ case offset_column:
{
- stations_[row].offset_ = value.value<FrequencyDelta> ();
- Q_EMIT dataChanged (model_index, model_index, roles);
- changed = true;
+ if (value.canConvert<FrequencyDelta> ())
+ {
+ FrequencyDelta offset {qvariant_cast<Radio::FrequencyDelta> (value)};
+ if (offset != stations_[row].offset_)
+ {
+ stations_[row].offset_ = offset;
+ Q_EMIT dataChanged (model_index, model_index, roles);
+ changed = true;
+ }
+ }
}
break;
- case 2:
+ case description_column:
stations_[row].antenna_description_ = value.toString ();
Q_EMIT dataChanged (model_index, model_index, roles);
changed = true;
@@ -508,9 +497,8 @@ bool StationList::impl::dropMimeData (QMimeData const * data, Qt::DropAction act
{
return true;
}
-
if (parent.isValid ()
- && 2 == parent.column ()
+ && description_column == parent.column ()
&& data->hasFormat (mime_type))
{
QByteArray encoded_data {data->data (mime_type)};
@@ -531,20 +519,18 @@ bool StationList::impl::dropMimeData (QMimeData const * data, Qt::DropAction act
QDataStream stream {&encoded_data, QIODevice::ReadOnly};
while (!stream.atEnd ())
{
- QString frequency_string;
- stream >> frequency_string;
- auto frequency = Radio::frequency (frequency_string, 0);
- auto band_index = bands_->find (frequency);
+ FrequencyList_v2::Item item;
+ stream >> item;
+ auto const& band = bands_->find (item.frequency_);
if (stations_.cend () == std::find_if (stations_.cbegin ()
, stations_.cend ()
- , [&band_index] (Station const& s) {return s.band_name_ == band_index.data ().toString ();}))
+ , [&band] (Station const& s) {return s.band_name_ == band;}))
{
- add (Station {band_index.data ().toString (), 0, QString {}});
+ // not found so add it
+ add (Station {band, 0, QString {}});
}
}
-
return true;
}
-
return false;
}