void ConfigDocumentXML::contextMenu(QPopupMenu& _menu) { menuAddSection_ = new QPopupMenu(&_menu); _menu.insertItem("Add Section", menuAddSection_); Miro::CFG::QStringVector childSections; QListViewItem * item = listViewItem()->firstChild(); while (item != NULL) { childSections.push_back(item->text(0)); item = item->nextSibling(); } // submenu: add all section names // not yet available in the document Miro::CFG::QStringVector sections = ConfigFile::instance()->description().groups(); Miro::CFG::QStringVector::const_iterator first, last = sections.end(); for (first = sections.begin(); first != last; ++first) { // if this section already exists, don't add it Miro::CFG::QStringVector::const_iterator i = std::find(childSections.begin(), childSections.end(), *first); if (i != childSections.end()) continue; // count the final parameter classes in the section int itemCount = 0; Miro::CFG::GroupMap::const_iterator f, l; ConfigFile::instance()->description().getGroupedTypes(*first, f, l); for (; f != l; ++f) { if (f->second.isFinal()) { ++itemCount; } } // if this section is empty, don't add it if (itemCount == 0) continue; menuAddSection_->insertItem(*first); } connect(menuAddSection_, SIGNAL(activated(int)), this, SLOT(onAddSection(int))); }
void ConfigDocumentXML::contextMenu(QMenu& _menu) { // The context menu has one QAction, "Add Section" // The "Add Section" menu is a submenu of the menu passed as argument. // Its signal is invoked when it is activated, not triggered. menuAddSection_ = _menu.addMenu(tr("Add Section")); // Construct a list of all siblings of the QTreeWidgetItem Miro::CFG::QStringVector childSections; // Fetch the QTreeWidgetItem QTreeWidgetItem * const pTreeWidgetItem = treeWidgetItem(); assert(pTreeWidgetItem != NULL); // For each child of the QTreeWidgetItem for (int i = 0; i < pTreeWidgetItem->childCount(); ++i) { // Fetch the ith sibling const QTreeWidgetItem * const pChildTreeWidgetItem = pTreeWidgetItem->child(i); assert(pChildTreeWidgetItem != NULL); // Fetch the sibling's label text const QString text = pChildTreeWidgetItem->text(0); // Append the label text to the QStringVector childSections.push_back(text); } // The QStringVector should have as many elements as the parent has children assert(static_cast<size_t>(pTreeWidgetItem->childCount()) == static_cast<size_t>(childSections.size())); // submenu: add all section names not yet available in the document Miro::CFG::QStringVector sections = ConfigFile::instance()->description().groups(); Miro::CFG::QStringVector::const_iterator first, last = sections.end(); for (first = sections.begin(); first != last; ++first) { // if this section already exists, don't add it Miro::CFG::QStringVector::const_iterator i = std::find(childSections.begin(), childSections.end(), *first); if (i != childSections.end()) continue; // count the final parameter classes in the section int itemCount = 0; Miro::CFG::GroupMap::const_iterator f, l; ConfigFile::instance()->description().getGroupedTypes(*first, f, l); for (; f != l; ++f) { if (f->second.isFinal()) { ++itemCount; } } // if this section is empty, don't add it if (itemCount == 0) continue; // The name of the Action const QString name = *first; // From "http://qt-project.org/doc/qt4-8/qaction.html": // "We recommend that actions are created as children of the window they // are used in. In most cases, actions will be children of the application's // main window." // This class doesn't have a handle to any window. QWidget * const pActionParent = NULL; QAction * const pAction = new QAction(name, pActionParent); connect(pAction, SIGNAL(activated(int)), this, SLOT(onAddSection(int))); // Add the Action to the Menu menuAddSection_->addAction(pAction); } }