comparison libgui/src/m-editor/file-editor.cc @ 15860:feba9ff6e6a8

editor: add list of recently used files to the file menu * file_editor_tab.cc (set_file_name): emit signal mru_add_file () for a new file * file_editor_tab.h: new signal mru_add_file () * file_editor.cc (request_mru_open_file): new handler for opening a file from the mru-list, (handle_mru_add_file): slot for signal emitted from file_editor_tab; adds the new file to the mru list, (mru_menu_update): private function for updating the mru-list, (mru_menu_update): private function for updating the mru-list, (construct): implement mru-list menu and the related actions, (add_file_editor_tab): connect signal mru_add_file () * file-editor.h: definition of new slots, methods, string list for mru-list, and array for the related actions
author Torsten <ttl@justmail.de>
date Fri, 28 Dec 2012 15:01:08 +0100
parents 424edeca3c66
children f425e680925e
comparison
equal deleted inserted replaced
15859:0e393e744e5e 15860:feba9ff6e6a8
166 { 166 {
167 // Supply empty title then have the file_editor_tab update 167 // Supply empty title then have the file_editor_tab update
168 // with full or short name. 168 // with full or short name.
169 add_file_editor_tab (fileEditorTab, ""); 169 add_file_editor_tab (fileEditorTab, "");
170 fileEditorTab->update_window_title (false); 170 fileEditorTab->update_window_title (false);
171 // file already loaded, add file to mru list here
172 handle_mru_add_file(QDir::cleanPath (openFileName));
171 } 173 }
172 else 174 else
173 { 175 {
174 delete fileEditorTab; 176 delete fileEditorTab;
175 // Create a NonModal message about error. 177 // Create a NonModal message about error.
184 } 186 }
185 } 187 }
186 } 188 }
187 } 189 }
188 190
191 // open a file from the mru list
192 void
193 file_editor::request_mru_open_file ()
194 {
195 QAction *action = qobject_cast<QAction *>(sender ());
196 if (action)
197 {
198 request_open_file (action->data ().toString ());
199 }
200 }
201
202
189 void 203 void
190 file_editor::check_conflict_save (const QString& saveFileName, bool remove_on_success) 204 file_editor::check_conflict_save (const QString& saveFileName, bool remove_on_success)
191 { 205 {
192 // Have all file editor tabs signal what their file names are. 206 // Have all file editor tabs signal what their file names are.
193 fetFileNames.clear (); 207 fetFileNames.clear ();
353 367
354 void 368 void
355 file_editor::request_find () 369 file_editor::request_find ()
356 { 370 {
357 emit fetab_find (_tab_widget->currentWidget ()); 371 emit fetab_find (_tab_widget->currentWidget ());
372 }
373
374 void
375 file_editor::handle_mru_add_file (const QString& file_name)
376 {
377 _mru_files.removeAll (file_name);
378 _mru_files.prepend (file_name);
379 mru_menu_update ();
380 }
381
382 void
383 file_editor::mru_menu_update ()
384 {
385 int num_files = qMin (_mru_files.size(), int (MaxMRUFiles));
386 // configure and show active actions of mru-menu
387 for (int i = 0; i < num_files; ++i)
388 {
389 QString text = tr("&%1 %2").
390 arg ((i+1) % int (MaxMRUFiles)).arg (_mru_files.at (i));
391 _mru_file_actions[i]->setText (text);
392 _mru_file_actions[i]->setData (_mru_files.at (i));
393 _mru_file_actions[i]->setVisible (true);
394 }
395 // hide unused mru-menu entries
396 for (int j = num_files; j < MaxMRUFiles; ++j)
397 _mru_file_actions[j]->setVisible (false);
398 // delete entries in string-list beyond MaxMRUFiles
399 while (_mru_files.size () > MaxMRUFiles)
400 _mru_files.removeLast ();
401 // save actual mru-list in settings
402 QSettings *settings = resource_manager::get_settings ();
403 // FIXME -- what should happen if settings is 0?
404 settings->setValue ("editor/mru_file_list",_mru_files);
358 } 405 }
359 406
360 void 407 void
361 file_editor::handle_file_name_changed (const QString& fileName) 408 file_editor::handle_file_name_changed (const QString& fileName)
362 { 409 {
518 QAction *find_action = new QAction (QIcon(":/actions/icons/find.png"), 565 QAction *find_action = new QAction (QIcon(":/actions/icons/find.png"),
519 tr ("&Find and Replace"), _tool_bar); 566 tr ("&Find and Replace"), _tool_bar);
520 567
521 _run_action = new QAction (QIcon(":/actions/icons/artsbuilderexecute.png"), 568 _run_action = new QAction (QIcon(":/actions/icons/artsbuilderexecute.png"),
522 tr("Save File And Run"), _tool_bar); 569 tr("Save File And Run"), _tool_bar);
570
571 // the mru-list and an empty array of actions
572 QSettings *settings = resource_manager::get_settings ();
573 // FIXME -- what should happen if settings is 0?
574 _mru_files = settings->value ("editor/mru_file_list").toStringList ();
575 for (int i = 0; i < MaxMRUFiles; ++i)
576 {
577 _mru_file_actions[i] = new QAction (this);
578 _mru_file_actions[i]->setVisible (false);
579 }
523 580
524 // some actions are disabled from the beginning 581 // some actions are disabled from the beginning
525 _copy_action->setEnabled(false); 582 _copy_action->setEnabled(false);
526 _cut_action->setEnabled(false); 583 _cut_action->setEnabled(false);
527 _run_action->setShortcut (Qt::ControlModifier+ Qt::Key_R); 584 _run_action->setShortcut (Qt::ControlModifier+ Qt::Key_R);
564 fileMenu->addAction (new_action); 621 fileMenu->addAction (new_action);
565 fileMenu->addAction (open_action); 622 fileMenu->addAction (open_action);
566 fileMenu->addAction (save_action); 623 fileMenu->addAction (save_action);
567 fileMenu->addAction (save_as_action); 624 fileMenu->addAction (save_as_action);
568 fileMenu->addSeparator (); 625 fileMenu->addSeparator ();
626 QMenu *mru_file_menu = new QMenu (tr ("Open &Recent"), fileMenu);
627 for (int i = 0; i < MaxMRUFiles; ++i)
628 {
629 mru_file_menu->addAction (_mru_file_actions[i]);
630 }
631 fileMenu->addMenu (mru_file_menu);
569 _menu_bar->addMenu (fileMenu); 632 _menu_bar->addMenu (fileMenu);
570 633
571 QMenu *editMenu = new QMenu (tr ("&Edit"), _menu_bar); 634 QMenu *editMenu = new QMenu (tr ("&Edit"), _menu_bar);
572 editMenu->addAction (undo_action); 635 editMenu->addAction (undo_action);
573 editMenu->addAction (redo_action); 636 editMenu->addAction (redo_action);
648 SIGNAL (triggered ()), this, SLOT (request_comment_selected_text ())); 711 SIGNAL (triggered ()), this, SLOT (request_comment_selected_text ()));
649 connect (uncomment_selection_action, 712 connect (uncomment_selection_action,
650 SIGNAL (triggered ()), this, SLOT (request_uncomment_selected_text ())); 713 SIGNAL (triggered ()), this, SLOT (request_uncomment_selected_text ()));
651 connect (find_action, 714 connect (find_action,
652 SIGNAL (triggered ()), this, SLOT (request_find ())); 715 SIGNAL (triggered ()), this, SLOT (request_find ()));
716 // The actions of the mru file menu
717 for (int i = 0; i < MaxMRUFiles; ++i)
718 {
719 connect(_mru_file_actions[i], SIGNAL (triggered ()), this, SLOT (request_mru_open_file ()));
720 }
721 mru_menu_update ();
653 connect (_tab_widget, 722 connect (_tab_widget,
654 SIGNAL (tabCloseRequested (int)), this, SLOT (handle_tab_close_request (int))); 723 SIGNAL (tabCloseRequested (int)), this, SLOT (handle_tab_close_request (int)));
655 connect (_tab_widget, 724 connect (_tab_widget,
656 SIGNAL (currentChanged(int)), this, SLOT (active_tab_changed (int))); 725 SIGNAL (currentChanged(int)), this, SLOT (active_tab_changed (int)));
657 // topLevelChanged is emitted when floating property changes (floating = true) 726 // topLevelChanged is emitted when floating property changes (floating = true)
660 resize (500, 400); 729 resize (500, 400);
661 setWindowIcon (QIcon(":/actions/icons/logo.png")); 730 setWindowIcon (QIcon(":/actions/icons/logo.png"));
662 setWindowTitle ("Editor"); 731 setWindowTitle ("Editor");
663 732
664 //restore previous session 733 //restore previous session
665 QSettings *settings = resource_manager::get_settings ();
666 if (settings->value ("editor/restoreSession",true).toBool ()) 734 if (settings->value ("editor/restoreSession",true).toBool ())
667 { 735 {
668 QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList (); 736 QStringList sessionFileNames = settings->value("editor/savedSessionTabs", QStringList()).toStringList ();
669 737
670 for (int n=0; n < sessionFileNames.count (); ++n) 738 for (int n=0; n < sessionFileNames.count (); ++n)
686 this, SLOT (handle_tab_remove_request ())); 754 this, SLOT (handle_tab_remove_request ()));
687 connect (f, SIGNAL (add_filename_to_list (const QString&)), 755 connect (f, SIGNAL (add_filename_to_list (const QString&)),
688 this, SLOT (handle_add_filename_to_list (const QString&))); 756 this, SLOT (handle_add_filename_to_list (const QString&)));
689 connect (f, SIGNAL (editor_check_conflict_save (const QString&, bool)), 757 connect (f, SIGNAL (editor_check_conflict_save (const QString&, bool)),
690 this, SLOT (check_conflict_save (const QString&, bool))); 758 this, SLOT (check_conflict_save (const QString&, bool)));
759 connect (f, SIGNAL (mru_add_file (const QString&)),
760 this, SLOT (handle_mru_add_file (const QString&)));
691 connect (f, SIGNAL (process_octave_code (const QString&)), 761 connect (f, SIGNAL (process_octave_code (const QString&)),
692 parent (), SLOT (handle_command_double_clicked (const QString&))); 762 parent (), SLOT (handle_command_double_clicked (const QString&)));
693 763
694 // Signals from the file_editor non-trivial operations 764 // Signals from the file_editor non-trivial operations
695 connect (this, SIGNAL (fetab_settings_changed ()), 765 connect (this, SIGNAL (fetab_settings_changed ()),