📄 keditcl2.cpp
字号:
/* $Id: keditcl2.cpp,v 1.1 2003/09/08 19:42:08 jasonk Exp $ kedit, a simple text editor for the KDE project This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. KEdit, simple editor class, hacked version of the original by */#include "keditcl.h"#include <klocale.h>#include <kapp.h>////////////////////////////////////////////////////////////////////////////// Find Methods// void KEdit::Search(){ if (replace_dialog) if (replace_dialog->isVisible()) replace_dialog->hide(); if(!srchdialog){ srchdialog = new KEdSrch(0, "searchdialog"); connect(srchdialog,SIGNAL(search_signal()),this,SLOT(search_slot())); connect(srchdialog,SIGNAL(search_done_signal()),this,SLOT(searchdone_slot())); } // If we already searched / replaced something before make sure it shows // up in the find dialog line-edit. QString string; string = srchdialog->getText(); if(string.isEmpty()) srchdialog->setText(pattern); this->deselect(); last_search = NONE; this->clearFocus(); QPoint point = this->mapToGlobal (QPoint (0,0)); QRect pos = this->geometry(); srchdialog->setGeometry(point.x() + pos.width()/2 - srchdialog->width()/2, point.y() + pos.height()/2 - srchdialog->height()/2, srchdialog->width(), srchdialog->height()); srchdialog->show(); srchdialog->result();}void KEdit::search_slot(){ int line, col; if (!srchdialog) return; QString to_find_string = srchdialog->getText(); getCursorPosition(&line,&col); // srchdialog->get_direction() is true if searching backward if (last_search != NONE && srchdialog->get_direction()){ col = col - pattern.length() - 1 ; }again: int result = doSearch(to_find_string, srchdialog->case_sensitive(), FALSE, (!srchdialog->get_direction()),line,col); if(result == 0){ if(!srchdialog->get_direction()){ // forward search int query = QMessageBox::information( srchdialog, klocale->translate("Find"), klocale->translate("End of document reached.\n"\ "Continue from the beginning?"), klocale->translate("Yes"), klocale->translate("No"), "", 0,1); if (query == 0){ line = 0; col = 0; goto again; } } else{ //backward search int query = QMessageBox::information( srchdialog, klocale->translate("Find"), klocale->translate("Beginning of document reached.\n"\ "Continue from the end?"), klocale->translate("Yes"), klocale->translate("No"), "", 0,1); if (query == 0){ QString string = textLine( numLines() - 1 ); line = numLines() - 1; col = string.length(); last_search = BACKWARD; goto again; } } } else{ emit CursorPositionChanged(); }}void KEdit::searchdone_slot(){ if (!srchdialog) return; srchdialog->hide(); this->setFocus(); last_search = NONE;}int KEdit::doSearch(QString s_pattern, bool case_sensitive, bool wildcard, bool forward, int line, int col){ (void) wildcard; // reserved for possible extension to regex int i, length; int pos = -1; if(forward){ QString string; for(i = line; i < numLines(); i++) { string = textLine(i); pos = string.find(s_pattern, i == line ? col : 0, case_sensitive); if( pos != -1){ length = s_pattern.length(); setCursorPosition(i,pos,FALSE); for(int l = 0 ; l < length; l++){ cursorRight(TRUE); } setCursorPosition( i , pos + length, TRUE ); pattern = s_pattern; last_search = FORWARD; return 1; } } } else{ // searching backwards QString string; for(i = line; i >= 0; i--) { string = textLine(i); int line_length = string.length(); pos = string.findRev(s_pattern, line == i ? col : line_length , case_sensitive); if (pos != -1){ length = s_pattern.length(); if( ! (line == i && pos > col ) ){ setCursorPosition(i ,pos ,FALSE ); for(int l = 0 ; l < length; l++){ cursorRight(TRUE); } setCursorPosition(i ,pos + length ,TRUE ); pattern = s_pattern; last_search = BACKWARD; return 1; } } } } return 0;}int KEdit::repeatSearch() { if(!srchdialog) return 0; if(pattern.isEmpty()) // there wasn't a previous search return 0; search_slot(); this->setFocus(); return 1;}////////////////////////////////////////////////////////////////////////////// Replace Methods//void KEdit::Replace(){ if (srchdialog) if (srchdialog->isVisible()) srchdialog->hide(); if (!replace_dialog){ replace_dialog = new KEdReplace(0, "replace_dialog"); connect(replace_dialog,SIGNAL(find_signal()),this,SLOT(replace_search_slot())); connect(replace_dialog,SIGNAL(replace_signal()),this,SLOT(replace_slot())); connect(replace_dialog,SIGNAL(replace_all_signal()),this,SLOT(replace_all_slot())); connect(replace_dialog,SIGNAL(replace_done_signal()),this,SLOT(replacedone_slot())); } QString string = replace_dialog->getText(); if(string.isEmpty()) replace_dialog->setText(pattern); this->deselect(); last_replace = NONE; this->clearFocus(); QPoint point = this->mapToGlobal (QPoint (0,0)); QRect pos = this->geometry(); replace_dialog->setGeometry(point.x() + pos.width()/2 - replace_dialog->width()/2, point.y() + pos.height()/2 - replace_dialog->height()/2, replace_dialog->width(), replace_dialog->height()); replace_dialog->show(); replace_dialog->result();}void KEdit::replace_slot(){ if (!replace_dialog) return; if(!can_replace){ QApplication::beep(); return; } int line,col, length; QString string = replace_dialog->getReplaceText(); length = string.length(); this->cut(); getCursorPosition(&line,&col); insertAt(string,line,col); setModified(); can_replace = FALSE; setCursorPosition(line,col); for( int k = 0; k < length; k++){ cursorRight(TRUE); }}void KEdit::replace_all_slot(){ if (!replace_dialog) return; QString to_find_string = replace_dialog->getText(); getCursorPosition(&replace_all_line,&replace_all_col); // replace_dialog->get_direction() is true if searching backward if (last_replace != NONE && replace_dialog->get_direction()){ replace_all_col = replace_all_col - pattern.length() - 1 ; } deselect();again: setAutoUpdate(FALSE); int result = 1; while(result){ result = doReplace(to_find_string, replace_dialog->case_sensitive(), FALSE, (!replace_dialog->get_direction()), replace_all_line,replace_all_col,TRUE); } setAutoUpdate(TRUE); update(); if(!replace_dialog->get_direction()){ // forward search int query = QMessageBox::information( replace_dialog, klocale->translate("Find"), klocale->translate("End of document reached.\n"\ "Continue from the beginning?"), klocale->translate("Yes"), klocale->translate("No"), "", 0,1); if (query == 0){ replace_all_line = 0; replace_all_col = 0; goto again; } } else{ //backward search int query = QMessageBox::information( replace_dialog, klocale->translate("Find"), klocale->translate("Beginning of document reached.\n"\ "Continue from the end?"), klocale->translate("Yes"), klocale->translate("No"), "", 0,1); if (query == 0){ QString string = textLine( numLines() - 1 ); replace_all_line = numLines() - 1; replace_all_col = string.length(); last_replace = BACKWARD; goto again; } } emit CursorPositionChanged(); }void KEdit::replace_search_slot(){ int line, col; if (!replace_dialog) return; QString to_find_string = replace_dialog->getText(); getCursorPosition(&line,&col); // replace_dialog->get_direction() is true if searching backward //printf("col %d length %d\n",col, pattern.length()); if (last_replace != NONE && replace_dialog->get_direction()){ col = col - pattern.length() -1; if (col < 0 ) { if(line !=0){ col = strlen(textLine(line - 1)); line --; } else{ int query = QMessageBox::information( replace_dialog, klocale->translate("Replace"), klocale->translate("Beginning of document reached.\n"\ "Continue from the end?"), klocale->translate("Yes"), klocale->translate("No"), "", 0,1); if (query == 0){ QString string = textLine( numLines() - 1 ); line = numLines() - 1; col = string.length(); last_replace = BACKWARD; } } } }again: // printf("Col %d \n",col); int result = doReplace(to_find_string, replace_dialog->case_sensitive(), FALSE, (!replace_dialog->get_direction()), line, col, FALSE ); if(result == 0){ if(!replace_dialog->get_direction()){ // forward search int query = QMessageBox::information( replace_dialog, klocale->translate("Replace"), klocale->translate("End of document reached.\n"\ "Continue from the beginning?"), klocale->translate("Yes"), klocale->translate("No"), "", 0,1); if (query == 0){ line = 0; col = 0; goto again;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -