26 #include <qlineedit.h>
27 #include <qtextedit.h>
28 #include <qtextstream.h>
29 #include <qdatastream.h>
30 #include <qcheckbox.h>
31 #include <qlistview.h>
32 #include <qvalidator.h>
33 #include <qmessagebox.h>
34 #include <qpushbutton.h>
35 #include <qscrollview.h>
36 #include <qvbuttongroup.h>
46 : QDialog(App_, 0, TRUE, Qt::WDestructiveClose)
49 setCaption(tr(
"Create Library"));
51 Expr.setPattern(
"[\\w_]+");
52 Validator =
new QRegExpValidator(Expr,
this);
55 all =
new QVBoxLayout(
this);
59 QHBox *h1 =
new QHBox(
this);
61 theLabel =
new QLabel(tr(
"Library Name:"), h1);
62 NameEdit =
new QLineEdit(h1);
63 NameEdit->setValidator(Validator);
65 Group =
new QVButtonGroup(tr(
"Choose subcircuits:"),
this);
66 all->addWidget(Group);
69 QHBox *h3 =
new QHBox(Group);
70 ButtSelectAll =
new QPushButton(tr(
"Select All"), h3);
71 connect(ButtSelectAll, SIGNAL(clicked()), SLOT(slotSelectAll()));
72 ButtSelectNone =
new QPushButton(tr(
"Deselect All"), h3);
73 connect(ButtSelectNone, SIGNAL(clicked()), SLOT(slotSelectNone()));
76 QScrollView *Dia_Scroll =
new QScrollView(Group);
77 Dia_Scroll->setMargin(5);
78 QVBox *Dia_Box =
new QVBox(Dia_Scroll->viewport());
79 Dia_Scroll->addChild(Dia_Box);
81 ErrText =
new QTextEdit(
this);
82 ErrText->setHidden(
true);
83 ErrText->setTextFormat(Qt::PlainText);
84 ErrText->setWordWrap(QTextEdit::NoWrap);
85 all->addWidget(ErrText);
88 QHBox *h2 =
new QHBox(
this);
90 ButtCancel =
new QPushButton(tr(
"Cancel"), h2);
91 connect(ButtCancel, SIGNAL(clicked()), SLOT(reject()));
92 ButtCreate =
new QPushButton(tr(
"Next >>"), h2);
93 connect(ButtCreate, SIGNAL(clicked()), SLOT(slotCreate()));
94 ButtCreate->setDefault(
true);
98 QListViewItem *p = SchematicList->firstChild();
102 if(!p->text(1).isEmpty())
103 BoxList.append(
new QCheckBox(p->text(0), Dia_Box));
104 p = p->nextSibling();
108 if(BoxList.isEmpty()) {
109 ButtCreate->setEnabled(
false);
111 (
new QLabel(tr(
"No subcircuits!"), Dia_Box))->paletteBackgroundColor();
114 theColor = BoxList.current()->paletteBackgroundColor();
115 Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
125 void LibraryDialog::slotCreate()
127 if(NameEdit->text().isEmpty()) {
128 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please insert a library name!"));
134 for(p = BoxList.first(); p != 0; p = BoxList.next())
139 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please choose at least one subcircuit!"));
145 if(!LibDir.cd(
"user_lib")) {
146 if(!LibDir.mkdir(
"user_lib")) {
147 QMessageBox::warning(
this, tr(
"Warning"),
148 tr(
"Cannot create user library directory !"));
151 LibDir.cd(
"user_lib");
156 if(LibFile.exists()) {
157 QMessageBox::critical(
this, tr(
"Error"), tr(
"A system library with this name already exists!"));
161 LibFile.setName(LibDir.absFilePath(NameEdit->text()) +
".lib");
162 if(LibFile.exists()) {
163 QMessageBox::critical(
this, tr(
"Error"), tr(
"A library with this name already exists!"));
167 Group->setHidden(
true);
168 ErrText->setHidden(
false);
169 NameEdit->setHidden(
true);
170 disconnect(ButtCreate, SIGNAL(clicked()), 0, 0);
171 connect(ButtCreate, SIGNAL(clicked()), SLOT(slotNext()));
173 for(p = BoxList.first(); p != 0; p = BoxList.next())
176 theLabel->setText(tr(
"Enter description for \"%1\":").
arg(p->text()));
178 for(p = BoxList.next(); p != 0; p = BoxList.next())
182 ButtCreate->setText(tr(
"Create"));
186 void LibraryDialog::intoStream(QTextStream &Stream, QString &tmp,
189 int i = tmp.find(
"TOP LEVEL MARK");
191 i = tmp.find(
'\n',i) + 1;
194 Stream <<
" <" << sec <<
">";
196 Stream <<
" </" << sec <<
">\n";
200 int LibraryDialog::intoFile(QString &ifn, QString &ofn, QStringList &IFiles)
204 if(!
ifile.open(IO_ReadOnly)) {
205 ErrText->insert(QObject::tr(
"ERROR: Cannot open file \"%1\".\n").
210 QByteArray FileContent =
ifile.readAll();
212 if(
ifile.name().right(4) ==
".lst")
213 LibDir.remove(
ifile.name());
214 QDir LibDirSub(LibDir);
215 if(!LibDirSub.cd(NameEdit->text())) {
216 if(!LibDirSub.mkdir(NameEdit->text())) {
218 QObject::tr(
"ERROR: Cannot create user library subdirectory !\n"));
221 LibDirSub.cd(NameEdit->text());
224 ofn = Info.fileName();
227 ofile.setName(LibDirSub.absFilePath(ofn));
228 if(!ofile.open(IO_WriteOnly)) {
230 QObject::tr(
"ERROR: Cannot create file \"%1\".\n").
arg(ofn));
234 QDataStream ds(&ofile);
235 ds.writeRawBytes(FileContent.data(), FileContent.size());
243 void LibraryDialog::slotNext()
245 Descriptions.append(ErrText->text());
248 QCheckBox *p = BoxList.current();
250 theLabel->setText(tr(
"Enter description for \"%1\":").
arg(p->text()));
251 for(p = BoxList.next(); p != 0; p = BoxList.next())
255 ButtCreate->setText(tr(
"Create"));
259 theLabel->setShown(
false);
260 ErrText->setReadOnly(
true);
261 ButtCancel->setEnabled(
false);
262 ButtCreate->setText(tr(
"Close"));
263 disconnect(ButtCreate, SIGNAL(clicked()), 0, 0);
264 connect(ButtCreate, SIGNAL(clicked()), SLOT(accept()));
266 if(!LibFile.open(IO_WriteOnly)) {
267 ErrText->append(tr(
"Error: Cannot create library!"));
271 Stream.setDevice(&LibFile);
272 Stream <<
"<Qucs Library " PACKAGE_VERSION
" \""
273 << NameEdit->text() <<
"\">\n\n";
276 bool Success =
true, ret;
277 QStringList::Iterator it = Descriptions.begin();
279 QTextStream ts(&tmp, IO_WriteOnly);
280 for(p = BoxList.first(); p != 0; p = BoxList.next())
282 Stream <<
"<Component " + p->text().section(
'.',0,0) +
">\n"
283 <<
" <Description>\n"
285 <<
"\n </Description>\n";
290 ErrText->append(tr(
"Error: Cannot load subcircuit \"%1\".").
294 Doc->
DocName = NameEdit->text() +
"_" + p->text();
302 intoStream(Stream, tmp,
"Model");
305 SubMap::Iterator it =
FileList.begin();
307 QString f = it.data().File;
309 if(it.data().Type ==
"SCH") {
312 }
else if(it.data().Type ==
"CIR") {
316 if (!ifn.isEmpty()) error += intoFile(ifn, ofn, IFiles);
320 if(!IFiles.isEmpty()) {
321 Stream <<
" <ModelIncludes \"" << IFiles.join(
"\" \"") <<
"\">\n";
323 Success = error > 0 ?
false :
true;
325 ErrText->insert(
"\n");
334 intoStream(Stream, tmp,
"VerilogModel");
337 SubMap::Iterator it =
FileList.begin();
339 QString f = it.data().File;
341 if(it.data().Type ==
"SCH") {
344 }
else if(it.data().Type ==
"VER") {
348 if (!ifn.isEmpty()) error += intoFile(ifn, ofn, IFiles);
352 if(!IFiles.isEmpty()) {
353 Stream <<
" <VerilogModelIncludes \""
354 << IFiles.join(
"\" \"") <<
"\">\n";
356 Success = error > 0 ?
false :
true;
358 ErrText->insert(
"\n");
367 intoStream(Stream, tmp,
"VHDLModel");
370 SubMap::Iterator it =
FileList.begin();
372 QString f = it.data().File;
374 if(it.data().Type ==
"SCH") {
377 }
else if(it.data().Type ==
"VHD") {
381 if (!ifn.isEmpty()) error += intoFile(ifn, ofn, IFiles);
385 if(!IFiles.isEmpty()) {
386 Stream <<
" <VHDLModelIncludes \""
387 << IFiles.join(
"\" \"") <<
"\">\n";
389 Success = error > 0 ?
false :
true;
391 ErrText->insert(
"\n");
394 Stream <<
" <Symbol>\n";
398 Stream <<
" <" << pp->
save() <<
">\n";
400 Stream <<
" </Symbol>\n"
401 <<
"</Component>\n\n";
413 ErrText->append(tr(
"Successfully created library."));
417 void LibraryDialog::slotSelectAll()
420 for(p = BoxList.first(); p != 0; p = BoxList.next()) p->setChecked(
true);
424 void LibraryDialog::slotSelectNone()
427 for(p = BoxList.first(); p != 0; p = BoxList.next()) p->setChecked(
false);