All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ToolbarsWidget.cpp
Go to the documentation of this file.
1 #include <ui_ToolbarsWidgetForm.h>
2 
3 #include "ToolbarsWidget.h"
4 #include "MenuBarModel.h"
5 
6 #include "../../../common/STLUtils.h"
7 #include "../ApplicationController.h"
8 #include "../Utils.h"
9 
10 // Qt
11 #include <QInputDialog>
12 #include <QMessageBox>
13 #include <QToolBar>
14 
15 void SetToolbars(QComboBox* cmb, std::vector< QList<QAction*> >& acts)
16 {
17  std::vector<QToolBar*> bars = te::qt::af::ApplicationController::getInstance().getToolBars();
18  std::vector<QToolBar*>::iterator it;
19 
20  for(it=bars.begin(); it != bars.end(); ++it)
21  {
22  QToolBar* tb = *it;
23  cmb->addItem(tb->objectName(), QVariant::fromValue<QObject*>(tb));
24  acts.push_back(tb->actions());
25  }
26 }
27 
28 void SetActions(QListView* view)
29 {
30  QAbstractItemModel* old_m = view->model();
31 
33  view->setModel(model);
34 
35  delete old_m;
36 }
37 
38 void UpdateActions(QList<QAction*>& acts, QAction* act, const bool& toAdd)
39 {
40  if(toAdd)
41  acts.append(act);
42  else
43  acts.removeAll(act);
44 }
45 
46 void RemoveBars(const std::set<QToolBar*>& bars)
47 {
48  std::set<QToolBar*>::const_iterator it;
49 
50  for(it = bars.begin(); it != bars.end(); ++it)
51  {
52  QToolBar* bar = *it;
53 
55 
56  te::qt::af::ApplicationController::getInstance().removeToolBar(bar->objectName());
57 
58  delete bar;
59  }
60 }
61 
63 AbstractSettingWidget(parent),
64 m_ui(new Ui::ToolbarsWidgetForm)
65 {
66  m_ui->setupUi(this);
67 
68  resetState();
69 
70  //Setting icons
71  m_ui->m_addToolButton->setIcon(QIcon::fromTheme("list-add"));
72  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
73  m_ui->m_actionsListViewWidget->setSelectionMode(QAbstractItemView::SingleSelection);
74 
75  // Signal/Slots connection
76  connect (m_ui->m_toolbarsComboBox, SIGNAL(currentIndexChanged(int)), SLOT(currentToolbarChanged(int)));
77  connect (m_ui->m_addToolButton, SIGNAL(clicked()), SLOT(onAddToolbarButtonClicked()));
78  connect (m_ui->m_removeToolButton, SIGNAL(clicked()), SLOT(onRemoveToolbarButtonClicked()));
79  connect (m_ui->m_actionsListViewWidget->model(), SIGNAL(updateAction(QAction*, const bool&)), SLOT(updateActions(QAction*, const bool&)));
80 
81  m_resumeText = tr("Add, remove or modify system tool bars.");
82 
84 }
85 
87 {
88  delete m_ui;
89 }
90 
92 {
93  // Updating actions
94  for (int i=0; i<m_ui->m_toolbarsComboBox->count(); i++)
95  {
96  QToolBar* bar = (QToolBar*) m_ui->m_toolbarsComboBox->itemData(i, Qt::UserRole).value<QObject*>();
97  bar->clear();
98  bar->addActions(m_actions[i]);
99  }
100 
102 
103  // Updating new toolbars
104  std::set<QToolBar*>::iterator it;
105 
106  for(it = m_createdBars.begin(); it != m_createdBars.end(); ++it)
107  {
108  QToolBar* bar = *it;
109 
111 
113  }
114 
115  // Removed toolbars
116  RemoveBars(m_removedToolBars);
117 
118  m_createdBars.clear();
119  m_removedToolBars.clear();
120 
121  changeApplyButtonState(false);
122 }
123 
125 {
126  te::common::FreeContents(m_createdBars);
127 
128  m_createdBars.clear();
129  m_removedToolBars.clear();
130  m_actions.clear();
131 
132  m_ui->m_toolbarsComboBox->clear();
133 
134  SetToolbars(m_ui->m_toolbarsComboBox, m_actions);
135  SetActions(m_ui->m_actionsListViewWidget);
136 
137  currentToolbarChanged(0);
138 }
139 
140 void te::qt::af::ToolbarsWidget::getHelpInformations(QString& ns, QString& helpFile)
141 {
142  ns = "dpi.inpe.br.apf";
143  helpFile = "apf/settings/toolbar/ToolbarConfig.html";
144 }
145 
147 {
148  if(idx < 0 || m_actions.empty())
149  return;
150 
151  QList<QAction*> acts = m_actions[idx];
152  ((te::qt::af::MenuBarModel*)m_ui->m_actionsListViewWidget->model())->updateActionsState(acts);
153 }
154 
156 {
157  bool ok;
158  QString text = QInputDialog::getText(this, tr("Creating tool bar"), tr("Tool bar name:"), QLineEdit::Normal, tr("Name of the new toolbar"), &ok);
159 
160  if(!ok)
161  return;
162 
163  if (text.isEmpty())
164  {
165  QMessageBox::warning(this, tr("Creating tool bar"), tr("Empty tool bar name not allowed!"));
166  return;
167  }
168 
169  QToolBar* bar = new QToolBar;
170  bar->setObjectName(text);
171 
172  m_createdBars.insert(bar);
173 
174  int count = m_ui->m_toolbarsComboBox->count();
175 
176  m_ui->m_toolbarsComboBox->addItem(bar->objectName(), QVariant::fromValue<QObject*>(bar));
177 
178  QList<QAction*> acts;
179  m_actions.push_back(acts);
180 
181  changeApplyButtonState(true);
182 
183  m_ui->m_toolbarsComboBox->setCurrentIndex(count);
184 }
185 
187 {
188  QString msg = tr("Did you really want to remove tool bar?");
189 
190  if(QMessageBox::question(this, tr("Tool bars customization"), msg, QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
191  return;
192 
193  int idx = m_ui->m_toolbarsComboBox->currentIndex();
194 
195  QToolBar* bar = (QToolBar*)m_ui->m_toolbarsComboBox->itemData(idx, Qt::UserRole).value<QObject*>();
196 
197  m_removedToolBars.insert(bar);
198 
199  m_ui->m_toolbarsComboBox->removeItem(idx);
200 
201  changeApplyButtonState(true);
202 }
203 
205 {
206  saveChanges();
207 }
208 
209 void te::qt::af::ToolbarsWidget::updateActions(QAction* act, const bool& toAdd)
210 {
211  int idx = m_ui->m_toolbarsComboBox->currentIndex();
212  UpdateActions(m_actions[idx], act, toAdd);
213 
214  changeApplyButtonState(true);
215 }
TEQTAFEXPORT void RemoveToolBarFromSettings(QToolBar *bar)
Removes a tool bar from the settings.
Definition: Utils.cpp:484
TEQTAFEXPORT void UpdateToolBarsInTheSettings()
Update plugins file.
Definition: Utils.cpp:453
void updateActions(QAction *act, const bool &toAdd)
TEQTAFEXPORT void AddToolBarToSettings(QToolBar *bar)
Update settings with a new tool bar.
Definition: Utils.cpp:473
ToolbarsWidget(QWidget *parent=0)
void SetToolbars(QComboBox *cmb, std::vector< QList< QAction * > > &acts)
A frame for setting tool bars options.
static ApplicationController & getInstance()
It returns a reference to the singleton instance.
A frame for setting Table options.
void SetActions(QListView *view)
void UpdateActions(QList< QAction * > &acts, QAction *act, const bool &toAdd)
Ui::ToolbarsWidgetForm * m_ui
void addToolBar(const QString &id, QToolBar *bar)
Register the toolbar in the list of the known toolbars and dispatch an event.
Defines a model, based on a QMenuBar, to present all system buttons as a table.
Definition: MenuBarModel.h:50
void RemoveBars(const std::set< QToolBar * > &bars)
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
virtual void getHelpInformations(QString &ns, QString &helpFile)