Theme Editor: Working on image rendering
authorbieber <bieber@a1c6a512-1295-4272-9138-f99709370657>
Wed, 23 Jun 2010 07:18:22 +0000 (23 07:18 +0000)
committerbieber <bieber@a1c6a512-1295-4272-9138-f99709370657>
Wed, 23 Jun 2010 07:18:22 +0000 (23 07:18 +0000)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27083 a1c6a512-1295-4272-9138-f99709370657

utils/themeeditor/graphics/rbimage.cpp
utils/themeeditor/graphics/rbimage.h
utils/themeeditor/graphics/rbscreen.h
utils/themeeditor/graphics/rbviewport.cpp
utils/themeeditor/models/parsetreenode.cpp

index 5105c95..16e6bcb 100644 (file)
  ****************************************************************************/
 
 #include <QPainter>
+#include <QFile>
+#include <QBitmap>
 
 #include "rbimage.h"
 
-RBImage::RBImage(QString file, int tiles)
-    :image(file), tiles(tiles)
+RBImage::RBImage(QString file, int tiles, int x, int y, QGraphicsItem* parent)
+                     : QGraphicsItem(parent), tiles(tiles), currentTile(0)
 {
+    if(QFile::exists(file))
+    {
+        image = new QPixmap(file);
+
+        if(image->isNull())
+        {
+            delete image;
+            image = 0;
+            return;
+        }
+        else
+        {
+            image->setMask(image->createMaskFromColor(QColor(255,0,255)));
+
+        }
+
+        size = QRectF(x, y, image->width(), image->height() / tiles);
+
+    }
+    else
+        image = 0;
 }
 
-void RBImage::draw(QPainter *painter, int x, int y, int tile)
+RBImage::~RBImage()
 {
-    if(tiles == 0)
-        painter->drawPixmap(x, y, image.width(), image.height(), image);
-    else
-        painter->drawPixmap(x, y, image, 0, tile * (image.height() / tiles),
-                            image.width(), image.height() / tiles);
+    if(image)
+        delete image;
+}
+
+QRectF RBImage::boundingRect() const
+{
+    return size;
+}
+
+void RBImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
+                    QWidget *widget)
+{
+    if(!image)
+        return;
+
+    painter->drawPixmap(size, *image, QRect(0, currentTile * image->height()
+                                            / tiles, image->width(),
+                                            image->height() / tiles));
 }
index 2eaf9b6..cc949d1 100644 (file)
 #define RBIMAGE_H
 
 #include <QPixmap>
+#include <QGraphicsItem>
 
-class RBImage
+class RBImage: public QGraphicsItem
 {
 public:
-    RBImage(QString file, int tiles = 0);
-    void draw(QPainter* painter, int x, int y, int tile = 0);
+    RBImage(QString file, int tiles, int x, int y, QGraphicsItem* parent = 0);
+    virtual ~RBImage();
+
+    QRectF boundingRect() const;
+    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
+               QWidget *widget);
+
+    void setTile(int tile)
+    {
+        currentTile = tile;
+        if(currentTile > tiles - 1)
+            currentTile = tiles -1;
+    }
+
 
 private:
-    QPixmap image;
+    QPixmap* image;
     int tiles;
+    int currentTile;
+
+    QRectF size;
 
 };
 
index b60705b..bc44071 100644 (file)
@@ -54,6 +54,7 @@ public:
     void loadImage(QString name, RBImage* image)
     {
         images.insert(name, image);
+        image->hide();
     }
     RBImage* getImage(QString name){ return images.value(name, 0); }
 
index 9d94967..3a20175 100644 (file)
@@ -37,6 +37,7 @@ RBViewport::RBViewport(skin_element* node, const RBRenderInfo& info)
         /* Default viewport takes up the entire screen */
         size = QRectF(0, 0, info.screen()->getWidth(),
                       info.screen()->getHeight());
+        customUI = false;
 
         if(info.model()->rowCount(QModelIndex()) > 1)
         {
@@ -122,8 +123,9 @@ QRectF RBViewport::boundingRect() const
 void RBViewport::paint(QPainter *painter,
                        const QStyleOptionGraphicsItem *option, QWidget *widget)
 {
-    QColor color = customUI ? Qt::blue : Qt::red;
-    painter->fillRect(size, color);
+    painter->setBrush(Qt::NoBrush);
+    painter->setPen(customUI ? Qt::blue : Qt::red);
+    painter->drawRect(size);
 }
 
 /* Called at the end of a logical line */
index ed518a4..41bee5c 100644 (file)
@@ -25,6 +25,8 @@
 #include "parsetreenode.h"
 #include "parsetreemodel.h"
 
+#include "rbimage.h"
+
 #include <iostream>
 
 int ParseTreeNode::openConditionals = 0;
@@ -504,6 +506,8 @@ void ParseTreeNode::render(const RBRenderInfo& info)
 
     for(int i = element->params_count; i < children.count(); i++)
         children[i]->render(info, dynamic_cast<RBViewport*>(rendered));
+
+    std::cout << rendered->children().count() << std::endl;
 }
 
 /* This version is called for logical lines and such */
@@ -518,17 +522,56 @@ void ParseTreeNode::render(const RBRenderInfo &info, RBViewport* viewport)
     else if(element->type == TAG)
     {
         QString filename;
+        QString id;
+        int x, y, tiles;
+        RBImage* image;
 
         /* Two switch statements to narrow down the tag name */
         switch(element->tag->name[0])
         {
 
+        case 'x':
+            switch(element->tag->name[1])
+            {
+            case 'l':
+                /* %xl */
+                id = element->params[0].data.text;
+                filename = info.settings()->value("imagepath", "") + "/" +
+                           element->params[1].data.text;
+                x = element->params[2].data.numeric;
+                y = element->params[3].data.numeric;
+                if(element->params_count > 4)
+                    tiles = element->params[4].data.numeric;
+                else
+                    tiles = 1;
+
+                info.screen()->loadImage(id, new RBImage(filename, tiles, x, y,
+                                                         viewport));
+                break;
+
+            case '\0':
+                /* %x */
+                id = element->params[0].data.text;
+                filename = info.settings()->value("imagepath", "") + "/" +
+                           element->params[1].data.text;
+                x = element->params[2].data.numeric;
+                y = element->params[3].data.numeric;
+                image = new RBImage(filename, 1, x, y, viewport);
+                info.screen()->loadImage(id, new RBImage(filename, 1, x, y,
+                                                         viewport));
+                info.screen()->getImage(id)->show();
+                break;
+
+            }
+
+            break;
+
         case 'X':
 
             switch(element->tag->name[1])
             {
             case '\0':
-                /* %X tag */
+                /* %X */
                 filename = QString(element->params[0].data.text);
                 info.screen()->setBackdrop(filename);
                 break;