BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ApplicationSettings.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Application/ApplicationSettings.cpp
6 //! @brief Implements class ApplicationSettings
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
17 #include <QApplication>
18 #include <QFile>
19 #include <QSettings>
20 #include <QStandardPaths>
21 #include <QWidget>
22 
23 namespace {
24 
25 const QString S_CREATE_NEW_PROJECT_ON_STARTUP = "CreateNewProjectOnStartup";
26 const QString S_STYLE = "UiStyle";
27 const QString S_SIZE = "Size";
28 const QString S_POS = "Pos";
29 const QString S_DEFAULT_FUNCTIONALITIES = "DefaultFunctionalities";
30 const QString S_SINGLE_INSTRUMENT_MODE = "SingleInstrumentMode";
31 const QString S_SINGLE_SAMPLE_MODE = "SingleSampleMode";
32 const QString S_DEFAULT_UNIT_IS_ANGSTROM = "DefaultUnitIsAngstrom";
33 
34 } // namespace
35 
36 ApplicationSettings* appSettings; //!< global pointer to _the_ instance
37 
39  : m_currentStyle(ApplicationSettings::Style::native)
40 {
41  appSettings = this;
42 }
43 
45 {
46 #ifdef _WIN32
47  return true;
48 #else
49  return false;
50 #endif
51 }
52 
54 {
55  return QSettings().value(S_CREATE_NEW_PROJECT_ON_STARTUP, true).toBool();
56 }
57 
59 {
60  QSettings().setValue(S_CREATE_NEW_PROJECT_ON_STARTUP, b);
61 }
62 
64 {
65  return static_cast<Style>(QSettings().value(S_STYLE, static_cast<int>(Style::light)).toInt());
66 }
67 
69 {
70  QSettings().setValue(S_STYLE, static_cast<int>(style));
71 }
72 
73 QVariant ApplicationSettings::defaultFunctionalities(const QVariant& absenceValue) const
74 {
75  return QSettings().value(S_DEFAULT_FUNCTIONALITIES, absenceValue);
76 }
77 
78 void ApplicationSettings::setDefaultFunctionalities(const QVariant& functionalities)
79 {
80  QSettings().setValue(S_DEFAULT_FUNCTIONALITIES, functionalities);
81 }
82 
84 {
85  return QSettings().value(S_SINGLE_INSTRUMENT_MODE, false).toBool();
86 }
87 
89 {
90  QSettings().setValue(S_SINGLE_INSTRUMENT_MODE, b);
91 }
92 
94 {
95  return QSettings().value(S_SINGLE_SAMPLE_MODE, false).toBool();
96 }
97 
99 {
100  QSettings().setValue(S_SINGLE_SAMPLE_MODE, b);
101 }
102 
104 {
105  return QSettings().value(S_DEFAULT_UNIT_IS_ANGSTROM, false).toBool();
106 }
107 
109 {
110  QSettings().setValue(S_DEFAULT_UNIT_IS_ANGSTROM, b);
111 }
112 
114 {
115  ASSERT(!w->objectName().isEmpty());
116  QSettings settings;
117  settings.setValue(S_SIZE + "/" + w->objectName(), w->size());
118  settings.setValue(S_POS + "/" + w->objectName(), w->pos());
119 }
120 
122 {
123  ASSERT(!w->objectName().isEmpty());
124 
125  QSettings settings;
126  const QSize size = settings.value(S_SIZE + "/" + w->objectName(), QSize()).toSize();
127  if (size.isValid())
128  w->resize(size);
129  if (settings.contains(S_POS + "/" + w->objectName()))
130  w->move(settings.value(S_POS + "/" + w->objectName()).toPoint());
131 }
132 
134 {
135  QString stylesheet;
136 
137  switch (style) {
139  QFile base(":/styles/Base.stylesheet");
140  base.open(QFile::ReadOnly);
141  QFile light(":/styles/Light.stylesheet");
142  light.open(QFile::ReadOnly);
143  stylesheet = base.readAll() + light.readAll();
144  break;
145  }
147  QFile base(":/styles/Base.stylesheet");
148  base.open(QFile::ReadOnly);
149  QFile dark(":/styles/Dark.stylesheet");
150  dark.open(QFile::ReadOnly);
151  stylesheet = base.readAll() + dark.readAll();
152  break;
153  }
154  default: {
155  QFile native(":/styles/Native.stylesheet");
156  native.open(QFile::ReadOnly);
157  stylesheet = native.readAll();
158  break;
159  }
160  }
161 
162  qApp->setStyleSheet(stylesheet);
163  m_currentStyle = style;
164 
165  // -- init palette for later usage; could be improved by parsing the stylesheet
166  switch (m_currentStyle) {
168  m_styleSheetPalette.headlineText = QColor(48, 113, 145);
169  m_styleSheetPalette.headlineBackground = QColor(222, 232, 237);
170  break;
171  }
173  m_styleSheetPalette.headlineText = QColor(213, 220, 223);
174  m_styleSheetPalette.headlineBackground = QColor(29, 39, 44);
175  break;
176  }
177  default: {
178  m_styleSheetPalette.headlineText = Qt::black;
179  m_styleSheetPalette.headlineBackground = QColor(250, 250, 250);
180  break;
181  }
182  }
183 }
184 
186 {
187  return m_currentStyle;
188 }
189 
191 {
192  return m_styleSheetPalette;
193 }
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Application wide settings.
void setStyleToUse(Style style)
void setDefaultIsSingleInstrumentMode(bool b)
void setDefaultUnitIsAngstrom(bool b) const
void loadWindowSizeAndPos(QWidget *w)
bool defaultIsSingleSampleMode() const
const Palette & styleSheetPalette() const
bool defaultIsSingleInstrumentMode() const
void saveWindowSizeAndPos(const QWidget *w)
bool useNativeFileDialog() const
ApplicationSettings::Style currentStyle()
bool createNewProjectOnStartup() const
void setDefaultFunctionalities(const QVariant &functionalities)
void setDefaultIsSingleSampleMode(bool b)
QVariant defaultFunctionalities(const QVariant &absenceValue) const
bool defaultUnitIsAngstrom() const
void loadStyle(ApplicationSettings::Style style)
ApplicationSettings::Style m_currentStyle
void setCreateNewProjectOnStartup(bool b)