comparison libgui/src/resource-manager.cc @ 31638:474e184321d3

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.
author John W. Eaton <jwe@octave.org>
date Sun, 04 Dec 2022 22:56:23 -0500
parents 2fbbf9c07327
children ca7d58406f82
comparison
equal deleted inserted replaced
31637:34c3cd39c4b9 31638:474e184321d3
33 #include <array> 33 #include <array>
34 #include <string> 34 #include <string>
35 35
36 #include <QDir> 36 #include <QDir>
37 #include <QFile> 37 #include <QFile>
38 #include <QFontComboBox>
39 #include <QFontDatabase>
40 #include <QLibraryInfo> 38 #include <QLibraryInfo>
41 #include <QMessageBox>
42 #include <QNetworkProxy> 39 #include <QNetworkProxy>
43 #include <QStandardPaths> 40 #include <QStandardPaths>
44 41
45 #include <QTextCodec> 42 #include <QTextCodec>
46 43
57 #include "file-ops.h" 54 #include "file-ops.h"
58 #include "localcharset-wrapper.h" 55 #include "localcharset-wrapper.h"
59 #include "oct-env.h" 56 #include "oct-env.h"
60 57
61 #include "defaults.h" 58 #include "defaults.h"
62 #include "error.h"
63 #include "help.h"
64 59
65 namespace octave 60 namespace octave
66 { 61 {
67 resource_manager::resource_manager (void) 62 resource_manager::resource_manager (void)
68 : m_settings_directory (), m_settings_file (), m_temporary_files () 63 : m_temporary_files ()
69 { 64 { }
70 // Location, name, and format of settings file determined by
71 // settings in qt_application class construtor.
72
73 check_settings ();
74
75 gui_settings settings;
76
77 m_settings_file = settings.fileName ();
78
79 QFileInfo sfile (m_settings_file);
80 m_settings_directory = sfile.absolutePath ();
81 }
82 65
83 resource_manager::~resource_manager (void) 66 resource_manager::~resource_manager (void)
84 { 67 {
85 for (int i = m_temporary_files.count () - 1; i >=0; i--) 68 for (int i = m_temporary_files.count () - 1; i >=0; i--)
86 remove_tmp_file (m_temporary_files.at (i)); 69 remove_tmp_file (m_temporary_files.at (i));
138 qsci_tr->load ("qscintilla_" + language.toLower (), qt_trans_dir); 121 qsci_tr->load ("qscintilla_" + language.toLower (), qt_trans_dir);
139 122
140 gui_tr->load (language, get_gui_translation_dir ()); 123 gui_tr->load (language, get_gui_translation_dir ());
141 } 124 }
142 125
143 }
144
145 QString resource_manager::get_settings_directory (void)
146 {
147 return m_settings_directory;
148 }
149
150 QString resource_manager::get_settings_file (void)
151 {
152 return m_settings_file;
153 }
154
155 QString resource_manager::get_default_font_family (void)
156 {
157 QString default_family;
158
159 // Get all available fixed width fonts via a font combobox
160 QFontComboBox font_combo_box;
161 font_combo_box.setFontFilters (QFontComboBox::MonospacedFonts);
162 QStringList fonts;
163
164 for (int index = 0; index < font_combo_box.count(); index++)
165 fonts << font_combo_box.itemText(index);
166
167 #if defined (Q_OS_MAC)
168 // Use hard coded default on macOS, since selection of fixed width
169 // default font is unreliable (see bug #59128).
170 // Test for macOS default fixed width font
171 if (fonts.contains (global_mono_font.def.toString ()))
172 default_family = global_mono_font.def.toString ();
173 #endif
174
175 // If default font is still empty (on all other platforms or
176 // if macOS default font is not available): use QFontDatabase
177 if (default_family.isEmpty ())
178 {
179 // Get the system's default monospaced font
180 QFont fixed_font = QFontDatabase::systemFont (QFontDatabase::FixedFont);
181 default_family = fixed_font.defaultFamily ();
182
183 // Since this might be unreliable, test all available fixed width fonts
184 if (! fonts.contains (default_family))
185 {
186 // Font returned by QFontDatabase is not in fixed fonts list.
187 // Fallback: take first from this list
188 default_family = fonts[0];
189 }
190 }
191
192 // Test env variable which has preference
193 std::string env_default_family = sys::env::getenv ("OCTAVE_DEFAULT_FONT");
194 if (! env_default_family.empty ())
195 default_family = QString::fromStdString (env_default_family);
196
197 return default_family;
198 }
199
200 QStringList resource_manager::get_default_font (void)
201 {
202 QString default_family = get_default_font_family ();
203
204 // determine the fefault font size of the system
205 // FIXME: QApplication::font () does not return the monospace font,
206 // but the size should be probably near to the monospace font
207 QFont font = QApplication::font ();
208
209 int font_size = font.pointSize ();
210 if (font_size == -1)
211 font_size = static_cast <int> (std::floor(font.pointSizeF ()));
212
213 // check for valid font size, otherwise take default 10
214 QString default_font_size = "10";
215 if (font_size > 0)
216 default_font_size = QString::number (font_size);
217
218 std::string env_default_font_size
219 = sys::env::getenv ("OCTAVE_DEFAULT_FONT_SIZE");
220
221 if (! env_default_font_size.empty ())
222 default_font_size = QString::fromStdString (env_default_font_size);
223
224 QStringList result;
225 result << default_family;
226 result << default_font_size;
227 return result;
228 }
229
230 void resource_manager::reload_settings (void)
231 {
232 // Declare some empty options, which may be set at first startup for
233 // writing them into the newly created settings file
234 QString custom_editor;
235 QStringList def_font;
236
237 // Check whether the settings file does not yet exist
238 if (! QFile::exists (m_settings_file))
239 {
240 // Get the default font (for terminal)
241 def_font = get_default_font ();
242
243 // Get a custom editor defined as env variable
244 std::string env_default_editor
245 = sys::env::getenv ("OCTAVE_DEFAULT_EDITOR");
246
247 if (! env_default_editor.empty ())
248 custom_editor = QString::fromStdString (env_default_editor);
249 }
250
251 check_settings ();
252
253 gui_settings settings;
254
255 // Write some settings that were dynamically determined at first startup
256
257 // Custom editor
258 if (! custom_editor.isEmpty ())
259 settings.setValue (global_custom_editor.key, custom_editor);
260
261 // Default monospace font for the terminal
262 if (def_font.count () > 1)
263 {
264 settings.setValue (cs_font.key, def_font[0]);
265 settings.setValue (cs_font_size.key, def_font[1].toInt ());
266 }
267
268 // Write the default monospace font into the settings for later use by
269 // console and editor as fallbacks of their font preferences.
270 settings.setValue (global_mono_font.key, get_default_font_family ());
271 } 126 }
272 127
273 #if defined (HAVE_QSCINTILLA) 128 #if defined (HAVE_QSCINTILLA)
274 int resource_manager::get_valid_lexer_styles (QsciLexer *lexer, int *styles) 129 int resource_manager::get_valid_lexer_styles (QsciLexer *lexer, int *styles)
275 { 130 {
328 // If mode == 0, take all settings except font from default lexer 183 // If mode == 0, take all settings except font from default lexer
329 // If Mode == 1, take all settings except font from default lexer 184 // If Mode == 1, take all settings except font from default lexer
330 // and convert the color by inverting the lightness 185 // and convert the color by inverting the lightness
331 186
332 // Get the default font 187 // Get the default font
333 QStringList def_font = get_default_font (); 188 QStringList def_font = settings.get_default_font ();
334 QFont df (def_font[0], def_font[1].toInt ()); 189 QFont df (def_font[0], def_font[1].toInt ());
335 QFont dfa = copy_font_attributes (lexer->defaultFont (), df); 190 QFont dfa = copy_font_attributes (lexer->defaultFont (), df);
336 lexer->setDefaultFont (dfa); 191 lexer->setDefaultFont (dfa);
337 192
338 QColor c, p; 193 QColor c, p;
368 lexer->readSettings (settings, group_str.c_str ()); 223 lexer->readSettings (settings, group_str.c_str ());
369 } 224 }
370 } 225 }
371 #endif 226 #endif
372 227
373 void resource_manager::check_settings (void)
374 {
375 gui_settings settings;
376
377 if (settings.status () == QSettings::NoError)
378 {
379 // Test usability (force file to be really created)
380 settings.setValue ("dummy", 0);
381 settings.sync ();
382 }
383
384 if (! (QFile::exists (settings.fileName ())
385 && settings.isWritable ()
386 && settings.status () == QSettings::NoError))
387 {
388 QString msg
389 = QString (QT_TR_NOOP ("Error %1 creating the settings file\n%2\n"
390 "Make sure you have read and write permissions to\n%3\n\n"
391 "Octave GUI must be closed now."));
392
393 QMessageBox::critical (nullptr,
394 QString (QT_TR_NOOP ("Octave Critical Error")),
395 msg.arg (settings.status ())
396 .arg (get_settings_file ())
397 .arg (get_settings_directory ()));
398
399 exit (1);
400 }
401 else
402 settings.remove ("dummy"); // Remove test entry
403 }
404
405 bool resource_manager::update_settings_key (const QString& old_key, 228 bool resource_manager::update_settings_key (const QString& old_key,
406 const QString& new_key) 229 const QString& new_key)
407 { 230 {
408 gui_settings settings; 231 gui_settings settings;
409 232
414 settings.remove (old_key); 237 settings.remove (old_key);
415 return true; 238 return true;
416 } 239 }
417 240
418 return false; 241 return false;
419 }
420
421 bool resource_manager::is_first_run (void) const
422 {
423 return ! QFile::exists (m_settings_file);
424 } 242 }
425 243
426 void resource_manager::update_network_settings (void) 244 void resource_manager::update_network_settings (void)
427 { 245 {
428 QNetworkProxy proxy; 246 QNetworkProxy proxy;