-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.cpp
147 lines (126 loc) · 4.64 KB
/
window.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
136
137
138
139
140
141
142
143
144
145
146
147
#include "window.h"
Window::Window(QWidget *parent) : QWidget(parent)
{
checkIntegrityOfFiles();
createThumbnailManager();
createTopLevelLayout();
createToolBar();
createLowerLayout();
createTagPanel();
createFileBrowser();
relaySignals();
setAcceptDrops(true);
}
void Window::checkIntegrityOfFiles()
{
Database* database = Database::getInstance();
QList<FileTuple> files = database->getAllFiles();
for (FileTuple file : files) {
if (!QFileInfo::exists(file.getPath())) {
MissingFileDialog popup(file);
int choice = popup.exec();
if (choice == QDialog::Accepted) {
QString newPath;
if (file.getType() == "folder") {
newPath = QFileDialog::getExistingDirectory(this, "Locate " + file.getName(), Settings::loadLastUsedDirectory(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);;
}
else {
QString searchConditions = "All Files (*)";
if (file.getType() == "image") {
searchConditions = "Image Files (*.png *.jpg)";
}
newPath = QFileDialog::getOpenFileName(this, "Locate " + file.getName(), Settings::loadLastUsedDirectory(), searchConditions);
}
if (newPath.isNull()) {
deleteThumbnail(file.getThumbnail());
database->removeFile(file.getId());
Prompt::show("The file was removed.");
}
else {
database->setPath(newPath, file.getId());
QString newName = extractNameFromPath(newPath);
if (newName != file.getName()) {
database->setFileName(newName, file.getId());
}
}
}
else if (choice == QDialog::Rejected) {
deleteThumbnail(file.getThumbnail());
database->removeFile(file.getId());
}
}
}
}
QString Window::extractNameFromPath(const QString& path)
{
int lastSlash = path.lastIndexOf('/');
int stringLength = path.length();
return path.right(stringLength - lastSlash - 1);
}
void Window::deleteThumbnail(const QString& path)
{
QFile file(path);
file.remove();
}
void Window::createThumbnailManager()
{
thumbnailManager = new ThumbnailManager(this);
}
// topLevelLayout contains the button bar at the very top with the
// bottom portion filled by the contents of lowerLayout.
void Window::createTopLevelLayout()
{
topLevelLayout = new QVBoxLayout;
this->setLayout(topLevelLayout);
}
void Window::createToolBar()
{
toolBar = new ToolBar(thumbnailManager);
topLevelLayout->addWidget(toolBar);
}
// lowerLayout contains the tagPanel on the left side and the
// fileBrowser on the right.
void Window::createLowerLayout()
{
lowerLayout = new QHBoxLayout;
topLevelLayout->addLayout(lowerLayout);
}
void Window::createTagPanel()
{
tagPanel = new TagPanel;
lowerLayout->addWidget(tagPanel);
}
void Window::createFileBrowser()
{
fileBrowser = new FileBrowser(thumbnailManager);
lowerLayout->addWidget(fileBrowser);
}
void Window::relaySignals()
{
connect(toolBar, SIGNAL (filesChanged()), fileBrowser, SLOT (showNewestItem()));
connect(fileBrowser, SIGNAL (selectionChanged(int)), tagPanel, SLOT (selectionChanged(int)));
connect(fileBrowser, SIGNAL (databaseTagsChanged()), tagPanel, SLOT (updateTagDictionaries()));
connect(toolBar, SIGNAL (databaseTagsChanged()), tagPanel, SLOT (updateTagDictionaries()));
connect(tagPanel, SIGNAL (activeSearchTagsChanged(QList<int>, QList<int>)), fileBrowser, SLOT (updateSearchList(QList<int>, QList<int>)));
connect(tagPanel, SIGNAL (tagAddedToSelectedFile(int)), fileBrowser, SLOT (reloadIfTagAddedImpactsSearch(int)));
connect(tagPanel, SIGNAL (tagRemovedFromSelectedFile(int)), fileBrowser, SLOT (reloadIfTagRemovedImpactsSearch(int)));
connect(toolBar, SIGNAL (thumbnailSliderMoved(int)), fileBrowser, SLOT (updateThumbnailScale(int)));
connect(this, SIGNAL (filesDropped(QStringList)), toolBar, SLOT(importFiles(QStringList)));
}
void Window::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void Window::dropEvent(QDropEvent* event)
{
if (event->mimeData()->hasUrls()) {
QStringList paths;
QList<QUrl> urls = event->mimeData()->urls();
for (int i = 0; i < urls.size(); ++i) {
paths.append(urls[i].toLocalFile());
}
emit filesDropped(paths);
}
}