1 /*
2 Copyright (C) 2005-2008 Remon Sijrier
3
4 This file is part of Traverso
5
6 Traverso is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 */
21
22 #include "BusMonitor.h"
23
24 #include "VUMeter.h"
25
26 #include <ProjectManager.h>
27 #include <Project.h>
28 #include <Themer.h>
29 #include <AudioDevice.h>
30 #include <AudioBus.h>
31 #include <QHBoxLayout>
32 #include <QMenu>
33 #include <QKeyEvent>
34 #include <QMouseEvent>
35
36 // Always put me below _all_ includes, this is needed
37 // in case we run with memory leak detection enabled!
38 #include "Debugger.h"
39
40
BusMonitor(QWidget * parent)41 BusMonitor::BusMonitor(QWidget* parent)
42 : QWidget( parent)
43 {
44 PENTERCONS;
45
46 setAutoFillBackground(false);
47
48 create_vu_meters();
49
50 m_menu = 0;
51
52 connect(&audiodevice(), SIGNAL(driverParamsChanged()), this, SLOT(create_vu_meters()));
53 connect(&pm(), SIGNAL(projectLoaded(Project*)), this, SLOT(set_project(Project*)));
54 }
55
56
~BusMonitor()57 BusMonitor::~BusMonitor()
58 {
59 PENTERDES;
60 }
61
sizeHint() const62 QSize BusMonitor::sizeHint() const
63 {
64 int width = 0;
65 foreach(QWidget* widget, outMeters) {
66 if (! widget->isHidden()) {
67 width += widget->width();
68 }
69 }
70 return QSize(width, 140);
71 }
72
minimumSizeHint() const73 QSize BusMonitor::minimumSizeHint() const
74 {
75 return QSize(50, 50);
76 }
77
create_vu_meters()78 void BusMonitor::create_vu_meters( )
79 {
80 PENTER;
81
82 QLayout* lay = layout();
83
84 if (lay) delete lay;
85
86 QHBoxLayout* layout = new QHBoxLayout(this);
87 layout->setMargin(0);
88 setLayout(layout);
89
90 while( ! inMeters.isEmpty() ) {
91 VUMeter* meter = inMeters.takeFirst();
92 layout->removeWidget( meter );
93 delete meter;
94 }
95
96 while ( ! outMeters.isEmpty() ) {
97 VUMeter* meter = outMeters.takeFirst();
98 layout->removeWidget( meter );
99 delete meter;
100 }
101
102 layout->addStretch(1);
103
104 QStringList list = audiodevice().get_capture_buses_names();
105 foreach(QString name, list)
106 {
107 AudioBus* bus = audiodevice().get_capture_bus(name.toLatin1());
108 VUMeter* meter = new VUMeter( this, bus );
109 connect(bus, SIGNAL(monitoringPeaksStarted()), meter, SLOT(peak_monitoring_started()));
110 connect(bus, SIGNAL(monitoringPeaksStopped()), meter, SLOT(peak_monitoring_stopped()));
111 layout->addWidget(meter);
112 inMeters.append(meter);
113 meter->hide();
114 }
115
116 list = audiodevice().get_playback_buses_names();
117 if (list.size())
118 {
119 VUMeter* meter = new VUMeter( this, audiodevice().get_playback_bus(list.at(0).toLatin1()) );
120 layout->addWidget(meter);
121 outMeters.append(meter);
122 }
123
124 layout->addSpacing(4);
125 }
126
set_project(Project * project)127 void BusMonitor::set_project(Project * project)
128 {
129 Q_UNUSED(project);
130
131 QStringList list = audiodevice().get_capture_buses_names();
132 foreach(QString name, list)
133 {
134 AudioBus* bus = audiodevice().get_capture_bus(name.toLatin1());
135 bus->reset_monitor_peaks();
136 }
137 }
138
keyPressEvent(QKeyEvent * event)139 void BusMonitor::keyPressEvent(QKeyEvent * event)
140 {
141 if (event->isAutoRepeat()) {
142 return;
143 } else if (event->key() == Qt::Key_R) {
144 reset_vu_meters();
145 } else {
146 QWidget::keyPressEvent(event);
147 }
148 }
149
mousePressEvent(QMouseEvent * event)150 void BusMonitor::mousePressEvent(QMouseEvent * event)
151 {
152 if (event->button() == Qt::RightButton) {
153 show_menu();
154 }
155 }
156
reset_vu_meters()157 void BusMonitor::reset_vu_meters()
158 {
159 foreach(VUMeter* meter, inMeters) {
160 meter->reset();
161 }
162 foreach(VUMeter* meter, outMeters) {
163 meter->reset();
164 }
165 }
166
show_menu()167 void BusMonitor::show_menu()
168 {
169 if (!m_menu) {
170 m_menu = new QMenu(this);
171 QAction* action = m_menu->addAction("Bus Monitor");
172 QFont font(themer()->get_font("ContextMenu:fontscale:actions"));
173 font.setBold(true);
174 action->setFont(font);
175 action->setEnabled(false);
176 m_menu->addSeparator();
177 action = m_menu->addAction("Reset VU's < R >");
178 connect(action, SIGNAL(triggered(bool)), this, SLOT(reset_vu_meters()));
179 }
180
181 m_menu->exec(QCursor::pos());
182 }
183
enterEvent(QEvent *)184 void BusMonitor::enterEvent(QEvent *)
185 {
186 setFocus();
187 }
188
189