All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MenuBarModel.cpp
Go to the documentation of this file.
1 #include "MenuBarModel.h"
2 
3 //Qt
4 #include <QMenuBar>
5 #include <QToolBar>
6 
7 std::vector<QAction*> GetActions(QList<QAction*> acts)
8 {
9  std::vector<QAction*> res;
10  QList<QAction*>::iterator it;
11 
12  for(it=acts.begin(); it != acts.end(); ++it)
13  if((*it)->menu() == 0 && !((*it)->text().isEmpty()) && !(*it)->icon().isNull())
14  res.push_back(*it);
15 
16  return res;
17 }
18 
19 te::qt::af::MenuBarModel::MenuBarModel(QMenuBar* mnuBar, QObject* parent) :
20 QAbstractTableModel(parent)
21 {
22  setMenuBar(mnuBar);
23 }
24 
26 {
27 }
28 
29 Qt::ItemFlags te::qt::af::MenuBarModel::flags (const QModelIndex & index) const
30 {
31  return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
32 }
33 
34 QVariant te::qt::af::MenuBarModel::data (const QModelIndex & index, int role) const
35 {
36  if(!index.isValid())
37  return QVariant();
38 
39  switch(role)
40  {
41  case Qt::TextAlignmentRole:
42  return int(Qt::AlignVCenter | Qt::AlignLeft);
43  break;
44 
45  case Qt::DisplayRole:
46  {
47  QString name = m_actions[index.row()]->objectName();
48  int pos = name.lastIndexOf(".");
49 
50  return (pos < 0) ? m_actions[index.row()]->text() : name.mid(pos+1);
51  }
52  break;
53 
54  case Qt::DecorationRole:
55  return m_actions[index.row()]->icon();
56  break;
57 
58  case Qt::CheckStateRole:
59  return (m_actionsActivation[index.row()]) ? Qt::Checked : Qt::Unchecked;
60  break;
61 
62  default:
63  return QVariant();
64  break;
65  }
66 }
67 
68 QVariant te::qt::af::MenuBarModel::headerData (int section, Qt::Orientation orientation, int role) const
69 {
70  return QVariant();
71 }
72 
73 int te::qt::af::MenuBarModel::columnCount (const QModelIndex & parent) const
74 {
75  return 1;
76 }
77 
78 int te::qt::af::MenuBarModel::rowCount (const QModelIndex & parent) const
79 {
80  return (int)m_actions.size();
81 }
82 
83 bool te::qt::af::MenuBarModel::setData (const QModelIndex & index, const QVariant & value, int role)
84 {
85  if (!index.isValid())
86  return false;
87 
88  int r = index.row();
89 
90  if(role == Qt::CheckStateRole)
91  m_actionsActivation[r] = (value.toInt() == Qt::Checked) ? true : false;
92 
93  QAction* act = m_actions[r];
94  bool toAdd = m_actionsActivation[r];
95 
96  emit updateAction(act, toAdd);
97 
98  return true;
99 }
100 
101 void te::qt::af::MenuBarModel::updateActionsState(const QList<QAction*>& acts)
102 {
103  QList<QAction*>::const_iterator it;
104 
105  m_actionsActivation.clear();
106  m_actionsActivation = std::vector<bool>(m_actions.size(), false);
107 
108  for(it = acts.begin(); it != acts.end(); ++it)
109  {
110  for(size_t i=0; i<m_actions.size(); i++)
111  if((*it) == m_actions[i])
112  m_actionsActivation[i] = true;
113  }
114 
115 #if (QT_VERSION < 0x050000)
116  reset();
117 #else
118  beginResetModel();
119  endResetModel();
120 #endif
121 }
122 
124 {
125  QList<QAction*> acts = mnuBar->findChildren<QAction*>();
126  m_actions = GetActions(acts);
127 
128  m_actionsActivation = std::vector<bool>(m_actions.size(), false);
129 
130 #if (QT_VERSION < 0x050000)
131  reset();
132 #else
133  beginResetModel();
134  endResetModel();
135 #endif
136 }
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
MenuBarModel(QMenuBar *mnuBar, QObject *parent=0)
Constructor.
int rowCount(const QModelIndex &parent=QModelIndex()) const
~MenuBarModel()
Destructor.
void setMenuBar(QMenuBar *mnuBar)
Sets the menu bar to be used in the model.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
virtual Qt::ItemFlags flags(const QModelIndex &index) const
void updateActionsState(const QList< QAction * > &acts)
Check the actions contained in acts.
int columnCount(const QModelIndex &parent=QModelIndex()) const