File indexing completed on 2025-01-26 05:24:15

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009 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 "switchviewcontroller.hpp"
0010 
0011 // Kasten gui
0012 #include <Kasten/AbstractGroupedViews>
0013 #include <Kasten/AbstractView>
0014 // KF
0015 #include <KXMLGUIClient>
0016 #include <KActionCollection>
0017 // Qt
0018 #include <QActionGroup>
0019 
0020 namespace Kasten {
0021 
0022 SwitchViewController::SwitchViewController(AbstractGroupedViews* groupedViews, KXMLGUIClient* guiClient)
0023     : mGroupedViews(groupedViews)
0024 {
0025     mForwardAction = KStandardAction::forward(this, &SwitchViewController::forward, this);
0026 
0027     mBackwardAction = KStandardAction::back(this, &SwitchViewController::backward, this);
0028 
0029     KActionCollection* actionCollection = guiClient->actionCollection();
0030     actionCollection->addAction(QStringLiteral("window_next"), mForwardAction);
0031     actionCollection->addAction(QStringLiteral("window_previous"), mBackwardAction);
0032 
0033     connect(groupedViews, &AbstractGroupedViews::added,
0034             this, &SwitchViewController::updateActions);
0035     connect(groupedViews, &AbstractGroupedViews::removing,
0036             this, &SwitchViewController::updateActions);
0037     connect(groupedViews, &AbstractGroupedViews::viewFocusChanged,
0038             this, &SwitchViewController::updateActions);
0039 
0040     updateActions();
0041 }
0042 
0043 void SwitchViewController::setTargetModel(AbstractModel* model)
0044 {
0045     Q_UNUSED(model)
0046 }
0047 
0048 // TODO: think about moving this properties/abilities (hasNext/Previous,switchToNext/Previous) into a interface for the groupedview
0049 void SwitchViewController::updateActions()
0050 {
0051     bool hasNext;
0052     bool hasPrevious;
0053     const QVector<AbstractView*> viewList = mGroupedViews->viewList();
0054     if (viewList.isEmpty()) {
0055         hasNext = false;
0056         hasPrevious = false;
0057     } else {
0058         AbstractView* focussedView = mGroupedViews->viewFocus();
0059         const int indexOfFocussedView = viewList.indexOf(focussedView);
0060 
0061         hasNext = (indexOfFocussedView + 1 < viewList.size());
0062         hasPrevious = (indexOfFocussedView > 0);
0063     }
0064 
0065     mForwardAction->setEnabled(hasNext);
0066     mBackwardAction->setEnabled(hasPrevious);
0067 }
0068 
0069 void SwitchViewController::forward()
0070 {
0071     const QVector<AbstractView*> viewList = mGroupedViews->viewList();
0072     AbstractView* focussedView = mGroupedViews->viewFocus();
0073     const int indexOfFocussedView = viewList.indexOf(focussedView);
0074     AbstractView* nextView = viewList.at(indexOfFocussedView + 1);
0075     mGroupedViews->setViewFocus(nextView);
0076 }
0077 
0078 void SwitchViewController::backward()
0079 {
0080     const QVector<AbstractView*> viewList = mGroupedViews->viewList();
0081     AbstractView* focussedView = mGroupedViews->viewFocus();
0082     const int indexOfFocussedView = viewList.indexOf(focussedView);
0083     AbstractView* previousView = viewList.at(indexOfFocussedView - 1);
0084     mGroupedViews->setViewFocus(previousView);
0085 }
0086 
0087 }
0088 
0089 #include "moc_switchviewcontroller.cpp"