PlotLegend.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <QChildEvent>
20 #include <QDrag>
21 #include <QEvent>
22 #include <QMimeData>
23 #include <QMouseEvent>
24 
25 #include <qwt/qwt.h>
26 #include <qwt/qwt_dyngrid_layout.h>
27 #if QWT_VERSION >= 0x060100
28  #include <qwt/qwt_legend_label.h>
29  #include <qwt/qwt_plot_legenditem.h>
30 #else
31  #include <qwt/qwt_legend_item.h>
32  #include <qwt/qwt_legend_itemmanager.h>
33 #endif
34 
39 
41 
42 namespace rqt_multiplot {
43 
44 /*****************************************************************************/
45 /* Constructors and Destructor */
46 /*****************************************************************************/
47 
48 PlotLegend::PlotLegend(QWidget* parent) :
49  QwtLegend(parent) {
50  QwtDynGridLayout* layout = static_cast<QwtDynGridLayout*>(
51  contentsWidget()->layout());
52  layout->setSpacing(10);
53 }
54 
56 }
57 
58 /*****************************************************************************/
59 /* Methods */
60 /*****************************************************************************/
61 
62 PlotCurve* PlotLegend::findCurve(QWidget* widget) const {
63  #if QWT_VERSION >= 0x060100
64  QVariant info = itemInfo(widget);
65 
66  if (info.canConvert<QwtPlotItem*>())
67  return dynamic_cast<PlotCurve*>(info.value<QwtPlotItem*>());
68  else
69  return 0;
70  #else
71  QwtLegendItemManager* legendItemManager = find(widget);
72 
73  if (legendItemManager)
74  return dynamic_cast<PlotCurve*>(legendItemManager);
75  else
76  return 0;
77  #endif
78 }
79 
80 bool PlotLegend::eventFilter(QObject* object, QEvent* event) {
81  if (object == contentsWidget()) {
82  if (event->type() == QEvent::ChildAdded) {
83  QChildEvent* childEvent = static_cast<QChildEvent*>(event);
84 
85  #if QWT_VERSION >= 0x060100
86  QwtLegendLabel* legendItem = qobject_cast<QwtLegendLabel*>(
87  childEvent->child());
88  #else
89  QwtLegendItem* legendItem = qobject_cast<QwtLegendItem*>(
90  childEvent->child());
91  #endif
92 
93  if (legendItem) {
94  legendItem->setCursor(Qt::PointingHandCursor);
95  legendItem->installEventFilter(this);
96  }
97  }
98  }
99  else if (object->isWidgetType()) {
100  QWidget* widget = static_cast<QWidget*>(object);
101  PlotCurve* curve = findCurve(widget);
102 
103  if (curve && curve->getConfig()) {
104  if (event->type() == QEvent::MouseButtonDblClick) {
105  CurveConfig* curveConfig = curve->getConfig();
106  CurveConfigDialog dialog(this);
107 
108  dialog.setWindowTitle(curveConfig->getTitle().isEmpty() ?
109  "Edit Curve" :
110  "Edit \""+curveConfig->getTitle()+"\"");
111  dialog.getWidget()->setConfig(*curveConfig);
112 
113  if (dialog.exec() == QDialog::Accepted)
114  *curveConfig = dialog.getWidget()->getConfig();
115  }
116  else if (event->type() == QEvent::MouseButtonPress) {
117  QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
118 
119  if ((mouseEvent->button() == Qt::LeftButton) ||
120  (mouseEvent->button() == Qt::RightButton)) {
121  QByteArray data;
122  QDataStream stream(&data, QIODevice::WriteOnly);
123  stream << *curve->getConfig();
124 
125  QMimeData* mimeData = new QMimeData();
126  mimeData->setData(CurveConfig::MimeType, data);
127 
128  QPixmap pixmap(widget->size());
129  pixmap.fill(Qt::transparent);
130  widget->render(&pixmap, QPoint(), QRegion(), QWidget::DrawChildren);
131 
132  QPoint hotSpot = mouseEvent->pos();
133  hotSpot.setX(0.5*pixmap.width());
134  hotSpot.setY(pixmap.height()+5);
135 
136  QDrag* drag = new QDrag(this);
137  drag->setMimeData(mimeData);
138  drag->setPixmap(pixmap);
139  drag->setHotSpot(hotSpot);
140 
141  Qt::DropAction defaultDropAction = Qt::CopyAction;
142  if (mouseEvent->button() == Qt::RightButton)
143  defaultDropAction = Qt::MoveAction;
144 
145  Qt::DropAction dropAction = drag->exec(Qt::CopyAction |
146  Qt::MoveAction, defaultDropAction);
147 
148  if (dropAction == Qt::MoveAction)
149  curve->getConfig()->deleteLater();
150  }
151  }
152  }
153  }
154 
155  return QwtLegend::eventFilter(object, event);
156 }
157 
158 }
static const QString MimeType
Definition: CurveConfig.h:41
void setConfig(const CurveConfig &config)
bool eventFilter(QObject *object, QEvent *event)
Definition: PlotLegend.cpp:80
CurveConfig * getConfig() const
Definition: PlotCurve.cpp:105
PlotLegend(QWidget *parent)
Definition: PlotLegend.cpp:48
PlotCurve * findCurve(QWidget *widget) const
Definition: PlotLegend.cpp:62
CurveConfigWidget * getWidget() const
const QString & getTitle() const
Definition: CurveConfig.cpp:69


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44