0a04037dcfe95b8804c53628f50f41e3a8451105
[kugel-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blob0a04037dcfe95b8804c53628f50f41e3a8451105
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 "rbscreen.h"
23 #include "rbviewport.h"
25 #include <QPainter>
26 #include <QFile>
28 RBScreen::RBScreen(const RBRenderInfo& info, QGraphicsItem *parent) :
29 QGraphicsItem(parent), backdrop(0), project(project)
32 width = info.settings()->value("#screenwidth", "300").toInt();
33 height = info.settings()->value("#screenheight", "200").toInt();
35 QString bg = info.settings()->value("background color", "000000");
36 bgColor = stringToColor(bg, Qt::white);
38 QString fg = info.settings()->value("foreground color", "FFFFFF");
39 fgColor = stringToColor(fg, Qt::black);
41 settings = info.settings();
43 /* Loading backdrop if available */
44 themeBase = info.settings()->value("themebase", "");
45 QString backdropFile = info.settings()->value("backdrop", "");
46 backdropFile.replace("/.rockbox/backdrops/", "");
48 if(QFile::exists(themeBase + "/backdrops/" + backdropFile))
50 backdrop = new QPixmap(themeBase + "/backdrops/" + backdropFile);
52 /* If a backdrop has been found, use its width and height */
53 if(!backdrop->isNull())
55 width = backdrop->width();
56 height = backdrop->height();
58 else
60 delete backdrop;
61 backdrop = 0;
66 RBScreen::~RBScreen()
68 if(backdrop)
69 delete backdrop;
72 QPainterPath RBScreen::shape() const
74 QPainterPath retval;
75 retval.addRect(0, 0, width, height);
76 return retval;
79 QRectF RBScreen::boundingRect() const
81 return QRectF(0, 0, width, height);
84 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
85 QWidget *widget)
87 if(backdrop)
89 painter->drawPixmap(0, 0, width, height, *backdrop);
91 else
93 painter->fillRect(0, 0, width, height, bgColor);
97 void RBScreen::showViewport(QString name)
99 if(namedViewports.value(name, 0) == 0)
100 return;
102 namedViewports.value(name)->show();
103 update();
106 void RBScreen::setBackdrop(QString filename)
109 if(backdrop)
110 delete backdrop;
112 filename = settings->value("imagepath", "") + "/" + filename;
114 if(QFile::exists(filename))
115 backdrop = new QPixmap(filename);
116 else
117 backdrop = 0;
120 QColor RBScreen::stringToColor(QString str, QColor fallback)
123 QColor retval;
125 if(str.length() == 6)
127 for(int i = 0; i < 6; i++)
129 char c = str[i].toAscii();
130 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
131 isdigit(c)))
133 str = "";
134 break;
137 if(str != "")
138 retval = QColor("#" + str);
139 else
140 retval = fallback;
142 else if(str.length() == 1)
144 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
146 int shade = 255 * (str[0].toAscii() - '0') / 3;
147 retval = QColor(shade, shade, shade);
149 else
151 retval = fallback;
154 else
156 retval = fallback;
159 return retval;