Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/projectView.cpp
Go to the documentation of this file.
00001 /*
00002  * ProjectView.cpp - implementation of project model
00003  *   the model manage the files in project directory
00004  *
00005  * Copyright (C) 2014, Yodalee, lc85301@gmail.com
00006  *
00007  * This file is part of Qucs
00008  *
00009  * Qucs is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013  *
00014  * This software is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with Qucs.  If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  */
00023 
00024 #include "projectView.h"
00025 #include "schematic.h"
00026 
00027 #include <QString>
00028 #include <QStringList>
00029 #include <QDir>
00030 #include <QStandardItemModel>
00031 
00032 ProjectView::ProjectView(QWidget *parent)
00033   : QTreeView(parent)
00034 {
00035   m_projPath = QString();
00036   m_projPath = QString();
00037   m_valid = false;
00038   m_model = new QStandardItemModel(8, 2, this);
00039 
00040   refresh();
00041 
00042   this->setModel(m_model);
00043   this->setEditTriggers(QAbstractItemView::NoEditTriggers);
00044 }
00045 
00046 ProjectView::~ProjectView()
00047 {
00048   delete m_model;
00049 }
00050 
00051 void
00052 ProjectView::setProjPath(const QString &path)
00053 {
00054   m_valid = !path.isEmpty() && QDir(path).exists();
00055 
00056   if (m_valid) {
00057     //test path exist
00058     m_projPath = path;
00059     m_projName = path;
00060 
00061     // cut off trailing '/'
00062     if (m_projName.endsWith(QDir::separator())) {
00063       m_projName.chop(1);
00064     }
00065     int i = m_projName.lastIndexOf(QDir::separator());
00066     if(i > 0) {
00067       m_projName = m_projName.mid(i+1);   // cut out the last subdirectory
00068     }
00069     m_projName.remove("_prj");
00070   }
00071   refresh();
00072 }
00073 
00074 // refresh using projectPath
00075 void
00076 ProjectView::refresh()
00077 {
00078   m_model->clear();
00079 
00080   QStringList header;
00081   header << tr("Content of %1").arg(m_projName) << tr("Note");
00082   m_model->setHorizontalHeaderLabels(header);
00083 
00084   APPEND_ROW(m_model, tr("Datasets")     );
00085   APPEND_ROW(m_model, tr("Data Displays"));
00086   APPEND_ROW(m_model, tr("Verilog")      );
00087   APPEND_ROW(m_model, tr("Verilog-A")    );
00088   APPEND_ROW(m_model, tr("VHDL")         );
00089   APPEND_ROW(m_model, tr("Octave")       );
00090   APPEND_ROW(m_model, tr("Schematics")   );
00091   APPEND_ROW(m_model, tr("Others")       );
00092 
00093   setExpanded(m_model->index(6, 0), true);
00094 
00095   if (!m_valid) {
00096     return;
00097   }
00098 
00099   // put all files into "Content"-ListView
00100   QDir workPath(m_projPath);
00101   QStringList files = workPath.entryList("*", QDir::Files, QDir::Name);
00102   QStringList::iterator it;
00103   QString extName, fileName;
00104   QList<QStandardItem *> columnData;
00105 
00106 #define APPEND_CHILD(category, data) \
00107   m_model->item(category, 0)->appendRow(data);
00108 
00109   for(it = files.begin(); it != files.end(); ++it) {
00110     fileName = (*it).toAscii();
00111     extName = QFileInfo(workPath.filePath(fileName)).suffix();
00112 
00113     columnData.clear();
00114     columnData.append(new QStandardItem(fileName));
00115 
00116     if(extName == "dat") {
00117       APPEND_CHILD(0, columnData);
00118     }
00119     else if(extName == "dpl") {
00120       APPEND_CHILD(1, columnData);
00121     }
00122     else if(extName == "v") {
00123       APPEND_CHILD(2, columnData);
00124     }
00125     else if(extName == "va") {
00126       APPEND_CHILD(3, columnData);
00127     }
00128     else if((extName == "vhdl") || (extName == "vhd")) {
00129       APPEND_CHILD(4, columnData);
00130     }
00131     else if((extName == "m") || (extName == "oct")) {
00132       APPEND_CHILD(5, columnData);
00133     }
00134     else if(extName == "sch") {
00135       int n = Schematic::testFile(workPath.filePath(fileName));
00136       if(n >= 0) {
00137         if(n > 0) {
00138           columnData.append(new QStandardItem(QString::number(n)+tr("-port")));
00139         }
00140       }
00141       APPEND_CHILD(6, columnData);
00142     }
00143     else {
00144       APPEND_CHILD(7, columnData);
00145     }
00146   }
00147 
00148   resizeColumnToContents(0);
00149 }
00150 
00151 QStringList
00152 ProjectView::exportSchematic()
00153 {
00154   QStringList list;
00155   QStandardItem *item = m_model->item(6, 0);
00156   for (int i = 0; i < item->rowCount(); ++i) {
00157     if (item->child(i,1)) {
00158       list.append(item->child(i,0)->text());
00159     }
00160   }
00161   return list;
00162 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines