LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / coreplugin / workspacesettings.cpp
blob61ea4080838c60ec0cb20e83ce550bed9d9b270e
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"
29 #include <coreplugin/icore.h>
30 #include <coreplugin/modemanager.h>
31 #include <coreplugin/uavgadgetmanager/uavgadgetmanager.h>
32 #include <QtCore/QSettings>
34 #include "ui_workspacesettings.h"
36 using namespace Core;
37 using namespace Core::Internal;
39 const int WorkspaceSettings::MAX_WORKSPACES = 10;
41 WorkspaceSettings::WorkspaceSettings(QObject *parent) :
42 IOptionsPage(parent)
45 WorkspaceSettings::~WorkspaceSettings()
48 // IOptionsPage
50 QString WorkspaceSettings::id() const
52 return QLatin1String("Workspaces");
55 QString WorkspaceSettings::trName() const
57 return tr("Workspaces");
60 QString WorkspaceSettings::category() const
62 return QLatin1String("Environment");
65 QString WorkspaceSettings::trCategory() const
67 return tr("Environment");
70 QWidget *WorkspaceSettings::createPage(QWidget *parent)
72 m_page = new Ui::WorkspaceSettings();
73 QWidget *w = new QWidget(parent);
74 m_page->setupUi(w);
76 m_page->numberOfWorkspacesSpinBox->setMaximum(MAX_WORKSPACES);
77 m_page->numberOfWorkspacesSpinBox->setValue(m_numberOfWorkspaces);
78 for (int i = 0; i < m_numberOfWorkspaces; ++i) {
79 m_page->workspaceComboBox->addItem(QIcon(m_iconNames.at(i)), m_names.at(i));
82 m_page->iconPathChooser->setExpectedKind(Utils::PathChooser::File);
83 m_page->iconPathChooser->setPromptDialogFilter(tr("Images (*.png *.jpg *.bmp *.xpm)"));
84 m_page->iconPathChooser->setPromptDialogTitle(tr("Choose icon"));
86 connect(m_page->workspaceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectWorkspace(int)));
87 connect(m_page->numberOfWorkspacesSpinBox, SIGNAL(valueChanged(int)), this, SLOT(numberOfWorkspacesChanged(int)));
88 connect(m_page->nameEdit, SIGNAL(textEdited(QString)), this, SLOT(textEdited(QString)));
89 connect(m_page->iconPathChooser, SIGNAL(browsingFinished()), this, SLOT(iconChanged()));
91 m_currentIndex = 0;
92 selectWorkspace(m_currentIndex);
94 if (0 <= m_tabBarPlacementIndex && m_tabBarPlacementIndex < m_page->comboBoxTabBarPlacement->count()) {
95 m_page->comboBoxTabBarPlacement->setCurrentIndex(m_tabBarPlacementIndex);
97 m_page->checkBoxAllowTabMovement->setChecked(m_allowTabBarMovement);
98 m_page->checkBoxRestoreSelectedOnStartup->setChecked(m_restoreSelectedOnStartup);
100 return w;
103 void WorkspaceSettings::readSettings(QSettings *qs)
105 m_names.clear();
106 m_iconNames.clear();
107 m_modeNames.clear();
109 qs->beginGroup(QLatin1String("Workspace"));
110 m_numberOfWorkspaces = qs->value(QLatin1String("NumberOfWorkspaces"), 2).toInt();
111 m_previousNumberOfWorkspaces = m_numberOfWorkspaces;
112 for (int i = 1; i <= MAX_WORKSPACES; ++i) {
113 QString numberString = QString::number(i);
114 QString defaultName = "Workspace" + numberString;
115 QString defaultIconName = "Icon" + numberString;
116 QString name = qs->value(defaultName, defaultName).toString();
117 QString iconName = qs->value(defaultIconName, ":/core/images/librepilot_logo_64.png").toString();
118 m_names.append(name);
119 m_iconNames.append(iconName);
120 m_modeNames.append(QString("Mode") + QString::number(i));
122 m_tabBarPlacementIndex = qs->value(QLatin1String("TabBarPlacementIndex"), 1).toInt(); // 1 == "Bottom"
123 m_allowTabBarMovement = qs->value(QLatin1String("AllowTabBarMovement"), false).toBool();
124 m_restoreSelectedOnStartup = qs->value(QLatin1String("RestoreSelectedOnStartup"), false).toBool();
126 qs->endGroup();
128 QTabWidget::TabPosition pos = m_tabBarPlacementIndex == 0 ? QTabWidget::North : QTabWidget::South;
129 emit tabBarSettingsApplied(pos, m_allowTabBarMovement);
132 void WorkspaceSettings::saveSettings(QSettings *qs)
134 qs->beginGroup(QLatin1String("Workspace"));
135 qs->setValue(QLatin1String("NumberOfWorkspaces"), m_numberOfWorkspaces);
136 for (int i = 0; i < MAX_WORKSPACES; ++i) {
137 QString mode = QString("Mode") + QString::number(i + 1);
138 int j = m_modeNames.indexOf(mode);
139 QString numberString = QString::number(i + 1);
140 QString defaultName = "Workspace" + numberString;
141 QString defaultIconName = "Icon" + numberString;
142 qs->setValue(defaultName, m_names.at(j));
143 qs->setValue(defaultIconName, m_iconNames.at(j));
145 qs->setValue(QLatin1String("TabBarPlacementIndex"), m_tabBarPlacementIndex);
146 qs->setValue(QLatin1String("AllowTabBarMovement"), m_allowTabBarMovement);
147 qs->setValue(QLatin1String("RestoreSelectedOnStartup"), m_restoreSelectedOnStartup);
148 qs->endGroup();
151 void WorkspaceSettings::apply()
153 selectWorkspace(m_currentIndex, true);
155 saveSettings(Core::ICore::instance()->settings());
157 if (m_numberOfWorkspaces != m_previousNumberOfWorkspaces) {
158 Core::ICore::instance()->readMainSettings(Core::ICore::instance()->settings(), true);
159 m_previousNumberOfWorkspaces = m_numberOfWorkspaces;
162 ModeManager *modeManager = Core::ICore::instance()->modeManager();
163 for (int i = 0; i < MAX_WORKSPACES; ++i) {
164 IMode *baseMode = modeManager->mode(modeName(i));
165 Core::UAVGadgetManager *mode = qobject_cast<Core::UAVGadgetManager *>(baseMode);
166 if (mode) {
167 modeManager->updateModeNameIcon(mode, QIcon(iconName(i)), name(i));
170 m_tabBarPlacementIndex = m_page->comboBoxTabBarPlacement->currentIndex();
171 m_allowTabBarMovement = m_page->checkBoxAllowTabMovement->isChecked();
172 m_restoreSelectedOnStartup = m_page->checkBoxRestoreSelectedOnStartup->isChecked();
174 QTabWidget::TabPosition pos = m_tabBarPlacementIndex == 0 ? QTabWidget::North : QTabWidget::South;
175 emit tabBarSettingsApplied(pos, m_allowTabBarMovement);
178 void WorkspaceSettings::finish()
180 delete m_page;
183 void WorkspaceSettings::textEdited(QString name)
185 Q_UNUSED(name);
186 m_page->workspaceComboBox->setItemText(m_currentIndex, m_page->nameEdit->text());
189 void WorkspaceSettings::iconChanged()
191 QString iconName = m_page->iconPathChooser->path();
193 m_page->workspaceComboBox->setItemIcon(m_currentIndex, QIcon(iconName));
196 void WorkspaceSettings::numberOfWorkspacesChanged(int value)
198 m_numberOfWorkspaces = value;
199 int count = m_page->workspaceComboBox->count();
200 if (value > count) {
201 for (int i = count; i < value; ++i) {
202 m_page->workspaceComboBox->addItem(QIcon(m_iconNames.at(i)), m_names.at(i));
204 } else if (value < count) {
205 for (int i = count - 1; i >= value; --i) {
206 m_page->workspaceComboBox->removeItem(i);
211 void WorkspaceSettings::selectWorkspace(int index, bool store)
213 if (store || (index != m_currentIndex)) {
214 // write old values of workspace not shown anymore
215 m_iconNames.replace(m_currentIndex, m_page->iconPathChooser->path());
216 m_names.replace(m_currentIndex, m_page->nameEdit->text());
217 m_page->workspaceComboBox->setItemIcon(m_currentIndex, QIcon(m_iconNames.at(m_currentIndex)));
218 m_page->workspaceComboBox->setItemText(m_currentIndex, m_names.at(m_currentIndex));
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;
228 void WorkspaceSettings::newModeOrder(QVector<IMode *> modes)
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());
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;
256 } while (swapped);