on 551c708 help sign in
unity8/plugins/HudClient/hudtoolbarmodel.cpp
98 lines 2.7 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "hudtoolbarmodel.h"
19#include "hudclient.h"
21static const int ActionRole = Qt::UserRole;
22static const int EnabledRole = Qt::UserRole + 1;
24static QString iconForAction(int action)
26 switch (action) {
27 case HUD_CLIENT_QUERY_TOOLBAR_UNDO:
28 return "graphics/undo.png";
29 case HUD_CLIENT_QUERY_TOOLBAR_HELP:
30 return "graphics/help.png";
31 case HUD_CLIENT_QUERY_TOOLBAR_FULLSCREEN:
32 return "graphics/view-fullscreen.png";
33 case HUD_CLIENT_QUERY_TOOLBAR_PREFERENCES:
34 return "graphics/settings.png";
35 }
36 return QString();
39HudToolBarModel::HudToolBarModel(HudClientQuery *query)
40 : m_query(query)
42 m_actions << HUD_CLIENT_QUERY_TOOLBAR_UNDO
43 << HUD_CLIENT_QUERY_TOOLBAR_HELP
44 << HUD_CLIENT_QUERY_TOOLBAR_FULLSCREEN
45 << HUD_CLIENT_QUERY_TOOLBAR_PREFERENCES;
48HudToolBarModel::~HudToolBarModel()
52int HudToolBarModel::rowCount(const QModelIndex &parent) const
54 if (parent.isValid())
55 return 0;
57 return m_actions.count();
60QVariant HudToolBarModel::data(const QModelIndex &index, int role) const
62 const int row = index.row();
63 if (row >= m_actions.count())
64 return QVariant();
66 const HudClientQueryToolbarItems action = m_actions[row];
68 switch (role) {
69 case Qt::DecorationRole:
70 return iconForAction(action);
71 break;
73 case ActionRole:
74 return action;
75 break;
77 case EnabledRole:
78 return hud_client_query_toolbar_item_active(m_query, action);
79 break;
80 }
81 return QVariant();
84QHash<int,QByteArray> HudToolBarModel::roleNames() const
86 static QHash<int,QByteArray> roles;
87 if (roles.isEmpty()) {
88 roles[Qt::DecorationRole] = "iconPath";
89 roles[ActionRole] = "action";
90 roles[EnabledRole] = "enabled";
91 }
92 return roles;
95void HudToolBarModel::updatedByBackend()
97 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << EnabledRole);