forked from zdo/zoomme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
243 lines (197 loc) · 7.47 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "zoomwidget.hpp"
#include <QtWidgets/QApplication>
#include <QCursor>
#include <QScreen>
#include <QImage>
#include <string.h>
#include <QString>
#include <QSystemTrayIcon>
#include <stdio.h>
#include <stdlib.h>
#include <QFileInfo>
void help(const char *errorMsg)
{
FILE *output;
int exitStatus;
if (strlen(errorMsg) == 0) {
output = stdout;
exitStatus = EXIT_SUCCESS;
fprintf(output, "ZoomMe is an application for zooming/magnifying and noting the desktop.\n");
} else {
output = stderr;
exitStatus = EXIT_FAILURE;
fprintf(output, "[ERROR] %s\n", errorMsg);
}
fprintf(output, "\nUsage: zoomme [configurations] [mode]\n");
fprintf(output, "\nHelp page:\n");
fprintf(output, " --help Display this help message\n");
fprintf(output, "\nConfigurations:\n");
fprintf(output, " -p [path/to/folder] Set the path where to save the exported files (default: Desktop folder)\n");
fprintf(output, " -n [file_name] Specify the name of the exported files (default: Zoomme {date})\n");
fprintf(output, " -e:i [extension] Specify the extension of the exported (saved) image (default: png)\n");
fprintf(output, " -e:v [extension] Specify the extension of the exported (saved) video file (default: mp4)\n");
fprintf(output, "\nModes:\n");
fprintf(output, " -l Not use a background (transparent). In this mode zooming is disabled\n");
fprintf(output, " -i <image_path> [opts] Specify the path to an image as the background, instead of the desktop.\n");
fprintf(output, " --copy This will copy the source image path (autocompletes -p, -e and -n flags) -it will NOT replace the original image-.\n");
fprintf(output, " -r [path/to/file] Load/Restore the state of the program saved in that file. It should be a '.zoomme' file\n");
fprintf(output, " -c Load an image from the clipboard as the background, instead of the desktop.\n");
fprintf(output, " --empty [width] [height] Create an empty blackboard with the given size\n");
fprintf(output, "\n For more information, visit https://github.com/Ezee1015/zoomme\n");
exit(exitStatus);
}
QString nextToken(const int argc, char *argv[], int *pos, const QString type)
{
(*pos)++;
if (*pos == argc) {
QString error(type + " not provided");
help(QSTRING_TO_STRING(error));
}
return argv[*pos];
}
enum Mode {
DESKTOP, // Grab the desktop (default)
LIVE_MODE,
IMAGE, // Grab an image
CLIPBOARD, // Grab an image from the clipboard
BACKUP, // Recover from a backup file (.zoomme)
BLACKBOARD // Empty pixmap
};
void setMode(Mode *mode, const Mode newMode)
{
switch (*mode) {
case BACKUP:
help("Mode already provided (Backup file provided)");
break;
case IMAGE:
help("Mode already provided (image provided)");
break;
case LIVE_MODE:
help("Mode already provided (live mode)");
break;
case CLIPBOARD:
help("Mode already provided (load from clipboard)");
break;
case BLACKBOARD:
help("Mode already provided (empty blackboard)");
break;
case DESKTOP: // Default value
*mode = newMode;
break;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSystemTrayIcon tray = QSystemTrayIcon(QIcon(":/resources/icon/Icon.png"));
tray.setVisible(true);
tray.show();
// Configurations
QString savePath;
QString saveName;
QString saveImgExt; // Extension
QString saveVidExt; // Extension
// Modes
Mode mode = DESKTOP;
QString imgPath;
QString backupPath;
QSize blackboardSize;
// Parsing arguments
for (int i=1; i<argc ; ++i) {
if (strcmp(argv[i], "--help") == 0) {
help("");
} else if (strcmp(argv[i], "-l") == 0) {
setMode(&mode, LIVE_MODE);
} else if (strcmp(argv[i], "-i") == 0) {
setMode(&mode, IMAGE);
imgPath = nextToken(argc, argv, &i, "Image path");
} else if (strcmp(argv[i], "--copy") == 0) {
if (imgPath == "") {
help("Copy the image path was indicated, but the source image is not provided");
}
if (savePath != "") help("Saving path already provided");
if (saveName != "") help("Saving name already provided");
if (saveImgExt != "") help("Saving extension already provided");
QFileInfo imgInfo = QFileInfo(imgPath);
savePath = imgInfo.path();
saveName = imgInfo.completeBaseName();
saveImgExt = imgInfo.suffix();
} else if (strcmp(argv[i], "-p") == 0) {
if (savePath != "") {
help("Saving path already provided");
}
savePath = nextToken(argc, argv, &i, "Save path");
} else if (strcmp(argv[i], "-n") == 0) {
if (saveName != "") {
help("Saving name already provided");
}
saveName = nextToken(argc, argv, &i, "Save name");
} else if (strcmp(argv[i], "-e:i") == 0) {
if (saveImgExt != "") {
help("Saving image extension already provided");
}
saveImgExt = nextToken(argc, argv, &i, "Image extension");
} else if (strcmp(argv[i], "-e:v") == 0) {
if (saveVidExt != "") {
help("Saving video extension already provided");
}
saveVidExt = nextToken(argc, argv, &i, "Video extension");
} else if (strcmp(argv[i], "-r") == 0) {
setMode(&mode, BACKUP);
backupPath = nextToken(argc, argv, &i, "Backup file path");
if (QFileInfo(backupPath).suffix() != "zoomme") {
QString errorMsg("It's not a '.zoomme' file: " + backupPath);
help(QSTRING_TO_STRING(errorMsg));
}
} else if (strcmp(argv[i], "-c") == 0) {
setMode(&mode, CLIPBOARD);
} else if (strcmp(argv[i], "--empty") == 0) {
setMode(&mode, BLACKBOARD);
bool widthCorrect = false, heightCorrect=false;
blackboardSize.setWidth(nextToken(argc, argv, &i, "Width for the blackboard").toInt(&widthCorrect));
blackboardSize.setHeight(nextToken(argc, argv, &i, "Height for the blackboard").toInt(&heightCorrect));
if (!widthCorrect) help("The given width is not a number");
if (!heightCorrect) help("The given height is not a number");
if (blackboardSize.width() < 1) help("The given width is not a positive number");
if (blackboardSize.height() < 1) help("The given height is not a positive number");
}
else {
QString textError("Unknown flag: ");
textError.append(argv[i]);
help(QSTRING_TO_STRING(textError));
}
}
ZoomWidget w;
w.setWindowFlags(Qt::WindowMinimizeButtonHint /* | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::BypassWindowManagerHint */);
w.resize(QApplication::screenAt(QCursor::pos())->geometry().size());
w.move(QApplication::screenAt(QCursor::pos())->geometry().topLeft());
w.setCursor(QCursor(Qt::CrossCursor));
// Set the path, name and extension for saving the file
w.initFileConfig(savePath, saveName, saveImgExt, saveVidExt);
// Configure the app mode
switch (mode) {
case BACKUP:
w.restoreStateFromFile(backupPath);
break;
case IMAGE:
w.grabImage(QPixmap(imgPath));
break;
case BLACKBOARD:
w.createBlackboard(blackboardSize);
break;
case CLIPBOARD:
w.grabFromClipboard();
break;
case LIVE_MODE:
// Set transparency for the window
w.setAttribute(Qt::WA_TranslucentBackground, true);
w.setLiveMode();
break;
case DESKTOP:
w.grabDesktop();
break;
}
QApplication::beep();
w.show();
return a.exec();
}