File indexing completed on 2025-02-02 05:43:13

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-2007 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "viewlistmenucontroller.hpp"
0010 
0011 // Kasten gui
0012 #include <Kasten/AbstractGroupedViews>
0013 #include <Kasten/ViewManager>
0014 // Kasten core
0015 #include <Kasten/AbstractDocument>
0016 // KF
0017 #include <KXMLGUIClient>
0018 #include <KXMLGUIFactory>
0019 #include <KActionCollection>
0020 #include <KLocalizedString>
0021 #include <KStringHandler>
0022 // Qt
0023 #include <QActionGroup>
0024 
0025 namespace Kasten {
0026 
0027 static constexpr int MaxEntryLength = 150;
0028 static constexpr char WindowsListActionListId[] = "windows_list";
0029 
0030 ViewListMenuController::ViewListMenuController(ViewManager* viewManager, AbstractGroupedViews* groupedViews,
0031                                                KXMLGUIClient* guiClient)
0032     : mViewManager(viewManager)
0033     , mGroupedViews(groupedViews)
0034     , mGuiClient(guiClient)
0035 {
0036     mWindowsActionGroup = new QActionGroup(this);   // TODO: do we use this only for the signal mapping?
0037 //     mWindowsActionGroup->setExclusive( true );
0038     connect(mWindowsActionGroup, &QActionGroup::triggered,
0039             this, &ViewListMenuController::onActionTriggered);
0040 
0041     connect(mViewManager, &ViewManager::opened,
0042             this, &ViewListMenuController::updateActions);
0043     connect(mViewManager, &ViewManager::closing,
0044             this, &ViewListMenuController::updateActions);
0045 
0046     updateActions();
0047 }
0048 
0049 void ViewListMenuController::setTargetModel(AbstractModel* model)
0050 {
0051     Q_UNUSED(model)
0052 }
0053 
0054 void ViewListMenuController::updateActions()
0055 {
0056     mGuiClient->unplugActionList(QLatin1String(WindowsListActionListId));
0057 
0058     qDeleteAll(mWindowsActionGroup->actions());
0059 
0060     const QVector<AbstractView*> views = mViewManager->views();
0061     const bool hasViews = (!views.isEmpty());
0062 
0063     if (hasViews) {
0064         // TODO: sortieren nach namen und erste 10 mit Zahl, siehe unten
0065         for (int v = 0; v < views.size(); ++v) {
0066             AbstractView* view = views.at(v);
0067             const QString title = KStringHandler::rsqueeze(view->title(), MaxEntryLength);
0068             auto* action = new QAction(v < 9 ? QStringLiteral("&%1 %2").arg(v + 1).arg(title) : title, mWindowsActionGroup);
0069             //         action->setCheckable( true );
0070 
0071             //         if(m_viewManager->activeView() && doc == m_viewManager->activeView()->document())
0072             //             action->setChecked(true);
0073             action->setData(QVariant::fromValue(view));
0074             mWindowsActionGroup->addAction(action);
0075         }
0076     } else {
0077         auto* noneAction = new QAction(i18nc("@item There are no windows.", "None."), mWindowsActionGroup);
0078         mWindowsActionGroup->addAction(noneAction);
0079     }
0080     mWindowsActionGroup->setEnabled(hasViews);
0081 
0082     mGuiClient->plugActionList(QLatin1String(WindowsListActionListId), mWindowsActionGroup->actions());
0083 }
0084 
0085 void ViewListMenuController::onActionTriggered(QAction* action)
0086 {
0087     auto* view = action->data().value<AbstractView*>();
0088     mGroupedViews->setViewFocus(view);
0089 }
0090 
0091 }
0092 
0093 #include "moc_viewlistmenucontroller.cpp"