Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
loaddialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2014 Guilherme Brondani Torri <guitorri@gmail.com> *
3  * *
4  * Modified from SaveDialog and LibraryDialog *
5  * *
6  * This is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2, or (at your option) *
9  * any later version. *
10  * *
11  * This software is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this package; see the file COPYING. If not, write to *
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, *
19  * Boston, MA 02110-1301, USA. *
20  ***************************************************************************/
21 
22 #include <QtGui>
23 #include <QVariant>
24 #include <QLabel>
25 #include <QPushButton>
26 #include <QHBoxLayout>
27 #include <QVBoxLayout>
28 #include <QScrollArea>
29 #include <QDebug>
30 #include <QFileDialog>
31 
32 #include <QScriptEngine>
33 #include <QScriptValue>
34 #include <QScriptValueIterator>
35 
36 #include "loaddialog.h"
37 #include "qucs.h"
38 #include "qucsdoc.h"
39 #include "components/components.h"
40 
41 LoadDialog::LoadDialog( QWidget* parent, const char* name, bool modal, Qt::WFlags fl )
42  : QDialog( parent, name, modal, fl )
43 {
44  if ( !name )
45  setWindowTitle( tr( "Load Verilog-A symbols" ) );
46  app = 0l;
47 // initDialog();
48 }
49 
51 {
52 }
53 
55 {
56  app = a;
57 }
58 
60 {
61  QVBoxLayout *all = new QVBoxLayout(this);
62  all->setMargin(5);
63  all->setSpacing(6);
64 
65  // hold group of files / group icon and checkboxes
66  QHBoxLayout *hGroups = new QHBoxLayout();
67  all->addLayout(hGroups);
68 
69  // ---
70  QGroupBox *group1 = new QGroupBox( tr( "Choose Verilog-A symbol files:" ) );
71  hGroups->addWidget(group1);
72 
73  QScrollArea *scrollArea = new QScrollArea(group1);
74  scrollArea->setWidgetResizable(true);
75 
76  fileView = new QListWidget(this);
77 
78  scrollArea->setWidget(fileView);
79 
80  QVBoxLayout *areaLayout = new QVBoxLayout();
81  areaLayout->addWidget(scrollArea);
82  group1->setLayout(areaLayout);
83 
84 
85  // ...........................................................
86  QGridLayout *gridButts = new QGridLayout();
87  all->addLayout(gridButts);
88 
89  ButtSelectAll = new QPushButton(tr("Select All"));
90  gridButts->addWidget(ButtSelectAll, 0, 0);
91  connect(ButtSelectAll, SIGNAL(clicked()), SLOT(slotSelectAll()));
92  ButtSelectNone = new QPushButton(tr("Deselect All"));
93  gridButts->addWidget(ButtSelectNone, 0, 1);
94  connect(ButtSelectNone, SIGNAL(clicked()), SLOT(slotSelectNone()));
95  // ...........................................................
96  ButtCancel = new QPushButton(tr("Cancel"));
97  gridButts->addWidget(ButtCancel, 1, 0);
98  connect(ButtCancel, SIGNAL(clicked()), SLOT(reject()));
99  ButtOk = new QPushButton(tr("Ok"));
100  gridButts->addWidget(ButtOk, 1, 1);
101  connect(ButtOk, SIGNAL(clicked()), SLOT(loadSelected()));
102  ButtOk->setDefault(true);
103 
104 
105  QVBoxLayout *iconLayout = new QVBoxLayout();
106 
107  QGroupBox *group2 = new QGroupBox( );
108 
109  QVBoxLayout *group2Layout = new QVBoxLayout();
110 
111  iconPixmap = new QLabel();
112  iconPixmap->setSizePolicy(QSizePolicy::Expanding,
113  QSizePolicy::Expanding);
114  iconPixmap->setAlignment(Qt::AlignCenter);
115  group2Layout->addWidget(iconPixmap);
116 
117  group2->setLayout(group2Layout);
118  iconLayout->addWidget(group2);
119 
120  ButtChangeIcon = new QPushButton(tr("Change Icon"));
121  iconLayout->addWidget(ButtChangeIcon);
122  hGroups->addLayout(iconLayout);
123  connect(ButtChangeIcon,SIGNAL(clicked()),this,SLOT(slotChangeIcon()));
124 
125  // group checkboxes
126  QGroupBox *group3 = new QGroupBox();
127  QVBoxLayout *group3Layout = new QVBoxLayout();
128  group3->setLayout(group3Layout);
129  iconLayout->addWidget(group3);
130 
131  //
132  QCheckBox *autoLoadSelCheck = new QCheckBox(tr("auto-load selected"));
133  autoLoadSelCheck->setToolTip(
134  tr("Load the selected symbols when opening the project."));
135 
136  autoLoadSelCheck->setDisabled(true); //disabled for now
137 
138  group3Layout->addWidget(autoLoadSelCheck);
139 
140  /*
141  QCheckBox *autoLoadAllwaysCheck = new QCheckBox(tr("auto-load all"));
142  autoLoadAllwaysCheck->setToolTip(
143  tr("Load all symbols."));
144  group3Layout->addWidget(autoLoadAllwaysCheck);
145  */
146 
147  connect(fileView, SIGNAL(itemPressed(QListWidgetItem*)),
148  this, SLOT(slotSymbolFileClicked(QListWidgetItem*)));
149 
150 // qDebug() << "files " << symbolFiles;
151 
152  for(int i=0; i < symbolFiles.size(); i++){
153  QListWidgetItem *item = new QListWidgetItem(symbolFiles.at(i), fileView);
154  item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
155  item->setCheckState(Qt::Checked);
156 
157  //set first as selected, one need to be selected to assign bitmap
158  fileView->setCurrentRow(0);
159  }
160 
161  // update icon
162  this->slotSymbolFileClicked(fileView->currentItem());
163 
164  fileView->installEventFilter(this);
165  fileView->setFocus();
166 
167 }
168 
170 {
171  for(int i = 0; i < fileView->count(); ++i)
172  {
173  QListWidgetItem* item = fileView->item(i);
174  item->setCheckState(Qt::Checked);
175 // qDebug() << "select" << item->text();
176  }
177 }
178 
180 {
181  for(int i = 0; i < fileView->count(); ++i)
182  {
183  QListWidgetItem* item = fileView->item(i);
184  item->setCheckState(Qt::Unchecked);
185 // qDebug() << "unselect" << item->text();
186  }
187 }
188 
189 void LoadDialog::slotSymbolFileClicked(QListWidgetItem* item)
190 {
191 // qDebug() << "pressed" << item->text();
192  // get bitmap, try to plot
193  // similar to QucsApp::slotSetCompView
194  QString JSON = projDir.filePath(item->text());
195 
196 // qDebug() << "read " << JSON;
197 
198  // Just need path to bitmap, do not create an object
199  QString Name, vaBitmap;
200  Component * c = (Component *)
201  vacomponent::info (Name, vaBitmap, false, JSON);
202  if (c) delete c;
203 
204 // qDebug() << "slotSymbolFileClicked" << Name << vaBitmap;
205 
206  // check if icon exists, fall back to default
207  QString iconPath = QString(projDir.absFilePath(vaBitmap+".png"));
208  QFile iconFile(iconPath);
209 
210  if(iconFile.exists())
211  {
212  // load bitmap defined on the JSON symbol file
213  iconPixmap->setPixmap(QPixmap(iconPath));
214  }
215  else
216  {
217  QMessageBox::information(this, tr("Info"),
218  tr("Icon not found:\n %1.png").arg(vaBitmap));
219  // default icon
220  iconPixmap->setPixmap(QPixmap(":/bitmaps/editdelete.png"));
221  }
222 }
223 
225 {
226  done(AbortClosing);
227 }
228 
229 //
231 {
232  // build list vaComponentds
233  // hand it down to main app
234 
235  selectedComponents.clear();
236 
237  for(int i = 0; i < fileView->count(); ++i)
238  {
239  QListWidgetItem* item = fileView->item(i);
240 
241  if (item->checkState() == Qt::Checked){
242  QString key = item->text().split("_symbol.json").at(0);
243  QString value = projDir.absoluteFilePath(item->text());
244 
245  qDebug() << "key" << key;
246  qDebug() << "file " << value;
247 
248  selectedComponents[key] = value;
249  }
250  }
251 
252  accept();
253 }
254 
255 
256 /*
257  * Browse for icon image
258  * Save image path to JSON symbol file
259  */
261 {
262 // qDebug() << "slotChangeIcon";
263  QString iconFileName =
264  QFileDialog::getOpenFileName(this,
265  tr("Open File"),
266  QString(projDir.absolutePath()),
267  tr("Icon image (*.png)"));
268 
269  QString newIcon = QFileInfo(iconFileName).baseName();
270 // qDebug() << "icon "<< newIcon;
271 
272  QString filename = fileView->currentItem()->text();
273  filename = projDir.absoluteFilePath(filename);
274 // qDebug() << "for " << filename;
275 
276  // open json
277  // change property
278  // save&close
279  // Try to open the JSON file, can use QScriptEngine for this?
280  //
281  QFile file(filename);
282  QByteArray ba;
283  ba.clear();
284  if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
285  QMessageBox::critical(this, tr("Error"),
286  tr("File not found: %1").arg(filename));
287  }
288  else {
289  QTextStream in(&file);
290  while ( !in.atEnd() )
291  {
292  QString line = in.readLine();
293  if (line.contains("BitmapFile")){
294  QString change =
295  QString(" \"BitmapFile\" : \"%1\",").arg(newIcon);
296  ba.append(change+"\n");
297  }
298  else{
299  ba.append(line+"\n");
300  }
301  }
302  }
303 
304  // write back to the same file, clear it first
305  file.resize(0);
306  file.write(ba);
307  file.close();
308 
309  // update icon
310  this->slotSymbolFileClicked(fileView->currentItem());
311 }
312 
313 bool LoadDialog::eventFilter(QObject *obj, QEvent *event)
314 {
315  if (event->type() == QEvent::KeyPress) {
316  QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
317 // qDebug() << "type" << keyEvent->key() << fileView->count();
318  if (keyEvent->key() == Qt::Key_Up) {
319 
320  fileView->setCurrentRow(std::max(0, fileView->currentRow()-1));
321  this->slotSymbolFileClicked(fileView->currentItem());
322  return true;
323  }
324  if (keyEvent->key() == Qt::Key_Down) {
325  fileView->setCurrentRow(std::min(fileView->count()-1, fileView->currentRow()+1));
326  this->slotSymbolFileClicked(fileView->currentItem());
327  return true;
328  }
329  }
330  return false;
331 }
QLabel * iconPixmap
Definition: loaddialog.h:81
QPushButton * ButtSelectNone
Definition: loaddialog.h:89
void slotSymbolFileClicked(QListWidgetItem *item)
Definition: loaddialog.cpp:189
LoadDialog(QWidget *p=0, const char *n=0, bool modal=true, Qt::WFlags fl=0)
Definition: loaddialog.cpp:41
QDir projDir
Definition: loaddialog.h:63
void loadSelected()
Definition: loaddialog.cpp:230
void initDialog()
Definition: loaddialog.cpp:59
QPushButton * ButtChangeIcon
Definition: loaddialog.h:89
QPushButton * ButtCancel
Definition: loaddialog.h:89
QMap< QString, QString > selectedComponents
Definition: loaddialog.h:65
QPushButton * ButtSelectAll
Definition: loaddialog.h:89
QListWidget * fileView
Definition: loaddialog.h:83
static Element * info(QString &, QString &, bool getNewOne=false, QString filename="")
vacomponent::info is used to either get information or create objects.
void slotChangeIcon()
Definition: loaddialog.cpp:260
QPushButton * ButtOk
Definition: loaddialog.h:89
void slotSelectNone()
Definition: loaddialog.cpp:179
void reject()
Definition: loaddialog.cpp:224
bool eventFilter(QObject *obj, QEvent *event)
Definition: loaddialog.cpp:313
void setApp(QucsApp *a)
Definition: loaddialog.cpp:54
Definition: qucs.h:61
QStringList symbolFiles
Definition: loaddialog.h:62
QucsApp * app
Definition: loaddialog.h:87
void slotSelectAll()
Definition: loaddialog.cpp:169