comparison libgui/src/m-editor/file-editor-tab.cc @ 16558:5fc1ce2947bd

style fixes * file-editor.h, file-editor.cc, file-editor-tab.h, file-editor-tab.cc: Style fixes. (file_editor_tab::editor_markers): Declare enum inside file_editor class instead of in global scope.
author John W. Eaton <jwe@octave.org>
date Tue, 23 Apr 2013 21:43:25 -0400
parents 3cd80afc3509
children 78765ae325fd
comparison
equal deleted inserted replaced
16557:d50bca1cdc22 16558:5fc1ce2947bd
55 #include "oct-env.h" 55 #include "oct-env.h"
56 #include "utils.h" 56 #include "utils.h"
57 57
58 // Make parent null for the file editor tab so that warning 58 // Make parent null for the file editor tab so that warning
59 // WindowModal messages don't affect grandparents. 59 // WindowModal messages don't affect grandparents.
60 file_editor_tab::file_editor_tab (QString directory) 60 file_editor_tab::file_editor_tab (const QString& directory_arg)
61 { 61 {
62 QString directory = directory_arg;
63
62 // Make sure there is a slash at the end of the directory name 64 // Make sure there is a slash at the end of the directory name
63 // for identification when saved later. 65 // for identification when saved later.
64 if (directory.count () && directory.at (directory.count () - 1) != '/') 66 if (directory.count () && directory.at (directory.count () - 1) != '/')
65 directory.append ("/"); 67 directory.append ("/");
68
66 _file_name = directory; 69 _file_name = directory;
70
67 _edit_area = new QsciScintilla (this); 71 _edit_area = new QsciScintilla (this);
68 72
69 // Leave the find dialog box out of memory until requested. 73 // Leave the find dialog box out of memory until requested.
70 _find_dialog = 0; 74 _find_dialog = 0;
71 _find_dialog_is_visible = false; 75 _find_dialog_is_visible = false;
83 Qt::KeyboardModifiers)), 87 Qt::KeyboardModifiers)),
84 this, SLOT (handle_margin_clicked (int, int, 88 this, SLOT (handle_margin_clicked (int, int,
85 Qt::KeyboardModifiers))); 89 Qt::KeyboardModifiers)));
86 90
87 // line numbers 91 // line numbers
88 _edit_area->setMarginsForegroundColor(QColor(96,96,96)); 92 _edit_area->setMarginsForegroundColor (QColor (96, 96, 96));
89 _edit_area->setMarginsBackgroundColor(QColor(232,232,220)); 93 _edit_area->setMarginsBackgroundColor (QColor (232, 232, 220));
90 _edit_area->setMarginType (2, QsciScintilla::TextMargin); 94 _edit_area->setMarginType (2, QsciScintilla::TextMargin);
91 95
92 // code folding 96 // code folding
93 _edit_area->setMarginType (3, QsciScintilla::SymbolMargin); 97 _edit_area->setMarginType (3, QsciScintilla::SymbolMargin);
94 _edit_area->setFolding (QsciScintilla::BoxedTreeFoldStyle , 3); 98 _edit_area->setFolding (QsciScintilla::BoxedTreeFoldStyle , 3);
95 99
96 //highlight current line color 100 //highlight current line color
97 _edit_area->setCaretLineBackgroundColor(QColor(245,245,245)); 101 _edit_area->setCaretLineBackgroundColor (QColor (245, 245, 245));
98 102
99 // other features 103 // other features
100 _edit_area->setBraceMatching (QsciScintilla::StrictBraceMatch); 104 _edit_area->setBraceMatching (QsciScintilla::StrictBraceMatch);
101 _edit_area->setAutoIndent (true); 105 _edit_area->setAutoIndent (true);
102 _edit_area->setIndentationWidth (2); 106 _edit_area->setIndentationWidth (2);
104 108
105 _edit_area->setUtf8 (true); 109 _edit_area->setUtf8 (true);
106 110
107 // auto completion 111 // auto completion
108 _edit_area->autoCompleteFromAll (); 112 _edit_area->autoCompleteFromAll ();
109 _edit_area->setAutoCompletionSource(QsciScintilla::AcsAll); 113 _edit_area->setAutoCompletionSource (QsciScintilla::AcsAll);
110 114
111 QVBoxLayout *edit_area_layout = new QVBoxLayout (); 115 QVBoxLayout *edit_area_layout = new QVBoxLayout ();
112 edit_area_layout->addWidget (_edit_area); 116 edit_area_layout->addWidget (_edit_area);
113 edit_area_layout->setMargin (0); 117 edit_area_layout->setMargin (0);
114 setLayout (edit_area_layout); 118 setLayout (edit_area_layout);
115 119
116 // connect modified signal 120 // connect modified signal
117 connect (_edit_area, SIGNAL (modificationChanged (bool)), 121 connect (_edit_area, SIGNAL (modificationChanged (bool)),
118 this, SLOT (update_window_title (bool))); 122 this, SLOT (update_window_title (bool)));
123
119 connect (_edit_area, SIGNAL (copyAvailable (bool)), 124 connect (_edit_area, SIGNAL (copyAvailable (bool)),
120 this, SLOT (handle_copy_available (bool))); 125 this, SLOT (handle_copy_available (bool)));
121 connect (&_file_system_watcher, SIGNAL (fileChanged (QString)), 126
122 this, SLOT (file_has_changed (QString))); 127 connect (&_file_system_watcher, SIGNAL (fileChanged (const QString&)),
128 this, SLOT (file_has_changed (const QString&)));
123 129
124 QSettings *settings = resource_manager::get_settings (); 130 QSettings *settings = resource_manager::get_settings ();
125 if (settings) 131 if (settings)
126 notice_settings (settings); 132 notice_settings (settings);
127 } 133 }
128 134
129 file_editor_tab::~file_editor_tab () 135 file_editor_tab::~file_editor_tab (void)
130 { 136 {
131 // Destroy items attached to _edit_area. 137 // Destroy items attached to _edit_area.
132 QsciLexer *lexer = _edit_area->lexer (); 138 QsciLexer *lexer = _edit_area->lexer ();
133 if (lexer) 139 if (lexer)
134 { 140 {
135 delete lexer; 141 delete lexer;
136 _edit_area->setLexer(0); 142 _edit_area->setLexer (0);
137 } 143 }
138 if (_find_dialog) 144 if (_find_dialog)
139 { 145 {
140 delete _find_dialog; 146 delete _find_dialog;
141 _find_dialog = 0; 147 _find_dialog = 0;
149 file_editor_tab::closeEvent (QCloseEvent *e) 155 file_editor_tab::closeEvent (QCloseEvent *e)
150 { 156 {
151 // ignore close event if file is not saved and user cancels 157 // ignore close event if file is not saved and user cancels
152 // closing this window 158 // closing this window
153 if (check_file_modified () == QMessageBox::Cancel) 159 if (check_file_modified () == QMessageBox::Cancel)
154 { 160 e->ignore ();
155 e->ignore (); 161 else
156 } 162 e->accept ();
157 else
158 {
159 e->accept();
160 }
161 } 163 }
162 164
163 void 165 void
164 file_editor_tab::set_file_name (const QString& fileName) 166 file_editor_tab::set_file_name (const QString& fileName)
165 { 167 {
179 // add the new file to the mru list 181 // add the new file to the mru list
180 emit mru_add_file (QDir::cleanPath (_file_name)); 182 emit mru_add_file (QDir::cleanPath (_file_name));
181 } 183 }
182 184
183 void 185 void
184 file_editor_tab::handle_margin_clicked(int margin, int line, 186 file_editor_tab::handle_margin_clicked (int margin, int line,
185 Qt::KeyboardModifiers state) 187 Qt::KeyboardModifiers state)
186 { 188 {
187 if (margin == 1) 189 if (margin == 1)
188 { 190 {
189 unsigned int markers_mask = _edit_area->markersAtLine (line); 191 unsigned int markers_mask = _edit_area->markersAtLine (line);
190 192
191 if (state & Qt::ControlModifier) 193 if (state & Qt::ControlModifier)
192 { 194 {
193 if (markers_mask && (1 << bookmark)) 195 if (markers_mask && (1 << bookmark))
194 _edit_area->markerDelete(line,bookmark); 196 _edit_area->markerDelete (line, bookmark);
195 else 197 else
196 _edit_area->markerAdd(line,bookmark); 198 _edit_area->markerAdd (line, bookmark);
197 } 199 }
198 else 200 else
199 { 201 {
200 if (markers_mask && (1 << breakpoint)) 202 if (markers_mask && (1 << breakpoint))
201 request_remove_breakpoint (line); 203 request_remove_breakpoint (line);
251 253
252 } 254 }
253 255
254 // slot for fetab_set_focus: sets the focus to the current edit area 256 // slot for fetab_set_focus: sets the focus to the current edit area
255 void 257 void
256 file_editor_tab::set_focus (const QWidget* ID) 258 file_editor_tab::set_focus (const QWidget *ID)
257 { 259 {
258 if (ID != this) 260 if (ID != this)
259 return; 261 return;
260 _edit_area->setFocus (); 262 _edit_area->setFocus ();
261 } 263 }
262 264
263 void 265 void
264 file_editor_tab::undo (const QWidget* ID) 266 file_editor_tab::undo (const QWidget *ID)
265 { 267 {
266 if (ID != this) 268 if (ID != this)
267 return; 269 return;
268 270
269 _edit_area->undo (); 271 _edit_area->undo ();
270 } 272 }
271 273
272 void 274 void
273 file_editor_tab::redo (const QWidget* ID) 275 file_editor_tab::redo (const QWidget *ID)
274 { 276 {
275 if (ID != this) 277 if (ID != this)
276 return; 278 return;
277 279
278 _edit_area->redo (); 280 _edit_area->redo ();
279 } 281 }
280 282
281 void 283 void
282 file_editor_tab::copy (const QWidget* ID) 284 file_editor_tab::copy (const QWidget *ID)
283 { 285 {
284 if (ID != this) 286 if (ID != this)
285 return; 287 return;
286 288
287 _edit_area->copy (); 289 _edit_area->copy ();
288 } 290 }
289 291
290 void 292 void
291 file_editor_tab::cut (const QWidget* ID) 293 file_editor_tab::cut (const QWidget *ID)
292 { 294 {
293 if (ID != this) 295 if (ID != this)
294 return; 296 return;
295 297
296 _edit_area->cut (); 298 _edit_area->cut ();
297 } 299 }
298 300
299 void 301 void
300 file_editor_tab::paste (const QWidget* ID) 302 file_editor_tab::paste (const QWidget *ID)
301 { 303 {
302 if (ID != this) 304 if (ID != this)
303 return; 305 return;
304 306
305 _edit_area->paste (); 307 _edit_area->paste ();
306 } 308 }
307 309
308 void 310 void
309 file_editor_tab::save_file (const QWidget* ID) 311 file_editor_tab::save_file (const QWidget *ID)
310 { 312 {
311 if (ID != this) 313 if (ID != this)
312 return; 314 return;
313 315
314 save_file (_file_name); 316 save_file (_file_name);
315 } 317 }
316 void 318 void
317 319
318 file_editor_tab::save_file (const QWidget* ID, const QString& fileName, bool remove_on_success) 320 file_editor_tab::save_file (const QWidget *ID, const QString& fileName,
321 bool remove_on_success)
319 { 322 {
320 if (ID != this) 323 if (ID != this)
321 return; 324 return;
322 325
323 save_file (fileName, remove_on_success); 326 save_file (fileName, remove_on_success);
324 } 327 }
325 328
326 void 329 void
327 file_editor_tab::save_file_as (const QWidget* ID) 330 file_editor_tab::save_file_as (const QWidget *ID)
328 { 331 {
329 if (ID != this) 332 if (ID != this)
330 return; 333 return;
331 334
332 save_file_as (); 335 save_file_as ();
333 } 336 }
334 337
335 void 338 void
336 file_editor_tab::print_file (const QWidget* ID) 339 file_editor_tab::print_file (const QWidget *ID)
337 { 340 {
338 if (ID != this) 341 if (ID != this)
339 return; 342 return;
340 343
341 QsciPrinter * printer = new QsciPrinter( QPrinter::HighResolution ); 344 QsciPrinter *printer = new QsciPrinter (QPrinter::HighResolution);
342 345
343 QPrintDialog printDlg(printer, this); 346 QPrintDialog printDlg (printer, this);
344 347
345 if(printDlg.exec() == QDialog::Accepted) 348 if (printDlg.exec () == QDialog::Accepted)
346 { 349 printer->printRange (_edit_area);
347 printer->printRange(_edit_area); 350
348 }
349 delete printer; 351 delete printer;
350 } 352 }
351 353
352 354
353 void 355 void
355 { 357 {
356 // Maybe someday we will do something here? 358 // Maybe someday we will do something here?
357 } 359 }
358 360
359 void 361 void
360 file_editor_tab::run_file (const QWidget* ID) 362 file_editor_tab::run_file (const QWidget *ID)
361 { 363 {
362 if (ID != this) 364 if (ID != this)
363 return; 365 return;
364 366
365 if (_edit_area->isModified ()) 367 if (_edit_area->isModified ())
370 QString function_name = file_info.fileName (); 372 QString function_name = file_info.fileName ();
371 373
372 // We have to cut off the suffix, because octave appends it. 374 // We have to cut off the suffix, because octave appends it.
373 function_name.chop (file_info.suffix ().length () + 1); 375 function_name.chop (file_info.suffix ().length () + 1);
374 emit process_octave_code (QString ("cd \'%1\'\n%2\n") 376 emit process_octave_code (QString ("cd \'%1\'\n%2\n")
375 .arg(path).arg (function_name)); 377 .arg (path).arg (function_name));
376 378
377 // TODO: Sending a run event crashes for long scripts. Find out why. 379 // TODO: Sending a run event crashes for long scripts. Find out why.
378 // octave_link::post_event 380 // octave_link::post_event
379 // (this, &file_editor_tab::run_file_callback, _file_name.toStdString ())); 381 // (this, &file_editor_tab::run_file_callback, _file_name.toStdString ()));
380 } 382 }
381 383
382 void 384 void
383 file_editor_tab::toggle_bookmark (const QWidget* ID) 385 file_editor_tab::toggle_bookmark (const QWidget *ID)
384 { 386 {
385 if (ID != this) 387 if (ID != this)
386 return; 388 return;
387 389
388 int line, cur; 390 int line, cur;
389 _edit_area->getCursorPosition (&line,&cur); 391 _edit_area->getCursorPosition (&line, &cur);
390 if ( _edit_area->markersAtLine (line) && (1 << bookmark) ) 392
393 if (_edit_area->markersAtLine (line) && (1 << bookmark))
391 _edit_area->markerDelete (line, bookmark); 394 _edit_area->markerDelete (line, bookmark);
392 else 395 else
393 _edit_area->markerAdd (line, bookmark); 396 _edit_area->markerAdd (line, bookmark);
394 } 397 }
395 398
396 void 399 void
397 file_editor_tab::next_bookmark (const QWidget* ID) 400 file_editor_tab::next_bookmark (const QWidget *ID)
398 { 401 {
399 if (ID != this) 402 if (ID != this)
400 return; 403 return;
401 404
402 int line, cur, nextline; 405 int line, cur;
403 _edit_area->getCursorPosition (&line, &cur); 406 _edit_area->getCursorPosition (&line, &cur);
404 if ( _edit_area->markersAtLine (line) && (1 << bookmark) ) 407
408 if (_edit_area->markersAtLine (line) && (1 << bookmark))
405 line++; // we have a breakpoint here, so start search from next line 409 line++; // we have a breakpoint here, so start search from next line
406 nextline = _edit_area->markerFindNext (line, (1 << bookmark)); 410
411 int nextline = _edit_area->markerFindNext (line, (1 << bookmark));
412
407 _edit_area->setCursorPosition (nextline, 0); 413 _edit_area->setCursorPosition (nextline, 0);
408 } 414 }
409 415
410 void 416 void
411 file_editor_tab::previous_bookmark (const QWidget* ID) 417 file_editor_tab::previous_bookmark (const QWidget *ID)
412 { 418 {
413 if (ID != this) 419 if (ID != this)
414 return; 420 return;
415 421
416 int line, cur, prevline; 422 int line, cur;
417 _edit_area->getCursorPosition (&line, &cur); 423 _edit_area->getCursorPosition (&line, &cur);
418 if ( _edit_area->markersAtLine (line) && (1 << bookmark) ) 424
425 if (_edit_area->markersAtLine (line) && (1 << bookmark))
419 line--; // we have a breakpoint here, so start search from prev line 426 line--; // we have a breakpoint here, so start search from prev line
420 prevline = _edit_area->markerFindPrevious (line, (1 << bookmark)); 427
428 int prevline = _edit_area->markerFindPrevious (line, (1 << bookmark));
429
421 _edit_area->setCursorPosition (prevline, 0); 430 _edit_area->setCursorPosition (prevline, 0);
422 } 431 }
423 432
424 void 433 void
425 file_editor_tab::remove_bookmark (const QWidget* ID) 434 file_editor_tab::remove_bookmark (const QWidget *ID)
426 { 435 {
427 if (ID != this) 436 if (ID != this)
428 return; 437 return;
429 438
430 _edit_area->markerDeleteAll (bookmark); 439 _edit_area->markerDeleteAll (bookmark);
553 octave_link::post_event 562 octave_link::post_event
554 (this, &file_editor_tab::remove_breakpoint_callback, info); 563 (this, &file_editor_tab::remove_breakpoint_callback, info);
555 } 564 }
556 565
557 void 566 void
558 file_editor_tab::toggle_breakpoint (const QWidget* ID) 567 file_editor_tab::toggle_breakpoint (const QWidget *ID)
559 { 568 {
560 if (ID != this) 569 if (ID != this)
561 return; 570 return;
562 571
563 int line, cur; 572 int line, cur;
564 _edit_area->getCursorPosition (&line, &cur); 573 _edit_area->getCursorPosition (&line, &cur);
565 if ( _edit_area->markersAtLine (line) && (1 << breakpoint) ) 574
575 if (_edit_area->markersAtLine (line) && (1 << breakpoint))
566 request_remove_breakpoint (line); 576 request_remove_breakpoint (line);
567 else 577 else
568 request_add_breakpoint (line); 578 request_add_breakpoint (line);
569 } 579 }
570 580
571 void 581 void
572 file_editor_tab::next_breakpoint (const QWidget* ID) 582 file_editor_tab::next_breakpoint (const QWidget *ID)
573 { 583 {
574 if (ID != this) 584 if (ID != this)
575 return; 585 return;
576 586
577 int line, cur, nextline; 587 int line, cur;
578 _edit_area->getCursorPosition (&line, &cur); 588 _edit_area->getCursorPosition (&line, &cur);
579 if ( _edit_area->markersAtLine (line) && (1 << breakpoint) ) 589
590 if (_edit_area->markersAtLine (line) && (1 << breakpoint))
580 line++; // we have a breakpoint here, so start search from next line 591 line++; // we have a breakpoint here, so start search from next line
581 nextline = _edit_area->markerFindNext (line, (1 << breakpoint)); 592
593 int nextline = _edit_area->markerFindNext (line, (1 << breakpoint));
594
582 _edit_area->setCursorPosition (nextline, 0); 595 _edit_area->setCursorPosition (nextline, 0);
583 } 596 }
584 597
585 void 598 void
586 file_editor_tab::previous_breakpoint (const QWidget* ID) 599 file_editor_tab::previous_breakpoint (const QWidget *ID)
587 { 600 {
588 if (ID != this) 601 if (ID != this)
589 return; 602 return;
590 603
591 int line, cur, prevline; 604 int line, cur, prevline;
592 _edit_area->getCursorPosition (&line, &cur); 605 _edit_area->getCursorPosition (&line, &cur);
593 if ( _edit_area->markersAtLine (line) && (1 << breakpoint) ) 606
607 if (_edit_area->markersAtLine (line) && (1 << breakpoint))
594 line--; // we have a breakpoint here, so start search from prev line 608 line--; // we have a breakpoint here, so start search from prev line
609
595 prevline = _edit_area->markerFindPrevious (line, (1 << breakpoint)); 610 prevline = _edit_area->markerFindPrevious (line, (1 << breakpoint));
611
596 _edit_area->setCursorPosition (prevline, 0); 612 _edit_area->setCursorPosition (prevline, 0);
597 } 613 }
598 614
599 void 615 void
600 file_editor_tab::remove_all_breakpoints (const QWidget* ID) 616 file_editor_tab::remove_all_breakpoints (const QWidget *ID)
601 { 617 {
602 if (ID != this) 618 if (ID != this)
603 return; 619 return;
604 620
605 QFileInfo file_info (_file_name); 621 QFileInfo file_info (_file_name);
614 octave_link::post_event 630 octave_link::post_event
615 (this, &file_editor_tab::remove_all_breakpoints_callback, info); 631 (this, &file_editor_tab::remove_all_breakpoints_callback, info);
616 } 632 }
617 633
618 void 634 void
619 file_editor_tab::comment_selected_text (const QWidget* ID) 635 file_editor_tab::comment_selected_text (const QWidget *ID)
620 { 636 {
621 if (ID != this) 637 if (ID != this)
622 return; 638 return;
623 639
624 do_comment_selected_text (true); 640 do_comment_selected_text (true);
625 } 641 }
626 642
627 void 643 void
628 file_editor_tab::uncomment_selected_text (const QWidget* ID) 644 file_editor_tab::uncomment_selected_text (const QWidget *ID)
629 { 645 {
630 if (ID != this) 646 if (ID != this)
631 return; 647 return;
632 648
633 do_comment_selected_text (false); 649 do_comment_selected_text (false);
641 _find_dialog_geometry = _find_dialog->geometry (); 657 _find_dialog_geometry = _find_dialog->geometry ();
642 _find_dialog_is_visible = false; 658 _find_dialog_is_visible = false;
643 } 659 }
644 660
645 void 661 void
646 file_editor_tab::find (const QWidget* ID) 662 file_editor_tab::find (const QWidget *ID)
647 { 663 {
648 if (ID != this) 664 if (ID != this)
649 return; 665 return;
650 666
651 // The find_dialog feature doesn't need a slot for return info. 667 // The find_dialog feature doesn't need a slot for return info.
656 // retaining position per file editor tabs, which can be undocked. 672 // retaining position per file editor tabs, which can be undocked.
657 673
658 if (!_find_dialog) 674 if (!_find_dialog)
659 { 675 {
660 _find_dialog = new find_dialog (_edit_area, 676 _find_dialog = new find_dialog (_edit_area,
661 qobject_cast<QWidget *>(sender ())); 677 qobject_cast<QWidget *> (sender ()));
662 connect (_find_dialog, SIGNAL (finished (int)), 678 connect (_find_dialog, SIGNAL (finished (int)),
663 this, SLOT (handle_find_dialog_finished (int))); 679 this, SLOT (handle_find_dialog_finished (int)));
664 _find_dialog->setWindowModality (Qt::NonModal); 680 _find_dialog->setWindowModality (Qt::NonModal);
665 _find_dialog_geometry = _find_dialog->geometry (); 681 _find_dialog_geometry = _find_dialog->geometry ();
666 } 682 }
676 _find_dialog->init_search_text (); 692 _find_dialog->init_search_text ();
677 693
678 } 694 }
679 695
680 void 696 void
681 file_editor_tab::goto_line (const QWidget* ID, int line) 697 file_editor_tab::goto_line (const QWidget *ID, int line)
682 { 698 {
683 if (ID != this) 699 if (ID != this)
684 return; 700 return;
685 701
686 if (line <= 0) // ask for desired line 702 if (line <= 0) // ask for desired line
687 { 703 {
688 bool ok = false; 704 bool ok = false;
689 int index; 705 int index;
690 _edit_area->getCursorPosition(&line, &index); 706 _edit_area->getCursorPosition (&line, &index);
691 line = QInputDialog::getInt (_edit_area, tr("Goto line"), tr("Line number"), 707 line = QInputDialog::getInt (_edit_area, tr ("Goto line"),
692 line+1, 1, _edit_area->lines(), 1, &ok); 708 tr ("Line number"), line+1, 1,
709 _edit_area->lines (), 1, &ok);
693 if (ok) 710 if (ok)
694 { 711 {
695 _edit_area->setCursorPosition (line-1, 0); 712 _edit_area->setCursorPosition (line-1, 0);
696 center_current_line (); 713 center_current_line ();
697 } 714 }
698 } 715 }
699 else // go to given line without dialog 716 else // go to given line without dialog
700 { 717 _edit_area->setCursorPosition (line-1, 0);
701 _edit_area->setCursorPosition (line-1, 0);
702 }
703 } 718 }
704 719
705 720
706 void 721 void
707 file_editor_tab::do_comment_selected_text (bool comment) 722 file_editor_tab::do_comment_selected_text (bool comment)
708 { 723 {
709 if ( _edit_area->hasSelectedText() ) 724 if (_edit_area->hasSelectedText ())
710 { 725 {
711 int lineFrom, lineTo, colFrom, colTo, i; 726 int lineFrom, lineTo, colFrom, colTo;
712 _edit_area->getSelection (&lineFrom,&colFrom,&lineTo,&colTo); 727 _edit_area->getSelection (&lineFrom, &colFrom, &lineTo, &colTo);
713 if ( colTo == 0 ) // the beginning of last line is not selected 728
729 if (colTo == 0) // the beginning of last line is not selected
714 lineTo--; // stop at line above 730 lineTo--; // stop at line above
731
715 _edit_area->beginUndoAction (); 732 _edit_area->beginUndoAction ();
716 for ( i=lineFrom; i<=lineTo; i++ ) 733
734 for (int i = lineFrom; i <= lineTo; i++)
717 { 735 {
718 if ( comment ) 736 if (comment)
719 _edit_area->insertAt("%",i,0); 737 _edit_area->insertAt ("%", i, 0);
720 else 738 else
721 { 739 {
722 QString line(_edit_area->text(i)); 740 QString line (_edit_area->text (i));
723 if ( line.startsWith("%") ) 741 if (line.startsWith ("%"))
724 { 742 {
725 _edit_area->setSelection(i,0,i,1); 743 _edit_area->setSelection (i, 0, i, 1);
726 _edit_area->removeSelectedText(); 744 _edit_area->removeSelectedText ();
727 } 745 }
728 } 746 }
729 } 747 }
748
730 _edit_area->endUndoAction (); 749 _edit_area->endUndoAction ();
731 } 750 }
732 } 751 }
733 752
734 void 753 void
735 file_editor_tab::update_window_title (bool modified) 754 file_editor_tab::update_window_title (bool modified)
736 { 755 {
737 QString title (""); 756 QString title ("");
738 QString tooltip (""); 757 QString tooltip ("");
758
739 if (_file_name.isEmpty () || _file_name.at (_file_name.count () - 1) == '/') 759 if (_file_name.isEmpty () || _file_name.at (_file_name.count () - 1) == '/')
740 title = tr("<unnamed>"); 760 title = tr ("<unnamed>");
741 else 761 else
742 { 762 {
743 if ( _long_title ) 763 if (_long_title)
744 title = _file_name; 764 title = _file_name;
745 else 765 else
746 { 766 {
747 QFileInfo file(_file_name); 767 QFileInfo file (_file_name);
748 title = file.fileName(); 768 title = file.fileName ();
749 tooltip = _file_name; 769 tooltip = _file_name;
750 } 770 }
751 } 771 }
752 772
753 if ( modified ) 773 if (modified)
754 { 774 emit file_name_changed (title.prepend ("* "), tooltip);
755 emit file_name_changed (title.prepend("* "), tooltip);
756 }
757 else 775 else
758 emit file_name_changed (title, tooltip); 776 emit file_name_changed (title, tooltip);
759 } 777 }
760 778
761 void 779 void
762 file_editor_tab::handle_copy_available(bool enableCopy) 780 file_editor_tab::handle_copy_available (bool enableCopy)
763 { 781 {
764 _copy_available = enableCopy; 782 _copy_available = enableCopy;
765 emit editor_state_changed (_copy_available, QDir::cleanPath (_file_name)); 783 emit editor_state_changed (_copy_available, QDir::cleanPath (_file_name));
766 } 784 }
767 785
772 if (_edit_area->isModified ()) 790 if (_edit_area->isModified ())
773 { 791 {
774 // File is modified but not saved, ask user what to do. The file 792 // File is modified but not saved, ask user what to do. The file
775 // editor tab can't be made parent because it may be deleted depending 793 // editor tab can't be made parent because it may be deleted depending
776 // upon the response. Instead, change the _edit_area to read only. 794 // upon the response. Instead, change the _edit_area to read only.
777 QMessageBox* msgBox = new QMessageBox ( 795 QMessageBox* msgBox
778 QMessageBox::Warning, tr ("Octave Editor"), 796 = new QMessageBox (QMessageBox::Warning, tr ("Octave Editor"),
779 tr ("The file\n" 797 tr ("The file\n"
780 "%1\n" 798 "%1\n"
781 "is about to be closed but has been modified.\n" 799 "is about to be closed but has been modified.\n"
782 "Do you want to cancel closing, save or discard the changes?"). 800 "Do you want to cancel closing, save or discard the changes?").
783 arg (_file_name), 801 arg (_file_name),
784 QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard, 0); 802 QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard, 0);
803
785 msgBox->setDefaultButton (QMessageBox::Save); 804 msgBox->setDefaultButton (QMessageBox::Save);
786 _edit_area->setReadOnly (true); 805 _edit_area->setReadOnly (true);
787 connect (msgBox, SIGNAL (finished (int)), 806 connect (msgBox, SIGNAL (finished (int)),
788 this, SLOT (handle_file_modified_answer (int))); 807 this, SLOT (handle_file_modified_answer (int)));
789 msgBox->setWindowModality (Qt::NonModal); 808 msgBox->setWindowModality (Qt::NonModal);
790 msgBox->setAttribute (Qt::WA_DeleteOnClose); 809 msgBox->setAttribute (Qt::WA_DeleteOnClose);
791 msgBox->show (); 810 msgBox->show ();
792 return (QMessageBox::Cancel); 811
812 return QMessageBox::Cancel;
793 } 813 }
794 else 814 else
795 { 815 {
796 // Nothing was modified, just remove from editor. 816 // Nothing was modified, just remove from editor.
797 emit tab_remove_request (); 817 emit tab_remove_request ();
798 } 818 }
799 819
800 return (decision); 820 return decision;
801 } 821 }
802 822
803 void 823 void
804 file_editor_tab::handle_file_modified_answer (int decision) 824 file_editor_tab::handle_file_modified_answer (int decision)
805 { 825 {
825 { 845 {
826 _edit_area->setModified (modified); 846 _edit_area->setModified (modified);
827 } 847 }
828 848
829 QString 849 QString
830 file_editor_tab::load_file(const QString& fileName) 850 file_editor_tab::load_file (const QString& fileName)
831 { 851 {
832 QFile file (fileName); 852 QFile file (fileName);
833 if (!file.open (QFile::ReadOnly)) 853 if (!file.open (QFile::ReadOnly))
834 { 854 return file.errorString ();
835 return file.errorString ();
836 }
837 855
838 QTextStream in (&file); 856 QTextStream in (&file);
839 QApplication::setOverrideCursor (Qt::WaitCursor); 857 QApplication::setOverrideCursor (Qt::WaitCursor);
840 _edit_area->setText (in.readAll ()); 858 _edit_area->setText (in.readAll ());
841 QApplication::restoreOverrideCursor (); 859 QApplication::restoreOverrideCursor ();
858 void 876 void
859 file_editor_tab::save_file (const QString& saveFileName, bool remove_on_success) 877 file_editor_tab::save_file (const QString& saveFileName, bool remove_on_success)
860 { 878 {
861 // If it is a new file with no name, signal that saveFileAs 879 // If it is a new file with no name, signal that saveFileAs
862 // should be performed. 880 // should be performed.
863 if (saveFileName.isEmpty () || saveFileName.at (saveFileName.count () - 1) == '/') 881 if (saveFileName.isEmpty ()
882 || saveFileName.at (saveFileName.count () - 1) == '/')
864 { 883 {
865 save_file_as (remove_on_success); 884 save_file_as (remove_on_success);
866 return; 885 return;
867 } 886 }
868 887
879 // watched previously. 898 // watched previously.
880 if (trackedFiles.contains (saveFileName)) 899 if (trackedFiles.contains (saveFileName))
881 _file_system_watcher.addPath (saveFileName); 900 _file_system_watcher.addPath (saveFileName);
882 901
883 // Create a NonModal message about error. 902 // Create a NonModal message about error.
884 QMessageBox* msgBox = new QMessageBox ( 903 QMessageBox* msgBox
885 QMessageBox::Critical, tr ("Octave Editor"), 904 = new QMessageBox (QMessageBox::Critical,
886 tr ("Could not open file %1 for write:\n%2."). 905 tr ("Octave Editor"),
887 arg (saveFileName).arg (file.errorString ()), 906 tr ("Could not open file %1 for write:\n%2.").
888 QMessageBox::Ok, 0); 907 arg (saveFileName).arg (file.errorString ()),
908 QMessageBox::Ok, 0);
889 msgBox->setWindowModality (Qt::NonModal); 909 msgBox->setWindowModality (Qt::NonModal);
890 msgBox->setAttribute (Qt::WA_DeleteOnClose); 910 msgBox->setAttribute (Qt::WA_DeleteOnClose);
891 msgBox->show (); 911 msgBox->show ();
912
892 return; 913 return;
893 } 914 }
894 915
895 // save the contents into the file 916 // save the contents into the file
896 QTextStream out (&file); 917 QTextStream out (&file);
897 QApplication::setOverrideCursor (Qt::WaitCursor); 918 QApplication::setOverrideCursor (Qt::WaitCursor);
898 out << _edit_area->text (); 919 out << _edit_area->text ();
899 QApplication::restoreOverrideCursor (); 920 QApplication::restoreOverrideCursor ();
900 file.close(); 921 file.close ();
901 922
902 // save file name after closing file as set_file_name starts watching again 923 // save file name after closing file as set_file_name starts watching again
903 set_file_name (saveFileName); 924 set_file_name (saveFileName);
925
904 // set the window title to actual file name (not modified) 926 // set the window title to actual file name (not modified)
905 update_window_title (false); 927 update_window_title (false);
928
906 // files is save -> not modified 929 // files is save -> not modified
907 _edit_area->setModified (false); 930 _edit_area->setModified (false);
908 931
909 if (remove_on_success) 932 if (remove_on_success)
910 { 933 {
937 fileDialog->selectFile (_file_name); 960 fileDialog->selectFile (_file_name);
938 } 961 }
939 else 962 else
940 { 963 {
941 fileDialog->selectFile (""); 964 fileDialog->selectFile ("");
965
942 if (_file_name.isEmpty ()) 966 if (_file_name.isEmpty ())
943 { 967 fileDialog->setDirectory (QDir::currentPath ());
944 fileDialog->setDirectory (QDir::currentPath ());
945 }
946 else 968 else
947 { 969 {
948 // The file name is actually the directory name from the 970 // The file name is actually the directory name from the
949 // constructor argument. 971 // constructor argument.
950 fileDialog->setDirectory (_file_name); 972 fileDialog->setDirectory (_file_name);
951 } 973 }
952 } 974 }
953 fileDialog->setNameFilter (tr("Octave Files (*.m);;All Files (*.*)")); 975
976 fileDialog->setNameFilter (tr ("Octave Files (*.m);;All Files (*.*)"));
954 fileDialog->setDefaultSuffix ("m"); 977 fileDialog->setDefaultSuffix ("m");
955 fileDialog->setAcceptMode (QFileDialog::AcceptSave); 978 fileDialog->setAcceptMode (QFileDialog::AcceptSave);
956 fileDialog->setViewMode (QFileDialog::Detail); 979 fileDialog->setViewMode (QFileDialog::Detail);
980
957 if (remove_on_success) 981 if (remove_on_success)
958 { 982 {
959 connect (fileDialog, SIGNAL (fileSelected (const QString&)), 983 connect (fileDialog, SIGNAL (fileSelected (const QString&)),
960 this, SLOT (handle_save_file_as_answer_close (const QString&))); 984 this, SLOT (handle_save_file_as_answer_close (const QString&)));
985
961 connect (fileDialog, SIGNAL (rejected ()), 986 connect (fileDialog, SIGNAL (rejected ()),
962 this, SLOT (handle_save_file_as_answer_cancel ())); 987 this, SLOT (handle_save_file_as_answer_cancel ()));
963 } 988 }
964 else 989 else
965 { 990 {
966 connect (fileDialog, SIGNAL (fileSelected (const QString&)), 991 connect (fileDialog, SIGNAL (fileSelected (const QString&)),
967 this, SLOT (handle_save_file_as_answer (const QString&))); 992 this, SLOT (handle_save_file_as_answer (const QString&)));
968 } 993 }
994
969 fileDialog->setWindowModality (Qt::WindowModal); 995 fileDialog->setWindowModality (Qt::WindowModal);
970 fileDialog->setAttribute (Qt::WA_DeleteOnClose); 996 fileDialog->setAttribute (Qt::WA_DeleteOnClose);
971 fileDialog->show (); 997 fileDialog->show ();
972 } 998 }
973 999
977 // Could overwrite the file here (and tell user the file was 1003 // Could overwrite the file here (and tell user the file was
978 // overwritten), but the user could have unintentionally 1004 // overwritten), but the user could have unintentionally
979 // selected the same name not intending to overwrite. 1005 // selected the same name not intending to overwrite.
980 1006
981 // Create a NonModal message about error. 1007 // Create a NonModal message about error.
982 QMessageBox* msgBox = new QMessageBox ( 1008 QMessageBox* msgBox
983 QMessageBox::Critical, tr ("Octave Editor"), 1009 = new QMessageBox (QMessageBox::Critical, tr ("Octave Editor"),
984 tr ("File not saved! The selected file name\n%1\n" 1010 tr ("File not saved! The selected file name\n%1\n"
985 "is the same as the current file name"). 1011 "is the same as the current file name").
986 arg (saveFileName), 1012 arg (saveFileName),
987 QMessageBox::Ok, 0); 1013 QMessageBox::Ok, 0);
1014
988 msgBox->setWindowModality (Qt::NonModal); 1015 msgBox->setWindowModality (Qt::NonModal);
989 msgBox->setAttribute (Qt::WA_DeleteOnClose); 1016 msgBox->setAttribute (Qt::WA_DeleteOnClose);
990 msgBox->show (); 1017 msgBox->show ();
991 } 1018 }
992 1019
1041 1068
1042 if (QFile::exists (_file_name)) 1069 if (QFile::exists (_file_name))
1043 { 1070 {
1044 // Create a WindowModal message that blocks the edit area 1071 // Create a WindowModal message that blocks the edit area
1045 // by making _edit_area parent. 1072 // by making _edit_area parent.
1046 QMessageBox* msgBox = new QMessageBox ( 1073 QMessageBox* msgBox
1047 QMessageBox::Warning, tr ("Octave Editor"), 1074 = new QMessageBox (QMessageBox::Warning,
1048 tr ("It seems that \'%1\' has been modified by another application. Do you want to reload it?"). 1075 tr ("Octave Editor"),
1049 arg (_file_name), QMessageBox::Yes | QMessageBox::No, this); 1076 tr ("It seems that \'%1\' has been modified by another application. Do you want to reload it?").
1077 arg (_file_name),
1078 QMessageBox::Yes | QMessageBox::No, this);
1079
1050 connect (msgBox, SIGNAL (finished (int)), 1080 connect (msgBox, SIGNAL (finished (int)),
1051 this, SLOT (handle_file_reload_answer (int))); 1081 this, SLOT (handle_file_reload_answer (int)));
1082
1052 msgBox->setWindowModality (Qt::WindowModal); 1083 msgBox->setWindowModality (Qt::WindowModal);
1053 msgBox->setAttribute (Qt::WA_DeleteOnClose); 1084 msgBox->setAttribute (Qt::WA_DeleteOnClose);
1054 msgBox->show (); 1085 msgBox->show ();
1055 } 1086 }
1056 else 1087 else
1057 { 1088 {
1058 QString modified = ""; 1089 QString modified = "";
1059 if (_edit_area->isModified ()) 1090 if (_edit_area->isModified ())
1060 modified = tr ("\n\nWarning: The contents in the editor is modified!"); 1091 modified = tr ("\n\nWarning: The contents in the editor is modified!");
1092
1061 // Create a WindowModal message. The file editor tab can't be made 1093 // Create a WindowModal message. The file editor tab can't be made
1062 // parent because it may be deleted depending upon the response. 1094 // parent because it may be deleted depending upon the response.
1063 // Instead, change the _edit_area to read only. 1095 // Instead, change the _edit_area to read only.
1064 QMessageBox* msgBox = new QMessageBox ( 1096 QMessageBox* msgBox
1065 QMessageBox::Warning, tr ("Octave Editor"), 1097 = new QMessageBox (QMessageBox::Warning, tr ("Octave Editor"),
1066 tr ("It seems that the file\n" 1098 tr ("It seems that the file\n"
1067 "%1\n" 1099 "%1\n"
1068 "has been deleted or renamed. Do you want to save it now?%2"). 1100 "has been deleted or renamed. Do you want to save it now?%2").
1069 arg (_file_name).arg (modified), 1101 arg (_file_name).arg (modified),
1070 QMessageBox::Save | QMessageBox::Close, 0); 1102 QMessageBox::Save | QMessageBox::Close, 0);
1103
1071 _edit_area->setReadOnly (true); 1104 _edit_area->setReadOnly (true);
1105
1072 connect (msgBox, SIGNAL (finished (int)), 1106 connect (msgBox, SIGNAL (finished (int)),
1073 this, SLOT (handle_file_resave_answer (int))); 1107 this, SLOT (handle_file_resave_answer (int)));
1108
1074 msgBox->setWindowModality (Qt::WindowModal); 1109 msgBox->setWindowModality (Qt::WindowModal);
1075 msgBox->setAttribute (Qt::WA_DeleteOnClose); 1110 msgBox->setAttribute (Qt::WA_DeleteOnClose);
1076 msgBox->show (); 1111 msgBox->show ();
1077 } 1112 }
1078 } 1113 }
1081 file_editor_tab::notice_settings (const QSettings *settings) 1116 file_editor_tab::notice_settings (const QSettings *settings)
1082 { 1117 {
1083 // QSettings pointer is checked before emitting. 1118 // QSettings pointer is checked before emitting.
1084 1119
1085 update_lexer (); 1120 update_lexer ();
1121
1086 QFontMetrics lexer_font_metrics (_edit_area->lexer ()->defaultFont (0)); 1122 QFontMetrics lexer_font_metrics (_edit_area->lexer ()->defaultFont (0));
1087 1123
1088 _edit_area->setCaretLineVisible(settings->value ("editor/highlightCurrentLine",true).toBool ()); 1124 _edit_area->setCaretLineVisible
1089 1125 (settings->value ("editor/highlightCurrentLine", true).toBool ());
1090 if (settings->value ("editor/codeCompletion",true).toBool ()) 1126
1127 if (settings->value ("editor/codeCompletion", true).toBool ())
1091 _edit_area->setAutoCompletionThreshold (1); 1128 _edit_area->setAutoCompletionThreshold (1);
1092 else 1129 else
1093 _edit_area->setAutoCompletionThreshold (-1); 1130 _edit_area->setAutoCompletionThreshold (-1);
1094 1131
1095 if (settings->value ("editor/showLineNumbers",true).toBool ()) 1132 if (settings->value ("editor/showLineNumbers", true).toBool ())
1096 { 1133 {
1097 _edit_area->setMarginLineNumbers (2, true); 1134 _edit_area->setMarginLineNumbers (2, true);
1098 _edit_area->setMarginWidth(2, lexer_font_metrics.width("9999")); 1135 _edit_area->setMarginWidth (2, lexer_font_metrics.width ("9999"));
1099 } 1136 }
1100 else 1137 else
1101 { 1138 {
1102 _edit_area->setMarginLineNumbers (2, false); 1139 _edit_area->setMarginLineNumbers (2, false);
1103 _edit_area->setMarginWidth(2, 0); 1140 _edit_area->setMarginWidth (2, 0);
1104 } 1141 }
1105 1142
1106 _long_title = settings->value ("editor/longWindowTitle",false).toBool (); 1143 _long_title = settings->value ("editor/longWindowTitle", false).toBool ();
1107 1144
1108 update_window_title (false); 1145 update_window_title (false);
1109 } 1146 }
1110 1147
1111 void 1148 void
1112 file_editor_tab::conditional_close (const QWidget* ID) 1149 file_editor_tab::conditional_close (const QWidget *ID)
1113 { 1150 {
1114 if (ID != this) 1151 if (ID != this)
1115 return; 1152 return;
1116 1153
1117 close (); 1154 close ();
1118 } 1155 }
1119 1156
1120 void 1157 void
1121 file_editor_tab::change_editor_state (const QWidget* ID) 1158 file_editor_tab::change_editor_state (const QWidget *ID)
1122 { 1159 {
1123 if (ID != this) 1160 if (ID != this)
1124 { 1161 {
1125 // Widget may be going out of focus. If so, record location. 1162 // Widget may be going out of focus. If so, record location.
1126 if (_find_dialog) 1163 if (_find_dialog)
1137 if (_find_dialog && _find_dialog_is_visible) 1174 if (_find_dialog && _find_dialog_is_visible)
1138 { 1175 {
1139 _find_dialog->setGeometry (_find_dialog_geometry); 1176 _find_dialog->setGeometry (_find_dialog_geometry);
1140 _find_dialog->show (); 1177 _find_dialog->show ();
1141 } 1178 }
1179
1142 emit editor_state_changed (_copy_available, QDir::cleanPath (_file_name)); 1180 emit editor_state_changed (_copy_available, QDir::cleanPath (_file_name));
1143 } 1181 }
1144 1182
1145 void 1183 void
1146 file_editor_tab::file_name_query (const QWidget* ID) 1184 file_editor_tab::file_name_query (const QWidget *ID)
1147 { 1185 {
1148 // A zero (null pointer) means that all file editor tabs 1186 // A zero (null pointer) means that all file editor tabs
1149 // should respond, otherwise just the desired file editor tab. 1187 // should respond, otherwise just the desired file editor tab.
1150 if (ID != this && ID != 0) 1188 if (ID != this && ID != 0)
1151 return; 1189 return;
1157 1195
1158 void 1196 void
1159 file_editor_tab::handle_file_reload_answer (int decision) 1197 file_editor_tab::handle_file_reload_answer (int decision)
1160 { 1198 {
1161 if (decision == QMessageBox::Yes) 1199 if (decision == QMessageBox::Yes)
1162 { // reload: file is readded to the file watcher in set_file_name () 1200 {
1201 // reload: file is readded to the file watcher in set_file_name ()
1163 load_file (_file_name); 1202 load_file (_file_name);
1164 } 1203 }
1165 else 1204 else
1166 { // do not reload: readd to the file watche 1205 {
1206 // do not reload: readd to the file watche
1167 _file_system_watcher.addPath (_file_name); 1207 _file_system_watcher.addPath (_file_name);
1168 } 1208 }
1169 } 1209 }
1170 1210
1171 void 1211 void
1228 1268
1229 1269
1230 void 1270 void
1231 file_editor_tab::center_current_line () 1271 file_editor_tab::center_current_line ()
1232 { 1272 {
1233 long int visible_lines = _edit_area->SendScintilla 1273 long int visible_lines
1234 (QsciScintillaBase::SCI_LINESONSCREEN); 1274 = _edit_area->SendScintilla (QsciScintillaBase::SCI_LINESONSCREEN);
1275
1235 if (visible_lines > 2) 1276 if (visible_lines > 2)
1236 { 1277 {
1237 int line, index; 1278 int line, index;
1238 _edit_area->getCursorPosition(&line,&index); 1279 _edit_area->getCursorPosition (&line, &index);
1280
1239 int first_line = _edit_area->firstVisibleLine (); 1281 int first_line = _edit_area->firstVisibleLine ();
1240 first_line = first_line + (line - first_line - (visible_lines-1)/2); 1282 first_line = first_line + (line - first_line - (visible_lines-1)/2);
1283
1241 _edit_area->setFirstVisibleLine (first_line); 1284 _edit_area->setFirstVisibleLine (first_line);
1242 } 1285 }
1243 } 1286 }
1244 1287
1245 #endif 1288 #endif