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:
+45
-12
@@ -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();
|
||||
}
|
||||
@@ -52,16 +53,20 @@ 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 {
|
||||
modelsChanged = radioData->renameLabel(replFrom, replTo);
|
||||
} catch(const std::length_error& le) {
|
||||
@@ -69,17 +74,21 @@ bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int r
|
||||
.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;
|
||||
}
|
||||
|
||||
@@ -141,6 +150,7 @@ QVariant LabelsModel::headerData(int section, Qt::Orientation orientation, int r
|
||||
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;
|
||||
}
|
||||
@@ -171,16 +184,20 @@ bool LabelsModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
if (parent.isValid() || row < 0)
|
||||
return false;
|
||||
|
||||
bool deleted=false;
|
||||
beginRemoveRows(parent, row, row + count - 1);
|
||||
for (int i = 0; i != count; ++i)
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -190,23 +207,26 @@ 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();
|
||||
int mi = index.row();
|
||||
|
||||
if (mi < (int)radioData->models.size()) {
|
||||
selectedModel = mi;
|
||||
buildLabelsList();
|
||||
@@ -216,26 +236,37 @@ void LabelsModel::modelsSelectionChanged()
|
||||
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('\"');
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <QValidator>
|
||||
#include <QLineEdit>
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "radiodata.h"
|
||||
|
||||
@@ -22,8 +23,9 @@ class LabelsModel : public QAbstractItemModel
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
LabelsModel(QItemSelectionModel *selectionModel, RadioData *radioData, QObject *parent = nullptr);
|
||||
~LabelsModel();
|
||||
LabelsModel(QSortFilterProxyModel * modelsListProxyModel, QItemSelectionModel *selectionModel, RadioData *radioData, QObject *parent = nullptr);
|
||||
virtual ~LabelsModel();
|
||||
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
@@ -46,16 +48,20 @@ signals:
|
||||
void labelsFault(QString msg);
|
||||
|
||||
private:
|
||||
QSortFilterProxyModel * modelsListProxyModel;
|
||||
QItemSelectionModel *modelsSelection;
|
||||
RadioData *radioData;
|
||||
int selectedModel;
|
||||
QList<QModelIndex> modelIndices;
|
||||
QList<LabelItem> labels;
|
||||
|
||||
QModelIndex getDataIndex(QModelIndex viewIndex) const;
|
||||
};
|
||||
|
||||
class LabelValidator : public QValidator
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QValidator::State validate(QString &label, int &pos) const;
|
||||
void fixup(QString &input) const;
|
||||
|
||||
@@ -553,8 +553,7 @@ void MdiChild::initModelsList()
|
||||
if (labelsListModel)
|
||||
delete labelsListModel;
|
||||
|
||||
labelsListModel = new LabelsModel(ui->modelsList->selectionModel(),
|
||||
&radioData, this);
|
||||
labelsListModel = new LabelsModel(modelsListProxyModel, ui->modelsList->selectionModel(), &radioData, this);
|
||||
connect(labelsListModel, &LabelsModel::modelChanged, this, &MdiChild::modelLabelsChanged);
|
||||
connect(labelsListModel, &LabelsModel::labelsFault, this, &MdiChild::labelsFault);
|
||||
ui->lstLabels->setModel(labelsListModel);
|
||||
@@ -1096,7 +1095,7 @@ bool MdiChild::hasClipboardData(const quint8 type) const
|
||||
return modelsListModel->hasModelsMimeData(QApplication::clipboard()->mimeData());
|
||||
}
|
||||
else {
|
||||
return modelsListModel->hasGenralMimeData(QApplication::clipboard()->mimeData());
|
||||
return modelsListModel->hasGeneralMimeData(QApplication::clipboard()->mimeData());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1754,11 +1753,12 @@ void MdiChild::labelMoveDown()
|
||||
ui->lstLabels->selectionModel()->setCurrentIndex(labelsListModel->index(row + 1,0), QItemSelectionModel::ClearAndSelect);
|
||||
}
|
||||
|
||||
void MdiChild::modelLabelsChanged(int row)
|
||||
void MdiChild::modelLabelsChanged(int index)
|
||||
{
|
||||
setWindowModified(true);
|
||||
refresh();
|
||||
ui->modelsList->selectionModel()->select(modelsListModel->index(row,0), QItemSelectionModel::ClearAndSelect |
|
||||
const QModelIndex idx = modelsListProxyModel->mapFromSource(modelsListModel->getIndexForModel(index));
|
||||
ui->modelsList->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect |
|
||||
QItemSelectionModel::Rows);
|
||||
}
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ class MdiChild : public QWidget
|
||||
void labelRename();
|
||||
void labelMoveUp();
|
||||
void labelMoveDown();
|
||||
void modelLabelsChanged(int row);
|
||||
void modelLabelsChanged(int index);
|
||||
void labelsFault(QString msg);
|
||||
void wizardEdit();
|
||||
void modelDuplicate();
|
||||
|
||||
@@ -416,7 +416,7 @@ bool ModelsListModel::hasModelsMimeData(const QMimeData * mimeData) const
|
||||
return mimeData->hasFormat("application/x-companion-modeldata");
|
||||
}
|
||||
|
||||
bool ModelsListModel::hasGenralMimeData(const QMimeData * mimeData) const
|
||||
bool ModelsListModel::hasGeneralMimeData(const QMimeData * mimeData) const
|
||||
{
|
||||
return mimeData->hasFormat("application/x-companion-generaldata");
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ class ModelsListModel : public QAbstractItemModel
|
||||
QUuid getMimeDataSourceId(const QMimeData * mimeData) const;
|
||||
bool hasSupportedMimeData(const QMimeData * mimeData) const;
|
||||
bool hasModelsMimeData(const QMimeData * mimeData) const;
|
||||
bool hasGenralMimeData(const QMimeData * mimeData) const;
|
||||
bool hasGeneralMimeData(const QMimeData * mimeData) const;
|
||||
bool hasHeaderMimeData(const QMimeData * mimeData) const;
|
||||
bool hasOwnMimeData(const QMimeData * mimeData) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user