PSQt/src/QGUtils.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------
00002 // File and Version Information:
00003 //   $Id: QGUtils.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/QGUtils.h"
00011 
00012 //#include "PSQt/WdgImage.h"
00013 //#include "ndarray/ndarray.h" // for img_from_pixel_arrays(...)
00014 
00015 #include <cstring>    // for memcpy
00016 #include <iostream>   // for std::cout
00017 #include <fstream>    // for ifstream
00018 #include <sys/stat.h>
00019 
00020 using namespace std;  // for cout without std::
00021 
00022 namespace PSQt {
00023 
00024 
00025 //--------------------------
00026 
00027 uint32_t
00028 fRGBA(const float R, const float G, const float B) // R, G, B = [0,1]
00029 {
00030   unsigned r = unsigned(R*255);
00031   unsigned g = unsigned(G*255);
00032   unsigned b = unsigned(B*255);
00033   return uint32_t(0xFF000000 + (r<<16) + (g<<8) + b);  
00034 }
00035 
00036 //--------------------------
00037 
00038 uint32_t 
00039 HSV2RGBA(const float H, const float S, const float V) // H=[0,360), S, V = [0,1]
00040 {
00041   // extend hue from [0,+360] to any angle in cycle
00042   float HE = fmod(H,360);
00043 
00044   float fhr = (HE<0) ? (HE+360)/60 : HE/60 ; 
00045   int   ihr = int(fhr); // [0,5]
00046   float rem = fhr - ihr;
00047 
00048   float p = V * (1 - S);
00049   float q = V * (1 - (S * rem));
00050   float t = V * (1 - (S * (1 - rem)));
00051 
00052   switch(ihr) {
00053     case  0: return fRGBA(V,t,p);
00054     case  1: return fRGBA(q,V,p);
00055     case  2: return fRGBA(p,V,t);
00056     case  3: return fRGBA(p,q,V);
00057     case  4: return fRGBA(t,p,V);
00058     case  5:
00059     default: return fRGBA(V,p,q);
00060   }
00061 }
00062 
00063 //--------------------------
00064 
00065 uint32_t*
00066 ColorTable(const unsigned& NColors, const float& H1, const float& H2) // H1, H2=[-360,360]
00067 {
00068   // Set default or input parameters for color table
00069   unsigned NCols = (NColors) ? NColors : 1024;
00070   float H1L = (H1 || H2) ? H1 : -120;
00071   float H2L = (H1 || H2) ? H2 :   60;
00072 
00073   uint32_t* ctable = new uint32_t[NCols];
00074 
00075   const float S=1; 
00076   const float V=1;
00077   const float dH = (H2L-H1L)/NCols;
00078   float H=H1L;
00079   for(unsigned i=0; i<NCols; ++i, H+=dH) {
00080     ctable[i] = HSV2RGBA(H, S, V);
00081   }
00082   return ctable;
00083 }
00084 
00085 //--------------------------
00086 
00087 ndarray<uint32_t,2>
00088 getColorBarImage( const unsigned& rows, 
00089                   const unsigned& cols,
00090                   const float&    hue1,
00091                   const float&    hue2
00092                 )
00093 {
00094   MsgInLog("QGUtils", DEBUG, "getColorBarImage()");
00095   uint32_t* colors = ColorTable(cols, hue1, hue2);
00096 
00097   unsigned int shape[2] = {rows, cols};
00098   ndarray<uint32_t,2> img_nda(shape);
00099 
00100   for(unsigned r=0; r<rows; ++r) {
00101     std::memcpy(&img_nda[r][0], &colors[0], cols*sizeof(uint32_t));
00102   }
00103 
00104   delete[] colors;
00105 
00106   return img_nda;
00107 }
00108 
00109 //--------------------------
00110 
00111 void 
00112 setPixmapForLabel(const QImage& image, QPixmap*& pixmap, QLabel*& label)
00113 {
00114   if(pixmap) delete pixmap;
00115   pixmap = new QPixmap(QPixmap::fromImage(image));
00116   //else pixmap -> loadFromData ( (const uchar*) &dimg[0], unsigned(rows*cols) );
00117 
00118   label->setPixmap(pixmap->scaled(label->size(), Qt::KeepAspectRatio, Qt::FastTransformation));
00119   //label->setPixmap(*pixmap);
00120 }
00121 
00122 //--------------------------
00123 
00124 int string_to_int(const std::string& str)
00125 {
00126   return atoi( str.c_str() );
00127 }
00128 
00129 //--------------------------
00130 
00131 void print_font_status(const QFont& font)
00132 {
00133   //QFont font(txtitem->font());
00134   std::cout << "Font family    : " << font.family().toStdString() << '\n';
00135   std::cout << "Font styleName : " << font.styleName().toStdString() << '\n';
00136   std::cout << "Font styleHint : " << font.styleHint() << '\n';
00137   std::cout << "Font style     : " << font.style() << '\n';
00138   std::cout << "Font weight    : " << font.weight() << '\n';
00139   std::cout << "Font stretch   : " << font.stretch() << '\n';
00140   std::cout << "Font bold      : " << font.bold() << '\n';
00141   std::cout << "Font kerning   : " << font.kerning() << '\n';
00142   std::cout << "Font rawMode   : " << font.rawMode() << '\n';
00143   std::cout << "Font rawName   : " << font.rawName().toStdString() << '\n';
00144   std::cout << "Font pointSize : " << font.pointSize() << '\n';
00145   std::cout << "Font pointSizeF: " << font.pointSizeF() << '\n';
00146   std::cout << "Font pixelSize : " << font.pixelSize() << '\n';
00147   std::cout << "Font strikeOut : " << font.strikeOut() << '\n';
00148 }  
00149 
00150 //--------------------------
00151 
00152 void set_pen(QPen & pen, const std::string & opt)
00153 {
00154   //QPen pen(Qt::blue,  2, Qt::SolidLine);
00155   //pen.setCosmetic(true);
00156   //pen.setStyle (Qt::SolidLine) // Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine, Qt::NoPen   
00157 
00158     if     ( opt.find('-') != std::string::npos ) pen.setStyle (Qt::SolidLine);
00159     else if( opt.find('.') != std::string::npos ) pen.setStyle (Qt::DotLine);
00160     else if( opt.find('!') != std::string::npos ) pen.setStyle (Qt::DashDotLine);
00161 
00162     if     ( opt.find('k') != std::string::npos ) pen.setColor(Qt::black);
00163     else if( opt.find('b') != std::string::npos ) pen.setColor(Qt::blue);
00164     else if( opt.find('r') != std::string::npos ) pen.setColor(Qt::red);
00165     else if( opt.find('g') != std::string::npos ) pen.setColor(Qt::green);
00166     else if( opt.find('y') != std::string::npos ) pen.setColor(Qt::yellow);
00167 
00168     if     ( opt.find('1') != std::string::npos ) pen.setWidth(1);
00169     else if( opt.find('2') != std::string::npos ) pen.setWidth(2);
00170     else if( opt.find('3') != std::string::npos ) pen.setWidth(3);
00171     else if( opt.find('4') != std::string::npos ) pen.setWidth(4);
00172     else if( opt.find('5') != std::string::npos ) pen.setWidth(5);
00173 }
00174 //--------------------------
00175 
00176 void set_brash(QBrush & brush, const std::string & opt)
00177 {
00178   //QBrush brush(Qt::transparent, Qt::SolidPattern);  
00179   //brush.setStyle (Qt::SolidPattern);
00180 
00181     if     ( opt.find('T') != std::string::npos ) brush.setColor(Qt::transparent);
00182     else if( opt.find('K') != std::string::npos ) brush.setColor(Qt::black);
00183     else if( opt.find('B') != std::string::npos ) brush.setColor(Qt::blue);
00184     else if( opt.find('R') != std::string::npos ) brush.setColor(Qt::red);
00185     else if( opt.find('G') != std::string::npos ) brush.setColor(Qt::green);
00186     else if( opt.find('Y') != std::string::npos ) brush.setColor(Qt::yellow);
00187 }
00188 
00189 //--------------------------
00190 
00191 void set_pen_brush(QPen & pen, QBrush & brush, const std::string & opt)
00192 {
00193   if(opt.empty()) return;
00194   set_pen(pen, opt);
00195   set_brash(brush, opt);
00196 }
00197 
00198 //--------------------------
00199 
00200 QGraphicsPathItem*      
00201 graph(QGraphicsScene* scene, const float* x, const float* y, const int n, const std::string& opt)
00202 {
00203   //std::cout << "QGraphicsScene\n";
00204   //QPen pen;
00205   QPen   pen  (Qt::blue, 2, Qt::SolidLine);
00206   QBrush brush(Qt::transparent, Qt::SolidPattern);
00207   set_pen_brush(pen, brush, opt);
00208   pen.setCosmetic(true);
00209 
00210   QPainterPath path;     path.moveTo(x[0],y[0]);
00211   for(int i=1; i<n; ++i) path.lineTo(x[i],y[i]);
00212   return scene->addPath(path, pen, brush);
00213 }
00214 
00215 //--------------------------
00216 
00217 QGraphicsPathItem*      
00218 graph(QGraphicsView* view, const float* x, const float* y, const int n, const std::string& opt)
00219 {
00220   return graph(view->scene(), x, y, n, opt);
00221 }
00222 
00223 //--------------------------
00224 
00225 QGraphicsPathItem*      
00226 hist(QGraphicsScene* scene, const float* x, const float* y, const int n, const std::string& opt)
00227 {
00228   //std::cout << "QGraphicsScene\n";
00229   //QPen pen;
00230   QPen   pen  (Qt::blue, 2, Qt::SolidLine);
00231   QBrush brush(Qt::transparent, Qt::SolidPattern);
00232   set_pen_brush(pen, brush, opt);
00233   pen.setCosmetic(true);
00234 
00235   QRectF sr = scene->sceneRect();
00236   float ymin = sr.top();
00237   /*
00238   float ymax = sr.bottom();
00239   float xmin = sr.left();
00240   float xmax = sr.right();
00241 
00242   std::cout << "  scene:" 
00243             << "  xmin:"  << xmin
00244             << "  xmax:"  << xmax
00245             << "  ymin:"  << ymin
00246             << "  ymax:"  << ymax
00247             << '\n';
00248   */
00249 
00250   QPainterPath path; 
00251       path.moveTo(x[0],ymin);
00252       path.lineTo(x[0],y[0]);
00253   for(int i=1; i<n; ++i) {
00254       path.lineTo(x[i],y[i-1]);
00255       path.lineTo(x[i],y[i]);
00256   }
00257       path.lineTo(x[n-1],ymin);
00258       //path.lineTo(x[n],y[n-1]);
00259       //path.lineTo(x[n],ymin);
00260   return scene->addPath(path, pen, brush);
00261 }
00262 
00263 //--------------------------
00264 
00265 QGraphicsPathItem*      
00266 hist(QGraphicsView* view, const float* x, const float* y, const int n, const std::string& opt)
00267 {
00268   return hist(view->scene(), x, y, n, opt);
00269 }
00270 
00271 //--------------------------
00272 
00273 bool is_directory(const std::string& path)
00274 {
00275   struct stat st;
00276   if(stat(path.c_str(),&st) == 0) 
00277     if ((st.st_mode & S_IFMT) == S_IFDIR) return true;
00278   return false;
00279 }
00280 
00281 //--------------------------
00282 
00283 bool is_link(const std::string& path)
00284 {
00285   struct stat st;
00286   if(stat(path.c_str(),&st) == 0) 
00287     if ((st.st_mode & S_IFMT) == S_IFLNK) return true;
00288   return false;
00289 }
00290 
00291 //--------------------------
00292 
00293 bool is_file(const std::string& path)
00294 {
00295   struct stat st;
00296   if(stat(path.c_str(),&st) == 0) 
00297     if ((st.st_mode & S_IFMT) == S_IFREG) return true;
00298   return false;
00299 }
00300 
00301 //--------------------------
00302 
00303 bool dir_exists(const std::string& dir)
00304 {    
00305   return is_directory(dir);
00306 }
00307 
00308 //--------------------------
00309 
00310 bool path_exists(const std::string& path) {
00311   struct stat st;   
00312   return (stat(path.c_str(), &st) == 0); 
00313 }
00314 
00315 
00316 //--------------------------
00317 
00318 
00319 bool file_exists(const std::string& fname)
00320 {
00321     std::ifstream f(fname.c_str());
00322     return (f.good())? true : false;
00323 }
00324 
00325 
00326 //--------------------------
00327 
00328 std::string
00329 split_string_left(const std::string& s, size_t& pos, const char& sep)
00330 {
00331   size_t p0 = pos;
00332   size_t p1 = s.find(sep, p0);
00333   size_t nchars = p1-p0; 
00334   pos = p1+1; // move position to the next character after separator
00335 
00336   if (p1 != std::string::npos) return std::string(s,p0,nchars);
00337   else if (p0 < s.size())      return std::string(s,p0);
00338   else                         return std::string();
00339 }
00340 
00341 //--------------------------
00342 // for input path = <path>/<fname> returns <fname>
00343 std::string basename(const std::string& path)
00344 {
00345   if (path.empty()) return path;
00346   size_t pos = path.find_last_of('/');
00347   if (pos == std::string::npos) return path;
00348   else if (pos < path.size()-1) return std::string(path,pos+1);
00349   else return std::string();
00350 }
00351 
00352 //--------------------------
00353 // for input path = <path>/<fname> returns <path>
00354 std::string dirname(const std::string& path)
00355 {
00356   if (path.empty()) return path;
00357   if (path==std::string(".")) return path;
00358   if (path==std::string("..")) return path;
00359 
00360   size_t pos = path.find_last_of('/');
00361   if (pos == std::string::npos) return std::string(); // assume that path is a file name
00362   else return std::string(path,0,pos);
00363 }
00364 
00365 //--------------------------
00366 // for input path = "<path>/<fname>.<ext>" returns root="<path>/<fname>" and ext=".<ext>"
00367 void splitext(const std::string& path, std::string& root, std::string& ext)
00368 {
00369   if (path.empty()) {
00370     root = std::string();
00371     ext  = std::string();
00372     return;
00373   }
00374 
00375   if(path==std::string(".")) {
00376     root = path;
00377     ext  = std::string();
00378     return;
00379   }
00380 
00381   size_t pos = path.find_last_of('.');
00382 
00383   if (pos == std::string::npos) {
00384     root = path;
00385     ext  = std::string();
00386     return;
00387   }
00388   else {
00389     root = std::string(path,0,pos);
00390     ext  = std::string(path,pos);
00391     return;
00392   }
00393 }
00394 
00395 //--------------------------
00396 
00397 std::string stringTimeStamp(const std::string& format)
00398 {
00399   time_t  time_sec;
00400   time ( &time_sec );
00401   struct tm* timeinfo; timeinfo = localtime ( &time_sec );
00402   char c_time_buf[32]; strftime(c_time_buf, 32, format.c_str(), timeinfo);
00403   return std::string (c_time_buf);
00404 }
00405 
00406 //--------------------------
00407 
00408 std::string
00409 getFileNameWithTStamp(const std::string& fname)
00410 {
00411   std::string ofname = (fname==std::string()) ? "file.txt" : fname;
00412   std::string root, ext;
00413   splitext(ofname, root, ext);
00414   return root + '-' + strTimeStamp() + ext; 
00415 }
00416 
00417 //--------------------------
00418 
00419 std::string
00420 getGeometryFileName(const std::string& fname, const bool& add_tstamp)
00421 {
00422   return (add_tstamp) ? getFileNameWithTStamp(fname) : fname;  
00423 }
00424 
00425 //--------------------------
00426 
00427 } // namespace PSQt
00428 
00429 //--------------------------

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7