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
9 * @addtogroup CorePlugin Core Plugin
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
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"
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>
45 #include <QHBoxLayout>
47 #include <QMouseEvent>
50 #include <QStyleOption>
51 #include <QToolButton>
56 #include <qmacstyle_mac.h>
59 Q_DECLARE_METATYPE(Core::IUAVGadget
*)
62 using namespace Core::Internal
;
64 UAVGadgetView::UAVGadgetView(Core::UAVGadgetManager
*uavGadgetManager
, IUAVGadget
*uavGadget
, 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)),
73 m_activeLabel(new QLabel
)
75 tl
= new QVBoxLayout(this);
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();
86 bool startFromOne
= false;
87 foreach(QString classId
, sl
) {
88 if (classId
== QString("EmptyGadget")) {
91 m_uavGadgetList
->insertItem(0, im
->gadgetName(classId
), classId
);
92 m_uavGadgetList
->setItemIcon(0, im
->gadgetIcon(classId
));
93 m_uavGadgetList
->insertSeparator(1);
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) {
101 m_uavGadgetList
->insertItem(i
, im
->gadgetName(classId
), classId
);
102 m_uavGadgetList
->setItemIcon(i
, im
->gadgetIcon(classId
));
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
*)));
145 setGadget(m_uavGadget
);
147 listSelectionActivated(m_defaultIndex
);
151 UAVGadgetView::~UAVGadgetView()
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()
176 tl
->removeWidget(m_uavGadget
->widget());
178 m_uavGadget
->setParent(0);
179 m_uavGadget
->widget()->setParent(0);
180 QWidget
*toolBar
= m_uavGadget
->toolBar();
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);
192 IUAVGadget
*UAVGadgetView::gadget() const
197 void UAVGadgetView::setGadget(IUAVGadget
*uavGadget
)
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
);
214 void UAVGadgetView::updateToolBar()
219 QComboBox
*toolBar
= m_uavGadget
->toolBar();
221 toolBar
= m_defaultToolBar
;
223 if (m_activeToolBar
== toolBar
) {
226 if (toolBar
->count() == 0) {
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
)) {
245 UAVGadgetInstanceManager
*im
= ICore::instance()->uavGadgetInstanceManager();
246 IUAVGadget
*gadget
= im
->createGadget(classId
, this);
248 IUAVGadget
*gadgetToRemove
= m_uavGadget
;
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
);
273 void UAVGadgetView::restoreState(QSettings
&settings
)
275 QString classId
= settings
.value("classId").toString();
276 int index
= indexOfClassId(classId
);
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
);
291 newGadget
= im
->createGadget(classId
, this);
294 IUAVGadget
*gadgetToRemove
= m_uavGadget
;
295 setGadget(newGadget
);
296 m_uavGadgetManager
->setCurrentGadget(newGadget
);
297 im
->removeGadget(gadgetToRemove
);