/* * Alabastra Project - C++ Editor writed with QT Library V.4 * Copyright (C) Igor Maculan - geocronos@gmail.com Marco Buoncristiano - marco.buoncristiano@gmail.com * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "AlSearch.h" #include AlSearch::AlSearch(QWidget *parent,AlTextEdit *a_txt):QDialog(parent){ ui.setupUi(this); if ( a_txt!=NULL) { txt=a_txt; connectSignal(); } else close(); }; void AlSearch::setAlTextEdit(AlTextEdit *a_txt){ if (a_txt!=NULL) txt=a_txt; } bool AlSearch::compare(AlTextEdit *a_txt){ return a_txt==txt; }; void AlSearch::connectSignal(){ connect (ui.btnNext,SIGNAL(clicked()),this,SLOT(next())); connect (ui.btnPrev,SIGNAL(clicked()),this,SLOT(prev())); connect (ui.btnReplace,SIGNAL(clicked()),this,SLOT(replace())); connect (ui.btnReplaceAll,SIGNAL(clicked()),this,SLOT(replaceAll())); connect (ui.chkInSel,SIGNAL(stateChanged(int)),this,SLOT(setSelectionCursor(int))); }; void AlSearch::prev(){ search(ui.edtSearch->text(),true); }; void AlSearch::next(){ search(ui.edtSearch->text()); }; void AlSearch::setSelectionCursor(int) { if (ui.chkInSel->isChecked() ) { if (txt->textCursor().hasSelection()) { selCursor = txt->textCursor(); qCur = selCursor; } else ui.chkInSel->setChecked(false); } }; void AlSearch::search(QString src,bool back){ if (ui.chkInSel->isChecked() && selCursor.hasSelection()){ //Se c'e' qualcosa di selezionato if (ui.chkRegExp->isChecked()) if (back) qCur=txt->document()->find(QRegExp(src),qCur,QTextDocument::FindBackward); else qCur=txt->document()->find(QRegExp(src),qCur); else if (back) qCur=txt->document()->find(src,qCur,QTextDocument::FindBackward); else qCur=txt->document()->find(src,qCur); if (back){ if (qCur.position()>selCursor.selectionStart()) txt->setTextCursor(qCur); else{ qCur.setPosition(selCursor.selectionEnd()); if (selCursor.selectedText()!=ui.edtSearch->text()) search(src,back); } } else{ if (qCur.position()setTextCursor(qCur); else{ qCur.setPosition(selCursor.selectionStart()); if (selCursor.selectedText()!=ui.edtSearch->text()) search(src,back); } } } if (!ui.chkInSel->isChecked()) if (back) txt->find(src,QTextDocument::FindBackward); else txt->find(src); }; void AlSearch::replace(){ if (!txt->textCursor().hasSelection()) next(); if (txt->textCursor().hasSelection()){ txt->textCursor().insertText(ui.edtReplace->text()); next(); } }; void AlSearch::replaceAll(){ QTextCursor q,curSave; curSave=txt->textCursor(); q.setPosition(0); txt->setTextCursor(q); next(); int i=0; while (txt->textCursor().hasSelection()){ txt->textCursor().insertText(ui.edtReplace->text()); next(); i++; } txt->setTextCursor(curSave); QMessageBox::information(this,tr("Sostituisci Tutto"),tr("Sostituzioni effettuate:%1").arg(i)); };