annotate base/RecentFiles.cpp @ 199:1d789d688f59

* When adding a layer, make it the selected layer on that pane * More OSC support, including transforms
author Chris Cannam
date Fri, 10 Nov 2006 17:45:26 +0000
parents d5052b5fea9c
children 6576a208e8e7
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@149 20
Chris@166 21 RecentFiles::RecentFiles(QString settingsGroup, size_t maxCount) :
Chris@166 22 m_settingsGroup(settingsGroup),
Chris@166 23 m_maxCount(maxCount)
Chris@149 24 {
Chris@166 25 read();
Chris@149 26 }
Chris@149 27
Chris@149 28 RecentFiles::~RecentFiles()
Chris@149 29 {
Chris@149 30 // nothing
Chris@149 31 }
Chris@149 32
Chris@149 33 void
Chris@166 34 RecentFiles::read()
Chris@149 35 {
Chris@166 36 m_names.clear();
Chris@156 37 QSettings settings;
Chris@166 38 settings.beginGroup(m_settingsGroup);
Chris@156 39
Chris@166 40 for (size_t i = 0; i < 100; ++i) {
Chris@166 41 QString key = QString("recent-%1").arg(i);
Chris@166 42 QString name = settings.value(key, "").toString();
Chris@166 43 if (name == "") break;
Chris@166 44 if (i < m_maxCount) m_names.push_back(name);
Chris@156 45 else settings.setValue(key, "");
Chris@149 46 }
Chris@156 47
Chris@156 48 settings.endGroup();
Chris@149 49 }
Chris@149 50
Chris@149 51 void
Chris@166 52 RecentFiles::write()
Chris@149 53 {
Chris@156 54 QSettings settings;
Chris@166 55 settings.beginGroup(m_settingsGroup);
Chris@156 56
Chris@166 57 for (size_t i = 0; i < m_maxCount; ++i) {
Chris@166 58 QString key = QString("recent-%1").arg(i);
Chris@166 59 QString name = "";
Chris@166 60 if (i < m_names.size()) name = m_names[i];
Chris@166 61 settings.setValue(key, name);
Chris@149 62 }
Chris@156 63
Chris@156 64 settings.endGroup();
Chris@149 65 }
Chris@149 66
Chris@149 67 void
Chris@149 68 RecentFiles::truncateAndWrite()
Chris@149 69 {
Chris@166 70 while (m_names.size() > m_maxCount) {
Chris@166 71 m_names.pop_back();
Chris@149 72 }
Chris@166 73 write();
Chris@149 74 }
Chris@149 75
Chris@149 76 std::vector<QString>
Chris@166 77 RecentFiles::getRecent() const
Chris@149 78 {
Chris@166 79 std::vector<QString> names;
Chris@166 80 for (size_t i = 0; i < m_maxCount; ++i) {
Chris@166 81 if (i < m_names.size()) {
Chris@166 82 names.push_back(m_names[i]);
Chris@149 83 }
Chris@149 84 }
Chris@166 85 return names;
Chris@149 86 }
Chris@149 87
Chris@149 88 void
Chris@166 89 RecentFiles::add(QString name)
Chris@149 90 {
Chris@149 91 bool have = false;
Chris@166 92 for (size_t i = 0; i < m_names.size(); ++i) {
Chris@166 93 if (m_names[i] == name) {
Chris@149 94 have = true;
Chris@149 95 break;
Chris@149 96 }
Chris@149 97 }
Chris@149 98
Chris@149 99 if (!have) {
Chris@166 100 m_names.push_front(name);
Chris@149 101 } else {
Chris@166 102 std::deque<QString> newnames;
Chris@166 103 newnames.push_back(name);
Chris@166 104 for (size_t i = 0; i < m_names.size(); ++i) {
Chris@166 105 if (m_names[i] == name) continue;
Chris@166 106 newnames.push_back(m_names[i]);
Chris@149 107 }
Chris@185 108 m_names = newnames;
Chris@149 109 }
Chris@149 110
Chris@149 111 truncateAndWrite();
Chris@166 112 emit recentChanged();
Chris@149 113 }
Chris@149 114
Chris@166 115 void
Chris@166 116 RecentFiles::addFile(QString name)
Chris@166 117 {
Chris@166 118 add(QFileInfo(name).absoluteFilePath());
Chris@166 119 }
Chris@149 120
Chris@166 121