Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / coreplugin / workspacesettings.cpp
blob459a011828456dc128164c222ae20236faf00306
1 /**
2 ******************************************************************************
4 * @file workspacesettings.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup CorePlugin Core Plugin
9 * @{
10 * @brief The Core GCS plugin
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "workspacesettings.h"
30 #include <coreplugin/icore.h>
31 #include <coreplugin/modemanager.h>
32 #include <coreplugin/uavgadgetmanager/uavgadgetmanager.h>
34 #include "ui_workspacesettings.h"
36 #include <QSettings>
38 using namespace Core;
39 using namespace Core::Internal;
41 const int WorkspaceSettings::MAX_WORKSPACES = 10;
43 WorkspaceSettings::WorkspaceSettings(QObject *parent) :
44 IOptionsPage(parent)
47 WorkspaceSettings::~WorkspaceSettings()
50 // IOptionsPage
52 QString WorkspaceSettings::id() const
54 return QLatin1String("Workspaces");
57 QString WorkspaceSettings::trName() const
59 return tr("Workspaces");
62 QString WorkspaceSettings::category() const
64 return QLatin1String("Environment");
67 QString WorkspaceSettings::trCategory() const
69 return tr("Environment");
72 QWidget *WorkspaceSettings::createPage(QWidget *parent)
74 m_page = new Ui::WorkspaceSettings();
75 QWidget *w = new QWidget(parent);
76 m_page->setupUi(w);
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));
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"));
88 connect(m_page->workspaceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectWorkspace(int)));
89 connect(m_page->numberOfWorkspacesSpinBox, SIGNAL(valueChanged(int)), this, SLOT(numberOfWorkspacesChanged(int)));
90 connect(m_page->nameEdit, SIGNAL(textEdited(QString)), this, SLOT(textEdited(QString)));
91 connect(m_page->iconPathChooser, SIGNAL(browsingFinished()), this, SLOT(iconChanged()));
93 m_currentIndex = 0;
94 selectWorkspace(m_currentIndex);
96 if (0 <= m_tabBarPlacementIndex && m_tabBarPlacementIndex < m_page->comboBoxTabBarPlacement->count()) {
97 m_page->comboBoxTabBarPlacement->setCurrentIndex(m_tabBarPlacementIndex);
99 m_page->checkBoxAllowTabMovement->setChecked(m_allowTabBarMovement);
100 m_page->checkBoxRestoreSelectedOnStartup->setChecked(m_restoreSelectedOnStartup);
102 return w;
105 void WorkspaceSettings::readSettings(QSettings &settings)
107 m_names.clear();
108 m_iconNames.clear();
109 m_modeNames.clear();
111 settings.beginGroup(QLatin1String("Workspace"));
112 m_numberOfWorkspaces = settings.value(QLatin1String("NumberOfWorkspaces"), 2).toInt();
113 m_previousNumberOfWorkspaces = m_numberOfWorkspaces;
114 for (int i = 1; i <= MAX_WORKSPACES; ++i) {
115 QString numberString = QString::number(i);
116 QString defaultName = "Workspace" + numberString;
117 QString defaultIconName = "Icon" + numberString;
118 QString name = settings.value(defaultName, defaultName).toString();
119 QString iconName = settings.value(defaultIconName, ":/core/images/librepilot_logo_64.png").toString();
120 m_names.append(name);
121 m_iconNames.append(iconName);
122 m_modeNames.append(QString("Mode") + QString::number(i));
124 m_tabBarPlacementIndex = settings.value(QLatin1String("TabBarPlacementIndex"), 1).toInt(); // 1 == "Bottom"
125 m_allowTabBarMovement = settings.value(QLatin1String("AllowTabBarMovement"), false).toBool();
126 m_restoreSelectedOnStartup = settings.value(QLatin1String("RestoreSelectedOnStartup"), false).toBool();
128 settings.endGroup();
130 QTabWidget::TabPosition pos = m_tabBarPlacementIndex == 0 ? QTabWidget::North : QTabWidget::South;
131 emit tabBarSettingsApplied(pos, m_allowTabBarMovement);
134 void WorkspaceSettings::saveSettings(QSettings &settings) const
136 settings.beginGroup(QLatin1String("Workspace"));
137 settings.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 settings.setValue(defaultName, m_names.at(j));
145 settings.setValue(defaultIconName, m_iconNames.at(j));
147 settings.setValue(QLatin1String("TabBarPlacementIndex"), m_tabBarPlacementIndex);
148 settings.setValue(QLatin1String("AllowTabBarMovement"), m_allowTabBarMovement);
149 settings.setValue(QLatin1String("RestoreSelectedOnStartup"), m_restoreSelectedOnStartup);
150 settings.endGroup();
153 void WorkspaceSettings::apply()
155 selectWorkspace(m_currentIndex, true);
157 QSettings settings;
158 saveSettings(settings);
160 if (m_numberOfWorkspaces != m_previousNumberOfWorkspaces) {
161 Core::ICore::instance()->readMainSettings(settings, true);
162 m_previousNumberOfWorkspaces = m_numberOfWorkspaces;
165 ModeManager *modeManager = Core::ICore::instance()->modeManager();
166 for (int i = 0; i < MAX_WORKSPACES; ++i) {
167 IMode *baseMode = modeManager->mode(modeName(i));
168 Core::UAVGadgetManager *mode = qobject_cast<Core::UAVGadgetManager *>(baseMode);
169 if (mode) {
170 modeManager->updateModeNameIcon(mode, QIcon(iconName(i)), name(i));
173 m_tabBarPlacementIndex = m_page->comboBoxTabBarPlacement->currentIndex();
174 m_allowTabBarMovement = m_page->checkBoxAllowTabMovement->isChecked();
175 m_restoreSelectedOnStartup = m_page->checkBoxRestoreSelectedOnStartup->isChecked();
177 QTabWidget::TabPosition pos = m_tabBarPlacementIndex == 0 ? QTabWidget::North : QTabWidget::South;
178 emit tabBarSettingsApplied(pos, m_allowTabBarMovement);
181 void WorkspaceSettings::finish()
183 delete m_page;
186 void WorkspaceSettings::textEdited(QString name)
188 Q_UNUSED(name);
189 m_page->workspaceComboBox->setItemText(m_currentIndex, m_page->nameEdit->text());
192 void WorkspaceSettings::iconChanged()
194 QString iconName = m_page->iconPathChooser->path();
196 m_page->workspaceComboBox->setItemIcon(m_currentIndex, QIcon(iconName));
199 void WorkspaceSettings::numberOfWorkspacesChanged(int value)
201 m_numberOfWorkspaces = value;
202 int count = m_page->workspaceComboBox->count();
203 if (value > count) {
204 for (int i = count; i < value; ++i) {
205 m_page->workspaceComboBox->addItem(QIcon(m_iconNames.at(i)), m_names.at(i));
207 } else if (value < count) {
208 for (int i = count - 1; i >= value; --i) {
209 m_page->workspaceComboBox->removeItem(i);
214 void WorkspaceSettings::selectWorkspace(int index, bool store)
216 if (store || (index != m_currentIndex)) {
217 // write old values of workspace not shown anymore
218 m_iconNames.replace(m_currentIndex, m_page->iconPathChooser->path());
219 m_names.replace(m_currentIndex, m_page->nameEdit->text());
220 m_page->workspaceComboBox->setItemIcon(m_currentIndex, QIcon(m_iconNames.at(m_currentIndex)));
221 m_page->workspaceComboBox->setItemText(m_currentIndex, m_names.at(m_currentIndex));
224 // display current workspace
225 QString iconName = m_iconNames.at(index);
226 m_page->iconPathChooser->setPath(iconName);
227 m_page->nameEdit->setText(m_names.at(index));
228 m_currentIndex = index;
231 void WorkspaceSettings::newModeOrder(QVector<IMode *> modes)
233 QList<int> priorities;
234 QStringList modeNames;
235 for (int i = 0; i < modes.count(); ++i) {
236 Core::UAVGadgetManager *mode = qobject_cast<Core::UAVGadgetManager *>(modes.at(i));
237 if (mode) {
238 priorities.append(mode->priority());
239 modeNames.append(mode->uniqueModeName());
242 // Bubble sort
243 bool swapped = false;
244 do {
245 swapped = false;
246 for (int i = 0; i < m_names.count() - 1; ++i) {
247 int j = i + 1;
248 int p = modeNames.indexOf(m_modeNames.at(i));
249 int q = modeNames.indexOf(m_modeNames.at(j));
250 bool nonShowingMode = (p == -1 && q >= 0);
251 bool pqBothFound = (p >= 0 && q >= 0);
252 if (nonShowingMode || (pqBothFound && (priorities.at(q) > priorities.at(p)))) {
253 m_names.swap(i, j);
254 m_iconNames.swap(i, j);
255 m_modeNames.swap(i, j);
256 swapped = true;
259 } while (swapped);