Added some effects to graphics elements and changed default theme a bit.
authoreshe <jessehakanen@gmail.com>
Mon, 19 Jul 2010 14:58:44 +0000 (15:58 +0100)
committereshe <jessehakanen@gmail.com>
Mon, 19 Jul 2010 14:58:44 +0000 (15:58 +0100)
15 files changed:
jspeed.pro
src/blureffect.cpp [new file with mode: 0644]
src/blureffect.h [new file with mode: 0644]
src/dropshadoweffect.cpp [new file with mode: 0644]
src/dropshadoweffect.h [new file with mode: 0644]
src/effect.cpp [new file with mode: 0644]
src/effect.h [new file with mode: 0644]
src/graphicselement.cpp
src/graphicselement.h
src/opacityeffect.cpp [new file with mode: 0644]
src/opacityeffect.h [new file with mode: 0644]
src/resources/themes/default/theme.xml
src/textelement.cpp
src/themescreen.cpp
src/themescreen.h

index d5f39ee..2004508 100644 (file)
@@ -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 (file)
index 0000000..5635964
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtCore/QString>
+#include <QtCore/QDebug>
+#include <QtGui/QGraphicsBlurEffect>
+#include <QtGui/QGraphicsItem>
+#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<Attribute>(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 (file)
index 0000000..28ea685
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#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 (file)
index 0000000..0eac608
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtCore/QString>
+#include <QtCore/QDebug>
+#include <QtCore/QRectF>
+#include <QtGui/QGraphicsDropShadowEffect>
+#include <QtGui/QGraphicsItem>
+#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<Attribute>(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 (file)
index 0000000..864d585
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#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 (file)
index 0000000..df7cc53
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtCore/QString>
+#include <QtGui/QGraphicsItem>
+#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<qreal>(tmp);
+                    return i;
+                }
+                else
+                {
+                    return -1;
+                }
+            }
+        }
+    }
+
+    return -1;
+}
+
diff --git a/src/effect.h b/src/effect.h
new file mode 100644 (file)
index 0000000..929680f
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#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
index 3befe29..452af54 100644 (file)
@@ -22,6 +22,9 @@
 #include <QtGui/QGraphicsScene>
 #include <QtGui/QFont>
 #include <QtGui/QFontDatabase>
+#include <QtGui/QGraphicsEffect>
+#include <QtGui/QGraphicsDropShadowEffect>
+#include <QtGui/QGraphicsItem>
 #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());
+}
index ee9c4c2..94d0143 100644 (file)
@@ -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<QString, QString> loadedFonts_;
 };
 
diff --git a/src/opacityeffect.cpp b/src/opacityeffect.cpp
new file mode 100644 (file)
index 0000000..2b1ea09
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtCore/QString>
+#include <QtCore/QDebug>
+#include <QtGui/QGraphicsOpacityEffect>
+#include <QtGui/QGraphicsItem>
+#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<Attribute>(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 (file)
index 0000000..e85a5ae
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#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
index 0064f70..6068249 100644 (file)
                <align>right</align>
                <width>600</width>
                <format>%.0f</format>
+               <effect name="dropshadow">
+                       <xoffset>0</xoffset>
+                       <yoffset>0</yoffset>                    
+                       <radius>35</radius>
+                       <color>#a5efff</color>
+               </effect>
        </text>
 </orientation>
 <orientation name="portrait">
                <align>right</align>
                <width>470</width>
                <format>%.0f</format>
+               <effect name="dropshadow">
+                        <xoffset>0</xoffset>
+                        <yoffset>0</yoffset>
+                        <radius>35</radius>
+                        <color>#a5efff</color>
+                </effect>
        </text>
 </orientation>
 </theme>
index b9d5a79..0187866 100644 (file)
@@ -140,6 +140,7 @@ void TextElement::addToScene(GraphicsScene* scene)
         {
             specialFields_.push_back(static_cast<Field>(i));
         }
+
     }
 
     QString replaced = data_;
index 6458836..b9ef61c 100644 (file)
@@ -16,6 +16,7 @@
  *
  */
 
+#include <QtGui/QApplication>
 #include <QtGui/QWidget>
 #include <QtGui/QGraphicsItem>
 #include <QtCore/QString>
@@ -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);
+}
index 33fc14a..14a8239 100644 (file)
@@ -34,6 +34,7 @@ public:
     bool load(QDomNode const& data, Reader* reader);
     virtual void update();
     void removeElements();
+    void forceRepaint();
 
 private:
     QList<GraphicsElement*> elements_;