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() == nullptr && !((*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 
28  const QModelIndex& /*index*/) const
29 {
30  return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
31 }
32 
33 QVariant te::qt::af::MenuBarModel::data (const QModelIndex & index, int role) const
34 {
35  if(!index.isValid())
36  return QVariant();
37 
38  switch(role)
39  {
40  case Qt::TextAlignmentRole:
41  return int(Qt::AlignVCenter | Qt::AlignLeft);
42  break;
43 
44  case Qt::DisplayRole:
45  {
46  QString name = m_actions[index.row()]->objectName();
47  int pos = name.lastIndexOf(".");
48 
49  return (pos < 0) ? m_actions[index.row()]->text() : name.mid(pos+1);
50  }
51  break;
52 
53  case Qt::DecorationRole:
54  return m_actions[index.row()]->icon();
55  break;
56 
57  case Qt::CheckStateRole:
58  return (m_actionsActivation[index.row()]) ? Qt::Checked : Qt::Unchecked;
59  break;
60 
61  default:
62  return QVariant();
63  break;
64  }
65 }
66 
67 QVariant te::qt::af::MenuBarModel::headerData(int /*section*/,
68  Qt::Orientation /*orientation*/,
69  int /*role*/) const
70 {
71  return QVariant();
72 }
73 
74 int te::qt::af::MenuBarModel::columnCount(const QModelIndex& /*parent*/) const
75 {
76  return 1;
77 }
78 
79 int te::qt::af::MenuBarModel::rowCount(const QModelIndex& /*parent*/) const
80 {
81  return (int)m_actions.size();
82 }
83 
84 bool te::qt::af::MenuBarModel::setData (const QModelIndex & index, const QVariant & value, int role)
85 {
86  if (!index.isValid())
87  return false;
88 
89  int r = index.row();
90 
91  if(role == Qt::CheckStateRole)
92  m_actionsActivation[r] = (value.toInt() == Qt::Checked) ? true : false;
93 
94  QAction* act = m_actions[r];
95  bool toAdd = m_actionsActivation[r];
96 
97  emit updateAction(act, toAdd);
98 
99  return true;
100 }
101 
102 void te::qt::af::MenuBarModel::updateActionsState(const QList<QAction*>& acts)
103 {
104  QList<QAction*>::const_iterator it;
105 
106  m_actionsActivation.clear();
107  m_actionsActivation = std::vector<bool>(m_actions.size(), false);
108 
109  for(it = acts.begin(); it != acts.end(); ++it)
110  {
111  for(size_t i=0; i<m_actions.size(); i++)
112  if((*it) == m_actions[i])
113  m_actionsActivation[i] = true;
114  }
115 
116 #if (QT_VERSION < 0x050000)
117  reset();
118 #else
119  beginResetModel();
120  endResetModel();
121 #endif
122 }
123 
125 {
126  QList<QAction*> acts = mnuBar->findChildren<QAction*>();
127  m_actions = GetActions(acts);
128 
129  m_actionsActivation = std::vector<bool>(m_actions.size(), false);
130 
131 #if (QT_VERSION < 0x050000)
132  reset();
133 #else
134  beginResetModel();
135  endResetModel();
136 #endif
137 }
std::vector< QAction * > m_actions
System buttons.
Definition: MenuBarModel.h:100
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
void updateAction(QAction *, const bool &)
~MenuBarModel()
Destructor.
void setMenuBar(QMenuBar *mnuBar)
Sets the menu bar to be used in the model.
std::vector< bool > m_actionsActivation
State of each system button. (True insert, False do nothing).
Definition: MenuBarModel.h:101
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
rasterPointer reset(te::rst::RasterFactory::make("MEM", new te::rst::Grid(nCols, nLines), bandsProps, std::map< std::string, std::string >(), 0, 0))
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