Theme Editor: Renderer now allows multiple viewports to share an identifier
[kugel-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blobd37050b0b5bd17c8eab93741b895dc7d550bb93c
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;
71 QMap<int, RBFont*>::iterator i;
72 for(i = fonts.begin(); i != fonts.end(); i++)
73 delete (*i);
75 QMap<QString, QList<RBViewport*>*>::iterator it;
76 for(it = namedViewports.begin(); it != namedViewports.end(); it++)
77 delete (*it);
80 QPainterPath RBScreen::shape() const
82 QPainterPath retval;
83 retval.addRect(0, 0, width, height);
84 return retval;
87 QRectF RBScreen::boundingRect() const
89 return QRectF(0, 0, width, height);
92 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
93 QWidget *widget)
95 if(backdrop)
97 painter->drawPixmap(0, 0, width, height, *backdrop);
99 else
101 painter->fillRect(0, 0, width, height, bgColor);
105 void RBScreen::loadViewport(QString name, RBViewport *view)
107 QList<RBViewport*>* list;
108 if(namedViewports.value(name, 0) == 0)
110 list = new QList<RBViewport*>;
111 list->append(view);
112 namedViewports.insert(name, list);
114 else
116 list = namedViewports.value(name, 0);
117 list->append(view);
121 void RBScreen::showViewport(QString name)
123 if(namedViewports.value(name, 0) == 0)
124 return;
126 QList<RBViewport*>* list = namedViewports.value(name, 0);
127 for(int i = 0; i < list->count(); i++)
128 list->at(i)->show();
131 void RBScreen::loadFont(int id, RBFont* font)
133 if(id < 2 || id > 9)
134 return;
136 fonts.insert(id, font);
139 RBFont* RBScreen::getFont(int id)
141 if(fonts.value(id, 0) != 0)
142 return fonts.value(id);
143 else
144 return fonts.value(0, 0);
148 void RBScreen::setBackdrop(QString filename)
151 if(backdrop)
152 delete backdrop;
154 filename = settings->value("imagepath", "") + "/" + filename;
156 if(QFile::exists(filename))
157 backdrop = new QPixmap(filename);
158 else
159 backdrop = 0;
162 void RBScreen::makeCustomUI(QString id)
164 if(namedViewports.value(id, 0) != 0)
166 QMap<QString, QList<RBViewport*>*>::iterator i;
167 for(i = namedViewports.begin(); i != namedViewports.end(); i++)
168 for(int j = 0; j < (*i)->count(); j++)
169 (*i)->at(j)->clearCustomUI();
170 for(int i = 0; i < namedViewports.value(id)->count(); i++)
171 namedViewports.value(id)->at(i)->makeCustomUI();
172 for(int i = 0; i < namedViewports.value(id)->count(); i++)
173 namedViewports.value(id)->at(i)->show();
177 QColor RBScreen::stringToColor(QString str, QColor fallback)
180 QColor retval;
182 if(str.length() == 6)
184 for(int i = 0; i < 6; i++)
186 char c = str[i].toAscii();
187 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
188 isdigit(c)))
190 str = "";
191 break;
194 if(str != "")
195 retval = QColor("#" + str);
196 else
197 retval = fallback;
199 else if(str.length() == 1)
201 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
203 int shade = 255 * (str[0].toAscii() - '0') / 3;
204 retval = QColor(shade, shade, shade);
206 else
208 retval = fallback;
211 else
213 retval = fallback;
216 return retval;