Perceptual Color

settings.cpp
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4// Own headers
5// First the interface, which forces the header to be self-contained.
6#include "settings.h"
7
8#include <qcoreapplication.h>
9#include <qfilesystemwatcher.h>
10#include <qglobal.h>
11
12#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
13#include <qcontainerfwd.h>
14#include <qlist.h>
15#else
16#include <qdatastream.h>
17#include <qstringlist.h>
18#endif
19
20namespace PerceptualColor
21{
22
23/** @brief Constructor.
24 *
25 * @param scope Passed to the underlying <tt>QSettings</tt> object’s
26 * constructor.
27 * @param organization Passed to the underlying <tt>QSettings</tt> object’s
28 * constructor.
29 * @param application Passed to the underlying <tt>QSettings</tt> object’s
30 * constructor. */
31Settings::Settings(QSettings::Scope scope, const QString &organization, const QString &application)
32 // There are important reasons to use <tt>QSettings::IniFormat</tt>.
33 //
34 // - It makes <tt>QSettings</tt> behave identical on all platforms.
35 // Though <tt>QSettings</tt> is an abstraction, it has still a lot
36 // of platform-dependant behaviour, like the fact the numbers are
37 // saved as numbers but read back as QString when using
38 // <tt>QSettings::IniFormat</tt> or when using the native format
39 // and the native platform uses Ini (like Linux); other platforms
40 // preserve the type information. By using <tt>QSettings::IniFormat</tt>
41 // the behaviour becomes at least predictable and is identical also
42 // cross-platform.
43 //
44 // - <tt>QSettings::IniFormat</tt> is a file-based approach (while the
45 // native approach for example on Windows is the Windows Registry
46 // instead of a file). Using a file is necessary to be able to monitor
47 // changes that other processes might make.
48 : m_qSettings(QSettings::IniFormat, scope, organization, application)
49{
50 if (QCoreApplication::instance() == nullptr) {
51 // A QCoreApplication object is required because otherwise
52 // the QFileSystemWatcher will not do anything and print the
53 // highly confusing warning “QSocketNotifier: Can only be used
54 // with threads started with QThread”. It's better to give clear
55 // feedback:
56 throw 0;
57 }
58
59 m_watcher.addPath(m_qSettings.fileName());
60
61 connect(&m_watcher, //
63 this, //
64 &PerceptualColor::Settings::updateFromFile //
65 );
66
67 // Initialize
68 updateFromFile();
69}
70
71/** @brief Destructor. */
72Settings::~Settings()
73{
74}
75
76/** @brief Updates all @ref Setting values to the corresponding values from
77 * the underlying file of @ref m_qSettings.
78 *
79 * This is done by emitting the @ref updatedAfterFileChange() signal, to which
80 * the @ref Setting objects are supposed to connect. */
81void Settings::updateFromFile()
82{
83 // From Qt documentation:
84 // “Note: As a safety measure, many applications save
85 // an open file by writing a new file and then deleting
86 // the old one. In your slot function, you can check
87 // watcher.files().contains(path). If it returns false,
88 // check whether the file still exists and then call
89 // addPath() to continue watching it.”
90 if (!m_watcher.files().contains(m_qSettings.fileName())) {
91 m_watcher.addPath(m_qSettings.fileName());
92 }
93
94 m_qSettings.sync();
95
96 Q_EMIT updatedAfterFileChange();
97}
98
99} // namespace PerceptualColor
The namespace of this library.
QCoreApplication * instance()
void fileChanged(const QString &path)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:51:26 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.