Krita Source Code Documentation
Loading...
Searching...
No Matches
KisView_p.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6#ifndef KIS_VIEW_P_H
7#define KIS_VIEW_P_H
8
9#include "KoUnit.h"
10#include "KisDocument.h"
11
12#include <QActionGroup>
13#include <QAction>
14
15
16// Action group which keeps the actions in sync with the document's unit property
17class UnitActionGroup : public QActionGroup
18{
19 Q_OBJECT
20public:
21 explicit UnitActionGroup(KisDocument *document, bool addPixelUnit, QObject* parent = 0)
22 : QActionGroup(parent)
23 , m_document(document)
24 , m_listOptions(addPixelUnit ? KoUnit::ListAll : KoUnit::HidePixel)
25 {
26 setExclusive(true);
27 connect(this, SIGNAL(triggered(QAction*)), SLOT(onTriggered(QAction*)));
28 connect(document, SIGNAL(unitChanged(KoUnit)), SLOT(onUnitChanged(KoUnit)));
29
31 const int currentUnitIndex = m_document->unit().indexInListForUi(m_listOptions);
32
33 for (int i = 0; i < unitNames.count(); ++i) {
34 QAction* action = new QAction(unitNames.at(i), this);
35 action->setData(i);
36 action->setCheckable(true);
37
38 if (currentUnitIndex == i) {
39 action->setChecked(true);
40 }
41 }
42 }
43
44private Q_SLOTS:
45 void onTriggered(QAction *action)
46 {
47 m_document->setUnit(KoUnit::fromListForUi(action->data().toInt(), m_listOptions));
48 }
49
50 void onUnitChanged(const KoUnit &unit)
51 {
52 const int indexInList = unit.indexInListForUi(m_listOptions);
53
54 Q_FOREACH (QAction *action, actions()) {
55 if (action->data().toInt() == indexInList) {
56 action->setChecked(true);
57 break;
58 }
59 // in case the new unit is not in the list of actions
60 // this ensures that the action currently checked is unchecked
61 // once the end of actions has been reached
62 if (action->isChecked()) {
63 action->setChecked(false);
64 }
65 }
66 }
67
68private:
70 KoUnit::ListOptions m_listOptions;
71};
72
73#endif
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void setUnit(const KoUnit &unit)
static KoUnit fromListForUi(int index, ListOptions listOptions=ListAll, qreal factor=1.0)
Definition KoUnit.cpp:80
static QStringList listOfUnitNameForUi(ListOptions listOptions=ListAll)
Returns the list of unit types for the UI, controlled with the given listOptions.
Definition KoUnit.cpp:69
int indexInListForUi(ListOptions listOptions=ListAll) const
Definition KoUnit.cpp:101
KisDocument * m_document
Definition KisView_p.h:69
UnitActionGroup(KisDocument *document, bool addPixelUnit, QObject *parent=0)
Definition KisView_p.h:21
KoUnit::ListOptions m_listOptions
Definition KisView_p.h:70
void onTriggered(QAction *action)
Definition KisView_p.h:45
void onUnitChanged(const KoUnit &unit)
Definition KisView_p.h:50