comparison libgui/src/main-window.cc @ 23380:c319e6d737f2

Backed out changeset 7332287221a9
author Torsten <mttl@mailbox.org>
date Mon, 10 Apr 2017 07:02:42 +0200
parents 2a122c3fd80f
children 41639665aa34
comparison
equal deleted inserted replaced
23379:2a122c3fd80f 23380:c319e6d737f2
25 # include "config.h" 25 # include "config.h"
26 #endif 26 #endif
27 27
28 #include <QKeySequence> 28 #include <QKeySequence>
29 #include <QApplication> 29 #include <QApplication>
30 #include <QInputDialog>
31 #include <QLabel> 30 #include <QLabel>
32 #include <QMenuBar> 31 #include <QMenuBar>
33 #include <QMenu> 32 #include <QMenu>
34 #include <QAction> 33 #include <QAction>
35 #include <QSettings> 34 #include <QSettings>
63 #include "defaults.h" 62 #include "defaults.h"
64 #include "octave.h" 63 #include "octave.h"
65 #include "symtab.h" 64 #include "symtab.h"
66 #include "version.h" 65 #include "version.h"
67 #include "utils.h" 66 #include "utils.h"
68 #include <oct-map.h> 67
69 68 static file_editor_interface *
69 create_default_editor (QWidget *p)
70 {
71 #if defined (HAVE_QSCINTILLA)
72 return new file_editor (p);
73 #else
74 octave_unused_parameter (p);
75
76 return 0;
77 #endif
78 }
70 79
71 octave_interpreter::octave_interpreter (octave::application *app_context) 80 octave_interpreter::octave_interpreter (octave::application *app_context)
72 : QObject (), thread_manager (), m_app_context (app_context) 81 : QObject (), thread_manager (), m_app_context (app_context)
73 { } 82 { }
74 83
144 status_bar = new QStatusBar (); 153 status_bar = new QStatusBar ();
145 command_window = new terminal_dock_widget (this); 154 command_window = new terminal_dock_widget (this);
146 history_window = new history_dock_widget (this); 155 history_window = new history_dock_widget (this);
147 file_browser_window = new files_dock_widget (this); 156 file_browser_window = new files_dock_widget (this);
148 doc_browser_window = new documentation_dock_widget (this); 157 doc_browser_window = new documentation_dock_widget (this);
149 #if defined (HAVE_QSCINTILLA) 158 editor_window = create_default_editor (this);
150 editor_window = new file_editor (this);
151 _external_editor = 0;
152 _active_editor = editor_window; // for connecting signals
153 #else
154 editor_window = 0;
155 _external_editor = new external_editor_interface (p);
156 _active_editor = _external_editor; // for connecting signals
157 #endif
158 workspace_window = new workspace_view (this); 159 workspace_window = new workspace_view (this);
159 } 160 }
160 161
161 QSettings *settings = resource_manager::get_settings (); 162 QSettings *settings = resource_manager::get_settings ();
162 163
198 { 199 {
199 // Destroy the terminal first so that STDERR stream is redirected back 200 // Destroy the terminal first so that STDERR stream is redirected back
200 // to its original pipe to capture error messages at exit. 201 // to its original pipe to capture error messages at exit.
201 202
202 delete editor_window; // first one for dialogs of modified editor-tabs 203 delete editor_window; // first one for dialogs of modified editor-tabs
203 delete _external_editor;
204 delete command_window; 204 delete command_window;
205 delete workspace_window; 205 delete workspace_window;
206 delete doc_browser_window; 206 delete doc_browser_window;
207 delete file_browser_window; 207 delete file_browser_window;
208 delete history_window; 208 delete history_window;
310 } 310 }
311 void 311 void
312 312
313 main_window::edit_mfile (const QString& name, int line) 313 main_window::edit_mfile (const QString& name, int line)
314 { 314 {
315 handle_edit_mfile_request (name, QString (), QString (), line); 315 emit edit_mfile_request (name, QString (), QString (), line);
316 } 316 }
317 317
318 void 318 void
319 main_window::report_status_message (const QString& statusMessage) 319 main_window::report_status_message (const QString& statusMessage)
320 { 320 {
1388 1388
1389 file_dialog->setAttribute (Qt::WA_DeleteOnClose); 1389 file_dialog->setAttribute (Qt::WA_DeleteOnClose);
1390 file_dialog->show (); 1390 file_dialog->show ();
1391 } 1391 }
1392 1392
1393
1394 //
1395 // Functions related to file editing
1396 //
1397 // These are moved from editor to here for also using them when octave
1398 // is built without qscintilla
1399 //
1400 void
1401 main_window::handle_edit_mfile_request (const QString& fname,
1402 const QString& ffile,
1403 const QString& curr_dir, int line)
1404 {
1405 // Is it a regular function within the search path? (Call __which__)
1406 octave_value_list fct = F__which__ (ovl (fname.toStdString ()),0);
1407 octave_map map = fct(0).map_value ();
1408
1409 QString type = QString::fromStdString (
1410 map.contents ("type").data ()[0].string_value ());
1411 QString name = QString::fromStdString (
1412 map.contents ("name").data ()[0].string_value ());
1413
1414 QString message = QString ();
1415 QString filename = QString ();
1416
1417 if (type == QString ("built-in function"))
1418 {
1419 // built in function: can't edit
1420 message = tr ("%1 is a built-in function");
1421 }
1422 else if (type.isEmpty ())
1423 {
1424 // function not known to octave -> try directory of edited file
1425 // get directory
1426 QDir dir;
1427 if (ffile.isEmpty ())
1428 {
1429 if (curr_dir.isEmpty ())
1430 dir = QDir (_current_directory_combo_box->itemText (0));
1431 else
1432 dir = QDir (curr_dir);
1433 }
1434 else
1435 dir = QDir (QFileInfo (ffile).canonicalPath ());
1436
1437 // function not known to octave -> try directory of edited file
1438 QFileInfo file = QFileInfo (dir, fname + ".m");
1439
1440 if (file.exists ())
1441 {
1442 filename = file.canonicalFilePath (); // local file exists
1443 }
1444 else
1445 {
1446 // local file does not exist -> try private directory
1447 file = QFileInfo (ffile);
1448 file = QFileInfo (QDir (file.canonicalPath () + "/private"),
1449 fname + ".m");
1450
1451 if (file.exists ())
1452 {
1453 filename = file.canonicalFilePath (); // private function exists
1454 }
1455 else
1456 {
1457 message = tr ("Can not find function %1"); // no file found
1458 }
1459 }
1460 }
1461
1462 if (! message.isEmpty ())
1463 {
1464 QMessageBox *msgBox
1465 = new QMessageBox (QMessageBox::Critical,
1466 tr ("Octave Editor"),
1467 message.arg (name),
1468 QMessageBox::Ok, this);
1469
1470 msgBox->setWindowModality (Qt::NonModal);
1471 msgBox->setAttribute (Qt::WA_DeleteOnClose);
1472 msgBox->show ();
1473 return;
1474 }
1475
1476 if (filename.isEmpty ())
1477 filename = QString::fromStdString (
1478 map.contents ("file").data ()[0].string_value ());
1479
1480 if (! filename.endsWith (".m"))
1481 filename.append (".m");
1482
1483 emit open_file_signal (filename, QString (), line); // default encoding
1484 }
1485
1486 // Create a new script
1487 void
1488 main_window::request_new_script (const QString& commands)
1489 {
1490 emit new_file_signal (commands);
1491 }
1492
1493 // Create a new function and open it
1494 void
1495 main_window::request_new_function (bool)
1496 {
1497 bool ok;
1498 // Get the name of the new function: Parent of the input dialog is the
1499 // editor window or the main window. The latter is chosen, if a custom
1500 // editor is used or qscintilla is not available
1501 QWidget *p = editor_window;
1502 QSettings *settings = resource_manager::get_settings ();
1503 if (! p || settings->value ("useCustomFileEditor",false).toBool ())
1504 p = this;
1505 QString new_name = QInputDialog::getText (p, tr ("New Function"),
1506 tr ("New function name:\n"), QLineEdit::Normal, "", &ok);
1507
1508 if (ok && new_name.length () > 0)
1509 {
1510 // append suffix if it not already exists
1511 if (new_name.rightRef (2) != ".m")
1512 new_name.append (".m");
1513 // check whether new files are created without prompt
1514 QSettings *settings = resource_manager::get_settings ();
1515 if (! settings->value ("editor/create_new_file",false).toBool ())
1516 {
1517 // no, so enable this settings and wait for end of new file loading
1518 settings->setValue ("editor/create_new_file",true);
1519 connect (this, SIGNAL (file_loaded_signal ()),
1520 this, SLOT (restore_create_file_setting ()));
1521 }
1522 // start the edit command
1523 execute_command_in_terminal ("edit " + new_name);
1524 }
1525 }
1526
1527 void
1528 main_window::restore_create_file_setting ()
1529 {
1530 // restore the new files creation setting
1531 QSettings *settings = resource_manager::get_settings ();
1532 settings->setValue ("editor/create_new_file",false);
1533 disconnect (this, SIGNAL (file_loaded_signal ()),
1534 this, SLOT (restore_create_file_setting ()));
1535 }
1536
1537
1538 //
1539 // Main subroutine of the constructor 1393 // Main subroutine of the constructor
1540 //
1541 void 1394 void
1542 main_window::construct (void) 1395 main_window::construct (void)
1543 { 1396 {
1544 _closing = false; // flag for editor files when closed 1397 _closing = false; // flag for editor files when closed
1545 1398
1791 1644
1792 connect (_octave_qt_link, 1645 connect (_octave_qt_link,
1793 SIGNAL (show_preferences_signal (void)), 1646 SIGNAL (show_preferences_signal (void)),
1794 this, SLOT (process_settings_dialog_request ())); 1647 this, SLOT (process_settings_dialog_request ()));
1795 1648
1649 #if defined (HAVE_QSCINTILLA)
1796 connect (_octave_qt_link, 1650 connect (_octave_qt_link,
1797 SIGNAL (edit_file_signal (const QString&)), 1651 SIGNAL (edit_file_signal (const QString&)),
1798 _active_editor, 1652 editor_window,
1799 SLOT (handle_edit_file_request (const QString&))); 1653 SLOT (handle_edit_file_request (const QString&)));
1654 #endif
1800 1655
1801 connect (_octave_qt_link, 1656 connect (_octave_qt_link,
1802 SIGNAL (insert_debugger_pointer_signal (const QString&, int)), 1657 SIGNAL (insert_debugger_pointer_signal (const QString&, int)),
1803 this, 1658 this,
1804 SLOT (handle_insert_debugger_pointer_request (const QString&, 1659 SLOT (handle_insert_debugger_pointer_request (const QString&,
1937 file_menu->addSeparator (); 1792 file_menu->addSeparator ();
1938 1793
1939 _exit_action = file_menu->addAction (tr ("Exit")); 1794 _exit_action = file_menu->addAction (tr ("Exit"));
1940 _exit_action->setShortcutContext (Qt::ApplicationShortcut); 1795 _exit_action->setShortcutContext (Qt::ApplicationShortcut);
1941 1796
1797 #if defined (HAVE_QSCINTILLA)
1942 connect (_open_action, SIGNAL (triggered ()), 1798 connect (_open_action, SIGNAL (triggered ()),
1943 _active_editor, SLOT (request_open_file ())); 1799 editor_window, SLOT (request_open_file ()));
1800 #endif
1944 1801
1945 connect (_load_workspace_action, SIGNAL (triggered ()), 1802 connect (_load_workspace_action, SIGNAL (triggered ()),
1946 this, SLOT (handle_load_workspace_request ())); 1803 this, SLOT (handle_load_workspace_request ()));
1947 1804
1948 connect (_save_workspace_action, SIGNAL (triggered ()), 1805 connect (_save_workspace_action, SIGNAL (triggered ()),
1967 _new_function_action->setShortcutContext (Qt::ApplicationShortcut); 1824 _new_function_action->setShortcutContext (Qt::ApplicationShortcut);
1968 1825
1969 _new_figure_action = new_menu->addAction (tr ("New Figure")); 1826 _new_figure_action = new_menu->addAction (tr ("New Figure"));
1970 _new_figure_action->setEnabled (true); 1827 _new_figure_action->setEnabled (true);
1971 1828
1829 #if defined (HAVE_QSCINTILLA)
1972 connect (_new_script_action, SIGNAL (triggered ()), 1830 connect (_new_script_action, SIGNAL (triggered ()),
1973 this, SLOT (request_new_script ())); 1831 editor_window, SLOT (request_new_script ()));
1832
1974 connect (_new_function_action, SIGNAL (triggered ()), 1833 connect (_new_function_action, SIGNAL (triggered ()),
1975 this, SLOT (request_new_function ())); 1834 editor_window, SLOT (request_new_function ()));
1976 connect (this, SIGNAL (new_file_signal (const QString&)), 1835 #endif
1977 _active_editor, SLOT (request_new_file (const QString&)));
1978 connect (this, SIGNAL (open_file_signal (const QString&)),
1979 _active_editor, SLOT (request_open_file (const QString&)));
1980 connect (this,
1981 SIGNAL (open_file_signal (const QString&, const QString&, int)),
1982 _active_editor,
1983 SLOT (request_open_file (const QString&, const QString&, int)));
1984 1836
1985 connect (_new_figure_action, SIGNAL (triggered ()), 1837 connect (_new_figure_action, SIGNAL (triggered ()),
1986 this, SLOT (handle_new_figure_request ())); 1838 this, SLOT (handle_new_figure_request ()));
1987 } 1839 }
1988 1840