comparison widgets/LevelPanToolButton.cpp @ 944:78c152e4db95

Merge from branch tonioni
author Chris Cannam
date Mon, 20 Apr 2015 09:12:17 +0100
parents fcbfc7fad1f2
children 6a6a63506e3f
comparison
equal deleted inserted replaced
896:78e041e45ff0 944:78c152e4db95
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #include "LevelPanToolButton.h"
16 #include "LevelPanWidget.h"
17
18 #include <QMenu>
19 #include <QWidgetAction>
20 #include <QImage>
21 #include <QStylePainter>
22 #include <QStyleOptionToolButton>
23
24 #include <iostream>
25 using std::cerr;
26 using std::endl;
27
28 LevelPanToolButton::LevelPanToolButton(QWidget *parent) :
29 QToolButton(parent),
30 m_pixels(32),
31 m_pixelsBig(32 * 3),
32 m_muted(false),
33 m_savedLevel(1.f)
34 {
35 m_lpw = new LevelPanWidget();
36
37 connect(m_lpw, SIGNAL(levelChanged(float)), this, SIGNAL(levelChanged(float)));
38 connect(m_lpw, SIGNAL(levelChanged(float)), this, SLOT(selfLevelChanged(float)));
39
40 connect(m_lpw, SIGNAL(panChanged(float)), this, SIGNAL(panChanged(float)));
41 connect(m_lpw, SIGNAL(panChanged(float)), this, SLOT(update()));
42
43 connect(this, SIGNAL(clicked(bool)), this, SLOT(selfClicked()));
44
45 QMenu *menu = new QMenu();
46 QWidgetAction *wa = new QWidgetAction(menu);
47 wa->setDefaultWidget(m_lpw);
48 menu->addAction(wa);
49
50 setPopupMode(InstantPopup);
51 setMenu(menu);
52
53 setImageSize(m_pixels);
54 setBigImageSize(m_pixelsBig);
55 }
56
57 LevelPanToolButton::~LevelPanToolButton()
58 {
59 }
60
61 float
62 LevelPanToolButton::getLevel() const
63 {
64 return m_lpw->getLevel();
65 }
66
67 float
68 LevelPanToolButton::getPan() const
69 {
70 return m_lpw->getPan();
71 }
72
73 bool
74 LevelPanToolButton::includesMute() const
75 {
76 return m_lpw->includesMute();
77 }
78
79 void
80 LevelPanToolButton::setImageSize(int pixels)
81 {
82 m_pixels = pixels;
83
84 QPixmap px(m_pixels, m_pixels);
85 px.fill(Qt::transparent);
86 setIcon(px);
87 }
88
89 void
90 LevelPanToolButton::setBigImageSize(int pixels)
91 {
92 m_pixelsBig = pixels;
93
94 m_lpw->setFixedWidth(m_pixelsBig);
95 m_lpw->setFixedHeight(m_pixelsBig);
96 }
97
98 void
99 LevelPanToolButton::setLevel(float level)
100 {
101 m_lpw->setLevel(level);
102 update();
103 }
104
105 void
106 LevelPanToolButton::setPan(float pan)
107 {
108 m_lpw->setPan(pan);
109 update();
110 }
111
112 void
113 LevelPanToolButton::setIncludeMute(bool include)
114 {
115 m_lpw->setIncludeMute(include);
116 update();
117 }
118
119 void
120 LevelPanToolButton::setEnabled(bool enabled)
121 {
122 m_lpw->setEnabled(enabled);
123 QToolButton::setEnabled(enabled);
124 }
125
126 void
127 LevelPanToolButton::selfLevelChanged(float level)
128 {
129 if (level > 0.f) {
130 m_muted = false;
131 } else {
132 m_muted = true;
133 m_savedLevel = 1.f;
134 }
135 update();
136 }
137
138 void
139 LevelPanToolButton::selfClicked()
140 {
141 cerr << "selfClicked" << endl;
142
143 if (m_muted) {
144 m_muted = false;
145 m_lpw->setLevel(m_savedLevel);
146 emit levelChanged(m_savedLevel);
147 } else {
148 m_savedLevel = m_lpw->getLevel();
149 m_muted = true;
150 m_lpw->setLevel(0.f);
151 emit levelChanged(0.f);
152 }
153 update();
154 }
155
156 void
157 LevelPanToolButton::paintEvent(QPaintEvent *)
158 {
159 QStylePainter p(this);
160 QStyleOptionToolButton opt;
161 initStyleOption(&opt);
162 opt.features &= (~QStyleOptionToolButton::HasMenu);
163 p.drawComplexControl(QStyle::CC_ToolButton, opt);
164
165 if (m_pixels >= height()) {
166 setImageSize(height()-1);
167 }
168
169 double margin = (double(height()) - m_pixels) / 2.0;
170 m_lpw->renderTo(this, QRectF(margin, margin, m_pixels, m_pixels), false);
171 }
172
173