Unity 8
 All Classes Functions Properties
hudtoolbarmodel.cpp
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  */
16 
17 #include "hudtoolbarmodel.h"
18 
19 #include "hudclient.h"
20 
21 static const int ActionRole = Qt::UserRole;
22 static const int EnabledRole = Qt::UserRole + 1;
23 
24 static QString iconForAction(int action)
25 {
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();
37 }
38 
39 HudToolBarModel::HudToolBarModel(HudClientQuery *query)
40  : m_query(query)
41 {
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;
46 }
47 
48 HudToolBarModel::~HudToolBarModel()
49 {
50 }
51 
52 int HudToolBarModel::rowCount(const QModelIndex &parent) const
53 {
54  if (parent.isValid())
55  return 0;
56 
57  return m_actions.count();
58 }
59 
60 QVariant HudToolBarModel::data(const QModelIndex &index, int role) const
61 {
62  const int row = index.row();
63  if (row >= m_actions.count())
64  return QVariant();
65 
66  const HudClientQueryToolbarItems action = m_actions[row];
67 
68  switch (role) {
69  case Qt::DecorationRole:
70  return iconForAction(action);
71  break;
72 
73  case ActionRole:
74  return action;
75  break;
76 
77  case EnabledRole:
78  return hud_client_query_toolbar_item_active(m_query, action);
79  break;
80  }
81  return QVariant();
82 }
83 
84 QHash<int,QByteArray> HudToolBarModel::roleNames() const
85 {
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;
93 }
94 
95 void HudToolBarModel::updatedByBackend()
96 {
97  Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0), QVector<int>() << EnabledRole);
98 }