annotate recentfiles.cpp @ 365:1482f5b9f9dc feature_101

Add an explosion of menus and many keyboard shortcuts.
author Chris Cannam
date Fri, 18 Mar 2011 13:14:14 +0000
parents 8fd71f570884
children
rev   line source
Chris@63 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@63 2
Chris@63 3 /*
Chris@63 4 EasyMercurial
Chris@63 5
Chris@63 6 Based on HgExplorer by Jari Korhonen
Chris@63 7 Copyright (c) 2010 Jari Korhonen
Chris@244 8 Copyright (c) 2011 Chris Cannam
Chris@244 9 Copyright (c) 2011 Queen Mary, University of London
Chris@63 10
Chris@63 11 This program is free software; you can redistribute it and/or
Chris@63 12 modify it under the terms of the GNU General Public License as
Chris@63 13 published by the Free Software Foundation; either version 2 of the
Chris@63 14 License, or (at your option) any later version. See the file
Chris@63 15 COPYING included with this distribution for more information.
Chris@63 16 */
Chris@63 17
Chris@63 18 #include "recentfiles.h"
Chris@63 19
Chris@63 20 #include <QFileInfo>
Chris@63 21 #include <QSettings>
Chris@63 22 #include <QRegExp>
Chris@63 23
Chris@63 24 RecentFiles::RecentFiles(QString settingsGroup, size_t maxCount,
Chris@63 25 bool ignoreTemporaries) :
Chris@63 26 m_settingsGroup(settingsGroup),
Chris@63 27 m_maxCount(maxCount),
Chris@63 28 m_ignoreTemporaries(ignoreTemporaries)
Chris@63 29 {
Chris@63 30 read();
Chris@63 31 }
Chris@63 32
Chris@63 33 RecentFiles::~RecentFiles()
Chris@63 34 {
Chris@63 35 // nothing
Chris@63 36 }
Chris@63 37
Chris@63 38 void
Chris@63 39 RecentFiles::read()
Chris@63 40 {
Chris@63 41 m_names.clear();
Chris@63 42 QSettings settings;
Chris@63 43 settings.beginGroup(m_settingsGroup);
Chris@63 44
Chris@63 45 for (size_t i = 0; i < 100; ++i) {
Chris@63 46 QString key = QString("recent-%1").arg(i);
Chris@63 47 QString name = settings.value(key, "").toString();
Chris@63 48 if (name == "") break;
Chris@63 49 if (i < m_maxCount) m_names.push_back(name);
Chris@63 50 else settings.setValue(key, "");
Chris@63 51 }
Chris@63 52
Chris@63 53 settings.endGroup();
Chris@63 54 }
Chris@63 55
Chris@63 56 void
Chris@63 57 RecentFiles::write()
Chris@63 58 {
Chris@63 59 QSettings settings;
Chris@63 60 settings.beginGroup(m_settingsGroup);
Chris@63 61
Chris@63 62 for (size_t i = 0; i < m_maxCount; ++i) {
Chris@63 63 QString key = QString("recent-%1").arg(i);
Chris@63 64 QString name = "";
Chris@63 65 if (i < m_names.size()) name = m_names[i];
Chris@63 66 settings.setValue(key, name);
Chris@63 67 }
Chris@63 68
Chris@63 69 settings.endGroup();
Chris@63 70 }
Chris@63 71
Chris@63 72 void
Chris@63 73 RecentFiles::truncateAndWrite()
Chris@63 74 {
Chris@63 75 while (m_names.size() > m_maxCount) {
Chris@63 76 m_names.pop_back();
Chris@63 77 }
Chris@63 78 write();
Chris@63 79 }
Chris@63 80
Chris@63 81 QStringList
Chris@63 82 RecentFiles::getRecent() const
Chris@63 83 {
Chris@63 84 QStringList names;
Chris@63 85 for (size_t i = 0; i < m_maxCount; ++i) {
Chris@63 86 if (i < m_names.size()) {
Chris@63 87 names.push_back(m_names[i]);
Chris@63 88 }
Chris@63 89 }
Chris@63 90 return names;
Chris@63 91 }
Chris@63 92
Chris@63 93 void
Chris@63 94 RecentFiles::add(QString name)
Chris@63 95 {
Chris@63 96 bool have = false;
Chris@63 97 for (size_t i = 0; i < m_names.size(); ++i) {
Chris@63 98 if (m_names[i] == name) {
Chris@63 99 have = true;
Chris@63 100 break;
Chris@63 101 }
Chris@63 102 }
Chris@63 103
Chris@63 104 if (!have) {
Chris@63 105 m_names.push_front(name);
Chris@63 106 } else {
Chris@63 107 std::deque<QString> newnames;
Chris@63 108 newnames.push_back(name);
Chris@63 109 for (size_t i = 0; i < m_names.size(); ++i) {
Chris@63 110 if (m_names[i] == name) continue;
Chris@63 111 newnames.push_back(m_names[i]);
Chris@63 112 }
Chris@63 113 m_names = newnames;
Chris@63 114 }
Chris@63 115
Chris@63 116 truncateAndWrite();
Chris@63 117 emit recentChanged();
Chris@63 118 }
Chris@63 119
Chris@63 120 void
Chris@63 121 RecentFiles::addFile(QString name)
Chris@63 122 {
Chris@63 123 static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
Chris@63 124 static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
Chris@63 125 if (schemeRE.indexIn(name) == 0) {
Chris@63 126 add(name);
Chris@63 127 } else {
Chris@63 128 QString absPath = QFileInfo(name).absoluteFilePath();
Chris@63 129 if (tempRE.indexIn(absPath) != -1) {
Chris@63 130 if (!m_ignoreTemporaries) {
Chris@63 131 add(absPath);
Chris@63 132 }
Chris@63 133 } else {
Chris@63 134 add(absPath);
Chris@63 135 }
Chris@63 136 }
Chris@63 137 }
Chris@63 138
Chris@63 139