PlotLegend.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <QChildEvent>
00020 #include <QDrag>
00021 #include <QEvent>
00022 #include <QMimeData>
00023 #include <QMouseEvent>
00024 
00025 #include <qwt/qwt.h>
00026 #include <qwt/qwt_dyngrid_layout.h>
00027 #if QWT_VERSION >= 0x060100
00028   #include <qwt/qwt_legend_label.h>
00029   #include <qwt/qwt_plot_legenditem.h>
00030 #else
00031   #include <qwt/qwt_legend_item.h>
00032   #include <qwt/qwt_legend_itemmanager.h>
00033 #endif
00034 
00035 #include <rqt_multiplot/PlotCurve.h>
00036 #include <rqt_multiplot/PlotWidget.h>
00037 #include <rqt_multiplot/CurveConfigDialog.h>
00038 #include <rqt_multiplot/CurveConfigWidget.h>
00039 
00040 #include "rqt_multiplot/PlotLegend.h"
00041 
00042 namespace rqt_multiplot {
00043 
00044 /*****************************************************************************/
00045 /* Constructors and Destructor                                               */
00046 /*****************************************************************************/
00047 
00048 PlotLegend::PlotLegend(QWidget* parent) :
00049   QwtLegend(parent) {
00050   QwtDynGridLayout* layout = static_cast<QwtDynGridLayout*>(
00051     contentsWidget()->layout());
00052   layout->setSpacing(10);
00053 }
00054 
00055 PlotLegend::~PlotLegend() {
00056 }
00057 
00058 /*****************************************************************************/
00059 /* Methods                                                                   */
00060 /*****************************************************************************/
00061 
00062 PlotCurve* PlotLegend::findCurve(QWidget* widget) const {
00063   #if QWT_VERSION >= 0x060100
00064     QVariant info = itemInfo(widget);
00065 
00066     if (info.canConvert<QwtPlotItem*>())
00067       return dynamic_cast<PlotCurve*>(info.value<QwtPlotItem*>());
00068     else
00069       return 0;
00070   #else
00071     QwtLegendItemManager* legendItemManager = find(widget);
00072 
00073     if (legendItemManager)
00074       return dynamic_cast<PlotCurve*>(legendItemManager);
00075     else
00076       return 0;
00077   #endif
00078 }
00079 
00080 bool PlotLegend::eventFilter(QObject* object, QEvent* event) {
00081   if (object == contentsWidget()) {
00082     if (event->type() == QEvent::ChildAdded) {
00083       QChildEvent* childEvent = static_cast<QChildEvent*>(event);
00084 
00085       #if QWT_VERSION >= 0x060100
00086         QwtLegendLabel* legendItem = qobject_cast<QwtLegendLabel*>(
00087           childEvent->child());
00088       #else
00089         QwtLegendItem* legendItem = qobject_cast<QwtLegendItem*>(
00090           childEvent->child());
00091       #endif
00092 
00093       if (legendItem) {
00094         legendItem->setCursor(Qt::PointingHandCursor);
00095         legendItem->installEventFilter(this);
00096       }
00097     }
00098   }
00099   else if (object->isWidgetType()) {
00100     QWidget* widget = static_cast<QWidget*>(object);
00101     PlotCurve* curve = findCurve(widget);
00102 
00103     if (curve && curve->getConfig()) {
00104       if (event->type() == QEvent::MouseButtonDblClick) {
00105         CurveConfig* curveConfig = curve->getConfig();
00106         CurveConfigDialog dialog(this);
00107 
00108         dialog.setWindowTitle(curveConfig->getTitle().isEmpty() ?
00109           "Edit Curve" :
00110           "Edit \""+curveConfig->getTitle()+"\"");
00111         dialog.getWidget()->setConfig(*curveConfig);
00112 
00113         if (dialog.exec() == QDialog::Accepted)
00114           *curveConfig = dialog.getWidget()->getConfig();
00115       }
00116       else if (event->type() == QEvent::MouseButtonPress) {
00117         QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
00118 
00119         if ((mouseEvent->button() == Qt::LeftButton) ||
00120             (mouseEvent->button() == Qt::RightButton)) {
00121           QByteArray data;
00122           QDataStream stream(&data, QIODevice::WriteOnly);
00123           stream << *curve->getConfig();
00124 
00125           QMimeData* mimeData = new QMimeData();
00126           mimeData->setData(CurveConfig::MimeType, data);
00127 
00128           QPixmap pixmap(widget->size());
00129           pixmap.fill(Qt::transparent);
00130           widget->render(&pixmap, QPoint(), QRegion(), QWidget::DrawChildren);
00131 
00132           QPoint hotSpot = mouseEvent->pos();
00133           hotSpot.setX(0.5*pixmap.width());
00134           hotSpot.setY(pixmap.height()+5);
00135 
00136           QDrag* drag = new QDrag(this);
00137           drag->setMimeData(mimeData);
00138           drag->setPixmap(pixmap);
00139           drag->setHotSpot(hotSpot);
00140 
00141           Qt::DropAction defaultDropAction = Qt::CopyAction;
00142           if (mouseEvent->button() == Qt::RightButton)
00143             defaultDropAction = Qt::MoveAction;
00144 
00145           Qt::DropAction dropAction = drag->exec(Qt::CopyAction |
00146             Qt::MoveAction, defaultDropAction);
00147 
00148           if (dropAction == Qt::MoveAction)
00149             curve->getConfig()->deleteLater();
00150         }
00151       }
00152     }
00153   }
00154 
00155   return QwtLegend::eventFilter(object, event);
00156 }
00157 
00158 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Tue May 9 2017 02:16:02