fix(cpn): Model and labels lists sync (#3967)
* labels use proxy model mapping to source * cosmetics * Fix model list focus * fix model focus on change to labels
This commit is contained in:
+58
-25
@@ -1,14 +1,15 @@
|
||||
#include "labels.h"
|
||||
|
||||
LabelsModel::LabelsModel(QItemSelectionModel *selectionModel,
|
||||
LabelsModel::LabelsModel(QSortFilterProxyModel * modelsListProxyModel,
|
||||
QItemSelectionModel *selectionModel,
|
||||
RadioData *radioData, QObject *parent) :
|
||||
QAbstractItemModel(parent),
|
||||
modelsListProxyModel(modelsListProxyModel),
|
||||
modelsSelection(selectionModel),
|
||||
radioData(radioData),
|
||||
selectedModel(-1)
|
||||
{
|
||||
connect(modelsSelection, &QItemSelectionModel::currentChanged,
|
||||
this, &LabelsModel::modelsSelectionChanged );
|
||||
connect(modelsSelection, &QItemSelectionModel::currentChanged, this, &LabelsModel::modelsSelectionChanged );
|
||||
|
||||
buildLabelsList();
|
||||
}
|
||||
@@ -44,7 +45,7 @@ bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int r
|
||||
if (radioData->removeLabelFromModel(selectedModel, radioData->labels.at(index.row()).name))
|
||||
emit modelChanged(selectedModel);
|
||||
} else {
|
||||
try{
|
||||
try {
|
||||
if (radioData->addLabelToModel(selectedModel, radioData->labels.at(index.row()).name))
|
||||
emit modelChanged(selectedModel);
|
||||
} catch(const std::length_error& le) {
|
||||
@@ -52,34 +53,42 @@ bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int r
|
||||
.arg(radioData->labels.at(index.row()).name).arg(le.what()));
|
||||
}
|
||||
}
|
||||
emit dataChanged(this->index(index.row(), 0),
|
||||
this->index(index.row(), 0));
|
||||
|
||||
emit dataChanged(this->index(index.row(), 0), this->index(index.row(), 0));
|
||||
return true;
|
||||
|
||||
} else if (role == Qt::EditRole) {
|
||||
QString replFrom = labels[index.row()].label;
|
||||
QString replTo = value.toString();
|
||||
|
||||
if (replFrom == replTo) // User exits edit without changing
|
||||
return true;
|
||||
|
||||
if (radioData->indexOfLabel(replTo) == -1) { // Don't allow duplicates
|
||||
bool modelsChanged = false;
|
||||
try{
|
||||
|
||||
try {
|
||||
modelsChanged = radioData->renameLabel(replFrom, replTo);
|
||||
} catch(const std::length_error& le) {
|
||||
emit labelsFault(tr("Unable to rename \"%1\" to \"%2\" not enough room in model %3")\
|
||||
.arg(replFrom).arg(replTo).arg(le.what()));
|
||||
return false;
|
||||
}
|
||||
|
||||
labels[index.row()].label = replTo;
|
||||
emit dataChanged(this->index(index.row(), 0),
|
||||
this->index(index.row(), 0));
|
||||
emit dataChanged(this->index(index.row(), 0), this->index(index.row(), 0));
|
||||
|
||||
if (selectedModel != -1 && modelsChanged)
|
||||
emit modelChanged(selectedModel);
|
||||
|
||||
} else {
|
||||
emit labelsFault(tr("Unable to rename \"%1\" to \"%2\" the label already exists")\
|
||||
.arg(replFrom).arg(replTo));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -88,7 +97,7 @@ QVariant LabelsModel::data(const QModelIndex &index, int role) const
|
||||
if (index.row() >= labels.size() || !index.isValid() )
|
||||
return QVariant();
|
||||
|
||||
QString label = radioData->labels.at(index.row()).name;
|
||||
QString label = radioData->labels.at(index.row()).name;
|
||||
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
||||
if (index.column() == 0) {
|
||||
@@ -138,9 +147,10 @@ int LabelsModel::columnCount(const QModelIndex &parent) const
|
||||
QVariant LabelsModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role != Qt::DisplayRole ||
|
||||
orientation != Qt::Horizontal ||
|
||||
section != 0)
|
||||
orientation != Qt::Horizontal ||
|
||||
section != 0)
|
||||
return QVariant();
|
||||
|
||||
return tr("Labels");
|
||||
}
|
||||
|
||||
@@ -153,16 +163,19 @@ bool LabelsModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||
for (int i = 0; i < count; i++) {
|
||||
int newNo=0;
|
||||
QString newStr;
|
||||
|
||||
do {
|
||||
newStr = QString(tr("New%1").arg(newNo));
|
||||
if (newNo == 0)
|
||||
newStr = QString(tr("New"));
|
||||
newNo++;
|
||||
} while(radioData->indexOfLabel(newStr) >= 0);
|
||||
|
||||
// Add it to radioData
|
||||
RadioData::LabelData ld = { newStr , false };
|
||||
radioData->labels.insert(row + i, ld);
|
||||
}
|
||||
|
||||
buildLabelsList();
|
||||
return true;
|
||||
}
|
||||
@@ -170,19 +183,23 @@ bool LabelsModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||
bool LabelsModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
if (parent.isValid() || row < 0)
|
||||
return false;
|
||||
return false;
|
||||
|
||||
bool deleted=false;
|
||||
beginRemoveRows(parent, row, row + count - 1);
|
||||
for (int i = 0; i != count; ++i)
|
||||
if (radioData->deleteLabel(row+i)) {
|
||||
deleted = true;
|
||||
}
|
||||
|
||||
for (int i = 0; i != count; ++i) {
|
||||
if (radioData->deleteLabel(row+i)) {
|
||||
deleted = true;
|
||||
}
|
||||
}
|
||||
endRemoveRows();
|
||||
// Refresh all
|
||||
if (deleted)
|
||||
emit modelChanged(-1);
|
||||
|
||||
buildLabelsList();
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void LabelsModel::buildLabelsList()
|
||||
@@ -190,52 +207,66 @@ void LabelsModel::buildLabelsList()
|
||||
labels.clear();
|
||||
|
||||
int i = 0;
|
||||
|
||||
foreach(RadioData::LabelData ld, radioData->labels) {
|
||||
LabelItem itm;
|
||||
itm.label = ld.name;
|
||||
itm.radioLabelIndex = i++;
|
||||
labels.append(itm);
|
||||
}
|
||||
|
||||
if (i) {
|
||||
emit dataChanged(index(0,0),
|
||||
index(i-1,0));
|
||||
emit dataChanged(index(0, 0), index(i - 1, 0));
|
||||
}
|
||||
}
|
||||
|
||||
void LabelsModel::modelsSelectionChanged()
|
||||
{
|
||||
QModelIndex index = modelsSelection->currentIndex();
|
||||
QModelIndex index = getDataIndex(modelsSelection->currentIndex());
|
||||
|
||||
if (index.isValid()) {
|
||||
int mi = modelsSelection->currentIndex().row();
|
||||
if (mi < (int)radioData->models.size()) {
|
||||
selectedModel = mi;
|
||||
buildLabelsList();
|
||||
int mi = index.row();
|
||||
|
||||
if (mi < (int)radioData->models.size()) {
|
||||
selectedModel = mi;
|
||||
buildLabelsList();
|
||||
}
|
||||
}
|
||||
else
|
||||
selectedModel = -1;
|
||||
}
|
||||
|
||||
QModelIndex LabelsModel::getDataIndex(QModelIndex viewIndex) const
|
||||
{
|
||||
return modelsListProxyModel->mapToSource(viewIndex);
|
||||
}
|
||||
|
||||
|
||||
QValidator::State LabelValidator::validate(QString &label, int &pos) const
|
||||
{
|
||||
Q_UNUSED(pos)
|
||||
QString lbl = RadioData::escapeCSV(label);
|
||||
|
||||
if (lbl.toUtf8().size() > LABEL_LENGTH)
|
||||
return QValidator::Invalid;
|
||||
|
||||
if(lbl.contains('\\') || // TODO: Fix me to allow all, requires FW changes
|
||||
lbl.contains('\"') ||
|
||||
lbl.contains(':') ||
|
||||
lbl.contains('-') ||
|
||||
lbl.contains('\''))
|
||||
return QValidator::Invalid;
|
||||
|
||||
if(lbl.size() == 0)
|
||||
return QValidator::Intermediate;
|
||||
|
||||
return QValidator::Acceptable;
|
||||
}
|
||||
|
||||
void LabelValidator::fixup(QString &input) const
|
||||
{
|
||||
QByteArray output = input.toUtf8();
|
||||
|
||||
if (output.size() > LABEL_LENGTH) {
|
||||
int truncateAt = 0;
|
||||
for (int i = LABEL_LENGTH; i > 0; i--) {
|
||||
@@ -244,8 +275,10 @@ void LabelValidator::fixup(QString &input) const
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
output.truncate(truncateAt);
|
||||
}
|
||||
|
||||
input = QString(output);
|
||||
input.remove('\\'); // TODO: Fix me to allow all, requires FW changes
|
||||
input.remove('\"');
|
||||
|
||||
Reference in New Issue
Block a user