annotate base/Preferences.cpp @ 383:94fc0591ea43 1.2-stable

* merge from trunk (1.2 ended up being tracked from trunk, but we may want this branch for fixes later)
author Chris Cannam
date Wed, 27 Feb 2008 10:32:45 +0000
parents 4fc6f49436b3
children
rev   line source
Chris@136 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@136 2
Chris@136 3 /*
Chris@136 4 Sonic Visualiser
Chris@136 5 An audio file viewer and annotation editor.
Chris@136 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@136 8
Chris@136 9 This program is free software; you can redistribute it and/or
Chris@136 10 modify it under the terms of the GNU General Public License as
Chris@136 11 published by the Free Software Foundation; either version 2 of the
Chris@136 12 License, or (at your option) any later version. See the file
Chris@136 13 COPYING included with this distribution for more information.
Chris@136 14 */
Chris@136 15
Chris@136 16 #include "Preferences.h"
Chris@136 17
Chris@145 18 #include "Exceptions.h"
Chris@145 19
Chris@145 20 #include "TempDirectory.h"
Chris@145 21
Chris@145 22 #include <QDir>
Chris@145 23 #include <QFileInfo>
Chris@156 24 #include <QMutex>
Chris@156 25 #include <QSettings>
Chris@383 26 #include <QApplication>
Chris@383 27 #include <QFont>
Chris@145 28
Chris@136 29 Preferences *
Chris@156 30 Preferences::m_instance = 0;
Chris@156 31
Chris@156 32 Preferences *
Chris@156 33 Preferences::getInstance()
Chris@156 34 {
Chris@156 35 if (!m_instance) m_instance = new Preferences();
Chris@156 36 return m_instance;
Chris@156 37 }
Chris@136 38
Chris@136 39 Preferences::Preferences() :
Chris@246 40 m_spectrogramSmoothing(SpectrogramZeroPadded),
Chris@140 41 m_tuningFrequency(440),
Chris@140 42 m_propertyBoxLayout(VerticallyStacked),
Chris@164 43 m_windowType(HanningWindow),
Chris@277 44 m_resampleQuality(1),
Chris@297 45 m_omitRecentTemps(true),
Chris@297 46 m_tempDirRoot(""),
Chris@383 47 m_resampleOnLoad(false),
Chris@383 48 m_viewFontSize(10),
Chris@383 49 m_backgroundMode(BackgroundFromTheme),
Chris@383 50 m_showSplash(true)
Chris@136 51 {
Chris@156 52 QSettings settings;
Chris@156 53 settings.beginGroup("Preferences");
Chris@246 54 m_spectrogramSmoothing = SpectrogramSmoothing
Chris@246 55 (settings.value("spectrogram-smoothing", int(m_spectrogramSmoothing)).toInt());
Chris@156 56 m_tuningFrequency = settings.value("tuning-frequency", 440.f).toDouble();
Chris@145 57 m_propertyBoxLayout = PropertyBoxLayout
Chris@156 58 (settings.value("property-box-layout", int(VerticallyStacked)).toInt());
Chris@145 59 m_windowType = WindowType
Chris@156 60 (settings.value("window-type", int(HanningWindow)).toInt());
Chris@164 61 m_resampleQuality = settings.value("resample-quality", 1).toInt();
Chris@304 62 m_resampleOnLoad = settings.value("resample-on-load", false).toBool();
Chris@297 63 m_backgroundMode = BackgroundMode
Chris@297 64 (settings.value("background-mode", int(BackgroundFromTheme)).toInt());
Chris@383 65 m_viewFontSize = settings.value
Chris@383 66 ("view-font-size", int(QApplication::font().pointSize() * 0.9))
Chris@383 67 .toInt();
Chris@383 68 m_showSplash = settings.value("show-splash", true).toBool();
Chris@297 69 settings.endGroup();
Chris@297 70
Chris@297 71 settings.beginGroup("TempDirectory");
Chris@297 72 m_tempDirRoot = settings.value("create-in", "$HOME").toString();
Chris@156 73 settings.endGroup();
Chris@145 74 }
Chris@145 75
Chris@145 76 Preferences::~Preferences()
Chris@145 77 {
Chris@136 78 }
Chris@136 79
Chris@136 80 Preferences::PropertyList
Chris@136 81 Preferences::getProperties() const
Chris@136 82 {
Chris@136 83 PropertyList props;
Chris@246 84 props.push_back("Spectrogram Smoothing");
Chris@136 85 props.push_back("Tuning Frequency");
Chris@138 86 props.push_back("Property Box Layout");
Chris@140 87 props.push_back("Window Type");
Chris@164 88 props.push_back("Resample Quality");
Chris@277 89 props.push_back("Omit Temporaries from Recent Files");
Chris@297 90 props.push_back("Resample On Load");
Chris@297 91 props.push_back("Temporary Directory Root");
Chris@297 92 props.push_back("Background Mode");
Chris@383 93 props.push_back("View Font Size");
Chris@383 94 props.push_back("Show Splash Screen");
Chris@136 95 return props;
Chris@136 96 }
Chris@136 97
Chris@136 98 QString
Chris@136 99 Preferences::getPropertyLabel(const PropertyName &name) const
Chris@136 100 {
Chris@246 101 if (name == "Spectrogram Smoothing") {
Chris@383 102 return tr("Spectrogram y-axis interpolation:");
Chris@136 103 }
Chris@136 104 if (name == "Tuning Frequency") {
Chris@141 105 return tr("Frequency of concert A");
Chris@136 106 }
Chris@138 107 if (name == "Property Box Layout") {
Chris@141 108 return tr("Property box layout");
Chris@138 109 }
Chris@140 110 if (name == "Window Type") {
Chris@141 111 return tr("Spectral analysis window shape");
Chris@140 112 }
Chris@164 113 if (name == "Resample Quality") {
Chris@165 114 return tr("Playback resampler type");
Chris@164 115 }
Chris@277 116 if (name == "Omit Temporaries from Recent Files") {
Chris@297 117 return tr("Omit temporaries from Recent Files menu");
Chris@297 118 }
Chris@297 119 if (name == "Resample On Load") {
Chris@297 120 return tr("Resample mismatching files on import");
Chris@297 121 }
Chris@297 122 if (name == "Temporary Directory Root") {
Chris@297 123 return tr("Location for cache file directory");
Chris@297 124 }
Chris@297 125 if (name == "Background Mode") {
Chris@297 126 return tr("Background colour preference");
Chris@277 127 }
Chris@383 128 if (name == "View Font Size") {
Chris@383 129 return tr("Font size for text overlays");
Chris@383 130 }
Chris@383 131 if (name == "Show Splash Screen") {
Chris@383 132 return tr("Show splash screen on startup");
Chris@383 133 }
Chris@136 134 return name;
Chris@136 135 }
Chris@136 136
Chris@136 137 Preferences::PropertyType
Chris@136 138 Preferences::getPropertyType(const PropertyName &name) const
Chris@136 139 {
Chris@246 140 if (name == "Spectrogram Smoothing") {
Chris@246 141 return ValueProperty;
Chris@136 142 }
Chris@136 143 if (name == "Tuning Frequency") {
Chris@136 144 return RangeProperty;
Chris@136 145 }
Chris@138 146 if (name == "Property Box Layout") {
Chris@138 147 return ValueProperty;
Chris@138 148 }
Chris@140 149 if (name == "Window Type") {
Chris@140 150 return ValueProperty;
Chris@140 151 }
Chris@164 152 if (name == "Resample Quality") {
Chris@164 153 return ValueProperty;
Chris@164 154 }
Chris@277 155 if (name == "Omit Temporaries from Recent Files") {
Chris@277 156 return ToggleProperty;
Chris@277 157 }
Chris@297 158 if (name == "Resample On Load") {
Chris@297 159 return ToggleProperty;
Chris@297 160 }
Chris@297 161 if (name == "Temporary Directory Root") {
Chris@297 162 // It's an arbitrary string, we don't have a set of values for this
Chris@297 163 return InvalidProperty;
Chris@297 164 }
Chris@297 165 if (name == "Background Mode") {
Chris@297 166 return ValueProperty;
Chris@297 167 }
Chris@383 168 if (name == "View Font Size") {
Chris@383 169 return RangeProperty;
Chris@383 170 }
Chris@383 171 if (name == "Show Splash Screen") {
Chris@383 172 return ToggleProperty;
Chris@383 173 }
Chris@136 174 return InvalidProperty;
Chris@136 175 }
Chris@136 176
Chris@136 177 int
Chris@136 178 Preferences::getPropertyRangeAndValue(const PropertyName &name,
Chris@245 179 int *min, int *max, int *deflt) const
Chris@136 180 {
Chris@246 181 if (name == "Spectrogram Smoothing") {
Chris@136 182 if (min) *min = 0;
Chris@246 183 if (max) *max = 2;
Chris@246 184 if (deflt) *deflt = int(SpectrogramZeroPadded);
Chris@246 185 return int(m_spectrogramSmoothing);
Chris@136 186 }
Chris@136 187
Chris@136 188 //!!! freq mapping
Chris@136 189
Chris@138 190 if (name == "Property Box Layout") {
Chris@138 191 if (min) *min = 0;
Chris@138 192 if (max) *max = 1;
Chris@245 193 if (deflt) *deflt = 0;
Chris@138 194 return m_propertyBoxLayout == Layered ? 1 : 0;
Chris@138 195 }
Chris@138 196
Chris@140 197 if (name == "Window Type") {
Chris@140 198 if (min) *min = int(RectangularWindow);
Chris@142 199 if (max) *max = int(BlackmanHarrisWindow);
Chris@245 200 if (deflt) *deflt = int(HanningWindow);
Chris@140 201 return int(m_windowType);
Chris@140 202 }
Chris@140 203
Chris@164 204 if (name == "Resample Quality") {
Chris@164 205 if (min) *min = 0;
Chris@164 206 if (max) *max = 2;
Chris@245 207 if (deflt) *deflt = 1;
Chris@164 208 return m_resampleQuality;
Chris@164 209 }
Chris@164 210
Chris@277 211 if (name == "Omit Temporaries from Recent Files") {
Chris@277 212 if (deflt) *deflt = 1;
Chris@277 213 }
Chris@277 214
Chris@297 215 if (name == "Background Mode") {
Chris@297 216 if (min) *min = 0;
Chris@297 217 if (max) *max = 2;
Chris@297 218 if (deflt) *deflt = 0;
Chris@297 219 return int(m_backgroundMode);
Chris@297 220 }
Chris@297 221
Chris@383 222 if (name == "View Font Size") {
Chris@383 223 if (min) *min = 3;
Chris@383 224 if (max) *max = 48;
Chris@383 225 if (deflt) *deflt = int(QApplication::font().pointSize() * 0.9);
Chris@383 226 return int(m_viewFontSize);
Chris@383 227 }
Chris@383 228
Chris@383 229 if (name == "Show Splash Screen") {
Chris@383 230 if (deflt) *deflt = 1;
Chris@383 231 }
Chris@383 232
Chris@136 233 return 0;
Chris@136 234 }
Chris@136 235
Chris@136 236 QString
Chris@136 237 Preferences::getPropertyValueLabel(const PropertyName &name,
Chris@136 238 int value) const
Chris@136 239 {
Chris@138 240 if (name == "Property Box Layout") {
Chris@141 241 if (value == 0) return tr("Show boxes for all panes");
Chris@141 242 else return tr("Show box for current pane only");
Chris@138 243 }
Chris@140 244 if (name == "Window Type") {
Chris@140 245 switch (WindowType(value)) {
Chris@140 246 case RectangularWindow: return tr("Rectangular");
Chris@141 247 case BartlettWindow: return tr("Triangular");
Chris@140 248 case HammingWindow: return tr("Hamming");
Chris@383 249 case HanningWindow: return tr("Hann");
Chris@140 250 case BlackmanWindow: return tr("Blackman");
Chris@140 251 case GaussianWindow: return tr("Gaussian");
Chris@140 252 case ParzenWindow: return tr("Parzen");
Chris@142 253 case NuttallWindow: return tr("Nuttall");
Chris@142 254 case BlackmanHarrisWindow: return tr("Blackman-Harris");
Chris@140 255 }
Chris@140 256 }
Chris@164 257 if (name == "Resample Quality") {
Chris@164 258 switch (value) {
Chris@164 259 case 0: return tr("Fastest");
Chris@165 260 case 1: return tr("Standard");
Chris@164 261 case 2: return tr("Highest quality");
Chris@164 262 }
Chris@164 263 }
Chris@246 264 if (name == "Spectrogram Smoothing") {
Chris@246 265 switch (value) {
Chris@246 266 case NoSpectrogramSmoothing: return tr("None - blocky but accurate");
Chris@383 267 case SpectrogramInterpolated: return tr("Linear - fast but fuzzy");
Chris@383 268 case SpectrogramZeroPadded: return tr("4 x Oversampled - slow but clear");
Chris@246 269 }
Chris@246 270 }
Chris@297 271 if (name == "Background Mode") {
Chris@297 272 switch (value) {
Chris@297 273 case BackgroundFromTheme: return tr("Follow desktop theme");
Chris@297 274 case DarkBackground: return tr("Dark background");
Chris@297 275 case LightBackground: return tr("Light background");
Chris@297 276 }
Chris@297 277 }
Chris@246 278
Chris@136 279 return "";
Chris@136 280 }
Chris@136 281
Chris@136 282 QString
Chris@136 283 Preferences::getPropertyContainerName() const
Chris@136 284 {
Chris@136 285 return tr("Preferences");
Chris@136 286 }
Chris@136 287
Chris@136 288 QString
Chris@136 289 Preferences::getPropertyContainerIconName() const
Chris@136 290 {
Chris@136 291 return "preferences";
Chris@136 292 }
Chris@136 293
Chris@136 294 void
Chris@136 295 Preferences::setProperty(const PropertyName &name, int value)
Chris@136 296 {
Chris@246 297 if (name == "Spectrogram Smoothing") {
Chris@246 298 setSpectrogramSmoothing(SpectrogramSmoothing(value));
Chris@136 299 } else if (name == "Tuning Frequency") {
Chris@136 300 //!!!
Chris@138 301 } else if (name == "Property Box Layout") {
Chris@138 302 setPropertyBoxLayout(value == 0 ? VerticallyStacked : Layered);
Chris@140 303 } else if (name == "Window Type") {
Chris@140 304 setWindowType(WindowType(value));
Chris@164 305 } else if (name == "Resample Quality") {
Chris@164 306 setResampleQuality(value);
Chris@277 307 } else if (name == "Omit Temporaries from Recent Files") {
Chris@277 308 setOmitTempsFromRecentFiles(value ? true : false);
Chris@297 309 } else if (name == "Background Mode") {
Chris@297 310 setBackgroundMode(BackgroundMode(value));
Chris@383 311 } else if (name == "View Font Size") {
Chris@383 312 setViewFontSize(value);
Chris@383 313 } else if (name == "Show Splash Screen") {
Chris@383 314 setShowSplash(value ? true : false);
Chris@136 315 }
Chris@136 316 }
Chris@136 317
Chris@136 318 void
Chris@246 319 Preferences::setSpectrogramSmoothing(SpectrogramSmoothing smoothing)
Chris@136 320 {
Chris@246 321 if (m_spectrogramSmoothing != smoothing) {
Chris@246 322
Chris@246 323 // "smoothing" is one of those words that looks increasingly
Chris@246 324 // ridiculous the more you see it. Smoothing smoothing smoothing.
Chris@246 325 m_spectrogramSmoothing = smoothing;
Chris@246 326
Chris@156 327 QSettings settings;
Chris@156 328 settings.beginGroup("Preferences");
Chris@246 329 settings.setValue("spectrogram-smoothing", int(smoothing));
Chris@156 330 settings.endGroup();
Chris@246 331 emit propertyChanged("Spectrogram Smoothing");
Chris@138 332 }
Chris@136 333 }
Chris@136 334
Chris@136 335 void
Chris@136 336 Preferences::setTuningFrequency(float freq)
Chris@136 337 {
Chris@138 338 if (m_tuningFrequency != freq) {
Chris@138 339 m_tuningFrequency = freq;
Chris@156 340 QSettings settings;
Chris@156 341 settings.beginGroup("Preferences");
Chris@156 342 settings.setValue("tuning-frequency", freq);
Chris@156 343 settings.endGroup();
Chris@141 344 emit propertyChanged("Tuning Frequency");
Chris@138 345 }
Chris@136 346 }
Chris@136 347
Chris@138 348 void
Chris@138 349 Preferences::setPropertyBoxLayout(PropertyBoxLayout layout)
Chris@138 350 {
Chris@138 351 if (m_propertyBoxLayout != layout) {
Chris@138 352 m_propertyBoxLayout = layout;
Chris@156 353 QSettings settings;
Chris@156 354 settings.beginGroup("Preferences");
Chris@156 355 settings.setValue("property-box-layout", int(layout));
Chris@156 356 settings.endGroup();
Chris@141 357 emit propertyChanged("Property Box Layout");
Chris@138 358 }
Chris@138 359 }
Chris@138 360
Chris@140 361 void
Chris@140 362 Preferences::setWindowType(WindowType type)
Chris@140 363 {
Chris@140 364 if (m_windowType != type) {
Chris@140 365 m_windowType = type;
Chris@156 366 QSettings settings;
Chris@156 367 settings.beginGroup("Preferences");
Chris@156 368 settings.setValue("window-type", int(type));
Chris@156 369 settings.endGroup();
Chris@141 370 emit propertyChanged("Window Type");
Chris@140 371 }
Chris@140 372 }
Chris@140 373
Chris@164 374 void
Chris@164 375 Preferences::setResampleQuality(int q)
Chris@164 376 {
Chris@164 377 if (m_resampleQuality != q) {
Chris@164 378 m_resampleQuality = q;
Chris@164 379 QSettings settings;
Chris@164 380 settings.beginGroup("Preferences");
Chris@164 381 settings.setValue("resample-quality", q);
Chris@164 382 settings.endGroup();
Chris@164 383 emit propertyChanged("Resample Quality");
Chris@164 384 }
Chris@164 385 }
Chris@277 386
Chris@277 387 void
Chris@277 388 Preferences::setOmitTempsFromRecentFiles(bool omit)
Chris@277 389 {
Chris@277 390 if (m_omitRecentTemps != omit) {
Chris@277 391 m_omitRecentTemps = omit;
Chris@277 392 QSettings settings;
Chris@277 393 settings.beginGroup("Preferences");
Chris@277 394 settings.setValue("omit-recent-temporaries", omit);
Chris@277 395 settings.endGroup();
Chris@277 396 emit propertyChanged("Omit Temporaries from Recent Files");
Chris@277 397 }
Chris@277 398 }
Chris@297 399
Chris@297 400 void
Chris@297 401 Preferences::setTemporaryDirectoryRoot(QString root)
Chris@297 402 {
Chris@297 403 if (root == QDir::home().absolutePath()) {
Chris@297 404 root = "$HOME";
Chris@297 405 }
Chris@297 406 if (m_tempDirRoot != root) {
Chris@297 407 m_tempDirRoot = root;
Chris@297 408 QSettings settings;
Chris@297 409 settings.beginGroup("TempDirectory");
Chris@297 410 settings.setValue("create-in", root);
Chris@297 411 settings.endGroup();
Chris@297 412 emit propertyChanged("Temporary Directory Root");
Chris@297 413 }
Chris@297 414 }
Chris@297 415
Chris@297 416 void
Chris@297 417 Preferences::setResampleOnLoad(bool resample)
Chris@297 418 {
Chris@297 419 if (m_resampleOnLoad != resample) {
Chris@297 420 m_resampleOnLoad = resample;
Chris@297 421 QSettings settings;
Chris@297 422 settings.beginGroup("Preferences");
Chris@297 423 settings.setValue("resample-on-load", resample);
Chris@297 424 settings.endGroup();
Chris@297 425 emit propertyChanged("Resample On Load");
Chris@297 426 }
Chris@297 427 }
Chris@297 428
Chris@297 429 void
Chris@297 430 Preferences::setBackgroundMode(BackgroundMode mode)
Chris@297 431 {
Chris@297 432 if (m_backgroundMode != mode) {
Chris@297 433
Chris@297 434 m_backgroundMode = mode;
Chris@297 435
Chris@297 436 QSettings settings;
Chris@297 437 settings.beginGroup("Preferences");
Chris@297 438 settings.setValue("background-mode", int(mode));
Chris@297 439 settings.endGroup();
Chris@297 440 emit propertyChanged("Background Mode");
Chris@297 441 }
Chris@297 442 }
Chris@297 443
Chris@383 444 void
Chris@383 445 Preferences::setViewFontSize(int size)
Chris@383 446 {
Chris@383 447 if (m_viewFontSize != size) {
Chris@297 448
Chris@383 449 m_viewFontSize = size;
Chris@383 450
Chris@383 451 QSettings settings;
Chris@383 452 settings.beginGroup("Preferences");
Chris@383 453 settings.setValue("view-font-size", size);
Chris@383 454 settings.endGroup();
Chris@383 455 emit propertyChanged("View Font Size");
Chris@383 456 }
Chris@383 457 }
Chris@383 458
Chris@383 459 void
Chris@383 460 Preferences::setShowSplash(bool show)
Chris@383 461 {
Chris@383 462 if (m_showSplash != show) {
Chris@383 463
Chris@383 464 m_showSplash = show;
Chris@383 465
Chris@383 466 QSettings settings;
Chris@383 467 settings.beginGroup("Preferences");
Chris@383 468 settings.setValue("show-splash", show);
Chris@383 469 settings.endGroup();
Chris@383 470 emit propertyChanged("Show Splash Screen");
Chris@383 471 }
Chris@383 472 }
Chris@383 473