annotate base/RecentFiles.cpp @ 245:4cd620bd4c61 sv1-1.0pre1

* Make getPropertyRangeAndValue return the default separately from the current value. Previously some contexts were incorrectly treating the current value as a default.
author Chris Cannam
date Fri, 02 Mar 2007 14:00:12 +0000
parents 6576a208e8e7
children 3b8008d09541
rev   line source
Chris@149 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@149 2
Chris@149 3 /*
Chris@149 4 Sonic Visualiser
Chris@149 5 An audio file viewer and annotation editor.
Chris@149 6 Centre for Digital Music, Queen Mary, University of London.
Chris@149 7 This file copyright 2006 Chris Cannam.
Chris@149 8
Chris@149 9 This program is free software; you can redistribute it and/or
Chris@149 10 modify it under the terms of the GNU General Public License as
Chris@149 11 published by the Free Software Foundation; either version 2 of the
Chris@149 12 License, or (at your option) any later version. See the file
Chris@149 13 COPYING included with this distribution for more information.
Chris@149 14 */
Chris@149 15
Chris@149 16 #include "RecentFiles.h"
Chris@149 17
Chris@149 18 #include <QFileInfo>
Chris@156 19 #include <QSettings>
Chris@209 20 #include <QRegExp>
Chris@149 21
Chris@166 22 RecentFiles::RecentFiles(QString settingsGroup, size_t maxCount) :
Chris@166 23 m_settingsGroup(settingsGroup),
Chris@166 24 m_maxCount(maxCount)
Chris@149 25 {
Chris@166 26 read();
Chris@149 27 }
Chris@149 28
Chris@149 29 RecentFiles::~RecentFiles()
Chris@149 30 {
Chris@149 31 // nothing
Chris@149 32 }
Chris@149 33
Chris@149 34 void
Chris@166 35 RecentFiles::read()
Chris@149 36 {
Chris@166 37 m_names.clear();
Chris@156 38 QSettings settings;
Chris@166 39 settings.beginGroup(m_settingsGroup);
Chris@156 40
Chris@166 41 for (size_t i = 0; i < 100; ++i) {
Chris@166 42 QString key = QString("recent-%1").arg(i);
Chris@166 43 QString name = settings.value(key, "").toString();
Chris@166 44 if (name == "") break;
Chris@166 45 if (i < m_maxCount) m_names.push_back(name);
Chris@156 46 else settings.setValue(key, "");
Chris@149 47 }
Chris@156 48
Chris@156 49 settings.endGroup();
Chris@149 50 }
Chris@149 51
Chris@149 52 void
Chris@166 53 RecentFiles::write()
Chris@149 54 {
Chris@156 55 QSettings settings;
Chris@166 56 settings.beginGroup(m_settingsGroup);
Chris@156 57
Chris@166 58 for (size_t i = 0; i < m_maxCount; ++i) {
Chris@166 59 QString key = QString("recent-%1").arg(i);
Chris@166 60 QString name = "";
Chris@166 61 if (i < m_names.size()) name = m_names[i];
Chris@166 62 settings.setValue(key, name);
Chris@149 63 }
Chris@156 64
Chris@156 65 settings.endGroup();
Chris@149 66 }
Chris@149 67
Chris@149 68 void
Chris@149 69 RecentFiles::truncateAndWrite()
Chris@149 70 {
Chris@166 71 while (m_names.size() > m_maxCount) {
Chris@166 72 m_names.pop_back();
Chris@149 73 }
Chris@166 74 write();
Chris@149 75 }
Chris@149 76
Chris@149 77 std::vector<QString>
Chris@166 78 RecentFiles::getRecent() const
Chris@149 79 {
Chris@166 80 std::vector<QString> names;
Chris@166 81 for (size_t i = 0; i < m_maxCount; ++i) {
Chris@166 82 if (i < m_names.size()) {
Chris@166 83 names.push_back(m_names[i]);
Chris@149 84 }
Chris@149 85 }
Chris@166 86 return names;
Chris@149 87 }
Chris@149 88
Chris@149 89 void
Chris@166 90 RecentFiles::add(QString name)
Chris@149 91 {
Chris@149 92 bool have = false;
Chris@166 93 for (size_t i = 0; i < m_names.size(); ++i) {
Chris@166 94 if (m_names[i] == name) {
Chris@149 95 have = true;
Chris@149 96 break;
Chris@149 97 }
Chris@149 98 }
Chris@149 99
Chris@149 100 if (!have) {
Chris@166 101 m_names.push_front(name);
Chris@149 102 } else {
Chris@166 103 std::deque<QString> newnames;
Chris@166 104 newnames.push_back(name);
Chris@166 105 for (size_t i = 0; i < m_names.size(); ++i) {
Chris@166 106 if (m_names[i] == name) continue;
Chris@166 107 newnames.push_back(m_names[i]);
Chris@149 108 }
Chris@185 109 m_names = newnames;
Chris@149 110 }
Chris@149 111
Chris@149 112 truncateAndWrite();
Chris@166 113 emit recentChanged();
Chris@149 114 }
Chris@149 115
Chris@166 116 void
Chris@166 117 RecentFiles::addFile(QString name)
Chris@166 118 {
Chris@209 119 static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
Chris@209 120 if (schemeRE.indexIn(name) == 0) {
Chris@209 121 add(name);
Chris@209 122 } else {
Chris@209 123 add(QFileInfo(name).absoluteFilePath());
Chris@209 124 }
Chris@166 125 }
Chris@149 126
Chris@166 127