1 |
// qsamplerOptions.cpp |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2003-2004, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
|
6 |
This program is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU General Public License |
8 |
as published by the Free Software Foundation; either version 2 |
9 |
of the License, or (at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
20 |
*****************************************************************************/ |
21 |
|
22 |
#include "qsamplerOptions.h" |
23 |
#include "qsamplerAbout.h" |
24 |
|
25 |
#include <qcombobox.h> |
26 |
|
27 |
#include <lscp/client.h> |
28 |
|
29 |
#include "config.h" |
30 |
|
31 |
|
32 |
//------------------------------------------------------------------------- |
33 |
// qsamplerOptions - Prototype settings structure. |
34 |
// |
35 |
|
36 |
// Constructor. |
37 |
qsamplerOptions::qsamplerOptions (void) |
38 |
{ |
39 |
// Begin master key group. |
40 |
m_settings.beginGroup("/qsampler"); |
41 |
|
42 |
// And go into general options group. |
43 |
m_settings.beginGroup("/Options"); |
44 |
|
45 |
// Load server options... |
46 |
m_settings.beginGroup("/Server"); |
47 |
sServerHost = m_settings.readEntry("/ServerHost", "localhost"); |
48 |
iServerPort = m_settings.readNumEntry("/ServerPort", 8888); |
49 |
iServerTimeout = m_settings.readNumEntry("/ServerTimeout", 500); |
50 |
bServerStart = m_settings.readBoolEntry("/ServerStart", true); |
51 |
sServerCmdLine = m_settings.readEntry("/ServerCmdLine", "linuxsampler"); |
52 |
iStartDelay = m_settings.readNumEntry("/StartDelay", 2); |
53 |
m_settings.endGroup(); |
54 |
|
55 |
// Load display options... |
56 |
m_settings.beginGroup("/Display"); |
57 |
sDisplayFont = m_settings.readEntry("/DisplayFont", QString::null); |
58 |
bAutoRefresh = m_settings.readBoolEntry("/AutoRefresh", true); |
59 |
iAutoRefreshTime = m_settings.readNumEntry("/AutoRefreshTime", 1000); |
60 |
iMaxVolume = m_settings.readNumEntry("/MaxVolume", 100); |
61 |
sMessagesFont = m_settings.readEntry("/MessagesFont", QString::null); |
62 |
bMessagesLimit = m_settings.readBoolEntry("/MessagesLimit", true); |
63 |
iMessagesLimitLines = m_settings.readNumEntry("/MessagesLimitLines", 1000); |
64 |
bConfirmRemove = m_settings.readBoolEntry("/ConfirmRemove", true); |
65 |
bStdoutCapture = m_settings.readBoolEntry("/StdoutCapture", true); |
66 |
bCompletePath = m_settings.readBoolEntry("/CompletePath", true); |
67 |
iMaxRecentFiles = m_settings.readNumEntry("/MaxRecentFiles", 5); |
68 |
m_settings.endGroup(); |
69 |
|
70 |
// And go into view options group. |
71 |
m_settings.beginGroup("/View"); |
72 |
bMenubar = m_settings.readBoolEntry("/Menubar", true); |
73 |
bToolbar = m_settings.readBoolEntry("/Toolbar", true); |
74 |
bStatusbar = m_settings.readBoolEntry("/Statusbar", true); |
75 |
bAutoArrange = m_settings.readBoolEntry("/AutoArrange", true); |
76 |
m_settings.endGroup(); |
77 |
|
78 |
m_settings.endGroup(); // Options group. |
79 |
|
80 |
// Recent file list. |
81 |
m_settings.beginGroup("/RecentFiles"); |
82 |
recentFiles.clear(); |
83 |
for (int i = 0; i < iMaxRecentFiles; i++) { |
84 |
QString sFilename = m_settings.readEntry("/File" + QString::number(i + 1), QString::null); |
85 |
if (!sFilename.isEmpty()) |
86 |
recentFiles.append(sFilename); |
87 |
} |
88 |
m_settings.endGroup(); |
89 |
|
90 |
// Last but not least, get the default directories. |
91 |
m_settings.beginGroup("/Default"); |
92 |
sSessionDir = m_settings.readEntry("/SessionDir", QString::null); |
93 |
sInstrumentDir = m_settings.readEntry("/InstrumentDir", QString::null); |
94 |
sEngineName = m_settings.readEntry("/EngineName", QString::null); |
95 |
sAudioDriver = m_settings.readEntry("/AudioDriver", QString::null); |
96 |
sMidiDriver = m_settings.readEntry("/MidiDriver", QString::null); |
97 |
m_settings.endGroup(); |
98 |
} |
99 |
|
100 |
|
101 |
// Default Destructor. |
102 |
qsamplerOptions::~qsamplerOptions (void) |
103 |
{ |
104 |
// Make program version available in the future. |
105 |
m_settings.beginGroup("/Program"); |
106 |
m_settings.writeEntry("/Version", QSAMPLER_VERSION); |
107 |
m_settings.endGroup(); |
108 |
|
109 |
// And go into general options group. |
110 |
m_settings.beginGroup("/Options"); |
111 |
|
112 |
// Save server options. |
113 |
m_settings.beginGroup("/Server"); |
114 |
m_settings.writeEntry("/ServerHost", sServerHost); |
115 |
m_settings.writeEntry("/ServerPort", iServerPort); |
116 |
m_settings.writeEntry("/ServerTimeout", iServerTimeout); |
117 |
m_settings.writeEntry("/ServerStart", bServerStart); |
118 |
m_settings.writeEntry("/ServerCmdLine", sServerCmdLine); |
119 |
m_settings.writeEntry("/StartDelay", iStartDelay); |
120 |
m_settings.endGroup(); |
121 |
|
122 |
// Save display options. |
123 |
m_settings.beginGroup("/Display"); |
124 |
m_settings.writeEntry("/DisplayFont", sDisplayFont); |
125 |
m_settings.writeEntry("/AutoRefresh", bAutoRefresh); |
126 |
m_settings.writeEntry("/AutoRefreshTime", iAutoRefreshTime); |
127 |
m_settings.writeEntry("/MaxVolume", iMaxVolume); |
128 |
m_settings.writeEntry("/MessagesFont", sMessagesFont); |
129 |
m_settings.writeEntry("/MessagesLimit", bMessagesLimit); |
130 |
m_settings.writeEntry("/MessagesLimitLines", iMessagesLimitLines); |
131 |
m_settings.writeEntry("/ConfirmRemove", bConfirmRemove); |
132 |
m_settings.writeEntry("/StdoutCapture", bStdoutCapture); |
133 |
m_settings.writeEntry("/CompletePath", bCompletePath); |
134 |
m_settings.writeEntry("/MaxRecentFiles", iMaxRecentFiles); |
135 |
m_settings.endGroup(); |
136 |
|
137 |
// View options group. |
138 |
m_settings.beginGroup("/View"); |
139 |
m_settings.writeEntry("/Menubar", bMenubar); |
140 |
m_settings.writeEntry("/Toolbar", bToolbar); |
141 |
m_settings.writeEntry("/Statusbar", bStatusbar); |
142 |
m_settings.writeEntry("/AutoArrange", bAutoArrange); |
143 |
m_settings.endGroup(); |
144 |
|
145 |
m_settings.endGroup(); // Options group. |
146 |
|
147 |
// Recent file list. |
148 |
m_settings.beginGroup("/RecentFiles"); |
149 |
for (int i = 0; i < (int) recentFiles.count(); i++) |
150 |
m_settings.writeEntry("/File" + QString::number(i + 1), recentFiles[i]); |
151 |
m_settings.endGroup(); |
152 |
|
153 |
// Default directories. |
154 |
m_settings.beginGroup("/Default"); |
155 |
m_settings.writeEntry("/SessionDir", sSessionDir); |
156 |
m_settings.writeEntry("/InstrumentDir", sInstrumentDir); |
157 |
m_settings.writeEntry("/EngineName", sEngineName); |
158 |
m_settings.writeEntry("/AudioDriver", sAudioDriver); |
159 |
m_settings.writeEntry("/MidiDriver", sMidiDriver); |
160 |
m_settings.endGroup(); |
161 |
|
162 |
m_settings.endGroup(); |
163 |
} |
164 |
|
165 |
//------------------------------------------------------------------------- |
166 |
// Settings accessor. |
167 |
// |
168 |
|
169 |
QSettings& qsamplerOptions::settings (void) |
170 |
{ |
171 |
return m_settings; |
172 |
} |
173 |
|
174 |
|
175 |
//------------------------------------------------------------------------- |
176 |
// Command-line argument stuff. |
177 |
// |
178 |
|
179 |
// Help about command line options. |
180 |
void qsamplerOptions::print_usage ( const char *arg0 ) |
181 |
{ |
182 |
const QString sEot = "\n\t"; |
183 |
const QString sEol = "\n\n"; |
184 |
|
185 |
fprintf(stderr, QObject::tr("Usage") + ": %s [" + QObject::tr("options") + "] [" + |
186 |
QObject::tr("session-file") + "]" + sEol, arg0); |
187 |
fprintf(stderr, QSAMPLER_TITLE " - " + QObject::tr(QSAMPLER_SUBTITLE) + sEol); |
188 |
fprintf(stderr, QObject::tr("Options") + ":" + sEol); |
189 |
fprintf(stderr, " -s, --start" + sEot + |
190 |
QObject::tr("Start linuxsampler server locally") + sEol); |
191 |
fprintf(stderr, " -h, --hostname" + sEot + |
192 |
QObject::tr("Specify linuxsampler server hostname") + sEol); |
193 |
fprintf(stderr, " -p, --port" + sEot + |
194 |
QObject::tr("Specify linuxsampler server port number") + sEol); |
195 |
fprintf(stderr, " -?, --help" + sEot + |
196 |
QObject::tr("Show help about command line options") + sEol); |
197 |
fprintf(stderr, " -v, --version" + sEot + |
198 |
QObject::tr("Show version information") + sEol); |
199 |
} |
200 |
|
201 |
|
202 |
// Parse command line arguments into m_settings. |
203 |
bool qsamplerOptions::parse_args ( int argc, char **argv ) |
204 |
{ |
205 |
const QString sEol = "\n\n"; |
206 |
int iCmdArgs = 0; |
207 |
|
208 |
for (int i = 1; i < argc; i++) { |
209 |
|
210 |
if (iCmdArgs > 0) { |
211 |
sSessionFile += " "; |
212 |
sSessionFile += argv[i]; |
213 |
iCmdArgs++; |
214 |
continue; |
215 |
} |
216 |
|
217 |
QString sArg = argv[i]; |
218 |
QString sVal = QString::null; |
219 |
int iEqual = sArg.find("="); |
220 |
if (iEqual >= 0) { |
221 |
sVal = sArg.right(sArg.length() - iEqual - 1); |
222 |
sArg = sArg.left(iEqual); |
223 |
} |
224 |
else if (i < argc) |
225 |
sVal = argv[i + 1]; |
226 |
|
227 |
if (sArg == "-s" || sArg == "--start") { |
228 |
bServerStart = true; |
229 |
} |
230 |
else if (sArg == "-h" || sArg == "--hostname") { |
231 |
if (sVal.isNull()) { |
232 |
fprintf(stderr, QObject::tr("Option -h requires an argument (hostname).") + sEol); |
233 |
return false; |
234 |
} |
235 |
sServerHost = sVal; |
236 |
if (iEqual < 0) |
237 |
i++; |
238 |
} |
239 |
else if (sArg == "-p" || sArg == "--port") { |
240 |
if (sVal.isNull()) { |
241 |
fprintf(stderr, QObject::tr("Option -p requires an argument (port).") + sEol); |
242 |
return false; |
243 |
} |
244 |
iServerPort = sVal.toInt(); |
245 |
if (iEqual < 0) |
246 |
i++; |
247 |
} |
248 |
else if (sArg == "-?" || sArg == "--help") { |
249 |
print_usage(argv[0]); |
250 |
return false; |
251 |
} |
252 |
else if (sArg == "-v" || sArg == "--version") { |
253 |
fprintf(stderr, "Qt: %s\n", qVersion()); |
254 |
fprintf(stderr, "liblscp: %s\n", ::lscp_client_version()); |
255 |
fprintf(stderr, "qsampler: %s\n", QSAMPLER_VERSION); |
256 |
return false; |
257 |
} |
258 |
else { |
259 |
// If we don't have one by now, |
260 |
// this will be the startup sesion file... |
261 |
sSessionFile += sArg; |
262 |
iCmdArgs++; |
263 |
} |
264 |
} |
265 |
|
266 |
// Alright with argument parsing. |
267 |
return true; |
268 |
} |
269 |
|
270 |
|
271 |
//--------------------------------------------------------------------------- |
272 |
// Widget geometry persistence helper methods. |
273 |
|
274 |
void qsamplerOptions::loadWidgetGeometry ( QWidget *pWidget ) |
275 |
{ |
276 |
// Try to restore old form window positioning. |
277 |
if (pWidget) { |
278 |
QPoint fpos; |
279 |
QSize fsize; |
280 |
bool bVisible; |
281 |
m_settings.beginGroup("/Geometry/" + QString(pWidget->name())); |
282 |
fpos.setX(m_settings.readNumEntry("/x", -1)); |
283 |
fpos.setY(m_settings.readNumEntry("/y", -1)); |
284 |
fsize.setWidth(m_settings.readNumEntry("/width", -1)); |
285 |
fsize.setHeight(m_settings.readNumEntry("/height", -1)); |
286 |
bVisible = m_settings.readBoolEntry("/visible", false); |
287 |
m_settings.endGroup(); |
288 |
if (fpos.x() > 0 && fpos.y() > 0) |
289 |
pWidget->move(fpos); |
290 |
if (fsize.width() > 0 && fsize.height() > 0) |
291 |
pWidget->resize(fsize); |
292 |
else |
293 |
pWidget->adjustSize(); |
294 |
if (bVisible) |
295 |
pWidget->show(); |
296 |
else |
297 |
pWidget->hide(); |
298 |
} |
299 |
} |
300 |
|
301 |
|
302 |
void qsamplerOptions::saveWidgetGeometry ( QWidget *pWidget ) |
303 |
{ |
304 |
// Try to save form window position... |
305 |
// (due to X11 window managers ideossincrasies, we better |
306 |
// only save the form geometry while its up and visible) |
307 |
if (pWidget) { |
308 |
m_settings.beginGroup("/Geometry/" + QString(pWidget->name())); |
309 |
bool bVisible = pWidget->isVisible(); |
310 |
if (bVisible) { |
311 |
QPoint fpos = pWidget->pos(); |
312 |
QSize fsize = pWidget->size(); |
313 |
m_settings.writeEntry("/x", fpos.x()); |
314 |
m_settings.writeEntry("/y", fpos.y()); |
315 |
m_settings.writeEntry("/width", fsize.width()); |
316 |
m_settings.writeEntry("/height", fsize.height()); |
317 |
} |
318 |
m_settings.writeEntry("/visible", bVisible); |
319 |
m_settings.endGroup(); |
320 |
} |
321 |
} |
322 |
|
323 |
|
324 |
//--------------------------------------------------------------------------- |
325 |
// Combo box history persistence helper implementation. |
326 |
|
327 |
void qsamplerOptions::add2ComboBoxHistory ( QComboBox *pComboBox, const QString& sNewText, int iLimit, int iIndex ) |
328 |
{ |
329 |
int iCount = pComboBox->count(); |
330 |
for (int i = 0; i < iCount; i++) { |
331 |
QString sText = pComboBox->text(i); |
332 |
if (sText == sNewText) { |
333 |
pComboBox->removeItem(i); |
334 |
iCount--; |
335 |
break; |
336 |
} |
337 |
} |
338 |
while (iCount >= iLimit) |
339 |
pComboBox->removeItem(--iCount); |
340 |
pComboBox->insertItem(sNewText, iIndex); |
341 |
} |
342 |
|
343 |
|
344 |
void qsamplerOptions::loadComboBoxHistory ( QComboBox *pComboBox, int iLimit ) |
345 |
{ |
346 |
pComboBox->setUpdatesEnabled(false); |
347 |
pComboBox->setDuplicatesEnabled(false); |
348 |
|
349 |
m_settings.beginGroup("/History/" + QString(pComboBox->name())); |
350 |
for (int i = 0; i < iLimit; i++) { |
351 |
QString sText = m_settings.readEntry("/Item" + QString::number(i + 1), QString::null); |
352 |
if (sText.isEmpty()) |
353 |
break; |
354 |
add2ComboBoxHistory(pComboBox, sText, iLimit); |
355 |
} |
356 |
m_settings.endGroup(); |
357 |
|
358 |
pComboBox->setUpdatesEnabled(true); |
359 |
} |
360 |
|
361 |
|
362 |
void qsamplerOptions::saveComboBoxHistory ( QComboBox *pComboBox, int iLimit ) |
363 |
{ |
364 |
add2ComboBoxHistory(pComboBox, pComboBox->currentText(), iLimit, 0); |
365 |
|
366 |
m_settings.beginGroup("/History/" + QString(pComboBox->name())); |
367 |
for (int i = 0; i < iLimit && i < pComboBox->count(); i++) { |
368 |
QString sText = pComboBox->text(i); |
369 |
if (sText.isEmpty()) |
370 |
break; |
371 |
m_settings.writeEntry("/Item" + QString::number(i + 1), sText); |
372 |
} |
373 |
m_settings.endGroup(); |
374 |
} |
375 |
|
376 |
|
377 |
// end of qsamplerOptions.cpp |