chore(cpn): move data model files to datamodels directory (#7626)

This commit is contained in:
Neil Horne
2026-09-14 12:57:24 +10:00
committed by GitHub
parent 2e6439854e
commit 1cca36f7d3
6 changed files with 2 additions and 2 deletions
+2
View File
@@ -5,6 +5,8 @@ set(${PROJECT_NAME}_NAMES
compounditemmodels
filtereditemmodels
itemmodeleventhandler
labels
modelslist
)
AddHeadersSources()
+272
View File
@@ -0,0 +1,272 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "labels.h"
#include "radiodata.h"
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 );
buildLabelsList();
}
LabelsModel::~LabelsModel()
{
}
Qt::ItemFlags LabelsModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled;
if (index.column() == 0) {
flags |= Qt::ItemIsUserCheckable;
flags |= Qt::ItemIsSelectable;
flags |= Qt::ItemIsDropEnabled;
flags |= Qt::ItemIsEditable;
if (index.isValid())
flags |= Qt::ItemIsDragEnabled;
}
return flags;
}
bool LabelsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || index.column() != 0 ||
index.row() >= radioData->labels.size())
return false;
if (role == Qt::CheckStateRole) {
if (value==Qt::Unchecked && selectedModel != -1) {
if (radioData->removeLabelFromModel(selectedModel, radioData->labels.at(index.row()).name))
emit modelChanged(selectedModel);
} else {
try {
if (radioData->addLabelToModel(selectedModel, radioData->labels.at(index.row()).name))
emit modelChanged(selectedModel);
} catch(const std::length_error& le) {
emit labelsFault(tr("Unable to add label \"%1\" to model \"%2\" not enough room")\
.arg(radioData->labels.at(index.row()).name).arg(le.what()));
}
}
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) {
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));
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;
}
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;
if (role == Qt::DisplayRole || role == Qt::EditRole) {
if (index.column() == 0) {
return labels.at(index.row()).label;
//return label;
}
} else if (role == Qt::CheckStateRole) {
if (index.column() == 0 && selectedModel >= 0 &&
selectedModel < (int)radioData->models.size()) {
QStringList modelLabels = radioData->models.at(selectedModel).labels.toQString().split(',',Qt::SkipEmptyParts);
label = RadioData::escapeCSV(label);
return modelLabels.indexOf(label)==-1?Qt::Unchecked:Qt::Checked;
} else if (index.column() == 0 && selectedModel == -1) {
return Qt::Unchecked;
}
}
return QVariant();
}
QModelIndex LabelsModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent)
if (row < labels.size())
return createIndex(row,0);
return QModelIndex();
}
QModelIndex LabelsModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
int LabelsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return labels.size();
}
int LabelsModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant LabelsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole ||
orientation != Qt::Horizontal ||
section != 0)
return QVariant();
return tr("Labels");
}
bool LabelsModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (parent.isValid())
return false;
// Find a New Number
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;
}
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) {
if (radioData->deleteLabel(row+i)) {
deleted = true;
}
}
endRemoveRows();
// Refresh all
if (deleted)
emit modelChanged(-1);
buildLabelsList();
return true;
}
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));
}
}
void LabelsModel::modelsSelectionChanged()
{
QModelIndex index = getDataIndex(modelsSelection->currentIndex());
if (index.isValid()) {
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);
}
QWidget * LabelEditTextDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QLineEdit *editor = new QLineEdit(parent);
editor->setValidator(new LabelValidator(parent));
editor->setMaxLength(LABEL_LENGTH);
return editor;
}
+89
View File
@@ -0,0 +1,89 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "labelvalidator.h"
#include <QObject>
#include <QList>
#include <QAbstractItemModel>
#include <QItemSelectionModel>
#include <QLineEdit>
#include <QStyledItemDelegate>
#include <QSortFilterProxyModel>
class RadioData;
typedef struct {
QString label;
int radioLabelIndex;
} LabelItem;
class LabelsModel : public QAbstractItemModel
{
Q_OBJECT
public:
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;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool insertRows(int row, int count, const QModelIndex &parent) override;
bool removeRows(int row, int count, const QModelIndex &parent) override;
public slots:
void buildLabelsList();
private slots:
void modelsSelectionChanged();
signals:
void modelChanged(int index);
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 LabelEditTextDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
LabelEditTextDelegate(QObject * parent = nullptr) : QStyledItemDelegate(parent) {}
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
};
+800
View File
@@ -0,0 +1,800 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "modelslist.h"
#include "edgetxinterface.h"
ModelListItem::ModelListItem(const QVector<QVariant> & itemData):
itemData(itemData),
parentItem(nullptr),
modelIndex(-1),
flags(0),
highlightRX(false)
{
}
ModelListItem::ModelListItem(ModelListItem * parent, int modelIndex):
ModelListItem(QVector<QVariant>(parent->columnCount()))
{
setParent(parent);
setModelIndex(modelIndex);
}
ModelListItem::~ModelListItem()
{
qDeleteAll(childItems);
}
ModelListItem * ModelListItem::child(int number)
{
return childItems.value(number);
}
int ModelListItem::childCount() const
{
return childItems.count();
}
int ModelListItem::childNumber() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<ModelListItem*>(this));
return 0;
}
int ModelListItem::columnCount() const
{
return itemData.count();
}
QVariant ModelListItem::data(int column) const
{
return itemData.value(column);
}
ModelListItem *ModelListItem::insertChild(const int row, int modelIndex)
{
ModelListItem * item = new ModelListItem(this, modelIndex);
childItems.insert(row, item);
return item;
}
ModelListItem * ModelListItem::appendChild(int modelIndex)
{
return insertChild(childItems.size(), modelIndex);
}
bool ModelListItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > childItems.size())
return false;
for (int row = 0; row < count; ++row)
delete childItems.takeAt(position);
return true;
}
bool ModelListItem::insertChildren(int row, int count)
{
for (int i=0; i < count; ++i) {
insertChild(row + i, -1);
}
return true;
}
bool ModelListItem::setData(int column, const QVariant & value)
{
if (column < 0 || column >= itemData.size())
return false;
itemData[column] = value;
return true;
}
void ModelListItem::setFlag(const quint16 & flag, const bool on)
{
if (on)
flags |= flag;
else
flags &= ~flag;
}
bool ModelListItem::isModel() const
{
return (modelIndex > -1);
}
/*
* ModelsListModel
*/
ModelsListModel::ModelsListModel(RadioData * radioData, QObject * parent):
QAbstractItemModel(parent),
radioData(radioData)
{
hasLabels = getCurrentFirmware()->getCapability(Capability::HasModelLabels);
QVector<QVariant> labels;
if (!hasLabels)
labels << tr("Index");
labels << tr("Name");
labels << tr("RX #");
if (hasLabels)
labels << tr("Labels");
rootItem = new ModelListItem(labels);
// uniqueId and version for drag/drop operations (see encodeHeaderData())
mimeHeaderData.instanceId = QUuid::createUuid();
mimeHeaderData.dataVersion = MIME_HEADER_DATA_VERSION;
mimeHeaderData.board = getCurrentBoard();
refresh();
//connect(this, &QAbstractItemModel::rowsAboutToBeRemoved, this, &ModelsListModel::onRowsAboutToBeRemoved);
connect(this, &QAbstractItemModel::rowsRemoved, this, &ModelsListModel::onRowsRemoved);
}
ModelsListModel::~ModelsListModel()
{
delete rootItem;
}
int ModelsListModel::columnCount(const QModelIndex & /* parent */) const
{
return rootItem->columnCount();
}
QVariant ModelsListModel::data(const QModelIndex & index, int role) const
{
if (!index.isValid())
return QVariant();
ModelListItem * item = getItem(index);
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return item->data(index.column());
}
if (role == Qt::FontRole && item->isModel()) {
QFont font;
if (item->getModelIndex() == (int)radioData->generalSettings.currModelIndex)
font.setBold(true);
if (radioData->models[item->getModelIndex()].modelUpdated)
font.setItalic(true);
return font;
}
if (role == Qt::ForegroundRole && (item->getFlags() & ModelListItem::MarkedForCut)) {
return QPalette().brush(QPalette::Disabled, QPalette::Text);
}
if (role == Qt::ForegroundRole && item->isModel()) {
if (index.column() == (hasLabels ? 0 : 1) && radioData->models[item->getModelIndex()].modelErrors) {
QBrush brush;
brush.setColor(Qt::red);
return brush;
}
int col = item->columnCount() - 1;
if(hasLabels)
col --;
if (index.column() == col && item->isHighlightRX()) {
QBrush brush;
brush.setColor(Qt::red);
return brush;
}
}
return QVariant();
}
Qt::ItemFlags ModelsListModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags f = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
f |= Qt::ItemIsDragEnabled;
f |= Qt::ItemIsDropEnabled;
//qDebug() << f;
return f;
}
QVariant ModelsListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
QModelIndex ModelsListModel::index(int row, int column, const QModelIndex & parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
ModelListItem * parentItem = getItem(parent);
ModelListItem * childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex ModelsListModel::parent(const QModelIndex & index) const
{
return QModelIndex();
}
bool ModelsListModel::removeRows(int position, int rows, const QModelIndex & parent)
{
ModelListItem * parentItem = getItem(parent);
if (!parentItem)
return false;
bool success = true;
if (position >= 0 && rows > 0) {
beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->removeChildren(position, rows);
endRemoveRows();
}
return success;
}
/* unused but possibly useful in future
bool ModelsListModel::insertRows(int row, int count, const QModelIndex & parent)
{
ModelListItem * parentItem = getItem(parent);
if (!parentItem)
return false;
bool success = true;
if (row >= 0 && count > 0) {
beginInsertRows(parent, row, row + count - 1);
success = parentItem->insertChildren(row, count);
endInsertRows();
}
return success;
} */
int ModelsListModel::rowCount(const QModelIndex &parent) const
{
ModelListItem * parentItem = getItem(parent);
return parentItem->childCount();
}
bool ModelsListModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if (role != Qt::EditRole)
return false;
if (!index.isValid())
return false;
ModelListItem * item = getItem(index);
bool result = item->setData(index.column(), value);
if (result) {
emit dataChanged(index, index);
}
return result;
}
QStringList ModelsListModel::mimeTypes() const
{
QStringList types;
types << "application/x-companion-modeldata";
types << "application/x-companion-generaldata";
//type << "application/x-companion-radiodata-header"; // supported but not advertised, must be in conjunction with one of the above
return types;
}
Qt::DropActions ModelsListModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt::DropActions ModelsListModel::supportedDragActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
// This method encodes all the data on default drag operation, including general radio settings. This is useful for eg. Compare dialog/model printer.
QMimeData * ModelsListModel::mimeData(const QModelIndexList & indexes) const
{
QMimeData * mimeData = new QMimeData();
getModelsMimeData(indexes, mimeData);
getGeneralMimeData(mimeData);
getHeaderMimeData(mimeData);
getFileMimeData(mimeData);
return mimeData;
}
bool ModelsListModel::canDropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) const
{
Q_UNUSED(action);
//qDebug() << action << row << column << parent.row();
// we do not accept dropped general settings right now (user must copy/paste those)
if (hasHeaderMimeData(data) &&
hasCompatibleBoardMimeData(data) &&
hasModelsMimeData(data) &&
(row > -1 || parent.isValid()))
return true;
return false;
}
bool ModelsListModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
{
if (!canDropMimeData(data, action, row, column, parent))
return false;
if (action == Qt::IgnoreAction)
return true;
QModelIndex idx;
bool isInsert = false;
if (row > -1) {
// dropped between rows (insert)
isInsert = true;
idx = index(row, column, parent);
}
else if (parent.isValid()) {
// was dropped on a row (overwrite)
idx = parent;
}
else {
// dropped who knows where, (shouldn't be here though due check in canDropMimeData())
return false;
}
//qDebug() << action << row << column << parent.row() << idx << idx.row() << hasOwnMimeData(data);
// Force drops from other file windows to be copy actions because we don't want to delete our models.
if (action == Qt::MoveAction && !hasOwnMimeData(data))
action = Qt::CopyAction;
// canDropMimeData() only accepts models
emit modelsDropped(data, idx, isInsert, (action == Qt::MoveAction));
return true;
}
QMimeData *ModelsListModel::getModelsMimeData(const QModelIndexList & indexes, QMimeData * mimeData) const
{
if (!mimeData)
mimeData = new QMimeData();
QByteArray mData;
encodeModelsData(indexes, &mData);
mimeData->setData("application/x-companion-modeldata", mData);
return mimeData;
}
QMimeData *ModelsListModel::getGeneralMimeData(QMimeData * mimeData) const
{
if (!mimeData)
mimeData = new QMimeData();
QByteArray mData;
encodeGeneralData(&mData);
mimeData->setData("application/x-companion-generaldata", mData);
return mimeData;
}
QMimeData *ModelsListModel::getHeaderMimeData(QMimeData * mimeData) const
{
if (!mimeData)
mimeData = new QMimeData();
QByteArray mData;
encodeHeaderData(&mData);
mimeData->setData("application/x-companion-radiodata-header", mData);
return mimeData;
}
QMimeData *ModelsListModel::getFileMimeData(QMimeData * mimeData) const
{
if (!mimeData)
mimeData = new QMimeData();
QByteArray mData;
encodeFileData(&mData);
mimeData->setData("application/x-companion-filedata", mData);
return mimeData;
}
QUuid ModelsListModel::getMimeDataSourceId(const QMimeData * mimeData) const
{
MimeHeaderData header;
decodeHeaderData(mimeData, &header);
return header.instanceId;
}
bool ModelsListModel::hasSupportedMimeData(const QMimeData * mimeData) const
{
foreach (const QString & mtype, mimeTypes()) {
if (mimeData->hasFormat(mtype))
return true;
}
return false;
}
bool ModelsListModel::hasModelsMimeData(const QMimeData * mimeData) const
{
return mimeData->hasFormat("application/x-companion-modeldata");
}
bool ModelsListModel::hasGeneralMimeData(const QMimeData * mimeData) const
{
return mimeData->hasFormat("application/x-companion-generaldata");
}
bool ModelsListModel::hasHeaderMimeData(const QMimeData * mimeData) const
{
return mimeData->hasFormat("application/x-companion-radiodata-header");
}
bool ModelsListModel::hasFileMimeData(const QMimeData * mimeData) const
{
return mimeData->hasFormat("application/x-companion-filedata");
}
// returns true if mime data origin was this data model (vs. from another file window)
bool ModelsListModel::hasOwnMimeData(const QMimeData * mimeData) const
{
return (getMimeDataSourceId(mimeData) == mimeHeaderData.instanceId);
}
void ModelsListModel::encodeModelsData(const QModelIndexList & indexes, QByteArray * data) const
{
int mdlCnt = 0;
foreach (const QModelIndex &index, indexes) {
if (index.isValid() && index.column() == 0)
mdlCnt++;
}
if (mdlCnt < 1)
return;
QDataStream out(data, QIODevice::WriteOnly);
out << mdlCnt;
foreach (const QModelIndex &index, indexes) {
if (index.isValid() && index.column() == 0) {
ModelData &modelData = radioData->models[getModelIndex(index)];
QByteArray yaml;
if (writeModelToYaml(modelData, yaml))
out << yaml << modelData.checklistData.data();
}
}
}
void ModelsListModel::encodeGeneralData(QByteArray * data) const
{
data->append('G');
data->append((char *)&radioData->generalSettings, sizeof(GeneralSettings));
}
void ModelsListModel::encodeHeaderData(QByteArray * data) const
{
// We use a unique ID representing this ModelsListModel instance (a unique file).
// This can be used eg. to detect cross-file drop operations.
QDataStream stream(data, QIODevice::WriteOnly);
stream << mimeHeaderData.dataVersion;
stream << mimeHeaderData.instanceId;
stream << mimeHeaderData.board;
}
void ModelsListModel::encodeFileData(QByteArray * data) const
{
*data = filename.toLatin1();
}
// static
bool ModelsListModel::decodeHeaderData(const QMimeData * mimeData, MimeHeaderData * header)
{
if (header && mimeData->hasFormat("application/x-companion-radiodata-header")) {
QByteArray data = mimeData->data("application/x-companion-radiodata-header");
QDataStream stream(&data, QIODevice::ReadOnly);
stream >> header->dataVersion >> header->instanceId;
if (header->dataVersion >= 2) {
stream >> header->board;
} else {
header->board = Board::BOARD_UNKNOWN;
}
return stream.status() == QDataStream::Ok;
}
return false;
}
// static
bool ModelsListModel::decodeFileData(const QMimeData * mimeData, QString * filedata)
{
if (filedata && mimeData->hasFormat("application/x-companion-filedata")) {
*filedata = mimeData->data("application/x-companion-filedata").data();
return true;
}
return false;
}
// static
bool ModelsListModel::decodeMimeData(const QMimeData * mimeData, QVector<ModelData> * models, GeneralSettings * gs, bool * hasGenSet)
{
bool ret = false;
char * gData;
if (hasGenSet)
*hasGenSet = false;
if (models && mimeData->hasFormat("application/x-companion-modeldata")) {
QByteArray clipboard = mimeData->data("application/x-companion-modeldata");
QDataStream in(&clipboard, QIODevice::ReadOnly);
int mdlCnt;
in >> mdlCnt;
for (int i = 1; i <= mdlCnt; i++) {
QByteArray *buffer = new QByteArray();
in >> *buffer;
if (buffer->size() > 0) {
ModelData *model = new ModelData();
if (loadModelFromYaml(*model, *buffer)) {
model->used = true;
QByteArray *chklst = new QByteArray();
in >> *chklst;
if (chklst->size() > 0) {
model->checklistData = *chklst;
models->append(*model);
}
delete chklst;
ret = true;
}
delete model;
delete buffer;
}
}
}
// General settings
if (gs && mimeData->hasFormat("application/x-companion-generaldata")) {
QByteArray genData = mimeData->data("application/x-companion-generaldata");
gData = genData.data();
char c = *gData++;
if (c == 'G') {
*gs = *((GeneralSettings *)gData);
ret = true;
if (hasGenSet)
*hasGenSet = true;
}
}
return ret;
}
int ModelsListModel::countModelsInMimeData(const QMimeData * mimeData) const
{
int ret = 0;
if (hasCompatibleBoardMimeData(mimeData) &&
mimeData->hasFormat("application/x-companion-modeldata")) {
QByteArray data = mimeData->data("application/x-companion-modeldata");
QDataStream in(&data, QIODevice::ReadOnly);
in >> ret;
}
return ret;
}
ModelListItem * ModelsListModel::getItem(const QModelIndex & index) const
{
if (index.isValid()) {
ModelListItem * item = static_cast<ModelListItem *>(index.internalPointer());
if (item) {
return item;
}
}
return rootItem;
}
// recursive
QModelIndex ModelsListModel::getIndexForModel(const int modelIndex, QModelIndex parent)
{
for (int i = 0; i < rowCount(parent); ++i) {
QModelIndex idx = index(i, 0, parent);
if (hasChildren(idx) && (idx = getIndexForModel(modelIndex, idx)).isValid())
return idx;
if (getItem(idx)->getModelIndex() == modelIndex)
return idx;
}
return QModelIndex();
}
int ModelsListModel::getModelIndex(const QModelIndex & index) const
{
return getItem(index)->getModelIndex();
}
int ModelsListModel::rowNumber(const QModelIndex & index) const
{
return getItem(index)->childNumber();
}
bool ModelsListModel::isModelType(const QModelIndex & index) const
{
return index.isValid() && getItem(index)->isModel();
}
void ModelsListModel::markItemForCut(const QModelIndex & index, bool on)
{
if (index.isValid() && index.column() == 0)
getItem(index)->setFlag(ModelListItem::MarkedForCut, on);
}
void ModelsListModel::markItemsForCut(const QModelIndexList & indexes, bool on)
{
foreach (const QModelIndex &index, indexes)
markItemForCut(index, on);
}
// onRowsAboutToBeRemoved could be a way to deal with models being drag-drop moved to another window/file.
// ModelsListModel detects these as removals and runs removeRows(), which deletes the Model indexes
// but not the actual models from the RadioData::models array.
// BUT this also runs when moving rows within our own tree, and if there is an error during the move,
// or the user cancels the operation, removeRows() is still called automatically somewhere inside QAbstractItemModel().
// If a solution could be found to this problem then we could enable DnD-moving models between file windows.
/*
void ModelsListModel::onRowsAboutToBeRemoved(const QModelIndex & parent, int first, int last)
{
qDebug() << parent << first << last;
QVector<int> modelIndices;
for (int i=first; i <= last; ++i) {
modelIndices << getItem(index(i, 0, parent))->getModelIndex();
}
if (modelIndices.size())
emit modelsRemoved(modelIndices);
}
*/
void ModelsListModel::onRowsRemoved(const QModelIndex & parent, int first, int last)
{
// This is a workaround to deal with models being DnD moved to another window/file or if user cancels a DnD move within our own.
// ModelsListModel detects these as removals and runs removeRows(), which deletes the Model indexes but not our actual models. See notes above.
//qDebug() << parent << first << last;
emit refreshRequested(); // request refresh from View because it may have it's own ideas
}
void ModelsListModel::refresh()
{
this->blockSignals(true); // make sure onRowsRemoved is not triggered
removeRows(0, rowCount());
this->blockSignals(false);
for (unsigned i = 0; i < radioData->models.size(); i++) {
ModelData & model = radioData->models[i];
int currentColumn = 0;
ModelListItem * current = nullptr;
model.modelIndex = i;
if (hasLabels) {
current = rootItem->appendChild(i);
}
else {
current = rootItem->appendChild(i);
current->setData(currentColumn++, QString("%1").arg(i + 1, 2, 10, QChar('0')));
}
if (!model.isEmpty() && current) {
QString modelName;
if (!model.name.empty()) {
modelName = model.name.toQString();
}
else {
/*: Translators: do NOT use accents here, this is a default model name. */
modelName = tr("Model %1").arg(uint(i+1), 2, 10, QChar('0'));
}
current->setData(currentColumn++, modelName);
int protocol;
QString rxs;
unsigned moduleIdx = 0;
for (auto const& moduleData: model.moduleData) {
protocol = moduleData.protocol;
// These are the only RXs that allow nominating RX # but changing RX or copying models can leave residual configuration which can cause issues
// if (protocol == PULSES_PXX_XJT_X16 || protocol == PULSES_PXX_XJT_LR12 || protocol == PULSES_PXX_R9M || protocol == PULSES_DSMX || protocol == PULSES_MULTIMODULE) {
if (moduleData.supportRxNum() && moduleData.modelId > 0) {
if (!rxs.isEmpty()) {
rxs.append(", ");
}
unsigned mdlidx = moduleData.modelId;
rxs.append(QString("%1").arg(uint(mdlidx), 2, 10, QChar('0')));
if (!isModelIdUnique(mdlidx, moduleIdx, protocol)) {
current->setHighlightRX(true);
}
}
moduleIdx++;
}
current->setData(currentColumn++, rxs);
}
if (hasLabels) {
QStringList labels = RadioData::fromCSV(QString::fromUtf8(model.labels.c_str()));
current->setData(currentColumn++, labels.join(QChar(0x2022)));
}
}
}
bool ModelsListModel::isModelIdUnique(unsigned modelIdx, unsigned module, unsigned protocol)
{
int cnt = 0;
if (protocol== PULSES_PXX_XJT_D8)
return true;
for (auto const& model: radioData->models) {
if (!model.isEmpty()) {
const ModuleData& moduleData = model.moduleData[module];
if (moduleData.protocol == protocol && moduleData.modelId == modelIdx) {
if (++cnt > 1) {
return false;
}
}
}
}
return true;
}
void ModelsListModel::setFilename(QString & name)
{
filename = name;
}
Board::Type ModelsListModel::getMimeDataBoard(const QMimeData * mimeData) const
{
MimeHeaderData header;
decodeHeaderData(mimeData, &header);
return header.board;
}
// returns true if mime data board type is the same as this data model
bool ModelsListModel::hasCompatibleBoardMimeData(const QMimeData * mimeData) const
{
return (getMimeDataBoard(mimeData) == mimeHeaderData.board);
}
/*
ModelsListProxyModel
*/
bool ModelsListProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
{
return true;
}
+183
View File
@@ -0,0 +1,183 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "eeprominterface.h"
#include "labels.h"
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QMimeData>
#include <QUuid>
class ModelListItem
{
public:
enum ModelListItemFlags { MarkedForCut = 0x01 };
explicit ModelListItem(const QVector<QVariant> & itemData);
explicit ModelListItem(ModelListItem * parent, int modelIndex);
~ModelListItem();
ModelListItem * child(int number);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
ModelListItem * appendChild( int modelIndex);
ModelListItem * insertChild(const int row, int modelIndex);
bool removeChildren(int position, int count);
bool insertChildren(int row, int count);
int childNumber() const;
bool setData(int column, const QVariant &value);
ModelListItem * parent() { return parentItem; }
void setParent(ModelListItem * p) { parentItem = p; }
int getModelIndex() const { return modelIndex; }
void setModelIndex(int value) { modelIndex = value; }
void setHighlightRX(int value) { highlightRX = value; }
bool isHighlightRX() const { return highlightRX; }
quint16 getFlags() const { return flags; }
void setFlags(const quint16 & value) { flags = value; }
void setFlag(const quint16 & flag, const bool on = true);
bool isModel() const;
private:
QList<ModelListItem*> childItems;
QVector<QVariant> itemData;
ModelListItem * parentItem;
int modelIndex;
quint16 flags;
bool highlightRX;
};
#define MIME_HEADER_DATA_VERSION 2
class ModelsListModel : public QAbstractItemModel
{
Q_OBJECT
public:
struct MimeHeaderData {
QUuid instanceId{};
quint16 dataVersion{0};
Board::Type board{Board::BOARD_UNKNOWN};
};
ModelsListModel(RadioData * radioData, QObject *parent = nullptr);
virtual ~ModelsListModel();
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex &index) const override;
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
//virtual bool insertRows(int row, int count, const QModelIndex & parent) override;
virtual QStringList mimeTypes() const override;
virtual Qt::DropActions supportedDropActions() const override;
virtual Qt::DropActions supportedDragActions() const override;
virtual QMimeData * mimeData(const QModelIndexList & indexes) const override;
virtual bool canDropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) const override;
virtual bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) override;
void encodeModelsData(const QModelIndexList & indexes, QByteArray * data) const;
void encodeGeneralData(QByteArray * data) const;
void encodeHeaderData(QByteArray * data) const;
void encodeFileData(QByteArray * data) const;
QMimeData * getModelsMimeData(const QModelIndexList & indexes, QMimeData * mimeData = nullptr) const;
QMimeData * getGeneralMimeData(QMimeData * mimeData = nullptr) const;
QMimeData * getHeaderMimeData(QMimeData * mimeData = nullptr) const;
QMimeData * getFileMimeData(QMimeData * mimeData = nullptr) const;
QUuid getMimeDataSourceId(const QMimeData * mimeData) const;
Board::Type getMimeDataBoard(const QMimeData * mimeData) const;
bool hasSupportedMimeData(const QMimeData * mimeData) const;
bool hasModelsMimeData(const QMimeData * mimeData) const;
bool hasGeneralMimeData(const QMimeData * mimeData) const;
bool hasHeaderMimeData(const QMimeData * mimeData) const;
bool hasOwnMimeData(const QMimeData * mimeData) const;
bool hasFileMimeData(const QMimeData * mimeData) const;
bool hasCompatibleBoardMimeData(const QMimeData * mimeData) const;
static bool decodeFileData(const QMimeData * mimeData, QString * filedata);
static bool decodeHeaderData(const QMimeData * mimeData, MimeHeaderData * header);
static bool decodeMimeData(const QMimeData * mimeData, QVector<ModelData> * models = nullptr, GeneralSettings * gs = nullptr, bool * hasGenSet = nullptr);
int countModelsInMimeData(const QMimeData * mimeData) const;
QModelIndex getIndexForModel(const int modelIndex, QModelIndex parent = QModelIndex());
int getModelIndex(const QModelIndex & index) const;
int rowNumber(const QModelIndex & index = QModelIndex()) const;
bool isModelType(const QModelIndex & index) const;
void setFilename(QString & name);
public slots:
void markItemForCut(const QModelIndex & index, bool on = true);
void markItemsForCut(const QModelIndexList & indexes, bool on = true);
void refresh();
signals:
void modelsDropped(const QMimeData * mimeData, const QModelIndex toRowIdx, const bool insert, const bool move);
void modelsRemoved(const QVector<int> modelIndices);
void refreshRequested();
private slots:
//void onRowsAboutToBeRemoved(const QModelIndex & parent, int first, int last);
void onRowsRemoved(const QModelIndex & parent, int first, int last);
private:
ModelListItem * getItem(const QModelIndex & index) const;
bool isModelIdUnique(unsigned modelId, unsigned module, unsigned protocol);
ModelListItem * rootItem;
RadioData * radioData;
MimeHeaderData mimeHeaderData;
bool hasLabels;
QString filename;
};
class ModelsListProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
ModelsListProxyModel() {}
virtual ~ModelsListProxyModel() {}
public slots:
void setFilter(LabelsModel* labels) { m_labels = labels; }
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const override;
private:
LabelsModel* m_labels;
};