Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
importdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  importdialog.cpp
3  ------------------
4  begin : Fri Jun 23 2006
5  copyright : (C) 2006 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <QtGui>
18 #include <QLabel>
19 #include <QLineEdit>
20 #include <QTextEdit>
21 #include <QGroupBox>
22 #include <QComboBox>
23 #include <QFileDialog>
24 #include <QPushButton>
25 #include <QMessageBox>
26 #include <QGridLayout>
27 #include <QDebug>
28 
29 #include "importdialog.h"
30 #include "main.h"
31 #include "qucs.h"
32 
33 
35  : QDialog(parent) //, 0, FALSE, Qt::WDestructiveClose)
36 {
37  setWindowTitle(tr("Convert Data File..."));
38 
39  all = new QGridLayout(this);
40 
41  QGroupBox *Group2 = new QGroupBox(tr("File specification"),this);
42 
43  QGridLayout *file = new QGridLayout();
44  file->addWidget(new QLabel(tr("Input File:")),0, 0);
45  ImportEdit = new QLineEdit();
46  file->addWidget(ImportEdit, 0, 1);
47  QPushButton *BrowseButt = new QPushButton(tr("Browse"));
48  file->addWidget(BrowseButt, 0, 2);
49  connect(BrowseButt, SIGNAL(clicked()), SLOT(slotBrowse()));
50  file->addWidget(new QLabel(tr("Output File:")), 1, 0);
51  OutputEdit = new QLineEdit();
52  file->addWidget(OutputEdit, 1, 1);
53  OutputLabel = new QLabel(tr("Output Data:"));
54  OutputLabel->setEnabled(false);
55  file->addWidget(OutputLabel, 2, 0);
56  OutputData = new QLineEdit();
57  OutputData->setEnabled(false);
58  file->addWidget(OutputData, 2, 1);
59  OutType = new QComboBox();
60  OutType->addItem(tr("Qucs dataset"));
61  OutType->addItem(tr("Touchstone"));
62  OutType->addItem(tr("CSV"));
63  OutType->addItem(tr("Qucs library"));
64  OutType->addItem(tr("Qucs netlist"));
65  OutType->addItem(tr("Matlab"));
66  connect(OutType, SIGNAL(activated(int)), SLOT(slotType(int)));
67  file->addWidget(OutType, 2, 2);
68 
69  Group2->setLayout(file);
70  all->addWidget(Group2, 0,0,1,1);
71 
72  QGroupBox *Group1 = new QGroupBox(tr("Messages"));
73 
74  QVBoxLayout *vMess = new QVBoxLayout();
75  MsgText = new QTextEdit();
76  vMess->addWidget(MsgText);
77  MsgText->setTextFormat(Qt::PlainText);
78  MsgText->setReadOnly(true);
79  MsgText->setWordWrapMode(QTextOption::NoWrap);
80  MsgText->setMinimumSize(250, 60);
81  Group1->setLayout(vMess);
82  all->addWidget(Group1, 1,0,1,1);
83 
84  QHBoxLayout *Butts = new QHBoxLayout();
85 
86  Butts->addStretch(5);
87 
88  ImportButt = new QPushButton(tr("Convert"));
89  connect(ImportButt, SIGNAL(clicked()), SLOT(slotImport()));
90  AbortButt = new QPushButton(tr("Abort"));
91  AbortButt->setDisabled(true);
92  connect(AbortButt, SIGNAL(clicked()), SLOT(slotAbort()));
93  CancelButt = new QPushButton(tr("Close"));
94  connect(CancelButt, SIGNAL(clicked()), SLOT(reject()));
95  Butts->addWidget(ImportButt);
96  Butts->addWidget(AbortButt);
97  Butts->addWidget(CancelButt);
98 
99  all->addLayout(Butts,2,0,1,1);
100 }
101 
103 {
104  if(Process.Running) Process.kill();
105  delete all;
106 }
107 
108 // ------------------------------------------------------------------------
110 {
111  QString s = QFileDialog::getOpenFileName(
112  lastDir.isEmpty() ? QString(".") : lastDir,
113  tr("All known")+
114  " (*.s?p *.csv *.citi *.cit *.asc *.mdl *.vcd *.dat *.cir);;"+
115  tr("Touchstone files")+" (*.s?p);;"+
116  tr("CSV files")+" (*.csv);;"+
117  tr("CITI files")+" (*.citi *.cit);;"+
118  tr("ZVR ASCII files")+" (*.asc);;"+
119  tr("IC-CAP model files")+" (*.mdl);;"+
120  tr("VCD files")+" (*.vcd);;"+
121  tr("Qucs dataset files")+" (*.dat);;"+
122  tr("SPICE files")+" (*.cir);;"+
123  tr("Any file")+" (*)",
124  this, 0, tr("Enter a Data File Name"));
125 
126  if(!s.isEmpty()) {
127  QFileInfo Info(s);
128  lastDir = Info.dirPath(true); // remember last directory
129  ImportEdit->setText(s);
130 
131  if(OutputEdit->text().isEmpty()) {
132  switch(OutType->currentItem()) {
133  case 0:
134  OutputEdit->setText(Info.baseName()+".dat");
135  break;
136  case 1:
137  OutputEdit->setText(Info.baseName()+".snp");
138  break;
139  case 2:
140  OutputEdit->setText(Info.baseName()+".csv");
141  break;
142  case 3:
143  OutputEdit->setText(Info.baseName()+".lib");
144  break;
145  case 4:
146  OutputEdit->setText(Info.baseName()+".txt");
147  break;
148  case 5:
149  OutputEdit->setText(Info.baseName()+".mat");
150  break;
151  default:
152  OutputEdit->setText(Info.baseName()+".dat");
153  break;
154  }
155  }
156  }
157 }
158 
159 // ------------------------------------------------------------------------
161 {
162  MsgText->clear();
163  if (OutputEdit->text().isEmpty())
164  return;
165 
166  ImportButt->setDisabled(true);
167  AbortButt->setDisabled(false);
168 
169  QFile File(QucsSettings.QucsWorkDir.filePath(OutputEdit->text()));
170  if(File.exists())
171  if(QMessageBox::information(this, tr("Info"),
172  tr("Output file already exists!")+"\n"+tr("Overwrite it?"),
173  tr("&Yes"), tr("&No"), 0,1,1))
174  {
175  ImportButt->setDisabled(false);
176  AbortButt->setDisabled(true);
177  return;
178  }
179 
180  QFileInfo Info(ImportEdit->text());
181  QString Suffix = Info.extension();
182  QString Program;
183  QStringList CommandLine;
184 
185  QString executableSuffix = "";
186 #ifdef __MINGW32__
187  executableSuffix = ".exe";
188 #endif
189 
190  Program = QucsSettings.BinDir + "qucsconv" + executableSuffix;
191  CommandLine << "-if";
192 
193  if((Suffix == "citi") || (Suffix == "cit"))
194  CommandLine << "citi";
195  else if(Suffix == "vcd")
196  CommandLine << "vcd";
197  else if(Suffix == "asc")
198  CommandLine << "zvr";
199  else if(Suffix == "mdl")
200  CommandLine << "mdl";
201  else if(Suffix == "csv")
202  CommandLine << "csv";
203  else if(Suffix == "dat")
204  CommandLine << "qucsdata";
205  else if(Suffix == "cir")
206  CommandLine << "spice";
207  else for(;;) {
208  if(Suffix.at(0) == 's')
209  if(Suffix.at(2) == 'p')
210  if(Suffix.length() == 3)
211  if(Suffix.at(1).isDigit()) {
212  CommandLine << "touchstone";
213  break;
214  }
215 
216  MsgText->append(tr("ERROR: Unknown file format! Please check file name extension!"));
217  return;
218  }
219 
220  CommandLine << "-of";
221  switch(OutType->currentItem()) {
222  case 0:
223  CommandLine << "qucsdata";
224  break;
225  case 1:
226  CommandLine << "touchstone";
227  if (!OutputData->text().isEmpty())
228  CommandLine << "-d" << OutputData->text();
229  break;
230  case 2:
231  CommandLine << "csv";
232  if (!OutputData->text().isEmpty())
233  CommandLine << "-d" << OutputData->text();
234  break;
235  case 3:
236  CommandLine << "qucslib";
237  break;
238  case 4:
239  CommandLine << "qucs";
240  break;
241  case 5:
242  CommandLine << "matlab";
243  break;
244  default:
245  CommandLine << "qucsdata";
246  break;
247  }
248 
249  CommandLine << "-i" << ImportEdit->text()
250  << "-o" << QucsSettings.QucsWorkDir.filePath(OutputEdit->text());
251 
252  Process.blockSignals(false);
253 
254  disconnect(&Process, 0, 0, 0);
255  connect(&Process, SIGNAL(readyReadStandardError()), SLOT(slotDisplayErr()));
256  connect(&Process, SIGNAL(readyReadStandardOutput()), SLOT(slotDisplayMsg()));
257  connect(&Process, SIGNAL(finished(int)), SLOT(slotProcessEnded(int)));
258 
259  MsgText->append(tr("Running command line:")+"\n");
260  MsgText->append(Program + CommandLine.join(" "));
261  MsgText->append("\n");
262 
263  qDebug() << "Command:" << Program << CommandLine.join(" ");
264  Process.start(Program, CommandLine);
265 
266  if(!Process.Running)
267  MsgText->append(tr("ERROR: Cannot start converter!"));
268 }
269 
270 // ------------------------------------------------------------------------
271 void ImportDialog::slotType(int index)
272 {
273  switch(index) {
274  case 0:
275  case 3:
276  case 4:
277  case 5:
278  OutputData->setEnabled(false);
279  OutputLabel->setEnabled(false);
280  break;
281  case 1:
282  case 2:
283  OutputData->setEnabled(true);
284  OutputLabel->setEnabled(true);
285  break;
286  default:
287  OutputData->setEnabled(false);
288  OutputLabel->setEnabled(false);
289  break;
290  }
291 }
292 
293 // ------------------------------------------------------------------------
295 {
296  if(Process.Running) Process.kill();
297  AbortButt->setDisabled(true);
298  ImportButt->setDisabled(false);
299 }
300 
301 // ------------------------------------------------------------------------
302 // Is called when the process sends an output to stdout.
304 {
305  MsgText->append(QString(Process.readAllStandardOutput()));
306 }
307 
308 // ------------------------------------------------------------------------
309 // Is called when the process sends an output to stderr.
311 {
312  MsgText->append(QString(Process.readAllStandardError()));
313 }
314 
315 // ------------------------------------------------------------------------
316 // Is called when the simulation process terminates.
318 {
319  ImportButt->setDisabled(false);
320  AbortButt->setDisabled(true);
321 
322  if(status == 0) {
323  MsgText->append(tr("Successfully converted file!"));
324 
325  disconnect(CancelButt, SIGNAL(clicked()), 0, 0);
326  connect(CancelButt, SIGNAL(clicked()), SLOT(accept()));
327  }
328  else
329  MsgText->append(tr("Converter ended with errors!"));
330 }
QDir QucsWorkDir
Definition: main.h:65
QPushButton * AbortButt
Definition: importdialog.h:59
QLabel * OutputLabel
Definition: importdialog.h:55
tQucsSettings QucsSettings
Definition: main.cpp:52
void slotDisplayMsg()
QPushButton * CancelButt
Definition: importdialog.h:59
QPushButton * ImportButt
Definition: importdialog.h:59
QProcess Process
Definition: importdialog.h:56
QString BinDir
Definition: main.h:57
Definitions and declarations for the main application.
void slotType(int)
QString lastDir
Definition: main.cpp:55
QGridLayout * all
Definition: importdialog.h:53
QLineEdit * OutputEdit
Definition: importdialog.h:58
QLineEdit * ImportEdit
Definition: importdialog.h:58
QTextEdit * MsgText
Definition: importdialog.h:57
QComboBox * OutType
Definition: importdialog.h:60
QLineEdit * OutputData
Definition: importdialog.h:58
void slotProcessEnded(int status)
#define executableSuffix
Definition: simmessage.cpp:45
void slotDisplayErr()
ImportDialog(QWidget *)