From 8abc2a7179760f07b0a58948bc540ca5cc8607b4 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Wed, 18 Jan 2017 08:27:17 +0100 Subject: [PATCH] [Companion] It's now possible to add Categories --- companion/src/mdichild.cpp | 43 ++++++++++++++++++++++-------------- companion/src/mdichild.h | 1 + companion/src/modelslist.cpp | 43 ++++++++++++++++++------------------ companion/src/modelslist.h | 12 +++++++--- 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/companion/src/mdichild.cpp b/companion/src/mdichild.cpp index 0bb07a395..6bb86504c 100644 --- a/companion/src/mdichild.cpp +++ b/companion/src/mdichild.cpp @@ -129,15 +129,13 @@ void MdiChild::deleteSelectedModels() void MdiChild::showModelsListContextMenu(const QPoint & pos) { - int modelIndex = getCurrentRow(); + QModelIndex modelIndex = ui->modelsList->indexAt(pos); QPoint globalPos = ui->modelsList->mapToGlobal(pos); QMenu contextMenu; - - const QClipboard * clipboard = QApplication::clipboard(); - const QMimeData * mimeData = clipboard->mimeData(); - bool hasData = mimeData->hasFormat("application/x-companion"); - - if (modelIndex >= 0) { + if (modelsListModel->getModelIndex(modelIndex) >= 0) { + const QClipboard * clipboard = QApplication::clipboard(); + const QMimeData * mimeData = clipboard->mimeData(); + bool hasData = mimeData->hasFormat("application/x-companion"); contextMenu.addAction(CompanionIcon("edit.png"), tr("&Edit"), this, SLOT(modelEdit())); contextMenu.addAction(CompanionIcon("open.png"), tr("&Restore from backup"), this, SLOT(loadBackup())); contextMenu.addAction(CompanionIcon("wizard.png"), tr("&Model Wizard"), this, SLOT(wizardEdit())); @@ -155,11 +153,16 @@ void MdiChild::showModelsListContextMenu(const QPoint & pos) contextMenu.addAction(CompanionIcon("simulate.png"), tr("&Simulate model"), this, SLOT(modelSimulate()), tr("Alt+S")); } else if (IS_HORUS(GetCurrentFirmware()->getBoard())) { - contextMenu.addAction(CompanionIcon("add.png"), tr("&Add model"), this, SLOT(modelAdd())); + if (modelsListModel->getCategoryIndex(modelIndex) >= 0) { + contextMenu.addAction(CompanionIcon("add.png"), tr("&Add model"), this, SLOT(modelAdd())); + } + else { + contextMenu.addAction(CompanionIcon("add.png"), tr("&Add category"), this, SLOT(categoryAdd())); + } + } + else { + return; } - - // TODO context menu for radio settings - // contextMenu.addAction(CompanionIcon("edit.png"), tr("&Edit"), this, SLOT(EditModel())); contextMenu.exec(globalPos); } @@ -334,22 +337,28 @@ void MdiChild::generalEdit() t->show(); } +void MdiChild::categoryAdd() +{ + CategoryData category("New category"); + radioData.categories.push_back(category); + setModified(); +} + void MdiChild::modelAdd() { - if (radioData.categories.size() == 0) { - CategoryData category("Models"); - radioData.categories.push_back(category); + int categoryIndex = modelsListModel->getCategoryIndex(ui->modelsList->currentIndex()); + if (categoryIndex < 0 || categoryIndex >= (int)radioData.categories.size()) { + return; } ModelData model; - model.category = radioData.categories.size() - 1; + model.category = categoryIndex; model.used = true; sprintf(model.filename, "model%lu.bin", radioData.models.size()+1); - sprintf(model.name, "Model%lu", radioData.models.size()+1); + sprintf(model.name, tr("New model").toStdString().c_str()); radioData.models.push_back(model); radioData.setCurrentModel(radioData.models.size() - 1); setModified(); - //modelEdit(); } void MdiChild::modelEdit() diff --git a/companion/src/mdichild.h b/companion/src/mdichild.h index c9e109e11..634290a1d 100644 --- a/companion/src/mdichild.h +++ b/companion/src/mdichild.h @@ -70,6 +70,7 @@ class MdiChild : public QWidget void showModelsListContextMenu(const QPoint & pos); void checkAndInitModel(int row); void generalEdit(); + void categoryAdd(); void modelAdd(); void modelEdit(); void wizardEdit(); diff --git a/companion/src/modelslist.cpp b/companion/src/modelslist.cpp index 1905c8b0e..af83be5ec 100644 --- a/companion/src/modelslist.cpp +++ b/companion/src/modelslist.cpp @@ -23,13 +23,15 @@ TreeItem::TreeItem(const QVector & itemData): itemData(itemData), parentItem(NULL), + categoryIndex(-1), modelIndex(-1) { } -TreeItem::TreeItem(TreeItem * parent, int modelIndex): +TreeItem::TreeItem(TreeItem * parent, int categoryIndex, int modelIndex): itemData(parent->columnCount()), parentItem(parent), + categoryIndex(categoryIndex), modelIndex(modelIndex) { } @@ -67,9 +69,9 @@ QVariant TreeItem::data(int column) const return itemData.value(column); } -TreeItem * TreeItem::appendChild(int modelIndex) +TreeItem * TreeItem::appendChild(int categoryIndex, int modelIndex) { - TreeItem * item = new TreeItem(this, modelIndex); + TreeItem * item = new TreeItem(this, categoryIndex, modelIndex); childItems.insert(childItems.size(), item); return item; } @@ -155,10 +157,10 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const return Qt::ItemIsEditable | QAbstractItemModel::flags(index); } -TreeItem * TreeModel::getItem(const QModelIndex &index) const +TreeItem * TreeModel::getItem(const QModelIndex & index) const { if (index.isValid()) { - TreeItem * item = static_cast(index.internalPointer()); + TreeItem * item = static_cast(index.internalPointer()); if (item) { return item; } @@ -166,8 +168,7 @@ TreeItem * TreeModel::getItem(const QModelIndex &index) const return rootItem; } -QVariant TreeModel::headerData(int section, Qt::Orientation orientation, - int role) const +QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return rootItem->data(section); @@ -175,7 +176,7 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } -QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex TreeModel::index(int row, int column, const QModelIndex & parent) const { if (parent.isValid() && parent.column() != 0) return QModelIndex(); @@ -189,13 +190,13 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con return QModelIndex(); } -QModelIndex TreeModel::parent(const QModelIndex &index) const +QModelIndex TreeModel::parent(const QModelIndex & index) const { if (!index.isValid()) return QModelIndex(); - TreeItem *childItem = getItem(index); - TreeItem *parentItem = childItem->parent(); + TreeItem * childItem = getItem(index); + TreeItem * parentItem = childItem->parent(); if (parentItem == rootItem) return QModelIndex(); @@ -203,11 +204,10 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const return createIndex(parentItem->childNumber(), 0, parentItem); } - -bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) +bool TreeModel::removeRows(int position, int rows, const QModelIndex & parent) { TreeItem * parentItem = getItem(parent); - bool success = true; + bool success; beginRemoveRows(parent, position, position + rows - 1); success = parentItem->removeChildren(position, rows); @@ -218,17 +218,17 @@ bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) int TreeModel::rowCount(const QModelIndex &parent) const { - TreeItem *parentItem = getItem(parent); + TreeItem * parentItem = getItem(parent); return parentItem->childCount(); } -bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) +bool TreeModel::setData(const QModelIndex &index, const QVariant & value, int role) { if (role != Qt::EditRole) return false; - TreeItem *item = getItem(index); + TreeItem * item = getItem(index); bool result = item->setData(index.column(), value); if (result) @@ -253,7 +253,7 @@ void TreeModel::refresh() if (IS_HORUS(board)) { for (unsigned i = 0; i < radioData->categories.size(); i++) { - TreeItem * current = rootItem->appendChild(-1); + TreeItem * current = rootItem->appendChild(i, -1); current->setData(0, QString(radioData->categories[i].name)); } } @@ -270,17 +270,18 @@ void TreeModel::refresh() categoryItem = rootItem->child(model.category); } else { + model.category = 0; if (!defaultCategoryItem) { - defaultCategoryItem = rootItem->appendChild(-1); + defaultCategoryItem = rootItem->appendChild(0, -1); defaultCategoryItem->setData(0, QObject::tr("Models")); } categoryItem = defaultCategoryItem; } - current = categoryItem->appendChild(i); + current = categoryItem->appendChild(model.category, i); } } else { - current = rootItem->appendChild(i); + current = rootItem->appendChild(0, i); current->setData(currentColumn++, QString().sprintf("%02d", i + 1)); } diff --git a/companion/src/modelslist.h b/companion/src/modelslist.h index 1bebd7d0d..0e0dd6c2e 100644 --- a/companion/src/modelslist.h +++ b/companion/src/modelslist.h @@ -34,14 +34,14 @@ class TreeItem { public: explicit TreeItem(const QVector & itemData); - explicit TreeItem(TreeItem * parent, int modelIndex = -1); + explicit TreeItem(TreeItem * parent, int categoryIndex, int modelIndex); ~TreeItem(); - TreeItem *child(int number); + TreeItem * child(int number); int childCount() const; int columnCount() const; QVariant data(int column) const; - TreeItem * appendChild(int modelIndex); + TreeItem * appendChild(int categoryIndex, int modelIndex); TreeItem * parent(); bool removeChildren(int position, int count); @@ -49,11 +49,13 @@ class TreeItem bool setData(int column, const QVariant &value); int getModelIndex() const { return modelIndex; } + int getCategoryIndex() const { return categoryIndex; } private: QList childItems; QVector itemData; TreeItem * parentItem; + int categoryIndex; int modelIndex; }; @@ -92,6 +94,10 @@ class TreeModel : public QAbstractItemModel return getItem(index)->getModelIndex(); } + int getCategoryIndex(const QModelIndex & index) const { + return getItem(index)->getCategoryIndex(); + } + private: TreeItem * getItem(const QModelIndex & index) const; TreeItem * rootItem;