blob: 678f3dd68bfa102668ecbe4e15ac67c37dc4ec28 [file] [log] [blame]
Robert Bieber97427042010-07-02 00:20:36 +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>
23
Robert Bieberc5025c72010-08-05 19:05:36 +000024#include "parsetreenode.h"
Robert Bieber97427042010-07-02 00:20:36 +000025#include "rbprogressbar.h"
26#include "projectmodel.h"
27
28RBProgressBar::RBProgressBar(RBViewport *parent, const RBRenderInfo &info,
Robert Bieberc5025c72010-08-05 19:05:36 +000029 ParseTreeNode* node, bool pv)
30 :RBMovable(parent), node(node)
Robert Bieber97427042010-07-02 00:20:36 +000031{
Robert Bieberc5025c72010-08-05 19:05:36 +000032 int paramCount = node->getElement()->params_count;
33 skin_tag_parameter* params = node->getElement()->params;
34
Robert Bieber97427042010-07-02 00:20:36 +000035 /* First we set everything to defaults */
36 bitmap = 0;
37 color = parent->getFGColor();
38 int x = 0;
39 int y = parent->getTextOffset();
40 int w = parent->boundingRect().width();
41 int h = 6;
42
43 /* Now we change defaults if the parameters are there */
44
45 if(paramCount > 0 && params[0].type != skin_tag_parameter::DEFAULT)
46 {
Robert Bieber15488a02010-07-15 06:24:11 +000047 x = params[0].data.number;
Robert Bieber97427042010-07-02 00:20:36 +000048 }
49
50 if(paramCount > 1 && params[1].type != skin_tag_parameter::DEFAULT)
51 {
Robert Bieber15488a02010-07-15 06:24:11 +000052 y = params[1].data.number;
Robert Bieber97427042010-07-02 00:20:36 +000053 }
54
55 if(paramCount > 2 && params[2].type != skin_tag_parameter::DEFAULT)
56 {
Robert Bieber15488a02010-07-15 06:24:11 +000057 w = params[2].data.number;
Robert Bieber97427042010-07-02 00:20:36 +000058 }
59
60 if(paramCount > 3 && params[3].type != skin_tag_parameter::DEFAULT)
61 {
Robert Bieber15488a02010-07-15 06:24:11 +000062 h = params[3].data.number;
Robert Bieber97427042010-07-02 00:20:36 +000063 }
64
65 if(paramCount > 4 && params[4].type != skin_tag_parameter::DEFAULT)
66 {
67 QString imPath(params[4].data.text);
68 imPath = info.settings()->value("imagepath", "") + "/" + imPath;
69 bitmap = new QPixmap(imPath);
70 if(bitmap->isNull())
71 {
72 delete bitmap;
73 bitmap = 0;
74 }
75 }
Robert Bieberc5025c72010-08-05 19:05:36 +000076 size = QRectF(0, 0, w, h);
Robert Bieber97427042010-07-02 00:20:36 +000077
78 /* Finally, we scale the width according to the amount played */
Robert Bieber64af2b32010-07-12 06:07:50 +000079 int percent;
80 if(pv)
81 {
82 percent = (info.device()->data("pv").toInt() + 50) * 100 / 56;
83 }
84 else
85 {
86 percent = info.device()->data("px").toInt();
87 }
88 if(percent > 100)
89 percent = 100;
90 if(percent < 0)
91 percent = 0;
92
Robert Bieber97427042010-07-02 00:20:36 +000093 w = w * percent / 100;
94
Robert Bieberc5025c72010-08-05 19:05:36 +000095 renderSize = QRectF(0, 0, w, h);
Robert Bieber97427042010-07-02 00:20:36 +000096 setPos(x, y);
97 parent->addTextOffset(h);
98}
99
100RBProgressBar::~RBProgressBar()
101{
102 if(bitmap)
103 delete bitmap;
104}
105
Robert Bieber97427042010-07-02 00:20:36 +0000106void RBProgressBar::paint(QPainter *painter,
107 const QStyleOptionGraphicsItem *option,
108 QWidget *widget)
109{
Robert Bieber92882f22010-08-06 07:53:19 +0000110 renderSize.setHeight(size.height());
111
Robert Bieber97427042010-07-02 00:20:36 +0000112 if(bitmap && !bitmap->isNull())
113 {
Robert Bieberc5025c72010-08-05 19:05:36 +0000114 painter->drawPixmap(renderSize, *bitmap, renderSize);
Robert Bieber97427042010-07-02 00:20:36 +0000115 }
116 else
117 {
Robert Bieberc5025c72010-08-05 19:05:36 +0000118 painter->fillRect(renderSize, color);
Robert Bieber97427042010-07-02 00:20:36 +0000119 }
Robert Bieberf657e492010-08-05 08:49:34 +0000120
121 RBMovable::paint(painter, option, widget);
122}
123
124void RBProgressBar::saveGeometry()
125{
Robert Bieberc5025c72010-08-05 19:05:36 +0000126 QPointF origin = pos();
127 QRectF bounds = boundingRect();
Robert Bieberf657e492010-08-05 08:49:34 +0000128
Robert Bieberc5025c72010-08-05 19:05:36 +0000129 node->modParam(static_cast<int>(origin.x()), 0);
130 node->modParam(static_cast<int>(origin.y()), 1);
131 node->modParam(static_cast<int>(bounds.width()), 2);
132 node->modParam(static_cast<int>(bounds.height()), 3);
133
134 if(!bitmap)
135 node->modParam(QVariant(), 4);
Robert Bieber97427042010-07-02 00:20:36 +0000136}