StatusWidget.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 
20 
21 namespace rqt_multiplot {
22 
23 /*****************************************************************************/
24 /* Constructors and Destructor */
25 /*****************************************************************************/
26 
27 StatusWidget::StatusWidget(QWidget* parent, Role role) :
28  QWidget(parent),
29  layout_(new QGridLayout(this)),
30  labelIcon_(new QLabel(this)),
31  timer_(new QTimer(this)),
32  currentRole_(role),
33  currentFrame_(0) {
34  setLayout(layout_);
35 
36  layout_->setContentsMargins(0, 0, 0, 0);
37  layout_->addWidget(labelIcon_, 0, 0);
38 
39  frames_[Okay] = QList<QPixmap>();
40  frames_[Error] = QList<QPixmap>();
41  frames_[Busy] = QList<QPixmap>();
42 
43  frameRates_[Okay] = 0.0;
44  frameRates_[Error] = 0.0;
45  frameRates_[Busy] = 0.0;
46 
47  connect(timer_, SIGNAL(timeout()), this, SLOT(timerTimeout()));
48 }
49 
51 }
52 
53 /*****************************************************************************/
54 /* Accessors */
55 /*****************************************************************************/
56 
57 void StatusWidget::setIcon(Role role, const QPixmap& icon) {
58  setFrames(role, icon, 1, 0.0);
59 }
60 
61 const QPixmap& StatusWidget::getIcon(Role role) const {
62  if (frames_[role].isEmpty()) {
63  static QPixmap icon;
64  return icon;
65  }
66  else
67  return frames_[role].front();
68 }
69 
70 void StatusWidget::setFrames(Role role, const QPixmap& frames, size_t
71  numFrames, double frameRate) {
72  QList<QPixmap> frameList;
73  size_t frameHeight = frames.height()/numFrames;
74 
75  for (size_t i = 0; i < numFrames; ++i) {
76  QPixmap frame = frames.copy(0, i*frameHeight, frames.width(),
77  frameHeight);
78  frameList.append(frame);
79  }
80 
81  setFrames(role, frameList, frameRate);
82 }
83 
84 void StatusWidget::setFrames(Role role, const QList<QPixmap>& frameList,
85  double frameRate) {
86  bool wasStarted = false;
87 
88  if (role == currentRole_) {
89  wasStarted = true;
90  stop();
91  }
92 
93  frames_[role] = frameList;
94  frameRates_[role] = frameRate;
95 
96  if (wasStarted)
97  start();
98 }
99 
100 const QList<QPixmap>& StatusWidget::getFrames(Role role) const {
101  QMap<Role, QList<QPixmap> >::const_iterator it = frames_.find(role);
102 
103  if (it == frames_.end()) {
104  static QList<QPixmap> frames;
105  return frames;
106  }
107  else
108  return it.value();
109 }
110 
111 void StatusWidget::setFrameRate(Role role, double frameRate) {
112  if (frameRate != frameRates_[role]) {
113  frameRates_[role] = frameRate;
114 
115  if ((role == currentRole_) && timer_->isActive()) {
116  if (frameRate > 0.0)
117  timer_->setInterval(1.0/frameRate*1e3);
118  else
119  timer_->stop();
120  }
121  }
122 }
123 
124 double StatusWidget::getFrameRate(Role role) const {
125  return frameRates_[role];
126 }
127 
128 void StatusWidget::setCurrentRole(Role role, const QString& toolTip) {
129  if (role != currentRole_) {
130  stop();
131 
132  currentRole_ = role;
133  setToolTip(toolTip);
134 
135  start();
136 
137  emit currentRoleChanged(role);
138  }
139  else
140  setToolTip(toolTip);
141 }
142 
144  return currentRole_;
145 }
146 
147 /*****************************************************************************/
148 /* Methods */
149 /*****************************************************************************/
150 
152  roleStack_.append(currentRole_);
153  toolTipStack_.append(toolTip());
154 }
155 
157  if (!roleStack_.isEmpty()) {
158  setCurrentRole(roleStack_.last(), toolTipStack_.last());
159 
160  roleStack_.removeLast();
161  toolTipStack_.removeLast();
162 
163  return true;
164  }
165  else
166  return false;
167 }
168 
170  if (timer_->isActive())
171  timer_->stop();
172 
173  currentFrame_ = 0;
174 
175  if (!frames_[currentRole_].isEmpty()) {
176  labelIcon_->setPixmap(frames_[currentRole_].front());
177 
178  if (frameRates_[currentRole_] > 0.0)
179  timer_->start(1.0/frameRates_[currentRole_]*1e3);
180  }
181 }
182 
184  ++currentFrame_;
185 
186  if (currentFrame_ >= frames_[currentRole_].length())
187  currentFrame_ = 0;
188 
189  if (!frames_[currentRole_].isEmpty())
190  labelIcon_->setPixmap(frames_[currentRole_].at(currentFrame_));
191 }
192 
194  if (timer_->isActive())
195  timer_->stop();
196 }
197 
198 /*****************************************************************************/
199 /* Slots */
200 /*****************************************************************************/
201 
203  step();
204 }
205 
206 }
void setIcon(Role role, const QPixmap &icon)
void currentRoleChanged(Role role)
QMap< Role, QList< QPixmap > > frames_
Definition: StatusWidget.h:68
double getFrameRate(Role role) const
StatusWidget(QWidget *parent=0, Role role=Okay)
void setFrameRate(Role role, double frameRate)
void setFrames(Role role, const QPixmap &frames, size_t numFrames, double frameRate=10.0)
const QPixmap & getIcon(Role role) const
QMap< Role, double > frameRates_
Definition: StatusWidget.h:69
QList< QString > toolTipStack_
Definition: StatusWidget.h:71
void setCurrentRole(Role role, const QString &toolTip=QString())
const QList< QPixmap > & getFrames(Role role) const


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