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 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 <qhbox.h>
23 #include <qlabel.h>
24 #include <qlayout.h>
25 #include <qcheckbox.h>
26 #include <qlineedit.h>
27 #include <qtabwidget.h>
28 #include <qvgroupbox.h>
29 #include <qpushbutton.h>
30 #include <qmessagebox.h>
31 
32 
34  : QDialog(App_, 0, true)
35 {
36  App = App_;
37 
38  QVBoxLayout *all = new QVBoxLayout(this);
39  all->setMargin(5);
40 
41  QVGroupBox *g1 = new QVGroupBox(tr("Text to search for"), this);
42  all->addWidget(g1);
43  SearchEdit = new QLineEdit(g1);
44 
45  ReplaceGroup = new QVGroupBox(tr("Text to replace with"), this);
46  all->addWidget(ReplaceGroup);
47  ReplaceEdit = new QLineEdit(ReplaceGroup);
48 
49  AskBox = new QCheckBox(tr("Ask before replacing"), this);
50  all->addWidget(AskBox);
51 
52  PositionBox = new QCheckBox(tr("From cursor position"), this);
53  all->addWidget(PositionBox);
54  CaseBox = new QCheckBox(tr("Case sensitive"), this);
55  all->addWidget(CaseBox);
56  WordBox = new QCheckBox(tr("Whole words only"), this);
57  all->addWidget(WordBox);
58  BackwardBox = new QCheckBox(tr("Search backwards"), this);
59  all->addWidget(BackwardBox);
60 
61  QHBox *Buttons = new QHBox(this);
62  all->addWidget(Buttons);
63  QPushButton *ButtonSearch = new QPushButton(tr("Search"), Buttons);
64  connect(ButtonSearch, SIGNAL(clicked()), SLOT(slotSearch()));
65  connect(new QPushButton(tr("Cancel"), Buttons),
66  SIGNAL(clicked()), SLOT(reject()));
67 
68  ButtonSearch->setDefault(true);
69 }
70 
72 {
73 }
74 
75 // ---------------------------------------------------------------------
76 void SearchDialog::initSearch(bool replace)
77 {
78  if(replace) {
79  setCaption(tr("Replace Text"));
80  AskBox->setHidden(false);
81  ReplaceGroup->setHidden(false);
82  }
83  else {
84  setCaption(tr("Search Text"));
85  AskBox->hide();
86  ReplaceGroup->hide();
87  }
88 
89  TextDoc *Doc = (TextDoc*)App->DocumentTab->currentPage();
90  ReplaceEdit->clear();
91  SearchEdit->setText(Doc->selectedText());
92  SearchEdit->selectAll();
93 
94  SearchEdit->setFocus();
95  exec();
96 }
97 
98 // ---------------------------------------------------------------------
99 void SearchDialog::searchText(bool fromCursor, int Offset)
100 {
101  TextDoc *Doc = (TextDoc*)App->DocumentTab->currentPage();
102 
103  int Line=0, Column=0, count=0, i;
104  if(fromCursor)
105  Doc->getCursorPosition(&Line, &Column);
106  else if(BackwardBox->isChecked())
107  Line = Doc->paragraphs();
108 
109  if(!BackwardBox->isChecked())
110  Column += Offset;
111 
112  if(SearchEdit->text().isEmpty())
113  return;
114  while(Doc->find(SearchEdit->text(), CaseBox->isChecked(),
115  WordBox->isChecked(), !BackwardBox->isChecked(), &Line, &Column)) {
116 
117  count++;
118  if(AskBox->isHidden()) // search only ?
119  return;
120 
121  i = QMessageBox::Yes;
122  if(AskBox->isChecked()) {
123  i = QMessageBox::information(this,
124  tr("Replace..."), tr("Replace occurrence ?"),
125  QMessageBox::Yes | QMessageBox::Default, QMessageBox::No,
126  QMessageBox::Cancel | QMessageBox::Escape);
127  }
128  switch(i) {
129  case QMessageBox::Yes:
130  Doc->insert(ReplaceEdit->text());
131  Column += ReplaceEdit->text().length();
132  break;
133  case QMessageBox::No:
134  Column += SearchEdit->text().length();
135  break;
136  default: return;
137  }
138  }
139 
140  if(count == 0)
141  QMessageBox::information(this, tr("Search..."),
142  tr("Search string not found!"));
143  else
144  if(!AskBox->isHidden()) // replace ?
145  if(!AskBox->isChecked()) // only with that, "count" has correct number !!!
146  QMessageBox::information(this, tr("Replace..."),
147  tr("Replaced %1 occurrences!").arg(count));
148 }
149 
150 // ---------------------------------------------------------------------
151 void SearchDialog::slotSearch()
152 {
153  accept();
154  searchText(PositionBox->isChecked(), 0);
155 }