PlotConfigWidget.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 <QApplication>
00020 #include <QClipboard>
00021 #include <QKeyEvent>
00022 #include <QLayout>
00023 #include <QMimeData>
00024 
00025 #include <ros/package.h>
00026 
00027 #include <rqt_multiplot/CurveConfigDialog.h>
00028 #include <rqt_multiplot/CurveConfigWidget.h>
00029 #include <rqt_multiplot/CurveItemWidget.h>
00030 
00031 #include <ui_PlotConfigWidget.h>
00032 
00033 #include "rqt_multiplot/PlotConfigWidget.h"
00034 
00035 namespace rqt_multiplot {
00036 
00037 /*****************************************************************************/
00038 /* Constructors and Destructor                                               */
00039 /*****************************************************************************/
00040 
00041 PlotConfigWidget::PlotConfigWidget(QWidget* parent) :
00042   QWidget(parent),
00043   ui_(new Ui::PlotConfigWidget()),
00044   config_(new PlotConfig(this)) {
00045   ui_->setupUi(this);  
00046   
00047   ui_->pushButtonAddCurve->setIcon(
00048     QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
00049     append("/resource/16x16/add.png"))));
00050   ui_->pushButtonEditCurve->setIcon(
00051     QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
00052     append("/resource/16x16/edit.png"))));
00053   ui_->pushButtonRemoveCurves->setIcon(
00054     QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
00055     append("/resource/16x16/remove.png"))));
00056 
00057   ui_->pushButtonEditCurve->setEnabled(false);
00058   ui_->pushButtonRemoveCurves->setEnabled(false);
00059   
00060   ui_->pushButtonCopyCurves->setIcon(
00061     QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
00062     append("/resource/16x16/copy.png"))));
00063   ui_->pushButtonPasteCurves->setIcon(
00064     QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
00065     append("/resource/16x16/paste.png"))));
00066   
00067   ui_->pushButtonCopyCurves->setEnabled(false);
00068   ui_->pushButtonPasteCurves->setEnabled(false);
00069   
00070   ui_->curveListWidget->installEventFilter(this);
00071   
00072   ui_->axesConfigWidget->setConfig(config_->getAxesConfig());
00073   ui_->legendConfigWidget->setConfig(config_->getLegendConfig());
00074   
00075   connect(config_, SIGNAL(titleChanged(const QString&)),
00076     this, SLOT(configTitleChanged(const QString&)));
00077   connect(config_, SIGNAL(plotRateChanged(double)),
00078     this, SLOT(configPlotRateChanged(double)));
00079   
00080   connect(ui_->lineEditTitle, SIGNAL(editingFinished()), this,
00081     SLOT(lineEditTitleEditingFinished()));
00082   
00083   connect(ui_->pushButtonAddCurve, SIGNAL(clicked()), this,
00084     SLOT(pushButtonAddCurveClicked()));
00085   connect(ui_->pushButtonEditCurve, SIGNAL(clicked()), this,
00086     SLOT(pushButtonEditCurveClicked()));
00087   connect(ui_->pushButtonRemoveCurves, SIGNAL(clicked()), this,
00088     SLOT(pushButtonRemoveCurvesClicked()));
00089   
00090   connect(ui_->pushButtonCopyCurves, SIGNAL(clicked()), this,
00091     SLOT(pushButtonCopyCurvesClicked()));
00092   connect(ui_->pushButtonPasteCurves, SIGNAL(clicked()), this,
00093     SLOT(pushButtonPasteCurvesClicked()));
00094   
00095   connect(ui_->curveListWidget, SIGNAL(itemSelectionChanged()),
00096     this, SLOT(curveListWidgetItemSelectionChanged()));
00097   connect(ui_->curveListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
00098     this, SLOT(curveListWidgetItemDoubleClicked(QListWidgetItem*)));
00099   
00100   connect(ui_->doubleSpinBoxPlotRate, SIGNAL(valueChanged(double)),
00101     this, SLOT(doubleSpinBoxPlotRateValueChanged(double)));
00102   
00103   connect(QApplication::clipboard(), SIGNAL(dataChanged()),
00104     this, SLOT(clipboardDataChanged()));
00105   
00106   configTitleChanged(config_->getTitle());
00107   configPlotRateChanged(config_->getPlotRate());
00108   
00109   clipboardDataChanged();
00110 }
00111 
00112 PlotConfigWidget::~PlotConfigWidget() {
00113   delete ui_;
00114 }
00115 
00116 /*****************************************************************************/
00117 /* Accessors                                                                 */
00118 /*****************************************************************************/
00119 
00120 void PlotConfigWidget::setConfig(const PlotConfig& config) {
00121   ui_->curveListWidget->clear();
00122   
00123   *config_ = config;
00124   
00125   for (size_t index = 0; index < config_->getNumCurves(); ++index)
00126     ui_->curveListWidget->addCurve(config_->getCurveConfig(index));
00127 }
00128 
00129 const PlotConfig& PlotConfigWidget::getConfig() const {
00130   return *config_;
00131 }
00132 
00133 /*****************************************************************************/
00134 /* Methods                                                                   */
00135 /*****************************************************************************/
00136 
00137 void PlotConfigWidget::copySelectedCurves() {
00138   QList<QListWidgetItem*> items = ui_->curveListWidget->selectedItems();
00139   
00140   if (!items.isEmpty()) {
00141     QByteArray data;
00142     QDataStream stream(&data, QIODevice::WriteOnly);
00143     
00144     stream << (quint64)items.count();
00145     
00146     for (QList<QListWidgetItem*>::const_iterator it = items.begin();
00147         it != items.end(); ++it) {
00148       CurveItemWidget* itemWidget = static_cast<CurveItemWidget*>(
00149         ui_->curveListWidget->itemWidget(*it));
00150       
00151       stream << *itemWidget->getConfig();
00152     }
00153     
00154     QMimeData* mimeData = new QMimeData();
00155     mimeData->setData(CurveConfig::MimeType+"-list", data);
00156     
00157     QApplication::clipboard()->setMimeData(mimeData);
00158   }
00159 }
00160 
00161 void PlotConfigWidget::pasteCurves() {
00162   const QMimeData* mimeData = QApplication::clipboard()->mimeData();
00163   
00164   if (mimeData && mimeData->hasFormat(CurveConfig::MimeType+"-list")) {
00165     QByteArray data = mimeData->data(CurveConfig::MimeType+"-list");
00166     QDataStream stream(&data, QIODevice::ReadOnly);
00167     
00168     quint64 numCurves;
00169     stream >> numCurves;
00170     
00171     for (size_t index = 0; index < numCurves; ++index) {
00172       CurveConfig* curveConfig = config_->addCurve();
00173       stream >> *curveConfig;
00174 
00175       while (config_->findCurves(curveConfig->getTitle()).count() > 1)
00176         curveConfig->setTitle("Copy of "+curveConfig->getTitle());
00177       
00178       ui_->curveListWidget->addCurve(curveConfig);
00179     }
00180   }
00181 }
00182 
00183 bool PlotConfigWidget::eventFilter(QObject* object, QEvent* event) {
00184   if (object == ui_->curveListWidget) {
00185     if (event->type() == QEvent::KeyPress) {
00186       QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00187       
00188       if (keyEvent->modifiers() == Qt::ControlModifier) {
00189         if (keyEvent->key() == Qt::Key_C)
00190           copySelectedCurves();
00191         else if  (keyEvent->key() == Qt::Key_V)
00192           pasteCurves();
00193       }
00194     }
00195   }
00196   
00197   return false;
00198 }
00199 
00200 /*****************************************************************************/
00201 /* Slots                                                                     */
00202 /*****************************************************************************/
00203 
00204 void PlotConfigWidget::configTitleChanged(const QString& title) {
00205   ui_->lineEditTitle->setText(title);
00206 }
00207 
00208 void PlotConfigWidget::configPlotRateChanged(double rate) {
00209   ui_->doubleSpinBoxPlotRate->setValue(rate);
00210 }
00211 
00212 void PlotConfigWidget::lineEditTitleEditingFinished() {
00213   config_->setTitle(ui_->lineEditTitle->text());
00214 }
00215 
00216 void PlotConfigWidget::pushButtonAddCurveClicked() {
00217   CurveConfigDialog dialog(this);
00218   
00219   dialog.setWindowTitle(config_->getTitle().isEmpty() ?
00220     "Add Curve to Plot" :
00221     "Add Curve to \""+config_->getTitle()+"\"");
00222   dialog.getWidget()->getConfig().getColorConfig()->setAutoColorIndex(
00223     config_->getNumCurves());
00224 
00225   if (dialog.exec() == QDialog::Accepted) {
00226     CurveConfig* curveConfig = config_->addCurve();
00227     *curveConfig = dialog.getWidget()->getConfig();
00228     
00229     ui_->curveListWidget->addCurve(curveConfig);
00230   }
00231 }
00232 
00233 void PlotConfigWidget::pushButtonEditCurveClicked() {
00234   QListWidgetItem* item = ui_->curveListWidget->currentItem();
00235   
00236   if (item) {
00237     CurveItemWidget* widget = static_cast<CurveItemWidget*>(
00238       ui_->curveListWidget->itemWidget(item));
00239     CurveConfig* curveConfig = widget->getConfig();
00240     
00241     CurveConfigDialog dialog(this);
00242     
00243     dialog.setWindowTitle(curveConfig->getTitle().isEmpty() ?
00244       "Edit Curve" :
00245       "Edit \""+curveConfig->getTitle()+"\"");
00246     dialog.getWidget()->setConfig(*curveConfig);
00247     
00248     if (dialog.exec() == QDialog::Accepted)
00249       *curveConfig = dialog.getWidget()->getConfig();
00250   }
00251 }
00252 
00253 void PlotConfigWidget::pushButtonRemoveCurvesClicked() {
00254   QList<QListWidgetItem*> items = ui_->curveListWidget->selectedItems();
00255     
00256   for (QList<QListWidgetItem*>::iterator it = items.begin();
00257       it != items.end(); ++it) {
00258     CurveItemWidget* widget = static_cast<CurveItemWidget*>(
00259       ui_->curveListWidget->itemWidget(*it));
00260     CurveConfig* curveConfig = widget->getConfig();
00261     
00262     delete *it;
00263     
00264     config_->removeCurve(curveConfig);
00265   }
00266 }
00267 
00268 void PlotConfigWidget::pushButtonCopyCurvesClicked() {
00269   copySelectedCurves();
00270 }
00271 
00272 void PlotConfigWidget::pushButtonPasteCurvesClicked() {
00273   pasteCurves();
00274 }
00275 
00276 void PlotConfigWidget::curveListWidgetItemSelectionChanged() {
00277   QList<QListWidgetItem*> items = ui_->curveListWidget->selectedItems();
00278   
00279   ui_->pushButtonEditCurve->setEnabled(items.count() == 1);
00280   ui_->pushButtonRemoveCurves->setEnabled(!items.isEmpty());
00281   
00282   ui_->pushButtonCopyCurves->setEnabled(!items.isEmpty());
00283 }
00284 
00285 void PlotConfigWidget::curveListWidgetItemDoubleClicked(QListWidgetItem*
00286     item) {
00287   pushButtonEditCurveClicked();
00288 }
00289 
00290 void PlotConfigWidget::doubleSpinBoxPlotRateValueChanged(double value) {
00291   config_->setPlotRate(value);
00292 }
00293 
00294 void PlotConfigWidget::clipboardDataChanged() {
00295   const QMimeData* mimeData = QApplication::clipboard()->mimeData();
00296   
00297   ui_->pushButtonPasteCurves->setEnabled(mimeData && mimeData->hasFormat(
00298     CurveConfig::MimeType+"-list"));
00299 }
00300 
00301 }


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