My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
searchdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  searchdialog.cpp
3  ------------------
4  begin : Sat Jun 11 2005
5  copyright : (C) 2005 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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include "searchdialog.h"
23 #include "qucslib.h"
24 
25 #include <qlayout.h>
26 #include <qhbox.h>
27 #include <qpushbutton.h>
28 #include <qlabel.h>
29 #include <qlineedit.h>
30 #include <qdir.h>
31 #include <qfile.h>
32 #include <qlistbox.h>
33 #include <qcombobox.h>
34 
35 
37  : QDialog(parent, 0, false, Qt::WDestructiveClose)
38 {
39  ParentDialog = parent;
40 
41  all = new QVBoxLayout(this);
42  all->setMargin(5);
43  all->setSpacing(5);
44 
45  all->addWidget(
46  new QLabel(tr("The search result contains all components whose\n"
47  "name contains the search string. All libraries\n"
48  "are included in the search."), this) );
49 
50  QHBox *h1 = new QHBox(this);
51  all->addWidget(h1);
52 
53  new QLabel(tr("Search string:"), h1);
54  SearchEdit = new QLineEdit(h1);
55  connect(SearchEdit, SIGNAL(returnPressed()), SLOT(slotSearch()));
56 
57  QHBox *h2 = new QHBox(this);
58  all->addWidget(h2);
59 
60  h2->setStretchFactor(new QWidget(h2), 5); // stretchable placeholder
61 
62  QPushButton *ButtonSearch = new QPushButton(tr("Search"), h2);
63  connect(ButtonSearch, SIGNAL(clicked()), SLOT(slotSearch()));
64  QPushButton *ButtonClose = new QPushButton(tr("Close"), h2);
65  connect(ButtonClose, SIGNAL(clicked()), SLOT(slotClose()));
66  ButtonSearch->setFocus();
67 
68  SearchEdit->setFocus();
69 }
70 
72 {
73  delete all;
74 }
75 
76 // ************************************************************
77 void SearchDialog::slotClose()
78 {
79  reject();
80 }
81 
82 // ************************************************************
83 void SearchDialog::slotSearch()
84 {
85  if(SearchEdit->text().isEmpty()) {
86  reject();
87  return;
88  }
89 
90  bool findComponent = false;
91  QDir LibDir(QucsSettings.LibDir);
92  QStringList LibFiles = LibDir.entryList("*.lib", QDir::Files, QDir::Name);
93 
94  QFile File;
95  QTextStream ReadWhole;
96  QString LibraryString, LibName, CompName;
97  QStringList::iterator it;
98  int Start, End, NameStart, NameEnd;
99  for(it = LibFiles.begin(); it != LibFiles.end(); it++) { // all library files
100  File.setName(QucsSettings.LibDir + (*it));
101  if(!File.open(IO_ReadOnly)) continue;
102 
103  ReadWhole.setDevice(&File);
104  LibraryString = ReadWhole.read();
105  File.close();
106 
107  Start = LibraryString.find("<Qucs Library ");
108  if(Start < 0) continue;
109  End = LibraryString.find('>', Start);
110  if(End < 0) continue;
111  LibName = LibraryString.mid(Start, End-Start).section('"', 1, 1);
112 
113  // check all components of the current library
114  while((Start=LibraryString.find("\n<Component ", Start)) > 0) {
115  Start++;
116  NameStart = Start + 11;
117  NameEnd = LibraryString.find('>', NameStart);
118  if(NameEnd < 0) continue;
119  CompName = LibraryString.mid(NameStart, NameEnd-NameStart);
120 
121  End = LibraryString.find("\n</Component>", NameEnd);
122  if(End < 0) continue;
123  End += 13;
124 
125  // does search criterion match ?
126  if(CompName.find(SearchEdit->text()) >= 0) {
127  if(!findComponent) {
128  ParentDialog->DefaultSymbol = "";
129  ParentDialog->CompList->clear();
130  ParentDialog->LibraryComps.clear();
131  }
132  findComponent = true;
133  ParentDialog->CompList->insertItem(CompName);
134  ParentDialog->LibraryComps.append(
135  LibName+'\n'+LibraryString.mid(Start, End-Start));
136  }
137  Start = End;
138  }
139  }
140 
141  if(findComponent) {
142  End = ParentDialog->Library->count();
143  if(ParentDialog->Library->text(End-1) != tr("Search result"))
144  ParentDialog->Library->insertItem(tr("Search result"));
145  ParentDialog->Library->setCurrentItem(End);
146  reject();
147  }
148  else accept();
149 }