File: actionlabel.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (112 lines) | stat: -rw-r--r-- 3,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "actionlabel.h"
#include "icons.h"
#include <QLabel>
#include <QPixmap>
#include <QTimer>
#include <QTransform>

// Borrowed from kolourpaint...
static QTransform transformWithZeroOrigin(const QTransform& matrix, int width, int height)
{
	QRect newRect(matrix.mapRect(QRect(0, 0, width, height)));

	return QTransform(matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(),
	                  matrix.dx() - newRect.left(), matrix.dy() - newRect.top());
}

static QTransform rotateMatrix(int width, int height, double angle)
{
	QTransform matrix;
	matrix.translate(width / 2, height / 2);
	matrix.rotate(angle);

	return transformWithZeroOrigin(matrix, width, height);
}

static const int constNumIcons = 8;
static int theUsageCount;
static QPixmap* theIcons[constNumIcons];

ActionLabel::ActionLabel(QWidget* parent)
	: QLabel(parent)
{
	int iconSize = Icon::dlgIconSize();
	int labelSize = Icon::stdSize((int)((iconSize * 1.333333333) + 0.5));

	setMinimumSize(labelSize, labelSize);
	setMaximumSize(labelSize, labelSize);
	setAlignment(Qt::AlignCenter);

	if (0 == theUsageCount++) {
		QImage img(Icons::self()->audioListIcon.pixmap(iconSize, iconSize).toImage());
		double increment = 360.0 / constNumIcons;

		for (int i = 0; i < constNumIcons; ++i) {
			theIcons[i] = new QPixmap(QPixmap::fromImage(0 == i ? img
			                                                    : img.transformed(rotateMatrix(img.width(), img.height(), increment * i),
			                                                                      Qt::SmoothTransformation)));
		}
	}

	setPixmap(*theIcons[0]);
	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), SLOT(rotateIcon()));
}

ActionLabel::~ActionLabel()
{
	if (0 == --theUsageCount) {
		for (int i = 0; i < constNumIcons; ++i) {
			delete theIcons[i];
			theIcons[i] = nullptr;
		}
	}
}

void ActionLabel::startAnimation()
{
	count = 0;
	setPixmap(*theIcons[0]);
	timer->start(2000 / constNumIcons);
}

void ActionLabel::stopAnimation()
{
	timer->stop();
	count = 0;
	setPixmap(*theIcons[count]);
}

void ActionLabel::rotateIcon()
{
	if (++count == constNumIcons) {
		count = 0;
	}

	setPixmap(*theIcons[count]);
}

#include "moc_actionlabel.cpp"