File: searchdialog.cpp

package info (click to toggle)
qucs 0.0.15-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 31,208 kB
  • ctags: 25,891
  • sloc: cpp: 202,101; sh: 6,422; ansic: 5,779; xml: 4,213; perl: 3,251; yacc: 2,850; lex: 2,088; makefile: 1,416
file content (149 lines) | stat: -rw-r--r-- 4,673 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/***************************************************************************
                               searchdialog.cpp
                              ------------------
    begin                : Sat Jun 11 2005
    copyright            : (C) 2005 by Michael Margraf
    email                : michael.margraf@alumni.tu-berlin.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "searchdialog.h"
#include "qucslib.h"

#include <qlayout.h>
#include <qhbox.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qdir.h>
#include <qfile.h>
#include <qlistbox.h>
#include <qcombobox.h>


SearchDialog::SearchDialog(QucsLib *parent)
                     : QDialog(parent, 0, false, Qt::WDestructiveClose)
{
  ParentDialog = parent;

  all = new QVBoxLayout(this);
  all->setMargin(5);
  all->setSpacing(5);

  all->addWidget(
    new QLabel(tr("The search result contains all components whose\n"
                  "name contains the search string. All libraries\n"
                  "are included in the search."), this) );

  QHBox *h1 = new QHBox(this);
  all->addWidget(h1);

  new QLabel(tr("Search string:"), h1);
  SearchEdit = new QLineEdit(h1);
  connect(SearchEdit, SIGNAL(returnPressed()), SLOT(slotSearch()));

  QHBox *h2 = new QHBox(this);
  all->addWidget(h2);

  h2->setStretchFactor(new QWidget(h2), 5); // stretchable placeholder

  QPushButton *ButtonSearch = new QPushButton(tr("Search"), h2);
  connect(ButtonSearch, SIGNAL(clicked()), SLOT(slotSearch()));
  QPushButton *ButtonClose = new QPushButton(tr("Close"), h2);
  connect(ButtonClose, SIGNAL(clicked()), SLOT(slotClose()));
  ButtonSearch->setFocus();

  SearchEdit->setFocus();
}

SearchDialog::~SearchDialog()
{
  delete all;
}

// ************************************************************
void SearchDialog::slotClose()
{
  reject();
}

// ************************************************************
void SearchDialog::slotSearch()
{
  if(SearchEdit->text().isEmpty()) {
    reject();
    return;
  }

  bool findComponent = false;
  QDir LibDir(QucsSettings.LibDir);
  QStringList LibFiles = LibDir.entryList("*.lib", QDir::Files, QDir::Name);

  QFile File;
  QTextStream ReadWhole;
  QString LibraryString, LibName, CompName;
  QStringList::iterator it;
  int Start, End, NameStart, NameEnd;
  for(it = LibFiles.begin(); it != LibFiles.end(); it++) { // all library files
    File.setName(QucsSettings.LibDir + (*it));
    if(!File.open(IO_ReadOnly))  continue;

    ReadWhole.setDevice(&File);
    LibraryString = ReadWhole.read();
    File.close();

    Start = LibraryString.find("<Qucs Library ");
    if(Start < 0)  continue;
    End = LibraryString.find('>', Start);
    if(End < 0)  continue;
    LibName = LibraryString.mid(Start, End-Start).section('"', 1, 1);

    // check all components of the current library
    while((Start=LibraryString.find("\n<Component ", Start)) > 0) {
      Start++;
      NameStart = Start + 11;
      NameEnd = LibraryString.find('>', NameStart);
      if(NameEnd < 0)  continue;
      CompName = LibraryString.mid(NameStart, NameEnd-NameStart);

      End = LibraryString.find("\n</Component>", NameEnd);
      if(End < 0)  continue;
      End += 13;

      // does search criterion match ?
      if(CompName.find(SearchEdit->text()) >= 0) {
        if(!findComponent) {
          ParentDialog->DefaultSymbol = "";
          ParentDialog->CompList->clear();
          ParentDialog->LibraryComps.clear();
        }
        findComponent = true;
        ParentDialog->CompList->insertItem(CompName);
        ParentDialog->LibraryComps.append(
			LibName+'\n'+LibraryString.mid(Start, End-Start));
      }
      Start = End;
    }
  }

  if(findComponent) {
    End = ParentDialog->Library->count();
    if(ParentDialog->Library->text(End-1) != tr("Search result"))
      ParentDialog->Library->insertItem(tr("Search result"));
    ParentDialog->Library->setCurrentItem(End);
    reject();
  }
  else  accept();
}