File indexing completed on 2024-05-05 04:41:04

0001 /*
0002     SPDX-FileCopyrightText: 2010 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "uihelper.h"
0008 
0009 #include "debug.h"
0010 
0011 #include <interfaces/icore.h>
0012 #include <interfaces/iuicontroller.h>
0013 #include <KParts/MainWindow>
0014 #include <QDebug>
0015 #include <QMenuBar>
0016 #include <sublime/area.h>
0017 #include <sublime/view.h>
0018 
0019 using namespace KDevelop;
0020 
0021 UiHelper::UiHelper(QObject* parent): QObject(parent)
0022 {}
0023 
0024 QAction* findActionRec(const QStringList& path, const QList<QAction*>& actions)
0025 {
0026     QStringList newPath = path;
0027     QString current = newPath.takeFirst();
0028     
0029     for (QAction* a : actions) {
0030         if(a->objectName() == current) {
0031             if(newPath.isEmpty())
0032                 return a;
0033             else if(a->menu())
0034                 return findActionRec(newPath, a->menu()->actions());
0035             else
0036                 qCDebug(PLUGIN_WELCOMEPAGE) << "shouldn't get here:" << path;
0037         }
0038     }
0039     
0040     qWarning() << "error: action path not found: " << path;
0041     return nullptr;
0042 }
0043 
0044 QAction* UiHelper::retrieveMenuAction(const QString& menuPath)
0045 {
0046     QMenuBar* m = ICore::self()->uiController()->activeMainWindow()->menuBar();
0047 
0048     QAction* a = findActionRec(menuPath.split(QLatin1Char('/')), m->actions());
0049     return a;
0050 }
0051 
0052 void UiHelper::setArea(const QString& name)
0053 {
0054     ICore::self()->uiController()->switchToArea(name, IUiController::ThisWindow);
0055 }
0056 
0057 void UiHelper::raiseToolView(const QString& id)
0058 {
0059     const QList<Sublime::View*> views = ICore::self()->uiController()->activeArea()->toolViews();
0060     for (Sublime::View* v : views) {
0061         QWidget* w=v->widget();
0062         if(w && id==w->objectName())
0063             ICore::self()->uiController()->raiseToolView(w);
0064     }
0065 }
0066 
0067 void UiHelper::showMenu(const QString& name)
0068 {
0069     QAction* action = retrieveMenuAction(name);
0070     Q_ASSERT(action);
0071     Q_ASSERT(action->menu());
0072     
0073     action->menu()->popup(QCursor::pos());
0074 }
0075 
0076 #include "moc_uihelper.cpp"