From fdb9b8b2d07ef0695275ad353f40ff068779a82f Mon Sep 17 00:00:00 2001 From: eshe Date: Mon, 19 Jul 2010 15:58:44 +0100 Subject: [PATCH] Added some effects to graphics elements and changed default theme a bit. --- jspeed.pro | 12 ++++- src/blureffect.cpp | 68 +++++++++++++++++++++++++ src/blureffect.h | 38 ++++++++++++++ src/dropshadoweffect.cpp | 81 ++++++++++++++++++++++++++++++ src/dropshadoweffect.h | 39 +++++++++++++++ src/effect.cpp | 85 ++++++++++++++++++++++++++++++++ src/effect.h | 44 +++++++++++++++++ src/graphicselement.cpp | 45 ++++++++++++++++- src/graphicselement.h | 6 +++ src/opacityeffect.cpp | 68 +++++++++++++++++++++++++ src/opacityeffect.h | 38 ++++++++++++++ src/resources/themes/default/theme.xml | 12 +++++ src/textelement.cpp | 1 + src/themescreen.cpp | 51 ++++++++++++++++++- src/themescreen.h | 1 + 15 files changed, 583 insertions(+), 6 deletions(-) create mode 100644 src/blureffect.cpp create mode 100644 src/blureffect.h create mode 100644 src/dropshadoweffect.cpp create mode 100644 src/dropshadoweffect.h create mode 100644 src/effect.cpp create mode 100644 src/effect.h create mode 100644 src/opacityeffect.cpp create mode 100644 src/opacityeffect.h diff --git a/jspeed.pro b/jspeed.pro index d5f39ee..2004508 100644 --- a/jspeed.pro +++ b/jspeed.pro @@ -27,7 +27,11 @@ SOURCES += src/main.cpp \ src/mainmenu.cpp \ src/buttonselector.cpp \ src/themeselector.cpp \ - src/unitselector.cpp + src/unitselector.cpp \ + src/effect.cpp \ + src/blureffect.cpp \ + src/opacityeffect.cpp \ + src/dropshadoweffect.cpp HEADERS += src/mainwindow.h \ src/mainwindowstack.h \ src/location.h \ @@ -53,7 +57,11 @@ HEADERS += src/mainwindow.h \ src/mainmenu.h \ src/buttonselector.h \ src/themeselector.h \ - src/unitselector.h + src/unitselector.h \ + src/effect.h \ + src/blureffect.h \ + src/opacityeffect.h \ + src/dropshadoweffect.h RESOURCES = src/resources.qrc CONFIG += link_pkgconfig PKGCONFIG += liblocation libzip diff --git a/src/blureffect.cpp b/src/blureffect.cpp new file mode 100644 index 0000000..5635964 --- /dev/null +++ b/src/blureffect.cpp @@ -0,0 +1,68 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#include +#include +#include +#include +#include "blureffect.h" + +namespace +{ + Effect::AttributeDetails ATTRIBUTES[BlurEffect::ATTRIBUTE_COUNT] = + { + {"radius", true} + }; +} + +BlurEffect::BlurEffect(): Effect() +{ + effect_ = new QGraphicsBlurEffect; +} + +bool BlurEffect::setAttribute(QString const& name, QString const& value) +{ + qreal realVal = 0; + int attrId = -1; + + if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, realVal)) != -1) + { + Attribute attr = static_cast(attrId); + + switch(attr) + { + case RADIUS: + effect_->setBlurRadius(realVal); + break; + default: + qDebug() << "Unknown attribute: " << attr; + return false; + } + + return true; + } + else + { + return false; + } +} + +void BlurEffect::apply(QGraphicsItem* item) +{ + item->setGraphicsEffect(effect_); +} diff --git a/src/blureffect.h b/src/blureffect.h new file mode 100644 index 0000000..28ea685 --- /dev/null +++ b/src/blureffect.h @@ -0,0 +1,38 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#ifndef BLUREFFECT_H +#define BLUREFFECT_H + +#include "effect.h" + +class QGraphicsBlurEffect; + +class BlurEffect : public Effect +{ +public: + enum Attribute {RADIUS, ATTRIBUTE_COUNT}; + BlurEffect(); + virtual bool setAttribute(QString const& name, QString const& value); + virtual void apply(QGraphicsItem* item); + +private: + QGraphicsBlurEffect* effect_; +}; + +#endif diff --git a/src/dropshadoweffect.cpp b/src/dropshadoweffect.cpp new file mode 100644 index 0000000..0eac608 --- /dev/null +++ b/src/dropshadoweffect.cpp @@ -0,0 +1,81 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include "dropshadoweffect.h" + +namespace +{ + Effect::AttributeDetails ATTRIBUTES[DropshadowEffect::ATTRIBUTE_COUNT] = + { + {"radius", true}, + {"color", false}, + {"xoffset", true}, + {"yoffset", true} + }; +} + +DropshadowEffect::DropshadowEffect(): Effect() +{ + effect_ = new QGraphicsDropShadowEffect; +} + +bool DropshadowEffect::setAttribute(QString const& name, QString const& value) +{ + qreal realVal = 0; + int attrId = -1; + + if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, realVal)) != -1) + { + Attribute attr = static_cast(attrId); + + switch(attr) + { + case RADIUS: + effect_->setBlurRadius(realVal); + break; + case COLOR: + effect_->setColor(QColor(value)); + break; + case XOFFSET: + effect_->setXOffset(realVal); + break; + case YOFFSET: + effect_->setYOffset(realVal); + break; + default: + qDebug() << "Unknown attribute: " << attr; + return false; + } + + return true; + } + else + { + return false; + } +} + +void DropshadowEffect::apply(QGraphicsItem* item) +{ + item->setGraphicsEffect(effect_); +} diff --git a/src/dropshadoweffect.h b/src/dropshadoweffect.h new file mode 100644 index 0000000..864d585 --- /dev/null +++ b/src/dropshadoweffect.h @@ -0,0 +1,39 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#ifndef DROPSHADOWEFFECT_H +#define DROPSHADOWEFFECT_H + +#include "effect.h" + +class QGraphicsDropShadowEffect; +class QGraphicsItem; + +class DropshadowEffect : public Effect +{ +public: + enum Attribute {RADIUS, COLOR, XOFFSET, YOFFSET, ATTRIBUTE_COUNT}; + DropshadowEffect(); + virtual bool setAttribute(QString const& name, QString const& value); + virtual void apply(QGraphicsItem* item); + +private: + QGraphicsDropShadowEffect* effect_; +}; + +#endif diff --git a/src/effect.cpp b/src/effect.cpp new file mode 100644 index 0000000..df7cc53 --- /dev/null +++ b/src/effect.cpp @@ -0,0 +1,85 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#include +#include +#include "effect.h" +#include "dropshadoweffect.h" +#include "blureffect.h" +#include "opacityeffect.h" + + +Effect::Effect() +{ +} + +Effect* Effect::getEffect(QString const& name) +{ + if(name == "dropshadow") + { + return new DropshadowEffect; + } + else if(name == "blur") + { + return new BlurEffect; + } + else if(name == "opacity") + { + return new OpacityEffect; + } + + return 0; +} + +int Effect::getAttribute(QString const& name, + QString const& value, + const Effect::AttributeDetails details[], + int count, + qreal& realValue) +{ + QString lower = name.toLower(); + + for(int i = 0; i < count; i++) + { + if(details[i].name == lower) + { + if(!details[i].isQreal) + { + return i; + } + else + { + bool ok = true; + double tmp = value.toDouble(&ok); + + if(ok) + { + realValue = static_cast(tmp); + return i; + } + else + { + return -1; + } + } + } + } + + return -1; +} + diff --git a/src/effect.h b/src/effect.h new file mode 100644 index 0000000..929680f --- /dev/null +++ b/src/effect.h @@ -0,0 +1,44 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#ifndef EFFECT_H +#define EFFECT_H + +class QString; +class QGraphicsItem; + +class Effect +{ +public: + struct AttributeDetails + { + QString name; + bool isQreal; + }; + + Effect(); + static Effect* getEffect(QString const& name); + virtual bool setAttribute(QString const& name, QString const& value) = 0; + virtual void apply(QGraphicsItem* item) = 0; + +protected: + int getAttribute(QString const& name, QString const& value, const AttributeDetails details[], int count, qreal& realValue); + +}; + +#endif diff --git a/src/graphicselement.cpp b/src/graphicselement.cpp index 3befe29..452af54 100644 --- a/src/graphicselement.cpp +++ b/src/graphicselement.cpp @@ -22,6 +22,9 @@ #include #include #include +#include +#include +#include #include "graphicselement.h" #include "reader.h" #include "textelement.h" @@ -29,8 +32,9 @@ #include "rectangle.h" #include "pointer.h" #include "settings.h" +#include "effect.h" -GraphicsElement::GraphicsElement(Reader* reader): reader_(reader), error_("") +GraphicsElement::GraphicsElement(Reader* reader): reader_(reader), error_(""), effect_(0) { } @@ -73,7 +77,6 @@ void GraphicsElement::setError(QString const& error) bool GraphicsElement::readFile(QString const& filename, QByteArray& data) { - if(!reader_->readFile(filename, data)) { setError(reader_->errorString()); @@ -167,3 +170,41 @@ int GraphicsElement::getAttribute(QString const& name, setError("Unknown attribute: " + name); return -1; } + +bool GraphicsElement::setEffect(QString const& name) +{ + /* QGraphicsDropShadowEffect* eff = new QGraphicsDropShadowEffect; + eff->setOffset(1); + eff->setBlurRadius(3); + + getElement()->setGraphicsEffect(eff); + + return true;*/ + + effect_ = Effect::getEffect(name); + + if(!effect_) + { + return false; + } + + return true; +} + +bool GraphicsElement::setEffectAttribute(QString const& name, QString const& value) +{ + //return true; + if(!effect_) + { + qDebug() << "Effect not set"; + return false; + } + + return effect_->setAttribute(name, value); +} + +void GraphicsElement::applyEffect() +{ + //return; + effect_->apply(getElement()); +} diff --git a/src/graphicselement.h b/src/graphicselement.h index ee9c4c2..94d0143 100644 --- a/src/graphicselement.h +++ b/src/graphicselement.h @@ -27,7 +27,9 @@ class QByteArray; class QFont; class QGraphicsItem; class GraphicsScene; +class QGraphicsEffect; class Reader; +class Effect; class GraphicsElement { @@ -46,6 +48,9 @@ public: virtual void update() = 0; virtual QGraphicsItem* getElement() const = 0; QString const& getError() const; + bool setEffect(QString const& effect); + bool setEffectAttribute(QString const& name, QString const& value); + void applyEffect(); protected: int getAttribute(QString const& name, QString const& value, const AttributeDetails details[], int count, int& intValue); @@ -56,6 +61,7 @@ protected: private: Reader* reader_; QString error_; + Effect* effect_; QMap loadedFonts_; }; diff --git a/src/opacityeffect.cpp b/src/opacityeffect.cpp new file mode 100644 index 0000000..2b1ea09 --- /dev/null +++ b/src/opacityeffect.cpp @@ -0,0 +1,68 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#include +#include +#include +#include +#include "opacityeffect.h" + +namespace +{ + Effect::AttributeDetails ATTRIBUTES[OpacityEffect::ATTRIBUTE_COUNT] = + { + {"opacity", true} + }; +} + +OpacityEffect::OpacityEffect(): Effect() +{ + effect_ = new QGraphicsOpacityEffect; +} + +bool OpacityEffect::setAttribute(QString const& name, QString const& value) +{ + qreal realVal = 0; + int attrId = -1; + + if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, realVal)) != -1) + { + Attribute attr = static_cast(attrId); + + switch(attr) + { + case OPACITY: + effect_->setOpacity(realVal); + break; + default: + qDebug() << "Unknown attribute: " << attr; + return false; + } + + return true; + } + else + { + return false; + } +} + +void OpacityEffect::apply(QGraphicsItem* item) +{ + item->setGraphicsEffect(effect_); +} diff --git a/src/opacityeffect.h b/src/opacityeffect.h new file mode 100644 index 0000000..e85a5ae --- /dev/null +++ b/src/opacityeffect.h @@ -0,0 +1,38 @@ +/* + * This file is part of jSpeed. + * + * jSpeed 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. + * + * jSpeed 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 jSpeed. If not, see . + * + */ + +#ifndef OPACITYEFFECT_H +#define OPACITYEFFECT_H + +#include "effect.h" + +class QGraphicsOpacityEffect; + +class OpacityEffect : public Effect +{ +public: + enum Attribute {OPACITY, ATTRIBUTE_COUNT}; + OpacityEffect(); + virtual bool setAttribute(QString const& name, QString const& value); + virtual void apply(QGraphicsItem* item); + +private: + QGraphicsOpacityEffect* effect_; +}; + +#endif diff --git a/src/resources/themes/default/theme.xml b/src/resources/themes/default/theme.xml index 0064f70..6068249 100644 --- a/src/resources/themes/default/theme.xml +++ b/src/resources/themes/default/theme.xml @@ -32,6 +32,12 @@ right 600 %.0f + + 0 + 0 + 35 + #a5efff + @@ -60,6 +66,12 @@ right 470 %.0f + + 0 + 0 + 35 + #a5efff + diff --git a/src/textelement.cpp b/src/textelement.cpp index b9d5a79..0187866 100644 --- a/src/textelement.cpp +++ b/src/textelement.cpp @@ -140,6 +140,7 @@ void TextElement::addToScene(GraphicsScene* scene) { specialFields_.push_back(static_cast(i)); } + } QString replaced = data_; diff --git a/src/themescreen.cpp b/src/themescreen.cpp index 6458836..b9ef61c 100644 --- a/src/themescreen.cpp +++ b/src/themescreen.cpp @@ -16,6 +16,7 @@ * */ +#include #include #include #include @@ -57,9 +58,46 @@ bool ThemeScreen::load(QDomNode const& data, Reader* reader) for(int i = 0; i < options.size(); i++) { - if(!element->setAttribute(options.at(i).nodeName(), options.at(i).toElement().text())) + QString nodeName = options.at(i).nodeName(); + + + + if(nodeName == "effect") + { + QDomNode effectName = options.at(i).attributes().namedItem("name"); + + if(effectName.isNull()) + { + qDebug() << "Missing name for effect"; + } + else + { + if(!element->setEffect(effectName.toAttr().value())) + { + qDebug() << "Invalid effect: " << effectName.toAttr().value(); + } + else + { + QDomNodeList themeOptions = options.at(i).childNodes(); + + for(int i = 0; i < themeOptions.size(); i++) + { + if(!element->setEffectAttribute(themeOptions.at(i).nodeName(), themeOptions.at(i).toElement().text())) + { + qDebug() << "Warning: invalid effect option: " << themeOptions.at(i).nodeName(); + } + } + + element->applyEffect(); + } + } + } + else { - qDebug() << "Warning: invalid option: " << options.at(i).nodeName(); + if(!element->setAttribute(nodeName, options.at(i).toElement().text())) + { + qDebug() << "Warning: invalid option: " << options.at(i).nodeName(); + } } } @@ -67,6 +105,8 @@ bool ThemeScreen::load(QDomNode const& data, Reader* reader) } + forceRepaint(); + return true; } @@ -95,3 +135,10 @@ void ThemeScreen::update() elements_.at(i)->update(); } } + +void ThemeScreen::forceRepaint() +{ + rotate(0.0001); + QApplication::processEvents(); + rotate(-0.0001); +} diff --git a/src/themescreen.h b/src/themescreen.h index 33fc14a..14a8239 100644 --- a/src/themescreen.h +++ b/src/themescreen.h @@ -34,6 +34,7 @@ public: bool load(QDomNode const& data, Reader* reader); virtual void update(); void removeElements(); + void forceRepaint(); private: QList elements_; -- 1.7.9.5