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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2038 - (show annotations) (download)
Thu Jan 7 18:42:26 2010 UTC (8 years, 1 month ago) by capela
File size: 6874 byte(s)
* MIDI Device Status menu is disabled when no MIDI device exists;
  a menu separator has been added.

* Window manager's close button was found missing from the Devices
  and Instruments widgets when on Qt >= 4.5, now fixed.

1 // qsamplerDeviceStatusForm.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2008, Christian Schoenebeck
5 Copyright (C) 2010, rncbc aka Rui Nuno Capela. All rights reserved.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 *****************************************************************************/
22
23 #include "qsamplerAbout.h"
24 #include "qsamplerDeviceStatusForm.h"
25
26 #include "qsamplerMainForm.h"
27
28 #include <QGridLayout>
29
30
31 namespace QSampler {
32
33 //-------------------------------------------------------------------------
34 // QSampler::MidiActivityLED -- Graphical indicator for MIDI activity.
35 //
36
37 // MIDI activity pixmap common resources.
38 int MidiActivityLED::g_iMidiActivityRefCount = 0;
39 QPixmap *MidiActivityLED::g_pMidiActivityLedOn = NULL;
40 QPixmap *MidiActivityLED::g_pMidiActivityLedOff = NULL;
41
42
43 MidiActivityLED::MidiActivityLED ( QString sText, QWidget *pParent )
44 : QLabel(sText, pParent)
45 {
46 if (++g_iMidiActivityRefCount == 1) {
47 g_pMidiActivityLedOn = new QPixmap(":/icons/ledon1.png");
48 g_pMidiActivityLedOff = new QPixmap(":/icons/ledoff1.png");
49 }
50
51 setPixmap(*g_pMidiActivityLedOff);
52 #ifndef CONFIG_EVENT_DEVICE_MIDI
53 setToolTip("MIDI Activity disabled");
54 #endif
55 m_timer.setSingleShot(true);
56
57 QObject::connect(&m_timer,
58 SIGNAL(timeout()),
59 SLOT(midiActivityLedOff())
60 );
61 }
62
63 MidiActivityLED::~MidiActivityLED (void)
64 {
65 if (--g_iMidiActivityRefCount == 0) {
66 if (g_pMidiActivityLedOn)
67 delete g_pMidiActivityLedOn;
68 g_pMidiActivityLedOn = NULL;
69 if (g_pMidiActivityLedOff)
70 delete g_pMidiActivityLedOff;
71 g_pMidiActivityLedOff = NULL;
72 }
73 }
74
75
76 void MidiActivityLED::midiActivityLedOn (void)
77 {
78 setPixmap(*g_pMidiActivityLedOn);
79 m_timer.start(100);
80 }
81
82
83 void MidiActivityLED::midiActivityLedOff (void)
84 {
85 setPixmap(*g_pMidiActivityLedOff);
86 }
87
88
89 //-------------------------------------------------------------------------
90 // QSampler::DeviceStatusForm -- Device status informations window.
91 //
92
93 std::map<int, DeviceStatusForm *> DeviceStatusForm::g_instances;
94
95
96 DeviceStatusForm::DeviceStatusForm (
97 int DeviceID, QWidget *pParent, Qt::WindowFlags wflags )
98 : QWidget(pParent, wflags)
99 {
100 m_pDevice = new Device(Device::Midi, DeviceID);
101
102 setLayout(new QGridLayout(/*this*/));
103 updateGUIPorts(); // build the GUI
104
105 m_pVisibleAction = new QAction(this);
106 m_pVisibleAction->setCheckable(true);
107 m_pVisibleAction->setChecked(false);
108 m_pVisibleAction->setText(m_pDevice->deviceName());
109 m_pVisibleAction->setToolTip(
110 QString("MIDI Device ID: ") +
111 QString::number(m_pDevice->deviceID())
112 );
113
114 QObject::connect(m_pVisibleAction,
115 SIGNAL(toggled(bool)),
116 SLOT(setVisible(bool))
117 );
118
119 setWindowTitle(tr("%1 Status").arg(m_pDevice->deviceName()));
120 }
121
122
123 void DeviceStatusForm::updateGUIPorts (void)
124 {
125 // refresh device informations
126 m_pDevice->setDevice(m_pDevice->deviceType(), m_pDevice->deviceID());
127 DevicePortList ports = m_pDevice->ports();
128
129 // clear the GUI
130 QGridLayout *pLayout = static_cast<QGridLayout *> (layout());
131 for (int i = pLayout->count() - 1; i >= 0; --i) {
132 QLayoutItem *pItem = pLayout->itemAt(i);
133 if (pItem) {
134 pLayout->removeItem(pItem);
135 if (pItem->widget())
136 delete pItem->widget();
137 delete pItem;
138 }
139 }
140
141 m_midiActivityLEDs.clear();
142
143 // rebuild the GUI
144 for (int i = 0; i < ports.size(); ++i) {
145 QLabel *pLabel
146 = new QLabel(tr("MIDI port %1").arg(ports[i]->portName()));
147 pLabel->setToolTip(tr("Device ID %1").arg(ports[i]->portID()));
148 pLayout->addWidget(pLabel, i, 0, Qt::AlignLeft);
149 MidiActivityLED *pLED = new MidiActivityLED();
150 m_midiActivityLEDs.push_back(pLED);
151 pLayout->addWidget(pLED, i, 1);
152 }
153 }
154
155
156 DeviceStatusForm::~DeviceStatusForm (void)
157 {
158 if (m_pDevice) delete m_pDevice;
159 }
160
161
162 QAction* DeviceStatusForm::visibleAction (void)
163 {
164 return m_pVisibleAction;
165 }
166
167 void DeviceStatusForm::closeEvent ( QCloseEvent *pCloseEvent )
168 {
169 m_pVisibleAction->setChecked(false);
170
171 pCloseEvent->accept();
172 }
173
174
175 void DeviceStatusForm::midiArrived ( int iPort )
176 {
177 if (uint(iPort) >= m_midiActivityLEDs.size())
178 return;
179
180 m_midiActivityLEDs[iPort]->midiActivityLedOn();
181 }
182
183
184 DeviceStatusForm *DeviceStatusForm::getInstance ( int iDeviceID )
185 {
186 std::map<int, DeviceStatusForm *>::iterator iter
187 = g_instances.find(iDeviceID);
188 return ((iter != g_instances.end()) ? iter->second : NULL);
189 }
190
191
192 const std::map<int, DeviceStatusForm *>& DeviceStatusForm::getInstances (void)
193 {
194 return g_instances;
195 }
196
197 void DeviceStatusForm::deleteAllInstances (void)
198 {
199 std::map<int, DeviceStatusForm *>::iterator iter = g_instances.begin();
200 for ( ; iter != g_instances.end(); ++iter) {
201 iter->second->hide();
202 delete iter->second;
203 }
204
205 g_instances.clear();
206 }
207
208
209 void DeviceStatusForm::onDevicesChanged (void)
210 {
211 MainForm* pMainForm = MainForm::getInstance();
212 if (pMainForm && pMainForm->client()) {
213 std::set<int> deviceIDs
214 = Device::getDeviceIDs(pMainForm->client(), Device::Midi);
215 // hide and delete status forms whose device has been destroyed
216 std::map<int, DeviceStatusForm *>::iterator iter = g_instances.begin();
217 for ( ; iter != g_instances.end(); ++iter) {
218 if (deviceIDs.find(iter->first) == deviceIDs.end()) {
219 iter->second->hide();
220 delete iter->second;
221 g_instances.erase(iter);
222 }
223 }
224 // create status forms for new devices
225 std::set<int>::iterator it = deviceIDs.begin();
226 for ( ; it != deviceIDs.end(); ++iter) {
227 if (g_instances.find(*it) == g_instances.end()) {
228 // What style do we create these forms?
229 Qt::WindowFlags wflags = Qt::Window
230 | Qt::CustomizeWindowHint
231 | Qt::WindowTitleHint
232 | Qt::WindowSystemMenuHint
233 | Qt::WindowMinMaxButtonsHint
234 | Qt::WindowCloseButtonHint;
235 Options *pOptions = pMainForm->options();
236 if (pOptions && pOptions->bKeepOnTop)
237 wflags |= Qt::Tool;
238 // Create the form, giving it the device id.
239 DeviceStatusForm *pStatusForm
240 = new DeviceStatusForm(*it, NULL, wflags);
241 g_instances[*it] = pStatusForm;
242 }
243 }
244 }
245 }
246
247
248 void DeviceStatusForm::onDeviceChanged ( int iDeviceID )
249 {
250 DeviceStatusForm *pStatusForm
251 = DeviceStatusForm::getInstance(iDeviceID);
252 if (pStatusForm)
253 pStatusForm->updateGUIPorts();
254 }
255
256
257 } // namespace QSampler
258
259 // end of qsamplerDeviceStatusForm.cpp

  ViewVC Help
Powered by ViewVC