File indexing completed on 2024-05-12 04:44:34

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 // Own headers
0005 // First the interface, which forces the header to be self-contained.
0006 #include "perceptualsettings.h"
0007 
0008 #include <qcoreapplication.h>
0009 #include <qsettings.h>
0010 #include <qstringliteral.h>
0011 
0012 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0013 #else
0014 #include <qdatastream.h>
0015 #endif
0016 
0017 namespace PerceptualColor
0018 {
0019 
0020 /** @brief Private constructor to prevent instantiation. */
0021 PerceptualSettings::PerceptualSettings()
0022     : Settings(QSettings::UserScope, QStringLiteral("kde.org"), QStringLiteral("libperceptualcolor"))
0023     // For maximum portability:
0024     // - No upper case should ever be used.
0025     //   (Some systems, like the INI that we are using, are case-insensitive.
0026     //   And even if we always use INI, having both capital and small letters
0027     //   is error-prone because typos are not checked by the compiler.)
0028     // - Only the letters a-z should be used.
0029     //   (Also, some characters like the backslash are not allowed on some
0030     //   platforms.)
0031     // - “group/key”: Each key has exactly one group. Don't use subgroups.
0032     //   Use the class name as group name.
0033     //   (This makes the settings file well readable for humans. Missing
0034     //   groups are confusing because the system generates a “General”
0035     //   group which is not easy to understand. And using class identifiers
0036     //   helps to understand the structure of the settings file.)
0037     // - In C++, use “const” variables to define key strings, instead of
0038     //   manually typing the key strings.
0039     //   (This avoids typing errors.)
0040     , customColors(QStringLiteral("colordialog/customcolors"), this)
0041     , history(QStringLiteral("colordialog/history"), this)
0042     , tab(QStringLiteral("colordialog/tab"), this)
0043     , tabExpanded(QStringLiteral("colordialog/tabexpanded"), this)
0044 {
0045 }
0046 
0047 /** @brief Destructor. */
0048 PerceptualSettings::~PerceptualSettings()
0049 {
0050 }
0051 
0052 /** @brief Get a reference to the singleton instance.
0053  *
0054  * @pre There exists a QCoreApplication object. (Otherwise, this
0055  * function will throw an exception.)
0056  *
0057  * @returns A reference to the instance.
0058  *
0059  * To use it, assign the return value to a reference (not a normal variable):
0060  *
0061  * @snippet testperceptualsettings.cpp PerceptualSettings Instance */
0062 PerceptualSettings &PerceptualSettings::instance()
0063 {
0064     if (QCoreApplication::instance() == nullptr) {
0065         // A QCoreApplication object is required because otherwise
0066         // the QFileSystemWatcher will not do anything and print the
0067         // highly confusing warning “QSocketNotifier: Can only be used
0068         // with threads started with QThread”. It's better to give clear
0069         // feedback:
0070         throw 0;
0071     }
0072     static PerceptualSettings myInstance;
0073     return myInstance;
0074 }
0075 
0076 } // namespace PerceptualColor