Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/dialogs/importdialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                              importdialog.cpp
00003                             ------------------
00004     begin                : Fri Jun 23 2006
00005     copyright            : (C) 2006 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include <QLabel>
00018 #include <QLineEdit>
00019 #include <QTextEdit>
00020 #include <QGroupBox>
00021 #include <QComboBox>
00022 #include <QFileDialog>
00023 #include <QPushButton>
00024 #include <QMessageBox>
00025 #include <QGridLayout>
00026 #include <QDebug>
00027 
00028 #include "importdialog.h"
00029 #include "main.h"
00030 #include "qucs.h"
00031 
00032 
00033 ImportDialog::ImportDialog(QWidget *parent)
00034     : QDialog(parent) 
00035 {
00036   setWindowTitle(tr("Convert Data File..."));
00037 
00038   all = new QGridLayout(this);
00039 
00040   QGroupBox *Group2 = new QGroupBox(tr("File specification"),this);
00041   
00042   QGridLayout *file = new QGridLayout();
00043   file->addWidget(new QLabel(tr("Input File:")),0, 0);
00044   ImportEdit = new QLineEdit();
00045   file->addWidget(ImportEdit, 0, 1);
00046   QPushButton *BrowseButt = new QPushButton(tr("Browse"));
00047   file->addWidget(BrowseButt, 0, 2);
00048   connect(BrowseButt, SIGNAL(clicked()), SLOT(slotBrowse()));
00049   file->addWidget(new QLabel(tr("Output File:")), 1, 0);
00050   OutputEdit = new QLineEdit();
00051   file->addWidget(OutputEdit, 1, 1);
00052   OutputLabel = new QLabel(tr("Output Data:"));
00053   OutputLabel->setEnabled(false);
00054   file->addWidget(OutputLabel, 2, 0);
00055   OutputData = new QLineEdit();
00056   OutputData->setEnabled(false);
00057   file->addWidget(OutputData, 2, 1);
00058   OutType = new QComboBox();
00059   OutType->addItem(tr("Qucs dataset"));
00060   OutType->addItem(tr("Touchstone"));
00061   OutType->addItem(tr("CSV"));
00062   OutType->addItem(tr("Qucs library"));
00063   OutType->addItem(tr("Qucs netlist"));
00064   OutType->addItem(tr("Matlab"));
00065   connect(OutType, SIGNAL(activated(int)), SLOT(slotType(int)));
00066   file->addWidget(OutType, 2, 2);
00067   
00068   Group2->setLayout(file);
00069   all->addWidget(Group2, 0,0,1,1);
00070   
00071   QGroupBox *Group1 = new QGroupBox(tr("Messages"));
00072   
00073   QVBoxLayout *vMess = new QVBoxLayout();
00074   MsgText = new QTextEdit();
00075   vMess->addWidget(MsgText);
00076   MsgText->setTextFormat(Qt::PlainText);
00077   MsgText->setReadOnly(true);
00078   MsgText->setWordWrapMode(QTextOption::NoWrap);
00079   MsgText->setMinimumSize(250, 60);
00080   Group1->setLayout(vMess);
00081   all->addWidget(Group1, 1,0,1,1);
00082 
00083   QHBoxLayout *Butts = new QHBoxLayout();
00084   
00085   Butts->addStretch(5);
00086  
00087   ImportButt = new QPushButton(tr("Convert"));
00088   connect(ImportButt, SIGNAL(clicked()), SLOT(slotImport()));
00089   AbortButt = new QPushButton(tr("Abort"));
00090   AbortButt->setDisabled(true);
00091   connect(AbortButt, SIGNAL(clicked()), SLOT(slotAbort()));
00092   CancelButt = new QPushButton(tr("Close"));
00093   connect(CancelButt, SIGNAL(clicked()), SLOT(reject()));
00094   Butts->addWidget(ImportButt);
00095   Butts->addWidget(AbortButt);
00096   Butts->addWidget(CancelButt);
00097   
00098   all->addLayout(Butts,2,0,1,1);
00099 }
00100 
00101 ImportDialog::~ImportDialog()
00102 {
00103   if(Process.Running)  Process.kill();
00104   delete all;
00105 }
00106 
00107 // ------------------------------------------------------------------------
00108 void ImportDialog::slotBrowse()
00109 {
00110   QString s = QFileDialog::getOpenFileName(
00111      lastDir.isEmpty() ? QString(".") : lastDir,
00112      tr("All known")+
00113      " (*.s?p *.csv *.citi *.cit *.asc *.mdl *.vcd *.dat *.cir);;"+
00114      tr("Touchstone files")+" (*.s?p);;"+
00115      tr("CSV files")+" (*.csv);;"+
00116      tr("CITI files")+" (*.citi *.cit);;"+
00117      tr("ZVR ASCII files")+" (*.asc);;"+
00118      tr("IC-CAP model files")+" (*.mdl);;"+
00119      tr("VCD files")+" (*.vcd);;"+
00120      tr("Qucs dataset files")+" (*.dat);;"+
00121      tr("SPICE files")+" (*.cir);;"+
00122      tr("Any file")+" (*)",
00123      this, 0, tr("Enter a Data File Name"));
00124 
00125   if(!s.isEmpty()) {
00126     QFileInfo Info(s);
00127     lastDir = Info.dirPath(true);  // remember last directory
00128     ImportEdit->setText(s);
00129 
00130     if(OutputEdit->text().isEmpty()) {
00131       switch(OutType->currentItem()) {
00132       case 0:
00133   OutputEdit->setText(Info.baseName()+".dat");
00134   break;
00135       case 1:
00136   OutputEdit->setText(Info.baseName()+".snp");
00137   break;
00138       case 2:
00139   OutputEdit->setText(Info.baseName()+".csv");
00140   break;
00141       case 3:
00142   OutputEdit->setText(Info.baseName()+".lib");
00143   break;
00144       case 4:
00145   OutputEdit->setText(Info.baseName()+".txt");
00146   break;
00147       case 5:
00148   OutputEdit->setText(Info.baseName()+".mat");
00149   break;
00150       default:
00151   OutputEdit->setText(Info.baseName()+".dat");
00152   break;
00153       }
00154     }
00155   }
00156 }
00157 
00158 // ------------------------------------------------------------------------
00159 void ImportDialog::slotImport()
00160 {
00161   MsgText->clear();
00162   if (OutputEdit->text().isEmpty())
00163     return;
00164 
00165   ImportButt->setDisabled(true);
00166   AbortButt->setDisabled(false);
00167 
00168   QFile File(QucsSettings.QucsWorkDir.filePath(OutputEdit->text()));
00169   if(File.exists())
00170     if(QMessageBox::information(this, tr("Info"),
00171           tr("Output file already exists!")+"\n"+tr("Overwrite it?"),
00172           tr("&Yes"), tr("&No"), 0,1,1))
00173       {
00174   ImportButt->setDisabled(false);
00175   AbortButt->setDisabled(true);
00176   return;
00177       }
00178 
00179   QFileInfo Info(ImportEdit->text());
00180   QString Suffix = Info.extension();
00181   QString Program;
00182   QStringList CommandLine;
00183 
00184   Program = QucsSettings.Qucsconv;
00185   CommandLine  << "-if";
00186   
00187   if((Suffix == "citi") || (Suffix == "cit"))
00188     CommandLine << "citi";
00189   else if(Suffix == "vcd")
00190     CommandLine << "vcd";
00191   else if(Suffix == "asc")
00192     CommandLine << "zvr";
00193   else if(Suffix == "mdl")
00194     CommandLine << "mdl";
00195   else if(Suffix == "csv")
00196     CommandLine << "csv";
00197   else if(Suffix == "dat")
00198     CommandLine << "qucsdata";
00199   else if(Suffix == "cir")
00200     CommandLine << "spice";
00201   else for(;;) {
00202     if(Suffix.at(0) == 's')
00203       if(Suffix.at(2) == 'p')
00204         if(Suffix.length() == 3)
00205           if(Suffix.at(1).isDigit()) {
00206             CommandLine << "touchstone";
00207             break;
00208           }
00209 
00210     MsgText->append(tr("ERROR: Unknown file format! Please check file name extension!"));
00211     return;
00212   }
00213 
00214   CommandLine << "-of";
00215   switch(OutType->currentItem()) {
00216   case 0:
00217     CommandLine << "qucsdata";
00218     break;
00219   case 1:
00220     CommandLine << "touchstone";
00221     if (!OutputData->text().isEmpty())
00222       CommandLine << "-d" << OutputData->text();
00223     break;
00224   case 2:
00225     CommandLine << "csv";
00226     if (!OutputData->text().isEmpty())
00227       CommandLine << "-d" << OutputData->text();
00228     break;
00229   case 3:
00230     CommandLine << "qucslib";
00231     break;
00232   case 4:
00233     CommandLine << "qucs";
00234     break;
00235   case 5:
00236     CommandLine << "matlab";
00237     break;
00238   default:
00239     CommandLine << "qucsdata";
00240     break;
00241   }
00242 
00243   CommandLine << "-i" << ImportEdit->text()
00244               << "-o" << QucsSettings.QucsWorkDir.filePath(OutputEdit->text());
00245 
00246   Process.blockSignals(false);
00247 
00248   disconnect(&Process, 0, 0, 0);
00249   connect(&Process, SIGNAL(readyReadStandardError()), SLOT(slotDisplayErr()));
00250   connect(&Process, SIGNAL(readyReadStandardOutput()), SLOT(slotDisplayMsg()));
00251   connect(&Process, SIGNAL(finished(int)), SLOT(slotProcessEnded(int)));
00252 
00253   MsgText->append(tr("Running command line:")+"\n");
00254   MsgText->append(Program + CommandLine.join(" "));
00255   MsgText->append("\n");
00256 
00257   qDebug() << "Command:" << Program << CommandLine.join(" ");
00258   Process.start(Program, CommandLine);
00259   
00260   if(!Process.Running)
00261     MsgText->append(tr("ERROR: Cannot start converter!"));
00262 }
00263 
00264 // ------------------------------------------------------------------------
00265 void ImportDialog::slotType(int index)
00266 {
00267   switch(index) {
00268   case 0:
00269   case 3:
00270   case 4:
00271   case 5:
00272     OutputData->setEnabled(false);
00273     OutputLabel->setEnabled(false);
00274     break;
00275   case 1:
00276   case 2:
00277     OutputData->setEnabled(true);
00278     OutputLabel->setEnabled(true);
00279     break;
00280   default:
00281     OutputData->setEnabled(false);
00282     OutputLabel->setEnabled(false);
00283     break;
00284   }
00285 }
00286 
00287 // ------------------------------------------------------------------------
00288 void ImportDialog::slotAbort()
00289 {
00290   if(Process.Running)  Process.kill();
00291   AbortButt->setDisabled(true);
00292   ImportButt->setDisabled(false);
00293 }
00294 
00295 // ------------------------------------------------------------------------
00296 // Is called when the process sends an output to stdout.
00297 void ImportDialog::slotDisplayMsg()
00298 {
00299   MsgText->append(QString(Process.readAllStandardOutput()));
00300 }
00301 
00302 // ------------------------------------------------------------------------
00303 // Is called when the process sends an output to stderr.
00304 void ImportDialog::slotDisplayErr()
00305 {
00306   MsgText->append(QString(Process.readAllStandardError()));
00307 }
00308 
00309 // ------------------------------------------------------------------------
00310 // Is called when the simulation process terminates.
00311 void ImportDialog::slotProcessEnded(int status)
00312 {
00313   ImportButt->setDisabled(false);
00314   AbortButt->setDisabled(true);
00315 
00316   if(status == 0) {    
00317     MsgText->append(tr("Successfully converted file!"));
00318 
00319     disconnect(CancelButt, SIGNAL(clicked()), 0, 0);
00320     connect(CancelButt, SIGNAL(clicked()), SLOT(accept()));
00321   }
00322   else
00323     MsgText->append(tr("Converter ended with errors!"));
00324 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines