Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/dialogs/loaddialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  * Copyright (C) 2014 Guilherme Brondani Torri <guitorri@gmail.com>        *
00003  *                                                                         *
00004  * Modified from SaveDialog and LibraryDialog                              *
00005  *                                                                         *
00006  * This is free software; you can redistribute it and/or modify            *
00007  * it under the terms of the GNU General Public License as published by    *
00008  * the Free Software Foundation; either version 2, or (at your option)     *
00009  * any later version.                                                      *
00010  *                                                                         *
00011  * This software is distributed in the hope that it will be useful,        *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
00014  * GNU General Public License for more details.                            *
00015  *                                                                         *
00016  * You should have received a copy of the GNU General Public License       *
00017  * along with this package; see the file COPYING.  If not, write to        *
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,   *
00019  * Boston, MA 02110-1301, USA.                                             *
00020  ***************************************************************************/
00021 
00022 #include <QVariant>
00023 #include <QLabel>
00024 #include <QPushButton>
00025 #include <QHBoxLayout>
00026 #include <QVBoxLayout>
00027 #include <QScrollArea>
00028 #include <QDebug>
00029 #include <QFileDialog>
00030 #include <QScriptEngine>
00031 #include <QScriptValue>
00032 #include <QScriptValueIterator>
00033 #include <QGroupBox>
00034 #include <QListWidget>
00035 #include <QListWidgetItem>
00036 #include <QEvent>
00037 #include <QKeyEvent>
00038 #include <QMessageBox>
00039 
00040 #include "loaddialog.h"
00041 #include "qucs.h"
00042 #include "qucsdoc.h"
00043 #include "components/components.h"
00044 
00045 LoadDialog::LoadDialog( QWidget* parent, const char* name, bool modal, Qt::WFlags fl )
00046    : QDialog( parent, name, modal, fl )
00047 {
00048    if ( !name )
00049       setWindowTitle( tr( "Load Verilog-A symbols" ) );
00050    app = 0l;
00051 //   initDialog();
00052 }
00053 
00054 LoadDialog::~LoadDialog()
00055 {
00056 }
00057 
00058 void LoadDialog::setApp(QucsApp *a)
00059 {
00060    app = a;
00061 }
00062 
00063 void LoadDialog::initDialog()
00064 {
00065    QVBoxLayout *all = new QVBoxLayout(this);
00066    all->setMargin(5);
00067    all->setSpacing(6);
00068 
00069    // hold group of files / group icon and checkboxes
00070    QHBoxLayout *hGroups = new QHBoxLayout();
00071    all->addLayout(hGroups);
00072 
00073    // ---
00074    QGroupBox *group1 = new QGroupBox( tr( "Choose Verilog-A symbol files:" ) );
00075    hGroups->addWidget(group1);
00076 
00077    QScrollArea *scrollArea = new QScrollArea(group1);
00078    scrollArea->setWidgetResizable(true);
00079 
00080    fileView = new QListWidget(this);
00081 
00082    scrollArea->setWidget(fileView);
00083 
00084    QVBoxLayout *areaLayout = new QVBoxLayout();
00085    areaLayout->addWidget(scrollArea);
00086    group1->setLayout(areaLayout);
00087 
00088 
00089    // ...........................................................
00090    QGridLayout *gridButts = new QGridLayout();
00091    all->addLayout(gridButts);
00092 
00093    ButtSelectAll = new QPushButton(tr("Select All"));
00094    gridButts->addWidget(ButtSelectAll, 0, 0);
00095    connect(ButtSelectAll, SIGNAL(clicked()), SLOT(slotSelectAll()));
00096    ButtSelectNone = new QPushButton(tr("Deselect All"));
00097    gridButts->addWidget(ButtSelectNone, 0, 1);
00098    connect(ButtSelectNone, SIGNAL(clicked()), SLOT(slotSelectNone()));
00099    // ...........................................................
00100    ButtCancel = new QPushButton(tr("Cancel"));
00101    gridButts->addWidget(ButtCancel, 1, 0);
00102    connect(ButtCancel, SIGNAL(clicked()), SLOT(reject()));
00103    ButtOk = new QPushButton(tr("Ok"));
00104    gridButts->addWidget(ButtOk, 1, 1);
00105    connect(ButtOk, SIGNAL(clicked()), SLOT(loadSelected()));
00106    ButtOk->setDefault(true);
00107 
00108 
00109    QVBoxLayout *iconLayout = new QVBoxLayout();
00110 
00111    QGroupBox *group2 = new QGroupBox( );
00112 
00113    QVBoxLayout *group2Layout = new QVBoxLayout();
00114 
00115    iconPixmap = new QLabel();
00116    iconPixmap->setSizePolicy(QSizePolicy::Expanding,
00117                              QSizePolicy::Expanding);
00118    iconPixmap->setAlignment(Qt::AlignCenter);
00119    group2Layout->addWidget(iconPixmap);
00120 
00121    group2->setLayout(group2Layout);
00122    iconLayout->addWidget(group2);
00123 
00124    ButtChangeIcon = new QPushButton(tr("Change Icon"));
00125    iconLayout->addWidget(ButtChangeIcon);
00126    hGroups->addLayout(iconLayout);
00127    connect(ButtChangeIcon,SIGNAL(clicked()),this,SLOT(slotChangeIcon()));
00128 
00129    // group checkboxes
00130    QGroupBox *group3 = new QGroupBox();
00131    QVBoxLayout *group3Layout = new QVBoxLayout();
00132    group3->setLayout(group3Layout);
00133    iconLayout->addWidget(group3);
00134 
00135    //
00136    QCheckBox *autoLoadSelCheck = new QCheckBox(tr("auto-load selected"));
00137    autoLoadSelCheck->setToolTip(
00138                tr("Load the selected symbols when opening the project."));
00139 
00140    autoLoadSelCheck->setDisabled(true); //disabled for now
00141 
00142    group3Layout->addWidget(autoLoadSelCheck);
00143 
00144    /*
00145    QCheckBox *autoLoadAllwaysCheck = new QCheckBox(tr("auto-load all"));
00146    autoLoadAllwaysCheck->setToolTip(
00147                tr("Load all symbols."));
00148    group3Layout->addWidget(autoLoadAllwaysCheck);
00149    */
00150 
00151    connect(fileView, SIGNAL(itemPressed(QListWidgetItem*)),
00152            this, SLOT(slotSymbolFileClicked(QListWidgetItem*)));
00153 
00154 //   qDebug() << "files " << symbolFiles;
00155 
00156    for(int i=0; i < symbolFiles.size(); i++){
00157        QListWidgetItem *item = new QListWidgetItem(symbolFiles.at(i), fileView);
00158      item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
00159      item->setCheckState(Qt::Checked);
00160 
00161      //set first as selected, one need to be selected to assign bitmap
00162      fileView->setCurrentRow(0);
00163    }
00164 
00165    // update icon
00166    this->slotSymbolFileClicked(fileView->currentItem());
00167 
00168    fileView->installEventFilter(this);
00169    fileView->setFocus();
00170 
00171 }
00172 
00173 void LoadDialog::slotSelectAll()
00174 {
00175     for(int i = 0; i < fileView->count(); ++i)
00176     {
00177         QListWidgetItem* item = fileView->item(i);
00178         item->setCheckState(Qt::Checked);
00179 //        qDebug() << "select" << item->text();
00180     }
00181 }
00182 
00183 void LoadDialog::slotSelectNone()
00184 {
00185     for(int i = 0; i < fileView->count(); ++i)
00186     {
00187         QListWidgetItem* item = fileView->item(i);
00188         item->setCheckState(Qt::Unchecked);
00189 //        qDebug() << "unselect" << item->text();
00190     }
00191 }
00192 
00193 void LoadDialog::slotSymbolFileClicked(QListWidgetItem* item)
00194 {
00195 //  qDebug() << "pressed" << item->text();
00196  // get bitmap, try to plot
00197  // similar to QucsApp::slotSetCompView
00198   QString JSON = projDir.filePath(item->text());
00199 
00200 //  qDebug() << "read " << JSON;
00201 
00202   // Just need path to bitmap, do not create an object
00203   QString Name, vaBitmap;
00204   Component * c = (Component *)
00205           vacomponent::info (Name, vaBitmap, false, JSON);
00206   if (c) delete c;
00207 
00208 //  qDebug() << "slotSymbolFileClicked" << Name << vaBitmap;
00209 
00210   // check if icon exists, fall back to default
00211   QString iconPath = QString(projDir.absFilePath(vaBitmap+".png"));
00212   QFile iconFile(iconPath);
00213 
00214   if(iconFile.exists())
00215   {
00216     // load bitmap defined on the JSON symbol file
00217     iconPixmap->setPixmap(QPixmap(iconPath));
00218   }
00219   else
00220   {
00221     QMessageBox::information(this, tr("Info"),
00222                  tr("Icon not found:\n %1.png").arg(vaBitmap));
00223     // default icon
00224     iconPixmap->setPixmap(QPixmap(":/bitmaps/editdelete.png"));
00225   }
00226 }
00227 
00228 void LoadDialog::reject()
00229 {
00230     done(AbortClosing);
00231 }
00232 
00233 //
00234 void LoadDialog::loadSelected()
00235 {
00236   // build list vaComponentds
00237   // hand it down to main app
00238 
00239   selectedComponents.clear();
00240 
00241   for(int i = 0; i < fileView->count(); ++i)
00242   {
00243     QListWidgetItem* item = fileView->item(i);
00244 
00245     if (item->checkState() == Qt::Checked){
00246         QString key = item->text().split("_symbol.json").at(0);
00247         QString value = projDir.absoluteFilePath(item->text());
00248 
00249         qDebug() << "key" << key;
00250         qDebug() << "file " << value;
00251 
00252         selectedComponents[key] = value;
00253     }
00254   }
00255 
00256   accept();
00257 }
00258 
00259 
00260 /*
00261  * Browse for icon image
00262  * Save image path to JSON symbol file
00263  */
00264 void LoadDialog::slotChangeIcon()
00265 {
00266 //  qDebug() << "slotChangeIcon";
00267   QString iconFileName =
00268           QFileDialog::getOpenFileName(this,
00269                                         tr("Open File"),
00270                                         QString(projDir.absolutePath()),
00271                                         tr("Icon image (*.png)"));
00272 
00273   QString newIcon =  QFileInfo(iconFileName).baseName();
00274 //  qDebug() << "icon "<< newIcon;
00275 
00276   QString filename = fileView->currentItem()->text();
00277   filename = projDir.absoluteFilePath(filename);
00278 //  qDebug() << "for " <<  filename;
00279 
00280   // open json
00281   // change property
00282   // save&close
00283   // Try to open the JSON file, can use QScriptEngine for this?
00284   //
00285   QFile file(filename);
00286   QByteArray ba;
00287   ba.clear();
00288   if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
00289     QMessageBox::critical(this, tr("Error"),
00290                           tr("File not found: %1").arg(filename));
00291   }
00292   else {
00293     QTextStream in(&file);
00294     while ( !in.atEnd() )
00295     {
00296       QString line = in.readLine();
00297       if (line.contains("BitmapFile")){
00298           QString change =
00299                   QString("  \"BitmapFile\" : \"%1\",").arg(newIcon);
00300           ba.append(change+"\n");
00301       }
00302       else{
00303           ba.append(line+"\n");
00304       }
00305     }
00306   }
00307 
00308   // write back to the same file, clear it first
00309   file.resize(0);
00310   file.write(ba);
00311   file.close();
00312 
00313   // update icon
00314   this->slotSymbolFileClicked(fileView->currentItem());
00315 }
00316 
00317 bool LoadDialog::eventFilter(QObject *obj, QEvent *event)
00318 {
00319   Q_UNUSED(obj);
00320 
00321   if (event->type() == QEvent::KeyPress) {
00322     QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
00323 //    qDebug() << "type" << keyEvent->key() << fileView->count();
00324     if (keyEvent->key() == Qt::Key_Up) {
00325 
00326         fileView->setCurrentRow(std::max(0, fileView->currentRow()-1));
00327         this->slotSymbolFileClicked(fileView->currentItem());
00328         return true;
00329     }
00330     if (keyEvent->key() == Qt::Key_Down) {
00331         fileView->setCurrentRow(std::min(fileView->count()-1, fileView->currentRow()+1));
00332         this->slotSymbolFileClicked(fileView->currentItem());
00333         return true;
00334     }
00335   }
00336   return false;
00337 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines