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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3855 - (show annotations) (download)
Thu Feb 4 10:09:42 2021 UTC (3 years, 1 month ago) by capela
File size: 19306 byte(s)
- Silence compiler warnings to GNU/MinGW only.
1 // qsamplerOptions.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2021, rncbc aka Rui Nuno Capela. All rights reserved.
5 Copyright (C) 2007,2008,2015 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 "qsamplerOptions.h"
25 #include "qsamplerMainForm.h"
26
27 #include <QTextStream>
28 #include <QComboBox>
29
30 #include <QApplication>
31
32 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
33 #include <QDesktopWidget>
34 #endif
35
36 #include <lscp/client.h>
37
38 #ifdef CONFIG_LIBGIG
39 #if defined(Q_CC_GNU) || defined(Q_CC_MINGW)
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wunused-parameter"
42 #endif
43 #include <gig.h>
44 #if defined(Q_CC_GNU) || defined(Q_CC_MINGW)
45 #pragma GCC diagnostic pop
46 #endif
47 #endif
48
49
50 namespace QSampler {
51
52 //-------------------------------------------------------------------------
53 // QSampler::Options - Prototype settings structure.
54 //
55
56 // Constructor.
57 Options::Options (void)
58 : m_settings(QSAMPLER_DOMAIN, QSAMPLER_TITLE)
59 {
60 loadOptions();
61 }
62
63
64 // Default Destructor.
65 Options::~Options (void)
66 {
67 saveOptions();
68 }
69
70
71 // Explicit load method.
72 void Options::loadOptions (void)
73 {
74 // Begin into general options group.
75 m_settings.beginGroup("/Options");
76
77 // Load server options...
78 m_settings.beginGroup("/Server");
79 sServerHost = m_settings.value("/ServerHost", "localhost").toString();
80 iServerPort = m_settings.value("/ServerPort", 8888).toInt();
81 #if defined(__APPLE__) // Toshi Nagata 20080105
82 // TODO: Should this be a configure option?
83 iServerTimeout = m_settings.value("/ServerTimeout", 10000).toInt();
84 #else
85 iServerTimeout = m_settings.value("/ServerTimeout", 1000).toInt();
86 #endif
87 bServerStart = m_settings.value("/ServerStart", true).toBool();
88 #if defined(__APPLE__)
89 sServerCmdLine = m_settings.value("/ServerCmdLine", "/usr/local/bin/linuxsampler").toString();
90 #else
91 sServerCmdLine = m_settings.value("/ServerCmdLine", "linuxsampler").toString();
92 #endif
93 iStartDelay = m_settings.value("/StartDelay", 3).toInt();
94 m_settings.endGroup();
95
96 // Load logging options...
97 m_settings.beginGroup("/Logging");
98 bMessagesLog = m_settings.value("/MessagesLog", false).toBool();
99 sMessagesLogPath = m_settings.value("/MessagesLogPath", "qsampler.log").toString();
100 m_settings.endGroup();
101
102 // Load display options...
103 m_settings.beginGroup("/Display");
104 sDisplayFont = m_settings.value("/DisplayFont").toString();
105 bDisplayEffect = m_settings.value("/DisplayEffect", true).toBool();
106 bAutoRefresh = m_settings.value("/AutoRefresh", true).toBool();
107 iAutoRefreshTime = m_settings.value("/AutoRefreshTime", 1000).toInt();
108 iMaxVolume = m_settings.value("/MaxVolume", 100).toInt();
109 sMessagesFont = m_settings.value("/MessagesFont").toString();
110 bMessagesLimit = m_settings.value("/MessagesLimit", true).toBool();
111 iMessagesLimitLines = m_settings.value("/MessagesLimitLines", 1000).toInt();
112 bConfirmRemove = m_settings.value("/ConfirmRemove", true).toBool();
113 bConfirmReset = m_settings.value("/ConfirmReset", true).toBool();
114 bConfirmRestart = m_settings.value("/ConfirmRestart", true).toBool();
115 bConfirmError = m_settings.value("/ConfirmError", true).toBool();
116 bKeepOnTop = m_settings.value("/KeepOnTop", true).toBool();
117 bStdoutCapture = m_settings.value("/StdoutCapture", true).toBool();
118 bCompletePath = m_settings.value("/CompletePath", true).toBool();
119 iMaxRecentFiles = m_settings.value("/MaxRecentFiles", 5).toInt();
120 iBaseFontSize = m_settings.value("/BaseFontSize", 0).toInt();
121 // if libgig provides a fast way to retrieve instrument names even for large
122 // .gig files, then we enable this feature by default
123 #ifdef CONFIG_LIBGIG_SETAUTOLOAD
124 bInstrumentNames = m_settings.value("/InstrumentNames", true).toBool();
125 #else
126 bInstrumentNames = m_settings.value("/InstrumentNames", false).toBool();
127 #endif
128 m_settings.endGroup();
129
130 // Load custom options...
131 m_settings.beginGroup("/Custom");
132 sCustomColorTheme = m_settings.value("/ColorTheme").toString();
133 sCustomStyleTheme = m_settings.value("/StyleTheme").toString();
134 m_settings.endGroup();
135
136 // And go into view options group.
137 m_settings.beginGroup("/View");
138 bMenubar = m_settings.value("/Menubar", true).toBool();
139 bToolbar = m_settings.value("/Toolbar", true).toBool();
140 bStatusbar = m_settings.value("/Statusbar", true).toBool();
141 bAutoArrange = m_settings.value("/AutoArrange", true).toBool();
142 m_settings.endGroup();
143
144 m_settings.endGroup(); // Options group.
145
146 // Recent file list.
147 recentFiles.clear();
148 m_settings.beginGroup("/RecentFiles");
149 for (int iFile = 0; iFile < iMaxRecentFiles; iFile++) {
150 QString sFilename = m_settings.value(
151 "/File" + QString::number(iFile + 1)).toString();
152 if (!sFilename.isEmpty())
153 recentFiles.append(sFilename);
154 }
155 m_settings.endGroup();
156
157 // Sampler fine tuning settings.
158 m_settings.beginGroup("/Tuning");
159 iMaxVoices = m_settings.value("/MaxVoices", -1).toInt();
160 iMaxStreams = m_settings.value("/MaxStreams", -1).toInt();
161 m_settings.endGroup();
162
163 // Last but not least, get the default directories.
164 m_settings.beginGroup("/Default");
165 sSessionDir = m_settings.value("/SessionDir").toString();
166 sInstrumentDir = m_settings.value("/InstrumentDir").toString();
167 sEngineName = m_settings.value("/EngineName").toString();
168 sAudioDriver = m_settings.value("/AudioDriver").toString();
169 sMidiDriver = m_settings.value("/MidiDriver").toString();
170 iMidiMap = m_settings.value("/MidiMap", 0).toInt();
171 iMidiBank = m_settings.value("/MidiBank", 0).toInt();
172 iMidiProg = m_settings.value("/MidiProg", 0).toInt();
173 iVolume = m_settings.value("/Volume", 100).toInt();
174 iLoadMode = m_settings.value("/Loadmode", 0).toInt();
175 m_settings.endGroup();
176 }
177
178
179 // Explicit save method.
180 void Options::saveOptions (void)
181 {
182 // Make program version available in the future.
183 m_settings.beginGroup("/Program");
184 m_settings.setValue("/Version", CONFIG_BUILD_VERSION);
185 m_settings.endGroup();
186
187 // And go into general options group.
188 m_settings.beginGroup("/Options");
189
190 // Save server options.
191 m_settings.beginGroup("/Server");
192 m_settings.setValue("/ServerHost", sServerHost);
193 m_settings.setValue("/ServerPort", iServerPort);
194 m_settings.setValue("/ServerTimeout", iServerTimeout);
195 m_settings.setValue("/ServerStart", bServerStart);
196 m_settings.setValue("/ServerCmdLine", sServerCmdLine);
197 m_settings.setValue("/StartDelay", iStartDelay);
198 m_settings.endGroup();
199
200 // Save logging options...
201 m_settings.beginGroup("/Logging");
202 m_settings.setValue("/MessagesLog", bMessagesLog);
203 m_settings.setValue("/MessagesLogPath", sMessagesLogPath);
204 m_settings.endGroup();
205
206 // Save display options.
207 m_settings.beginGroup("/Display");
208 m_settings.setValue("/DisplayFont", sDisplayFont);
209 m_settings.setValue("/DisplayEffect", bDisplayEffect);
210 m_settings.setValue("/AutoRefresh", bAutoRefresh);
211 m_settings.setValue("/AutoRefreshTime", iAutoRefreshTime);
212 m_settings.setValue("/MaxVolume", iMaxVolume);
213 m_settings.setValue("/MessagesFont", sMessagesFont);
214 m_settings.setValue("/MessagesLimit", bMessagesLimit);
215 m_settings.setValue("/MessagesLimitLines", iMessagesLimitLines);
216 m_settings.setValue("/ConfirmRemove", bConfirmRemove);
217 m_settings.setValue("/ConfirmReset", bConfirmReset);
218 m_settings.setValue("/ConfirmRestart", bConfirmRestart);
219 m_settings.setValue("/ConfirmError", bConfirmError);
220 m_settings.setValue("/KeepOnTop", bKeepOnTop);
221 m_settings.setValue("/StdoutCapture", bStdoutCapture);
222 m_settings.setValue("/CompletePath", bCompletePath);
223 m_settings.setValue("/MaxRecentFiles", iMaxRecentFiles);
224 m_settings.setValue("/BaseFontSize", iBaseFontSize);
225 m_settings.setValue("/InstrumentNames", bInstrumentNames);
226 m_settings.endGroup();
227
228 // Save custom options...
229 m_settings.beginGroup("/Custom");
230 m_settings.setValue("/ColorTheme", sCustomColorTheme);
231 m_settings.setValue("/StyleTheme", sCustomStyleTheme);
232 m_settings.endGroup();
233
234 // View options group.
235 m_settings.beginGroup("/View");
236 m_settings.setValue("/Menubar", bMenubar);
237 m_settings.setValue("/Toolbar", bToolbar);
238 m_settings.setValue("/Statusbar", bStatusbar);
239 m_settings.setValue("/AutoArrange", bAutoArrange);
240 m_settings.endGroup();
241
242 m_settings.endGroup(); // Options group.
243
244 // Recent file list.
245 int iFile = 0;
246 m_settings.beginGroup("/RecentFiles");
247 QStringListIterator iter(recentFiles);
248 while (iter.hasNext())
249 m_settings.setValue("/File" + QString::number(++iFile), iter.next());
250 m_settings.endGroup();
251
252 // Sampler fine tuning settings.
253 m_settings.beginGroup("/Tuning");
254 if (iMaxVoices > 0)
255 m_settings.setValue("/MaxVoices", iMaxVoices);
256 if (iMaxStreams >= 0)
257 m_settings.setValue("/MaxStreams", iMaxStreams);
258 m_settings.endGroup();
259
260 // Default directories.
261 m_settings.beginGroup("/Default");
262 m_settings.setValue("/SessionDir", sSessionDir);
263 m_settings.setValue("/InstrumentDir", sInstrumentDir);
264 m_settings.setValue("/EngineName", sEngineName);
265 m_settings.setValue("/AudioDriver", sAudioDriver);
266 m_settings.setValue("/MidiDriver", sMidiDriver);
267 m_settings.setValue("/MidiMap", iMidiMap);
268 m_settings.setValue("/MidiBank", iMidiBank);
269 m_settings.setValue("/MidiProg", iMidiProg);
270 m_settings.setValue("/Volume", iVolume);
271 m_settings.setValue("/Loadmode", iLoadMode);
272 m_settings.endGroup();
273
274 // Save/commit to disk.
275 m_settings.sync();
276 }
277
278
279 //-------------------------------------------------------------------------
280 // Settings accessor.
281 //
282
283 QSettings& Options::settings (void)
284 {
285 return m_settings;
286 }
287
288
289 //-------------------------------------------------------------------------
290 // Command-line argument stuff.
291 //
292
293 // Help about command line options.
294 void Options::print_usage ( const QString& arg0 )
295 {
296 QTextStream out(stderr);
297 out << QObject::tr("Usage: %1 [options] [session-file]\n\n"
298 QSAMPLER_TITLE " - " QSAMPLER_SUBTITLE "\n\n"
299 "Options:\n\n"
300 " -s, --start\n\tStart linuxsampler server locally\n\n"
301 " -h, --hostname\n\tSpecify linuxsampler server hostname (default = localhost)\n\n"
302 " -p, --port\n\tSpecify linuxsampler server port number (default = 8888)\n\n"
303 " -?, --help\n\tShow help about command line options\n\n"
304 " -v, --version\n\tShow version information\n\n")
305 .arg(arg0);
306 }
307
308
309 // Parse command line arguments into m_settings.
310 bool Options::parse_args ( const QStringList& args )
311 {
312 QTextStream out(stderr);
313 const QString sEol = "\n\n";
314 const int argc = args.count();
315 int iCmdArgs = 0;
316
317 for (int i = 1; i < argc; ++i) {
318
319 if (iCmdArgs > 0) {
320 sSessionFile += " ";
321 sSessionFile += args.at(i);
322 ++iCmdArgs;
323 continue;
324 }
325
326 QString sArg = args.at(i);
327 QString sVal;
328 const int iEqual = sArg.indexOf("=");
329 if (iEqual >= 0) {
330 sVal = sArg.right(sArg.length() - iEqual - 1);
331 sArg = sArg.left(iEqual);
332 }
333 else if (i < argc - 1) {
334 sVal = args.at(i + 1);
335 if (sVal[0] == '-')
336 sVal.clear();
337 }
338
339 if (sArg == "-s" || sArg == "--start") {
340 bServerStart = true;
341 }
342 else if (sArg == "-h" || sArg == "--hostname") {
343 if (sVal.isNull()) {
344 out << QObject::tr("Option -h requires an argument (host).") + sEol;
345 return false;
346 }
347 sServerHost = sVal;
348 if (iEqual < 0)
349 ++i;
350 }
351 else if (sArg == "-p" || sArg == "--port") {
352 if (sVal.isNull()) {
353 out << QObject::tr("Option -p requires an argument (port).") + sEol;
354 return false;
355 }
356 iServerPort = sVal.toInt();
357 if (iEqual < 0)
358 ++i;
359 }
360 else if (sArg == "-?" || sArg == "--help") {
361 print_usage(args.at(0));
362 return false;
363 }
364 else if (sArg == "-v" || sArg == "--version") {
365 out << QString("Qt: %1").arg(qVersion());
366 #if defined(QT_STATIC)
367 out << "-static";
368 #endif
369 out << '\n';
370 #ifdef CONFIG_LIBGIG
371 out << QString("%1: %2\n")
372 .arg(gig::libraryName().c_str())
373 .arg(gig::libraryVersion().c_str());
374 #endif
375 out << QString("%1: %2\n")
376 .arg(::lscp_client_package())
377 .arg(::lscp_client_version());
378 out << QString("%1: %2\n")
379 .arg(QSAMPLER_TITLE)
380 .arg(CONFIG_BUILD_VERSION);
381 return false;
382 }
383 else {
384 // If we don't have one by now,
385 // this will be the startup sesion file...
386 sSessionFile += sArg;
387 ++iCmdArgs;
388 }
389 }
390
391 // Alright with argument parsing.
392 return true;
393 }
394
395
396 //---------------------------------------------------------------------------
397 // Widget geometry persistence helper methods.
398
399 void Options::loadWidgetGeometry ( QWidget *pWidget, bool bVisible )
400 {
401 // Try to restore old form window positioning.
402 if (pWidget) {
403 // if (bVisible) pWidget->show(); -- force initial exposure?
404 m_settings.beginGroup("/Geometry/" + pWidget->objectName());
405 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
406 const QByteArray& geometry
407 = m_settings.value("/geometry").toByteArray();
408 if (geometry.isEmpty()) {
409 QWidget *pParent = pWidget->parentWidget();
410 if (pParent)
411 pParent = pParent->window();
412 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
413 if (pParent == nullptr)
414 pParent = QApplication::desktop();
415 #endif
416 if (pParent) {
417 QRect wrect(pWidget->geometry());
418 wrect.moveCenter(pParent->geometry().center());
419 pWidget->move(wrect.topLeft());
420 }
421 } else {
422 pWidget->restoreGeometry(geometry);
423 }
424 #else//--LOAD_OLD_GEOMETRY
425 QPoint wpos;
426 QSize wsize;
427 wpos.setX(m_settings.value("/x", -1).toInt());
428 wpos.setY(m_settings.value("/y", -1).toInt());
429 wsize.setWidth(m_settings.value("/width", -1).toInt());
430 wsize.setHeight(m_settings.value("/height", -1).toInt());
431 if (wpos.x() > 0 && wpos.y() > 0)
432 pWidget->move(wpos);
433 if (wsize.width() > 0 && wsize.height() > 0)
434 pWidget->resize(wsize);
435 #endif
436 // else
437 // pWidget->adjustSize();
438 if (!bVisible)
439 bVisible = m_settings.value("/visible", false).toBool();
440 if (bVisible)
441 pWidget->show();
442 else
443 pWidget->hide();
444 m_settings.endGroup();
445 }
446 }
447
448
449 void Options::saveWidgetGeometry ( QWidget *pWidget, bool bVisible )
450 {
451 // Try to save form window position...
452 // (due to X11 window managers ideossincrasies, we better
453 // only save the form geometry while its up and visible)
454 if (pWidget) {
455 m_settings.beginGroup("/Geometry/" + pWidget->objectName());
456 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
457 m_settings.setValue("/geometry", pWidget->saveGeometry());
458 #else//--SAVE_OLD_GEOMETRY
459 const QPoint& wpos = pWidget->pos();
460 const QSize& wsize = pWidget->size();
461 m_settings.setValue("/x", wpos.x());
462 m_settings.setValue("/y", wpos.y());
463 m_settings.setValue("/width", wsize.width());
464 m_settings.setValue("/height", wsize.height());
465 #endif
466 if (!bVisible) bVisible = pWidget->isVisible();
467 m_settings.setValue("/visible", bVisible);
468 m_settings.endGroup();
469 }
470 }
471
472
473 //---------------------------------------------------------------------------
474 // Combo box history persistence helper implementation.
475
476 void Options::loadComboBoxHistory ( QComboBox *pComboBox, int iLimit )
477 {
478 const bool bBlockSignals = pComboBox->blockSignals(true);
479
480 // Load combobox list from configuration settings file...
481 m_settings.beginGroup("/History/" + pComboBox->objectName());
482
483 if (m_settings.childKeys().count() > 0) {
484 pComboBox->setUpdatesEnabled(false);
485 pComboBox->setDuplicatesEnabled(false);
486 pComboBox->clear();
487 for (int i = 0; i < iLimit; ++i) {
488 const QString& sText = m_settings.value(
489 "/Item" + QString::number(i + 1)).toString();
490 if (sText.isEmpty())
491 break;
492 pComboBox->addItem(sText);
493 }
494 pComboBox->setUpdatesEnabled(true);
495 }
496
497 m_settings.endGroup();
498
499 pComboBox->blockSignals(bBlockSignals);
500 }
501
502
503 void Options::saveComboBoxHistory ( QComboBox *pComboBox, int iLimit )
504 {
505 const bool bBlockSignals = pComboBox->blockSignals(true);
506
507 // Add current text as latest item...
508 const QString sCurrentText = pComboBox->currentText();
509 int iCount = pComboBox->count();
510 for (int i = 0; i < iCount; i++) {
511 const QString& sText = pComboBox->itemText(i);
512 if (sText == sCurrentText) {
513 pComboBox->removeItem(i);
514 --iCount;
515 break;
516 }
517 }
518 while (iCount >= iLimit)
519 pComboBox->removeItem(--iCount);
520 pComboBox->insertItem(0, sCurrentText);
521 pComboBox->setCurrentIndex(0);
522 ++iCount;
523
524 // Save combobox list to configuration settings file...
525 m_settings.beginGroup("/History/" + pComboBox->objectName());
526 for (int i = 0; i < iCount; ++i) {
527 const QString& sText = pComboBox->itemText(i);
528 if (sText.isEmpty())
529 break;
530 m_settings.setValue("/Item" + QString::number(i + 1), sText);
531 }
532 m_settings.endGroup();
533
534 pComboBox->blockSignals(bBlockSignals);
535 }
536
537
538 int Options::getMaxVoices() {
539 #ifndef CONFIG_MAX_VOICES
540 return -1;
541 #else
542 if (iMaxVoices > 0) return iMaxVoices;
543 return getEffectiveMaxVoices();
544 #endif // CONFIG_MAX_VOICES
545 }
546
547 int Options::getEffectiveMaxVoices() {
548 #ifndef CONFIG_MAX_VOICES
549 return -1;
550 #else
551 MainForm *pMainForm = MainForm::getInstance();
552 if (!pMainForm || !pMainForm->client())
553 return -1;
554
555 return ::lscp_get_voices(pMainForm->client());
556 #endif // CONFIG_MAX_VOICES
557 }
558
559 void Options::setMaxVoices(int iMaxVoices) {
560 #ifdef CONFIG_MAX_VOICES
561 if (iMaxVoices < 1) return;
562
563 MainForm *pMainForm = MainForm::getInstance();
564 if (!pMainForm || !pMainForm->client())
565 return;
566
567 lscp_status_t result =
568 ::lscp_set_voices(pMainForm->client(), iMaxVoices);
569
570 if (result != LSCP_OK) {
571 pMainForm->appendMessagesClient("lscp_set_voices");
572 return;
573 }
574
575 this->iMaxVoices = iMaxVoices;
576 #endif // CONFIG_MAX_VOICES
577 }
578
579 int Options::getMaxStreams() {
580 #ifndef CONFIG_MAX_VOICES
581 return -1;
582 #else
583 if (iMaxStreams > 0) return iMaxStreams;
584 return getEffectiveMaxStreams();
585 #endif // CONFIG_MAX_VOICES
586 }
587
588 int Options::getEffectiveMaxStreams() {
589 #ifndef CONFIG_MAX_VOICES
590 return -1;
591 #else
592 MainForm *pMainForm = MainForm::getInstance();
593 if (!pMainForm || !pMainForm->client())
594 return -1;
595
596 return ::lscp_get_streams(pMainForm->client());
597 #endif // CONFIG_MAX_VOICES
598 }
599
600 void Options::setMaxStreams(int iMaxStreams) {
601 #ifdef CONFIG_MAX_VOICES
602 if (iMaxStreams < 0) return;
603
604 MainForm *pMainForm = MainForm::getInstance();
605 if (!pMainForm || !pMainForm->client())
606 return;
607
608 lscp_status_t result =
609 ::lscp_set_streams(pMainForm->client(), iMaxStreams);
610
611 if (result != LSCP_OK) {
612 pMainForm->appendMessagesClient("lscp_set_streams");
613 return;
614 }
615
616 this->iMaxStreams = iMaxStreams;
617 #endif // CONFIG_MAX_VOICES
618 }
619
620 void Options::sendFineTuningSettings() {
621 setMaxVoices(iMaxVoices);
622 setMaxStreams(iMaxStreams);
623
624 MainForm *pMainForm = MainForm::getInstance();
625 if (!pMainForm || !pMainForm->client())
626 return;
627
628 pMainForm->appendMessages(QObject::tr("Sent fine tuning settings."));
629 }
630
631 } // namespace QSampler
632
633
634 // end of qsamplerOptions.cpp
635

  ViewVC Help
Powered by ViewVC