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, te::qt::af::ApplicationController* app)
16 {
17  std::vector<QToolBar*> bars = app->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, te::qt::af::ApplicationController* app)
29 {
30  QAbstractItemModel* old_m = view->model();
31 
32  te::qt::af::MenuBarModel* model = new te::qt::af::MenuBarModel(app->getMenuBar("menubar"), view);
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, te::qt::af::ApplicationController* app)
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  app->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  //Setting icons
69  m_ui->m_addToolButton->setIcon(QIcon::fromTheme("list-add"));
70  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
71  m_ui->m_actionsListViewWidget->setSelectionMode(QAbstractItemView::SingleSelection);
72 
73  // Signal/Slots connection
74  connect (m_ui->m_toolbarsComboBox, SIGNAL(currentIndexChanged(int)), SLOT(currentToolbarChanged(int)));
75  connect (m_ui->m_addToolButton, SIGNAL(clicked()), SLOT(onAddToolbarButtonClicked()));
76  connect (m_ui->m_removeToolButton, SIGNAL(clicked()), SLOT(onRemoveToolbarButtonClicked()));
77 
78  m_resumeText = tr("Add, remove or modify system tool bars.");
79 }
80 
82 {
83  delete m_ui;
84 }
85 
87 {
88  // Updating actions
89  for (int i=0; i<m_ui->m_toolbarsComboBox->count(); i++)
90  {
91  QToolBar* bar = (QToolBar*) m_ui->m_toolbarsComboBox->itemData(i, Qt::UserRole).value<QObject*>();
92  bar->clear();
93  bar->addActions(m_actions[i]);
94  }
95 
97 
98  // Updating new toolbars
99  std::set<QToolBar*>::iterator it;
100 
101  for(it = m_createdBars.begin(); it != m_createdBars.end(); ++it)
102  {
103  QToolBar* bar = *it;
104 
106 
107  m_app->addToolBar(bar->objectName(), bar);
108  }
109 
110  // Removed toolbars
112 
113  m_createdBars.clear();
114  m_removedToolBars.clear();
115 
116  changeApplyButtonState(false);
117 }
118 
120 {
122 
123  m_createdBars.clear();
124  m_removedToolBars.clear();
125  m_actions.clear();
126 
127  m_ui->m_toolbarsComboBox->clear();
128 
129  SetToolbars(m_ui->m_toolbarsComboBox, m_actions, m_app);
130  SetActions(m_ui->m_actionsListViewWidget, m_app);
131 
133 }
134 
135 void te::qt::af::ToolbarsWidget::getHelpInformations(QString& ns, QString& helpFile)
136 {
137  ns = "dpi.inpe.br.apf";
138  helpFile = "apf/settings/toolbar/ToolbarConfig.html";
139 }
140 
142 {
143  m_app = app;
144 
145  resetState();
146 
147  connect(m_ui->m_actionsListViewWidget->model(), SIGNAL(updateAction(QAction*, const bool&)), SLOT(updateActions(QAction*, const bool&)));
148 }
149 
151 {
152  if(idx < 0 || m_actions.empty())
153  return;
154 
155  QList<QAction*> acts = m_actions[idx];
156  ((te::qt::af::MenuBarModel*)m_ui->m_actionsListViewWidget->model())->updateActionsState(acts);
157 }
158 
160 {
161  bool ok;
162  QString text = QInputDialog::getText(this, tr("Creating tool bar"), tr("Tool bar name:"), QLineEdit::Normal, tr("Name of the new toolbar"), &ok);
163 
164  if(!ok)
165  return;
166 
167  if (text.isEmpty())
168  {
169  QMessageBox::warning(this, tr("Creating tool bar"), tr("Empty tool bar name not allowed!"));
170  return;
171  }
172 
173  QToolBar* bar = new QToolBar;
174  bar->setObjectName(text);
175 
176  m_createdBars.insert(bar);
177 
178  int count = m_ui->m_toolbarsComboBox->count();
179 
180  m_ui->m_toolbarsComboBox->addItem(bar->objectName(), QVariant::fromValue<QObject*>(bar));
181 
182  QList<QAction*> acts;
183  m_actions.push_back(acts);
184 
186 
187  m_ui->m_toolbarsComboBox->setCurrentIndex(count);
188 }
189 
191 {
192  QString msg = tr("Did you really want to remove tool bar?");
193 
194  if(QMessageBox::question(this, tr("Tool bars customization"), msg, QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
195  return;
196 
197  int idx = m_ui->m_toolbarsComboBox->currentIndex();
198 
199  QToolBar* bar = (QToolBar*)m_ui->m_toolbarsComboBox->itemData(idx, Qt::UserRole).value<QObject*>();
200 
201  m_removedToolBars.insert(bar);
202 
203  m_ui->m_toolbarsComboBox->removeItem(idx);
204 
206 }
207 
209 {
210  saveChanges();
211 }
212 
213 void te::qt::af::ToolbarsWidget::updateActions(QAction* act, const bool& toAdd)
214 {
215  int idx = m_ui->m_toolbarsComboBox->currentIndex();
216  UpdateActions(m_actions[idx], act, toAdd);
217 
219 }
TEQTAFEXPORT void RemoveToolBarFromSettings(QToolBar *bar)
Removes a tool bar from the settings.
void SetToolbars(QComboBox *cmb, std::vector< QList< QAction * > > &acts, te::qt::af::ApplicationController *app)
te::qt::af::ApplicationController * m_app
The base API for TerraLib applications.
void RemoveBars(const std::set< QToolBar * > &bars, te::qt::af::ApplicationController *app)
QMenuBar * getMenuBar(const QString &id) const
Returns a menu bar registered with key id.
void updateActions(QAction *act, const bool &toAdd)
TEQTAFEXPORT void UpdateToolBarsInTheSettings(te::qt::af::ApplicationController *appController)
Update plugins file.
TEQTAFEXPORT void AddToolBarToSettings(QToolBar *bar)
Update settings with a new tool bar.
ToolbarsWidget(QWidget *parent=0)
virtual void setApplicationController(te::qt::af::ApplicationController *app)
A frame for setting tool bars options.
void removeToolBar(const QString &id)
Removes the toolbar identified by id.
A frame for setting Table options.
void UpdateActions(QList< QAction * > &acts, QAction *act, const bool &toAdd)
Ui::ToolbarsWidgetForm * m_ui
void SetActions(QListView *view, te::qt::af::ApplicationController *app)
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
std::vector< QToolBar * > getToolBars() const
Return the list of registered toolbars.
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 changeApplyButtonState(const bool &state)
Indicates that there&#39;s unsaved information. Use this method after each change in informations of the ...
virtual void getHelpInformations(QString &ns, QString &helpFile)
std::vector< QList< QAction * > > m_actions
std::set< QToolBar * > m_removedToolBars
Indexes of the removed bars.
std::set< QToolBar * > m_createdBars