00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "PSQt/WdgColorBar.h"
00011 #include "PSQt/Logger.h"
00012
00013 #include <QString>
00014 #include <string>
00015
00016 #include <iostream>
00017 #include <cstring>
00018 #include <sstream>
00019 #include <algorithm>
00020 #include <iomanip>
00021
00022
00023
00024 namespace PSQt {
00025
00026
00027
00028 WdgColorBar::WdgColorBar( QWidget *parent,
00029 const float& h1,
00030 const float& h2,
00031 const unsigned& colors,
00032 const ORIENT& orient,
00033 const float& aspect)
00034 : QLabel(parent)
00035 , m_lab_cbar(this)
00036 , m_pixmap_cbar(0)
00037 {
00038
00039 setStyle();
00040
00041 setColorBar(h1, h2, colors, orient, aspect);
00042
00043
00044 }
00045
00046
00047
00048 void WdgColorBar::connectForTest()
00049 {
00050
00051 connect(this, SIGNAL(pressColorBar(QMouseEvent*, const float&)),
00052 this, SLOT(testPressColorBar(QMouseEvent*, const float&)));
00053
00054 connect(this, SIGNAL(releaseColorBar(QMouseEvent*, const float&)),
00055 this, SLOT(testReleaseColorBar(QMouseEvent*, const float&)));
00056
00057
00058
00059 }
00060
00061
00062
00063 WdgColorBar::~WdgColorBar()
00064 {
00065 delete m_pixmap_cbar;
00066 }
00067
00068
00069
00070 void
00071 WdgColorBar::showTips()
00072 {
00073 this -> setToolTip("Color bar with mouse button control");
00074 }
00075
00076
00077
00078 void
00079 WdgColorBar::setStyle()
00080 {
00081 this -> setMargin(0);
00082 this -> setWindowTitle(tr("Color bar"));
00083
00084
00085
00086
00087 }
00088
00089
00090
00091 void
00092 WdgColorBar::resizeEvent(QResizeEvent *e)
00093 {
00094 this->setPixmap(m_pixmap_cbar->scaled(this->size(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
00095
00096
00097
00098
00099
00100
00101 }
00102
00103
00104
00105 void
00106 WdgColorBar::closeEvent(QCloseEvent *e)
00107 {
00108 QWidget::closeEvent(e);
00109
00110
00111 }
00112
00113
00114 void
00115 WdgColorBar::moveEvent(QMoveEvent *e)
00116 {
00117
00118
00119
00120
00121 }
00122
00123
00124
00125 float
00126 WdgColorBar::fractionOfColorBar(QMouseEvent *e)
00127 {
00128
00129 float fraction = 0;
00130
00131 float width = this->size().width();
00132 float height = this->size().height();
00133 float x = e->pos().x();
00134 float y = e->pos().y();
00135
00136 if (m_orient == HR) fraction = (width) ? x/width : 0;
00137 else if (m_orient == HL) fraction = (width) ? (width-x)/width : 0;
00138 else if (m_orient == VU) fraction = (height) ? (height-y)/height : 0;
00139 else if (m_orient == VD) fraction = (height) ? y/height : 0;
00140
00141 return fraction;
00142 }
00143
00144
00145
00146 void
00147 WdgColorBar::mousePressEvent(QMouseEvent *e)
00148 {
00149 float f = fractionOfColorBar(e);
00150 emit pressColorBar(e, f);
00151 }
00152
00153
00154
00155 void
00156 WdgColorBar::mouseReleaseEvent(QMouseEvent *e)
00157 {
00158 float f = fractionOfColorBar(e);
00159 emit releaseColorBar(e, f);
00160 }
00161
00162
00163
00164 void
00165 WdgColorBar::mouseMoveEvent(QMouseEvent *e)
00166 {
00167 float f = fractionOfColorBar(e);
00168 emit moveOnColorBar(e, f);
00169 }
00170
00171
00172
00173 void
00174 WdgColorBar::onHueAnglesUpdated(const float& h1, const float& h2)
00175 {
00176 this->setColorBar(h1, h2, m_colors, m_orient, m_aspect);
00177
00178 this->setPixmap(m_pixmap_cbar->scaled(this->size(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
00179
00180 std::stringstream ss; ss << "Set hue angles h1:" << m_h1 << " h2:" << m_h2;
00181 MsgInLog(_name_(), DEBUG, ss.str());
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 void
00196 WdgColorBar::setColorBar( const float& h1,
00197 const float& h2,
00198 const unsigned& colors,
00199 const ORIENT& orient,
00200 const float& aspect
00201 )
00202 {
00203 m_h1 = h1;
00204 m_h2 = h2;
00205 m_colors = colors;
00206 m_orient = orient;
00207 m_aspect = aspect;
00208
00209 const unsigned cols = (orient==HR || orient==HL) ? colors : unsigned(colors * aspect);
00210 const unsigned rows = (orient==HR || orient==HL) ? unsigned(colors * aspect) : colors;
00211
00212 uint32_t dimg[rows][cols];
00213 uint32_t* ctable = ColorTable(colors, h1, h2);
00214 uint32_t ctable_mirror[colors];
00215
00216 switch (orient) {
00217 default :
00218 case HR :
00219 for(unsigned r=0; r<rows; ++r)
00220 std::memcpy(&dimg[r][0], &ctable[0], cols*sizeof(uint32_t));
00221 break;
00222
00223 case HL :
00224 for(unsigned c=0; c<colors; ++c) ctable_mirror[c] = ctable[colors-c-1];
00225 for(unsigned r=0; r<rows; ++r)
00226 std::memcpy(&dimg[r][0], &ctable_mirror[0], cols*sizeof(uint32_t));
00227 break;
00228
00229 case VU :
00230 for(unsigned r=0; r<rows; ++r)
00231 std::fill_n(&dimg[r][0], int(cols), ctable[rows-r-1]);
00232 break;
00233
00234 case VD :
00235 for(unsigned r=0; r<rows; ++r)
00236 std::fill_n(&dimg[r][0], int(cols), ctable[r]);
00237 break;
00238
00239 MsgInLog(_name_(), WARNING, "Wrong orientation parameter for color bar object.");
00240 }
00241
00242 QImage image((const uchar*) &dimg[0][0], cols, rows, QImage::Format_ARGB32);
00243 setPixmapForLabel(image, m_pixmap_cbar, m_lab_cbar);
00244 }
00245
00246
00247
00248
00249
00250
00251 void
00252 WdgColorBar::message(QMouseEvent* e, const float& fraction, const char* comment)
00253 {
00254 std::stringstream ss; ss << comment
00255 << " : button " << int(e->button())
00256 << " ratio from zero:" << std::fixed << std::setprecision(3) << fraction;
00257 MsgInLog(_name_(), DEBUG, ss.str());
00258
00259 }
00260
00261
00262
00263 void
00264 WdgColorBar::testPressColorBar(QMouseEvent* e, const float& ratio)
00265 {
00266 this->message(e, ratio, ":testPressColorBar");
00267 }
00268
00269
00270
00271 void
00272 WdgColorBar::testReleaseColorBar(QMouseEvent* e, const float& ratio)
00273 {
00274 this->message(e, ratio, "testReleaseColorBar");
00275 }
00276
00277
00278
00279 void
00280 WdgColorBar::testMoveOnColorBar(QMouseEvent* e, const float& ratio)
00281 {
00282 this->message(e, ratio, "testMoveOnColorBar");
00283 }
00284
00285
00286
00287 }
00288
00289