d6a9aa6240b9242c5252adc3e35100fe89944826
[kugel-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blobd6a9aa6240b9242c5252adc3e35100fe89944826
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", "FFFFFF");
36 bgColor = stringToColor(bg, Qt::white);
38 QString fg = info.settings()->value("foreground color", "000000");
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;
65 fonts.insert(0, new RBFont("Nothin'"));
68 RBScreen::~RBScreen()
70 if(backdrop)
71 delete backdrop;
73 QMap<int, RBFont*>::iterator i;
74 for(i = fonts.begin(); i != fonts.end(); i++)
75 delete (*i);
77 QMap<QString, QList<RBViewport*>*>::iterator it;
78 for(it = namedViewports.begin(); it != namedViewports.end(); it++)
79 delete (*it);
82 QPainterPath RBScreen::shape() const
84 QPainterPath retval;
85 retval.addRect(0, 0, width, height);
86 return retval;
89 QRectF RBScreen::boundingRect() const
91 return QRectF(0, 0, width, height);
94 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
95 QWidget *widget)
97 if(backdrop)
99 painter->drawPixmap(0, 0, width, height, *backdrop);
101 else
103 painter->fillRect(0, 0, width, height, bgColor);
107 void RBScreen::loadViewport(QString name, RBViewport *view)
109 QList<RBViewport*>* list;
110 if(namedViewports.value(name, 0) == 0)
112 list = new QList<RBViewport*>;
113 list->append(view);
114 namedViewports.insert(name, list);
116 else
118 list = namedViewports.value(name, 0);
119 list->append(view);
123 void RBScreen::showViewport(QString name)
125 if(namedViewports.value(name, 0) == 0)
126 return;
128 QList<RBViewport*>* list = namedViewports.value(name, 0);
129 for(int i = 0; i < list->count(); i++)
130 list->at(i)->show();
133 void RBScreen::loadFont(int id, RBFont* font)
135 if(id < 2 || id > 9)
136 return;
138 fonts.insert(id, font);
141 RBFont* RBScreen::getFont(int id)
143 if(fonts.value(id, 0) != 0)
144 return fonts.value(id);
145 else
146 return fonts.value(0, 0);
150 void RBScreen::setBackdrop(QString filename)
153 if(backdrop)
154 delete backdrop;
156 filename = settings->value("imagepath", "") + "/" + filename;
158 if(QFile::exists(filename))
159 backdrop = new QPixmap(filename);
160 else
161 backdrop = 0;
164 void RBScreen::makeCustomUI(QString id)
166 if(namedViewports.value(id, 0) != 0)
168 QMap<QString, QList<RBViewport*>*>::iterator i;
169 for(i = namedViewports.begin(); i != namedViewports.end(); i++)
170 for(int j = 0; j < (*i)->count(); j++)
171 (*i)->at(j)->clearCustomUI();
172 for(int i = 0; i < namedViewports.value(id)->count(); i++)
173 namedViewports.value(id)->at(i)->makeCustomUI();
174 for(int i = 0; i < namedViewports.value(id)->count(); i++)
175 namedViewports.value(id)->at(i)->show();
179 QColor RBScreen::stringToColor(QString str, QColor fallback)
182 QColor retval;
184 if(str.length() == 6)
186 for(int i = 0; i < 6; i++)
188 char c = str[i].toAscii();
189 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
190 isdigit(c)))
192 str = "";
193 break;
196 if(str != "")
197 retval = QColor("#" + str);
198 else
199 retval = fallback;
201 else if(str.length() == 1)
203 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
205 int shade = 255 * (str[0].toAscii() - '0') / 3;
206 retval = QColor(shade, shade, shade);
208 else
210 retval = fallback;
213 else
215 retval = fallback;
218 return retval;