/** * Returns the model index of an item in the tree. * \param row The row where the item is located * \param column The column where the item is located * \param parent The parent of the item * \return The model index of the item */ QModelIndex StationTreeModel::index( int row, int column, const QModelIndex &parent /*= QModelIndex()*/ ) const { if (!hasIndex(row, column, parent)) return QModelIndex(); ModelTreeItem* parentItem; if (!parent.isValid()) parentItem = (ModelTreeItem*)(_rootItem); else parentItem = static_cast<ModelTreeItem*>(parent.internalPointer()); ModelTreeItem* childItem = (ModelTreeItem*)(parentItem->child(row)); if (childItem) { QModelIndex newIndex = createIndex(row, column, childItem); // assign ModelIndex to BaseItem so it can communicate with the model BaseItem* item = childItem->getItem(); if ( item != nullptr ) item->setModelIndex(newIndex); return newIndex; } else return QModelIndex(); }
/** * Inserts a subtree under _rootItem. * \param listName Name of the new subtree. If no name is given a default name is assigned. * \param stations The list with stations to be added as children of that subtree */ void StationTreeModel::addStationList(QString listName, const std::vector<GeoLib::Point*>* stations) { QList<QVariant> grpName; if (listName.compare("") == 0) // if no name is given a default name is assigned { listName = "List"; listName.append(QString::number(rowCount() + 1)); } grpName << listName << "" << "" << ""; ModelTreeItem* group = new ModelTreeItem(grpName, _rootItem, new BaseItem(listName, stations)); _lists.push_back(group); _rootItem->appendChild(group); int vectorSize = stations->size(); for (int i = 0; i < vectorSize; i++) { QList<QVariant> stn; stn << QString::fromStdString(static_cast<GeoLib::Station*>((*stations)[i])->getName()) << QString::number((*(*stations)[i])[0],'f') << QString::number((*(*stations)[i])[1],'f') << QString::number((*(*stations)[i])[2],'f'); ModelTreeItem* child = new ModelTreeItem(stn, group); child->setStation(static_cast<GeoLib::Station*>((*stations)[i])); group->appendChild(child); } qDebug() << "List" << listName << "loaded, " << stations->size() << "items added."; reset(); }
/** * Returns the Station-Object of the ModelTreeItem with the given index and the name of the list this station belongs to. * \param index Index of the requested item * \param listName Here, the method will put the name of the list this station belongs to. * \return The station object associated with the tree item */ GeoLib::Station* StationTreeModel::stationFromIndex( const QModelIndex& index, QString &listName ) const { if (index.isValid()) { ModelTreeItem* treeItem = static_cast<ModelTreeItem*>(index.internalPointer()); TreeItem* parentItem = treeItem->parentItem(); listName = parentItem->data(0).toString(); return treeItem->getStation(); } else return nullptr; }
QString Model::getTypeName(QModelIndex const &index, int const role) const { do { if (!index.isValid()) break; ModelTreeItem *item = static_cast<ModelTreeItem*>(index.internalPointer()); if (!item) break; QString selectedProperty = findPropertyName(item->id(), role); return mEditorManager.getTypeName(item->id(), selectedProperty); } while (false); return QString(); }
void StationTreeView::contextMenuEvent( QContextMenuEvent* event ) { QModelIndex index = this->selectionModel()->currentIndex(); ModelTreeItem* item = static_cast<ModelTreeItem*>(index.internalPointer()); if (!item) // Otherwise sometimes it crashes when (unmotivated ;-) ) clicking in a treeview return; // The current index refers to a parent item (e.g. a listname) if (item->childCount() > 0) { QMenu menu; QAction* propertyAction = menu.addAction("Display list properties..."); QAction* exportAction = menu.addAction("Export to GMS..."); QAction* saveAction = menu.addAction("Save to file..."); menu.addSeparator(); QAction* removeAction = menu.addAction("Remove station list"); connect(propertyAction, SIGNAL(triggered()), this, SLOT(showPropertiesDialog())); connect(exportAction, SIGNAL(triggered()), this, SLOT(exportList())); connect(saveAction, SIGNAL(triggered()), this, SLOT(saveList())); connect(removeAction, SIGNAL(triggered()), this, SLOT(removeStationList())); menu.exec(event->globalPos()); } // The current index refers to a station object else { QString temp_name; QMenu menu; if (static_cast<StationTreeModel*>(model())->stationFromIndex(index, temp_name)->type() == GeoLib::Station::BOREHOLE) { QAction* stratAction = menu.addAction("Display Stratigraphy..."); QAction* exportAction = menu.addAction("Export to GMS..."); connect(stratAction, SIGNAL(triggered()), this, SLOT(displayStratigraphy())); connect(exportAction, SIGNAL(triggered()), this, SLOT(exportStation())); menu.exec(event->globalPos()); } else { menu.addAction("View Information..."); QAction* showDiagramAction = menu.addAction("View Diagram..."); connect(showDiagramAction, SIGNAL(triggered()), this, SLOT(showDiagramPrefsDialog())); menu.exec(event->globalPos()); } } }
/** * Removes the TreeItem with the given Index including all its children */ void StationTreeModel::removeStationList(QModelIndex index) { if (index.isValid()) // { ModelTreeItem* item = static_cast<ModelTreeItem*>(getItem(index)); // also delete the lists entry in the list directory of the model for (size_t i = 0; i < _lists.size(); i++) if (item == _lists[i]) _lists.erase(_lists.begin() + i); removeRows(0, item->childCount(), index); removeRows(item->row(), 1, parent(index)); } }
bool Model::setData(QModelIndex const &index, QVariant const &value, int role) { if (index.isValid()) { ModelTreeItem *item = static_cast<ModelTreeItem*>(index.internalPointer()); switch (role) { case Qt::DisplayRole: case Qt::EditRole: mApi.setName(item->id(), value.toString()); emit nameChanged(index); break; case roles::positionRole: mApi.setProperty(item->id(), "position", value); break; case roles::configurationRole: mApi.setProperty(item->id(), "configuration", value); break; case roles::fromRole: mApi.setFrom(item->id(), value.value<Id>()); break; case roles::toRole: mApi.setTo(item->id(), value.value<Id>()); break; case roles::fromPortRole: mApi.setFromPort(item->id(), value.toDouble()); break; case roles::toPortRole: mApi.setToPort(item->id(), value.toDouble()); break; default: if (role >= roles::customPropertiesBeginRole) { QString selectedProperty = findPropertyName(item->id(), role); mApi.setProperty(item->id(), selectedProperty, value); break; } Q_ASSERT(role < Qt::UserRole); return false; } emit dataChanged(index, index); return true; } return false; }
QVariant Model::data(QModelIndex const &index, int role) const { if (index.isValid()) { ModelTreeItem *item = static_cast<ModelTreeItem*>(index.internalPointer()); Q_ASSERT(item); switch (role) { case Qt::DisplayRole: case Qt::EditRole: return mApi.name(item->id()); case Qt::DecorationRole: return mEditorManager.icon(item->id()); case roles::idRole: return item->id().toVariant(); case roles::positionRole: return mApi.property(item->id(), "position"); case roles::fromRole: return mApi.from(item->id()).toVariant(); case roles::toRole: return mApi.to(item->id()).toVariant(); case roles::fromPortRole: return mApi.fromPort(item->id()); case roles::toPortRole: return mApi.toPort(item->id()); case roles::configurationRole: return mApi.property(item->id(), "configuration"); } if (role >= roles::customPropertiesBeginRole) { QString selectedProperty = findPropertyName(item->id(), role); return mApi.property(item->id(), selectedProperty); } Q_ASSERT(role < Qt::UserRole); return QVariant(); } else { return QVariant(); } }