annotate base/Preferences.cpp @ 370:e73a1a1e8f24

* more reference frame records
author Chris Cannam
date Mon, 04 Feb 2008 16:48:52 +0000
parents 722bc705775a
children d31b4ccb7ddb
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@354 26 #include <QApplication>
Chris@354 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@354 47 m_resampleOnLoad(false),
Chris@354 48 m_viewFontSize(10),
Chris@354 49 m_backgroundMode(BackgroundFromTheme)
Chris@136 50 {
Chris@156 51 QSettings settings;
Chris@156 52 settings.beginGroup("Preferences");
Chris@246 53 m_spectrogramSmoothing = SpectrogramSmoothing
Chris@246 54 (settings.value("spectrogram-smoothing", int(m_spectrogramSmoothing)).toInt());
Chris@156 55 m_tuningFrequency = settings.value("tuning-frequency", 440.f).toDouble();
Chris@145 56 m_propertyBoxLayout = PropertyBoxLayout
Chris@156 57 (settings.value("property-box-layout", int(VerticallyStacked)).toInt());
Chris@145 58 m_windowType = WindowType
Chris@156 59 (settings.value("window-type", int(HanningWindow)).toInt());
Chris@164 60 m_resampleQuality = settings.value("resample-quality", 1).toInt();
Chris@304 61 m_resampleOnLoad = settings.value("resample-on-load", false).toBool();
Chris@297 62 m_backgroundMode = BackgroundMode
Chris@297 63 (settings.value("background-mode", int(BackgroundFromTheme)).toInt());
Chris@354 64 m_viewFontSize = settings.value
Chris@367 65 ("view-font-size", int(QApplication::font().pointSize() * 0.9))
Chris@367 66 .toInt();
Chris@297 67 settings.endGroup();
Chris@297 68
Chris@297 69 settings.beginGroup("TempDirectory");
Chris@297 70 m_tempDirRoot = settings.value("create-in", "$HOME").toString();
Chris@156 71 settings.endGroup();
Chris@145 72 }
Chris@145 73
Chris@145 74 Preferences::~Preferences()
Chris@145 75 {
Chris@136 76 }
Chris@136 77
Chris@136 78 Preferences::PropertyList
Chris@136 79 Preferences::getProperties() const
Chris@136 80 {
Chris@136 81 PropertyList props;
Chris@246 82 props.push_back("Spectrogram Smoothing");
Chris@136 83 props.push_back("Tuning Frequency");
Chris@138 84 props.push_back("Property Box Layout");
Chris@140 85 props.push_back("Window Type");
Chris@164 86 props.push_back("Resample Quality");
Chris@277 87 props.push_back("Omit Temporaries from Recent Files");
Chris@297 88 props.push_back("Resample On Load");
Chris@297 89 props.push_back("Temporary Directory Root");
Chris@297 90 props.push_back("Background Mode");
Chris@354 91 props.push_back("View Font Size");
Chris@136 92 return props;
Chris@136 93 }
Chris@136 94
Chris@136 95 QString
Chris@136 96 Preferences::getPropertyLabel(const PropertyName &name) const
Chris@136 97 {
Chris@246 98 if (name == "Spectrogram Smoothing") {
Chris@246 99 return tr("Spectrogram y-axis smoothing:");
Chris@136 100 }
Chris@136 101 if (name == "Tuning Frequency") {
Chris@141 102 return tr("Frequency of concert A");
Chris@136 103 }
Chris@138 104 if (name == "Property Box Layout") {
Chris@141 105 return tr("Property box layout");
Chris@138 106 }
Chris@140 107 if (name == "Window Type") {
Chris@141 108 return tr("Spectral analysis window shape");
Chris@140 109 }
Chris@164 110 if (name == "Resample Quality") {
Chris@165 111 return tr("Playback resampler type");
Chris@164 112 }
Chris@277 113 if (name == "Omit Temporaries from Recent Files") {
Chris@297 114 return tr("Omit temporaries from Recent Files menu");
Chris@297 115 }
Chris@297 116 if (name == "Resample On Load") {
Chris@297 117 return tr("Resample mismatching files on import");
Chris@297 118 }
Chris@297 119 if (name == "Temporary Directory Root") {
Chris@297 120 return tr("Location for cache file directory");
Chris@297 121 }
Chris@297 122 if (name == "Background Mode") {
Chris@297 123 return tr("Background colour preference");
Chris@277 124 }
Chris@354 125 if (name == "View Font Size") {
Chris@354 126 return tr("Font size for text overlays");
Chris@354 127 }
Chris@136 128 return name;
Chris@136 129 }
Chris@136 130
Chris@136 131 Preferences::PropertyType
Chris@136 132 Preferences::getPropertyType(const PropertyName &name) const
Chris@136 133 {
Chris@246 134 if (name == "Spectrogram Smoothing") {
Chris@246 135 return ValueProperty;
Chris@136 136 }
Chris@136 137 if (name == "Tuning Frequency") {
Chris@136 138 return RangeProperty;
Chris@136 139 }
Chris@138 140 if (name == "Property Box Layout") {
Chris@138 141 return ValueProperty;
Chris@138 142 }
Chris@140 143 if (name == "Window Type") {
Chris@140 144 return ValueProperty;
Chris@140 145 }
Chris@164 146 if (name == "Resample Quality") {
Chris@164 147 return ValueProperty;
Chris@164 148 }
Chris@277 149 if (name == "Omit Temporaries from Recent Files") {
Chris@277 150 return ToggleProperty;
Chris@277 151 }
Chris@297 152 if (name == "Resample On Load") {
Chris@297 153 return ToggleProperty;
Chris@297 154 }
Chris@297 155 if (name == "Temporary Directory Root") {
Chris@297 156 // It's an arbitrary string, we don't have a set of values for this
Chris@297 157 return InvalidProperty;
Chris@297 158 }
Chris@297 159 if (name == "Background Mode") {
Chris@297 160 return ValueProperty;
Chris@297 161 }
Chris@354 162 if (name == "View Font Size") {
Chris@354 163 return RangeProperty;
Chris@354 164 }
Chris@136 165 return InvalidProperty;
Chris@136 166 }
Chris@136 167
Chris@136 168 int
Chris@136 169 Preferences::getPropertyRangeAndValue(const PropertyName &name,
Chris@245 170 int *min, int *max, int *deflt) const
Chris@136 171 {
Chris@246 172 if (name == "Spectrogram Smoothing") {
Chris@136 173 if (min) *min = 0;
Chris@246 174 if (max) *max = 2;
Chris@246 175 if (deflt) *deflt = int(SpectrogramZeroPadded);
Chris@246 176 return int(m_spectrogramSmoothing);
Chris@136 177 }
Chris@136 178
Chris@136 179 //!!! freq mapping
Chris@136 180
Chris@138 181 if (name == "Property Box Layout") {
Chris@138 182 if (min) *min = 0;
Chris@138 183 if (max) *max = 1;
Chris@245 184 if (deflt) *deflt = 0;
Chris@138 185 return m_propertyBoxLayout == Layered ? 1 : 0;
Chris@138 186 }
Chris@138 187
Chris@140 188 if (name == "Window Type") {
Chris@140 189 if (min) *min = int(RectangularWindow);
Chris@142 190 if (max) *max = int(BlackmanHarrisWindow);
Chris@245 191 if (deflt) *deflt = int(HanningWindow);
Chris@140 192 return int(m_windowType);
Chris@140 193 }
Chris@140 194
Chris@164 195 if (name == "Resample Quality") {
Chris@164 196 if (min) *min = 0;
Chris@164 197 if (max) *max = 2;
Chris@245 198 if (deflt) *deflt = 1;
Chris@164 199 return m_resampleQuality;
Chris@164 200 }
Chris@164 201
Chris@277 202 if (name == "Omit Temporaries from Recent Files") {
Chris@277 203 if (deflt) *deflt = 1;
Chris@277 204 }
Chris@277 205
Chris@297 206 if (name == "Background Mode") {
Chris@297 207 if (min) *min = 0;
Chris@297 208 if (max) *max = 2;
Chris@297 209 if (deflt) *deflt = 0;
Chris@297 210 return int(m_backgroundMode);
Chris@297 211 }
Chris@297 212
Chris@354 213 if (name == "View Font Size") {
Chris@354 214 if (min) *min = 3;
Chris@354 215 if (max) *max = 48;
Chris@354 216 if (deflt) *deflt = int(QApplication::font().pointSize() * 0.9);
Chris@354 217 return int(m_viewFontSize);
Chris@354 218 }
Chris@354 219
Chris@136 220 return 0;
Chris@136 221 }
Chris@136 222
Chris@136 223 QString
Chris@136 224 Preferences::getPropertyValueLabel(const PropertyName &name,
Chris@136 225 int value) const
Chris@136 226 {
Chris@138 227 if (name == "Property Box Layout") {
Chris@141 228 if (value == 0) return tr("Show boxes for all panes");
Chris@141 229 else return tr("Show box for current pane only");
Chris@138 230 }
Chris@140 231 if (name == "Window Type") {
Chris@140 232 switch (WindowType(value)) {
Chris@140 233 case RectangularWindow: return tr("Rectangular");
Chris@141 234 case BartlettWindow: return tr("Triangular");
Chris@140 235 case HammingWindow: return tr("Hamming");
Chris@140 236 case HanningWindow: return tr("Hanning");
Chris@140 237 case BlackmanWindow: return tr("Blackman");
Chris@140 238 case GaussianWindow: return tr("Gaussian");
Chris@140 239 case ParzenWindow: return tr("Parzen");
Chris@142 240 case NuttallWindow: return tr("Nuttall");
Chris@142 241 case BlackmanHarrisWindow: return tr("Blackman-Harris");
Chris@140 242 }
Chris@140 243 }
Chris@164 244 if (name == "Resample Quality") {
Chris@164 245 switch (value) {
Chris@164 246 case 0: return tr("Fastest");
Chris@165 247 case 1: return tr("Standard");
Chris@164 248 case 2: return tr("Highest quality");
Chris@164 249 }
Chris@164 250 }
Chris@246 251 if (name == "Spectrogram Smoothing") {
Chris@246 252 switch (value) {
Chris@246 253 case NoSpectrogramSmoothing: return tr("None - blocky but accurate");
Chris@246 254 case SpectrogramInterpolated: return tr("Interpolate - fast but fuzzy");
Chris@246 255 case SpectrogramZeroPadded: return tr("Zero pad FFT - slow but clear");
Chris@246 256 }
Chris@246 257 }
Chris@297 258 if (name == "Background Mode") {
Chris@297 259 switch (value) {
Chris@297 260 case BackgroundFromTheme: return tr("Follow desktop theme");
Chris@297 261 case DarkBackground: return tr("Dark background");
Chris@297 262 case LightBackground: return tr("Light background");
Chris@297 263 }
Chris@297 264 }
Chris@246 265
Chris@136 266 return "";
Chris@136 267 }
Chris@136 268
Chris@136 269 QString
Chris@136 270 Preferences::getPropertyContainerName() const
Chris@136 271 {
Chris@136 272 return tr("Preferences");
Chris@136 273 }
Chris@136 274
Chris@136 275 QString
Chris@136 276 Preferences::getPropertyContainerIconName() const
Chris@136 277 {
Chris@136 278 return "preferences";
Chris@136 279 }
Chris@136 280
Chris@136 281 void
Chris@136 282 Preferences::setProperty(const PropertyName &name, int value)
Chris@136 283 {
Chris@246 284 if (name == "Spectrogram Smoothing") {
Chris@246 285 setSpectrogramSmoothing(SpectrogramSmoothing(value));
Chris@136 286 } else if (name == "Tuning Frequency") {
Chris@136 287 //!!!
Chris@138 288 } else if (name == "Property Box Layout") {
Chris@138 289 setPropertyBoxLayout(value == 0 ? VerticallyStacked : Layered);
Chris@140 290 } else if (name == "Window Type") {
Chris@140 291 setWindowType(WindowType(value));
Chris@164 292 } else if (name == "Resample Quality") {
Chris@164 293 setResampleQuality(value);
Chris@277 294 } else if (name == "Omit Temporaries from Recent Files") {
Chris@277 295 setOmitTempsFromRecentFiles(value ? true : false);
Chris@297 296 } else if (name == "Background Mode") {
Chris@297 297 setBackgroundMode(BackgroundMode(value));
Chris@354 298 } else if (name == "View Font Size") {
Chris@354 299 setViewFontSize(value);
Chris@136 300 }
Chris@136 301 }
Chris@136 302
Chris@136 303 void
Chris@246 304 Preferences::setSpectrogramSmoothing(SpectrogramSmoothing smoothing)
Chris@136 305 {
Chris@246 306 if (m_spectrogramSmoothing != smoothing) {
Chris@246 307
Chris@246 308 // "smoothing" is one of those words that looks increasingly
Chris@246 309 // ridiculous the more you see it. Smoothing smoothing smoothing.
Chris@246 310 m_spectrogramSmoothing = smoothing;
Chris@246 311
Chris@156 312 QSettings settings;
Chris@156 313 settings.beginGroup("Preferences");
Chris@246 314 settings.setValue("spectrogram-smoothing", int(smoothing));
Chris@156 315 settings.endGroup();
Chris@246 316 emit propertyChanged("Spectrogram Smoothing");
Chris@138 317 }
Chris@136 318 }
Chris@136 319
Chris@136 320 void
Chris@136 321 Preferences::setTuningFrequency(float freq)
Chris@136 322 {
Chris@138 323 if (m_tuningFrequency != freq) {
Chris@138 324 m_tuningFrequency = freq;
Chris@156 325 QSettings settings;
Chris@156 326 settings.beginGroup("Preferences");
Chris@156 327 settings.setValue("tuning-frequency", freq);
Chris@156 328 settings.endGroup();
Chris@141 329 emit propertyChanged("Tuning Frequency");
Chris@138 330 }
Chris@136 331 }
Chris@136 332
Chris@138 333 void
Chris@138 334 Preferences::setPropertyBoxLayout(PropertyBoxLayout layout)
Chris@138 335 {
Chris@138 336 if (m_propertyBoxLayout != layout) {
Chris@138 337 m_propertyBoxLayout = layout;
Chris@156 338 QSettings settings;
Chris@156 339 settings.beginGroup("Preferences");
Chris@156 340 settings.setValue("property-box-layout", int(layout));
Chris@156 341 settings.endGroup();
Chris@141 342 emit propertyChanged("Property Box Layout");
Chris@138 343 }
Chris@138 344 }
Chris@138 345
Chris@140 346 void
Chris@140 347 Preferences::setWindowType(WindowType type)
Chris@140 348 {
Chris@140 349 if (m_windowType != type) {
Chris@140 350 m_windowType = type;
Chris@156 351 QSettings settings;
Chris@156 352 settings.beginGroup("Preferences");
Chris@156 353 settings.setValue("window-type", int(type));
Chris@156 354 settings.endGroup();
Chris@141 355 emit propertyChanged("Window Type");
Chris@140 356 }
Chris@140 357 }
Chris@140 358
Chris@164 359 void
Chris@164 360 Preferences::setResampleQuality(int q)
Chris@164 361 {
Chris@164 362 if (m_resampleQuality != q) {
Chris@164 363 m_resampleQuality = q;
Chris@164 364 QSettings settings;
Chris@164 365 settings.beginGroup("Preferences");
Chris@164 366 settings.setValue("resample-quality", q);
Chris@164 367 settings.endGroup();
Chris@164 368 emit propertyChanged("Resample Quality");
Chris@164 369 }
Chris@164 370 }
Chris@277 371
Chris@277 372 void
Chris@277 373 Preferences::setOmitTempsFromRecentFiles(bool omit)
Chris@277 374 {
Chris@277 375 if (m_omitRecentTemps != omit) {
Chris@277 376 m_omitRecentTemps = omit;
Chris@277 377 QSettings settings;
Chris@277 378 settings.beginGroup("Preferences");
Chris@277 379 settings.setValue("omit-recent-temporaries", omit);
Chris@277 380 settings.endGroup();
Chris@277 381 emit propertyChanged("Omit Temporaries from Recent Files");
Chris@277 382 }
Chris@277 383 }
Chris@297 384
Chris@297 385 void
Chris@297 386 Preferences::setTemporaryDirectoryRoot(QString root)
Chris@297 387 {
Chris@297 388 if (root == QDir::home().absolutePath()) {
Chris@297 389 root = "$HOME";
Chris@297 390 }
Chris@297 391 if (m_tempDirRoot != root) {
Chris@297 392 m_tempDirRoot = root;
Chris@297 393 QSettings settings;
Chris@297 394 settings.beginGroup("TempDirectory");
Chris@297 395 settings.setValue("create-in", root);
Chris@297 396 settings.endGroup();
Chris@297 397 emit propertyChanged("Temporary Directory Root");
Chris@297 398 }
Chris@297 399 }
Chris@297 400
Chris@297 401 void
Chris@297 402 Preferences::setResampleOnLoad(bool resample)
Chris@297 403 {
Chris@297 404 if (m_resampleOnLoad != resample) {
Chris@297 405 m_resampleOnLoad = resample;
Chris@297 406 QSettings settings;
Chris@297 407 settings.beginGroup("Preferences");
Chris@297 408 settings.setValue("resample-on-load", resample);
Chris@297 409 settings.endGroup();
Chris@297 410 emit propertyChanged("Resample On Load");
Chris@297 411 }
Chris@297 412 }
Chris@297 413
Chris@297 414 void
Chris@297 415 Preferences::setBackgroundMode(BackgroundMode mode)
Chris@297 416 {
Chris@297 417 if (m_backgroundMode != mode) {
Chris@297 418
Chris@297 419 m_backgroundMode = mode;
Chris@297 420
Chris@297 421 QSettings settings;
Chris@297 422 settings.beginGroup("Preferences");
Chris@297 423 settings.setValue("background-mode", int(mode));
Chris@297 424 settings.endGroup();
Chris@297 425 emit propertyChanged("Background Mode");
Chris@297 426 }
Chris@297 427 }
Chris@297 428
Chris@354 429 void
Chris@354 430 Preferences::setViewFontSize(int size)
Chris@354 431 {
Chris@354 432 if (m_viewFontSize != size) {
Chris@297 433
Chris@354 434 m_viewFontSize = size;
Chris@354 435
Chris@354 436 QSettings settings;
Chris@354 437 settings.beginGroup("Preferences");
Chris@354 438 settings.setValue("view-font-size", size);
Chris@354 439 settings.endGroup();
Chris@354 440 emit propertyChanged("View Font Size");
Chris@354 441 }
Chris@354 442 }
Chris@354 443