00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "PSQt/WdgFile.h"
00011 #include "PSQt/Logger.h"
00012
00013 #include <iostream>
00014 #include <fstream>
00015
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
00035
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
00046
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
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);
00080
00081
00082 m_frame -> setLineWidth(0);
00083 m_frame -> setMidLineWidth(1);
00084 m_frame -> setCursor(Qt::SizeAllCursor);
00085
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
00096
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
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
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;
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 }
00213
00214