Go to the documentation of this file.00001 #include "rqt_mrta/status_widget.h"
00002 #include "ros/package.h"
00003
00004 namespace rqt_mrta
00005 {
00006 StatusWidget::StatusWidget(QWidget* parent, Role role)
00007 : QWidget(parent), layout_(new QGridLayout(this)),
00008 label_icon_(new QLabel(this)), timer_(new QTimer(this)),
00009 current_role_(role), current_frame_(0)
00010 {
00011 setLayout(layout_);
00012 layout_->setContentsMargins(0, 0, 0, 0);
00013 layout_->addWidget(label_icon_, 0, 0);
00014 frames_[None] = QList<QPixmap>();
00015 frames_[Okay] = QList<QPixmap>();
00016 frames_[Warn] = QList<QPixmap>();
00017 frames_[Error] = QList<QPixmap>();
00018 frames_[Busy] = QList<QPixmap>();
00019 frames_[Green] = QList<QPixmap>();
00020 frames_[Yellow] = QList<QPixmap>();
00021 frames_[Red] = QList<QPixmap>();
00022 frames_[Blue] = QList<QPixmap>();
00023 frames_[Gray] = QList<QPixmap>();
00024 frame_rates_[None] = 0.0;
00025 frame_rates_[Okay] = 0.0;
00026 frame_rates_[Warn] = 0.0;
00027 frame_rates_[Error] = 0.0;
00028 frame_rates_[Busy] = 0.0;
00029 frame_rates_[Green] = 0.0;
00030 frame_rates_[Yellow] = 0.0;
00031 frame_rates_[Red] = 0.0;
00032 frame_rates_[Blue] = 0.0;
00033 frame_rates_[Gray] = 0.0;
00034 QPixmap pixmap_none(QString::fromStdString(
00035 ros::package::getPath("rqt_mrta").append("/resource/22x22/none.png")));
00036 QPixmap pixmap_okay(QString::fromStdString(
00037 ros::package::getPath("rqt_mrta").append("/resource/22x22/okay.png")));
00038 QPixmap pixmap_warn(QString::fromStdString(
00039 ros::package::getPath("rqt_mrta").append("/resource/22x22/warn.png")));
00040 QPixmap pixmap_error(QString::fromStdString(
00041 ros::package::getPath("rqt_mrta").append("/resource/22x22/error.png")));
00042 QPixmap pixmap_busy(QString::fromStdString(
00043 ros::package::getPath("rqt_mrta").append("/resource/22x22/busy.png")));
00044 QPixmap pixmap_green(QString::fromStdString(
00045 ros::package::getPath("rqt_mrta").append("/resource/22x22/green.png")));
00046 QPixmap pixmap_yellow(QString::fromStdString(
00047 ros::package::getPath("rqt_mrta").append("/resource/22x22/yellow.png")));
00048 QPixmap pixmap_red(QString::fromStdString(
00049 ros::package::getPath("rqt_mrta").append("/resource/22x22/red.png")));
00050 QPixmap pixmap_blue(QString::fromStdString(
00051 ros::package::getPath("rqt_mrta").append("/resource/22x22/blue.png")));
00052 QPixmap pixmap_gray(QString::fromStdString(
00053 ros::package::getPath("rqt_mrta").append("/resource/22x22/gray.png")));
00054 setIcon(None, pixmap_none);
00055 setIcon(Okay, pixmap_okay);
00056 setIcon(Warn, pixmap_warn);
00057 setIcon(Error, pixmap_error);
00058 setFrames(Busy, pixmap_busy, 8);
00059 setIcon(Green, pixmap_green);
00060 setIcon(Yellow, pixmap_yellow);
00061 setIcon(Red, pixmap_red);
00062 setIcon(Blue, pixmap_blue);
00063 setIcon(Gray, pixmap_gray);
00064 connect(timer_, SIGNAL(timeout()), this, SLOT(timerTimeout()));
00065 }
00066
00067 StatusWidget::~StatusWidget() {}
00068
00069 void StatusWidget::setIcon(Role role, const QPixmap& icon)
00070 {
00071 setFrames(role, icon, 1, 0.0);
00072 }
00073
00074 const QPixmap& StatusWidget::getIcon(Role role) const
00075 {
00076 if (frames_[role].isEmpty())
00077 {
00078 static QPixmap icon;
00079 return icon;
00080 }
00081 return frames_[role].front();
00082 }
00083
00084 void StatusWidget::setFrames(Role role, const QPixmap& frames,
00085 size_t num_frames, double frame_rate)
00086 {
00087 QList<QPixmap> frame_list;
00088 size_t frame_height = frames.height() / num_frames;
00089 for (size_t i = 0; i < num_frames; ++i)
00090 {
00091 QPixmap frame =
00092 frames.copy(0, i * frame_height, frames.width(), frame_height);
00093 frame_list.append(frame);
00094 }
00095 setFrames(role, frame_list, frame_rate);
00096 }
00097
00098 void StatusWidget::setFrames(Role role, const QList<QPixmap>& frame_list,
00099 double frame_rate)
00100 {
00101 bool wasStarted(false);
00102 if (role == current_role_)
00103 {
00104 wasStarted = true;
00105 stop();
00106 }
00107 frames_[role] = frame_list;
00108 frame_rates_[role] = frame_rate;
00109 if (wasStarted)
00110 {
00111 start();
00112 }
00113 }
00114
00115 const QList<QPixmap>& StatusWidget::getFrames(Role role) const
00116 {
00117 QMap<Role, QList<QPixmap>>::const_iterator it(frames_.find(role));
00118 if (it == frames_.end())
00119 {
00120 static QList<QPixmap> frames;
00121 return frames;
00122 }
00123 return it.value();
00124 }
00125
00126 void StatusWidget::setFrameRate(Role role, double frame_rate)
00127 {
00128 if (frame_rate != frame_rates_[role])
00129 {
00130 frame_rates_[role] = frame_rate;
00131 if ((role == current_role_) && timer_->isActive())
00132 {
00133 if (frame_rate > 0.0)
00134 {
00135 timer_->setInterval(1.0 / frame_rate * 1e3);
00136 }
00137 else
00138 {
00139 timer_->stop();
00140 }
00141 }
00142 }
00143 }
00144
00145 double StatusWidget::getFrameRate(Role role) const
00146 {
00147 return frame_rates_[role];
00148 }
00149
00150 void StatusWidget::setCurrentRole(Role role, const QString& tool_tip)
00151 {
00152 if (role != current_role_)
00153 {
00154 stop();
00155 current_role_ = role;
00156 setToolTip(tool_tip);
00157 start();
00158 emit currentRoleChanged(role);
00159 }
00160 else
00161 {
00162 setToolTip(tool_tip);
00163 }
00164 }
00165
00166 StatusWidget::Role StatusWidget::getCurrentRole() const
00167 {
00168 return current_role_;
00169 }
00170
00171 void StatusWidget::pushCurrentRole()
00172 {
00173 role_stack_.append(current_role_);
00174 tool_tip_stack_.append(toolTip());
00175 }
00176
00177 bool StatusWidget::popCurrentRole()
00178 {
00179 if (role_stack_.isEmpty())
00180 {
00181 return false;
00182 }
00183 setCurrentRole(role_stack_.last(), tool_tip_stack_.last());
00184 role_stack_.removeLast();
00185 tool_tip_stack_.removeLast();
00186 return true;
00187 }
00188
00189 void StatusWidget::start()
00190 {
00191 if (timer_->isActive())
00192 {
00193 timer_->stop();
00194 }
00195 current_frame_ = 0;
00196 if (!frames_[current_role_].isEmpty())
00197 {
00198 label_icon_->setPixmap(frames_[current_role_].front());
00199 if (frame_rates_[current_role_] > 0.0)
00200 {
00201 timer_->start(1.0 / frame_rates_[current_role_] * 1e3);
00202 }
00203 }
00204 }
00205
00206 void StatusWidget::step()
00207 {
00208 ++current_frame_;
00209 if (current_frame_ >= frames_[current_role_].length())
00210 {
00211 current_frame_ = 0;
00212 }
00213 if (!frames_[current_role_].isEmpty())
00214 {
00215 label_icon_->setPixmap(frames_[current_role_].at(current_frame_));
00216 }
00217 }
00218
00219 void StatusWidget::stop()
00220 {
00221 if (timer_->isActive())
00222 {
00223 timer_->stop();
00224 }
00225 }
00226
00227 void StatusWidget::timerTimeout() { step(); }
00228 }