# HG changeset patch # User John W. Eaton # Date 1670212583 18000 # Node ID 474e184321d325ba47408942ed676a9a16c2983b # Parent 34c3cd39c4b9768b1b99f54441637e5bba3f634c move some functions from resource_manager to gui_settings * gui-settings.h, gui-settings.cc (gui_settings::get_default_font_family, gui_settings::get_default_font, gui_settings::reload, gui_settings::check): Rename and move here from resource-manager.h and resource-manager.cc. Change all uses. * resource-manager.h, resource-manager.cc (resource_manager::is_first_run): Delete. * gui-preferences-global.h (global_skip_welcome_wizard): New gui_pref object. * main-window.cc (main_window::main_window): Check this setting to determine whether to display welcome wizard. diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/gui-preferences-global.h --- a/libgui/src/gui-preferences-global.h Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/gui-preferences-global.h Sun Dec 04 22:56:23 2022 -0500 @@ -34,6 +34,9 @@ // Global preferences +const gui_pref +global_skip_welcome_wizard ("global_skip_welcome_wizard", false); + // Get the default monospaced font #if defined (Q_OS_WIN) const QString global_font_family = "Courier"; diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/gui-settings.cc --- a/libgui/src/gui-settings.cc Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/gui-settings.cc Sun Dec 04 22:56:23 2022 -0500 @@ -27,15 +27,37 @@ # include "config.h" #endif +#include + #include #include +#include +#include +#include +#include #include +#include +#include +#include "gui-preferences-cs.h" #include "gui-preferences-global.h" #include "gui-settings.h" +#include "oct-env.h" + namespace octave { + QString gui_settings::file_name (void) const + { + return fileName (); + } + + QString gui_settings::directory_name (void) const + { + QFileInfo sfile (fileName ()); + + return sfile.absolutePath (); + } QColor gui_settings::get_color_value (const QVariant& def, int mode) const { @@ -175,4 +197,150 @@ //QIcon::setThemeName (current_theme); return QIcon (); } + + QString gui_settings::get_default_font_family (void) + { + QString default_family; + + // Get all available fixed width fonts via a font combobox + QFontComboBox font_combo_box; + font_combo_box.setFontFilters (QFontComboBox::MonospacedFonts); + QStringList fonts; + + for (int index = 0; index < font_combo_box.count(); index++) + fonts << font_combo_box.itemText(index); + +#if defined (Q_OS_MAC) + // Use hard coded default on macOS, since selection of fixed width + // default font is unreliable (see bug #59128). + // Test for macOS default fixed width font + if (fonts.contains (global_mono_font.def.toString ())) + default_family = global_mono_font.def.toString (); +#endif + + // If default font is still empty (on all other platforms or + // if macOS default font is not available): use QFontDatabase + if (default_family.isEmpty ()) + { + // Get the system's default monospaced font + QFont fixed_font = QFontDatabase::systemFont (QFontDatabase::FixedFont); + default_family = fixed_font.defaultFamily (); + + // Since this might be unreliable, test all available fixed width fonts + if (! fonts.contains (default_family)) + { + // Font returned by QFontDatabase is not in fixed fonts list. + // Fallback: take first from this list + default_family = fonts[0]; + } + } + + // Test env variable which has preference + std::string env_default_family = sys::env::getenv ("OCTAVE_DEFAULT_FONT"); + if (! env_default_family.empty ()) + default_family = QString::fromStdString (env_default_family); + + return default_family; + } + + QStringList gui_settings::get_default_font (void) + { + QString default_family = get_default_font_family (); + + // determine the fefault font size of the system + // FIXME: QApplication::font () does not return the monospace font, + // but the size should be probably near to the monospace font + QFont font = QApplication::font (); + + int font_size = font.pointSize (); + if (font_size == -1) + font_size = static_cast (std::floor(font.pointSizeF ())); + + // check for valid font size, otherwise take default 10 + QString default_font_size = "10"; + if (font_size > 0) + default_font_size = QString::number (font_size); + + std::string env_default_font_size + = sys::env::getenv ("OCTAVE_DEFAULT_FONT_SIZE"); + + if (! env_default_font_size.empty ()) + default_font_size = QString::fromStdString (env_default_font_size); + + QStringList result; + result << default_family; + result << default_font_size; + return result; + } + + void gui_settings::reload (void) + { + // Declare some empty options, which may be set at first startup for + // writing them into the newly created settings file + QString custom_editor; + QStringList def_font; + + // Check whether the settings file does not yet exist + if (! QFile::exists (file_name ())) + { + // Get the default font (for terminal) + def_font = get_default_font (); + + // Get a custom editor defined as env variable + std::string env_default_editor + = sys::env::getenv ("OCTAVE_DEFAULT_EDITOR"); + + if (! env_default_editor.empty ()) + custom_editor = QString::fromStdString (env_default_editor); + } + + check (); + + // Write some settings that were dynamically determined at first startup + + // Custom editor + if (! custom_editor.isEmpty ()) + setValue (global_custom_editor.key, custom_editor); + + // Default monospace font for the terminal + if (def_font.count () > 1) + { + setValue (cs_font.key, def_font[0]); + setValue (cs_font_size.key, def_font[1].toInt ()); + } + + // Write the default monospace font into the settings for later use by + // console and editor as fallbacks of their font preferences. + setValue (global_mono_font.key, get_default_font_family ()); + } + + void gui_settings::check (void) + { + if (status () == QSettings::NoError) + { + // Test usability (force file to be really created) + setValue ("dummy", 0); + sync (); + } + + if (! (QFile::exists (file_name ()) + && isWritable () + && status () == QSettings::NoError)) + { + QString msg + = QString (QT_TR_NOOP ("Error %1 creating the settings file\n%2\n" + "Make sure you have read and write permissions to\n%3\n\n" + "Octave GUI must be closed now.")); + + QMessageBox::critical (nullptr, + QString (QT_TR_NOOP ("Octave Critical Error")), + msg.arg (status ()) + .arg (file_name ()) + .arg (directory_name ())); + + exit (1); + } + else + remove ("dummy"); // Remove test entry + } } diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/gui-settings.h --- a/libgui/src/gui-settings.h Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/gui-settings.h Sun Dec 04 22:56:23 2022 -0500 @@ -40,6 +40,9 @@ public: + // Location, name, and format of settings file determined by + // settings in qt_application class construtor. + gui_settings (QObject *parent = nullptr) : QSettings (parent) { } @@ -64,6 +67,10 @@ ~gui_settings (void) = default; + QString file_name (void) const; + + QString directory_name (void) const; + using QSettings::value; QVariant value (const gui_pref& pref) const @@ -122,18 +129,30 @@ QKeySequence sc_def_value (const sc_pref& pref) const; - // Both config_icon_theme and icon could be global functions instead - // of member functions. But at least for the icon function, - // defining it as a member function means that we can create a - // single gui_settings object and access multiple icon objects - // rather than having to create a separate settings object each time - // that an icon is needed. OTOH, creating the base QSettings object - // is supposed to be fast, so that may not matter. Hmm. + // config_icon_theme, icon, get_default_font_family, + // get_default_font, and possibly reload and check could be global + // functions instead of member functions. But at least for the icon + // function, defining it as a member function means that we can + // create a single gui_settings object and access multiple icon + // objects rather than having to create a separate settings object + // each time that an icon is needed. OTOH, creating the base + // QSettings object is supposed to be fast, so that may not matter. + // Hmm. void config_icon_theme (void); QIcon icon (const QString& icon_name, bool octave_only = false, const QString& icon_alt_name = QString ()); + + QString get_default_font_family (void); + + QStringList get_default_font (void); + + void reload (void); + + private: + + void check (void); }; } diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/main-window.cc --- a/libgui/src/main-window.cc Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/main-window.cc Sun Dec 04 22:56:23 2022 -0500 @@ -111,7 +111,9 @@ { resource_manager& rmgr = m_octave_qobj.get_resource_manager (); - if (rmgr.is_first_run ()) + gui_settings settings; + + if (! settings.value (global_skip_welcome_wizard).toBool ()) { // Before wizard. m_octave_qobj.config_translators (); @@ -121,13 +123,15 @@ if (welcomeWizard.exec () == QDialog::Rejected) exit (1); + settings.setValue (global_skip_welcome_wizard.key, QVariant (true)); + // Install settings file. - rmgr.reload_settings (); + settings.reload (); } else { // Get settings file. - rmgr.reload_settings (); + settings.reload (); // After settings. m_octave_qobj.config_translators (); @@ -135,8 +139,6 @@ setObjectName (gui_obj_name_main_window); - gui_settings settings; - settings.config_icon_theme (); rmgr.update_network_settings (); diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/octave-qobject.cc --- a/libgui/src/octave-qobject.cc Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/octave-qobject.cc Sun Dec 04 22:56:23 2022 -0500 @@ -307,13 +307,13 @@ else { // Get settings file. - m_resource_manager.reload_settings (); + gui_settings settings; + + settings.reload (); // After settings. config_translators (); - gui_settings settings; - settings.config_icon_theme (); // Initilize the shortcut-manager diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/resource-manager.cc --- a/libgui/src/resource-manager.cc Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/resource-manager.cc Sun Dec 04 22:56:23 2022 -0500 @@ -35,10 +35,7 @@ #include #include -#include -#include #include -#include #include #include @@ -59,26 +56,12 @@ #include "oct-env.h" #include "defaults.h" -#include "error.h" -#include "help.h" namespace octave { resource_manager::resource_manager (void) - : m_settings_directory (), m_settings_file (), m_temporary_files () - { - // Location, name, and format of settings file determined by - // settings in qt_application class construtor. - - check_settings (); - - gui_settings settings; - - m_settings_file = settings.fileName (); - - QFileInfo sfile (m_settings_file); - m_settings_directory = sfile.absolutePath (); - } + : m_temporary_files () + { } resource_manager::~resource_manager (void) { @@ -142,134 +125,6 @@ } - QString resource_manager::get_settings_directory (void) - { - return m_settings_directory; - } - - QString resource_manager::get_settings_file (void) - { - return m_settings_file; - } - - QString resource_manager::get_default_font_family (void) - { - QString default_family; - - // Get all available fixed width fonts via a font combobox - QFontComboBox font_combo_box; - font_combo_box.setFontFilters (QFontComboBox::MonospacedFonts); - QStringList fonts; - - for (int index = 0; index < font_combo_box.count(); index++) - fonts << font_combo_box.itemText(index); - -#if defined (Q_OS_MAC) - // Use hard coded default on macOS, since selection of fixed width - // default font is unreliable (see bug #59128). - // Test for macOS default fixed width font - if (fonts.contains (global_mono_font.def.toString ())) - default_family = global_mono_font.def.toString (); -#endif - - // If default font is still empty (on all other platforms or - // if macOS default font is not available): use QFontDatabase - if (default_family.isEmpty ()) - { - // Get the system's default monospaced font - QFont fixed_font = QFontDatabase::systemFont (QFontDatabase::FixedFont); - default_family = fixed_font.defaultFamily (); - - // Since this might be unreliable, test all available fixed width fonts - if (! fonts.contains (default_family)) - { - // Font returned by QFontDatabase is not in fixed fonts list. - // Fallback: take first from this list - default_family = fonts[0]; - } - } - - // Test env variable which has preference - std::string env_default_family = sys::env::getenv ("OCTAVE_DEFAULT_FONT"); - if (! env_default_family.empty ()) - default_family = QString::fromStdString (env_default_family); - - return default_family; - } - - QStringList resource_manager::get_default_font (void) - { - QString default_family = get_default_font_family (); - - // determine the fefault font size of the system - // FIXME: QApplication::font () does not return the monospace font, - // but the size should be probably near to the monospace font - QFont font = QApplication::font (); - - int font_size = font.pointSize (); - if (font_size == -1) - font_size = static_cast (std::floor(font.pointSizeF ())); - - // check for valid font size, otherwise take default 10 - QString default_font_size = "10"; - if (font_size > 0) - default_font_size = QString::number (font_size); - - std::string env_default_font_size - = sys::env::getenv ("OCTAVE_DEFAULT_FONT_SIZE"); - - if (! env_default_font_size.empty ()) - default_font_size = QString::fromStdString (env_default_font_size); - - QStringList result; - result << default_family; - result << default_font_size; - return result; - } - - void resource_manager::reload_settings (void) - { - // Declare some empty options, which may be set at first startup for - // writing them into the newly created settings file - QString custom_editor; - QStringList def_font; - - // Check whether the settings file does not yet exist - if (! QFile::exists (m_settings_file)) - { - // Get the default font (for terminal) - def_font = get_default_font (); - - // Get a custom editor defined as env variable - std::string env_default_editor - = sys::env::getenv ("OCTAVE_DEFAULT_EDITOR"); - - if (! env_default_editor.empty ()) - custom_editor = QString::fromStdString (env_default_editor); - } - - check_settings (); - - gui_settings settings; - - // Write some settings that were dynamically determined at first startup - - // Custom editor - if (! custom_editor.isEmpty ()) - settings.setValue (global_custom_editor.key, custom_editor); - - // Default monospace font for the terminal - if (def_font.count () > 1) - { - settings.setValue (cs_font.key, def_font[0]); - settings.setValue (cs_font_size.key, def_font[1].toInt ()); - } - - // Write the default monospace font into the settings for later use by - // console and editor as fallbacks of their font preferences. - settings.setValue (global_mono_font.key, get_default_font_family ()); - } - #if defined (HAVE_QSCINTILLA) int resource_manager::get_valid_lexer_styles (QsciLexer *lexer, int *styles) { @@ -330,7 +185,7 @@ // and convert the color by inverting the lightness // Get the default font - QStringList def_font = get_default_font (); + QStringList def_font = settings.get_default_font (); QFont df (def_font[0], def_font[1].toInt ()); QFont dfa = copy_font_attributes (lexer->defaultFont (), df); lexer->setDefaultFont (dfa); @@ -370,38 +225,6 @@ } #endif - void resource_manager::check_settings (void) - { - gui_settings settings; - - if (settings.status () == QSettings::NoError) - { - // Test usability (force file to be really created) - settings.setValue ("dummy", 0); - settings.sync (); - } - - if (! (QFile::exists (settings.fileName ()) - && settings.isWritable () - && settings.status () == QSettings::NoError)) - { - QString msg - = QString (QT_TR_NOOP ("Error %1 creating the settings file\n%2\n" - "Make sure you have read and write permissions to\n%3\n\n" - "Octave GUI must be closed now.")); - - QMessageBox::critical (nullptr, - QString (QT_TR_NOOP ("Octave Critical Error")), - msg.arg (settings.status ()) - .arg (get_settings_file ()) - .arg (get_settings_directory ())); - - exit (1); - } - else - settings.remove ("dummy"); // Remove test entry - } - bool resource_manager::update_settings_key (const QString& old_key, const QString& new_key) { @@ -418,11 +241,6 @@ return false; } - bool resource_manager::is_first_run (void) const - { - return ! QFile::exists (m_settings_file); - } - void resource_manager::update_network_settings (void) { QNetworkProxy proxy; diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/resource-manager.h --- a/libgui/src/resource-manager.h Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/resource-manager.h Sun Dec 04 22:56:23 2022 -0500 @@ -59,22 +59,12 @@ void config_translators (QTranslator *qt_tr, QTranslator *qsci_tr, QTranslator *gui_tr); - QString get_settings_directory (void); - - QString get_settings_file (void); - - QString get_default_font_family (void); - - QStringList get_default_font (void); - QPointer create_tmp_file (const QString& extension = QString (), const QString& contents = QString ()); void remove_tmp_file (QPointer tmp_file); - void reload_settings (void); - #if defined (HAVE_QSCINTILLA) int get_valid_lexer_styles (QsciLexer *lexer, int *styles); void read_lexer_settings (QsciLexer *lexer, int mode = 0, int def = 0); @@ -82,8 +72,6 @@ bool update_settings_key (const QString& new_key, const QString& old_key); - bool is_first_run (void) const; - void update_network_settings (void); void get_codecs (QStringList *codecs); @@ -92,8 +80,6 @@ private: - void check_settings (void); - /*! * Copys the attributes bold, italic and underline from QFont * @p attr to the font @p base and returns the result without @@ -103,10 +89,6 @@ */ QFont copy_font_attributes (const QFont& attr, const QFont& base) const; - QString m_settings_directory; - - QString m_settings_file; - QList m_temporary_files; }; } diff -r 34c3cd39c4b9 -r 474e184321d3 libgui/src/welcome-wizard.cc --- a/libgui/src/welcome-wizard.cc Sun Dec 04 15:29:51 2022 -0500 +++ b/libgui/src/welcome-wizard.cc Sun Dec 04 22:56:23 2022 -0500 @@ -144,10 +144,9 @@ { // Create default settings file. - resource_manager& rmgr = m_octave_qobj.get_resource_manager (); - rmgr.reload_settings (); + gui_settings settings; - gui_settings settings; + settings.reload (); settings.setValue (nr_allow_connection.key, m_allow_web_connect_state); @@ -157,7 +156,7 @@ QDialog::accept (); } - initial_page::initial_page (base_qobject& oct_qobj, welcome_wizard *wizard) + initial_page::initial_page (base_qobject&, welcome_wizard *wizard) : QWidget (wizard), m_title (new QLabel (tr ("Welcome to Octave!"), this)), m_message (new QLabel (this)), @@ -169,7 +168,7 @@ ft.setPointSize (20); m_title->setFont (ft); - resource_manager& rmgr = oct_qobj.get_resource_manager (); + gui_settings settings; m_message->setText (tr ("\n" @@ -177,7 +176,7 @@ "Click 'Next' to create a configuration file and launch Octave.

\n" "

The configuration file is stored in
%1.

\n" ""). - arg (rmgr.get_settings_file ())); + arg (settings.file_name ())); m_message->setWordWrap (true); m_message->setMinimumWidth (400);