forked from ZiQiangWang/ImagePorc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecentOpened.cpp
135 lines (118 loc) · 3.36 KB
/
RecentOpened.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "RecentOpened.h"
#include <QMenu>
#include <QSettings>
#include <QFileInfo>
#include <QAction>
#include <QDebug>
RecentOpened::RecentOpened(QMenu *menu,QObject *parent) :
QObject(parent)
{
m_menu = menu;
m_clear = m_menu->actions().at(0);
initRecent();
}
void RecentOpened::initRecent()
{
QSettings settings("settings.ini",QSettings::IniFormat);
settings.beginGroup("RecentlyOpened");
QStringList keys,values;
keys = settings.childKeys();
for(int i=0;i<keys.size();i++)
values << settings.value(keys[i]).toString();
settings.endGroup();
if(keys.size())
{
m_clear->setEnabled(true);
m_clear->setText("清除记录");
}
for(int i=0;i<values.size();i++)
{
QFileInfo info(values[i]);
QString item = QString("%1 %2").arg(i+1).arg(info.fileName());
QAction *recent = new QAction(QIcon(values[i]),item,this);
recent->setToolTip(values[i]);
connect(recent,&QAction::triggered,this,&RecentOpened::actionTriggered);
m_menu->insertAction(m_clear,recent);
}
}
void RecentOpened::UpdateList(const QString &file)
{
QStringList keys,values;
QSettings settings("settings.ini",QSettings::IniFormat);
settings.beginGroup("RecentlyOpened");
keys = settings.childKeys();
for(int i=0;i<keys.size();i++)
values << settings.value(keys[i]).toString();
settings.endGroup();
int index = values.indexOf(file);
if(index != -1)
{
values.removeAt(index);
}
QFileInfo fi(file);
if(fi.exists())
{
values.insert(0,file);
}
keys.clear();
for(int i=0;i<values.size();i++)
{
keys << QString::number(i+1);
}
settings.remove("RecentlyOpened");
settings.beginGroup("RecentlyOpened");
for(int i=0;i<values.size();i++)
{
settings.setValue(keys[i],values[i]);
}
settings.endGroup();
QList<QAction *> actions = m_menu->actions();
for(int i=0;i<actions.size()-1;i++)
{
m_menu->removeAction(actions[i]);
}
for(int i=0;i<values.size();i++)
{
QFileInfo info(values[i]);
QString item = QString("%1 %2").arg(i+1).arg(info.fileName());
QAction *recent = new QAction(QIcon(values[i]),item,this);
recent->setToolTip(values[i]);
connect(recent,&QAction::triggered,this,&RecentOpened::actionTriggered);
m_menu->insertAction(m_clear,recent);
}
m_clear->setEnabled(true);
m_clear->setText("清除记录");
}
void RecentOpened::actionTriggered()
{
int index=0;
QList<QAction *> actions = m_menu->actions();
for(int i=0;i<actions.size()-1;i++)
{
if(sender() == actions[i])
{
index = i+1;
emit SelectedItem(i+1);
break;
}
}
QSettings settings("settings.ini",QSettings::IniFormat);
settings.beginGroup("RecentlyOpened");
this->UpdateList(settings.value(QString::number(index)).toString());
settings.endGroup();
}
void RecentOpened::clearActions()
{
QSettings settings("settings.ini",QSettings::IniFormat);
settings.remove("RecentlyOpened");
QList<QAction *> actions = m_menu->actions();
if(actions.size()>1)
{
for(int i=0;i<actions.size()-1;i++)
{
m_menu->removeAction(actions[i]);
}
m_clear->setEnabled(false);
m_clear->setText("(无)");
}
}