File indexing completed on 2024-05-12 15:58:49

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KISBATCHNODEUPDATE_H
0008 #define KISBATCHNODEUPDATE_H
0009 
0010 #include "kritaimage_export.h"
0011 
0012 #include <QRect>
0013 #include <QSharedPointer>
0014 #include <boost/operators.hpp>
0015 #include <kis_types.h>
0016 
0017 /**
0018  * A simple class for storing the updates of multiple nodes
0019  * in a single place. These updates may later be "compressed",
0020  * which means that only the topmost root layers will get updates.
0021  * In such a case the update should be issued as refreshGraphAsync().
0022  */
0023 class KRITAIMAGE_EXPORT KisBatchNodeUpdate
0024         : public std::vector<std::pair<KisNodeSP, QRect>>,
0025         boost::orable<KisBatchNodeUpdate>
0026 {
0027 public:
0028     KisBatchNodeUpdate() = default;
0029     KisBatchNodeUpdate(const KisBatchNodeUpdate &rhs) = default;
0030     KisBatchNodeUpdate(KisBatchNodeUpdate &&rhs) = default;
0031     KisBatchNodeUpdate& operator=(const KisBatchNodeUpdate &rhs) = default;
0032 
0033     KisBatchNodeUpdate(const std::vector<std::pair<KisNodeSP, QRect>> &rhs);
0034 
0035     /**
0036      * Add an update designated for \p node with dirty rect \p rc
0037      *
0038      * Please node that adding multiple updates for the same node,
0039      * will result in multiple records added into the internal
0040      * vector. These duplicated records may be resolved by calling
0041      * compress() method.
0042      *
0043      * \see compress()
0044      */
0045     void addUpdate(KisNodeSP node, const QRect &rc);
0046 
0047     /**
0048      * Compress the stored updates:
0049      *
0050      * 1) All updates for the same node will be merged into one
0051      *
0052      * 2) If the list contains a child and its parent, the two
0053      *    updates will be merged into one. This new update
0054      *    record will be designated for the parent.
0055      *
0056      *    The idea is that the parent will be updated with
0057      *    refreshGraphAsync(), which would update the child
0058      *    anyway.
0059      */
0060     void compress();
0061 
0062     /**
0063      * \see compress()
0064      */
0065     KisBatchNodeUpdate compressed() const;
0066 
0067     /**
0068      * Merge two update batches. The updates for the same nodes
0069      * will be merged. This merge operation does **not** do
0070      * parent-child compression though. You need to call compress()
0071      * separately for that.
0072      */
0073     KisBatchNodeUpdate& operator|=(const KisBatchNodeUpdate &rhs);
0074 
0075 };
0076 
0077 KRITAIMAGE_EXPORT QDebug operator<<(QDebug dbg, const KisBatchNodeUpdate &update);
0078 
0079 using KisBatchNodeUpdateSP = QSharedPointer<KisBatchNodeUpdate>;
0080 using KisBatchNodeUpdateWSP = QWeakPointer<KisBatchNodeUpdate>;
0081 
0082 #endif // KISBATCHNODEUPDATE_H