Theme Editor: Fixed some compiler warnings
[kugel-rb.git] / utils / themeeditor / graphics / rbscreen.cpp
blob8d3ef86ad0c3bd2e7b4ec4ce5f4559a76e3e9133
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"
24 #include <QPainter>
25 #include <QFile>
27 RBScreen::RBScreen(ProjectModel* project, QGraphicsItem *parent) :
28 QGraphicsItem(parent), backdrop(0), project(project)
31 width = safeSetting(project, "#screenwidth", "300").toInt();
32 height = safeSetting(project, "#screenheight", "200").toInt();
34 QString bg = safeSetting(project, "background color", "FFFFFF");
35 bgColor = stringToColor(bg, Qt::white);
37 QString fg = safeSetting(project, "foreground color", "FFFFFF");
38 fgColor = stringToColor(fg, Qt::black);
40 /* Loading backdrop if available */
41 if(project)
43 QString base = project->getSetting("themebase", "");
44 QString backdropFile = project->getSetting("backdrop", "");
46 if(QFile::exists(base + "/backdrops/" + backdropFile))
48 backdrop = new QPixmap(base + "/backdrops/" + backdropFile);
50 /* If a backdrop has been found, use its width and height */
51 if(!backdrop->isNull())
53 width = backdrop->width();
54 height = backdrop->height();
56 else
58 delete backdrop;
59 backdrop = 0;
65 RBScreen::~RBScreen()
67 if(backdrop)
68 delete backdrop;
71 QPainterPath RBScreen::shape() const
73 QPainterPath retval;
74 retval.addRect(0, 0, width, height);
75 return retval;
78 QRectF RBScreen::boundingRect() const
80 return QRectF(0, 0, width, height);
83 void RBScreen::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
84 QWidget *widget)
86 if(backdrop)
88 painter->drawPixmap(0, 0, width, height, *backdrop);
90 else
92 painter->fillRect(0, 0, width, height, bgColor);
96 QColor RBScreen::stringToColor(QString str, QColor fallback)
99 QColor retval;
101 if(str.length() == 6)
103 for(int i = 0; i < 6; i++)
105 char c = str[i].toAscii();
106 if(!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
107 isdigit(c)))
109 str = "";
110 break;
113 if(str != "")
114 retval = QColor("#" + str);
115 else
116 retval = fallback;
118 else if(str.length() == 1)
120 if(isdigit(str[0].toAscii()) && str[0].toAscii() <= '3')
122 int shade = 255 * (str[0].toAscii() - '0') / 3;
123 retval = QColor(shade, shade, shade);
125 else
127 retval = fallback;
130 else
132 retval = fallback;
135 return retval;