PSQt/src/WdgFile.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------
00002 // File and Version Information:
00003 //   $Id: WdgFile.cpp 10660 2015-09-09 23:09:24Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Author: Mikhail S. Dubrovin
00006 //---------------------------------------------------------------------
00007 
00008 //--------------------------
00009 
00010 #include "PSQt/WdgFile.h"
00011 #include "PSQt/Logger.h"
00012 
00013 #include <iostream>    // for std::cout
00014 #include <fstream>    // for std::ifstream(fname)
00015 //using namespace std; // for cout without std::
00016 
00017 namespace PSQt {
00018 
00019 //--------------------------
00020 
00021 WdgFile::WdgFile( QWidget *parent, 
00022                   const std::string& but_title, 
00023                   const std::string& path,
00024                   const std::string& search_fmt,
00025                   const bool& show_frame,
00026                   const unsigned& but_width )
00027     : QWidget(parent)
00028     , m_path(path)
00029     , m_search_fmt(search_fmt)
00030     , m_show_frame(show_frame)
00031 {
00032   this -> setFrame();
00033 
00034   //setPalette ( QPalette(QColor(255, 255, 255, 255)) );
00035   //setAutoFillBackground (true);  
00036 
00037   m_but_file = new QPushButton(but_title.c_str(), this);
00038   m_edi_file = new QLineEdit  (m_path.c_str());
00039 
00040   m_but_file -> setFixedWidth(but_width);
00041   m_but_file -> setCursor(Qt::PointingHandCursor); 
00042 
00043   connect( m_but_file, SIGNAL( clicked() ),          this, SLOT( onButFile()) ); 
00044   connect( m_edi_file, SIGNAL( editingFinished() ),  this, SLOT( onEdiFile()) ); 
00045   //connect( m_edi_file, SIGNAL( textChanged() ),  this, SLOT( onEdiFile()) ); 
00046   //connect( this, SIGNAL(fileNameIsChanged(const std::string&)), this, SLOT(testSignalString(const std::string&)) ); 
00047  
00048   QHBoxLayout *hbox = new QHBoxLayout;
00049   hbox -> addWidget(m_but_file);
00050   hbox -> addWidget(m_edi_file);
00051   this -> setLayout(hbox);
00052 
00053   this -> setWindowTitle(tr("WdgFile"));
00054   this -> setMinimumWidth(200);
00055   this -> setFixedHeight( (m_show_frame)? 50 : 34);
00056 
00057   if (! m_show_frame) this -> setContentsMargins(-9,-9,-9,-9);
00058 
00059   //this -> move(300,50); // open qt window in specified position
00060 
00061   this -> showTips();
00062 }
00063 
00064 //--------------------------
00065 
00066 void
00067 WdgFile::showTips() 
00068 {
00069   m_but_file -> setToolTip("Click and select the file");
00070   m_edi_file -> setToolTip("File name field");
00071 }
00072 
00073 //--------------------------
00074 
00075 void
00076 WdgFile::setFrame() 
00077 {
00078   m_frame = new QFrame(this);
00079   m_frame -> setFrameStyle ( QFrame::Box | QFrame::Sunken); // or
00080   //m_frame -> setFrameStyle ( QFrame::Box );    // NoFrame, Box, Panel, WinPanel, ..., StyledPanel 
00081   //m_frame -> setFrameShadow( QFrame::Sunken ); // Plain, Sunken, Raised 
00082   m_frame -> setLineWidth(0);
00083   m_frame -> setMidLineWidth(1);
00084   m_frame -> setCursor(Qt::SizeAllCursor);     // Qt::WaitCursor, Qt::PointingHandCursor
00085   //m_frame -> setStyleSheet("background-color: rgb(0, 255, 255); color: rgb(255, 255, 100)");
00086   m_frame -> setVisible(m_show_frame);
00087 }
00088 
00089 //--------------------------
00090 
00091 void 
00092 WdgFile::resizeEvent(QResizeEvent *event)
00093 {
00094   m_frame->setGeometry(0, 0, event->size().width(), event->size().height());
00095   //std::cout << "WdgFile::resizeEvent(...): w=" << event->size().width() 
00096   //          << "  h=" << event->size().height() << '\n';
00097   setWindowTitle("Window is resized");
00098 }
00099 
00100 //--------------------------
00101 
00102 void 
00103 WdgFile::closeEvent(QCloseEvent *event)
00104 {
00105   QWidget::closeEvent(event);
00106   stringstream ss; ss << "closeEvent(...): type = " << event -> type();
00107   MsgInLog(_name_(), INFO, ss.str());
00108 }
00109 
00110 //--------------------------
00111 //--------------------------
00112 //--------------------------
00113 //--------------------------
00114 
00115 void 
00116 WdgFile::onEdiFile()
00117 {
00118   MsgInLog(_name_(), DEBUG, "onEdiFile");
00119   std::string str_fname = (m_edi_file -> displayText()).toStdString();
00120   MsgInLog(_name_(), INFO, "Edited file name: " + str_fname );
00121 
00122   setNewFileName(str_fname);
00123 }
00124 
00125 //--------------------------
00126 
00127 void 
00128 WdgFile::onButFile()
00129 {
00130   MsgInLog(_name_(), DEBUG, "onButFile");
00131 
00132   std::string str_path_file_edi = (m_edi_file -> displayText()).toStdString();
00133 
00134   /*
00135   //dname =  "/reg/d/psdm/CXI/cxi49012/xtc/";
00136   if (str_path_file_edi.empty()) str_path_file_edi = "./";
00137 
00138   size_t pos = str_path_file_edi.rfind('/');
00139   pos = (pos != std::string::npos) ? pos : str_path_file_edi.size();
00140 
00141   std::string dname(str_path_file_edi, 0, pos );
00142   std::string fname(str_path_file_edi, pos);
00143 
00144   MsgInLog(_name_(), DEBUG, "dname: " + dname );
00145   MsgInLog(_name_(), DEBUG, "fname: " + fname );
00146   
00147   QString path_file = QFileDialog::getOpenFileName(this, tr("Select input file"), 
00148                                                    QString(dname.c_str()), tr(m_search_fmt.c_str()));
00149   */
00150 
00151   QString path_file = QFileDialog::getOpenFileName(this, tr("Select input file"), 
00152                                                    QString(str_path_file_edi.c_str()), tr(m_search_fmt.c_str()));
00153 
00154   std::string str_path_file = path_file.toStdString();
00155 
00156   if(str_path_file.empty()) {
00157     MsgInLog(_name_(), INFO, "Cancel file selection");
00158     return;
00159   }
00160 
00161   MsgInLog(_name_(), INFO, "Selected file name: " + str_path_file);
00162 
00163   if(setNewFileName(str_path_file)) m_edi_file -> setText(path_file);
00164 }
00165 
00166 //--------------------------
00167 
00168 bool 
00169 WdgFile::setNewFileName(const std::string& fname)
00170 {
00171   if(fname == m_path) {
00172     MsgInLog(_name_(), INFO, "File name has not been changed");
00173     return false; // if the file name has not been changed
00174   }
00175 
00176   if( fileExists(fname) ) {
00177     MsgInLog(_name_(), DEBUG, "Emit signal fileNameIsChanged(fname), fname: " + fname);
00178     emit fileNameIsChanged(fname);
00179     m_path = fname;
00180     return true;
00181   }
00182   return false;
00183 }
00184 
00185 //--------------------------
00186 
00187 bool
00188 WdgFile::fileExists(const std::string& fname)
00189 {
00190   std::ifstream f(fname.c_str());
00191   if(f.good()) {
00192     MsgInLog(_name_(), DEBUG, "Selected file exists");
00193     return true;
00194   } 
00195 
00196   MsgInLog(_name_(), WARNING, "Selected file DOES NOT exists, try to select other file.");
00197   return false;
00198 }
00199 
00200 //--------------------------
00201 
00202 void 
00203 WdgFile::testSignalString(const std::string& fname)
00204 {
00205   MsgInLog(_name_(), DEBUG, "Received signal in testSignalSlot(string), fname: " + fname);
00206 }
00207 
00208 //--------------------------
00209 //--------------------------
00210 //--------------------------
00211 
00212 } // namespace PSQt
00213 
00214 //--------------------------

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7