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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1528 - (show annotations) (download)
Mon Nov 26 18:24:38 2007 UTC (16 years, 4 months ago) by capela
File size: 9418 byte(s)
* Qt4 migration: instrument map context menu is back in business.

1 // qsamplerInstrumentListForm.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved.
5 Copyright (C) 2007, 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
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 "qsamplerInstrumentListForm.h"
25
26 #include "qsamplerInstrumentForm.h"
27
28 #include "qsamplerOptions.h"
29 #include "qsamplerInstrument.h"
30 #include "qsamplerMainForm.h"
31
32 #include <QHeaderView>
33 #include <QMessageBox>
34
35
36 namespace QSampler {
37
38 InstrumentListForm::InstrumentListForm (
39 QWidget* pParent, Qt::WindowFlags wflags )
40 : QMainWindow(pParent, wflags)
41 {
42 m_ui.setupUi(this);
43
44 // Setup toolbar widgets.
45 m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar);
46 m_pMapComboBox->setMinimumWidth(120);
47 m_pMapComboBox->setEnabled(false);
48 m_pMapComboBox->setToolTip(tr("Instrument Map"));
49 m_ui.InstrumentToolbar->addWidget(m_pMapComboBox);
50
51 m_ui.InstrumentToolbar->addSeparator();
52 m_ui.InstrumentToolbar->addAction(m_ui.newInstrumentAction);
53 m_ui.InstrumentToolbar->addAction(m_ui.editInstrumentAction);
54 m_ui.InstrumentToolbar->addAction(m_ui.deleteInstrumentAction);
55 m_ui.InstrumentToolbar->addSeparator();
56 m_ui.InstrumentToolbar->addAction(m_ui.refreshInstrumentsAction);
57
58 int iRowHeight = m_ui.InstrumentTable->fontMetrics().height() + 4;
59 m_ui.InstrumentTable->verticalHeader()->setDefaultSectionSize(iRowHeight);
60
61 m_ui.InstrumentTable->setModel(&m_model);
62 m_ui.InstrumentTable->setItemDelegate(&m_delegate);
63 m_ui.InstrumentTable->verticalHeader()->hide();
64
65 QHeaderView *pHeader = m_ui.InstrumentTable->horizontalHeader();
66 pHeader->setDefaultAlignment(Qt::AlignLeft);
67 pHeader->setMovable(false);
68 pHeader->setStretchLastSection(true);
69 pHeader->resizeSection(0, 120); // Name
70 m_ui.InstrumentTable->resizeColumnToContents(1); // Map
71 m_ui.InstrumentTable->resizeColumnToContents(2); // Bank
72 m_ui.InstrumentTable->resizeColumnToContents(3); // Prog
73 m_ui.InstrumentTable->resizeColumnToContents(4); // Engine
74 pHeader->resizeSection(5, 240); // File
75 m_ui.InstrumentTable->resizeColumnToContents(6); // Nr
76 pHeader->resizeSection(7, 60); // Vol
77
78 // Enable custom context menu...
79 m_ui.InstrumentTable->setContextMenuPolicy(Qt::CustomContextMenu);
80
81 QObject::connect(m_pMapComboBox,
82 SIGNAL(activated(int)),
83 SLOT(activateMap(int)));
84 QObject::connect(
85 m_ui.InstrumentTable,
86 SIGNAL(customContextMenuRequested(const QPoint&)),
87 SLOT(contextMenu(const QPoint&)));
88 QObject::connect(
89 m_ui.InstrumentTable,
90 SIGNAL(pressed(const QModelIndex&)),
91 SLOT(stabilizeForm()));
92 QObject::connect(
93 m_ui.InstrumentTable,
94 SIGNAL(activated(const QModelIndex&)),
95 SLOT(editInstrument(const QModelIndex&)));
96 QObject::connect(
97 m_ui.newInstrumentAction,
98 SIGNAL(triggered()),
99 SLOT(newInstrument()));
100 QObject::connect(
101 m_ui.deleteInstrumentAction,
102 SIGNAL(triggered()),
103 SLOT(deleteInstrument()));
104 QObject::connect(
105 m_ui.editInstrumentAction,
106 SIGNAL(triggered()),
107 SLOT(editInstrument()));
108 QObject::connect(
109 m_ui.refreshInstrumentsAction,
110 SIGNAL(triggered()),
111 SLOT(refreshInstruments()));
112
113 MainForm *pMainForm = MainForm::getInstance();
114 if (pMainForm) {
115 QObject::connect(&m_model,
116 SIGNAL(instrumentsChanged()),
117 pMainForm, SLOT(sessionDirty()));
118 }
119
120 // Things must be stable from the start.
121 stabilizeForm();
122 }
123
124
125 InstrumentListForm::~InstrumentListForm (void)
126 {
127 delete m_pMapComboBox;
128 }
129
130
131 // Notify our parent that we're emerging.
132 void InstrumentListForm::showEvent ( QShowEvent *pShowEvent )
133 {
134 MainForm* pMainForm = MainForm::getInstance();
135 if (pMainForm)
136 pMainForm->stabilizeForm();
137
138 QWidget::showEvent(pShowEvent);
139 }
140
141
142 // Notify our parent that we're closing.
143 void InstrumentListForm::hideEvent ( QHideEvent *pHideEvent )
144 {
145 QWidget::hideEvent(pHideEvent);
146
147 MainForm* pMainForm = MainForm::getInstance();
148 if (pMainForm)
149 pMainForm->stabilizeForm();
150 }
151
152
153 // Just about to notify main-window that we're closing.
154 void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ )
155 {
156 QWidget::hide();
157
158 MainForm *pMainForm = MainForm::getInstance();
159 if (pMainForm)
160 pMainForm->stabilizeForm();
161 }
162
163
164 // Refresh all instrument list and views.
165 void InstrumentListForm::refreshInstruments (void)
166 {
167 MainForm* pMainForm = MainForm::getInstance();
168 if (pMainForm == NULL)
169 return;
170
171 qsamplerOptions *pOptions = pMainForm->options();
172 if (pOptions == NULL)
173 return;
174
175 // Get/save current map selection...
176 int iMap = m_pMapComboBox->currentIndex();
177 if (iMap < 0 || m_pMapComboBox->count() < 2)
178 iMap = pOptions->iMidiMap + 1;
179
180 // Populate maps list.
181 m_pMapComboBox->clear();
182 m_pMapComboBox->addItem(tr("(All)"));
183 m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames());
184
185 // Adjust to saved selection...
186 if (iMap < 0 || iMap >= m_pMapComboBox->count())
187 iMap = 0;
188 m_pMapComboBox->setCurrentIndex(iMap);
189 m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1);
190
191 activateMap(iMap);
192 }
193
194
195 // Refresh instrument maps selector.
196 void InstrumentListForm::activateMap ( int iMap )
197 {
198 MainForm* pMainForm = MainForm::getInstance();
199 if (pMainForm == NULL)
200 return;
201
202 qsamplerOptions *pOptions = pMainForm->options();
203 if (pOptions == NULL)
204 return;
205
206 int iMidiMap = iMap - 1;
207 if (iMidiMap >= 0)
208 pOptions->iMidiMap = iMidiMap;
209
210 m_model.setMidiMap(iMidiMap);
211 m_model.refresh();
212
213 stabilizeForm();
214 }
215
216
217 void InstrumentListForm::editInstrument (void)
218 {
219 editInstrument(m_ui.InstrumentTable->currentIndex());
220 }
221
222
223 void InstrumentListForm::editInstrument ( const QModelIndex& index )
224 {
225 if (!index.isValid() || !index.data(Qt::UserRole).isValid())
226 return;
227
228 qsamplerInstrument* pInstrument
229 = static_cast<qsamplerInstrument *> (
230 index.data(Qt::UserRole).value<void *> ());
231
232 if (pInstrument == NULL)
233 return;
234
235 // Save current key values...
236 qsamplerInstrument oldInstrument(*pInstrument);
237 // Do the edit dance...
238 InstrumentForm form(this);
239 form.setup(pInstrument);
240 if (form.exec()) {
241 // Commit...
242 pInstrument->mapInstrument();
243 // Check whether we changed instrument key...
244 if (oldInstrument.map() == pInstrument->map() &&
245 oldInstrument.bank() == pInstrument->bank() &&
246 oldInstrument.prog() == pInstrument->prog()) {
247 // Just update tree item...
248 //pItem->update();
249 } else {
250 // Unmap old instance...
251 oldInstrument.unmapInstrument();
252 // correct the position of the instrument in the model
253 m_model.resort(*pInstrument);
254 }
255 // Notify we've changes...
256 emit m_model.reset();
257 }
258 }
259
260
261 void InstrumentListForm::newInstrument (void)
262 {
263 qsamplerInstrument instrument;
264
265 InstrumentForm form(this);
266 form.setup(&instrument);
267 if (!form.exec())
268 return;
269
270 // Commit...
271 instrument.mapInstrument();
272 // add new item to the table model
273 m_model.resort(instrument);
274 // Notify we've changes...
275 //emit model.reset();
276 //FIXME: call above didnt really refresh, so we use this for now ...
277 refreshInstruments();
278 }
279
280
281 void InstrumentListForm::deleteInstrument (void)
282 {
283 const QModelIndex& index = m_ui.InstrumentTable->currentIndex();
284 if (!index.isValid() || !index.data(Qt::UserRole).isValid())
285 return;
286
287 qsamplerInstrument *pInstrument =
288 static_cast<qsamplerInstrument*> (
289 index.data(Qt::UserRole).value<void *> ());
290
291 if (pInstrument == NULL)
292 return;
293
294 MainForm *pMainForm = MainForm::getInstance();
295 if (pMainForm == NULL)
296 return;
297
298 // Prompt user if this is for real...
299 qsamplerOptions *pOptions = pMainForm->options();
300 if (pOptions && pOptions->bConfirmRemove) {
301 if (QMessageBox::warning(this,
302 QSAMPLER_TITLE ": " + tr("Warning"),
303 tr("About to delete instrument map entry:\n\n"
304 "%1\n\n"
305 "Are you sure?")
306 .arg(pInstrument->name()),
307 tr("OK"), tr("Cancel")) > 0)
308 return;
309 }
310
311 pInstrument->unmapInstrument();
312 // let the instrument vanish from the table model
313 m_model.removeInstrument(*pInstrument);
314 // Notify we've changes...
315 emit m_model.reset();
316 }
317
318
319 // Update form actions enablement...
320 void InstrumentListForm::stabilizeForm (void)
321 {
322 MainForm *pMainForm = MainForm::getInstance();
323
324 bool bEnabled = (pMainForm && pMainForm->client());
325 m_ui.newInstrumentAction->setEnabled(bEnabled);
326 const QModelIndex& index = m_ui.InstrumentTable->currentIndex();
327 bEnabled = (bEnabled && index.isValid());
328 m_ui.editInstrumentAction->setEnabled(bEnabled);
329 m_ui.deleteInstrumentAction->setEnabled(bEnabled);
330 }
331
332
333 // Handle custom context menu here...
334 void InstrumentListForm::contextMenu ( const QPoint& pos )
335 {
336 if (!m_ui.newInstrumentAction->isEnabled())
337 return;
338
339 m_ui.contextMenu->exec(
340 (m_ui.InstrumentTable->viewport())->mapToGlobal(pos));
341 }
342
343
344 } // namespace QSampler
345
346
347 // end of qsamplerInstrumentListForm.cpp

  ViewVC Help
Powered by ViewVC