-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
200 lines (170 loc) · 5.69 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "DecoderFactory.hpp"
#include "Image.hpp"
#include "Formatter.hpp"
#include "types.hpp"
#include "CenteredBoxProxyStyle.hpp"
#include "ImageSectionDataContainer.hpp"
#include "DirectoryWorker.hpp"
#include "TomsSplash.hpp"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QFutureWatcher>
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <QScreen>
#include <QtDebug>
#include <QFileInfo>
#include <QMainWindow>
#include <QStatusBar>
#include <QProgressBar>
#include <QPromise>
#include <QDir>
#include <QMessageBox>
#include <QThread>
#include <QStyleFactory>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
#include "ANPV.hpp"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
char* fluid_get_windows_error(void)
{
static TCHAR err[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
err,
sizeof(err) / sizeof(err[0]),
NULL);
#ifdef _UNICODE
static char ascii_err[sizeof(err)];
WideCharToMultiByte(CP_UTF8, 0, err, -1, ascii_err, sizeof(ascii_err) / sizeof(ascii_err[0]), 0, 0);
return ascii_err;
#else
return err;
#endif
}
QString getLongPathName(const char* shortPath8)
{
auto shortPath16 = QString::fromLocal8Bit(shortPath8);
QString longPath;
auto len = GetLongPathNameW((LPCTSTR)shortPath16.utf16(), nullptr, 0);
if (len == 0)
{
// probably the file doesn't exist, handle that error later.
return shortPath16;
}
longPath.resize(len-1);
len = GetLongPathNameW((LPCTSTR)shortPath16.utf16(), (LPWSTR)longPath.data(), len);
if (len == 0)
{
throw std::runtime_error(fluid_get_windows_error());
}
return longPath;
}
#else
inline QString getLongPathName(const char* path)
{
return QString::fromLocal8Bit(path);
}
#endif
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(ANPV);
Q_INIT_RESOURCE(oxygen);
QApplication app(argc, argv);
app.setOrganizationName("derselbst");
app.setApplicationName("ANPV");
TomsSplash splash;
splash.showMessage(QStringLiteral("Initialize Decoder Factory"));
// set UI Thread to high prio
QThread::currentThread()->setPriority(QThread::HighestPriority);
// create and init DecoderFactory in main thread
(void)DecoderFactory::globalInstance();
splash.showMessage(QStringLiteral("Setting application-wide style"));
app.setStyle(new CenteredBoxProxyStyle(QStyleFactory::create("Fusion")));
if (QIcon::themeName().isEmpty())
{
QIcon::setThemeName("oxygen");
}
try
{
ANPV anpv(&splash);
QDir cur = anpv.currentDir();
switch (argc)
{
case 1:
anpv.fixupAndSetCurrentDir(anpv.savedCurrentDir());
anpv.showThumbnailView(&splash);
break;
case 2:
{
QString arg = getLongPathName(argv[1]);
QFileInfo info(arg);
if (info.exists() && info.isDir())
{
anpv.setCurrentDir(info.absoluteFilePath());
anpv.showThumbnailView(&splash);
break;
}
// else
[[fallthrough]];
}
default:
{
QSharedPointer<ImageSectionDataContainer> currentDirModel;
QList<std::pair<QSharedPointer<Image>, QSharedPointer<ImageSectionDataContainer>>> imagesWithFileModel;
QFileInfo prevFileInfo;
for (int i = 1; i < argc; i++)
{
QFileInfo fileInfo(getLongPathName(argv[i]));
if (!fileInfo.exists())
{
Formatter f;
f << "Path '" << argv[i] << "' not found";
QMessageBox::critical(nullptr, "ANPV", f.str().c_str());
qCritical() << f.str().c_str();
return -1;
}
if (currentDirModel == nullptr || fileInfo.canonicalPath() != prevFileInfo.canonicalPath())
{
splash.showMessage(QString("Discover directory contents %1").arg(fileInfo.canonicalPath()));
currentDirModel.reset(new ImageSectionDataContainer(nullptr));
currentDirModel->sortSections(SortField::None, Qt::AscendingOrder);
currentDirModel->sortImageItems(SortField::FileName, Qt::AscendingOrder);
DirectoryWorker w(currentDirModel.data());
auto task = w.changeDirAsync(fileInfo.canonicalPath());
app.processEvents(QEventLoop::AllEvents);
task.waitForFinished();
}
auto emplacedImage = AbstractListItem::imageCast(currentDirModel->getItemByLinearIndex(currentDirModel->getLinearIndexOfItem(fileInfo)));
if (emplacedImage.isNull())
{
throw std::logic_error("This shouldn't happen: emplacedImage is null!");
}
imagesWithFileModel.push_back({ emplacedImage, currentDirModel });
prevFileInfo = fileInfo;
}
splash.showMessage("Starting the image decoding task...");
anpv.openImages(imagesWithFileModel);
splash.close();
}
break;
}
int r = app.exec();
return r;
}
catch(const std::exception& e)
{
Formatter f;
f << "An unexpected error caused ANPV to terminate.\n"
"Error Type: " << typeid(e).name() << "\n"
"Error Message: \n" << e.what();
QMessageBox::critical(nullptr, "Unexpected error", f.str().c_str());
}
return -1;
}