Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / coreplugin / uavgadgetmanager / uavgadgetview.cpp
blobffad1e2a90743949200f5ac1e3f706ae26337213
1 /**
2 ******************************************************************************
4 * @file uavgadgetview.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 "uavgadgetview.h"
30 #include "uavgadgetmanager.h"
31 #include "uavgadgetinstancemanager.h"
32 #include "iuavgadget.h"
33 #include "coreimpl.h"
34 #include "minisplitter.h"
35 #include <coreplugin/coreconstants.h>
36 #include <coreplugin/actionmanager/actionmanager.h>
38 #include <utils/qtcassert.h>
39 #include <utils/styledbar.h>
41 #include <QtCore/QDebug>
43 #include <QtWidgets/QApplication>
44 #include <QComboBox>
45 #include <QHBoxLayout>
46 #include <QLabel>
47 #include <QMouseEvent>
48 #include <QPainter>
49 #include <QStyle>
50 #include <QStyleOption>
51 #include <QToolButton>
52 #include <QMenu>
53 #include <QClipboard>
55 #ifdef Q_WS_MAC
56 #include <qmacstyle_mac.h>
57 #endif
59 Q_DECLARE_METATYPE(Core::IUAVGadget *)
61 using namespace Core;
62 using namespace Core::Internal;
64 UAVGadgetView::UAVGadgetView(Core::UAVGadgetManager *uavGadgetManager, IUAVGadget *uavGadget, QWidget *parent) :
65 QWidget(parent),
66 m_uavGadgetManager(uavGadgetManager),
67 m_uavGadget(uavGadget),
68 m_toolBar(new QWidget(this)),
69 m_defaultToolBar(new QComboBox(this)),
70 m_uavGadgetList(new QComboBox(this)),
71 m_closeButton(new QToolButton(this)),
72 m_defaultIndex(0),
73 m_activeLabel(new QLabel)
75 tl = new QVBoxLayout(this);
76 tl->setSpacing(0);
77 tl->setMargin(0);
79 m_uavGadgetList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
80 m_uavGadgetList->setMinimumContentsLength(15);
81 m_uavGadgetList->setMaxVisibleItems(40);
82 m_uavGadgetList->setContextMenuPolicy(Qt::CustomContextMenu);
83 UAVGadgetInstanceManager *im = ICore::instance()->uavGadgetInstanceManager();
84 QStringList sl = im->classIds();
85 int index = 0;
86 bool startFromOne = false;
87 foreach(QString classId, sl) {
88 if (classId == QString("EmptyGadget")) {
89 m_defaultIndex = 0;
90 startFromOne = true;
91 m_uavGadgetList->insertItem(0, im->gadgetName(classId), classId);
92 m_uavGadgetList->setItemIcon(0, im->gadgetIcon(classId));
93 m_uavGadgetList->insertSeparator(1);
94 } else {
95 int i = startFromOne ? 1 : 0;
96 for (; i < m_uavGadgetList->count(); i++) {
97 if (QString::localeAwareCompare(m_uavGadgetList->itemText(i), im->gadgetName(classId)) > 0) {
98 break;
101 m_uavGadgetList->insertItem(i, im->gadgetName(classId), classId);
102 m_uavGadgetList->setItemIcon(i, im->gadgetIcon(classId));
104 ++index;
107 m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
108 m_activeToolBar = m_defaultToolBar;
110 QHBoxLayout *toolBarLayout = new QHBoxLayout(m_toolBar);
111 toolBarLayout->setMargin(0);
112 toolBarLayout->setSpacing(0);
113 toolBarLayout->addWidget(m_defaultToolBar);
114 m_toolBar->setLayout(toolBarLayout);
115 m_toolBar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
117 QWidget *spacerWidget = new QWidget(this);
118 spacerWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
120 m_activeLabel->setTextFormat(Qt::RichText);
121 m_activeLabel->setText("<font color=red><b>" + tr("Active") + "</b></font>");
123 m_closeButton->setAutoRaise(true);
124 m_closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
126 m_top = new Utils::StyledBar(this);
127 m_top->setMaximumHeight(35);
128 QHBoxLayout *toplayout = new QHBoxLayout(m_top);
129 toplayout->setSpacing(4);
130 toplayout->setMargin(4);
131 toplayout->addWidget(m_uavGadgetList);
132 toplayout->addWidget(m_toolBar); // Custom toolbar stretches
133 toplayout->addWidget(spacerWidget);
134 toplayout->addWidget(m_activeLabel);
135 toplayout->addWidget(m_closeButton);
137 m_top->setLayout(toplayout);
138 tl->addWidget(m_top);
140 connect(m_uavGadgetList, SIGNAL(activated(int)), this, SLOT(listSelectionActivated(int)));
141 connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()), Qt::QueuedConnection);
142 connect(m_uavGadgetManager, SIGNAL(currentGadgetChanged(IUAVGadget *)), this, SLOT(currentGadgetChanged(IUAVGadget *)));
144 if (m_uavGadget) {
145 setGadget(m_uavGadget);
146 } else {
147 listSelectionActivated(m_defaultIndex);
151 UAVGadgetView::~UAVGadgetView()
153 removeGadget();
156 bool UAVGadgetView::hasGadget(IUAVGadget *uavGadget) const
158 return m_uavGadget == uavGadget;
161 void UAVGadgetView::showToolbar(bool show)
163 m_top->setHidden(!show);
166 void UAVGadgetView::closeView()
168 m_uavGadgetManager->closeView(this);
171 void UAVGadgetView::removeGadget()
173 if (!m_uavGadget) {
174 return;
176 tl->removeWidget(m_uavGadget->widget());
178 m_uavGadget->setParent(0);
179 m_uavGadget->widget()->setParent(0);
180 QWidget *toolBar = m_uavGadget->toolBar();
181 if (toolBar != 0) {
182 if (m_activeToolBar == toolBar) {
183 m_activeToolBar = m_defaultToolBar;
184 m_activeToolBar->setVisible(true);
186 m_toolBar->layout()->removeWidget(toolBar);
187 toolBar->setParent(0);
189 m_uavGadget = 0;
192 IUAVGadget *UAVGadgetView::gadget() const
194 return m_uavGadget;
197 void UAVGadgetView::setGadget(IUAVGadget *uavGadget)
199 if (!uavGadget) {
200 return;
202 removeGadget();
203 m_uavGadget = uavGadget;
204 tl->addWidget(m_uavGadget->widget());
205 m_uavGadget->widget()->setParent(this);
206 m_uavGadget->widget()->show();
207 int index = indexOfClassId(m_uavGadget->classId());
208 Q_ASSERT(index >= 0);
209 m_uavGadgetList->setCurrentIndex(index);
211 updateToolBar();
214 void UAVGadgetView::updateToolBar()
216 if (!m_uavGadget) {
217 return;
219 QComboBox *toolBar = m_uavGadget->toolBar();
220 if (!toolBar) {
221 toolBar = m_defaultToolBar;
223 if (m_activeToolBar == toolBar) {
224 return;
226 if (toolBar->count() == 0) {
227 toolBar->hide();
229 m_toolBar->layout()->addWidget(toolBar);
230 m_activeToolBar->setVisible(false);
231 m_activeToolBar = toolBar;
234 void UAVGadgetView::listSelectionActivated(int index)
236 if (index < 0 || index >= m_uavGadgetList->count()) {
237 index = m_defaultIndex;
240 QString classId = m_uavGadgetList->itemData(index).toString();
241 if (m_uavGadget && (m_uavGadget->classId() == classId)) {
242 return;
245 UAVGadgetInstanceManager *im = ICore::instance()->uavGadgetInstanceManager();
246 IUAVGadget *gadget = im->createGadget(classId, this);
248 IUAVGadget *gadgetToRemove = m_uavGadget;
249 setGadget(gadget);
250 m_uavGadgetManager->setCurrentGadget(gadget);
251 im->removeGadget(gadgetToRemove);
254 int UAVGadgetView::indexOfClassId(QString classId)
256 return m_uavGadgetList->findData(classId);
259 void UAVGadgetView::currentGadgetChanged(IUAVGadget *gadget)
261 m_activeLabel->setVisible(m_uavGadget == gadget);
264 void UAVGadgetView::saveState(QSettings &settings) const
266 settings.setValue("type", "uavGadget");
267 settings.setValue("classId", gadget()->classId());
268 settings.beginGroup("gadget");
269 gadget()->saveState(settings);
270 settings.endGroup();
273 void UAVGadgetView::restoreState(QSettings &settings)
275 QString classId = settings.value("classId").toString();
276 int index = indexOfClassId(classId);
278 if (index < 0) {
279 index = m_defaultIndex;
281 classId = m_uavGadgetList->itemData(index).toString();
283 IUAVGadget *newGadget;
284 UAVGadgetInstanceManager *im = ICore::instance()->uavGadgetInstanceManager();
285 if (settings.childGroups().contains("gadget")) {
286 newGadget = im->createGadget(classId, this, false);
287 settings.beginGroup("gadget");
288 newGadget->restoreState(settings);
289 settings.endGroup();
290 } else {
291 newGadget = im->createGadget(classId, this);
294 IUAVGadget *gadgetToRemove = m_uavGadget;
295 setGadget(newGadget);
296 m_uavGadgetManager->setCurrentGadget(newGadget);
297 im->removeGadget(gadgetToRemove);