Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Bieber | df1ff11 | 2010-08-06 20:53:50 +0000 | [diff] [blame] | 22 | #include "rbscene.h" |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 23 | #include "rbscreen.h" |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 24 | #include "rbviewport.h" |
Robert Bieber | c32728c | 2010-06-26 05:18:21 +0000 | [diff] [blame] | 25 | #include "devicestate.h" |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 26 | |
| 27 | #include <QPainter> |
| 28 | #include <QFile> |
Robert Bieber | df1ff11 | 2010-08-06 20:53:50 +0000 | [diff] [blame] | 29 | #include <QGraphicsSceneHoverEvent> |
| 30 | #include <QGraphicsSceneMouseEvent> |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 31 | |
Robert Bieber | 5300c70 | 2010-06-26 05:51:07 +0000 | [diff] [blame] | 32 | RBScreen::RBScreen(const RBRenderInfo& info, bool remote, |
| 33 | QGraphicsItem *parent) |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 34 | :QGraphicsItem(parent), backdrop(0), project(project), |
Robert Bieber | 1ee2cdd | 2010-08-12 23:05:53 +0000 | [diff] [blame] | 35 | albumArt(0), customUI(0), defaultView(0), ax(false) |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 36 | { |
| 37 | |
Robert Bieber | df1ff11 | 2010-08-06 20:53:50 +0000 | [diff] [blame] | 38 | setAcceptHoverEvents(true); |
| 39 | |
Robert Bieber | 5300c70 | 2010-06-26 05:51:07 +0000 | [diff] [blame] | 40 | if(remote) |
| 41 | { |
Robert Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 42 | fullWidth = info.device()->data("remotewidth").toInt(); |
| 43 | fullHeight = info.device()->data("remoteheight").toInt(); |
Robert Bieber | 5300c70 | 2010-06-26 05:51:07 +0000 | [diff] [blame] | 44 | } |
| 45 | else |
| 46 | { |
Robert Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 47 | fullWidth = info.device()->data("screenwidth").toInt(); |
| 48 | fullHeight = info.device()->data("screenheight").toInt(); |
Robert Bieber | 5300c70 | 2010-06-26 05:51:07 +0000 | [diff] [blame] | 49 | } |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 50 | |
Robert Bieber | 273b9d6 | 2010-06-25 05:14:13 +0000 | [diff] [blame] | 51 | QString bg = info.settings()->value("background color", "FFFFFF"); |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 52 | bgColor = stringToColor(bg, Qt::white); |
| 53 | |
Robert Bieber | 273b9d6 | 2010-06-25 05:14:13 +0000 | [diff] [blame] | 54 | QString fg = info.settings()->value("foreground color", "000000"); |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 55 | fgColor = stringToColor(fg, Qt::black); |
| 56 | |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 57 | settings = info.settings(); |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 58 | |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 59 | /* 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 Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 65 | { |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 66 | backdrop = new QPixmap(themeBase + "/backdrops/" + backdropFile); |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 67 | |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 68 | /* If a backdrop has been found, use its width and height */ |
| 69 | if(!backdrop->isNull()) |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 70 | { |
Robert Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 71 | fullWidth = backdrop->width(); |
| 72 | fullHeight = backdrop->height(); |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 73 | } |
| 74 | else |
| 75 | { |
| 76 | delete backdrop; |
| 77 | backdrop = 0; |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
Robert Bieber | 273b9d6 | 2010-06-25 05:14:13 +0000 | [diff] [blame] | 80 | |
Robert Bieber | 120f3e1 | 2010-07-06 19:32:12 +0000 | [diff] [blame] | 81 | 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 Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 88 | |
| 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 Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | RBScreen::~RBScreen() |
| 102 | { |
| 103 | if(backdrop) |
| 104 | delete backdrop; |
Robert Bieber | 6d61566 | 2010-06-23 20:18:31 +0000 | [diff] [blame] | 105 | |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 106 | if(albumArt) |
| 107 | delete albumArt; |
| 108 | |
Robert Bieber | 6d61566 | 2010-06-23 20:18:31 +0000 | [diff] [blame] | 109 | QMap<int, RBFont*>::iterator i; |
| 110 | for(i = fonts.begin(); i != fonts.end(); i++) |
Robert Bieber | 481bcf2 | 2010-06-24 18:55:04 +0000 | [diff] [blame] | 111 | delete (*i); |
| 112 | |
| 113 | QMap<QString, QList<RBViewport*>*>::iterator it; |
| 114 | for(it = namedViewports.begin(); it != namedViewports.end(); it++) |
| 115 | delete (*it); |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | QPainterPath RBScreen::shape() const |
| 119 | { |
| 120 | QPainterPath retval; |
| 121 | retval.addRect(0, 0, width, height); |
| 122 | return retval; |
| 123 | } |
| 124 | |
| 125 | QRectF RBScreen::boundingRect() const |
| 126 | { |
| 127 | return QRectF(0, 0, width, height); |
| 128 | } |
| 129 | |
| 130 | void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, |
| 131 | QWidget *widget) |
| 132 | { |
Robert Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 133 | if(parentItem() != 0) |
| 134 | return; |
| 135 | |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 136 | 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 Bieber | 35150b6 | 2010-06-30 21:28:11 +0000 | [diff] [blame] | 144 | |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Robert Bieber | 481bcf2 | 2010-06-24 18:55:04 +0000 | [diff] [blame] | 147 | void 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 Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 163 | void RBScreen::showViewport(QString name) |
| 164 | { |
| 165 | if(namedViewports.value(name, 0) == 0) |
Robert Bieber | 1fbea62 | 2010-06-29 21:09:29 +0000 | [diff] [blame] | 166 | { |
| 167 | displayedViewports.append(name); |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 168 | return; |
Robert Bieber | 1fbea62 | 2010-06-29 21:09:29 +0000 | [diff] [blame] | 169 | } |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 170 | |
Robert Bieber | 481bcf2 | 2010-06-24 18:55:04 +0000 | [diff] [blame] | 171 | QList<RBViewport*>* list = namedViewports.value(name, 0); |
| 172 | for(int i = 0; i < list->count(); i++) |
| 173 | list->at(i)->show(); |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Robert Bieber | 6d61566 | 2010-06-23 20:18:31 +0000 | [diff] [blame] | 176 | void RBScreen::loadFont(int id, RBFont* font) |
| 177 | { |
| 178 | if(id < 2 || id > 9) |
| 179 | return; |
| 180 | |
| 181 | fonts.insert(id, font); |
| 182 | } |
| 183 | |
| 184 | RBFont* 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 Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 193 | void 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 Bieber | dfc109a | 2010-08-13 19:14:54 +0000 | [diff] [blame] | 204 | { |
| 205 | RBScene* s = dynamic_cast<RBScene*>(scene()); |
| 206 | s->addWarning(QObject::tr("Image not found: ") + filename); |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 207 | backdrop = 0; |
Robert Bieber | dfc109a | 2010-08-13 19:14:54 +0000 | [diff] [blame] | 208 | } |
Robert Bieber | ac9287b | 2010-06-22 07:55:50 +0000 | [diff] [blame] | 209 | } |
Robert Bieber | e1d8a3d | 2010-06-21 20:11:58 +0000 | [diff] [blame] | 210 | |
Robert Bieber | deab1bb | 2010-06-23 19:41:30 +0000 | [diff] [blame] | 211 | void RBScreen::makeCustomUI(QString id) |
| 212 | { |
| 213 | if(namedViewports.value(id, 0) != 0) |
| 214 | { |
Robert Bieber | 481bcf2 | 2010-06-24 18:55:04 +0000 | [diff] [blame] | 215 | QMap<QString, QList<RBViewport*>*>::iterator i; |
Robert Bieber | deab1bb | 2010-06-23 19:41:30 +0000 | [diff] [blame] | 216 | for(i = namedViewports.begin(); i != namedViewports.end(); i++) |
Robert Bieber | 481bcf2 | 2010-06-24 18:55:04 +0000 | [diff] [blame] | 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(); |
Robert Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 223 | |
| 224 | customUI = namedViewports.value(id)->at(0); |
Robert Bieber | deab1bb | 2010-06-23 19:41:30 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Robert Bieber | d1a392a | 2010-08-09 19:37:23 +0000 | [diff] [blame] | 228 | void 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 Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 244 | void RBScreen::breakSBS() |
| 245 | { |
Robert Bieber | d1a392a | 2010-08-09 19:37:23 +0000 | [diff] [blame] | 246 | for(QList<QGraphicsItem*>::iterator i = sbsChildren.begin() |
| 247 | ; i != sbsChildren.end(); i++) |
| 248 | (*i)->hide(); |
| 249 | if(defaultView) |
| 250 | defaultView->makeFullScreen(); |
Robert Bieber | 2f68e5b | 2010-07-03 22:57:42 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Robert Bieber | 02c3774 | 2010-06-17 20:44:11 +0000 | [diff] [blame] | 253 | QColor 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 Bieber | df1ff11 | 2010-08-06 20:53:50 +0000 | [diff] [blame] | 295 | |
| 296 | void 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 | } |