scide: cancel ScRequests when recompiling the class library
[supercollider.git] / editors / sc-ide / widgets / find_replace_tool.cpp
blobb42bd88b1cdbfe0456d9ab63ad49769cca08f701
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program 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 of the License, or
9 (at your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "find_replace_tool.hpp"
22 #include "code_editor/editor.hpp"
24 #include <QApplication>
26 namespace ScIDE {
28 TextFindReplacePanel::TextFindReplacePanel( QWidget * parent ):
29 QWidget(parent),
30 mMode((Mode) 0), // a hack so that first setMode() works
31 mEditor(0),
32 mSearchPosition(-1)
34 mFindField = new QLineEdit;
35 mReplaceField = new QLineEdit;
37 mNextBtn = new QPushButton(tr("Next"));
38 mPrevBtn = new QPushButton(tr("Previous"));
39 mFindAllBtn = new QPushButton(tr("Find All"));
40 mReplaceBtn = new QPushButton(tr("Replace"));
41 mReplaceAllBtn = new QPushButton(tr("Replace All"));
43 mOptionsBtn = new QPushButton(tr("Options"));
44 QMenu *optMenu = new QMenu(this);
45 mMatchCaseAction = optMenu->addAction(tr("Match Case"));
46 mMatchCaseAction->setCheckable(true);
47 mRegExpAction = optMenu->addAction(tr("Regular Expression"));
48 mRegExpAction->setCheckable(true);
49 mWholeWordAction = optMenu->addAction(tr("Whole Words"));
50 mWholeWordAction->setCheckable(true);
51 mOptionsBtn->setMenu(optMenu);
53 mFindLabel = new QLabel(tr("Find:"));
54 mFindLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
55 mReplaceLabel = new QLabel(tr("Replace:"));
56 mReplaceLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
58 mGrid = new QGridLayout();
59 mGrid->setContentsMargins(2,2,2,2);
60 mGrid->addWidget(mFindLabel, 0, 0);
61 mGrid->addWidget(mFindField, 0, 1);
62 mGrid->addWidget(mNextBtn, 0, 2);
63 mGrid->addWidget(mPrevBtn, 0, 3);
64 mGrid->addWidget(mFindAllBtn, 0, 4);
65 mGrid->addWidget(mOptionsBtn, 0, 5);
66 mGrid->addWidget(mReplaceLabel, 1, 0);
67 mGrid->addWidget(mReplaceField, 1, 1);
68 mGrid->addWidget(mReplaceBtn, 1, 2);
69 mGrid->addWidget(mReplaceAllBtn, 1, 3);
70 setLayout(mGrid);
72 setMode(Find);
74 setFocusProxy(mFindField);
75 QWidget::setTabOrder(mFindField, mReplaceField);
76 mFindField->installEventFilter(this);
78 connect(mNextBtn, SIGNAL(clicked()), this, SLOT(findNext()));
79 connect(mPrevBtn, SIGNAL(clicked()), this, SLOT(findPrevious()));
80 connect(mFindAllBtn, SIGNAL(clicked()), this, SLOT(findAll()));
81 connect(mReplaceBtn, SIGNAL(clicked()), this, SLOT(replace()));
82 connect(mReplaceAllBtn, SIGNAL(clicked()), this, SLOT(replaceAll()));
83 connect(mFindField, SIGNAL(returnPressed()), this, SLOT(onFindFieldReturn()));
84 connect(mFindField, SIGNAL(textChanged(QString)), this, SLOT(onFindFieldTextChanged()));
85 connect(mReplaceField, SIGNAL(returnPressed()), this, SLOT(replace()));
88 void TextFindReplacePanel::setMode( Mode mode )
90 if (mode == mMode) return;
92 mMode = mode;
94 bool visible = mMode == Replace;
95 mReplaceLabel->setVisible(visible);
96 mReplaceField->setVisible(visible);
97 mReplaceBtn->setVisible(visible);
98 mReplaceAllBtn->setVisible(visible);
101 void TextFindReplacePanel::initiate()
103 mSearchPosition = -1;
105 if(mEditor)
107 QTextCursor c( mEditor->textCursor() );
108 if(c.hasSelection() &&
109 c.document()->findBlock(c.selectionStart()) ==
110 c.document()->findBlock(c.selectionEnd()))
112 mFindField->setText(c.selectedText());
113 mReplaceField->clear();
117 mFindField->selectAll();
118 findAll();
121 QRegExp TextFindReplacePanel::regexp()
123 QRegExp expr(findString());
124 expr.setPatternSyntax(asRegExp() ? QRegExp::RegExp : QRegExp::FixedString);
125 expr.setCaseSensitivity(matchCase() ? Qt::CaseSensitive : Qt::CaseInsensitive);
126 return expr;
129 QTextDocument::FindFlags TextFindReplacePanel::flags()
131 QTextDocument::FindFlags f;
132 if(wholeWords())
133 f |= QTextDocument::FindWholeWords;
134 return f;
137 void TextFindReplacePanel::findNext()
139 find(false);
142 void TextFindReplacePanel::findPrevious()
144 find(true);
147 void TextFindReplacePanel::onFindFieldReturn()
149 find (QApplication::keyboardModifiers() & Qt::ShiftModifier);
152 void TextFindReplacePanel::onFindFieldTextChanged()
154 // Incremental search
156 if (!mEditor) return;
158 QRegExp expr(regexp());
159 QTextDocument::FindFlags flagz(flags());
161 if (mSearchPosition == -1)
162 mSearchPosition = mEditor->textCursor().selectionStart();
164 int count = mEditor->findAll(expr, flagz);
166 QTextCursor searchCursor(mEditor->textDocument());
167 searchCursor.setPosition(mSearchPosition);
169 if (expr.isEmpty()) {
170 mEditor->setTextCursor(searchCursor);
171 } else if (count) {
172 mEditor->setTextCursor(searchCursor);
173 mEditor->find(expr, flagz);
177 bool TextFindReplacePanel::eventFilter (QObject *obj, QEvent *ev)
179 if (obj == mFindField && ev->type() == QEvent::FocusOut)
180 mSearchPosition = -1;
182 return QWidget::eventFilter(obj,ev);
185 void TextFindReplacePanel::find (bool backwards)
187 // Non incremental search!
189 if (!mEditor) return;
191 QRegExp expr = regexp();
192 if (expr.isEmpty()) return;
194 QTextDocument::FindFlags opt = flags();
195 if (backwards)
196 opt |= QTextDocument::FindBackward;
198 mEditor->find(expr, opt);
200 // This was not incremental search, so reset search position
201 mSearchPosition = -1;
204 void TextFindReplacePanel::findAll()
206 if (!mEditor) return;
208 // NOTE: empty expression removes any search highlighting
209 mEditor->findAll(regexp(), flags());
212 void TextFindReplacePanel::replace()
214 if (!mEditor) return;
216 QRegExp expr = regexp();
217 if (expr.isEmpty()) return;
219 mEditor->replace(expr, replaceString(), flags());
221 mSearchPosition = -1;
224 void TextFindReplacePanel::replaceAll()
226 if (!mEditor) return;
228 QRegExp expr = regexp();
229 if (expr.isEmpty()) return;
231 mEditor->replaceAll(expr, replaceString(), flags());
233 mSearchPosition = -1;
236 } // namespace ScIDE