From 99b19dfcc6fe5a8ddfc97c67001efb13db99d902 Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 4 Jan 2016 00:19:34 -0600 Subject: [PATCH] First step in refactoring: brought both Paint and Mapping under the common umbrella class Element and implemented basic QProperties. --- Element.cpp | 40 ++++++++++++++++++++++++++ Element.h | 66 +++++++++++++++++++++++++++++++++++++++++++ Mapping.cpp | 20 ++++--------- Mapping.h | 33 +++++++++++----------- MappingGui.cpp | 6 ++-- Paint.cpp | 16 ++--------- Paint.h | 62 +++++++++++++++++++++++++--------------- ShapeGraphicsItem.cpp | 4 +-- mapmap.pro | 2 ++ 9 files changed, 177 insertions(+), 72 deletions(-) create mode 100644 Element.cpp create mode 100644 Element.h diff --git a/Element.cpp b/Element.cpp new file mode 100644 index 0000000..0bcbb10 --- /dev/null +++ b/Element.cpp @@ -0,0 +1,40 @@ +/* + * Element.cpp + * + * (c) 2016 Sofian Audry -- info(@)sofianaudry(.)com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Element.h" + +Element::Element(uid id, UidAllocator* allocator) : _name(""), _isLocked(false), _opacity(1.0f), _allocator(allocator) +{ + if (id == NULL_UID) + id = allocator->allocate(); + else + { + Q_ASSERT(!allocator->exists(id)); + allocator->reserve(id); + } + // Assign id. + _id = id; + // Reset name. + unsetName(); +} + +Element::~Element() { + _allocator->free(_id); +} + diff --git a/Element.h b/Element.h new file mode 100644 index 0000000..7594830 --- /dev/null +++ b/Element.h @@ -0,0 +1,66 @@ +/* + * Element.h + * + * (c) 2016 Sofian Audry -- info(@)sofianaudry(.)com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef ELEMENT_H_ +#define ELEMENT_H_ + +#include +#include + +#include "UidAllocator.h" + +class Element : public QObject +{ + Q_OBJECT + + Q_PROPERTY(uid id READ getId) + Q_PROPERTY(QString name READ getName WRITE setName RESET unsetName) + Q_PROPERTY(bool locked READ isLocked WRITE setLocked) + Q_PROPERTY(float opacity READ getOpacity WRITE setOpacity) + +public: + Element(uid id, UidAllocator* allocator); + virtual ~Element(); + + uid getId() const { return _id; } + + void setName(const QString& name) { _name = name; } + QString getName() const { return _name; } + virtual void unsetName() { _name = _id; } + + float getOpacity() const { return _opacity; } + void setOpacity(float opacity) { + _opacity = qBound(opacity, 0.0f, 1.0f); + } + + bool isLocked() const { return _isLocked; } + void setLocked(bool locked) { _isLocked = locked; } + void toggleLocked() { _isLocked = !_isLocked; } + + virtual void build() {} + +private: + uid _id; + QString _name; + bool _isLocked; + float _opacity; + UidAllocator* _allocator; +}; + +#endif /* MOBJECT_H_ */ diff --git a/Mapping.cpp b/Mapping.cpp index 75b8f94..1701421 100644 --- a/Mapping.cpp +++ b/Mapping.cpp @@ -23,25 +23,15 @@ UidAllocator Mapping::allocator; Mapping::Mapping(Paint::ptr paint, MShape::ptr shape, uid id) - : _paint(paint), _shape(shape), - _isLocked(false), _isSolo(false), _isVisible(true), _opacity(1.0f) + : Element(id, &allocator), + _paint(paint), _shape(shape), + _isSolo(false), _isVisible(true) { - if (id == NULL_UID) - id = allocator.allocate(); - else - { - Q_ASSERT(!allocator.exists(id)); - allocator.reserve(id); - } - - // Assign id. - _id = id; - // Default. - _depth = _id; + _depth = getId(); } Mapping::~Mapping() { - allocator.free(_id); + allocator.free(getId()); } diff --git a/Mapping.h b/Mapping.h index 7a6d568..bed8561 100644 --- a/Mapping.h +++ b/Mapping.h @@ -25,6 +25,9 @@ #include "Shape.h" #include "Paint.h" + +#include "Element.h" + #include "UidAllocator.h" /** @@ -39,8 +42,20 @@ * can thus change their opacity level, toggle their visibility, set * them in "solo" mode and lock them. */ -class Mapping +class Mapping : public Element { + Q_OBJECT + + Q_PROPERTY(bool solo READ isSolo WRITE setSolo) + Q_PROPERTY(bool visible READ isVisible WRITE setVisible) + Q_PROPERTY(int depth READ getDepth WRITE setDepth) + + Q_PROPERTY(MShape::ptr shape READ getShape) + Q_PROPERTY(MShape::ptr inputShape READ getInputShape) + + Q_PROPERTY(bool hasInputShape READ hasInputShape STORED false) + Q_PROPERTY(Paint::ptr paint READ getPaint WRITE setPaint) + protected: /// The input Paint instance. Paint::ptr _paint; @@ -51,12 +66,8 @@ protected: private: static UidAllocator allocator; - uid _id; - - bool _isLocked; bool _isSolo; bool _isVisible; - float _opacity; int _depth; // depth of the layer protected: @@ -94,28 +105,18 @@ public: /// Returns the input (source) shape (if this mapping has one) or a null pointer if not. virtual MShape::ptr getInputShape() const { return MShape::ptr(); } - uid getId() const { return _id; } - - void setLocked(bool locked) { _isLocked = locked; } void setSolo(bool solo) { _isSolo = solo; } void setVisible(bool visible) { _isVisible = visible; } - void setRawOpacity(float opacity) { - Q_ASSERT(0.0f <= opacity && opacity <= 1.0f); - _opacity = opacity; - } void setDepth(int depth) { _depth = depth; } - void toggleLocked() { _isLocked = !_isLocked; } void toggleSolo() { _isSolo = !_isSolo; } void toggleVisible() { _isVisible = !_isVisible; } - bool isLocked() const { return _isLocked; } bool isSolo() const { return _isSolo; } bool isVisible() const { return _isVisible; } - float getRawOpacity() const { return _opacity; } int getDepth() const { return _depth; } - float getOpacity() const { return _opacity * _paint->getOpacity(); } + float getComputedOpacity() const { return getOpacity() * _paint->getOpacity(); } void setPaint(Paint::ptr p) { _paint = p; } }; diff --git a/MappingGui.cpp b/MappingGui.cpp index cca6c71..1e5c9d6 100644 --- a/MappingGui.cpp +++ b/MappingGui.cpp @@ -45,7 +45,7 @@ MappingGui::MappingGui(Mapping::ptr mapping) _opacityItem->setAttribute("minimum", 0.0); _opacityItem->setAttribute("maximum", 100.0); _opacityItem->setAttribute("decimals", 1); - _opacityItem->setValue(_mapping->getRawOpacity()*100.0); + _opacityItem->setValue(_mapping->getOpacity()*100.0); _topItem->addSubProperty(_opacityItem); // Output shape. @@ -69,9 +69,9 @@ void MappingGui::setValue(QtProperty* property, const QVariant& value) if (property == _opacityItem) { double opacity = qBound(value.toDouble() / 100.0, 0.0, 1.0); - if (opacity != _mapping->getRawOpacity()) + if (opacity != _mapping->getOpacity()) { - _mapping->setRawOpacity(opacity); + _mapping->setOpacity(opacity); emit valueChanged(); } } diff --git a/Paint.cpp b/Paint.cpp index e4e2d59..695cadb 100644 --- a/Paint.cpp +++ b/Paint.cpp @@ -24,30 +24,20 @@ UidAllocator Paint::allocator; -Paint::Paint(uid id) - : _opacity(1.0f) +Paint::Paint(uid id) : Element(id, &allocator) { - if (id == NULL_UID) - id = allocator.allocate(); - else - { - Q_ASSERT(!allocator.exists(id)); - allocator.reserve(id); - } - // Assign id. - _id = id; } Paint::~Paint() { - allocator.free(_id); + allocator.free(getId()); } bool Image::setUri(const QString &uri) { this->uri = uri; build(); - return true; + return !image.isNull(); } /* Implementation of the Video class */ diff --git a/Paint.h b/Paint.h index 938673f..f23dcf5 100644 --- a/Paint.h +++ b/Paint.h @@ -24,6 +24,7 @@ #include #include + #include #include #include @@ -34,7 +35,7 @@ #include #endif -#include "UidAllocator.h" +#include "Element.h" /** * A Paint is a style that can be applied when drawing potentially any shape. @@ -43,8 +44,10 @@ * There must be a MappingGui that implements this paint for every shape * so that this shape might be drawn with it. */ -class Paint +class Paint : public Element { + Q_OBJECT + private: static UidAllocator allocator; @@ -60,8 +63,6 @@ public: static const UidAllocator& getUidAllocator() { return allocator; } - virtual void build() {} - /// This method should be called at each call of draw(). virtual void update() {} @@ -80,25 +81,15 @@ public: /// Unlocks mutex (default = no effect). virtual void unlockMutex() {} - void setName(const QString& name) { _name = name; } - QString getName() const { return _name; } - uid getId() const { return _id; } - - float getOpacity() const { return _opacity; } - void setOpacity(float opacity) { - Q_ASSERT(0.0f <= opacity && opacity <= 1.0f); - _opacity = opacity; - } - virtual QString getType() const = 0; - -private: - QString _name; - float _opacity; }; class Color : public Paint { + Q_OBJECT + + Q_PROPERTY(QColor color READ getColor WRITE setColor) + protected: QColor color; @@ -107,7 +98,7 @@ public: Color(const QColor& color_, uid id=NULL_UID) : Paint(id), color(color_) {} QColor getColor() const { return color; } - virtual void setColor(const QColor& color_) { color = color_; } + void setColor(const QColor& color_) { color = color_; } virtual QString getType() const { return "color"; } }; @@ -119,6 +110,11 @@ public: */ class Texture : public Paint { + Q_OBJECT + + Q_PROPERTY(GLfloat x READ getX WRITE setX) + Q_PROPERTY(GLfloat y READ getY WRITE setY) + protected: GLuint textureId; GLfloat x; @@ -151,13 +147,22 @@ public: /// Returns true iff bits have changed since last call to getBits(). virtual bool bitsHaveChanged() const = 0; - virtual void setPosition(GLfloat xPos, GLfloat yPos) { - x = xPos; - y = yPos; - } virtual GLfloat getX() const { return x; } virtual GLfloat getY() const { return y; } + virtual void setX(GLfloat xPos) { + x = xPos; + } + + virtual void setY(GLfloat yPos) { + y = yPos; + } + + virtual void setPosition(GLfloat xPos, GLfloat yPos) { + setX(xPos); + setY(yPos); + } + virtual QRectF getRect() const { return QRectF(getX(), getY(), getWidth(), getHeight()); } }; @@ -166,6 +171,10 @@ public: */ class Image : public Texture { + Q_OBJECT + + Q_PROPERTY(QString uri READ getUri WRITE setUri) + protected: QString uri; QImage image; @@ -207,6 +216,13 @@ class MediaImpl; // forward declaration */ class Media : public Texture { + Q_OBJECT + + Q_PROPERTY(QString uri READ getUri WRITE setUri) + + Q_PROPERTY(double volume READ getVolume WRITE setVolume) + Q_PROPERTY(double rate READ getRate WRITE setRate) + protected: QString uri; public: diff --git a/ShapeGraphicsItem.cpp b/ShapeGraphicsItem.cpp index c935be7..4a74f4b 100644 --- a/ShapeGraphicsItem.cpp +++ b/ShapeGraphicsItem.cpp @@ -114,7 +114,7 @@ void ColorGraphicsItem::_prePaint(QPainter *painter, // Set brush. QColor col = color->getColor(); - col.setAlphaF(getMapping()->getOpacity()); + col.setAlphaF(getMapping()->getComputedOpacity()); painter->setBrush(col); } @@ -257,7 +257,7 @@ void TextureGraphicsItem::_prePaint(QPainter* painter, // Set texture color (apply opacity). glColor4f(1.0f, 1.0f, 1.0f, - isOutput() ? getMapping()->getOpacity() : getMapping()->getPaint()->getOpacity()); + isOutput() ? getMapping()->getComputedOpacity() : getMapping()->getPaint()->getOpacity()); } void TextureGraphicsItem::_postPaint(QPainter* painter, diff --git a/mapmap.pro b/mapmap.pro index 3264353..244e8fe 100644 --- a/mapmap.pro +++ b/mapmap.pro @@ -9,6 +9,7 @@ DEFINES += UNICODE QT_THREAD_SUPPORT QT_CORE_LIB QT_GUI_LIB HEADERS = \ Commands.h \ DestinationGLCanvas.h \ + Element.h \ MM.h \ MainApplication.h \ MainWindow.h \ @@ -37,6 +38,7 @@ HEADERS = \ SOURCES = \ Commands.cpp \ DestinationGLCanvas.cpp \ + Element.cpp \ MM.cpp \ MainApplication.cpp \ MainWindow.cpp \