20 #include "modelactionrootstate.h"
21 #include "indicators.h"
23 #include <ayatanamenumodel.h>
32 ModelActionRootState::ModelActionRootState(QObject *parent)
33 : RootStateObject(parent),
35 , m_reentryGuard(false)
39 ModelActionRootState::~ModelActionRootState()
43 AyatanaMenuModel* ModelActionRootState::menu()
const
48 void ModelActionRootState::setMenu(AyatanaMenuModel* menu)
51 bool wasValid = valid();
54 m_menu->disconnect(
this);
59 connect(m_menu, &AyatanaMenuModel::rowsInserted,
this, &ModelActionRootState::onModelRowsAdded);
60 connect(m_menu, &AyatanaMenuModel::rowsRemoved,
this, &ModelActionRootState::onModelRowsRemoved);
61 connect(m_menu, &AyatanaMenuModel::dataChanged,
this, &ModelActionRootState::onModelDataChanged);
63 connect(m_menu, &AyatanaMenuModel::destroyed,
this, &ModelActionRootState::reset);
69 if (wasValid != valid())
70 Q_EMIT validChanged();
74 QString ModelActionRootState::secondaryAction()
const
76 return m_secondaryAction;
79 QString ModelActionRootState::scrollAction()
const
81 return m_scrollAction;
84 QString ModelActionRootState::submenuAction()
const
86 return m_submenuAction;
89 bool ModelActionRootState::valid()
const
91 return !currentState().empty();
94 void ModelActionRootState::onModelRowsAdded(
const QModelIndex& parent,
int start,
int end)
97 if (start == 0 && end >= 0) {
103 void ModelActionRootState::onModelRowsRemoved(
const QModelIndex& parent,
int start,
int end)
106 if (start == 0 && end >= 0) {
108 updateOtherActions();
112 void ModelActionRootState::onModelDataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight,
const QVector<int>& roles)
115 if (!topLeft.isValid() || !bottomRight.isValid()) {
119 if (topLeft.row() <= 0 && bottomRight.row() >= 0) {
121 updateOtherActions();
125 void ModelActionRootState::reset()
129 Q_EMIT menuChanged();
130 setCurrentState(QVariantMap());
132 updateOtherActions();
135 void ModelActionRootState::updateActionState()
137 if (m_reentryGuard)
return;
138 m_reentryGuard =
true;
140 if (m_menu && m_menu->rowCount() > 0) {
141 ActionStateParser* oldParser = m_menu->actionStateParser();
142 m_menu->setActionStateParser(&m_parser);
144 QVariantMap state = m_menu->get(0,
"actionState").toMap();
146 m_menu->setActionStateParser(oldParser);
148 setCurrentState(state);
149 }
else if (!m_menu) {
150 setCurrentState(QVariantMap());
155 m_reentryGuard =
false;
158 void ModelActionRootState::updateOtherActions()
160 if (m_reentryGuard)
return;
161 m_reentryGuard =
true;
163 if (m_menu && m_menu->rowCount() > 0) {
165 map[QStringLiteral(
"submenu-action")] = QStringLiteral(
"string");
166 map[QStringLiteral(
"x-canonical-scroll-action")] = QStringLiteral(
"string");
167 map[QStringLiteral(
"x-canonical-secondary-action")] = QStringLiteral(
"string");
168 m_menu->loadExtendedAttributes(0, map);
169 QVariantMap extMap = m_menu->get(0,
"ext").toMap();
171 QString secondaryAction = extMap.value(QStringLiteral(
"xCanonicalSecondaryAction")).toString();
172 if (m_secondaryAction != secondaryAction) {
173 m_secondaryAction = secondaryAction;
174 Q_EMIT secondaryActionChanged();
177 QString scrollAction = extMap.value(QStringLiteral(
"xCanonicalScrollAction")).toString();
178 if (m_scrollAction != scrollAction) {
179 m_scrollAction = scrollAction;
180 Q_EMIT scrollActionChanged();
183 QString submenuAction = extMap.value(QStringLiteral(
"submenuAction")).toString();
184 if (m_submenuAction != submenuAction) {
185 m_submenuAction = submenuAction;
186 Q_EMIT submenuActionChanged();
189 if (!m_secondaryAction.isEmpty()) {
190 m_secondaryAction.clear();
191 Q_EMIT secondaryActionChanged();
193 if (!m_scrollAction.isEmpty()) {
194 m_scrollAction.clear();
195 Q_EMIT scrollActionChanged();
197 if (!m_submenuAction.isEmpty()) {
198 m_submenuAction.clear();
199 Q_EMIT submenuActionChanged();
203 m_reentryGuard =
false;