PSQt/src/WdgColorTable.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------
00002 // File and Version Information:
00003 //   $Id: WdgColorTable.cpp 9841 2015-03-26 18:35:44Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Author: Mikhail S. Dubrovin
00006 //---------------------------------------------------------------------
00007 
00008 //--------------------------
00009 
00010 #include "PSQt/WdgColorTable.h"
00011 #include "PSQt/Logger.h"
00012 
00013 #include <QString>
00014 #include <string>
00015 
00016 #include <iostream>    // cout
00017 #include <cstring>  // for memcpy, placed in the std namespace
00018 #include <sstream>   // for stringstream
00019 
00020 //using namespace std; // for cout without std::
00021 
00022 namespace PSQt {
00023 
00024 //--------------------------
00025 
00026 WdgColorTable::WdgColorTable( QWidget *parent, const float& h1, const float& h2, const unsigned& colors )
00027     : QWidget(parent)
00028     , m_h1(h1)
00029     , m_h2(h2)
00030     , m_colors(colors)
00031 {
00032 
00033   m_figsize = 256;
00034   m_cbar_width = 40;
00035 
00036   this -> setFrame();
00037 
00038   m_edi_h1 = new QLineEdit( val_to_string<float>(m_h1).c_str(), this );
00039   m_edi_h2 = new QLineEdit( val_to_string<float>(m_h2).c_str(), this );
00040   m_but_apply = new QPushButton("Apply");
00041   m_lab_cring = new PSQt::LabColorRing(this, m_figsize, m_h1, m_h2);
00042 
00043   QChar chars1[] = {0x03B1, '1', ':'};
00044   QChar chars2[] = {0x03B1, '2', ':'};
00045   m_lab_h1    = new QLabel(QString(chars1));
00046   m_lab_h2    = new QLabel(QString(chars2));
00047   m_lab_cbar  = new QLabel("L2", this);
00048 
00049   connect( m_edi_h1, SIGNAL( editingFinished() ), this, SLOT(onEdiH1()) );
00050   connect( m_edi_h2, SIGNAL( editingFinished() ), this, SLOT(onEdiH2()) );
00051   connect( m_but_apply, SIGNAL( clicked() ), this, SLOT(onButApply()) );
00052   //connect( m_lab_cring, SIGNAL( mouseReleaseEvent(QMouseEvent*) ), this, SLOT(onSetH()) );
00053   connect( m_lab_cring, SIGNAL( hueAngleIsMoving(const unsigned&) ), 
00054            this, SLOT(onSetH(const unsigned&)) );
00055  
00056   connect( m_lab_cring, SIGNAL( hueAngleIsMoved() ), 
00057            this, SLOT(onHueAngleIsChanged()) );
00058 
00059   connect( this, SIGNAL( hueAngleIsEdited(const unsigned&) ), 
00060            m_lab_cring, SLOT(onSetShifter(const unsigned&)) );
00061 
00062   connect( this, SIGNAL( hueAnglesUpdated(const float&, const float&) ), 
00063            this, SLOT(testSignalHueAnglesUpdated(const float&, const float&)) );
00064   
00065   QHBoxLayout *hbox1 = new QHBoxLayout();
00066   hbox1 -> addWidget(m_lab_h1);
00067   hbox1 -> addWidget(m_edi_h1);
00068   hbox1 -> addStretch(1);
00069   hbox1 -> addWidget(m_lab_h2);
00070   hbox1 -> addWidget(m_edi_h2);
00071   hbox1 -> addStretch(1);
00072   hbox1 -> addWidget(m_but_apply);
00073 
00074   QVBoxLayout *vbox = new QVBoxLayout();
00075   vbox -> addWidget(m_lab_cring);
00076   vbox -> addLayout(hbox1);
00077   vbox -> addWidget(m_lab_cbar);
00078   vbox -> addStretch(1);
00079 
00080   //QHBoxLayout *hbox = new QHBoxLayout();
00081   //hbox -> addWidget(m_lab_cring);
00082   //hbox -> addLayout(vbox);
00083 
00084   this -> setLayout(vbox);
00085   this -> setWindowTitle(tr("Color Table"));
00086   this -> move(100,50);  
00087 
00088   showTips();
00089   setStyle();
00090 
00091   m_pixmap_cbar=0;
00092   setColorBar(m_h1, m_h2, m_cbar_width, m_figsize);
00093 
00094   //this        -> setMouseTracking(true);
00095   //m_lab_cring -> setMouseTracking(true);
00096   //m_lab_cring->installEventFilter(this);
00097   //this->viewport()->setMouseTracking(true);
00098 }
00099 
00100 //--------------------------
00101 
00102 void
00103 WdgColorTable::showTips() 
00104 {
00105   m_edi_h1 -> setToolTip("Hue angle 1");
00106   m_edi_h2 -> setToolTip("Hue angle 2");
00107 }
00108 
00109 //--------------------------
00110 
00111 void
00112 WdgColorTable::setFrame() 
00113 {
00114   m_frame = new QFrame(this);
00115   m_frame -> setFrameStyle ( QFrame::Box | QFrame::Sunken); // or
00116   //m_frame -> setFrameStyle ( QFrame::Box );    // NoFrame, Box, Panel, WinPanel, ..., StyledPanel 
00117   //m_frame -> setFrameShadow( QFrame::Sunken ); // Plain, Sunken, Raised 
00118   m_frame -> setLineWidth(0);
00119   m_frame -> setMidLineWidth(1);
00120   //m_frame -> setCursor(Qt::SizeAllCursor);     // Qt::WaitCursor, Qt::PointingHandCursor
00121   //m_frame -> setStyleSheet("background-color: rgb(0, 255, 255); color: rgb(255, 255, 100)");
00122 }
00123 
00124 //--------------------------
00125 
00126 void
00127 WdgColorTable::setStyle() 
00128 {
00129   m_edi_h1   -> setFixedSize(50,30);
00130   m_edi_h2   -> setFixedSize(50,30);
00131   m_lab_cbar -> setMargin(0);
00132   m_lab_cbar -> setFixedSize(m_figsize, m_cbar_width);
00133   m_but_apply-> setFixedSize(60,30);
00134   this       -> setFixedWidth(m_figsize+22);
00135 
00136   //m_edi_h1    -> setMinimumSize(80,30);
00137   //m_edi_h2    -> setMinimumSize(80,30);
00138   //m_lab_cring -> setFixedSize(100,100);
00139 }
00140 
00141 //--------------------------
00142 
00143 void 
00144 WdgColorTable::resizeEvent(QResizeEvent *event)
00145 {
00146 //m_frame->setGeometry(this->rect());
00147   m_frame->setGeometry(0, 0, event->size().width(), event->size().height());
00148   //setWindowTitle("Window resized");
00149   //std::cout << "WdgColorTable::resizeEvent(...): w=" << event->size().width() 
00150   //                                          << " h=" << event->size().height() << '\n';
00151 
00152   m_lab_cbar ->setPixmap(m_pixmap_cbar ->scaled(m_lab_cbar->size(), Qt::KeepAspectRatio, Qt::FastTransformation));
00153   //  m_lab_cring->setPixmap(m_pixmap_cring->scaled(m_lab_cring->size(), Qt::KeepAspectRatio, Qt::FastTransformation));
00154 }
00155 
00156 //--------------------------
00157 
00158 void 
00159 WdgColorTable::closeEvent(QCloseEvent *event)
00160 {
00161   QWidget::closeEvent(event);
00162   std::stringstream ss; ss << "closeEvent(...): type = " << event -> type();
00163   MsgInLog(_name_(), DEBUG, ss.str());
00164 }
00165 
00166 //--------------------------
00167 void
00168 WdgColorTable::moveEvent(QMoveEvent *event)
00169 {
00170   //int x = event->pos().x();
00171   //int y = event->pos().y();
00172   //QString text = QString::number(x) + "," + QString::number(y);
00173   //setWindowTitle(text);
00174 }
00175 
00176 //--------------------------
00177 
00178 void 
00179 WdgColorTable::mousePressEvent(QMouseEvent *event)
00180 {
00181   //int x = event->pos().x();
00182   //int y = event->pos().y();
00183   //QString text = "mousePressEvent: " + QString::number(x) + "," + QString::number(y);
00184   //std::cout << text.toStdString()  << std::endl;
00185 }
00186 //--------------------------
00187 
00188 void 
00189 WdgColorTable::mouseMoveEvent(QMouseEvent *e)
00190 {
00191   //    std::cout << "WdgColorTable::mouseMoveEvent: "
00192   //            << "  x(),y() = "  << e->x() << ", " << e->y()
00193   //            << '\n';
00194 }
00195 
00196 //--------------------------
00197 
00198 //  bool 
00199 //  WdgColorTable::eventFilter(QObject *obj, QEvent *e)
00200 //  {
00201 //    if(obj==this && (e->type()==QEvent::Enter || e->type()==QEvent::Leave)) std::cout << "WdgColorTable::eventFilter(...):\n";
00202 //    return false;
00203 //  }
00204 
00205 //--------------------------
00206 
00207 void 
00208 WdgColorTable::onEdiH1()
00209 {
00210   std::string str = (m_edi_h1 -> displayText()).toStdString();
00211   //std::cout << "WdgColorTable::onEdiH1()  H1: " << str << " i=" << string_to_int(str) << std::endl;
00212   
00213   m_h1 = (float)string_to_int(str);
00214   emit hueAngleIsEdited(1);    
00215   setColorBar(m_h1, m_h2, m_cbar_width, m_figsize);
00216   this->onHueAngleIsChanged();
00217 }
00218 
00219 //--------------------------
00220 
00221 void 
00222 WdgColorTable::onEdiH2()
00223 {
00224   std::string str = (m_edi_h2 -> displayText()).toStdString();
00225   //std::cout << "WdgColorTable::onEdiH2()  H2: " << str << " i=" << string_to_int(str) << std::endl;
00226 
00227   m_h2 = (float)string_to_int(str);
00228   emit hueAngleIsEdited(2);
00229   setColorBar(m_h1, m_h2, m_cbar_width, m_figsize);
00230   this->onHueAngleIsChanged();
00231 }
00232 
00233 //--------------------------
00234 
00235 void 
00236 WdgColorTable::onSetH(const unsigned& selected)
00237 {
00238   //std::cout << "WdgColorTable::onSetH() h1:" << m_h1 << " h2:" << m_h2 
00239   //          << " selected:" << selected << '\n';
00240 
00241   if(selected == 1) m_edi_h1 -> setText(QString(val_to_string<int>(int(m_h1)).c_str()));
00242   if(selected == 2) m_edi_h2 -> setText(QString(val_to_string<int>(int(m_h2)).c_str()));
00243 
00244   setColorBar(m_h1, m_h2, m_cbar_width, m_figsize);
00245 }
00246 //--------------------------
00247 
00248 void 
00249 WdgColorTable::onButApply()
00250 {
00251   std::stringstream ss; ss << "Emit signal with Hue angles h1:" << m_h1 << " h2:" << m_h2;
00252   MsgInLog(_name_(), INFO, ss.str());  
00253   emit hueAnglesUpdated(m_h1, m_h2);
00254 }
00255 
00256 //--------------------------
00257 
00258 void 
00259 WdgColorTable::onHueAngleIsChanged()
00260 {
00261   //std::stringstream ss; ss << "Emit signal with Hue angles h1:" << m_h1 << " h2:" << m_h2;
00262   //MsgInLog(_name_(), DEBUG, ss.str());  
00263   //emit hueAnglesUpdated(m_h1, m_h2);
00264 }
00265 
00266 //--------------------------
00267 
00268 void 
00269 WdgColorTable::onButExit()
00270 {
00271   //std::cout << "WdgColorTable::onButExit()\n";
00272   this->close(); // will call closeEvent(...)
00273 }
00274 
00275 //--------------------------
00276 
00277 void
00278 WdgColorTable::setColorBar( const float&    hue1,
00279                             const float&    hue2,
00280                             const unsigned& rows,
00281                             const unsigned& cols
00282                            )
00283 {
00284   //std::cout << "WdgImage::setColorBar()\n";
00285   uint32_t* ctable = ColorTable(cols, hue1, hue2);
00286   uint32_t dimg[rows][cols];
00287 
00288   for(unsigned r=0; r<rows; ++r) {
00289     std::memcpy(&dimg[r][0], &ctable[0], cols*sizeof(uint32_t));
00290   }
00291 
00292   QImage image((const uchar*) &dimg[0], cols, rows, QImage::Format_ARGB32);
00293   setPixmapForLabel(image, m_pixmap_cbar, m_lab_cbar);
00294 
00295 }
00296 
00297 //--------------------------
00298 
00299 ndarray<uint32_t,1> 
00300 WdgColorTable::getColorTableAsNDArray(const unsigned& colors)
00301 {
00302   uint32_t* ctable = ColorTable(colors, m_h1, m_h2);
00303   unsigned shape[] = {colors}; ndarray<uint32_t,1> nda(shape);
00304   //ndarray<uint32_t,1> nda = make_ndarray<uint32_t>(colors);
00305   std::memcpy(nda.data(), ctable, colors*sizeof(uint32_t)); 
00306   //for(unsigned i=0; i<colors; ++i) nda[i] = ctable[i];
00307   return nda;
00308 }
00309 
00310 //--------------------------
00311 void 
00312 WdgColorTable::testSignalHueAnglesUpdated(const float& h1, const float& h2)
00313 {
00314   std::stringstream ss; ss << ":testSignalHueAnglesUpdated h1:" << m_h1 << " h2:" << m_h2;
00315   MsgInLog(_name_(), DEBUG, ss.str());  
00316 }
00317 
00318 //--------------------------
00319 
00320 } // namespace PSQt
00321 
00322 //--------------------------

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7