diff --git a/Plugins/org.blueberry.ui.qt/src/berryIViewRegistry.h b/Plugins/org.blueberry.ui.qt/src/berryIViewRegistry.h index 0ee69890b1..559948cbdb 100644 --- a/Plugins/org.blueberry.ui.qt/src/berryIViewRegistry.h +++ b/Plugins/org.blueberry.ui.qt/src/berryIViewRegistry.h @@ -1,79 +1,90 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef BERRYIVIEWREGISTRY_H_ #define BERRYIVIEWREGISTRY_H_ #include #include "berryIViewDescriptor.h" #include "berryIViewCategory.h" #include "berryIStickyViewDescriptor.h" namespace berry { /** * \ingroup org_blueberry_ui_qt * * The view registry maintains a list of views explicitly registered * against the view extension point. *

* The description of a given view is kept in a IViewDescriptor. *

*

* This interface is not intended to be implemented by clients. *

* * @see IViewDescriptor * @see IStickyViewDescriptor */ struct BERRY_UI_QT IViewRegistry { /** * Return a view descriptor with the given extension id. If no view exists * with the id return null. * Will also return null if the view descriptor exists, but * is filtered by an expression-based activity. * * @param id the id to search for * @return the descriptor or null */ virtual IViewDescriptor::Pointer Find(const QString& id) const = 0; /** * Returns an array of view categories. * * @return the categories. */ virtual QList GetCategories() = 0; /** * Return a list of views defined in the registry. * * @return the views. */ virtual QList GetViews() const = 0; + /** + * @brief Get views as multi map with categories as keys. + * + * Exclude views without category and categories that contain a literal '.', e.g. + * "org.blueberry.ui" or "org.mitk.views.general", as they typically do not have + * a corresponding tool bar. + * + * @return the views by vategory as described above. + */ + virtual QMultiMap GetViewsByCategory() const = 0; + /** * Return a list of sticky views defined in the registry. * * @return the sticky views. Never null. */ virtual QList GetStickyViews() const = 0; virtual ~IViewRegistry(); }; } #endif /*BERRYIVIEWREGISTRY_H_*/ diff --git a/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.cpp b/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.cpp index 309967e985..f2c7fa7e78 100644 --- a/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.cpp +++ b/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.cpp @@ -1,281 +1,301 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "berryViewRegistry.h" #include "berryPlatformUI.h" #include "berryWorkbenchPlugin.h" #include "berryWorkbenchRegistryConstants.h" #include "berryPlatform.h" namespace berry { ViewRegistry::ViewCategoryProxy::ViewCategoryProxy( IViewDescriptorCategoryPtr rawCategory) : rawCategory(rawCategory) { } QList ViewRegistry::ViewCategoryProxy::GetViews() const { return rawCategory->GetElements(); } QString ViewRegistry::ViewCategoryProxy::GetId() const { return rawCategory->GetId(); } QStringList ViewRegistry::ViewCategoryProxy::GetPath() const { QList path; QString rawParentPath = rawCategory->GetRawParentPath(); // nested categories are not supported yet // assume an empty raw parent path path.push_back(rawParentPath); return path; } QString ViewRegistry::ViewCategoryProxy::GetLabel() const { return rawCategory->GetLabel(); } QString ViewRegistry::ViewCategoryProxy::GetLocalId() const { return GetId(); } QString ViewRegistry::ViewCategoryProxy::GetPluginId() const { return rawCategory->GetPluginId(); } bool ViewRegistry::ViewCategoryProxy::operator==(const Object* o) const { if (const IViewCategory* other = dynamic_cast(o)) return this->GetId() == other->GetId(); return false; } uint ViewRegistry::ViewCategoryProxy::HashCode() const { return qHash(GetId()); } QString ViewRegistry::EXTENSIONPOINT_UNIQUE_ID = "org.blueberry.ui.views"; // PlatformUI::PLUGIN_ID + "." + WorkbenchRegistryConstants::PL_VIEWS; Category::Pointer ViewRegistry::InternalFindCategory(const QString& id) { for (QList::iterator itr = categories.begin(); itr != categories.end(); ++itr) { if (id == (*itr)->GetRootPath()) { return *itr; } } return IViewDescriptorCategoryPtr(nullptr); } IExtensionPoint::Pointer ViewRegistry::GetExtensionPointFilter() { return Platform::GetExtensionRegistry()->GetExtensionPoint(EXTENSIONPOINT_UNIQUE_ID); } const QString ViewRegistry::TAG_DESCRIPTION = "description"; ViewRegistry::ViewRegistry() : dirtyViewCategoryMappings(true) { miscCategory = new IViewDescriptorCategory(); this->Add(miscCategory); //PlatformUI.getWorkbench().getExtensionTracker().registerHandler(this, // ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter())); reader.ReadViews(Platform::GetExtensionRegistry(), this); } void ViewRegistry::Add(IViewDescriptorCategoryPtr desc) { /* fix for 1877 */ if (this->InternalFindCategory(desc->GetId()).IsNull()) { dirtyViewCategoryMappings = true; // Mark categories list as dirty categories.push_back(desc); // IConfigurationElement::Pointer element( // dynamic_cast( // desc->GetAdapter(typeid(IConfigurationElement)) // )); // if (element.IsNull()) // { // return; // } // PlatformUI::GetWorkbench()->GetExtensionTracker() // .registerObject(element->GetDeclaringExtension(), desc, // IExtensionTracker::REF_WEAK); } } void ViewRegistry::Add(ViewDescriptor::Pointer desc) { for (QList::const_iterator itr = views.begin(); itr != views.end(); ++itr) { if (desc.GetPointer() == itr->GetPointer()) return; } views.push_back(desc); dirtyViewCategoryMappings = true; //desc.activateHandler(); } void ViewRegistry::Add(StickyViewDescriptor::Pointer desc) { if (std::find(sticky.begin(), sticky.end(), desc) == sticky.end()) { sticky.push_back(desc); // PlatformUI::GetWorkbench()->GetExtensionTracker() // .registerObject(desc.getConfigurationElement().getDeclaringExtension(), // desc, IExtensionTracker.REF_WEAK); } } IViewDescriptor::Pointer ViewRegistry::Find(const QString& id) const { for (QList::const_iterator itr = views.begin(); itr != views.end(); ++itr) { if (id == (*itr)->GetId()) { return *itr; } } return IViewDescriptor::Pointer(nullptr); } IViewCategory::Pointer ViewRegistry::FindCategory(const QString& id) { this->MapViewsToCategories(); IViewDescriptorCategoryPtr category(this->InternalFindCategory(id)); if (category.IsNull()) { return IViewCategory::Pointer(nullptr); } IViewCategory::Pointer cat(new ViewCategoryProxy(category)); return cat; } QList ViewRegistry::GetCategories() { this->MapViewsToCategories(); QList retArray; for (QList::iterator itr = categories.begin(); itr != categories.end(); ++itr) { retArray.push_back(IViewCategory::Pointer(new ViewCategoryProxy(*itr))); } return retArray; } QList ViewRegistry::GetStickyViews() const { return sticky; } Category::Pointer ViewRegistry::GetMiscCategory() const { return miscCategory; } QList ViewRegistry::GetViews() const { return views; } +QMultiMap ViewRegistry::GetViewsByCategory() const +{ + QMultiMap result; + + const auto views = this->GetViews(); + + for (auto view : views) + { + QString category; + + if (auto categoryPath = view->GetCategoryPath(); !categoryPath.isEmpty()) + category = categoryPath.back(); + + if (!category.isEmpty() && !category.contains('.') && !view->IsInternal()) + result.insert(category, view); + } + + return result; +} + void ViewRegistry::MapViewsToCategories() { if (dirtyViewCategoryMappings) { dirtyViewCategoryMappings = false; // clear all category mappings for (QList::iterator i = categories.begin(); i != categories.end(); ++i) { (*i)->Clear(); // this is bad } miscCategory->Clear(); for (QList::iterator i = views.begin(); i != views.end(); ++i) { IViewDescriptor::Pointer desc(*i); IViewDescriptorCategoryPtr cat(nullptr); const QList& catPath = desc->GetCategoryPath(); if (catPath.size() > 0) { cat = this->InternalFindCategory(catPath[0]); } if (cat.IsNotNull()) { if (!cat->HasElement(desc)) { cat->AddElement(desc); } } else { if (catPath.size() > 0) { // If we get here, this view specified a category which // does not exist. Add this view to the 'Other' category // but give out a message (to the log only) indicating // this has been done. QString fmt("Category %1 not found for view %2. This view added to ''%3'' category."); WorkbenchPlugin::Log(fmt.arg(catPath[0]).arg(desc->GetId()).arg(miscCategory->GetLabel())); } miscCategory->AddElement(desc); } } } } //void ViewRegistry::AddExtension(IExtensionTracker tracker, // IExtension addedExtension) //{ // IConfigurationElement[] addedElements = addedExtension.getConfigurationElements(); // for (int i = 0; i < addedElements.length; i++) // { // IConfigurationElement element = addedElements[i]; // if (element.getName().equals(IWorkbenchRegistryConstants.TAG_VIEW)) // { // reader.readView(element); // } // else if (element.getName().equals(IWorkbenchRegistryConstants.TAG_CATEGORY)) // { // reader.readCategory(element); // } // else if (element.getName().equals(IWorkbenchRegistryConstants.TAG_STICKYVIEW)) // { // reader.readSticky(element); // } // } //} } // namespace berry diff --git a/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.h b/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.h index 91e010688b..161df4674b 100644 --- a/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.h +++ b/Plugins/org.blueberry.ui.qt/src/internal/berryViewRegistry.h @@ -1,227 +1,229 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef BERRYVIEWREGISTRY_H_ #define BERRYVIEWREGISTRY_H_ #include "berryIViewRegistry.h" #include "berryIViewCategory.h" #include "berryIViewDescriptor.h" #include "berryCategory.h" #include "berryViewDescriptor.h" #include "berryViewRegistryReader.h" #include "berryStickyViewDescriptor.h" #include "berryIExtensionPoint.h" namespace berry { /** * \ingroup org_blueberry_ui_internal * * The central manager for view descriptors. */ class ViewRegistry : public IViewRegistry { public: typedef Category IViewDescriptorCategory; typedef IViewDescriptorCategory::Pointer IViewDescriptorCategoryPtr; private: /** * Proxies a Category implementation. * */ class ViewCategoryProxy : public IViewCategory, public IPluginContribution { private: typedef Category IViewDescriptorCategory; IViewDescriptorCategoryPtr rawCategory; /** * Create a new instance of this class * * @param rawCategory the category */ public: ViewCategoryProxy(IViewDescriptorCategoryPtr rawCategory); /* * @see IViewCategory#GetViews() */ QList GetViews() const override; /* * @see IViewCategory#GetId() */ QString GetId() const override; /* * @see IViewCategory#GetPath() */ QStringList GetPath() const override; /* * @see IViewCategory#GetLabel() */ QString GetLabel() const override; /* * @see IPluginContribution#GetLocalId() */ QString GetLocalId() const override; /* * @see IPluginContribution#GetPluginId() */ QString GetPluginId() const override; /* * @see Object#operator==(Object*) */ bool operator==(const Object* o) const override; /* * @see Object#HashCode() */ uint HashCode() const override; }; private: static QString EXTENSIONPOINT_UNIQUE_ID; /** * A set that will only ever contain ViewDescriptors. */ QList views; // = new TreeSet(new Comparator() { // public int compare(Object o1, Object o2) { // String id1 = ((ViewDescriptor) o1).getId(); // String id2 = ((ViewDescriptor) o2).getId(); // // return id1.compareTo(id2); // }}); QList sticky; QList categories; IViewDescriptorCategoryPtr miscCategory; ViewRegistryReader reader; bool dirtyViewCategoryMappings; /** * Returns the category with no updating of the view/category mappings. * * @param id the category id * @return the Category * @since 3.1 */ IViewDescriptorCategoryPtr InternalFindCategory(const QString& id); SmartPointer GetExtensionPointFilter(); protected: static const QString TAG_DESCRIPTION; // = "description"; public: ViewRegistry(); /** * Add a category to the registry. * * @param desc the descriptor to add */ void Add(IViewDescriptorCategoryPtr desc); /** * Add a descriptor to the registry. * * @param desc the descriptor to add */ void Add(ViewDescriptor::Pointer desc); /** * Add a sticky descriptor to the registry. * * @param desc the descriptor to add */ void Add(StickyViewDescriptor::Pointer desc); /** * Find a descriptor in the registry. */ IViewDescriptor::Pointer Find(const QString& id) const override; /** * Find a category with a given name. * * @param id the id to search for * @return the category or null */ IViewCategory::Pointer FindCategory(const QString& id); /** * Get the list of view categories. */ QList GetCategories() override; /** * Get the list of sticky views minus the sticky views which failed the * Expressions check. */ QList GetStickyViews() const override; /** * Returns the Misc category. This may be null if there are * no miscellaneous views. * * @return the misc category or null */ IViewDescriptorCategoryPtr GetMiscCategory() const; /** * Get an enumeration of view descriptors. */ QList GetViews() const override; + QMultiMap GetViewsByCategory() const override; + /** * Adds each view in the registry to a particular category. * The view category may be defined in xml. If not, the view is * added to the "misc" category. */ void MapViewsToCategories(); /* (non-Javadoc) * @see org.blueberry.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.blueberry.core.runtime.dynamicHelpers.IExtensionTracker, org.blueberry.core.runtime.IExtension) */ //void AddExtension(IExtensionTracker tracker, IExtension addedExtension); }; } // namespace berry #endif /*BERRYVIEWREGISTRY_H_*/ diff --git a/Plugins/org.mitk.gui.qt.application/src/internal/QmitkToolBarsPreferencePage.cpp b/Plugins/org.mitk.gui.qt.application/src/internal/QmitkToolBarsPreferencePage.cpp index 60ec15202b..dc48ae13f9 100644 --- a/Plugins/org.mitk.gui.qt.application/src/internal/QmitkToolBarsPreferencePage.cpp +++ b/Plugins/org.mitk.gui.qt.application/src/internal/QmitkToolBarsPreferencePage.cpp @@ -1,208 +1,182 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "QmitkToolBarsPreferencePage.h" #include #include #include #include #include #include #include #include namespace { mitk::IPreferences* GetPreferences() { auto prefService = mitk::CoreServices::GetPreferencesService(); return prefService->GetSystemPreferences()->Node(QmitkApplicationConstants::TOOL_BARS_PREFERENCES); } - // Get views as multimap with categories as keys. - // - // Exclude views without category and categories that contain a literal '.', e.g. - // "org.blueberry.ui" or "org.mitk.views.general", as they typically do not have - // a corresponding tool bar. - std::multimap GetViewsByCategory() - { - std::multimap result; - - const auto workbench = berry::PlatformUI::GetWorkbench(); - const auto viewRegistry = workbench->GetViewRegistry(); - const auto views = viewRegistry->GetViews(); - - for (auto view : views) - { - QString category; - - if (auto categoryPath = view->GetCategoryPath(); !categoryPath.isEmpty()) - category = categoryPath.back(); - - if (!category.isEmpty() && !category.contains('.') && !view->IsInternal()) - result.emplace(category, view); - } - - return result; - } - // Get all toolbars of all (typically one) Workbench windows. std::vector GetToolBars() { std::vector result; const auto* workbench = berry::PlatformUI::GetWorkbench(); auto workbenchWindows = workbench->GetWorkbenchWindows(); for (auto workbenchWindow : workbenchWindows) { if (auto shell = workbenchWindow->GetShell(); shell.IsNotNull()) { if (const auto* mainWindow = qobject_cast(shell->GetControl()); mainWindow != nullptr) { for (auto child : mainWindow->children()) { if (auto toolBar = qobject_cast(child); toolBar != nullptr) result.push_back(toolBar); } } } } return result; } // Find a toolbar by object name and apply preferences. bool ApplyPreferences(const std::vector& toolBars, const QString& name, bool isVisible, bool showCategory) { auto it = std::find_if(toolBars.cbegin(), toolBars.cend(), [&name](const QToolBar* toolBar) { return toolBar->objectName() == name; }); if (it != toolBars.cend()) { auto toolBar = *it; toolBar->setVisible(isVisible); for (auto action : toolBar->actions()) { if (action->objectName() == "category") { action->setVisible(showCategory); break; } } return true; } return false; } } QmitkToolBarsPreferencePage::QmitkToolBarsPreferencePage() : m_Ui(new Ui::QmitkToolBarsPreferencePage), m_Control(nullptr) { } QmitkToolBarsPreferencePage::~QmitkToolBarsPreferencePage() { } void QmitkToolBarsPreferencePage::Init(berry::IWorkbench::Pointer) { } void QmitkToolBarsPreferencePage::CreateQtControl(QWidget* parent) { m_Control = new QWidget(parent); m_Ui->setupUi(m_Control); - const auto views = GetViewsByCategory(); + const auto views = berry::PlatformUI::GetWorkbench()->GetViewRegistry()->GetViewsByCategory(); - for (auto category = views.cbegin(), end = views.cend(); category != end; category = views.upper_bound(category->first)) + for(const auto category : views.uniqueKeys()) { auto categoryItem = new QTreeWidgetItem; - categoryItem->setText(0, category->first); + categoryItem->setText(0, category); categoryItem->setCheckState(0, Qt::Checked); - const auto range = views.equal_range(category->first); + auto [viewIter, end] = views.equal_range(category); - for (auto view = range.first; view != range.second; ++view) + while (viewIter != end) { auto viewItem = new QTreeWidgetItem; - viewItem->setText(0, view->second->GetLabel()); - viewItem->setIcon(0, view->second->GetImageDescriptor()); + viewItem->setText(0, (*viewIter)->GetLabel()); + viewItem->setIcon(0, (*viewIter)->GetImageDescriptor()); categoryItem->addChild(viewItem); + ++viewIter; } m_Ui->treeWidget->addTopLevelItem(categoryItem); } this->Update(); } QWidget* QmitkToolBarsPreferencePage::GetQtControl() const { return m_Control; } bool QmitkToolBarsPreferencePage::PerformOk() { auto prefs = GetPreferences(); bool showCategories = m_Ui->showCategoriesCheckBox->isChecked(); prefs->PutBool(QmitkApplicationConstants::TOOL_BARS_SHOW_CATEGORIES, showCategories); const auto toolBars = GetToolBars(); for (int i = 0, count = m_Ui->treeWidget->topLevelItemCount(); i < count; ++i) { const auto* item = m_Ui->treeWidget->topLevelItem(i); const auto category = item->text(0); const bool isVisible = item->checkState(0) == Qt::Checked; prefs->PutBool(category.toStdString(), isVisible); if (!ApplyPreferences(toolBars, category, isVisible, showCategories)) MITK_WARN << "Could not find tool bar for category \"" << category << "\" to set its visibility!"; } return true; } void QmitkToolBarsPreferencePage::PerformCancel() { } void QmitkToolBarsPreferencePage::Update() { const auto prefs = GetPreferences(); m_Ui->showCategoriesCheckBox->setChecked(prefs->GetBool(QmitkApplicationConstants::TOOL_BARS_SHOW_CATEGORIES, true)); for (int i = 0, count = m_Ui->treeWidget->topLevelItemCount(); i < count; ++i) { auto item = m_Ui->treeWidget->topLevelItem(i); const auto category = item->text(0).toStdString(); const bool isVisible = prefs->GetBool(category, true); item->setCheckState(0, isVisible ? Qt::Checked : Qt::Unchecked); } }