dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
uavgadgetview.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 #include "uavgadgetview.h"
31 #include "uavgadgetmanager.h"
33 #include "iuavgadget.h"
34 #include "coreimpl.h"
35 #include "minisplitter.h"
38 
39 #include <utils/qtcassert.h>
40 #include <utils/styledbar.h>
41 
42 #include <QtCore/QDebug>
43 
44 #include <QApplication>
45 #include <QComboBox>
46 #include <QHBoxLayout>
47 #include <QLabel>
48 #include <QMouseEvent>
49 #include <QPainter>
50 #include <QStyle>
51 #include <QStyleOption>
52 #include <QToolButton>
53 #include <QMenu>
54 #include <QClipboard>
55 #include <QListView>
56 
58 
59 using namespace Core;
60 using namespace Core::Internal;
61 
62 UAVGadgetView::UAVGadgetView(Core::UAVGadgetManager *uavGadgetManager, IUAVGadget *uavGadget,
63  QWidget *parent, bool restoring)
64  : QWidget(parent)
65  , m_uavGadgetManager(uavGadgetManager)
66  , m_uavGadget(uavGadget)
67  , m_toolBar(new QWidget(this))
68  , m_defaultToolBar(new QComboBox(this))
69  , m_uavGadgetList(new QComboBox(this))
70  , m_closeButton(new QToolButton(this))
71  , m_defaultIndex(0)
72  , m_activeLabel(new QLabel)
73 {
74 
75  tl = new QVBoxLayout(this);
76  tl->setSpacing(0);
77  tl->setMargin(0);
78  {
79  // Ugly hack to bypass Qt's styling antics regarding combobox flyouts.
80  m_uavGadgetList->setView(new QListView(m_uavGadgetList));
81 
82  m_uavGadgetList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
83  m_uavGadgetList->setMinimumContentsLength(15);
84  m_uavGadgetList->setMaxVisibleItems(40);
85  m_uavGadgetList->setContextMenuPolicy(Qt::CustomContextMenu);
87  QStringList sl = im->classIds();
88  int index = 0;
89  bool startFromOne = false;
90  foreach (QString classId, sl) {
91  if (classId == QString("EmptyGadget")) {
92  m_defaultIndex = 0;
93  startFromOne = true;
94  m_uavGadgetList->insertItem(0, im->gadgetName(classId), classId);
95  m_uavGadgetList->setItemIcon(0, im->gadgetIcon(classId));
96  m_uavGadgetList->insertSeparator(1);
97  } else {
98 
99  int i = startFromOne ? 1 : 0;
100  for (; i < m_uavGadgetList->count(); i++) {
101  if (QString::localeAwareCompare(m_uavGadgetList->itemText(i),
102  im->gadgetName(classId))
103  > 0)
104  break;
105  }
106  m_uavGadgetList->insertItem(i, im->gadgetName(classId), classId);
107  m_uavGadgetList->setItemIcon(i, im->gadgetIcon(classId));
108  }
109  ++index;
110  }
111 
112  m_defaultToolBar->setSizePolicy(QSizePolicy::MinimumExpanding,
113  QSizePolicy::MinimumExpanding);
114  m_activeToolBar = m_defaultToolBar;
115 
116  QHBoxLayout *toolBarLayout = new QHBoxLayout(m_toolBar);
117  toolBarLayout->setMargin(0);
118  toolBarLayout->setSpacing(0);
119  toolBarLayout->addWidget(m_defaultToolBar);
120 
121  m_toolBar->setLayout(toolBarLayout);
122  m_toolBar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
123 
124  QWidget *spacerWidget = new QWidget(this);
125  spacerWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
126 
127  m_activeLabel->setTextFormat(Qt::RichText);
128  m_activeLabel->setText("<font color=#c0392b><b>" + tr("Active") + "</b></font>");
129 
130  m_closeButton->setAutoRaise(true);
131  m_closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
132 
133  m_top = new Utils::StyledBar(this);
134  m_top->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
135 
136  QHBoxLayout *toplayout = new QHBoxLayout(m_top);
137  toplayout->setSpacing(0);
138  toplayout->setMargin(0);
139  toplayout->addWidget(m_uavGadgetList);
140  toplayout->addWidget(m_toolBar); // Custom toolbar stretches
141  toplayout->addWidget(spacerWidget);
142  toplayout->addWidget(m_activeLabel);
143  toplayout->addWidget(m_closeButton);
144 
145  m_top->setLayout(toplayout);
146  tl->addWidget(m_top);
147 
148  connect(m_uavGadgetList, SIGNAL(activated(int)), this, SLOT(doReplaceGadget(int)));
149  connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()), Qt::QueuedConnection);
150  connect(m_uavGadgetManager, SIGNAL(currentGadgetChanged(IUAVGadget *)), this,
151  SLOT(currentGadgetChanged(IUAVGadget *)));
152  }
153  if (m_uavGadget) {
154  setGadget(m_uavGadget);
155  } else {
156  if (!restoring) {
157  selectionActivated(m_defaultIndex, false);
158  }
159  }
160 }
161 
163 {
164  removeGadget();
165 }
166 
167 bool UAVGadgetView::hasGadget(IUAVGadget *uavGadget) const
168 {
169  return (m_uavGadget == uavGadget);
170 }
171 
173 {
174  m_top->setHidden(!show);
175 }
176 
178 {
179  m_uavGadgetManager->closeView(this);
180 }
181 
183 {
184  if (!m_uavGadget)
185  return;
186  tl->removeWidget(m_uavGadget->widget());
187 
188  m_uavGadget->setParent(nullptr);
189  m_uavGadget->widget()->setParent(nullptr);
190  QWidget *toolBar = m_uavGadget->toolBar();
191  if (toolBar != nullptr) {
192  if (m_activeToolBar == toolBar) {
193  m_activeToolBar = m_defaultToolBar;
194  m_activeToolBar->setVisible(true);
195  }
196  m_toolBar->layout()->removeWidget(toolBar);
197  toolBar->setParent(nullptr);
198  }
199  m_uavGadget = nullptr;
200 }
201 
203 {
204  return m_uavGadget;
205 }
206 
208 {
209  if (!uavGadget) {
210  return;
211  }
212  removeGadget();
213  m_uavGadget = uavGadget;
214  tl->addWidget(m_uavGadget->widget());
215  m_uavGadget->widget()->setParent(this);
216  m_uavGadget->widget()->show();
217  int index = indexOfClassId(m_uavGadget->classId());
218  Q_ASSERT(index >= 0);
219  m_uavGadgetList->setCurrentIndex(index);
220 
221  updateToolBar();
222 }
223 
224 void UAVGadgetView::updateToolBar()
225 {
226  if (!m_uavGadget)
227  return;
228  QComboBox *toolBar = m_uavGadget->toolBar();
229  if (!toolBar)
230  toolBar = m_defaultToolBar;
231  if (m_activeToolBar == toolBar)
232  return;
233  if (toolBar->count() == 0)
234  toolBar->hide();
235 
236  // Ugly hack to bypass Qt's styling antics regarding combobox flyouts.
237  toolBar->setView(new QListView(toolBar));
238 
239  m_toolBar->layout()->addWidget(toolBar);
240  m_activeToolBar->setVisible(false);
241  m_activeToolBar = toolBar;
242 }
243 
252 void UAVGadgetView::selectionActivated(int index, bool forceLoadConfiguration)
253 {
254  if (index < 0) // this could happen when called from SplitterOrView::restoreState()
255  index = m_defaultIndex;
256  QString classId = m_uavGadgetList->itemData(index).toString();
257  if (m_uavGadget && (m_uavGadget->classId() == classId))
258  return;
260  IUAVGadget *gadgetToRemove = m_uavGadget;
261  IUAVGadget *gadget = im->createGadget(classId, this, forceLoadConfiguration);
262 
263  setGadget(gadget);
264  m_uavGadgetManager->setCurrentGadget(gadget);
265  im->removeGadget(gadgetToRemove);
266 }
267 
273 {
274  selectionActivated(index, true);
275 }
276 
277 int UAVGadgetView::indexOfClassId(QString classId)
278 {
279  return m_uavGadgetList->findData(classId);
280 }
281 
282 void UAVGadgetView::currentGadgetChanged(IUAVGadget *gadget)
283 {
284  m_activeLabel->setVisible(m_uavGadget == gadget);
285 }
void doReplaceGadget(int index)
Slot called when the user changes the selected gadget on the view's dropbox.
QIcon gadgetIcon(QString classId) const
int indexOfClassId(QString classId)
IUAVGadget * gadget() const
IUAVGadget * createGadget(QString classId, QWidget *parent, bool forceLoadConfiguration=false)
for i
Definition: OPPlots.m:140
bool hasGadget(IUAVGadget *uavGadget) const
static ICore * instance()
Definition: coreimpl.cpp:46
void setGadget(IUAVGadget *uavGadget)
Q_DECLARE_METATYPE(Core::Internal::MenuActionContainer *) using namespace Core
QString gadgetName(QString classId) const
void selectionActivated(int index, bool forceLoadConfiguration)
Function used to select the gadget to show on this view.
virtual UAVGadgetInstanceManager * uavGadgetInstanceManager() const =0