annotate base/Preferences.cpp @ 145:82f529a08cf3

* Change preferences dialog to ok/apply/cancel model * Make preferences persist in a config file * Change instance() to getInstance() for all singleton types * Make pasting to time-value layer with no values in clipboard ask you how to generate the values * Fix bad behaviour caused by importing "data"-type (i.e. 3d dense) model from annotation file without a fixed window size available
author Chris Cannam
date Thu, 27 Jul 2006 16:06:32 +0000
parents 0ba66b160a02
children 4b2ea82fd0ed
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 "fileio/ConfigFile.h" //!!! reorg
Chris@145 23
Chris@145 24 #include <QDir>
Chris@145 25 #include <QFileInfo>
Chris@145 26
Chris@136 27 Preferences *
Chris@136 28 Preferences::m_instance = new Preferences();
Chris@136 29
Chris@136 30 Preferences::Preferences() :
Chris@137 31 m_smoothSpectrogram(true),
Chris@140 32 m_tuningFrequency(440),
Chris@140 33 m_propertyBoxLayout(VerticallyStacked),
Chris@145 34 m_windowType(HanningWindow),
Chris@145 35 m_configFile(0)
Chris@136 36 {
Chris@145 37 // Let TempDirectory do its thing first, so as to avoid any race
Chris@145 38 // condition if it happens that we both want to use the same directory
Chris@145 39 TempDirectory::getInstance();
Chris@145 40
Chris@145 41 QString svDirBase = ".sv1";
Chris@145 42 QString svDir = QDir::home().filePath(svDirBase);
Chris@145 43 if (!QFileInfo(svDir).exists()) {
Chris@145 44 if (!QDir::home().mkdir(svDirBase)) {
Chris@145 45 throw DirectoryCreationFailed(QString("%1 directory in $HOME")
Chris@145 46 .arg(svDirBase));
Chris@145 47 }
Chris@145 48 } else if (!QFileInfo(svDir).isDir()) {
Chris@145 49 throw DirectoryCreationFailed(QString("$HOME/%1 is not a directory")
Chris@145 50 .arg(svDirBase));
Chris@145 51 }
Chris@145 52
Chris@145 53 m_configFile = new ConfigFile(QDir(svDir).filePath("preferences.cfg"));
Chris@145 54
Chris@145 55 m_smoothSpectrogram =
Chris@145 56 m_configFile->getBool("preferences-smooth-spectrogram", true);
Chris@145 57 m_tuningFrequency =
Chris@145 58 m_configFile->getFloat("preferences-tuning-frequency", 440.f);
Chris@145 59 m_propertyBoxLayout = PropertyBoxLayout
Chris@145 60 (m_configFile->getInt("preferences-property-box-layout",
Chris@145 61 int(VerticallyStacked)));
Chris@145 62 m_windowType = WindowType
Chris@145 63 (m_configFile->getInt("preferences-window-type", int(HanningWindow)));
Chris@145 64 }
Chris@145 65
Chris@145 66 Preferences::~Preferences()
Chris@145 67 {
Chris@145 68 delete m_configFile;
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@136 75 props.push_back("Smooth Spectrogram");
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@136 79 return props;
Chris@136 80 }
Chris@136 81
Chris@136 82 QString
Chris@136 83 Preferences::getPropertyLabel(const PropertyName &name) const
Chris@136 84 {
Chris@136 85 if (name == "Smooth Spectrogram") {
Chris@141 86 return tr("Smooth spectrogram display by zero padding FFT");
Chris@136 87 }
Chris@136 88 if (name == "Tuning Frequency") {
Chris@141 89 return tr("Frequency of concert A");
Chris@136 90 }
Chris@138 91 if (name == "Property Box Layout") {
Chris@141 92 return tr("Property box layout");
Chris@138 93 }
Chris@140 94 if (name == "Window Type") {
Chris@141 95 return tr("Spectral analysis window shape");
Chris@140 96 }
Chris@136 97 return name;
Chris@136 98 }
Chris@136 99
Chris@136 100 Preferences::PropertyType
Chris@136 101 Preferences::getPropertyType(const PropertyName &name) const
Chris@136 102 {
Chris@136 103 if (name == "Smooth Spectrogram") {
Chris@136 104 return ToggleProperty;
Chris@136 105 }
Chris@136 106 if (name == "Tuning Frequency") {
Chris@136 107 return RangeProperty;
Chris@136 108 }
Chris@138 109 if (name == "Property Box Layout") {
Chris@138 110 return ValueProperty;
Chris@138 111 }
Chris@140 112 if (name == "Window Type") {
Chris@140 113 return ValueProperty;
Chris@140 114 }
Chris@136 115 return InvalidProperty;
Chris@136 116 }
Chris@136 117
Chris@136 118 int
Chris@136 119 Preferences::getPropertyRangeAndValue(const PropertyName &name,
Chris@136 120 int *min, int *max) const
Chris@136 121 {
Chris@136 122 if (name == "Smooth Spectrogram") {
Chris@136 123 if (min) *min = 0;
Chris@136 124 if (max) *max = 1;
Chris@136 125 return m_smoothSpectrogram ? 1 : 0;
Chris@136 126 }
Chris@136 127
Chris@136 128 //!!! freq mapping
Chris@136 129
Chris@138 130 if (name == "Property Box Layout") {
Chris@138 131 if (min) *min = 0;
Chris@138 132 if (max) *max = 1;
Chris@138 133 return m_propertyBoxLayout == Layered ? 1 : 0;
Chris@138 134 }
Chris@138 135
Chris@140 136 if (name == "Window Type") {
Chris@140 137 if (min) *min = int(RectangularWindow);
Chris@142 138 if (max) *max = int(BlackmanHarrisWindow);
Chris@140 139 return int(m_windowType);
Chris@140 140 }
Chris@140 141
Chris@136 142 return 0;
Chris@136 143 }
Chris@136 144
Chris@136 145 QString
Chris@136 146 Preferences::getPropertyValueLabel(const PropertyName &name,
Chris@136 147 int value) const
Chris@136 148 {
Chris@138 149 if (name == "Property Box Layout") {
Chris@141 150 if (value == 0) return tr("Show boxes for all panes");
Chris@141 151 else return tr("Show box for current pane only");
Chris@138 152 }
Chris@140 153 if (name == "Window Type") {
Chris@140 154 switch (WindowType(value)) {
Chris@140 155 case RectangularWindow: return tr("Rectangular");
Chris@141 156 case BartlettWindow: return tr("Triangular");
Chris@140 157 case HammingWindow: return tr("Hamming");
Chris@140 158 case HanningWindow: return tr("Hanning");
Chris@140 159 case BlackmanWindow: return tr("Blackman");
Chris@140 160 case GaussianWindow: return tr("Gaussian");
Chris@140 161 case ParzenWindow: return tr("Parzen");
Chris@142 162 case NuttallWindow: return tr("Nuttall");
Chris@142 163 case BlackmanHarrisWindow: return tr("Blackman-Harris");
Chris@140 164 }
Chris@140 165 }
Chris@136 166 return "";
Chris@136 167 }
Chris@136 168
Chris@136 169 QString
Chris@136 170 Preferences::getPropertyContainerName() const
Chris@136 171 {
Chris@136 172 return tr("Preferences");
Chris@136 173 }
Chris@136 174
Chris@136 175 QString
Chris@136 176 Preferences::getPropertyContainerIconName() const
Chris@136 177 {
Chris@136 178 return "preferences";
Chris@136 179 }
Chris@136 180
Chris@136 181 void
Chris@136 182 Preferences::setProperty(const PropertyName &name, int value)
Chris@136 183 {
Chris@136 184 if (name == "Smooth Spectrogram") {
Chris@136 185 setSmoothSpectrogram(value > 0.1);
Chris@136 186 } else if (name == "Tuning Frequency") {
Chris@136 187 //!!!
Chris@138 188 } else if (name == "Property Box Layout") {
Chris@138 189 setPropertyBoxLayout(value == 0 ? VerticallyStacked : Layered);
Chris@140 190 } else if (name == "Window Type") {
Chris@140 191 setWindowType(WindowType(value));
Chris@136 192 }
Chris@136 193 }
Chris@136 194
Chris@136 195 void
Chris@136 196 Preferences::setSmoothSpectrogram(bool smooth)
Chris@136 197 {
Chris@138 198 if (m_smoothSpectrogram != smooth) {
Chris@138 199 m_smoothSpectrogram = smooth;
Chris@145 200 m_configFile->set("preferences-smooth-spectrogram", smooth);
Chris@141 201 emit propertyChanged("Smooth Spectrogram");
Chris@138 202 }
Chris@136 203 }
Chris@136 204
Chris@136 205 void
Chris@136 206 Preferences::setTuningFrequency(float freq)
Chris@136 207 {
Chris@138 208 if (m_tuningFrequency != freq) {
Chris@138 209 m_tuningFrequency = freq;
Chris@145 210 m_configFile->set("preferences-tuning-frequency", freq);
Chris@141 211 emit propertyChanged("Tuning Frequency");
Chris@138 212 }
Chris@136 213 }
Chris@136 214
Chris@138 215 void
Chris@138 216 Preferences::setPropertyBoxLayout(PropertyBoxLayout layout)
Chris@138 217 {
Chris@138 218 if (m_propertyBoxLayout != layout) {
Chris@138 219 m_propertyBoxLayout = layout;
Chris@145 220 m_configFile->set("preferences-property-box-layout", int(layout));
Chris@141 221 emit propertyChanged("Property Box Layout");
Chris@138 222 }
Chris@138 223 }
Chris@138 224
Chris@140 225 void
Chris@140 226 Preferences::setWindowType(WindowType type)
Chris@140 227 {
Chris@140 228 if (m_windowType != type) {
Chris@140 229 m_windowType = type;
Chris@145 230 m_configFile->set("preferences-window-type", int(type));
Chris@141 231 emit propertyChanged("Window Type");
Chris@140 232 }
Chris@140 233 }
Chris@140 234