PSQt/src/WdgDirTree.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------
00002 // File and Version Information:
00003 //   $Id: WdgDirTree.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/WdgDirTree.h"
00011 #include "PSQt/QGUtils.h"
00012 #include "PSQt/Logger.h"
00013 
00014 //#include <string>
00015 //#include <fstream>   // ofstream
00016 //#include <iomanip>   // for setw, setfill
00017 //#include <math.h>
00018 //#include <stdio.h>
00019 
00020 #include <sstream>   // for stringstream
00021 #include <iostream>    // cout
00022 #include <fstream>    // ifstream(fname), ofstream
00023 
00024 #include <dirent.h> // for DIR, dirent
00025 
00026 //using namespace std; // for cout without std::
00027 
00028 namespace PSQt {
00029 
00030 //--------------------------
00031 
00032   WdgDirTree::WdgDirTree( QWidget *parent, const std::string& dir_top, const unsigned& pbits)
00033     : Frame(parent)
00034     , m_dir_top(dir_top)
00035     , m_pbits(pbits)
00036 //  : QWidget(parent)
00037 {
00038   m_model = 0;
00039 
00040   setGeometry(100, 100, 400, 300);
00041   setWindowTitle("Item selection tree");
00042  
00043   std::cout << "point 1\n";
00044 
00045   makeTreeModel();
00046 
00047   m_view = new QTreeView();  
00048   m_view->setModel(m_model);
00049   m_view->setAnimated(true);
00050 
00051   QVBoxLayout *vbox = new QVBoxLayout();
00052   vbox -> addWidget(m_view);
00053   //vbox -> addStretch(1);
00054   //vbox -> addLayout(hbox);
00055 
00056   this -> setLayout(vbox);
00057   this -> move(100,50);  
00058 
00059   //connect(m_but_exit, SIGNAL( clicked() ), this, SLOT(onButExit()) );
00060   //connect(m_but_test, SIGNAL( clicked() ), m_image, SLOT(onTest()) );
00061   //connect(m_file, SIGNAL(fileNameIsChanged(const std::string&)), m_image, SLOT(onFileNameChanged(const std::string&)) ); 
00062   m_view->expandAll();
00063   showTips();
00064 }
00065 
00066 //--------------------------
00067 
00068 void
00069 WdgDirTree::showTips() 
00070 {
00071   //m_file_geo  -> setToolTip("Select \"geometry\" file");
00072   //m_file_geo  -> setToolTip("Select ndarray with image file");
00073   //m_but_exit  -> setToolTip("Exit application");
00074 }
00075 
00076 //--------------------------
00077 
00078 void 
00079 WdgDirTree::makeTreeModel()
00080 {
00081   if ( m_model == 0 ) {
00082     m_model = new QStandardItemModel();
00083     //m_model->setHorizontalHeaderLabels(QStringList(QString('o')));
00084     updateTreeModel(m_dir_top);
00085   }
00086 }
00087 
00088 //--------------------------
00089 
00090 void 
00091 WdgDirTree::updateTreeModel(const std::string& dir_top)
00092 {
00093     if (dir_top.empty()) return;
00094 
00095     m_model->clear();
00096 
00097     fillTreeModel(dir_top,0,0,m_pbits);
00098     //fillTreeModelTest();
00099 }
00100 
00101 //--------------------------
00102 
00103 void 
00104 WdgDirTree::fillTreeModel(const std::string& path0, QStandardItem* pitem, const unsigned& level, const unsigned& pbits)
00105 {
00106    std::string bname = basename(path0); 
00107    std::string dname = dirname(path0); 
00108    std::string path = (dname.empty()) ? "." : dname;
00109    if (!bname.empty()) path += '/' + bname;
00110 
00111    if (bname.empty()) bname = ".";
00112    else if (bname==std::string("./")) bname = ".";
00113 
00114    QStandardItem* item_add = new QStandardItem(QString(bname.c_str()));
00115    QStandardItem* item_parent = (pitem) ? pitem : m_model->invisibleRootItem();
00116 
00117    item_parent->appendRow(item_add);
00118 
00119    if(pbits & 1) std::cout << "==== Add item: \"" << path << "\"\n";
00120       
00121    if (is_link(path)) {
00122      if(pbits & 2) std::cout << "    add as a link\n";
00123      return;
00124    }
00125 
00126    if (is_file(path)) {
00127        if(pbits & 2) std::cout << "    add as a file\n";
00128        //item_add->setIcon(icon);
00129        item_add->setCheckable(true);
00130        return;
00131    }
00132 
00133    else if (is_directory(path)) {
00134        if(pbits & 2) std::cout << "    add as a directory\n";
00135 
00136        DIR*     dir;
00137        dirent*  pdir; 
00138        dir = opendir(path.c_str());      // open current directory
00139        while ((pdir = readdir(dir))) {
00140           std::string fname(pdir->d_name);
00141 
00142           if(pbits & 2) cout << "    " << fname << '\n';
00143 
00144           if(fname==std::string(".")) continue;
00145           if(fname==std::string("..")) continue;
00146           if(fname.empty()) continue;
00147 
00148           std::string path_to_child = path; path_to_child += '/' + fname;
00149           if(pbits & 2) cout << "    path_to_child: " << path_to_child << '\n'; 
00150 
00151 
00152           fillTreeModel(path_to_child, item_add, level+1, pbits);
00153        }
00154        closedir(dir);
00155    }
00156 
00157    //std::cout << "WdgDirTree::fillTreeModel(...) !!!!!!!!!!! TBD\n";
00158 }
00159 
00160 
00161 //--------------------------
00162 
00163 void 
00164 WdgDirTree::fillTreeModelTest()
00165 {
00166    QStandardItem* item_parent = m_model->invisibleRootItem();
00167 
00168    QStandardItem* item1 = new QStandardItem(QString("item1"));
00169    QStandardItem* item2 = new QStandardItem(QString("item2"));
00170    QStandardItem* item3 = new QStandardItem(QString("item3"));
00171 
00172    item_parent->appendRow(item1);
00173    item_parent->appendRow(item2);
00174    item_parent->appendRow(item3);
00175 
00176    QStandardItem* item31 = new QStandardItem(QString("item31"));
00177    QStandardItem* item32 = new QStandardItem(QString("item32"));
00178 
00179    item3->appendRow(item31);
00180    item3->appendRow(item32);
00181 }
00182 
00183 //--------------------------
00184 
00185 void 
00186 WdgDirTree::resizeEvent(QResizeEvent *event)
00187 {
00188   //  m_frame->setGeometry(0, 0, event->size().width(), event->size().height());
00189   stringstream ss; ss << "Window is resized, w:" << event->size().width() << " h:" <<  event->size().height();
00190   setWindowTitle(ss.str().c_str());
00191 }
00192 
00193 //--------------------------
00194 
00195 void 
00196 WdgDirTree::closeEvent(QCloseEvent *event)
00197 {
00198   QWidget::closeEvent(event);
00199   std::stringstream ss; ss << "closeEvent(...): type = " << event -> type();
00200   MsgInLog(_name_(), INFO, ss.str());
00201 }
00202 
00203 //--------------------------
00204 void
00205 WdgDirTree::moveEvent(QMoveEvent *event)
00206 {
00207   int x = event->pos().x();
00208   int y = event->pos().y();
00209   QString text = QString::number(x) + "," + QString::number(y);
00210   setWindowTitle(text);
00211 }
00212 
00213 //--------------------------
00214 
00215 void 
00216 WdgDirTree::mousePressEvent(QMouseEvent *event)
00217 {
00218   //int x = event->pos().x();
00219   //int y = event->pos().y();
00220   //QString text = "mousePressEvent: " + QString::number(x) + "," + QString::number(y);
00221   //std::cout << text.toStdString()  << std::endl;
00222 }
00223 
00224 //--------------------------
00225 //--------------------------
00226 //--------------------------
00227 
00228 void 
00229 WdgDirTree::onButExit()
00230 {
00231   std::cout << "WdgDirTree::onButExit()\n";
00232   this->close(); // will call closeEvent(...)
00233 }
00234 
00235 //--------------------------
00236 
00237 } // namespace PSQt
00238 
00239 //--------------------------

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7