da6d20bbe8cc19da1adcb3a8c092fec17efe3bbe
[kugel-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blobda6d20bbe8cc19da1adcb3a8c092fec17efe3bbe
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"
24 #include "devicestate.h"
26 #include <QPainter>
27 #include <QFile>
29 RBScreen::RBScreen(const RBRenderInfo& info, QGraphicsItem *parent) :
30 QGraphicsItem(parent), backdrop(0), project(project)
34 width = info.settings()->value("#screenwidth", "300").toInt();
35 height = info.settings()->value("#screenheight", "200").toInt();
38 width = info.device()->data("screenwidth").toInt();
39 height = info.device()->data("screenheight").toInt();
41 QString bg = info.settings()->value("background color", "FFFFFF");
42 bgColor = stringToColor(bg, Qt::white);
44 QString fg = info.settings()->value("foreground color", "000000");
45 fgColor = stringToColor(fg, Qt::black);
47 settings = info.settings();
49 /* Loading backdrop if available */
50 themeBase = info.settings()->value("themebase", "");
51 QString backdropFile = info.settings()->value("backdrop", "");
52 backdropFile.replace("/.rockbox/backdrops/", "");
54 if(QFile::exists(themeBase + "/backdrops/" + backdropFile))
56 backdrop = new QPixmap(themeBase + "/backdrops/" + backdropFile);
58 /* If a backdrop has been found, use its width and height */
59 if(!backdrop->isNull())
61 width = backdrop->width();
62 height = backdrop->height();
64 else
66 delete backdrop;
67 backdrop = 0;
71 fonts.insert(0, new RBFont("Nothin'"));
74 RBScreen::~RBScreen()
76 if(backdrop)
77 delete backdrop;
79 QMap<int, RBFont*>::iterator i;
80 for(i = fonts.begin(); i != fonts.end(); i++)
81 delete (*i);
83 QMap<QString, QList<RBViewport*>*>::iterator it;
84 for(it = namedViewports.begin(); it != namedViewports.end(); it++)
85 delete (*it);
88 QPainterPath RBScreen::shape() const
90 QPainterPath retval;
91 retval.addRect(0, 0, width, height);
92 return retval;
95 QRectF RBScreen::boundingRect() const
97 return QRectF(0, 0, width, height);
100 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
101 QWidget *widget)
103 if(backdrop)
105 painter->drawPixmap(0, 0, width, height, *backdrop);
107 else
109 painter->fillRect(0, 0, width, height, bgColor);
113 void RBScreen::loadViewport(QString name, RBViewport *view)
115 QList<RBViewport*>* list;
116 if(namedViewports.value(name, 0) == 0)
118 list = new QList<RBViewport*>;
119 list->append(view);
120 namedViewports.insert(name, list);
122 else
124 list = namedViewports.value(name, 0);
125 list->append(view);
129 void RBScreen::showViewport(QString name)
131 if(namedViewports.value(name, 0) == 0)
132 return;
134 QList<RBViewport*>* list = namedViewports.value(name, 0);
135 for(int i = 0; i < list->count(); i++)
136 list->at(i)->show();
139 void RBScreen::loadFont(int id, RBFont* font)
141 if(id < 2 || id > 9)
142 return;
144 fonts.insert(id, font);
147 RBFont* RBScreen::getFont(int id)
149 if(fonts.value(id, 0) != 0)
150 return fonts.value(id);
151 else
152 return fonts.value(0, 0);
156 void RBScreen::setBackdrop(QString filename)
159 if(backdrop)
160 delete backdrop;
162 filename = settings->value("imagepath", "") + "/" + filename;
164 if(QFile::exists(filename))
165 backdrop = new QPixmap(filename);
166 else
167 backdrop = 0;
170 void RBScreen::makeCustomUI(QString id)
172 if(namedViewports.value(id, 0) != 0)
174 QMap<QString, QList<RBViewport*>*>::iterator i;
175 for(i = namedViewports.begin(); i != namedViewports.end(); i++)
176 for(int j = 0; j < (*i)->count(); j++)
177 (*i)->at(j)->clearCustomUI();
178 for(int i = 0; i < namedViewports.value(id)->count(); i++)
179 namedViewports.value(id)->at(i)->makeCustomUI();
180 for(int i = 0; i < namedViewports.value(id)->count(); i++)
181 namedViewports.value(id)->at(i)->show();
185 QColor RBScreen::stringToColor(QString str, QColor fallback)
188 QColor retval;
190 if(str.length() == 6)
192 for(int i = 0; i < 6; i++)
194 char c = str[i].toAscii();
195 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
196 isdigit(c)))
198 str = "";
199 break;
202 if(str != "")
203 retval = QColor("#" + str);
204 else
205 retval = fallback;
207 else if(str.length() == 1)
209 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
211 int shade = 255 * (str[0].toAscii() - '0') / 3;
212 retval = QColor(shade, shade, shade);
214 else
216 retval = fallback;
219 else
221 retval = fallback;
224 return retval;