Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 23 | #include <QFile> |
| 24 | #include <QBitmap> |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 25 | |
| 26 | #include "rbimage.h" |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 27 | #include "parsetreenode.h" |
Robert Bieber | dfc109a | 2010-08-13 19:14:54 +0000 | [diff] [blame] | 28 | #include <rbscene.h> |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 29 | |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 30 | RBImage::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 Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 34 | { |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 35 | /* Prevents RBMovable from interfering with initial position setting */ |
| 36 | setFlag(ItemSendsGeometryChanges, false); |
| 37 | |
Robert Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 38 | 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 Bieber | f4fd16c | 2010-06-23 07:23:47 +0000 | [diff] [blame] | 54 | size = QRectF(0, 0, image->width(), image->height() / tiles); |
| 55 | setPos(x, y); |
Robert Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 56 | |
| 57 | } |
| 58 | else |
Robert Bieber | 3313ab8 | 2010-06-29 07:15:41 +0000 | [diff] [blame] | 59 | { |
Robert Bieber | dfc109a | 2010-08-13 19:14:54 +0000 | [diff] [blame] | 60 | RBScene* s = dynamic_cast<RBScene*>(scene()); |
| 61 | s->addWarning(QObject::tr("Image not found: ") + file); |
| 62 | |
Robert Bieber | 3313ab8 | 2010-06-29 07:15:41 +0000 | [diff] [blame] | 63 | size = QRectF(0, 0, 0, 0); |
Robert Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 64 | image = 0; |
Robert Bieber | 3313ab8 | 2010-06-29 07:15:41 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | RBImage::RBImage(const RBImage &other, QGraphicsItem* parent) |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 69 | : RBMovable(parent), tiles(other.tiles), currentTile(other.currentTile), |
| 70 | node(other.node) |
Robert Bieber | 3313ab8 | 2010-06-29 07:15:41 +0000 | [diff] [blame] | 71 | { |
| 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 Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Robert Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 80 | RBImage::~RBImage() |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 81 | { |
Robert Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 82 | if(image) |
| 83 | delete image; |
| 84 | } |
| 85 | |
Robert Bieber | d302705 | 2010-06-23 07:18:22 +0000 | [diff] [blame] | 86 | void 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 Bieber | 83c60a1 | 2010-08-03 22:29:26 +0000 | [diff] [blame] | 95 | |
| 96 | RBMovable::paint(painter, option, widget); |
| 97 | } |
| 98 | |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 99 | |
| 100 | |
Robert Bieber | 83c60a1 | 2010-08-03 22:29:26 +0000 | [diff] [blame] | 101 | void RBImage::saveGeometry() |
| 102 | { |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 103 | QPointF origin = pos(); |
Robert Bieber | 83c60a1 | 2010-08-03 22:29:26 +0000 | [diff] [blame] | 104 | |
Robert Bieber | bfc2097 | 2010-08-05 07:28:01 +0000 | [diff] [blame] | 105 | node->modParam(static_cast<int>(origin.x()), 2); |
| 106 | node->modParam(static_cast<int>(origin.y()), 3); |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 107 | } |