diff --git a/Plugins/org.blueberry.ui.qt/src/internal/berryReopenEditorMenu.cpp b/Plugins/org.blueberry.ui.qt/src/internal/berryReopenEditorMenu.cpp index ed9001b401..9126eb59e3 100644 --- a/Plugins/org.blueberry.ui.qt/src/internal/berryReopenEditorMenu.cpp +++ b/Plugins/org.blueberry.ui.qt/src/internal/berryReopenEditorMenu.cpp @@ -1,300 +1,300 @@ /*============================================================================ 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 "berryReopenEditorMenu.h" #include #include #include #include #include "berryEditorHistory.h" #include "berryEditorHistoryItem.h" #include "berryWorkbench.h" #include "berryWorkbenchPlugin.h" #include "berryPreferenceConstants.h" #include #include #include #include namespace berry { ReopenEditorMenu::ReopenEditorMenu(IWorkbenchWindow* window, const QString& id, bool showSeparator) : ContributionItem(id) , window(window) , history(nullptr) , showSeparator(showSeparator) , dirty(true) { IWorkbench* workbench = window->GetWorkbench(); if (Workbench* w = dynamic_cast(workbench)) { history = w->GetEditorHistory(); } } QString ReopenEditorMenu::CalcText(int index, const QString& name, const QString& toolTip, bool rtl) { QString sb; int mnemonic = index + 1; QString nm = QString::number(mnemonic); if (mnemonic <= MAX_MNEMONIC_SIZE) { nm.prepend('&'); } QString fileName = name; QString pathName = toolTip; if (pathName == fileName) { // tool tip text isn't necessarily a path; // sometimes it's the same as name, so it shouldn't be treated as a path then pathName.clear(); } QFileInfo path(pathName); // if last segment in path is the fileName, remove it if (path.fileName() == fileName) { path.setFile(path.path()); pathName = path.absoluteFilePath(); } if ((fileName.size() + pathName.size()) <= (MAX_TEXT_LENGTH - 4)) { // entire item name fits within maximum length sb += fileName; if (!pathName.isEmpty()) { sb += " [" + pathName + "]"; } } else { // need to shorten the item name int length = fileName.size(); if (length > MAX_TEXT_LENGTH) { // file name does not fit within length, truncate it - sb += fileName.leftRef(MAX_TEXT_LENGTH - 3); + sb += QStringView(fileName).left(MAX_TEXT_LENGTH - 3); sb += "..."; } else if (length > MAX_TEXT_LENGTH - 7) { sb += fileName; } else { sb += fileName; QStringList pathSegments = path.absoluteFilePath().split('/', Qt::SkipEmptyParts); int segmentCount = pathSegments.size(); if (segmentCount > 0) { length += 7; // 7 chars are taken for " [...]" sb += " ["; // Add first n segments that fit int i = 0; while (i < segmentCount && length < MAX_TEXT_LENGTH) { const QString& segment = pathSegments[i]; if (length + segment.size() < MAX_TEXT_LENGTH) { sb += segment + QDir::separator(); length += segment.size() + 1; i++; } else if (i == 0) { // append at least part of the first segment - sb += segment.leftRef(MAX_TEXT_LENGTH - length); + sb += QStringView(segment).left(MAX_TEXT_LENGTH - length); length = MAX_TEXT_LENGTH; break; } else { break; } } sb += "..."; i = segmentCount - 1; // Add last n segments that fit while (i > 0 && length < MAX_TEXT_LENGTH) { const QString& segment = pathSegments[i]; if (length + segment.size() < MAX_TEXT_LENGTH) { sb += QDir::separator(); sb += segment; length += segment.size() + 1; i--; } else { break; } } sb.append("]"); } } } QString process; if (rtl) { process = sb + " " + nm; } else { process = nm + " " + sb; } //return TextProcessor.process(process, TextProcessor.getDefaultDelimiters() + "[]"); return process; } void ReopenEditorMenu::Fill(QMenu* menu, QAction* before) { if (window->GetActivePage() == nullptr || window->GetActivePage()->GetPerspective().IsNull()) { return; } if (MenuManager* mm = dynamic_cast(this->GetParent())) { mm->connect(mm, SIGNAL(AboutToShow(IMenuManager*)), this, SLOT(MenuAboutToShow(IMenuManager*))); } int itemsToShow = WorkbenchPlugin::GetDefault()->GetPreferences()->GetInt(PreferenceConstants::RECENT_FILES, 6); if (itemsToShow == 0 || history == nullptr) { return; } // Get items. QList historyItems = history->GetItems(); int n = std::min(itemsToShow, historyItems.size()); if (n <= 0) { return; } if (showSeparator) { menu->addSeparator(); } struct _SafeRunnable : public ISafeRunnable { QMenu* menu; QAction* before; EditorHistoryItem::Pointer item; const int historyIndex; _SafeRunnable(QMenu* menu, QAction* before, EditorHistoryItem::Pointer item, int index) : menu(menu), before(before), item(item), historyIndex(index) {} void Run() override { QString text = ReopenEditorMenu::CalcText(historyIndex, item); auto mi = new QAction(text, nullptr); menu->insertAction(before, mi); mi->setData(QVariant::fromValue(item)); } void HandleException(const ctkException& e) override { // just skip the item if there's an error, // e.g. in the calculation of the shortened name WorkbenchPlugin::Log(this->GetClassName(), "Fill", e); } }; for (int i = 0; i < n; i++) { EditorHistoryItem::Pointer item = historyItems[i]; ISafeRunnable::Pointer runnable(new _SafeRunnable(menu, before, item, i)); SafeRunner::Run(runnable); } dirty = false; } bool ReopenEditorMenu::IsDirty() const { return dirty; } bool ReopenEditorMenu::IsDynamic() const { return true; } void ReopenEditorMenu::Open(const EditorHistoryItem::Pointer& item) { IWorkbenchPage::Pointer page = window->GetActivePage(); if (page.IsNotNull()) { try { QString itemName = item->GetName(); if (!item->IsRestored()) { item->RestoreState(); } IEditorInput::Pointer input = item->GetInput(); IEditorDescriptor::Pointer desc = item->GetDescriptor(); if (!input || !desc) { QString title = "Problems opening editor"; QString msg = QString("Unable to open %1.").arg(itemName); QMessageBox::warning(window->GetShell()->GetControl(), title, msg); history->Remove(item); } else { page->OpenEditor(input, desc->GetId()); } } catch (const PartInitException& e2) { QString title = "Problems opening editor"; QMessageBox::warning(window->GetShell()->GetControl(), title, e2.what()); history->Remove(item); } } } QString ReopenEditorMenu::CalcText(int index, const EditorHistoryItem::Pointer& item) { // IMPORTANT: avoid accessing the item's input since // this can require activating plugins. // Instead, ask the item for the info, which can // consult its memento if it is not restored yet. return CalcText(index, item->GetName(), item->GetToolTipText(), false); // Window::GetDefaultOrientation() == SWT.RIGHT_TO_LEFT); } void ReopenEditorMenu::MenuAboutToShow(IMenuManager* manager) { manager->MarkDirty(); dirty = true; } } diff --git a/Plugins/org.blueberry.ui.qt/src/internal/berrySwitchToWindowMenu.cpp b/Plugins/org.blueberry.ui.qt/src/internal/berrySwitchToWindowMenu.cpp index 88de2a2fa0..c92f6dad15 100644 --- a/Plugins/org.blueberry.ui.qt/src/internal/berrySwitchToWindowMenu.cpp +++ b/Plugins/org.blueberry.ui.qt/src/internal/berrySwitchToWindowMenu.cpp @@ -1,155 +1,155 @@ /*============================================================================ 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 "berrySwitchToWindowMenu.h" #include #include #include #include #include #include #include #include namespace berry { void SwitchToWindowMenu::AboutToShow(IMenuManager* manager) { manager->MarkDirty(); dirty = true; } void SwitchToWindowMenu::MenuItemTriggered(int index) { if (index < 0 || index >= windows.size()) return; IWorkbenchWindow* window = windows[index]; Shell::Pointer windowShell = window->GetShell(); if (windowShell->GetMinimized()) { windowShell->SetMinimized(false); } windowShell->SetActive(); windowShell->GetControl()->raise(); } QString SwitchToWindowMenu::CalcText(int number, IWorkbenchWindow* window) { QString suffix = window->GetShell()->GetText(); if (suffix.isEmpty()) { return QString(); } QString sb; if (number < 10) { sb.append('&'); } sb.append(QString::number(number)); sb.append(' '); if (suffix.size() <= MAX_TEXT_LENGTH) { sb.append(suffix); } else { - sb.append(suffix.leftRef(MAX_TEXT_LENGTH)); + sb.append(QStringView(suffix).left(MAX_TEXT_LENGTH)); sb.append("..."); } return sb; } SwitchToWindowMenu::SwitchToWindowMenu(IWorkbenchWindow* window, const QString& id, bool showSeparator) : ContributionItem(id) , workbenchWindow(window) , showSeparator(showSeparator) , dirty(true) { } void SwitchToWindowMenu::Fill(QMenu* menu, QAction* before) { // Get workbench windows. IWorkbench* workbench = workbenchWindow->GetWorkbench(); windows.clear(); for (const auto &window : workbench->GetWorkbenchWindows()) { windows.push_back(window.GetPointer()); } // avoid showing the separator and list for 0 or 1 items if (windows.size() < 2) { return; } if (MenuManager* mm = dynamic_cast(GetParent())) { this->connect(mm, SIGNAL(AboutToShow(IMenuManager*)), SLOT(AboutToShow(IMenuManager*))); } if (!dirty) { return; } // Add separator. if (showSeparator) { menu->insertSeparator(before); } // Add one item for each window. auto actionGroup = new QActionGroup(menu); actionGroup->setExclusive(true); auto signalMapper = new QSignalMapper(menu); connect(signalMapper, SIGNAL(mapped(int)), SLOT(MenuItemTriggered(int))); int count = 0; for (auto window : qAsConst(windows)) { // can encounter disposed shells if this update is in response to a shell closing //if (!window->GetShell()->IsDisposed()) //{ QString name = CalcText(count, window); if (!name.isEmpty()) { auto mi = new QAction(name, menu); mi->setCheckable(true); mi->setChecked(window == workbenchWindow); actionGroup->addAction(mi); menu->insertAction(before, mi); signalMapper->setMapping(mi, count); connect(mi, SIGNAL(triggered()), signalMapper, SLOT(map())); } ++count; } //} dirty = false; } bool SwitchToWindowMenu::IsDirty() const { return dirty; } bool SwitchToWindowMenu::IsDynamic() const { return true; } }