annotate base/Preferences.cpp @ 158:74abef65711b

* Fixes pulled across from Rosegarden
author Chris Cannam
date Wed, 16 Aug 2006 14:57:22 +0000
parents 059b0322009c
children da3701a3953e
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@136 7 This file copyright 2006 Chris Cannam.
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@137 38 m_smoothSpectrogram(true),
Chris@140 39 m_tuningFrequency(440),
Chris@140 40 m_propertyBoxLayout(VerticallyStacked),
Chris@156 41 m_windowType(HanningWindow)
Chris@136 42 {
Chris@156 43 QSettings settings;
Chris@156 44 settings.beginGroup("Preferences");
Chris@156 45 m_smoothSpectrogram = settings.value("smooth-spectrogram", true).toBool();
Chris@156 46 m_tuningFrequency = settings.value("tuning-frequency", 440.f).toDouble();
Chris@145 47 m_propertyBoxLayout = PropertyBoxLayout
Chris@156 48 (settings.value("property-box-layout", int(VerticallyStacked)).toInt());
Chris@145 49 m_windowType = WindowType
Chris@156 50 (settings.value("window-type", int(HanningWindow)).toInt());
Chris@156 51 settings.endGroup();
Chris@145 52 }
Chris@145 53
Chris@145 54 Preferences::~Preferences()
Chris@145 55 {
Chris@136 56 }
Chris@136 57
Chris@136 58 Preferences::PropertyList
Chris@136 59 Preferences::getProperties() const
Chris@136 60 {
Chris@136 61 PropertyList props;
Chris@136 62 props.push_back("Smooth Spectrogram");
Chris@136 63 props.push_back("Tuning Frequency");
Chris@138 64 props.push_back("Property Box Layout");
Chris@140 65 props.push_back("Window Type");
Chris@136 66 return props;
Chris@136 67 }
Chris@136 68
Chris@136 69 QString
Chris@136 70 Preferences::getPropertyLabel(const PropertyName &name) const
Chris@136 71 {
Chris@136 72 if (name == "Smooth Spectrogram") {
Chris@141 73 return tr("Smooth spectrogram display by zero padding FFT");
Chris@136 74 }
Chris@136 75 if (name == "Tuning Frequency") {
Chris@141 76 return tr("Frequency of concert A");
Chris@136 77 }
Chris@138 78 if (name == "Property Box Layout") {
Chris@141 79 return tr("Property box layout");
Chris@138 80 }
Chris@140 81 if (name == "Window Type") {
Chris@141 82 return tr("Spectral analysis window shape");
Chris@140 83 }
Chris@136 84 return name;
Chris@136 85 }
Chris@136 86
Chris@136 87 Preferences::PropertyType
Chris@136 88 Preferences::getPropertyType(const PropertyName &name) const
Chris@136 89 {
Chris@136 90 if (name == "Smooth Spectrogram") {
Chris@136 91 return ToggleProperty;
Chris@136 92 }
Chris@136 93 if (name == "Tuning Frequency") {
Chris@136 94 return RangeProperty;
Chris@136 95 }
Chris@138 96 if (name == "Property Box Layout") {
Chris@138 97 return ValueProperty;
Chris@138 98 }
Chris@140 99 if (name == "Window Type") {
Chris@140 100 return ValueProperty;
Chris@140 101 }
Chris@136 102 return InvalidProperty;
Chris@136 103 }
Chris@136 104
Chris@136 105 int
Chris@136 106 Preferences::getPropertyRangeAndValue(const PropertyName &name,
Chris@136 107 int *min, int *max) const
Chris@136 108 {
Chris@136 109 if (name == "Smooth Spectrogram") {
Chris@136 110 if (min) *min = 0;
Chris@136 111 if (max) *max = 1;
Chris@136 112 return m_smoothSpectrogram ? 1 : 0;
Chris@136 113 }
Chris@136 114
Chris@136 115 //!!! freq mapping
Chris@136 116
Chris@138 117 if (name == "Property Box Layout") {
Chris@138 118 if (min) *min = 0;
Chris@138 119 if (max) *max = 1;
Chris@138 120 return m_propertyBoxLayout == Layered ? 1 : 0;
Chris@138 121 }
Chris@138 122
Chris@140 123 if (name == "Window Type") {
Chris@140 124 if (min) *min = int(RectangularWindow);
Chris@142 125 if (max) *max = int(BlackmanHarrisWindow);
Chris@140 126 return int(m_windowType);
Chris@140 127 }
Chris@140 128
Chris@136 129 return 0;
Chris@136 130 }
Chris@136 131
Chris@136 132 QString
Chris@136 133 Preferences::getPropertyValueLabel(const PropertyName &name,
Chris@136 134 int value) const
Chris@136 135 {
Chris@138 136 if (name == "Property Box Layout") {
Chris@141 137 if (value == 0) return tr("Show boxes for all panes");
Chris@141 138 else return tr("Show box for current pane only");
Chris@138 139 }
Chris@140 140 if (name == "Window Type") {
Chris@140 141 switch (WindowType(value)) {
Chris@140 142 case RectangularWindow: return tr("Rectangular");
Chris@141 143 case BartlettWindow: return tr("Triangular");
Chris@140 144 case HammingWindow: return tr("Hamming");
Chris@140 145 case HanningWindow: return tr("Hanning");
Chris@140 146 case BlackmanWindow: return tr("Blackman");
Chris@140 147 case GaussianWindow: return tr("Gaussian");
Chris@140 148 case ParzenWindow: return tr("Parzen");
Chris@142 149 case NuttallWindow: return tr("Nuttall");
Chris@142 150 case BlackmanHarrisWindow: return tr("Blackman-Harris");
Chris@140 151 }
Chris@140 152 }
Chris@136 153 return "";
Chris@136 154 }
Chris@136 155
Chris@136 156 QString
Chris@136 157 Preferences::getPropertyContainerName() const
Chris@136 158 {
Chris@136 159 return tr("Preferences");
Chris@136 160 }
Chris@136 161
Chris@136 162 QString
Chris@136 163 Preferences::getPropertyContainerIconName() const
Chris@136 164 {
Chris@136 165 return "preferences";
Chris@136 166 }
Chris@136 167
Chris@136 168 void
Chris@136 169 Preferences::setProperty(const PropertyName &name, int value)
Chris@136 170 {
Chris@136 171 if (name == "Smooth Spectrogram") {
Chris@136 172 setSmoothSpectrogram(value > 0.1);
Chris@136 173 } else if (name == "Tuning Frequency") {
Chris@136 174 //!!!
Chris@138 175 } else if (name == "Property Box Layout") {
Chris@138 176 setPropertyBoxLayout(value == 0 ? VerticallyStacked : Layered);
Chris@140 177 } else if (name == "Window Type") {
Chris@140 178 setWindowType(WindowType(value));
Chris@136 179 }
Chris@136 180 }
Chris@136 181
Chris@136 182 void
Chris@136 183 Preferences::setSmoothSpectrogram(bool smooth)
Chris@136 184 {
Chris@138 185 if (m_smoothSpectrogram != smooth) {
Chris@138 186 m_smoothSpectrogram = smooth;
Chris@156 187 QSettings settings;
Chris@156 188 settings.beginGroup("Preferences");
Chris@156 189 settings.setValue("smooth-spectrogram", smooth);
Chris@156 190 settings.endGroup();
Chris@141 191 emit propertyChanged("Smooth Spectrogram");
Chris@138 192 }
Chris@136 193 }
Chris@136 194
Chris@136 195 void
Chris@136 196 Preferences::setTuningFrequency(float freq)
Chris@136 197 {
Chris@138 198 if (m_tuningFrequency != freq) {
Chris@138 199 m_tuningFrequency = freq;
Chris@156 200 QSettings settings;
Chris@156 201 settings.beginGroup("Preferences");
Chris@156 202 settings.setValue("tuning-frequency", freq);
Chris@156 203 settings.endGroup();
Chris@141 204 emit propertyChanged("Tuning Frequency");
Chris@138 205 }
Chris@136 206 }
Chris@136 207
Chris@138 208 void
Chris@138 209 Preferences::setPropertyBoxLayout(PropertyBoxLayout layout)
Chris@138 210 {
Chris@138 211 if (m_propertyBoxLayout != layout) {
Chris@138 212 m_propertyBoxLayout = layout;
Chris@156 213 QSettings settings;
Chris@156 214 settings.beginGroup("Preferences");
Chris@156 215 settings.setValue("property-box-layout", int(layout));
Chris@156 216 settings.endGroup();
Chris@141 217 emit propertyChanged("Property Box Layout");
Chris@138 218 }
Chris@138 219 }
Chris@138 220
Chris@140 221 void
Chris@140 222 Preferences::setWindowType(WindowType type)
Chris@140 223 {
Chris@140 224 if (m_windowType != type) {
Chris@140 225 m_windowType = type;
Chris@156 226 QSettings settings;
Chris@156 227 settings.beginGroup("Preferences");
Chris@156 228 settings.setValue("window-type", int(type));
Chris@156 229 settings.endGroup();
Chris@141 230 emit propertyChanged("Window Type");
Chris@140 231 }
Chris@140 232 }
Chris@140 233