blob: 31159ecf7549d91aaf821a835e9aff7d6cfaff8a [file] [log] [blame]
Robert Bieberac9287b2010-06-22 07:55:50 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Robert Bieber
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <QPainter>
Robert Bieberd3027052010-06-23 07:18:22 +000023#include <QFile>
24#include <QBitmap>
Robert Bieberac9287b2010-06-22 07:55:50 +000025
26#include "rbimage.h"
Robert Bieberbfc20972010-08-05 07:28:01 +000027#include "parsetreenode.h"
Robert Bieberdfc109a2010-08-13 19:14:54 +000028#include <rbscene.h>
Robert Bieberac9287b2010-06-22 07:55:50 +000029
Robert Bieberbfc20972010-08-05 07:28:01 +000030RBImage::RBImage(QString file, int tiles, int x, int y, ParseTreeNode* node,
31 QGraphicsItem* parent)
32 : RBMovable(parent), tiles(tiles), currentTile(0),
33 node(node)
Robert Bieberac9287b2010-06-22 07:55:50 +000034{
Robert Bieberbfc20972010-08-05 07:28:01 +000035 /* Prevents RBMovable from interfering with initial position setting */
36 setFlag(ItemSendsGeometryChanges, false);
37
Robert Bieberd3027052010-06-23 07:18:22 +000038 if(QFile::exists(file))
39 {
40 image = new QPixmap(file);
41
42 if(image->isNull())
43 {
44 delete image;
45 image = 0;
46 return;
47 }
48 else
49 {
50 image->setMask(image->createMaskFromColor(QColor(255,0,255)));
51
52 }
53
Robert Bieberf4fd16c2010-06-23 07:23:47 +000054 size = QRectF(0, 0, image->width(), image->height() / tiles);
55 setPos(x, y);
Robert Bieberd3027052010-06-23 07:18:22 +000056
57 }
58 else
Robert Bieber3313ab82010-06-29 07:15:41 +000059 {
Robert Bieberdfc109a2010-08-13 19:14:54 +000060 RBScene* s = dynamic_cast<RBScene*>(scene());
61 s->addWarning(QObject::tr("Image not found: ") + file);
62
Robert Bieber3313ab82010-06-29 07:15:41 +000063 size = QRectF(0, 0, 0, 0);
Robert Bieberd3027052010-06-23 07:18:22 +000064 image = 0;
Robert Bieber3313ab82010-06-29 07:15:41 +000065 }
66}
67
68RBImage::RBImage(const RBImage &other, QGraphicsItem* parent)
Robert Bieberbfc20972010-08-05 07:28:01 +000069 : RBMovable(parent), tiles(other.tiles), currentTile(other.currentTile),
70 node(other.node)
Robert Bieber3313ab82010-06-29 07:15:41 +000071{
72 if(other.image)
73 image = new QPixmap(*(other.image));
74 else
75 image = 0;
76 size = other.size;
77 setPos(other.x(), other.y());
Robert Bieberac9287b2010-06-22 07:55:50 +000078}
79
Robert Bieberd3027052010-06-23 07:18:22 +000080RBImage::~RBImage()
Robert Bieberac9287b2010-06-22 07:55:50 +000081{
Robert Bieberd3027052010-06-23 07:18:22 +000082 if(image)
83 delete image;
84}
85
Robert Bieberd3027052010-06-23 07:18:22 +000086void RBImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
87 QWidget *widget)
88{
89 if(!image)
90 return;
91
92 painter->drawPixmap(size, *image, QRect(0, currentTile * image->height()
93 / tiles, image->width(),
94 image->height() / tiles));
Robert Bieber83c60a12010-08-03 22:29:26 +000095
96 RBMovable::paint(painter, option, widget);
97}
98
Robert Bieberbfc20972010-08-05 07:28:01 +000099
100
Robert Bieber83c60a12010-08-03 22:29:26 +0000101void RBImage::saveGeometry()
102{
Robert Bieberbfc20972010-08-05 07:28:01 +0000103 QPointF origin = pos();
Robert Bieber83c60a12010-08-03 22:29:26 +0000104
Robert Bieberbfc20972010-08-05 07:28:01 +0000105 node->modParam(static_cast<int>(origin.x()), 2);
106 node->modParam(static_cast<int>(origin.y()), 3);
Robert Bieberac9287b2010-06-22 07:55:50 +0000107}