MessageFieldWidget.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 <ui_MessageFieldWidget.h>
00020 
00021 #include "rqt_multiplot/MessageFieldWidget.h"
00022 
00023 namespace rqt_multiplot {
00024 
00025 /*****************************************************************************/
00026 /* Constructors and Destructor                                               */
00027 /*****************************************************************************/
00028 
00029 MessageFieldWidget::MessageFieldWidget(QWidget* parent) :
00030   QWidget(parent),
00031   ui_(new Ui::MessageFieldWidget()),
00032   loader_(new MessageDefinitionLoader(this)),
00033   isLoading_(false),
00034   registry_(new MessageSubscriberRegistry(this)),
00035   isConnecting_(false),
00036   connectionTimer_(new QTimer(this)) {
00037   ui_->setupUi(this);
00038   
00039   connectionTimer_->setSingleShot(true);    
00040     
00041   connect(loader_, SIGNAL(loadingStarted()), this,
00042     SLOT(loaderLoadingStarted()));
00043   connect(loader_, SIGNAL(loadingFinished()), this,
00044     SLOT(loaderLoadingFinished()));
00045   connect(loader_, SIGNAL(loadingFailed(const QString&)), this,
00046     SLOT(loaderLoadingFailed(const QString&)));
00047   
00048   connect(connectionTimer_, SIGNAL(timeout()), this,
00049     SLOT(connectionTimerTimeout()));
00050   
00051   connect(ui_->lineEdit, SIGNAL(currentFieldChanged(const QString&)),
00052     this, SLOT(lineEditCurrentFieldChanged(const QString&)));
00053   connect(ui_->treeWidget, SIGNAL(currentFieldChanged(const QString&)),
00054     this, SLOT(treeWidgetCurrentFieldChanged(const QString&)));
00055 }
00056 
00057 MessageFieldWidget::~MessageFieldWidget() {
00058 }
00059 
00060 /*****************************************************************************/
00061 /* Accessors                                                                 */
00062 /*****************************************************************************/
00063 
00064 QString MessageFieldWidget::getCurrentMessageType() const {
00065   return loader_->getType();
00066 }
00067 
00068 variant_topic_tools::MessageDataType MessageFieldWidget::
00069     getCurrentMessageDataType() const {
00070   return loader_->getDefinition().getMessageDataType();
00071 }
00072 
00073 void MessageFieldWidget::setCurrentField(const QString& field) {
00074   if (field != currentField_) {
00075     currentField_ = field;
00076     
00077     ui_->lineEdit->setCurrentField(field);
00078     ui_->treeWidget->setCurrentField(field);
00079     
00080     emit currentFieldChanged(field);
00081   }
00082 }
00083 
00084 QString MessageFieldWidget::getCurrentField() const {
00085   return currentField_;
00086 }
00087 
00088 variant_topic_tools::DataType MessageFieldWidget::
00089     getCurrentFieldDataType() const {
00090   return ui_->treeWidget->getCurrentFieldDataType();
00091 }
00092 
00093 bool MessageFieldWidget::isLoading() const {
00094   return isLoading_;
00095 }
00096 
00097 bool MessageFieldWidget::isConnecting() const {
00098   return isConnecting_;
00099 }
00100 
00101 bool MessageFieldWidget::isCurrentFieldDefined() const {
00102   return getCurrentFieldDataType().isValid();
00103 }
00104 
00105 /*****************************************************************************/
00106 /* Methods                                                                   */
00107 /*****************************************************************************/
00108 
00109 void MessageFieldWidget::loadFields(const QString& type) {
00110   if (isConnecting_) {
00111     disconnect();
00112     
00113     ui_->treeWidget->clear();
00114     setEnabled(true);
00115   }
00116   
00117   loader_->load(type);
00118 }
00119 
00120 void MessageFieldWidget::connectTopic(const QString& topic, double
00121     timeout) {
00122   loader_->wait();
00123   
00124   if (topic != subscribedTopic_) {
00125     if (isConnecting_) {
00126       disconnect();
00127       
00128       ui_->treeWidget->clear();
00129       setEnabled(true);
00130     }
00131   
00132     if (registry_->subscribe(topic, this, SLOT(subscriberMessageReceived(
00133         const QString&, const Message&)))) {
00134       setEnabled(false);
00135       
00136       isConnecting_ = true;
00137       subscribedTopic_ = topic;
00138       if (timeout > 0.0)
00139         connectionTimer_->start(timeout*1e3);
00140       emit connecting(topic);
00141       
00142       ui_->treeWidget->clear();
00143     }
00144   }
00145 }
00146 
00147 void MessageFieldWidget::disconnect() {
00148   registry_->unsubscribe(subscribedTopic_, this);
00149   
00150   isConnecting_ = false;
00151   subscribedTopic_.clear();
00152   connectionTimer_->stop();
00153 }
00154 
00155 /*****************************************************************************/
00156 /* Slots                                                                     */
00157 /*****************************************************************************/
00158 
00159 void MessageFieldWidget::loaderLoadingStarted() {
00160   setEnabled(false);  
00161   ui_->treeWidget->clear();
00162   
00163   isLoading_ = true;
00164   
00165   emit loadingStarted();
00166 }
00167 
00168 void MessageFieldWidget::loaderLoadingFinished() {
00169   ui_->lineEdit->setMessageDataType(loader_->getDefinition().
00170     getMessageDataType());
00171   ui_->treeWidget->setMessageDataType(loader_->getDefinition().
00172     getMessageDataType());
00173   
00174   ui_->lineEdit->setCurrentField(currentField_);
00175   ui_->treeWidget->setCurrentField(currentField_);
00176   
00177   setEnabled(true);
00178   
00179   isLoading_ = false;
00180   
00181   emit loadingFinished();  
00182 }
00183 
00184 void MessageFieldWidget::loaderLoadingFailed(const QString& error) {
00185   ui_->treeWidget->clear();
00186   
00187   isLoading_ = false;
00188   emit loadingFailed(error);
00189 }
00190 
00191 void MessageFieldWidget::subscriberMessageReceived(const QString& topic,
00192     const Message& message) {
00193   if (!isConnecting_)
00194     return;
00195   
00196   disconnect();
00197   
00198   ui_->lineEdit->setMessageDataType(message.getVariant().getType());
00199   ui_->treeWidget->setMessageDataType(message.getVariant().getType());
00200   
00201   ui_->lineEdit->setCurrentField(currentField_);
00202   ui_->treeWidget->setCurrentField(currentField_);
00203   
00204   setEnabled(true);
00205   
00206   emit connected(topic);
00207 }
00208 
00209 void MessageFieldWidget::connectionTimerTimeout() {
00210   if (!isConnecting_)
00211     return;
00212   
00213   QString topic = subscribedTopic_;
00214   double timeout = connectionTimer_->interval()*1e-3;
00215   
00216   disconnect();
00217   
00218   ui_->treeWidget->clear();
00219   
00220   emit connectionTimeout(topic, timeout);
00221 }
00222 
00223 void MessageFieldWidget::lineEditCurrentFieldChanged(const QString& field) {
00224   setCurrentField(field);
00225 }
00226 
00227 void MessageFieldWidget::treeWidgetCurrentFieldChanged(const QString& field) {
00228   setCurrentField(field);
00229 }
00230 
00231 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Thu Jun 6 2019 21:49:11