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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC