[Companion] It's now possible to add Categories

This commit is contained in:
Bertrand Songis
2017-01-18 08:27:17 +01:00
parent e740a88e13
commit 8abc2a7179
4 changed files with 58 additions and 41 deletions
+26 -17
View File
@@ -129,15 +129,13 @@ void MdiChild::deleteSelectedModels()
void MdiChild::showModelsListContextMenu(const QPoint & pos) void MdiChild::showModelsListContextMenu(const QPoint & pos)
{ {
int modelIndex = getCurrentRow(); QModelIndex modelIndex = ui->modelsList->indexAt(pos);
QPoint globalPos = ui->modelsList->mapToGlobal(pos); QPoint globalPos = ui->modelsList->mapToGlobal(pos);
QMenu contextMenu; QMenu contextMenu;
if (modelsListModel->getModelIndex(modelIndex) >= 0) {
const QClipboard * clipboard = QApplication::clipboard(); const QClipboard * clipboard = QApplication::clipboard();
const QMimeData * mimeData = clipboard->mimeData(); const QMimeData * mimeData = clipboard->mimeData();
bool hasData = mimeData->hasFormat("application/x-companion"); bool hasData = mimeData->hasFormat("application/x-companion");
if (modelIndex >= 0) {
contextMenu.addAction(CompanionIcon("edit.png"), tr("&Edit"), this, SLOT(modelEdit())); 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("open.png"), tr("&Restore from backup"), this, SLOT(loadBackup()));
contextMenu.addAction(CompanionIcon("wizard.png"), tr("&Model Wizard"), this, SLOT(wizardEdit())); 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")); contextMenu.addAction(CompanionIcon("simulate.png"), tr("&Simulate model"), this, SLOT(modelSimulate()), tr("Alt+S"));
} }
else if (IS_HORUS(GetCurrentFirmware()->getBoard())) { 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); contextMenu.exec(globalPos);
} }
@@ -334,22 +337,28 @@ void MdiChild::generalEdit()
t->show(); t->show();
} }
void MdiChild::categoryAdd()
{
CategoryData category("New category");
radioData.categories.push_back(category);
setModified();
}
void MdiChild::modelAdd() void MdiChild::modelAdd()
{ {
if (radioData.categories.size() == 0) { int categoryIndex = modelsListModel->getCategoryIndex(ui->modelsList->currentIndex());
CategoryData category("Models"); if (categoryIndex < 0 || categoryIndex >= (int)radioData.categories.size()) {
radioData.categories.push_back(category); return;
} }
ModelData model; ModelData model;
model.category = radioData.categories.size() - 1; model.category = categoryIndex;
model.used = true; model.used = true;
sprintf(model.filename, "model%lu.bin", radioData.models.size()+1); 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.models.push_back(model);
radioData.setCurrentModel(radioData.models.size() - 1); radioData.setCurrentModel(radioData.models.size() - 1);
setModified(); setModified();
//modelEdit();
} }
void MdiChild::modelEdit() void MdiChild::modelEdit()
+1
View File
@@ -70,6 +70,7 @@ class MdiChild : public QWidget
void showModelsListContextMenu(const QPoint & pos); void showModelsListContextMenu(const QPoint & pos);
void checkAndInitModel(int row); void checkAndInitModel(int row);
void generalEdit(); void generalEdit();
void categoryAdd();
void modelAdd(); void modelAdd();
void modelEdit(); void modelEdit();
void wizardEdit(); void wizardEdit();
+22 -21
View File
@@ -23,13 +23,15 @@
TreeItem::TreeItem(const QVector<QVariant> & itemData): TreeItem::TreeItem(const QVector<QVariant> & itemData):
itemData(itemData), itemData(itemData),
parentItem(NULL), parentItem(NULL),
categoryIndex(-1),
modelIndex(-1) modelIndex(-1)
{ {
} }
TreeItem::TreeItem(TreeItem * parent, int modelIndex): TreeItem::TreeItem(TreeItem * parent, int categoryIndex, int modelIndex):
itemData(parent->columnCount()), itemData(parent->columnCount()),
parentItem(parent), parentItem(parent),
categoryIndex(categoryIndex),
modelIndex(modelIndex) modelIndex(modelIndex)
{ {
} }
@@ -67,9 +69,9 @@ QVariant TreeItem::data(int column) const
return itemData.value(column); 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); childItems.insert(childItems.size(), item);
return item; return item;
} }
@@ -155,10 +157,10 @@ Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
return Qt::ItemIsEditable | QAbstractItemModel::flags(index); return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
} }
TreeItem * TreeModel::getItem(const QModelIndex &index) const TreeItem * TreeModel::getItem(const QModelIndex & index) const
{ {
if (index.isValid()) { if (index.isValid()) {
TreeItem * item = static_cast<TreeItem*>(index.internalPointer()); TreeItem * item = static_cast<TreeItem *>(index.internalPointer());
if (item) { if (item) {
return item; return item;
} }
@@ -166,8 +168,7 @@ TreeItem * TreeModel::getItem(const QModelIndex &index) const
return rootItem; return rootItem;
} }
QVariant TreeModel::headerData(int section, Qt::Orientation orientation, QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
int role) const
{ {
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section); return rootItem->data(section);
@@ -175,7 +176,7 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
return QVariant(); 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) if (parent.isValid() && parent.column() != 0)
return QModelIndex(); return QModelIndex();
@@ -189,13 +190,13 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
return QModelIndex(); return QModelIndex();
} }
QModelIndex TreeModel::parent(const QModelIndex &index) const QModelIndex TreeModel::parent(const QModelIndex & index) const
{ {
if (!index.isValid()) if (!index.isValid())
return QModelIndex(); return QModelIndex();
TreeItem *childItem = getItem(index); TreeItem * childItem = getItem(index);
TreeItem *parentItem = childItem->parent(); TreeItem * parentItem = childItem->parent();
if (parentItem == rootItem) if (parentItem == rootItem)
return QModelIndex(); return QModelIndex();
@@ -203,11 +204,10 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
return createIndex(parentItem->childNumber(), 0, parentItem); 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); TreeItem * parentItem = getItem(parent);
bool success = true; bool success;
beginRemoveRows(parent, position, position + rows - 1); beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->removeChildren(position, rows); 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 int TreeModel::rowCount(const QModelIndex &parent) const
{ {
TreeItem *parentItem = getItem(parent); TreeItem * parentItem = getItem(parent);
return parentItem->childCount(); 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) if (role != Qt::EditRole)
return false; return false;
TreeItem *item = getItem(index); TreeItem * item = getItem(index);
bool result = item->setData(index.column(), value); bool result = item->setData(index.column(), value);
if (result) if (result)
@@ -253,7 +253,7 @@ void TreeModel::refresh()
if (IS_HORUS(board)) { if (IS_HORUS(board)) {
for (unsigned i = 0; i < radioData->categories.size(); i++) { 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)); current->setData(0, QString(radioData->categories[i].name));
} }
} }
@@ -270,17 +270,18 @@ void TreeModel::refresh()
categoryItem = rootItem->child(model.category); categoryItem = rootItem->child(model.category);
} }
else { else {
model.category = 0;
if (!defaultCategoryItem) { if (!defaultCategoryItem) {
defaultCategoryItem = rootItem->appendChild(-1); defaultCategoryItem = rootItem->appendChild(0, -1);
defaultCategoryItem->setData(0, QObject::tr("Models")); defaultCategoryItem->setData(0, QObject::tr("Models"));
} }
categoryItem = defaultCategoryItem; categoryItem = defaultCategoryItem;
} }
current = categoryItem->appendChild(i); current = categoryItem->appendChild(model.category, i);
} }
} }
else { else {
current = rootItem->appendChild(i); current = rootItem->appendChild(0, i);
current->setData(currentColumn++, QString().sprintf("%02d", i + 1)); current->setData(currentColumn++, QString().sprintf("%02d", i + 1));
} }
+9 -3
View File
@@ -34,14 +34,14 @@ class TreeItem
{ {
public: public:
explicit TreeItem(const QVector<QVariant> & itemData); explicit TreeItem(const QVector<QVariant> & itemData);
explicit TreeItem(TreeItem * parent, int modelIndex = -1); explicit TreeItem(TreeItem * parent, int categoryIndex, int modelIndex);
~TreeItem(); ~TreeItem();
TreeItem *child(int number); TreeItem * child(int number);
int childCount() const; int childCount() const;
int columnCount() const; int columnCount() const;
QVariant data(int column) const; QVariant data(int column) const;
TreeItem * appendChild(int modelIndex); TreeItem * appendChild(int categoryIndex, int modelIndex);
TreeItem * parent(); TreeItem * parent();
bool removeChildren(int position, int count); bool removeChildren(int position, int count);
@@ -49,11 +49,13 @@ class TreeItem
bool setData(int column, const QVariant &value); bool setData(int column, const QVariant &value);
int getModelIndex() const { return modelIndex; } int getModelIndex() const { return modelIndex; }
int getCategoryIndex() const { return categoryIndex; }
private: private:
QList<TreeItem*> childItems; QList<TreeItem*> childItems;
QVector<QVariant> itemData; QVector<QVariant> itemData;
TreeItem * parentItem; TreeItem * parentItem;
int categoryIndex;
int modelIndex; int modelIndex;
}; };
@@ -92,6 +94,10 @@ class TreeModel : public QAbstractItemModel
return getItem(index)->getModelIndex(); return getItem(index)->getModelIndex();
} }
int getCategoryIndex(const QModelIndex & index) const {
return getItem(index)->getCategoryIndex();
}
private: private:
TreeItem * getItem(const QModelIndex & index) const; TreeItem * getItem(const QModelIndex & index) const;
TreeItem * rootItem; TreeItem * rootItem;