remove set but unused variable
[maemo-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blobb01552cc44941cdc531543b57b1d0adfc5d58b70
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 "rbscene.h"
23 #include "rbscreen.h"
24 #include "rbviewport.h"
25 #include "devicestate.h"
27 #include <QPainter>
28 #include <QFile>
29 #include <QGraphicsSceneHoverEvent>
30 #include <QGraphicsSceneMouseEvent>
32 RBScreen::RBScreen(const RBRenderInfo& info, bool remote,
33 QGraphicsItem *parent)
34 :QGraphicsItem(parent), backdrop(0), project(project),
35 albumArt(0), customUI(0), defaultView(0), ax(false)
38 setAcceptHoverEvents(true);
40 if(remote)
42 fullWidth = info.device()->data("remotewidth").toInt();
43 fullHeight = info.device()->data("remoteheight").toInt();
45 else
47 fullWidth = info.device()->data("screenwidth").toInt();
48 fullHeight = info.device()->data("screenheight").toInt();
51 QString bg = info.settings()->value("background color", "FFFFFF");
52 bgColor = stringToColor(bg, Qt::white);
54 QString fg = info.settings()->value("foreground color", "000000");
55 fgColor = stringToColor(fg, Qt::black);
57 settings = info.settings();
59 /* Loading backdrop if available */
60 themeBase = info.settings()->value("themebase", "");
61 QString backdropFile = info.settings()->value("backdrop", "");
62 backdropFile.replace("/.rockbox/backdrops/", "");
64 if(QFile::exists(themeBase + "/backdrops/" + backdropFile))
66 backdrop = new QPixmap(themeBase + "/backdrops/" + backdropFile);
68 /* If a backdrop has been found, use its width and height */
69 if(!backdrop->isNull())
71 fullWidth = backdrop->width();
72 fullHeight = backdrop->height();
74 else
76 delete backdrop;
77 backdrop = 0;
81 fonts.insert(0, new RBFont("Default"));
82 QString defaultFont = settings->value("font", "");
83 if(defaultFont != "")
85 defaultFont.replace("/.rockbox", settings->value("themebase", ""));
86 fonts.insert(1, new RBFont(defaultFont));
89 if(parent == 0)
91 width = fullWidth;
92 height = fullHeight;
94 else
96 width = parent->boundingRect().width();
97 height = parent->boundingRect().height();
101 RBScreen::~RBScreen()
103 if(backdrop)
104 delete backdrop;
106 if(albumArt)
107 delete albumArt;
109 QMap<int, RBFont*>::iterator i;
110 for(i = fonts.begin(); i != fonts.end(); i++)
111 delete (*i);
113 QMap<QString, QList<RBViewport*>*>::iterator it;
114 for(it = namedViewports.begin(); it != namedViewports.end(); it++)
115 delete (*it);
118 QPainterPath RBScreen::shape() const
120 QPainterPath retval;
121 retval.addRect(0, 0, width, height);
122 return retval;
125 QRectF RBScreen::boundingRect() const
127 return QRectF(0, 0, width, height);
130 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
131 QWidget *widget)
133 if(parentItem() != 0)
134 return;
136 if(backdrop)
138 painter->drawPixmap(0, 0, width, height, *backdrop);
140 else
142 painter->fillRect(0, 0, width, height, bgColor);
147 void RBScreen::loadViewport(QString name, RBViewport *view)
149 QList<RBViewport*>* list;
150 if(namedViewports.value(name, 0) == 0)
152 list = new QList<RBViewport*>;
153 list->append(view);
154 namedViewports.insert(name, list);
156 else
158 list = namedViewports.value(name, 0);
159 list->append(view);
163 void RBScreen::showViewport(QString name)
165 if(namedViewports.value(name, 0) == 0)
167 displayedViewports.append(name);
168 return;
171 QList<RBViewport*>* list = namedViewports.value(name, 0);
172 for(int i = 0; i < list->count(); i++)
173 list->at(i)->show();
176 void RBScreen::loadFont(int id, RBFont* font)
178 if(id < 2 || id > 9)
179 return;
181 fonts.insert(id, font);
184 RBFont* RBScreen::getFont(int id)
186 if(fonts.value(id, 0) != 0)
187 return fonts.value(id);
188 else
189 return fonts.value(0, 0);
193 void RBScreen::setBackdrop(QString filename)
196 if(backdrop)
197 delete backdrop;
199 filename = settings->value("imagepath", "") + "/" + filename;
201 if(QFile::exists(filename))
202 backdrop = new QPixmap(filename);
203 else
205 RBScene* s = dynamic_cast<RBScene*>(scene());
206 s->addWarning(QObject::tr("Image not found: ") + filename);
207 backdrop = 0;
211 void RBScreen::makeCustomUI(QString id)
213 if(namedViewports.value(id, 0) != 0)
215 QMap<QString, QList<RBViewport*>*>::iterator i;
216 for(i = namedViewports.begin(); i != namedViewports.end(); i++)
217 for(int j = 0; j < (*i)->count(); j++)
218 (*i)->at(j)->clearCustomUI();
219 for(int i = 0; i < namedViewports.value(id)->count(); i++)
220 namedViewports.value(id)->at(i)->makeCustomUI();
221 for(int i = 0; i < namedViewports.value(id)->count(); i++)
222 namedViewports.value(id)->at(i)->show();
224 customUI = namedViewports.value(id)->at(0);
228 void RBScreen::endSbsRender()
230 sbsChildren = children();
232 QList<int> keys = fonts.keys();
233 for(QList<int>::iterator i = keys.begin(); i != keys.end(); i++)
235 if(*i > 2)
236 fonts.remove(*i);
239 images.clear();
240 namedViewports.clear();
241 displayedViewports.clear();
244 void RBScreen::breakSBS()
246 for(QList<QGraphicsItem*>::iterator i = sbsChildren.begin()
247 ; i != sbsChildren.end(); i++)
248 (*i)->hide();
249 if(defaultView)
250 defaultView->makeFullScreen();
253 QColor RBScreen::stringToColor(QString str, QColor fallback)
256 QColor retval;
258 if(str.length() == 6)
260 for(int i = 0; i < 6; i++)
262 char c = str[i].toAscii();
263 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
264 isdigit(c)))
266 str = "";
267 break;
270 if(str != "")
271 retval = QColor("#" + str);
272 else
273 retval = fallback;
275 else if(str.length() == 1)
277 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
279 int shade = 255 * (str[0].toAscii() - '0') / 3;
280 retval = QColor(shade, shade, shade);
282 else
284 retval = fallback;
287 else
289 retval = fallback;
292 return retval;
296 void RBScreen::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
298 RBScene* s = dynamic_cast<RBScene*>(scene());
299 QPoint p = event->scenePos().toPoint();
300 s->moveMouse("(" + QString::number(p.x()) + ", "
301 + QString::number(p.y()) + ")");
303 QGraphicsItem::hoverMoveEvent(event);