dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
workspacesettings.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "workspacesettings.h"
29 #include <coreplugin/icore.h>
30 #include <coreplugin/modemanager.h>
32 #include <QtCore/QSettings>
33 
34 #include "ui_workspacesettings.h"
35 
36 using namespace Core;
37 using namespace Core::Internal;
38 
39 const int WorkspaceSettings::MAX_WORKSPACES = 10;
40 
42  : IOptionsPage(parent)
43 {
44 }
45 
47 {
48 }
49 
50 // IOptionsPage
51 
52 QString WorkspaceSettings::id() const
53 {
54  return QLatin1String("Workspaces");
55 }
56 
58 {
59  return tr("Workspaces");
60 }
61 
63 {
64  return QLatin1String("GCS");
65 }
66 
68 {
69  return tr("GCS");
70 }
71 
72 QWidget *WorkspaceSettings::createPage(QWidget *parent)
73 {
74  m_page = new Ui::WorkspaceSettings();
75  QWidget *w = new QWidget(parent);
76  m_page->setupUi(w);
77 
78  m_page->numberOfWorkspacesSpinBox->setMaximum(MAX_WORKSPACES);
79  m_page->numberOfWorkspacesSpinBox->setValue(m_numberOfWorkspaces);
80  for (int i = 0; i < m_numberOfWorkspaces; ++i) {
81  m_page->workspaceComboBox->addItem(QIcon(m_iconNames.at(i)), m_names.at(i));
82  }
83 
84  m_page->iconPathChooser->setExpectedKind(Utils::PathChooser::File);
85  m_page->iconPathChooser->setPromptDialogFilter(tr("Images (*.png *.jpg *.bmp *.xpm)"));
86  m_page->iconPathChooser->setPromptDialogTitle(tr("Choose icon"));
87 
88  connect(m_page->workspaceComboBox, SIGNAL(currentIndexChanged(int)), this,
89  SLOT(selectWorkspace(int)));
90  connect(m_page->numberOfWorkspacesSpinBox, SIGNAL(valueChanged(int)), this,
91  SLOT(numberOfWorkspacesChanged(int)));
92  connect(m_page->nameEdit, SIGNAL(textEdited(QString)), this, SLOT(textEdited(QString)));
93  connect(m_page->iconPathChooser, SIGNAL(browsingFinished()), this, SLOT(iconChanged()));
94 
95  m_currentIndex = 0;
96  selectWorkspace(m_currentIndex);
97 
98  if (0 <= m_tabBarPlacementIndex
99  && m_tabBarPlacementIndex < m_page->comboBoxTabBarPlacement->count())
100  m_page->comboBoxTabBarPlacement->setCurrentIndex(m_tabBarPlacementIndex);
101  m_page->checkBoxAllowTabMovement->setChecked(m_allowTabBarMovement);
102 
103  return w;
104 }
105 
107 {
108  m_names.clear();
109  m_iconNames.clear();
110  m_modeNames.clear();
111 
112  qs->beginGroup(QLatin1String("Workspace"));
113  m_numberOfWorkspaces = qs->value(QLatin1String("NumberOfWorkspaces"), 2).toInt();
114  m_previousNumberOfWorkspaces = m_numberOfWorkspaces;
115  for (int i = 1; i <= MAX_WORKSPACES; ++i) {
116  QString numberString = QString::number(i);
117  QString defaultName = "Workspace" + numberString;
118  QString defaultIconName = "Icon" + numberString;
119  QString name = qs->value(defaultName, defaultName).toString();
120  QString iconName = qs->value(defaultIconName, ":/core/gcs_logo_64").toString();
121  m_names.append(name);
122  m_iconNames.append(iconName);
123  m_modeNames.append(QString("Mode") + QString::number(i));
124  }
125  m_tabBarPlacementIndex =
126  qs->value(QLatin1String("TabBarPlacementIndex"), 1).toInt(); // 1 == "Bottom"
127  m_allowTabBarMovement = qs->value(QLatin1String("AllowTabBarMovement"), false).toBool();
128  qs->endGroup();
129  QTabWidget::TabPosition pos =
130  m_tabBarPlacementIndex == 0 ? QTabWidget::North : QTabWidget::South;
131  emit tabBarSettingsApplied(pos, m_allowTabBarMovement);
132 }
133 
135 {
136  qs->beginGroup(QLatin1String("Workspace"));
137  qs->setValue(QLatin1String("NumberOfWorkspaces"), m_numberOfWorkspaces);
138  for (int i = 0; i < MAX_WORKSPACES; ++i) {
139  QString mode = QString("Mode") + QString::number(i + 1);
140  int j = m_modeNames.indexOf(mode);
141  QString numberString = QString::number(i + 1);
142  QString defaultName = "Workspace" + numberString;
143  QString defaultIconName = "Icon" + numberString;
144  qs->setValue(defaultName, m_names.at(j));
145  qs->setValue(defaultIconName, m_iconNames.at(j));
146  }
147  qs->setValue(QLatin1String("TabBarPlacementIndex"), m_tabBarPlacementIndex);
148  qs->setValue(QLatin1String("AllowTabBarMovement"), m_allowTabBarMovement);
149  qs->endGroup();
150 }
151 
153 {
154  selectWorkspace(m_currentIndex, true);
155 
156  saveSettings(Core::ICore::instance()->settings());
157 
158  if (m_numberOfWorkspaces != m_previousNumberOfWorkspaces) {
160  m_previousNumberOfWorkspaces = m_numberOfWorkspaces;
161  }
162 
163  ModeManager *modeManager = Core::ICore::instance()->modeManager();
164  for (int i = 0; i < MAX_WORKSPACES; ++i) {
165  IMode *baseMode = modeManager->mode(modeName(i));
166  Core::UAVGadgetManager *mode = qobject_cast<Core::UAVGadgetManager *>(baseMode);
167  if (mode) {
168  modeManager->updateModeNameIcon(mode, QIcon(iconName(i)), name(i));
169  }
170  }
171  m_tabBarPlacementIndex = m_page->comboBoxTabBarPlacement->currentIndex();
172  m_allowTabBarMovement = m_page->checkBoxAllowTabMovement->isChecked();
173  QTabWidget::TabPosition pos =
174  m_tabBarPlacementIndex == 0 ? QTabWidget::North : QTabWidget::South;
175  emit tabBarSettingsApplied(pos, m_allowTabBarMovement);
176 }
177 
179 {
180  delete m_page;
181 }
182 
184 {
185  Q_UNUSED(name);
186  m_page->workspaceComboBox->setItemText(m_currentIndex, m_page->nameEdit->text());
187 }
188 
190 {
191  QString iconName = m_page->iconPathChooser->path();
192  m_page->workspaceComboBox->setItemIcon(m_currentIndex, QIcon(iconName));
193 }
194 
196 {
197  m_numberOfWorkspaces = value;
198  int count = m_page->workspaceComboBox->count();
199  if (value > count) {
200  for (int i = count; i < value; ++i) {
201  m_page->workspaceComboBox->addItem(QIcon(m_iconNames.at(i)), m_names.at(i));
202  }
203  } else if (value < count) {
204  for (int i = count - 1; i >= value; --i) {
205  m_page->workspaceComboBox->removeItem(i);
206  }
207  }
208 }
209 
210 void WorkspaceSettings::selectWorkspace(int index, bool store)
211 {
212  if (store || (index != m_currentIndex)) {
213  // write old values of workspace not shown anymore
214  m_iconNames.replace(m_currentIndex, m_page->iconPathChooser->path());
215  m_names.replace(m_currentIndex, m_page->nameEdit->text());
216  m_page->workspaceComboBox->setItemIcon(m_currentIndex,
217  QIcon(m_iconNames.at(m_currentIndex)));
218  m_page->workspaceComboBox->setItemText(m_currentIndex, m_names.at(m_currentIndex));
219  }
220 
221  // display current workspace
222  QString iconName = m_iconNames.at(index);
223  m_page->iconPathChooser->setPath(iconName);
224  m_page->nameEdit->setText(m_names.at(index));
225  m_currentIndex = index;
226 }
227 
228 void WorkspaceSettings::newModeOrder(QVector<IMode *> modes)
229 {
230  QList<int> priorities;
231  QStringList modeNames;
232  for (int i = 0; i < modes.count(); ++i) {
233  Core::UAVGadgetManager *mode = qobject_cast<Core::UAVGadgetManager *>(modes.at(i));
234  if (mode) {
235  priorities.append(mode->priority());
236  modeNames.append(mode->uniqueModeName());
237  }
238  }
239  // Bubble sort
240  bool swapped = false;
241  do {
242  swapped = false;
243  for (int i = 0; i < m_names.count() - 1; ++i) {
244  int j = i + 1;
245  int p = modeNames.indexOf(m_modeNames.at(i));
246  int q = modeNames.indexOf(m_modeNames.at(j));
247  bool nonShowingMode = (p == -1 && q >= 0);
248  bool pqBothFound = (p >= 0 && q >= 0);
249  if (nonShowingMode || (pqBothFound && (priorities.at(q) > priorities.at(p)))) {
250  m_names.swap(i, j);
251  m_iconNames.swap(i, j);
252  m_modeNames.swap(i, j);
253  swapped = true;
254  }
255  }
256  } while (swapped);
257 }
void selectWorkspace(int index, bool store=false)
void tabBarSettingsApplied(QTabWidget::TabPosition pos, bool movable)
virtual void readMainSettings(QSettings *qs, bool workspaceDiffOnly=false)=0
QWidget * createPage(QWidget *parent)
for i
Definition: OPPlots.m:140
IMode * mode(const QString &id) const
void newModeOrder(QVector< IMode * > modes)
static ICore * instance()
Definition: coreimpl.cpp:46
void updateModeNameIcon(IMode *mode, const QIcon &icon, const QString &label)
const char * uniqueModeName() const
WorkspaceSettings(QObject *parent=nullptr)
virtual ModeManager * modeManager() const =0
Returns the application's mode manager.
The IOptionsPage is an interface for providing options pages.
Definition: ioptionspage.h:42