1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 "rbalbumart.h"
26 #include "parsetreenode.h"
28 RBAlbumArt::RBAlbumArt(QGraphicsItem
*parent
, int x
, int y
, int maxWidth
,
29 int maxHeight
, int artWidth
, int artHeight
,
30 ParseTreeNode
* node
, char hAlign
, char vAlign
)
31 : RBMovable(parent
),artWidth(artWidth
),
32 artHeight(artHeight
), hAlign(hAlign
), vAlign(vAlign
),
33 texture(":/render/albumart.png"), node(node
)
35 size
= QRectF(0, 0, maxWidth
, maxHeight
);
36 setFlag(ItemSendsGeometryChanges
, false);
42 void RBAlbumArt::paint(QPainter
*painter
,
43 const QStyleOptionGraphicsItem
*option
, QWidget
*widget
)
47 /* Making sure the alignment flags are sane */
48 if(hAlign
!= 'c' && hAlign
!= 'l' && hAlign
!= 'r')
50 if(vAlign
!= 'c' && vAlign
!= 't' && vAlign
!= 'b')
53 if(artWidth
<= size
.width() && artHeight
<= size
.height())
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
);
63 /* Otherwise, figure out our scale factor, and which dimension needs
64 * to be scaled, and how to align said dimension
66 double xScale
= size
.width() / artWidth
;
67 double yScale
= size
.height() / artHeight
;
68 double scale
= xScale
< yScale
? xScale
: yScale
;
70 double scaleWidth
= artWidth
* scale
;
71 double scaleHeight
= artHeight
* scale
;
75 else if(hAlign
== 'c')
76 drawArea
.setX((size
.width() - scaleWidth
) / 2 );
78 drawArea
.setX(size
.width() - scaleWidth
);
82 else if(vAlign
== 'c')
83 drawArea
.setY((size
.height() - scaleHeight
) / 2);
85 drawArea
.setY(size
.height() - scaleHeight
);
87 drawArea
.setWidth(scaleWidth
);
88 drawArea
.setHeight(scaleHeight
);
92 painter
->fillRect(drawArea
, texture
);
94 RBMovable::paint(painter
, option
, widget
);
97 void RBAlbumArt::saveGeometry()
100 QPointF origin
= pos();
101 QRectF bounds
= boundingRect();
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);