blob: bb9bd1236908575d3989a7959658124da5ba2e48 [file] [log] [blame]
Robert Bieber81149792010-06-30 19:35:00 +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
22#include "rbalbumart.h"
23
24#include <QPainter>
Robert Bieber81149792010-06-30 19:35:00 +000025
Robert Bieber265ee152010-08-05 08:03:32 +000026#include "parsetreenode.h"
27
Robert Bieber81149792010-06-30 19:35:00 +000028RBAlbumArt::RBAlbumArt(QGraphicsItem *parent, int x, int y, int maxWidth,
Robert Bieber265ee152010-08-05 08:03:32 +000029 int maxHeight, int artWidth, int artHeight,
30 ParseTreeNode* node, char hAlign, char vAlign)
Robert Bieber10b9e3b2010-08-05 22:31:46 +000031 : RBMovable(parent),artWidth(artWidth),
32 artHeight(artHeight), hAlign(hAlign), vAlign(vAlign),
Robert Bieber265ee152010-08-05 08:03:32 +000033 texture(":/render/albumart.png"), node(node)
Robert Bieber81149792010-06-30 19:35:00 +000034{
Robert Bieber10b9e3b2010-08-05 22:31:46 +000035 size = QRectF(0, 0, maxWidth, maxHeight);
Robert Bieber265ee152010-08-05 08:03:32 +000036 setFlag(ItemSendsGeometryChanges, false);
37
Robert Bieber83c60a12010-08-03 22:29:26 +000038 setPos(x, y);
Robert Bieber81149792010-06-30 19:35:00 +000039 hide();
40}
41
Robert Bieber81149792010-06-30 19:35:00 +000042void RBAlbumArt::paint(QPainter *painter,
43 const QStyleOptionGraphicsItem *option, QWidget *widget)
44{
45 QRectF drawArea;
46
47 /* Making sure the alignment flags are sane */
48 if(hAlign != 'c' && hAlign != 'l' && hAlign != 'r')
49 hAlign = 'c';
50 if(vAlign != 'c' && vAlign != 't' && vAlign != 'b')
51 vAlign = 'c';
52
53 if(artWidth <= size.width() && artHeight <= size.height())
54 {
55 /* If the art is smaller than the viewport, just center it up */
56 drawArea.setX((size.width() - artWidth) / 2);
57 drawArea.setY((size.height() - artHeight) / 2);
58 drawArea.setWidth(artWidth);
59 drawArea.setHeight(artHeight);
60 }
61 else
62 {
63 /* Otherwise, figure out our scale factor, and which dimension needs
64 * to be scaled, and how to align said dimension
65 */
66 double xScale = size.width() / artWidth;
67 double yScale = size.height() / artHeight;
68 double scale = xScale < yScale ? xScale : yScale;
69
70 double scaleWidth = artWidth * scale;
71 double scaleHeight = artHeight * scale;
72
73 if(hAlign == 'l')
74 drawArea.setX(0);
75 else if(hAlign == 'c')
76 drawArea.setX((size.width() - scaleWidth) / 2 );
77 else
78 drawArea.setX(size.width() - scaleWidth);
79
80 if(vAlign == 't')
81 drawArea.setY(0);
82 else if(vAlign == 'c')
83 drawArea.setY((size.height() - scaleHeight) / 2);
84 else
85 drawArea.setY(size.height() - scaleHeight);
86
87 drawArea.setWidth(scaleWidth);
88 drawArea.setHeight(scaleHeight);
89
90 }
91
92 painter->fillRect(drawArea, texture);
Robert Bieber83c60a12010-08-03 22:29:26 +000093
94 RBMovable::paint(painter, option, widget);
95}
96
97void RBAlbumArt::saveGeometry()
98{
99
Robert Bieber265ee152010-08-05 08:03:32 +0000100 QPointF origin = pos();
101 QRectF bounds = boundingRect();
102
103 node->modParam(static_cast<int>(origin.x()), 0);
104 node->modParam(static_cast<int>(origin.y()), 1);
105 node->modParam(static_cast<int>(bounds.width()), 2);
106 node->modParam(static_cast<int>(bounds.height()), 3);
Robert Bieber81149792010-06-30 19:35:00 +0000107}