comparison libgui/src/gui-settings.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 0645ea65ca6b
children ab33554f97d2
comparison
equal deleted inserted replaced
31637:34c3cd39c4b9 31638:474e184321d3
25 25
26 #if defined (HAVE_CONFIG_H) 26 #if defined (HAVE_CONFIG_H)
27 # include "config.h" 27 # include "config.h"
28 #endif 28 #endif
29 29
30 #include <cmath>
31
30 #include <QApplication> 32 #include <QApplication>
31 #include <QFile> 33 #include <QFile>
34 #include <QFileInfo>
35 #include <QFontComboBox>
36 #include <QFontDatabase>
37 #include <QMessageBox>
32 #include <QSettings> 38 #include <QSettings>
33 39 #include <QString>
40 #include <QStringList>
41
42 #include "gui-preferences-cs.h"
34 #include "gui-preferences-global.h" 43 #include "gui-preferences-global.h"
35 #include "gui-settings.h" 44 #include "gui-settings.h"
36 45
46 #include "oct-env.h"
47
37 namespace octave 48 namespace octave
38 { 49 {
50 QString gui_settings::file_name (void) const
51 {
52 return fileName ();
53 }
54
55 QString gui_settings::directory_name (void) const
56 {
57 QFileInfo sfile (fileName ());
58
59 return sfile.absolutePath ();
60 }
39 61
40 QColor gui_settings::get_color_value (const QVariant& def, int mode) const 62 QColor gui_settings::get_color_value (const QVariant& def, int mode) const
41 { 63 {
42 QColor default_color; 64 QColor default_color;
43 65
173 } 195 }
174 196
175 //QIcon::setThemeName (current_theme); 197 //QIcon::setThemeName (current_theme);
176 return QIcon (); 198 return QIcon ();
177 } 199 }
200
201 QString gui_settings::get_default_font_family (void)
202 {
203 QString default_family;
204
205 // Get all available fixed width fonts via a font combobox
206 QFontComboBox font_combo_box;
207 font_combo_box.setFontFilters (QFontComboBox::MonospacedFonts);
208 QStringList fonts;
209
210 for (int index = 0; index < font_combo_box.count(); index++)
211 fonts << font_combo_box.itemText(index);
212
213 #if defined (Q_OS_MAC)
214 // Use hard coded default on macOS, since selection of fixed width
215 // default font is unreliable (see bug #59128).
216 // Test for macOS default fixed width font
217 if (fonts.contains (global_mono_font.def.toString ()))
218 default_family = global_mono_font.def.toString ();
219 #endif
220
221 // If default font is still empty (on all other platforms or
222 // if macOS default font is not available): use QFontDatabase
223 if (default_family.isEmpty ())
224 {
225 // Get the system's default monospaced font
226 QFont fixed_font = QFontDatabase::systemFont (QFontDatabase::FixedFont);
227 default_family = fixed_font.defaultFamily ();
228
229 // Since this might be unreliable, test all available fixed width fonts
230 if (! fonts.contains (default_family))
231 {
232 // Font returned by QFontDatabase is not in fixed fonts list.
233 // Fallback: take first from this list
234 default_family = fonts[0];
235 }
236 }
237
238 // Test env variable which has preference
239 std::string env_default_family = sys::env::getenv ("OCTAVE_DEFAULT_FONT");
240 if (! env_default_family.empty ())
241 default_family = QString::fromStdString (env_default_family);
242
243 return default_family;
244 }
245
246 QStringList gui_settings::get_default_font (void)
247 {
248 QString default_family = get_default_font_family ();
249
250 // determine the fefault font size of the system
251 // FIXME: QApplication::font () does not return the monospace font,
252 // but the size should be probably near to the monospace font
253 QFont font = QApplication::font ();
254
255 int font_size = font.pointSize ();
256 if (font_size == -1)
257 font_size = static_cast <int> (std::floor(font.pointSizeF ()));
258
259 // check for valid font size, otherwise take default 10
260 QString default_font_size = "10";
261 if (font_size > 0)
262 default_font_size = QString::number (font_size);
263
264 std::string env_default_font_size
265 = sys::env::getenv ("OCTAVE_DEFAULT_FONT_SIZE");
266
267 if (! env_default_font_size.empty ())
268 default_font_size = QString::fromStdString (env_default_font_size);
269
270 QStringList result;
271 result << default_family;
272 result << default_font_size;
273 return result;
274 }
275
276 void gui_settings::reload (void)
277 {
278 // Declare some empty options, which may be set at first startup for
279 // writing them into the newly created settings file
280 QString custom_editor;
281 QStringList def_font;
282
283 // Check whether the settings file does not yet exist
284 if (! QFile::exists (file_name ()))
285 {
286 // Get the default font (for terminal)
287 def_font = get_default_font ();
288
289 // Get a custom editor defined as env variable
290 std::string env_default_editor
291 = sys::env::getenv ("OCTAVE_DEFAULT_EDITOR");
292
293 if (! env_default_editor.empty ())
294 custom_editor = QString::fromStdString (env_default_editor);
295 }
296
297 check ();
298
299 // Write some settings that were dynamically determined at first startup
300
301 // Custom editor
302 if (! custom_editor.isEmpty ())
303 setValue (global_custom_editor.key, custom_editor);
304
305 // Default monospace font for the terminal
306 if (def_font.count () > 1)
307 {
308 setValue (cs_font.key, def_font[0]);
309 setValue (cs_font_size.key, def_font[1].toInt ());
310 }
311
312 // Write the default monospace font into the settings for later use by
313 // console and editor as fallbacks of their font preferences.
314 setValue (global_mono_font.key, get_default_font_family ());
315 }
316
317 void gui_settings::check (void)
318 {
319 if (status () == QSettings::NoError)
320 {
321 // Test usability (force file to be really created)
322 setValue ("dummy", 0);
323 sync ();
324 }
325
326 if (! (QFile::exists (file_name ())
327 && isWritable ()
328 && status () == QSettings::NoError))
329 {
330 QString msg
331 = QString (QT_TR_NOOP ("Error %1 creating the settings file\n%2\n"
332 "Make sure you have read and write permissions to\n%3\n\n"
333 "Octave GUI must be closed now."));
334
335 QMessageBox::critical (nullptr,
336 QString (QT_TR_NOOP ("Octave Critical Error")),
337 msg.arg (status ())
338 .arg (file_name ())
339 .arg (directory_name ()));
340
341 exit (1);
342 }
343 else
344 remove ("dummy"); // Remove test entry
345 }
178 } 346 }