StatusWidget.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 "rqt_multiplot/StatusWidget.h"
00020 
00021 namespace rqt_multiplot {
00022 
00023 /*****************************************************************************/
00024 /* Constructors and Destructor                                               */
00025 /*****************************************************************************/
00026 
00027 StatusWidget::StatusWidget(QWidget* parent, Role role) :
00028   QWidget(parent),
00029   layout_(new QGridLayout(this)),
00030   labelIcon_(new QLabel(this)),
00031   timer_(new QTimer(this)),
00032   currentRole_(role),
00033   currentFrame_(0) {
00034   setLayout(layout_);
00035   
00036   layout_->setContentsMargins(0, 0, 0, 0);
00037   layout_->addWidget(labelIcon_, 0, 0);
00038   
00039   frames_[Okay] = QList<QPixmap>();
00040   frames_[Error] = QList<QPixmap>();
00041   frames_[Busy] = QList<QPixmap>();
00042     
00043   frameRates_[Okay] = 0.0;
00044   frameRates_[Error] = 0.0;
00045   frameRates_[Busy] = 0.0;
00046   
00047   connect(timer_, SIGNAL(timeout()), this, SLOT(timerTimeout()));
00048 }
00049 
00050 StatusWidget::~StatusWidget() {
00051 }
00052 
00053 /*****************************************************************************/
00054 /* Accessors                                                                 */
00055 /*****************************************************************************/
00056 
00057 void StatusWidget::setIcon(Role role, const QPixmap& icon) {
00058   setFrames(role, icon, 1, 0.0);
00059 }
00060 
00061 const QPixmap& StatusWidget::getIcon(Role role) const {
00062   if (frames_[role].isEmpty()) {
00063     static QPixmap icon;
00064     return icon;
00065   }
00066   else
00067     return frames_[role].front();
00068 }
00069 
00070 void StatusWidget::setFrames(Role role, const QPixmap& frames, size_t
00071     numFrames, double frameRate) {
00072   QList<QPixmap> frameList;  
00073   size_t frameHeight = frames.height()/numFrames;
00074 
00075   for (size_t i = 0; i < numFrames; ++i) {
00076     QPixmap frame = frames.copy(0, i*frameHeight, frames.width(),
00077       frameHeight);
00078     frameList.append(frame);
00079   }
00080 
00081   setFrames(role, frameList, frameRate);
00082 }
00083 
00084 void StatusWidget::setFrames(Role role, const QList<QPixmap>& frameList,
00085     double frameRate) {
00086   bool wasStarted = false;
00087   
00088   if (role == currentRole_) {
00089     wasStarted = true;
00090     stop();
00091   }
00092   
00093   frames_[role] = frameList;
00094   frameRates_[role] = frameRate;
00095 
00096   if (wasStarted)
00097     start();
00098 }
00099 
00100 const QList<QPixmap>& StatusWidget::getFrames(Role role) const {
00101   QMap<Role, QList<QPixmap> >::const_iterator it = frames_.find(role);
00102   
00103   if (it == frames_.end()) {
00104     static QList<QPixmap> frames;
00105     return frames;
00106   }
00107   else
00108     return it.value();
00109 }
00110 
00111 void StatusWidget::setFrameRate(Role role, double frameRate) {
00112   if (frameRate != frameRates_[role]) {
00113     frameRates_[role] = frameRate;
00114     
00115     if ((role == currentRole_) && timer_->isActive()) {
00116       if (frameRate > 0.0)
00117         timer_->setInterval(1.0/frameRate*1e3);
00118       else
00119         timer_->stop();
00120     }
00121   }
00122 }
00123 
00124 double StatusWidget::getFrameRate(Role role) const {
00125   return frameRates_[role];
00126 }
00127 
00128 void StatusWidget::setCurrentRole(Role role, const QString& toolTip) {
00129   if (role != currentRole_) {
00130     stop();
00131     
00132     currentRole_ = role;
00133     setToolTip(toolTip);
00134     
00135     start();
00136     
00137     emit currentRoleChanged(role);
00138   }
00139   else
00140     setToolTip(toolTip);
00141 }
00142 
00143 StatusWidget::Role StatusWidget::getCurrentRole() const {
00144   return currentRole_;
00145 }
00146 
00147 /*****************************************************************************/
00148 /* Methods                                                                   */
00149 /*****************************************************************************/
00150 
00151 void StatusWidget::pushCurrentRole() {
00152   roleStack_.append(currentRole_);
00153   toolTipStack_.append(toolTip());
00154 }
00155 
00156 bool StatusWidget::popCurrentRole() {
00157   if (!roleStack_.isEmpty()) {
00158     setCurrentRole(roleStack_.last(), toolTipStack_.last());
00159     
00160     roleStack_.removeLast();
00161     toolTipStack_.removeLast();
00162     
00163     return true;
00164   }
00165   else
00166     return false;
00167 }
00168 
00169 void StatusWidget::start() {
00170   if (timer_->isActive())
00171     timer_->stop();
00172     
00173   currentFrame_ = 0;
00174   
00175   if (!frames_[currentRole_].isEmpty()) {
00176     labelIcon_->setPixmap(frames_[currentRole_].front());
00177 
00178     if (frameRates_[currentRole_] > 0.0)
00179       timer_->start(1.0/frameRates_[currentRole_]*1e3);
00180   }  
00181 }
00182 
00183 void StatusWidget::step() {
00184   ++currentFrame_;
00185   
00186   if (currentFrame_ >= frames_[currentRole_].length())
00187     currentFrame_ = 0;
00188 
00189   if (!frames_[currentRole_].isEmpty())
00190     labelIcon_->setPixmap(frames_[currentRole_].at(currentFrame_));
00191 }
00192 
00193 void StatusWidget::stop() {
00194   if (timer_->isActive())
00195     timer_->stop();
00196 }
00197 
00198 /*****************************************************************************/
00199 /* Slots                                                                     */
00200 /*****************************************************************************/
00201 
00202 void StatusWidget::timerTimeout() {
00203   step();
00204 }
00205 
00206 }


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