File indexing completed on 2024-05-19 04:36:23

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2015 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef TIKZ_CORE_CONFIG_OBJECT_H
0021 #define TIKZ_CORE_CONFIG_OBJECT_H
0022 
0023 #include "tikz_export.h"
0024 
0025 #include <QObject>
0026 
0027 namespace tikz {
0028 namespace core {
0029 
0030 /**
0031  * Base class that signals change events.
0032  *
0033  * The ConfigObject contains a changed() signal.
0034  * @TODO FIXME
0035  */
0036 class TIKZKITCORE_EXPORT ConfigObject : public QObject
0037 {
0038     Q_OBJECT
0039 
0040     public:
0041         /**
0042          * Default constructor with optional parent.
0043          */
0044         explicit ConfigObject(QObject * parent = nullptr);
0045 
0046         /**
0047          * Virtual destructor.
0048          */
0049         virtual ~ConfigObject();
0050 
0051     //
0052     // config methods
0053     //
0054     public:
0055         /**
0056          * Start changing properties.
0057          * This call is ref-counted. For each beginConfig() you finally
0058          * have to call endConfig().
0059          */
0060         void beginConfig();
0061 
0062         /**
0063          * End of changing properties.
0064          * This will emit changed(), if the number of calls of endConfig()
0065          * matches the calls the one of beginConfig(), i.e. the ref-counter is zero.
0066          *
0067          * Using beginConfig() and endConfig() allows to change multiple
0068          * config values, while still only emitting the changed() signal only once.
0069          */
0070         void endConfig();
0071 
0072         /**
0073          * Returns whether beginConfig() was called without an endConfig() yet.
0074          */
0075         bool configActive() const;
0076 
0077     Q_SIGNALS:
0078         /**
0079          * This signal is emitted whenever the style changes.
0080          * This includes changes in the parent style that possibly influence
0081          * the appearance of this style.
0082          */
0083         void changed();
0084 
0085     public Q_SLOTS:
0086         /**
0087          * Emits changed() if the config reference counter is 0.
0088          * Otherwise, emitting changed() is delayed until the reference
0089          * counter is 0 after a call of endConfig().
0090          */
0091         void emitChangedIfNeeded();
0092 
0093     private:
0094         int m_refCounter = 0;
0095 };
0096 
0097 class TIKZKITCORE_EXPORT ConfigTransaction
0098 {
0099 public:
0100     // Disable some constructors.
0101     ConfigTransaction() = delete;
0102     ConfigTransaction(const ConfigTransaction & other) = delete;
0103 
0104     /**
0105      * Constructor that immediately calls configObject->beginConfig().
0106      */
0107     ConfigTransaction(ConfigObject * configObject);
0108 
0109     /**
0110      * Destructor that calls configObject->endConfig(), if required.
0111      */
0112     ~ConfigTransaction();
0113 
0114     /**
0115      * Call endConfig() on the ConfigObject passed in the constructor.
0116      * You may call this only once.
0117      */
0118     void endConfig();
0119 
0120 private:
0121     ConfigObject * m_configObject = nullptr;
0122 };
0123 
0124 }
0125 }
0126 
0127 #endif // TIKZ_CORE_CONFIG_OBJECT_H
0128 
0129 // kate: indent-width 4; replace-tabs on;