File indexing completed on 2024-05-19 04:29:21

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