summaryrefslogtreecommitdiff
path: root/src/main-view/RecentFilesManager.cpp
blob: d554eb4c8321580bf7298802b5e72fcda765b80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "RecentFilesManager.h"

RecentFilesManager::RecentFilesManager(QObject* parent, QMenu* recentFilesMenu):
    QObject(parent), recentFilesMenu(recentFilesMenu)
{
}

void RecentFilesManager::createRecentFileActs(const QStringList& recentFiles)
{
    if(recentFiles.isEmpty())
    {
        recentFilesMenu->setEnabled(false);
        return;
    }
    int recentFilesNum = recentFiles.size();
    if(recentFilesNum > MaxRecentFiles)
        recentFilesNum = MaxRecentFiles;
    for(int i = 0; i < recentFilesNum; i++)
        recentFilesMenu->addAction(createRecentFileAction(recentFiles[i]));
    updateActionTexts();
}

QAction* RecentFilesManager::createRecentFileAction(const QString& filePath)
{
    QAction* action = new QAction(this);
    action->setData(QDir::fromNativeSeparators(filePath));
    connect(action, SIGNAL(triggered()), this, SLOT(triggerRecentFile()));
    return action;
}

void RecentFilesManager::updateActionTexts()
{
    for(int i = 0; i < recentFilesMenu->actions().size(); i++)
    {
        QAction* action = recentFilesMenu->actions()[i];
		QString filePath = getActionFile(action);
        QString fileName = getShortFileName(filePath);
        QString fileDir = QDir::toNativeSeparators(getShortDirPath(filePath));
        QString text = QString("&%1 %2  (%3)").arg((i + 1) % 10)
            .arg(fileName).arg(fileDir);
        action->setText(text);
    }
}

QString RecentFilesManager::getActionFile(QAction* action)
{
	return action->data().toString();
}

QString RecentFilesManager::getLastUsedFilePath() const
{
	if(recentFilesMenu->actions().isEmpty())
		return QString();
	return getActionFile(recentFilesMenu->actions().first());
}

QString RecentFilesManager::getShortDirPath(const QString& filePath) const
{
    QString path = QFileInfo(filePath).absolutePath();
    QFontMetrics metrics(recentFilesMenu->font());
    return metrics.elidedText(path, Qt::ElideMiddle, MaxPathLength);
}

void RecentFilesManager::triggerRecentFile()
{
    QAction *action = qobject_cast<QAction*>(sender());
    if(action)
        emit recentFileTriggered(getActionFile(action));
}

void RecentFilesManager::addFile( const QString& filePath)
{
    addRecentFileAction(filePath);
    updateActionTexts();
    emit addedFile();
}

void RecentFilesManager::addRecentFileAction(const QString& filePath)
{
    recentFilesMenu->setEnabled(true);
    foreach(QAction* action, recentFilesMenu->actions())
        if(action->data() == filePath)
            recentFilesMenu->removeAction(action);
    QAction* newAction = createRecentFileAction(filePath);
    if(!recentFilesMenu->actions().isEmpty())
        recentFilesMenu->insertAction(recentFilesMenu->actions().first(), newAction);
    else
        recentFilesMenu->addAction(newAction);
    if(recentFilesMenu->actions().size() > MaxRecentFiles)
        recentFilesMenu->removeAction(recentFilesMenu->actions().last());
}

QStringList RecentFilesManager::getFiles() const
{
	QStringList list;
	foreach(QAction* action, recentFilesMenu->actions())
		list << getActionFile(action);
	return list;
}