Qucs-GUI  0.0.18
 All Data Structures 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 Apr 01 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 "searchdialog.h"
19 #include "textdoc.h"
20 #include "qucs.h"
21 
22 #include <QLabel>
23 #include <QCheckBox>
24 #include <QLineEdit>
25 #include <QGroupBox>
26 #include <QPushButton>
27 #include <QMessageBox>
28 #include <QVBoxLayout>
29 #include <QHBoxLayout>
30 #include <QDebug>
31 
32 
34  : QDialog(App_, 0, true)
35 {
36  App = App_;
37 
38  QVBoxLayout *all = new QVBoxLayout(this);
39  all->setMargin(5);
40 
41  QGroupBox *g1 = new QGroupBox(tr("Text to search for"));
42  QVBoxLayout *vbox1 = new QVBoxLayout;
43  SearchEdit = new QLineEdit();
44  vbox1->addWidget(SearchEdit);
45  g1->setLayout(vbox1);
46 
47  all->addWidget(g1);
48 
49  ReplaceGroup = new QGroupBox(tr("Text to replace with"));
50  QVBoxLayout *vbox2 = new QVBoxLayout;
51  ReplaceEdit = new QLineEdit();
52  vbox2->addWidget(ReplaceEdit);
53  ReplaceGroup->setLayout(vbox2);
54 
55  all->addWidget(ReplaceGroup);
56 
57  AskBox = new QCheckBox(tr("Ask before replacing"));
58  all->addWidget(AskBox);
59 
60  PositionBox = new QCheckBox(tr("From cursor position"));
61  all->addWidget(PositionBox);
62  CaseBox = new QCheckBox(tr("Case sensitive"));
63  all->addWidget(CaseBox);
64  WordBox = new QCheckBox(tr("Whole words only"));
65  all->addWidget(WordBox);
66  BackwardBox = new QCheckBox(tr("Search backwards"));
67  all->addWidget(BackwardBox);
68 
69  QHBoxLayout *Buttons = new QHBoxLayout();
70  all->addLayout(Buttons);
71  QPushButton *ButtonSearch = new QPushButton(tr("Search"));
72  Buttons->addWidget(ButtonSearch);
73  QPushButton *ButtonCancel = new QPushButton(tr("Cancel"));
74  Buttons->addWidget(ButtonCancel);
75  connect(ButtonSearch, SIGNAL(clicked()), SLOT(slotSearch()));
76  connect(ButtonCancel, SIGNAL(clicked()), SLOT(reject()));
77 
78  ButtonSearch->setDefault(true);
79 }
80 
82 {
83 }
84 
85 // ---------------------------------------------------------------------
86 void SearchDialog::initSearch(bool replace)
87 {
88  if(replace) {
89  setWindowTitle(tr("Replace Text"));
90  AskBox->setHidden(false);
91  ReplaceGroup->setHidden(false);
92  }
93  else {
94  setWindowTitle(tr("Search Text"));
95  AskBox->hide();
96  ReplaceGroup->hide();
97  }
98 
99  TextDoc *Doc = (TextDoc*)App->DocumentTab->currentPage();
100  ReplaceEdit->clear();
101  SearchEdit->setText(Doc->textCursor().selectedText());
102  SearchEdit->selectAll();
103 
104  SearchEdit->setFocus();
105  exec();
106 }
107 
108 // ---------------------------------------------------------------------
109 void SearchDialog::searchText(bool fromCursor, int Offset)
110 {
111  TextDoc *Doc = (TextDoc*)App->DocumentTab->currentPage();
112 
113  //FIXME : the find already searches from cursor
114  int Line=0, Column=0, count=0, i;
115  if(fromCursor) {
116  Line = Doc->textCursor().blockNumber();
117  Column = Doc->textCursor().columnNumber();
118  }
119  else if(BackwardBox->isChecked())
120  Line = Doc->document()->blockCount();
121 
122  if(!BackwardBox->isChecked())
123  Column += Offset;
124 
125  if(SearchEdit->text().isEmpty())
126  return;
127 
128  QFlag findFlags = 0;
129  if (CaseBox->isChecked())
130  findFlags = QTextDocument::FindCaseSensitively;
131  if (WordBox->isChecked())
132  findFlags = findFlags | QTextDocument::FindWholeWords;
133  if (BackwardBox->isChecked())
134  findFlags = findFlags | QTextDocument::FindBackward;
135 
136  while ( Doc->find(SearchEdit->text(), findFlags)) {
137 
138  count++;
139  if(AskBox->isHidden()) // search only ?
140  return;
141 
142  i = QMessageBox::Yes;
143  if(AskBox->isChecked()) {
144  i = QMessageBox::information(this,
145  tr("Replace..."), tr("Replace occurrence ?"),
146  QMessageBox::Yes | QMessageBox::Default, QMessageBox::No,
147  QMessageBox::Cancel | QMessageBox::Escape);
148  }
149 
150  switch(i) {
151  case QMessageBox::Yes:
152  Doc->insertPlainText(ReplaceEdit->text());
153  Column += ReplaceEdit->text().length();
154  break;
155  case QMessageBox::No:
156  Column += SearchEdit->text().length();
157  break;
158  default: return;
159  }
160  }
161 
162  if(count == 0)
163  QMessageBox::information(this, tr("Search..."),
164  tr("Search string not found!"));
165  else
166  if(!AskBox->isHidden()) // replace ?
167  if(!AskBox->isChecked()) // only with that, "count" has correct number !!!
168  QMessageBox::information(this, tr("Replace..."),
169  tr("Replaced %1 occurrences!").arg(count));
170 }
171 
172 // ---------------------------------------------------------------------
174 {
175  accept();
176  searchText(PositionBox->isChecked(), 0);
177 }
Definition of the TextDoc class.
SearchDialog(QucsApp *)
QLineEdit * ReplaceEdit
Definition: searchdialog.h:44
QLineEdit * SearchEdit
Definition: searchdialog.h:44
QCheckBox * WordBox
Definition: searchdialog.h:45
QGroupBox * ReplaceGroup
Definition: searchdialog.h:46
QucsApp * App
Definition: searchdialog.h:43
QCheckBox * BackwardBox
Definition: searchdialog.h:45
void searchText(bool, int)
Definition: element.h:48
QCheckBox * CaseBox
Definition: searchdialog.h:45
void initSearch(bool replace=false)
QTabWidget * DocumentTab
Definition: qucs.h:164
QCheckBox * PositionBox
Definition: searchdialog.h:45
QCheckBox * AskBox
Definition: searchdialog.h:45
The TextDoc class definition.
Definition: textdoc.h:49
Definition: qucs.h:61