-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmenu.cpp
More file actions
100 lines (92 loc) · 3.29 KB
/
menu.cpp
File metadata and controls
100 lines (92 loc) · 3.29 KB
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
#include "menu.h"
#include <QDir>
#include <QAction>
#include <QMenu>
#include "src/mapwidget/poiitem.h"
Menu::Menu(QObject *parent, const QHash<QString, QString> &info, QMenu *popUpMenu, const QString &iconsPath)
: QObject(parent)
{
PopUpMenu = popUpMenu;
this->info = info;
signalMapper = new QSignalMapper(this);
QDir dir;
dir.setPath(iconsPath);
ScanDir(dir, QStringList(), PopUpMenu);
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(menuSelection(QString)));
}
void Menu::ScanDir(QDir dir, const QStringList &parentDirs, QMenu* menuParent)
{
AddFolderItem(dir, parentDirs, menuParent);
AddFileItem(dir, parentDirs, menuParent);
}
void Menu::AddFileItem(QDir dir, const QStringList &parentDirs, QMenu* menuParent)
{
dir.setNameFilters(QStringList("*.svg"));
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
QStringList fileList = dir.entryList();
for (int i=0; i<fileList.count(); ++i)
{
QFileInfo fileInfo = QFileInfo(dir, fileList.at(i));
QString absoluteFilePath = fileInfo.absoluteFilePath();
QString fileName = fileInfo.fileName();
QStringList fileNameItems = fileName.split(".");
QMenu* menuPrev = menuParent;
QStringList dirs = QStringList(parentDirs);
for(int j=0; j< fileNameItems.count() - 1; ++j)
{
menuPrev = GetMenuItem(dirs, fileNameItems[j], menuPrev, absoluteFilePath,
j >= fileNameItems.count() - 2);
dirs.append(fileNameItems[j]);
}
}
}
void Menu::AddFolderItem(QDir dir, const QStringList &parentDirs, QMenu* menuParent)
{
dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
QStringList dirList = dir.entryList();
for (int i=0; i<dirList.size(); ++i)
{
QFileInfo fileInfo = dirList.at(i);
QString newPath = QString("%1/%2").arg(dir.absolutePath()).arg(dirList.at(i));
QStringList dirs = QStringList(parentDirs);
dirs.append(fileInfo.fileName());
QMenu* menu = GetMenuItem(parentDirs, fileInfo.fileName(), menuParent, "", false);
ScanDir(QDir(newPath), dirs, menu);
}
}
QMenu* Menu::GetMenuItem(const QStringList &parentDirs, QString name, QMenu *menuParent,
const QString &absoluteFilePath, bool addAction)
{
QString key = parentDirs.join('\\') + "\\" + name;
if(MenuItemsList.contains(key))
{
return MenuItemsList.value(key);
}
else
{
if(addAction)
{
QAction* action = new QAction(GetName(name), this);
QIcon* icon = new QIcon(absoluteFilePath);
action->setIcon(* icon);
connect(action, SIGNAL(triggered()), signalMapper, SLOT(map()));
signalMapper->setMapping(action, name);
menuParent->addAction(action);
return menuParent;
}
else
{
QMenu* menu = menuParent->addMenu(name);
MenuItemsList[key] = menu;
return menu;
}
}
}
void Menu::menuSelection(QString name)
{
emit clicked(name);
}
QString Menu::GetName(QString &code)
{
return this->info.contains(code)?this->info[code]:code;
}