annotate base/Preferences.cpp @ 350:d7c41483af8f

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