Theme Editor: Fixed some compiler warnings and a segfault. Got some basic text rende...
[kugel-rb.git] / utils / themeeditor / graphics / rbviewport.cpp
blob03a760480432f8a647567a08553ebec3e7366962
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <QPainter>
23 #include <QPainterPath>
25 #include "rbviewport.h"
26 #include "rbscreen.h"
27 #include "rbrenderinfo.h"
28 #include "parsetreemodel.h"
29 #include "tag_table.h"
30 #include "skin_parser.h"
32 RBViewport::RBViewport(skin_element* node, const RBRenderInfo& info)
33 : QGraphicsItem(info.screen()), font(info.screen()->getFont(0)),
34 foreground(info.screen()->foreground()),
35 background(info.screen()->background()), textOffset(0,0),
36 screen(info.screen())
38 if(!node->tag)
40 /* Default viewport takes up the entire screen */
41 size = QRectF(0, 0, info.screen()->getWidth(),
42 info.screen()->getHeight());
43 customUI = false;
45 if(info.model()->rowCount(QModelIndex()) > 1)
47 /* If there is more than one viewport in the document */
48 setVisible(false);
50 else
52 setVisible(true);
55 else
57 int param = 0;
58 QString ident;
59 int x,y,w,h;
60 /* Rendering one of the other types of viewport */
61 switch(node->tag->name[1])
63 case '\0':
64 customUI = false;
65 param = 0;
66 break;
68 case 'l':
69 /* A preloaded viewport definition */
70 ident = node->params[0].data.text;
71 customUI = false;
72 hide();
73 info.screen()->loadViewport(ident, this);
74 param = 1;
75 break;
77 case 'i':
78 /* Custom UI Viewport */
79 customUI = true;
80 param = 1;
81 if(node->params[0].type == skin_tag_parameter::DEFAULT)
83 setVisible(true);
85 else
87 hide();
88 info.screen()->loadViewport(ident, this);
90 break;
92 /* Now we grab the info common to all viewports */
93 x = node->params[param++].data.numeric;
94 y = node->params[param++].data.numeric;
96 if(node->params[param].type == skin_tag_parameter::DEFAULT)
97 w = info.screen()->getWidth() - x;
98 else
99 w = node->params[param].data.numeric;
101 if(node->params[++param].type == skin_tag_parameter::DEFAULT)
102 h = info.screen()->getHeight() - y;
103 else
104 h = node->params[param].data.numeric;
106 setPos(x, y);
107 size = QRectF(0, 0, w, h);
112 RBViewport::~RBViewport()
116 QPainterPath RBViewport::shape() const
118 QPainterPath retval;
119 retval.addRect(size);
120 return retval;
123 QRectF RBViewport::boundingRect() const
125 return size;
128 void RBViewport::paint(QPainter *painter,
129 const QStyleOptionGraphicsItem *option, QWidget *widget)
131 if(!screen->hasBackdrop() && background != screen->background())
133 painter->fillRect(size, QBrush(background));
136 painter->setBrush(Qt::NoBrush);
137 painter->setPen(customUI ? Qt::blue : Qt::red);
138 painter->drawRect(size);
141 void RBViewport::newLine()
143 if(textOffset.x() > 0)
145 textOffset.setY(textOffset.y() + lineHeight);
146 textOffset.setX(0);
150 void RBViewport::write(QString text)
152 QGraphicsItem* graphic = font->renderText(text, foreground, this);
153 graphic->setPos(textOffset.x(), textOffset.y());
154 textOffset.setX(textOffset.x() + graphic->boundingRect().width());
155 lineHeight = font->lineHeight();