Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / libs / osgearth / osgQtQuick / DirtySupport.cpp
blobc5d488159c80e2e7c67031e9e148230cc8f2c563
1 /**
2 ******************************************************************************
4 * @file DirtySupport.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
6 * @addtogroup
7 * @{
8 * @addtogroup
9 * @{
10 * @brief
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "DirtySupport.hpp"
30 #include <osg/Node>
31 #include <osg/NodeVisitor>
33 #include <QDebug>
35 namespace osgQtQuick {
36 class Hidden;
38 struct DirtySupport::NodeUpdateCallback : public osg::NodeCallback {
39 public:
40 NodeUpdateCallback(DirtySupport::Hidden *h) : h(h)
43 void operator()(osg::Node *node, osg::NodeVisitor *nv);
45 private:
46 DirtySupport::Hidden *const h;
49 struct DirtySupport::Hidden {
50 private:
51 DirtySupport *const self;
53 osg::ref_ptr<osg::NodeCallback> nodeUpdateCallback;
55 int dirtyFlags;
57 public:
58 Hidden(DirtySupport *self) : self(self), dirtyFlags(0)
61 bool isDirty(int mask) const
63 return (dirtyFlags & mask) != 0;
66 int dirty() const
68 return dirtyFlags;
71 void setDirty(int mask)
73 // qDebug() << "DirtySupport::setDirty" << mask;
74 if (!dirtyFlags) {
75 osg::Node *node = self->nodeToUpdate();
76 if (node) {
77 if (!nodeUpdateCallback.valid()) {
78 // lazy creation
79 nodeUpdateCallback = new NodeUpdateCallback(this);
81 node->addUpdateCallback(nodeUpdateCallback.get());
82 } else {
83 // qWarning() << "DirtySupport::setDirty - node to update is null";
86 dirtyFlags |= mask;
89 void clearDirty()
91 osg::Node *node = self->nodeToUpdate();
93 if (node && nodeUpdateCallback.valid()) {
94 node->removeUpdateCallback(nodeUpdateCallback.get());
96 dirtyFlags = 0;
99 void update()
101 // qDebug() << "DirtySupport::update";
102 if (dirtyFlags) {
103 // qDebug() << "DirtySupport::update - updating...";
104 self->update();
106 clearDirty();
110 /* struct DirtySupport::NodeUpdateCallback */
112 void DirtySupport::NodeUpdateCallback::operator()(osg::Node *node, osg::NodeVisitor *nv)
114 // qDebug() << "DirtySupport::NodeUpdateCallback";
115 nv->traverse(*node);
116 h->update();
119 /* class DirtySupport */
121 DirtySupport::DirtySupport() : h(new Hidden(this))
124 DirtySupport::~DirtySupport()
126 delete h;
129 int DirtySupport::dirty() const
131 return h->dirty();
134 bool DirtySupport::isDirty(int mask) const
136 return h->isDirty(mask);
139 void DirtySupport::setDirty(int mask)
141 h->setDirty(mask);
144 void DirtySupport::clearDirty()
146 h->clearDirty();
148 } // namespace osgQtQuick