Robert Bieber | 8114979 | 2010-06-30 19:35:00 +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 | |
| 22 | #include "rbalbumart.h" |
| 23 | |
| 24 | #include <QPainter> |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 25 | |
Robert Bieber | 265ee15 | 2010-08-05 08:03:32 +0000 | [diff] [blame] | 26 | #include "parsetreenode.h" |
| 27 | |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 28 | RBAlbumArt::RBAlbumArt(QGraphicsItem *parent, int x, int y, int maxWidth, |
Robert Bieber | 265ee15 | 2010-08-05 08:03:32 +0000 | [diff] [blame] | 29 | int maxHeight, int artWidth, int artHeight, |
| 30 | ParseTreeNode* node, char hAlign, char vAlign) |
Robert Bieber | 10b9e3b | 2010-08-05 22:31:46 +0000 | [diff] [blame] | 31 | : RBMovable(parent),artWidth(artWidth), |
| 32 | artHeight(artHeight), hAlign(hAlign), vAlign(vAlign), |
Robert Bieber | 265ee15 | 2010-08-05 08:03:32 +0000 | [diff] [blame] | 33 | texture(":/render/albumart.png"), node(node) |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 34 | { |
Robert Bieber | 10b9e3b | 2010-08-05 22:31:46 +0000 | [diff] [blame] | 35 | size = QRectF(0, 0, maxWidth, maxHeight); |
Robert Bieber | 265ee15 | 2010-08-05 08:03:32 +0000 | [diff] [blame] | 36 | setFlag(ItemSendsGeometryChanges, false); |
| 37 | |
Robert Bieber | 83c60a1 | 2010-08-03 22:29:26 +0000 | [diff] [blame] | 38 | setPos(x, y); |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 39 | hide(); |
| 40 | } |
| 41 | |
Robert Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 42 | void 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 Bieber | 83c60a1 | 2010-08-03 22:29:26 +0000 | [diff] [blame] | 93 | |
| 94 | RBMovable::paint(painter, option, widget); |
| 95 | } |
| 96 | |
| 97 | void RBAlbumArt::saveGeometry() |
| 98 | { |
| 99 | |
Robert Bieber | 265ee15 | 2010-08-05 08:03:32 +0000 | [diff] [blame] | 100 | 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 Bieber | 8114979 | 2010-06-30 19:35:00 +0000 | [diff] [blame] | 107 | } |