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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC