Theme Editor: Added Show Viewports option to device configuration panel, implemented...
[kugel-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blobb93d3c88ad67080d7995210de9d13c4b87f46153
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, bool remote,
30 QGraphicsItem *parent)
31 :QGraphicsItem(parent), backdrop(0), project(project)
34 if(remote)
36 width = info.device()->data("remotewidth").toInt();
37 height = info.device()->data("remoteheight").toInt();
39 else
41 width = info.device()->data("screenwidth").toInt();
42 height = info.device()->data("screenheight").toInt();
45 QString bg = info.settings()->value("background color", "FFFFFF");
46 bgColor = stringToColor(bg, Qt::white);
48 QString fg = info.settings()->value("foreground color", "000000");
49 fgColor = stringToColor(fg, Qt::black);
51 settings = info.settings();
53 /* Loading backdrop if available */
54 themeBase = info.settings()->value("themebase", "");
55 QString backdropFile = info.settings()->value("backdrop", "");
56 backdropFile.replace("/.rockbox/backdrops/", "");
58 if(QFile::exists(themeBase + "/backdrops/" + backdropFile))
60 backdrop = new QPixmap(themeBase + "/backdrops/" + backdropFile);
62 /* If a backdrop has been found, use its width and height */
63 if(!backdrop->isNull())
65 width = backdrop->width();
66 height = backdrop->height();
68 else
70 delete backdrop;
71 backdrop = 0;
75 fonts.insert(0, new RBFont("Nothin'"));
78 RBScreen::~RBScreen()
80 if(backdrop)
81 delete backdrop;
83 QMap<int, RBFont*>::iterator i;
84 for(i = fonts.begin(); i != fonts.end(); i++)
85 delete (*i);
87 QMap<QString, QList<RBViewport*>*>::iterator it;
88 for(it = namedViewports.begin(); it != namedViewports.end(); it++)
89 delete (*it);
92 QPainterPath RBScreen::shape() const
94 QPainterPath retval;
95 retval.addRect(0, 0, width, height);
96 return retval;
99 QRectF RBScreen::boundingRect() const
101 return QRectF(0, 0, width, height);
104 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
105 QWidget *widget)
107 if(backdrop)
109 painter->drawPixmap(0, 0, width, height, *backdrop);
111 else
113 painter->fillRect(0, 0, width, height, bgColor);
117 void RBScreen::loadViewport(QString name, RBViewport *view)
119 QList<RBViewport*>* list;
120 if(namedViewports.value(name, 0) == 0)
122 list = new QList<RBViewport*>;
123 list->append(view);
124 namedViewports.insert(name, list);
126 else
128 list = namedViewports.value(name, 0);
129 list->append(view);
133 void RBScreen::showViewport(QString name)
135 if(namedViewports.value(name, 0) == 0)
136 return;
138 QList<RBViewport*>* list = namedViewports.value(name, 0);
139 for(int i = 0; i < list->count(); i++)
140 list->at(i)->show();
143 void RBScreen::loadFont(int id, RBFont* font)
145 if(id < 2 || id > 9)
146 return;
148 fonts.insert(id, font);
151 RBFont* RBScreen::getFont(int id)
153 if(fonts.value(id, 0) != 0)
154 return fonts.value(id);
155 else
156 return fonts.value(0, 0);
160 void RBScreen::setBackdrop(QString filename)
163 if(backdrop)
164 delete backdrop;
166 filename = settings->value("imagepath", "") + "/" + filename;
168 if(QFile::exists(filename))
169 backdrop = new QPixmap(filename);
170 else
171 backdrop = 0;
174 void RBScreen::makeCustomUI(QString id)
176 if(namedViewports.value(id, 0) != 0)
178 QMap<QString, QList<RBViewport*>*>::iterator i;
179 for(i = namedViewports.begin(); i != namedViewports.end(); i++)
180 for(int j = 0; j < (*i)->count(); j++)
181 (*i)->at(j)->clearCustomUI();
182 for(int i = 0; i < namedViewports.value(id)->count(); i++)
183 namedViewports.value(id)->at(i)->makeCustomUI();
184 for(int i = 0; i < namedViewports.value(id)->count(); i++)
185 namedViewports.value(id)->at(i)->show();
189 QColor RBScreen::stringToColor(QString str, QColor fallback)
192 QColor retval;
194 if(str.length() == 6)
196 for(int i = 0; i < 6; i++)
198 char c = str[i].toAscii();
199 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
200 isdigit(c)))
202 str = "";
203 break;
206 if(str != "")
207 retval = QColor("#" + str);
208 else
209 retval = fallback;
211 else if(str.length() == 1)
213 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
215 int shade = 255 * (str[0].toAscii() - '0') / 3;
216 retval = QColor(shade, shade, shade);
218 else
220 retval = fallback;
223 else
225 retval = fallback;
228 return retval;