PageRenderTime 143ms CodeModel.GetById 12ms RepoModel.GetById 3ms app.codeStats 0ms

/tags/KFEM_0_0_0/kfem/kfem/keditcl2.cpp

#
C++ | 1064 lines | 648 code | 334 blank | 82 comment | 97 complexity | 7f46bfe4d88ede520a2d7ead9a1898f6 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * * NOTE THIS CLASS IS NOT THE SAME AS THE ONE IN THE KDEUI LIB
  3. * The difference is that this one uses KFileDialog instead of
  4. * QFileDialog. So don't remove this class with the idea in mind to
  5. * link against kdeui.
  6. * Bernd
  7. *
  8. $Id: keditcl2.cpp 4 2000-01-13 16:41:48Z prudhomm $
  9. kedit, a simple text editor for the KDE project
  10. Copyright (C) 1997 Bernd Johannes Wuebben
  11. wuebben@math.cornell.edu
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. KEdit, simple editor class, hacked version of the original by
  24. */
  25. #include "keditcl.h"
  26. #include <klocale.h>
  27. #include <kmsgbox.h>
  28. #include <kapp.h>
  29. //////////////////////////////////////////////////////////////////////////
  30. //
  31. // Find Methods
  32. //
  33. void KEdit::Search(){
  34. if (replace_dialog)
  35. if (replace_dialog->isVisible())
  36. replace_dialog->hide();
  37. if(!srchdialog){
  38. srchdialog = new KEdSrch(0, "searchdialog");
  39. connect(srchdialog,SIGNAL(search_signal()),this,SLOT(search_slot()));
  40. connect(srchdialog,SIGNAL(search_done_signal()),this,SLOT(searchdone_slot()));
  41. }
  42. // If we already searched / replaced something before make sure it shows
  43. // up in the find dialog line-edit.
  44. QString string;
  45. string = srchdialog->getText();
  46. if(string.isEmpty())
  47. srchdialog->setText(pattern);
  48. this->deselect();
  49. last_search = NONE;
  50. this->clearFocus();
  51. QPoint point = this->mapToGlobal (QPoint (0,0));
  52. QRect pos = this->geometry();
  53. srchdialog->setGeometry(point.x() + pos.width()/2 - srchdialog->width()/2,
  54. point.y() + pos.height()/2 - srchdialog->height()/2,
  55. srchdialog->width(),
  56. srchdialog->height());
  57. srchdialog->show();
  58. srchdialog->result();
  59. }
  60. void KEdit::search_slot(){
  61. int line, col;
  62. if (!srchdialog)
  63. return;
  64. QString to_find_string = srchdialog->getText();
  65. getCursorPosition(&line,&col);
  66. // srchdialog->get_direction() is true if searching backward
  67. if (last_search != NONE && srchdialog->get_direction()){
  68. col = col - pattern.length() - 1 ;
  69. }
  70. again:
  71. int result = doSearch(to_find_string, srchdialog->case_sensitive(),
  72. FALSE, (!srchdialog->get_direction()),line,col);
  73. if(result == 0){
  74. if(!srchdialog->get_direction()){ // forward search
  75. int query = QMessageBox::information(
  76. this,
  77. klocale->translate("Find"),
  78. klocale->translate("End of document reached.\n"\
  79. "Continue from the beginning?"),
  80. klocale->translate("Yes"),
  81. klocale->translate("No"),
  82. "",
  83. 0,1);
  84. if (query == 0){
  85. line = 0;
  86. col = 0;
  87. goto again;
  88. }
  89. }
  90. else{ //backward search
  91. int query = QMessageBox::information(
  92. this,
  93. klocale->translate("Find"),
  94. klocale->translate("Beginning of document reached.\n"\
  95. "Continue from the end?"),
  96. klocale->translate("Yes"),
  97. klocale->translate("No"),
  98. "",
  99. 0,1);
  100. if (query == 0){
  101. QString string = textLine( numLines() - 1 );
  102. line = numLines() - 1;
  103. col = string.length();
  104. last_search = BACKWARD;
  105. goto again;
  106. }
  107. }
  108. }
  109. else{
  110. emit CursorPositionChanged();
  111. }
  112. }
  113. void KEdit::searchdone_slot(){
  114. if (!srchdialog)
  115. return;
  116. srchdialog->hide();
  117. this->setFocus();
  118. last_search = NONE;
  119. }
  120. int KEdit::doSearch(QString s_pattern, bool case_sensitive,
  121. bool wildcard, bool forward, int line, int col){
  122. (void) wildcard; // reserved for possible extension to regex
  123. int i, length;
  124. int pos = -1;
  125. if(forward){
  126. QString string;
  127. for(i = line; i < numLines(); i++) {
  128. string = textLine(i);
  129. pos = string.find(s_pattern, i == line ? col : 0, case_sensitive);
  130. if( pos != -1){
  131. length = s_pattern.length();
  132. setCursorPosition(i,pos,FALSE);
  133. for(int l = 0 ; l < length; l++){
  134. cursorRight(TRUE);
  135. }
  136. setCursorPosition( i , pos + length, TRUE );
  137. pattern = s_pattern;
  138. last_search = FORWARD;
  139. return 1;
  140. }
  141. }
  142. }
  143. else{ // searching backwards
  144. QString string;
  145. for(i = line; i >= 0; i--) {
  146. string = textLine(i);
  147. int line_length = string.length();
  148. pos = string.findRev(s_pattern, line == i ? col : line_length , case_sensitive);
  149. if (pos != -1){
  150. length = s_pattern.length();
  151. if( ! (line == i && pos > col ) ){
  152. setCursorPosition(i ,pos ,FALSE );
  153. for(int l = 0 ; l < length; l++){
  154. cursorRight(TRUE);
  155. }
  156. setCursorPosition(i ,pos + length ,TRUE );
  157. pattern = s_pattern;
  158. last_search = BACKWARD;
  159. return 1;
  160. }
  161. }
  162. }
  163. }
  164. return 0;
  165. }
  166. int KEdit::repeatSearch() {
  167. if(!srchdialog)
  168. return 0;
  169. if(pattern.isEmpty()) // there wasn't a previous search
  170. return 0;
  171. search_slot();
  172. this->setFocus();
  173. return 1;
  174. }
  175. //////////////////////////////////////////////////////////////////////////
  176. //
  177. // Replace Methods
  178. //
  179. void KEdit::Replace(){
  180. if (srchdialog)
  181. if (srchdialog->isVisible())
  182. srchdialog->hide();
  183. if (!replace_dialog){
  184. replace_dialog = new KEdReplace(0, "replace_dialog");
  185. connect(replace_dialog,SIGNAL(find_signal()),this,SLOT(replace_search_slot()));
  186. connect(replace_dialog,SIGNAL(replace_signal()),this,SLOT(replace_slot()));
  187. connect(replace_dialog,SIGNAL(replace_all_signal()),this,SLOT(replace_all_slot()));
  188. connect(replace_dialog,SIGNAL(replace_done_signal()),this,SLOT(replacedone_slot()));
  189. }
  190. QString string = replace_dialog->getText();
  191. if(string.isEmpty())
  192. replace_dialog->setText(pattern);
  193. this->deselect();
  194. last_replace = NONE;
  195. this->clearFocus();
  196. QPoint point = this->mapToGlobal (QPoint (0,0));
  197. QRect pos = this->geometry();
  198. replace_dialog->setGeometry(point.x() + pos.width()/2 - replace_dialog->width()/2,
  199. point.y() + pos.height()/2 - replace_dialog->height()/2,
  200. replace_dialog->width(),
  201. replace_dialog->height());
  202. replace_dialog->show();
  203. replace_dialog->result();
  204. }
  205. void KEdit::replace_slot(){
  206. if (!replace_dialog)
  207. return;
  208. if(!can_replace){
  209. QApplication::beep();
  210. return;
  211. }
  212. int line,col, length;
  213. QString string = replace_dialog->getReplaceText();
  214. length = string.length();
  215. this->cut();
  216. getCursorPosition(&line,&col);
  217. insertAt(string,line,col);
  218. setModified();
  219. can_replace = FALSE;
  220. setCursorPosition(line,col);
  221. for( int k = 0; k < length; k++){
  222. cursorRight(TRUE);
  223. }
  224. }
  225. void KEdit::replace_all_slot(){
  226. if (!replace_dialog)
  227. return;
  228. QString to_find_string = replace_dialog->getText();
  229. getCursorPosition(&replace_all_line,&replace_all_col);
  230. // replace_dialog->get_direction() is true if searching backward
  231. if (last_replace != NONE && replace_dialog->get_direction()){
  232. replace_all_col = replace_all_col - pattern.length() - 1 ;
  233. }
  234. deselect();
  235. again:
  236. setAutoUpdate(FALSE);
  237. int result = 1;
  238. while(result){
  239. result = doReplace(to_find_string, replace_dialog->case_sensitive(),
  240. FALSE, (!replace_dialog->get_direction()),
  241. replace_all_line,replace_all_col,TRUE);
  242. }
  243. setAutoUpdate(TRUE);
  244. update();
  245. if(!replace_dialog->get_direction()){ // forward search
  246. int query = QMessageBox::information(
  247. this,
  248. klocale->translate("Find"),
  249. klocale->translate("End of document reached.\n"\
  250. "Continue from the beginning?"),
  251. klocale->translate("Yes"),
  252. klocale->translate("No"),
  253. "",
  254. 0,1);
  255. if (query == 0){
  256. replace_all_line = 0;
  257. replace_all_col = 0;
  258. goto again;
  259. }
  260. }
  261. else{ //backward search
  262. int query = QMessageBox::information(
  263. this,
  264. klocale->translate("Find"),
  265. klocale->translate("Beginning of document reached.\n"\
  266. "Continue from the end?"),
  267. klocale->translate("Yes"),
  268. klocale->translate("No"),
  269. "",
  270. 0,1);
  271. if (query == 0){
  272. QString string = textLine( numLines() - 1 );
  273. replace_all_line = numLines() - 1;
  274. replace_all_col = string.length();
  275. last_replace = BACKWARD;
  276. goto again;
  277. }
  278. }
  279. emit CursorPositionChanged();
  280. }
  281. void KEdit::replace_search_slot(){
  282. int line, col;
  283. if (!replace_dialog)
  284. return;
  285. QString to_find_string = replace_dialog->getText();
  286. getCursorPosition(&line,&col);
  287. // replace_dialog->get_direction() is true if searching backward
  288. //printf("col %d length %d\n",col, pattern.length());
  289. if (last_replace != NONE && replace_dialog->get_direction()){
  290. col = col - pattern.length() -1;
  291. if (col < 0 ) {
  292. if(line !=0){
  293. col = strlen(textLine(line - 1));
  294. line --;
  295. }
  296. else{
  297. int query = QMessageBox::information(
  298. this,
  299. klocale->translate("Replace"),
  300. klocale->translate("Beginning of document reached.\n"\
  301. "Continue from the end?"),
  302. klocale->translate("Yes"),
  303. klocale->translate("No"),
  304. "",
  305. 0,1);
  306. if (query == 0){
  307. QString string = textLine( numLines() - 1 );
  308. line = numLines() - 1;
  309. col = string.length();
  310. last_replace = BACKWARD;
  311. }
  312. }
  313. }
  314. }
  315. again:
  316. // printf("Col %d \n",col);
  317. int result = doReplace(to_find_string, replace_dialog->case_sensitive(),
  318. FALSE, (!replace_dialog->get_direction()), line, col, FALSE );
  319. if(result == 0){
  320. if(!replace_dialog->get_direction()){ // forward search
  321. int query = QMessageBox::information(
  322. this,
  323. klocale->translate("Replace"),
  324. klocale->translate("End of document reached.\n"\
  325. "Continue from the beginning?"),
  326. klocale->translate("Yes"),
  327. klocale->translate("No"),
  328. "",
  329. 0,1);
  330. if (query == 0){
  331. line = 0;
  332. col = 0;
  333. goto again;
  334. }
  335. }
  336. else{ //backward search
  337. int query = QMessageBox::information(
  338. this,
  339. klocale->translate("Replace"),
  340. klocale->translate("Beginning of document reached.\n"\
  341. "Continue from the end?"),
  342. klocale->translate("Yes"),
  343. klocale->translate("No"),
  344. "",
  345. 0,1);
  346. if (query == 0){
  347. QString string = textLine( numLines() - 1 );
  348. line = numLines() - 1;
  349. col = string.length();
  350. last_replace = BACKWARD;
  351. goto again;
  352. }
  353. }
  354. }
  355. else{
  356. emit CursorPositionChanged();
  357. }
  358. }
  359. void KEdit::replacedone_slot(){
  360. if (!replace_dialog)
  361. return;
  362. replace_dialog->hide();
  363. // replace_dialog->clearFocus();
  364. this->setFocus();
  365. last_replace = NONE;
  366. can_replace = FALSE;
  367. }
  368. int KEdit::doReplace(QString s_pattern, bool case_sensitive,
  369. bool wildcard, bool forward, int line, int col, bool replace_all){
  370. (void) wildcard; // reserved for possible extension to regex
  371. int line_counter, length;
  372. int pos = -1;
  373. QString string;
  374. QString stringnew;
  375. QString replacement;
  376. replacement = replace_dialog->getReplaceText();
  377. line_counter = line;
  378. replace_all_col = col;
  379. if(forward){
  380. int num_lines = numLines();
  381. while (line_counter < num_lines){
  382. string = "";
  383. string = textLine(line_counter);
  384. if (replace_all){
  385. pos = string.find(s_pattern, replace_all_col, case_sensitive);
  386. }
  387. else{
  388. pos = string.find(s_pattern, line_counter == line ? col : 0, case_sensitive);
  389. }
  390. if (pos == -1 ){
  391. line_counter ++;
  392. replace_all_col = 0;
  393. replace_all_line = line_counter;
  394. }
  395. if( pos != -1){
  396. length = s_pattern.length();
  397. if(replace_all){ // automatic
  398. stringnew = string.copy();
  399. stringnew.replace(pos,length,replacement);
  400. removeLine(line_counter);
  401. insertLine(stringnew.data(),line_counter);
  402. replace_all_col = replace_all_col + replacement.length();
  403. replace_all_line = line_counter;
  404. setModified();
  405. }
  406. else{ // interactive
  407. setCursorPosition( line_counter , pos, FALSE );
  408. for(int l = 0 ; l < length; l++){
  409. cursorRight(TRUE);
  410. }
  411. setCursorPosition( line_counter , pos + length, TRUE );
  412. pattern = s_pattern;
  413. last_replace = FORWARD;
  414. can_replace = TRUE;
  415. return 1;
  416. }
  417. }
  418. }
  419. }
  420. else{ // searching backwards
  421. while(line_counter >= 0){
  422. string = "";
  423. string = textLine(line_counter);
  424. int line_length = string.length();
  425. if( replace_all ){
  426. pos = string.findRev(s_pattern, replace_all_col , case_sensitive);
  427. }
  428. else{
  429. pos = string.findRev(s_pattern,
  430. line == line_counter ? col : line_length , case_sensitive);
  431. }
  432. if (pos == -1 ){
  433. line_counter --;
  434. if(line_counter >= 0){
  435. string = "";
  436. string = textLine(line_counter);
  437. replace_all_col = string.length();
  438. }
  439. replace_all_line = line_counter;
  440. }
  441. if (pos != -1){
  442. length = s_pattern.length();
  443. if(replace_all){ // automatic
  444. stringnew = string.copy();
  445. stringnew.replace(pos,length,replacement);
  446. removeLine(line_counter);
  447. insertLine(stringnew.data(),line_counter);
  448. replace_all_col = replace_all_col - replacement.length();
  449. replace_all_line = line_counter;
  450. setModified();
  451. }
  452. else{ // interactive
  453. // printf("line_counter %d pos %d col %d\n",line_counter, pos,col);
  454. if( ! (line == line_counter && pos > col ) ){
  455. setCursorPosition(line_counter ,pos ,FALSE );
  456. for(int l = 0 ; l < length; l++){
  457. cursorRight(TRUE);
  458. }
  459. setCursorPosition(line_counter ,pos + length ,TRUE );
  460. pattern = s_pattern;
  461. last_replace = BACKWARD;
  462. can_replace = TRUE;
  463. return 1;
  464. }
  465. }
  466. }
  467. }
  468. }
  469. return 0;
  470. }
  471. ////////////////////////////////////////////////////////////////////
  472. //
  473. // Find Dialog
  474. //
  475. KEdSrch::KEdSrch(QWidget *parent, const char *name)
  476. : QDialog(parent, name,FALSE){
  477. this->setFocusPolicy(QWidget::StrongFocus);
  478. frame1 = new QGroupBox(klocale->translate("Find"), this, "frame1");
  479. value = new QLineEdit( this, "value");
  480. value->setFocus();
  481. connect(value, SIGNAL(returnPressed()), this, SLOT(ok_slot()));
  482. sensitive = new QCheckBox(klocale->translate("Case Sensitive"), this, "case");
  483. direction = new QCheckBox(klocale->translate("Find Backwards"), this, "direction");
  484. ok = new QPushButton(klocale->translate("Find"), this, "find");
  485. connect(ok, SIGNAL(clicked()), this, SLOT(ok_slot()));
  486. cancel = new QPushButton(klocale->translate("Done"), this, "cancel");
  487. connect(cancel, SIGNAL(clicked()), this, SLOT(done_slot()));
  488. // connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
  489. setFixedSize(330, 130);
  490. }
  491. void KEdSrch::focusInEvent( QFocusEvent *)
  492. {
  493. value->setFocus();
  494. //value->selectAll();
  495. }
  496. QString KEdSrch::getText() { return value->text(); }
  497. void KEdSrch::setText(QString string){
  498. value->setText(string);
  499. }
  500. void KEdSrch::done_slot(){
  501. emit search_done_signal();
  502. }
  503. bool KEdSrch::case_sensitive(){
  504. return sensitive->isChecked();
  505. }
  506. bool KEdSrch::get_direction(){
  507. return direction->isChecked();
  508. }
  509. void KEdSrch::ok_slot(){
  510. QString text;
  511. text = value->text();
  512. if (!text.isEmpty())
  513. emit search_signal();
  514. }
  515. void KEdSrch::resizeEvent(QResizeEvent *){
  516. frame1->setGeometry(5, 5, width() - 10, 80);
  517. cancel->setGeometry(width() - 80, height() - 30, 70, 25);
  518. ok->setGeometry(10, height() - 30, 70, 25);
  519. value->setGeometry(20, 25, width() - 40, 25);
  520. sensitive->setGeometry(20, 55, 110, 25);
  521. direction->setGeometry(width()- 20 - 130, 55, 130, 25);
  522. }
  523. ////////////////////////////////////////////////////////////////////
  524. //
  525. // Replace Dialog
  526. //
  527. KEdReplace::KEdReplace(QWidget *parent, const char *name)
  528. : QDialog(parent, name,FALSE){
  529. this->setFocusPolicy(QWidget::StrongFocus);
  530. frame1 = new QGroupBox(klocale->translate("Find:"), this, "frame1");
  531. value = new QLineEdit( this, "value");
  532. value->setFocus();
  533. connect(value, SIGNAL(returnPressed()), this, SLOT(ok_slot()));
  534. replace_value = new QLineEdit( this, "replac_value");
  535. connect(replace_value, SIGNAL(returnPressed()), this, SLOT(ok_slot()));
  536. label = new QLabel(this,"Rlabel");
  537. label->setText(klocale->translate("Replace with:"));
  538. sensitive = new QCheckBox(klocale->translate("Case Sensitive"), this, "case");
  539. sensitive->setChecked(TRUE);
  540. direction = new QCheckBox(klocale->translate("Find Backwards")
  541. , this, "direction");
  542. ok = new QPushButton(klocale->translate("Find"), this, "find");
  543. connect(ok, SIGNAL(clicked()), this, SLOT(ok_slot()));
  544. replace = new QPushButton(klocale->translate("Replace"), this, "rep");
  545. connect(replace, SIGNAL(clicked()), this, SLOT(replace_slot()));
  546. replace_all = new QPushButton(klocale->translate("Replace All"), this, "repall");
  547. connect(replace_all, SIGNAL(clicked()), this, SLOT(replace_all_slot()));
  548. cancel = new QPushButton(klocale->translate("Done"), this, "cancel");
  549. connect(cancel, SIGNAL(clicked()), this, SLOT(done_slot()));
  550. setFixedSize(330, 180);
  551. }
  552. void KEdReplace::focusInEvent( QFocusEvent *){
  553. value->setFocus();
  554. // value->selectAll();
  555. }
  556. QString KEdReplace::getText() { return value->text(); }
  557. QString KEdReplace::getReplaceText() { return replace_value->text(); }
  558. void KEdReplace::setText(QString string) {
  559. value->setText(string);
  560. }
  561. void KEdReplace::done_slot(){
  562. emit replace_done_signal();
  563. }
  564. void KEdReplace::replace_slot(){
  565. emit replace_signal();
  566. }
  567. void KEdReplace::replace_all_slot(){
  568. emit replace_all_signal();
  569. }
  570. bool KEdReplace::case_sensitive(){
  571. return sensitive->isChecked();
  572. }
  573. bool KEdReplace::get_direction(){
  574. return direction->isChecked();
  575. }
  576. void KEdReplace::ok_slot(){
  577. QString text;
  578. text = value->text();
  579. if (!text.isEmpty())
  580. emit find_signal();
  581. }
  582. void KEdReplace::resizeEvent(QResizeEvent *){
  583. frame1->setGeometry(5, 5, width() - 10, 135);
  584. cancel->setGeometry(width() - 80, height() - 30, 70, 25);
  585. ok->setGeometry(10, height() - 30, 70, 25);
  586. replace->setGeometry(85, height() - 30, 70, 25);
  587. replace_all->setGeometry(160, height() - 30, 85, 25);
  588. value->setGeometry(20, 25, width() - 40, 25);
  589. replace_value->setGeometry(20, 80, width() - 40, 25);
  590. label->setGeometry(20,55,80,20);
  591. sensitive->setGeometry(20, 110, 110, 25);
  592. direction->setGeometry(width()- 20 - 130, 110, 130, 25);
  593. }
  594. KEdGotoLine::KEdGotoLine( QWidget *parent, const char *name)
  595. : QDialog( parent, name, TRUE )
  596. {
  597. frame = new QGroupBox( klocale->translate("Goto Line"), this );
  598. lineNum = new KIntLineEdit( this );
  599. this->setFocusPolicy( QWidget::StrongFocus );
  600. connect(lineNum, SIGNAL(returnPressed()), this, SLOT(accept()));
  601. ok = new QPushButton(klocale->translate("Go"), this );
  602. cancel = new QPushButton(klocale->translate("Cancel"), this );
  603. connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
  604. connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
  605. resize(300, 120);
  606. }
  607. void KEdGotoLine::selected(int)
  608. {
  609. accept();
  610. }
  611. void KEdGotoLine::resizeEvent(QResizeEvent *)
  612. {
  613. frame->setGeometry(5, 5, width() - 10, 80);
  614. cancel->setGeometry(width() - 80, height() - 30, 70, 25);
  615. ok->setGeometry(10, height() - 30, 70, 25);
  616. lineNum->setGeometry(20, 35, width() - 40, 25);
  617. }
  618. void KEdGotoLine::focusInEvent( QFocusEvent *)
  619. {
  620. lineNum->setFocus();
  621. lineNum->selectAll();
  622. }
  623. int KEdGotoLine::getLineNumber()
  624. {
  625. return lineNum->getValue();
  626. }
  627. //////////////////////////////////////////////////////////////////////////////
  628. //
  629. // Spell Checking
  630. //
  631. void KEdit::spellcheck()
  632. {
  633. kspell= new KSpell (this, "KEdit: Spellcheck", this,
  634. SLOT (spellcheck2 (KSpell *)));
  635. }
  636. void KEdit::spellcheck2(KSpell *)
  637. {
  638. if (kspell->isOk())
  639. {
  640. setReadOnly (TRUE);
  641. connect (kspell, SIGNAL (misspelling (char *, QStrList *, unsigned)),
  642. this, SLOT (misspelling (char *, QStrList *, unsigned)));
  643. connect (kspell, SIGNAL (corrected (char *,
  644. char *, unsigned)),
  645. this, SLOT (corrected (char *,
  646. char *, unsigned)));
  647. connect (kspell, SIGNAL (progress (unsigned int)),
  648. this, SIGNAL (spellcheck_progress (unsigned int)) );
  649. connect (kspell, SIGNAL (done(char *)),
  650. this, SLOT (spellResult (char *)));
  651. kspell->setProgressResolution (2);
  652. kspell->check (text().data());
  653. }
  654. else
  655. {
  656. KMsgBox::message(this,"KEdit: Error","Error starting KSpell.\n"\
  657. "Please make sure you have ISpell properly configured and in your PATH.", KMsgBox::STOP);
  658. }
  659. }
  660. void KEdit::misspelling (char *word, QStrList *, unsigned pos)
  661. {
  662. int l, cnt=0;
  663. for (l=0;l<numLines() && cnt<=pos;l++)
  664. cnt+=strlen (textLine(l))+1;
  665. l--;
  666. cnt=pos-cnt+strlen (textLine (l))+1;
  667. setCursorPosition (l, cnt);
  668. //According to the Qt docs this could be done more quickly with
  669. //setCursorPosition (l, cnt+strlen(word), TRUE);
  670. //but that doesn't seem to work.
  671. for(l = 0 ; l < (int)strlen(word); l++)
  672. cursorRight(TRUE);
  673. /*
  674. if (cursorPoint().y()>height()/2)
  675. kspell->moveDlg (10, height()/2-kspell->heightDlg()-15);
  676. else
  677. kspell->moveDlg (10, height()/2 + 15);
  678. */
  679. // setCursorPosition (line, cnt+strlen(word),TRUE);
  680. }
  681. //need to use pos for insert, not cur, so forget cur altogether
  682. void KEdit::corrected (char *originalword, char *newword, unsigned pos)
  683. {
  684. //we'll reselect the original word in case the user has played with
  685. //the selection in eframe or the word was auto-replaced
  686. int line, cnt=0, l;
  687. if( (QString) newword != (QString) originalword)
  688. {
  689. for (line=0;line<numLines() && cnt<=pos;line++)
  690. cnt+=lineLength(line)+1;
  691. line--;
  692. cnt=pos-cnt+ lineLength(line)+1;
  693. //remove old word
  694. setCursorPosition (line, cnt);
  695. for(l = 0 ; l < (int)strlen(originalword); l++)
  696. cursorRight(TRUE);
  697. /// setCursorPosition (line,
  698. // cnt+strlen(originalword),TRUE);
  699. cut();
  700. insertAt (newword, line, cnt);
  701. }
  702. deselect();
  703. }
  704. void KEdit::spellResult (char *newtext)
  705. {
  706. spell_offset=0;
  707. deselect();
  708. //This has to be here in case the spellcheck is CANCELed.
  709. setText (newtext);
  710. setReadOnly (FALSE);
  711. setModified();
  712. connect (kspell, SIGNAL(cleanDone()),
  713. this, SLOT(spellCleanDone()));
  714. kspell->cleanUp();
  715. }
  716. void KEdit::spellCleanDone ()
  717. {
  718. kdebug (KDEBUG_WARN, 750, "deleting kspell");
  719. delete kspell;
  720. emit spellcheck_done();
  721. }