/[svn]/qsampler/trunk/src/qsamplerDeviceStatusForm.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerDeviceStatusForm.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1699 - (show annotations) (download)
Sun Feb 17 10:46:17 2008 UTC (16 years, 2 months ago) by schoenebeck
File size: 5841 byte(s)
* refresh device management dialog when device informations changed,
  e.g. on changes caused by other frontends
  (fixes segfault on device changes)
* rebuild device status window on device info changes (including ones
  caused by other frontends)
* bugfix: close all device status windows when main window was closed

1 // qsamplerDeviceStatusForm.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2008, Christian Schoenebeck
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 *****************************************************************************/
21
22 #include "qsamplerAbout.h"
23 #include "qsamplerDeviceStatusForm.h"
24 #include "qsamplerMainForm.h"
25
26 #include <QGridLayout>
27
28 #define MIDI_OFF_COLOR Qt::darkGreen
29 #define MIDI_ON_COLOR Qt::green
30
31 namespace QSampler {
32
33 //-------------------------------------------------------------------------
34 // QSampler::MidiActivityLED -- Graphical indicator for MIDI activity.
35 //
36
37 MidiActivityLED::MidiActivityLED(QString text, QWidget* parent) : QLabel(text, parent) {
38 #if CONFIG_LSCP_DEVICE_MIDI
39 setPalette(MIDI_OFF_COLOR);
40 setAutoFillBackground(true);
41 #else
42 setText("X");
43 setTooltip("MIDI Activity Disabled");
44 #endif
45 timer.setSingleShot(true);
46 QObject::connect(
47 &timer, SIGNAL(timeout()),
48 this, SLOT(midiDataCeased())
49 );
50 }
51
52 void MidiActivityLED::midiDataArrived() {
53 setPalette(MIDI_ON_COLOR);
54 timer.start(50);
55 }
56
57 void MidiActivityLED::midiDataCeased() {
58 setPalette(MIDI_OFF_COLOR);
59 }
60
61 //-------------------------------------------------------------------------
62 // QSampler::DeviceStatusForm -- Device status informations window.
63 //
64
65 std::map<int, DeviceStatusForm*> DeviceStatusForm::instances;
66
67 DeviceStatusForm::DeviceStatusForm (
68 int DeviceID, QWidget* pParent, Qt::WindowFlags wflags )
69 : QMainWindow(pParent, wflags)
70 {
71 m_pDevice = new Device(Device::Midi, DeviceID);
72
73 if (!centralWidget()) setCentralWidget(new QWidget(this));
74
75 QGridLayout* pLayout = new QGridLayout(centralWidget());
76 centralWidget()->setLayout(pLayout);
77 updateGUIPorts(); // build the GUI
78
79 m_pVisibleAction = new QAction(this);
80 m_pVisibleAction->setCheckable(true);
81 m_pVisibleAction->setChecked(false);
82 m_pVisibleAction->setText(m_pDevice->deviceName());
83 m_pVisibleAction->setToolTip(
84 QString("MIDI Device ID: ") +
85 QString::number(m_pDevice->deviceID())
86 );
87 QObject::connect(
88 m_pVisibleAction, SIGNAL(toggled(bool)),
89 this, SLOT(setVisible(bool))
90 );
91
92 setWindowTitle(m_pDevice->deviceName() + " Status");
93 }
94
95 void DeviceStatusForm::updateGUIPorts() {
96 // refresh device informations
97 m_pDevice->setDevice(m_pDevice->deviceType(), m_pDevice->deviceID());
98 DevicePortList ports = m_pDevice->ports();
99
100 // clear the GUI
101 QGridLayout* pLayout = (QGridLayout*) centralWidget()->layout();
102 for (int i = pLayout->count() - 1; i >= 0; --i) {
103 QLayoutItem* pItem = pLayout->itemAt(i);
104 if (!pItem) continue;
105 pLayout->removeItem(pItem);
106 if (pItem->widget()) delete pItem->widget();
107 delete pItem;
108 }
109 midiActivityLEDs.clear();
110
111 // rebuild the GUI
112 for (int i = 0; i < ports.size(); ++i) {
113 QLabel* pLabel =
114 new QLabel(QString("MIDI port \"") + ports[i]->portName() + "\": ");
115 pLabel->setToolTip(QString("Device ID ") + QString::number(ports[i]->portID()));
116 pLayout->addWidget(pLabel, i, 0, Qt::AlignLeft);
117 MidiActivityLED* pLED = new MidiActivityLED();
118 midiActivityLEDs.push_back(pLED);
119 pLayout->addWidget(pLED, i, 1);
120 }
121 }
122
123 DeviceStatusForm::~DeviceStatusForm() {
124 if (m_pDevice) delete m_pDevice;
125 }
126
127 QAction* DeviceStatusForm::visibleAction() {
128 return m_pVisibleAction;
129 }
130
131 void DeviceStatusForm::closeEvent(QCloseEvent* event) {
132 m_pVisibleAction->setChecked(false);
133 event->accept();
134 }
135
136 void DeviceStatusForm::midiArrived(int iPort) {
137 if (uint(iPort) >= midiActivityLEDs.size()) return;
138 midiActivityLEDs[iPort]->midiDataArrived();
139 }
140
141 DeviceStatusForm* DeviceStatusForm::getInstance(int iDeviceID) {
142 std::map<int, DeviceStatusForm*>::iterator iter =
143 instances.find(iDeviceID);
144 return (iter != instances.end()) ? iter->second : NULL;
145 }
146
147 const std::map<int, DeviceStatusForm*>& DeviceStatusForm::getInstances() {
148 return instances;
149 }
150
151 void DeviceStatusForm::deleteAllInstances() {
152 for (
153 std::map<int, DeviceStatusForm*>::iterator iter = instances.begin();
154 iter != instances.end(); ++iter
155 ) {
156 iter->second->hide();
157 delete iter->second;
158 }
159 instances.clear();
160 }
161
162 void DeviceStatusForm::onDevicesChanged() {
163 MainForm* pMainForm = MainForm::getInstance();
164 if (pMainForm && pMainForm->client()) {
165 std::set<int> deviceIDs =
166 Device::getDeviceIDs(pMainForm->client(), Device::Midi);
167 // hide and delete status forms whose device has been destroyed
168 for (
169 std::map<int, DeviceStatusForm*>::iterator iter = instances.begin();
170 iter != instances.end(); ++iter
171 ) {
172 if (deviceIDs.find(iter->first) == deviceIDs.end()) {
173 iter->second->hide();
174 delete iter->second;
175 instances.erase(iter);
176 }
177 }
178 // create status forms for new devices
179 for (
180 std::set<int>::iterator iter = deviceIDs.begin();
181 iter != deviceIDs.end(); ++iter
182 ) {
183 if (instances.find(*iter) == instances.end()) {
184 DeviceStatusForm* pStatusForm =
185 new DeviceStatusForm(*iter);
186 instances[*iter] = pStatusForm;
187 }
188 }
189 }
190 }
191
192 void DeviceStatusForm::onDeviceChanged(int iDeviceID) {
193 DeviceStatusForm* pForm = DeviceStatusForm::getInstance(iDeviceID);
194 if (!pForm) return;
195 pForm->updateGUIPorts();
196 }
197
198 } // namespace QSampler
199
200 // end of qsamplerDeviceStatusForm.cpp

  ViewVC Help
Powered by ViewVC