My Project  0.0.16
QUCS Mapping
 All Classes Namespaces 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 
18 #include <qhbox.h>
19 #include <qlabel.h>
20 #include <qlayout.h>
21 #include <qlineedit.h>
22 #include <qtextedit.h>
23 #include <qvgroupbox.h>
24 #include <qcombobox.h>
25 #include <qfiledialog.h>
26 #include <qpushbutton.h>
27 #include <qmessagebox.h>
28 
29 #include "importdialog.h"
30 #include "main.h"
31 #include "qucs.h"
32 
33 
35  : QDialog(parent, 0, FALSE, Qt::WDestructiveClose)
36 {
37  setCaption(tr("Convert Data File..."));
38 
39  all = new QGridLayout(this, 4, 3, 5, 3);
40 
41  QVGroupBox *Group2 = new QVGroupBox(tr("File specification"),this);
42  all->addMultiCellWidget(Group2, 0,1, 0,2);
43  QWidget *f = new QWidget(Group2);
44  QGridLayout *file = new QGridLayout(f, 3, 3, 5);
45  file->addWidget(new QLabel(tr("Input File:"), f), 0, 0);
46  ImportEdit = new QLineEdit(f);
47  file->addWidget(ImportEdit, 0, 1);
48  QPushButton *BrowseButt = new QPushButton(tr("Browse"), f);
49  file->addWidget(BrowseButt, 0, 2);
50  connect(BrowseButt, SIGNAL(clicked()), SLOT(slotBrowse()));
51  file->addWidget(new QLabel(tr("Output File:"), f), 1, 0);
52  OutputEdit = new QLineEdit(f);
53  file->addWidget(OutputEdit, 1, 1);
54  OutputLabel = new QLabel(tr("Output Data:"), f);
55  OutputLabel->setEnabled(false);
56  file->addWidget(OutputLabel, 2, 0);
57  OutputData = new QLineEdit(f);
58  OutputData->setEnabled(false);
59  file->addWidget(OutputData, 2, 1);
60  OutType = new QComboBox(f);
61  OutType->insertItem(tr("Qucs dataset"));
62  OutType->insertItem(tr("Touchstone"));
63  OutType->insertItem(tr("CSV"));
64  OutType->insertItem(tr("Qucs library"));
65  OutType->insertItem(tr("Qucs netlist"));
66  OutType->insertItem(tr("Matlab"));
67  connect(OutType, SIGNAL(activated(int)), SLOT(slotType(int)));
68  file->addWidget(OutType, 2, 2);
69 
70  QVGroupBox *Group1 = new QVGroupBox(tr("Messages"),this);
71  all->addMultiCellWidget(Group1, 2,2, 0,2);
72 
73  MsgText = new QTextEdit(Group1);
74  MsgText->setTextFormat(Qt::PlainText);
75  MsgText->setReadOnly(true);
76  MsgText->setWordWrap(QTextEdit::NoWrap);
77  MsgText->setMinimumSize(250, 60);
78 
79  QHBox *Butts = new QHBox(this);
80  all->addMultiCellWidget(Butts, 3,3, 0,2);
81 
82  Butts->setStretchFactor(new QWidget(Butts), 5); // stretchable placeholder
83 
84  ImportButt = new QPushButton(tr("Convert"), Butts);
85  connect(ImportButt, SIGNAL(clicked()), SLOT(slotImport()));
86  AbortButt = new QPushButton(tr("Abort"), Butts);
87  AbortButt->setDisabled(true);
88  connect(AbortButt, SIGNAL(clicked()), SLOT(slotAbort()));
89  CancelButt = new QPushButton(tr("Close"), Butts);
90  connect(CancelButt, SIGNAL(clicked()), SLOT(reject()));
91 }
92 
94 {
95  if(Process.isRunning()) Process.kill();
96  delete all;
97 }
98 
99 // ------------------------------------------------------------------------
100 void ImportDialog::slotBrowse()
101 {
102  QString s = QFileDialog::getOpenFileName(
103  lastDir.isEmpty() ? QString(".") : lastDir,
104  tr("All known")+
105  " (*.s?p *.csv *.citi *.cit *.asc *.mdl *.vcd *.dat *.cir);;"+
106  tr("Touchstone files")+" (*.s?p);;"+
107  tr("CSV files")+" (*.csv);;"+
108  tr("CITI files")+" (*.citi *.cit);;"+
109  tr("ZVR ASCII files")+" (*.asc);;"+
110  tr("IC-CAP model files")+" (*.mdl);;"+
111  tr("VCD files")+" (*.vcd);;"+
112  tr("Qucs dataset files")+" (*.dat);;"+
113  tr("SPICE files")+" (*.cir);;"+
114  tr("Any file")+" (*)",
115  this, 0, tr("Enter a Data File Name"));
116 
117  if(!s.isEmpty()) {
118  QFileInfo Info(s);
119  lastDir = Info.dirPath(true); // remember last directory
120  ImportEdit->setText(s);
121 
122  if(OutputEdit->text().isEmpty()) {
123  switch(OutType->currentItem()) {
124  case 0:
125  OutputEdit->setText(Info.baseName()+".dat");
126  break;
127  case 1:
128  OutputEdit->setText(Info.baseName()+".snp");
129  break;
130  case 2:
131  OutputEdit->setText(Info.baseName()+".csv");
132  break;
133  case 3:
134  OutputEdit->setText(Info.baseName()+".lib");
135  break;
136  case 4:
137  OutputEdit->setText(Info.baseName()+".txt");
138  break;
139  case 5:
140  OutputEdit->setText(Info.baseName()+".mat");
141  break;
142  default:
143  OutputEdit->setText(Info.baseName()+".dat");
144  break;
145  }
146  }
147  }
148 }
149 
150 // ------------------------------------------------------------------------
151 void ImportDialog::slotImport()
152 {
153  MsgText->clear();
154  if (OutputEdit->text().isEmpty())
155  return;
156 
157  ImportButt->setDisabled(true);
158  AbortButt->setDisabled(false);
159 
160  QFile File(QucsWorkDir.filePath(OutputEdit->text()));
161  if(File.exists())
162  if(QMessageBox::information(this, tr("Info"),
163  tr("Output file already exists!")+"\n"+tr("Overwrite it?"),
164  tr("&Yes"), tr("&No"), 0,1,1))
165  {
166  ImportButt->setDisabled(false);
167  AbortButt->setDisabled(true);
168  return;
169  }
170 
171  QFileInfo Info(ImportEdit->text());
172  QString Suffix = Info.extension();
173  QStringList CommandLine;
174  CommandLine << QucsSettings.BinDir + "qucsconv" << "-if";
175 
176  if((Suffix == "citi") || (Suffix == "cit"))
177  CommandLine << "citi";
178  else if(Suffix == "vcd")
179  CommandLine << "vcd";
180  else if(Suffix == "asc")
181  CommandLine << "zvr";
182  else if(Suffix == "mdl")
183  CommandLine << "mdl";
184  else if(Suffix == "csv")
185  CommandLine << "csv";
186  else if(Suffix == "dat")
187  CommandLine << "qucsdata";
188  else if(Suffix == "cir")
189  CommandLine << "spice";
190  else for(;;) {
191  if(Suffix.at(0) == 's')
192  if(Suffix.at(2) == 'p')
193  if(Suffix.length() == 3)
194  if(Suffix.at(1).isDigit()) {
195  CommandLine << "touchstone";
196  break;
197  }
198 
199  MsgText->append(tr("ERROR: Unknown file format! Please check file name extension!"));
200  return;
201  }
202 
203  CommandLine << "-of";
204  switch(OutType->currentItem()) {
205  case 0:
206  CommandLine << "qucsdata";
207  break;
208  case 1:
209  CommandLine << "touchstone";
210  if (!OutputData->text().isEmpty())
211  CommandLine << "-d" << OutputData->text();
212  break;
213  case 2:
214  CommandLine << "csv";
215  if (!OutputData->text().isEmpty())
216  CommandLine << "-d" << OutputData->text();
217  break;
218  case 3:
219  CommandLine << "qucslib";
220  break;
221  case 4:
222  CommandLine << "qucs";
223  break;
224  case 5:
225  CommandLine << "matlab";
226  break;
227  default:
228  CommandLine << "qucsdata";
229  break;
230  }
231 
232  CommandLine << "-i" << ImportEdit->text()
233  << "-o" << QucsWorkDir.filePath(OutputEdit->text());
234 
235  Process.setArguments(CommandLine);
236  Process.blockSignals(false);
237 
238  disconnect(&Process, 0, 0, 0);
239  connect(&Process, SIGNAL(readyReadStderr()), SLOT(slotDisplayErr()));
240  connect(&Process, SIGNAL(readyReadStdout()), SLOT(slotDisplayMsg()));
241  connect(&Process, SIGNAL(processExited()), SLOT(slotProcessEnded()));
242 
243  MsgText->append(tr("Running command line:")+"\n");
244  MsgText->append(CommandLine.join(" "));
245  MsgText->append("\n");
246 
247  if(!Process.start())
248  MsgText->append(tr("ERROR: Cannot start converter!"));
249 }
250 
251 // ------------------------------------------------------------------------
252 void ImportDialog::slotType(int index)
253 {
254  switch(index) {
255  case 0:
256  case 3:
257  case 4:
258  case 5:
259  OutputData->setEnabled(false);
260  OutputLabel->setEnabled(false);
261  break;
262  case 1:
263  case 2:
264  OutputData->setEnabled(true);
265  OutputLabel->setEnabled(true);
266  break;
267  default:
268  OutputData->setEnabled(false);
269  OutputLabel->setEnabled(false);
270  break;
271  }
272 }
273 
274 // ------------------------------------------------------------------------
275 void ImportDialog::slotAbort()
276 {
277  if(Process.isRunning()) Process.kill();
278  AbortButt->setDisabled(true);
279  ImportButt->setDisabled(false);
280 }
281 
282 // ------------------------------------------------------------------------
283 // Is called when the process sends an output to stdout.
284 void ImportDialog::slotDisplayMsg()
285 {
286  int par = MsgText->paragraphs();
287  int idx = MsgText->paragraphLength(par-1);
288  MsgText->setCursorPosition(par-1,idx);
289  MsgText->insert(QString(Process.readStdout()));
290 }
291 
292 // ------------------------------------------------------------------------
293 // Is called when the process sends an output to stderr.
294 void ImportDialog::slotDisplayErr()
295 {
296  int par = MsgText->paragraphs();
297  int idx = MsgText->paragraphLength(par-1);
298  MsgText->setCursorPosition(par-1,idx);
299  MsgText->insert(QString(Process.readStderr()));
300 }
301 
302 // ------------------------------------------------------------------------
303 // Is called when the simulation process terminates.
304 void ImportDialog::slotProcessEnded()
305 {
306  ImportButt->setDisabled(false);
307  AbortButt->setDisabled(true);
308 
309  if(Process.normalExit() && (Process.exitStatus() == 0)) {
310  MsgText->append(tr("Successfully converted file!"));
311 
312  disconnect(CancelButt, SIGNAL(clicked()), 0, 0);
313  connect(CancelButt, SIGNAL(clicked()), SLOT(accept()));
314  }
315  else
316  MsgText->append(tr("Converter ended with errors!"));
317 }