LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / coreplugin / rightpane.cpp
bloba6d93dbaf7cb3ed8fb05c4353b8ad83dd4da2c9c
1 /**
2 ******************************************************************************
4 * @file rightpane.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup CorePlugin Core Plugin
10 * @{
11 * @brief The Core GCS plugin
12 *****************************************************************************/
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.
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.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "rightpane.h"
31 #include <coreplugin/modemanager.h>
32 #include <extensionsystem/pluginmanager.h>
34 #include <QtCore/QSettings>
36 #include <QVBoxLayout>
37 #include <QSplitter>
38 #include <QResizeEvent>
39 #include <QTextEdit>
42 using namespace Core;
43 using namespace Core::Internal;
45 RightPanePlaceHolder *RightPanePlaceHolder::m_current = 0;
47 RightPanePlaceHolder *RightPanePlaceHolder::current()
49 return m_current;
52 RightPanePlaceHolder::RightPanePlaceHolder(Core::IMode *mode, QWidget *parent)
53 : QWidget(parent), m_mode(mode)
55 setLayout(new QVBoxLayout);
56 layout()->setMargin(0);
57 connect(Core::ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode *)),
58 this, SLOT(currentModeChanged(Core::IMode *)));
61 RightPanePlaceHolder::~RightPanePlaceHolder()
63 if (m_current == this) {
64 RightPaneWidget::instance()->setParent(0);
65 RightPaneWidget::instance()->hide();
69 void RightPanePlaceHolder::applyStoredSize(int width)
71 if (width) {
72 QSplitter *splitter = qobject_cast<QSplitter *>(parentWidget());
73 if (splitter) {
74 // A splitter we need to resize the splitter sizes
75 QList<int> sizes = splitter->sizes();
76 int index = splitter->indexOf(this);
77 int diff = width - sizes.at(index);
78 int adjust = sizes.count() > 1 ? (diff / (sizes.count() - 1)) : 0;
79 for (int i = 0; i < sizes.count(); ++i) {
80 if (i != index) {
81 sizes[i] -= adjust;
84 sizes[index] = width;
85 splitter->setSizes(sizes);
86 } else {
87 QSize s = size();
88 s.setWidth(width);
89 resize(s);
94 // This function does work even though the order in which
95 // the placeHolder get the signal is undefined.
96 // It does ensure that after all PlaceHolders got the signal
97 // m_current points to the current PlaceHolder, or zero if there
98 // is no PlaceHolder in this mode
99 // And that the parent of the RightPaneWidget gets the correct parent
100 void RightPanePlaceHolder::currentModeChanged(Core::IMode *mode)
102 if (m_current == this) {
103 m_current = 0;
104 RightPaneWidget::instance()->setParent(0);
105 RightPaneWidget::instance()->hide();
107 if (m_mode == mode) {
108 m_current = this;
110 int width = RightPaneWidget::instance()->storedWidth();
112 layout()->addWidget(RightPaneWidget::instance());
113 RightPaneWidget::instance()->show();
115 applyStoredSize(width);
116 setVisible(RightPaneWidget::instance()->isShown());
120 /////
121 // RightPaneWidget
122 /////
125 RightPaneWidget *RightPaneWidget::m_instance = 0;
127 RightPaneWidget::RightPaneWidget()
128 : m_shown(true), m_width(0)
130 m_instance = this;
132 QVBoxLayout *layout = new QVBoxLayout;
133 layout->setMargin(0);
134 setLayout(layout);
136 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
138 BaseRightPaneWidget *rpw = pm->getObject<BaseRightPaneWidget>();
139 if (rpw) {
140 layout->addWidget(rpw->widget());
142 connect(pm, SIGNAL(objectAdded(QObject *)),
143 this, SLOT(objectAdded(QObject *)));
144 connect(pm, SIGNAL(aboutToRemoveObject(QObject *)),
145 this, SLOT(aboutToRemoveObject(QObject *)));
148 RightPaneWidget::~RightPaneWidget()
150 m_instance = 0;
153 void RightPaneWidget::objectAdded(QObject *obj)
155 BaseRightPaneWidget *rpw = qobject_cast<BaseRightPaneWidget *>(obj);
157 if (rpw) {
158 layout()->addWidget(rpw->widget());
159 setFocusProxy(rpw->widget());
163 void RightPaneWidget::aboutToRemoveObject(QObject *obj)
165 BaseRightPaneWidget *rpw = qobject_cast<BaseRightPaneWidget *>(obj);
167 if (rpw) {
168 delete rpw->widget();
172 RightPaneWidget *RightPaneWidget::instance()
174 return m_instance;
177 int RightPaneWidget::storedWidth()
179 return m_width;
182 void RightPaneWidget::resizeEvent(QResizeEvent *re)
184 if (m_width && re->size().width()) {
185 m_width = re->size().width();
187 QWidget::resizeEvent(re);
190 void RightPaneWidget::saveSettings(QSettings *settings)
192 settings->setValue("RightPane/Visible", isShown());
193 settings->setValue("RightPane/Width", m_width);
196 void RightPaneWidget::readSettings(QSettings *settings)
198 if (settings->contains("RightPane/Visible")) {
199 setShown(settings->value("RightPane/Visible").toBool());
200 } else {
201 setShown(false); // TODO set to false
204 if (settings->contains("RightPane/Width")) {
205 m_width = settings->value("RightPane/Width").toInt();
206 if (!m_width) {
207 m_width = 500;
209 } else {
210 m_width = 500; // pixel
212 // Apply
213 if (RightPanePlaceHolder::m_current) {
214 RightPanePlaceHolder::m_current->applyStoredSize(m_width);
218 void RightPaneWidget::setShown(bool b)
220 if (RightPanePlaceHolder::m_current) {
221 RightPanePlaceHolder::m_current->setVisible(b);
223 m_shown = b;
226 bool RightPaneWidget::isShown()
228 return m_shown;
231 /////
232 // BaseRightPaneWidget
233 /////
235 BaseRightPaneWidget::BaseRightPaneWidget(QWidget *widget)
237 m_widget = widget;
240 BaseRightPaneWidget::~BaseRightPaneWidget()
243 QWidget *BaseRightPaneWidget::widget() const
245 return m_widget;