annotate base/Preferences.cpp @ 359:824ee993ca8d

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