blob: b01552cc44941cdc531543b57b1d0adfc5d58b70 [file] [log] [blame]
Robert Bieber02c37742010-06-17 20:44:11 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Robert Bieber
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
Robert Bieberdf1ff112010-08-06 20:53:50 +000022#include "rbscene.h"
Robert Bieber02c37742010-06-17 20:44:11 +000023#include "rbscreen.h"
Robert Biebere1d8a3d2010-06-21 20:11:58 +000024#include "rbviewport.h"
Robert Bieberc32728c2010-06-26 05:18:21 +000025#include "devicestate.h"
Robert Bieber02c37742010-06-17 20:44:11 +000026
27#include <QPainter>
28#include <QFile>
Robert Bieberdf1ff112010-08-06 20:53:50 +000029#include <QGraphicsSceneHoverEvent>
30#include <QGraphicsSceneMouseEvent>
Robert Bieber02c37742010-06-17 20:44:11 +000031
Robert Bieber5300c702010-06-26 05:51:07 +000032RBScreen::RBScreen(const RBRenderInfo& info, bool remote,
33 QGraphicsItem *parent)
Robert Bieber81149792010-06-30 19:35:00 +000034 :QGraphicsItem(parent), backdrop(0), project(project),
Robert Bieber1ee2cdd2010-08-12 23:05:53 +000035 albumArt(0), customUI(0), defaultView(0), ax(false)
Robert Bieber02c37742010-06-17 20:44:11 +000036{
37
Robert Bieberdf1ff112010-08-06 20:53:50 +000038 setAcceptHoverEvents(true);
39
Robert Bieber5300c702010-06-26 05:51:07 +000040 if(remote)
41 {
Robert Bieber2f68e5b2010-07-03 22:57:42 +000042 fullWidth = info.device()->data("remotewidth").toInt();
43 fullHeight = info.device()->data("remoteheight").toInt();
Robert Bieber5300c702010-06-26 05:51:07 +000044 }
45 else
46 {
Robert Bieber2f68e5b2010-07-03 22:57:42 +000047 fullWidth = info.device()->data("screenwidth").toInt();
48 fullHeight = info.device()->data("screenheight").toInt();
Robert Bieber5300c702010-06-26 05:51:07 +000049 }
Robert Bieber02c37742010-06-17 20:44:11 +000050
Robert Bieber273b9d62010-06-25 05:14:13 +000051 QString bg = info.settings()->value("background color", "FFFFFF");
Robert Bieber02c37742010-06-17 20:44:11 +000052 bgColor = stringToColor(bg, Qt::white);
53
Robert Bieber273b9d62010-06-25 05:14:13 +000054 QString fg = info.settings()->value("foreground color", "000000");
Robert Bieber02c37742010-06-17 20:44:11 +000055 fgColor = stringToColor(fg, Qt::black);
56
Robert Bieberac9287b2010-06-22 07:55:50 +000057 settings = info.settings();
Robert Biebere1d8a3d2010-06-21 20:11:58 +000058
Robert Bieberac9287b2010-06-22 07:55:50 +000059 /* Loading backdrop if available */
60 themeBase = info.settings()->value("themebase", "");
61 QString backdropFile = info.settings()->value("backdrop", "");
62 backdropFile.replace("/.rockbox/backdrops/", "");
63
64 if(QFile::exists(themeBase + "/backdrops/" + backdropFile))
Robert Bieber02c37742010-06-17 20:44:11 +000065 {
Robert Bieberac9287b2010-06-22 07:55:50 +000066 backdrop = new QPixmap(themeBase + "/backdrops/" + backdropFile);
Robert Bieber02c37742010-06-17 20:44:11 +000067
Robert Biebere1d8a3d2010-06-21 20:11:58 +000068 /* If a backdrop has been found, use its width and height */
69 if(!backdrop->isNull())
Robert Bieber02c37742010-06-17 20:44:11 +000070 {
Robert Bieber2f68e5b2010-07-03 22:57:42 +000071 fullWidth = backdrop->width();
72 fullHeight = backdrop->height();
Robert Biebere1d8a3d2010-06-21 20:11:58 +000073 }
74 else
75 {
76 delete backdrop;
77 backdrop = 0;
Robert Bieber02c37742010-06-17 20:44:11 +000078 }
79 }
Robert Bieber273b9d62010-06-25 05:14:13 +000080
Robert Bieber120f3e12010-07-06 19:32:12 +000081 fonts.insert(0, new RBFont("Default"));
82 QString defaultFont = settings->value("font", "");
83 if(defaultFont != "")
84 {
85 defaultFont.replace("/.rockbox", settings->value("themebase", ""));
86 fonts.insert(1, new RBFont(defaultFont));
87 }
Robert Bieber2f68e5b2010-07-03 22:57:42 +000088
89 if(parent == 0)
90 {
91 width = fullWidth;
92 height = fullHeight;
93 }
94 else
95 {
96 width = parent->boundingRect().width();
97 height = parent->boundingRect().height();
98 }
Robert Bieber02c37742010-06-17 20:44:11 +000099}
100
101RBScreen::~RBScreen()
102{
103 if(backdrop)
104 delete backdrop;
Robert Bieber6d615662010-06-23 20:18:31 +0000105
Robert Bieber81149792010-06-30 19:35:00 +0000106 if(albumArt)
107 delete albumArt;
108
Robert Bieber6d615662010-06-23 20:18:31 +0000109 QMap<int, RBFont*>::iterator i;
110 for(i = fonts.begin(); i != fonts.end(); i++)
Robert Bieber481bcf22010-06-24 18:55:04 +0000111 delete (*i);
112
113 QMap<QString, QList<RBViewport*>*>::iterator it;
114 for(it = namedViewports.begin(); it != namedViewports.end(); it++)
115 delete (*it);
Robert Bieber02c37742010-06-17 20:44:11 +0000116}
117
118QPainterPath RBScreen::shape() const
119{
120 QPainterPath retval;
121 retval.addRect(0, 0, width, height);
122 return retval;
123}
124
125QRectF RBScreen::boundingRect() const
126{
127 return QRectF(0, 0, width, height);
128}
129
130void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
131 QWidget *widget)
132{
Robert Bieber2f68e5b2010-07-03 22:57:42 +0000133 if(parentItem() != 0)
134 return;
135
Robert Bieber02c37742010-06-17 20:44:11 +0000136 if(backdrop)
137 {
138 painter->drawPixmap(0, 0, width, height, *backdrop);
139 }
140 else
141 {
142 painter->fillRect(0, 0, width, height, bgColor);
143 }
Robert Bieber35150b62010-06-30 21:28:11 +0000144
Robert Bieber02c37742010-06-17 20:44:11 +0000145}
146
Robert Bieber481bcf22010-06-24 18:55:04 +0000147void RBScreen::loadViewport(QString name, RBViewport *view)
148{
149 QList<RBViewport*>* list;
150 if(namedViewports.value(name, 0) == 0)
151 {
152 list = new QList<RBViewport*>;
153 list->append(view);
154 namedViewports.insert(name, list);
155 }
156 else
157 {
158 list = namedViewports.value(name, 0);
159 list->append(view);
160 }
161}
162
Robert Biebere1d8a3d2010-06-21 20:11:58 +0000163void RBScreen::showViewport(QString name)
164{
165 if(namedViewports.value(name, 0) == 0)
Robert Bieber1fbea622010-06-29 21:09:29 +0000166 {
167 displayedViewports.append(name);
Robert Biebere1d8a3d2010-06-21 20:11:58 +0000168 return;
Robert Bieber1fbea622010-06-29 21:09:29 +0000169 }
Robert Biebere1d8a3d2010-06-21 20:11:58 +0000170
Robert Bieber481bcf22010-06-24 18:55:04 +0000171 QList<RBViewport*>* list = namedViewports.value(name, 0);
172 for(int i = 0; i < list->count(); i++)
173 list->at(i)->show();
Robert Biebere1d8a3d2010-06-21 20:11:58 +0000174}
175
Robert Bieber6d615662010-06-23 20:18:31 +0000176void RBScreen::loadFont(int id, RBFont* font)
177{
178 if(id < 2 || id > 9)
179 return;
180
181 fonts.insert(id, font);
182}
183
184RBFont* RBScreen::getFont(int id)
185{
186 if(fonts.value(id, 0) != 0)
187 return fonts.value(id);
188 else
189 return fonts.value(0, 0);
190}
191
192
Robert Bieberac9287b2010-06-22 07:55:50 +0000193void RBScreen::setBackdrop(QString filename)
194{
195
196 if(backdrop)
197 delete backdrop;
198
199 filename = settings->value("imagepath", "") + "/" + filename;
200
201 if(QFile::exists(filename))
202 backdrop = new QPixmap(filename);
203 else
Robert Bieberdfc109a2010-08-13 19:14:54 +0000204 {
205 RBScene* s = dynamic_cast<RBScene*>(scene());
206 s->addWarning(QObject::tr("Image not found: ") + filename);
Robert Bieberac9287b2010-06-22 07:55:50 +0000207 backdrop = 0;
Robert Bieberdfc109a2010-08-13 19:14:54 +0000208 }
Robert Bieberac9287b2010-06-22 07:55:50 +0000209}
Robert Biebere1d8a3d2010-06-21 20:11:58 +0000210
Robert Bieberdeab1bb2010-06-23 19:41:30 +0000211void RBScreen::makeCustomUI(QString id)
212{
213 if(namedViewports.value(id, 0) != 0)
214 {
Robert Bieber481bcf22010-06-24 18:55:04 +0000215 QMap<QString, QList<RBViewport*>*>::iterator i;
Robert Bieberdeab1bb2010-06-23 19:41:30 +0000216 for(i = namedViewports.begin(); i != namedViewports.end(); i++)
Robert Bieber481bcf22010-06-24 18:55:04 +0000217 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();
Robert Bieber2f68e5b2010-07-03 22:57:42 +0000223
224 customUI = namedViewports.value(id)->at(0);
Robert Bieberdeab1bb2010-06-23 19:41:30 +0000225 }
226}
227
Robert Bieberd1a392a2010-08-09 19:37:23 +0000228void RBScreen::endSbsRender()
229{
230 sbsChildren = children();
231
232 QList<int> keys = fonts.keys();
233 for(QList<int>::iterator i = keys.begin(); i != keys.end(); i++)
234 {
235 if(*i > 2)
236 fonts.remove(*i);
237 }
238
239 images.clear();
240 namedViewports.clear();
241 displayedViewports.clear();
242}
243
Robert Bieber2f68e5b2010-07-03 22:57:42 +0000244void RBScreen::breakSBS()
245{
Robert Bieberd1a392a2010-08-09 19:37:23 +0000246 for(QList<QGraphicsItem*>::iterator i = sbsChildren.begin()
247 ; i != sbsChildren.end(); i++)
248 (*i)->hide();
249 if(defaultView)
250 defaultView->makeFullScreen();
Robert Bieber2f68e5b2010-07-03 22:57:42 +0000251}
252
Robert Bieber02c37742010-06-17 20:44:11 +0000253QColor RBScreen::stringToColor(QString str, QColor fallback)
254{
255
256 QColor retval;
257
258 if(str.length() == 6)
259 {
260 for(int i = 0; i < 6; i++)
261 {
262 char c = str[i].toAscii();
263 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
264 isdigit(c)))
265 {
266 str = "";
267 break;
268 }
269 }
270 if(str != "")
271 retval = QColor("#" + str);
272 else
273 retval = fallback;
274 }
275 else if(str.length() == 1)
276 {
277 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
278 {
279 int shade = 255 * (str[0].toAscii() - '0') / 3;
280 retval = QColor(shade, shade, shade);
281 }
282 else
283 {
284 retval = fallback;
285 }
286 }
287 else
288 {
289 retval = fallback;
290 }
291
292 return retval;
293
294}
Robert Bieberdf1ff112010-08-06 20:53:50 +0000295
296void RBScreen::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
297{
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()) + ")");
302
303 QGraphicsItem::hoverMoveEvent(event);
304}