File indexing completed on 2024-05-12 16:01:56

0001 /* This file is part of the Calligra project, made within the KDE community.
0002  * SPDX-FileCopyrightText: 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_VIEW_P_H
0008 #define KIS_VIEW_P_H
0009 
0010 // Calligra
0011 #include "KoUnit.h"
0012 #include "KisDocument.h"
0013 // Qt
0014 #include <QActionGroup>
0015 #include <QAction>
0016 
0017 
0018 // Action group which keeps the actions in sync with the document's unit property
0019 class UnitActionGroup : public QActionGroup
0020 {
0021     Q_OBJECT
0022 public:
0023     explicit UnitActionGroup(KisDocument *document, bool addPixelUnit, QObject* parent = 0)
0024             : QActionGroup(parent)
0025             , m_document(document)
0026             , m_listOptions(addPixelUnit ? KoUnit::ListAll : KoUnit::HidePixel)
0027     {
0028         setExclusive(true);
0029         connect(this, SIGNAL(triggered(QAction*)), SLOT(onTriggered(QAction*)));
0030         connect(document, SIGNAL(unitChanged(KoUnit)), SLOT(onUnitChanged(KoUnit)));
0031 
0032         const QStringList unitNames = KoUnit::listOfUnitNameForUi(m_listOptions);
0033         const int currentUnitIndex = m_document->unit().indexInListForUi(m_listOptions);
0034 
0035         for (int i = 0; i < unitNames.count(); ++i) {
0036             QAction* action = new QAction(unitNames.at(i), this);
0037             action->setData(i);
0038             action->setCheckable(true);
0039 
0040             if (currentUnitIndex == i) {
0041                 action->setChecked(true);
0042             }
0043         }
0044     }
0045 
0046 private Q_SLOTS:
0047     void onTriggered(QAction *action)
0048     {
0049         m_document->setUnit(KoUnit::fromListForUi(action->data().toInt(), m_listOptions));
0050     }
0051 
0052     void onUnitChanged(const KoUnit &unit)
0053     {
0054         const int indexInList = unit.indexInListForUi(m_listOptions);
0055 
0056         Q_FOREACH (QAction *action, actions()) {
0057             if (action->data().toInt() == indexInList) {
0058                 action->setChecked(true);
0059                 break;
0060             }
0061             // in case the new unit is not in the list of actions
0062             // this ensures that the action currently checked is unchecked
0063             // once the end of actions has been reached
0064             if (action->isChecked()) {
0065                 action->setChecked(false);
0066             }
0067         }
0068     }
0069 
0070 private:
0071     KisDocument *m_document;
0072     KoUnit::ListOptions m_listOptions;
0073 };
0074 
0075 #endif