-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs.cpp
More file actions
281 lines (237 loc) · 8.75 KB
/
Copy pathfuncs.cpp
File metadata and controls
281 lines (237 loc) · 8.75 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <QDir>
#include <QDateTime>
#include <QDBusInterface>
#include <QDBusArgument>
#include <QDBusMetaType>
#include <QWidget>
#include <QIcon>
#include <QMutex>
#include <QRegularExpression>
extern "C" {
#include <unistd.h>
}
#include "funcs.h"
#include "mainwindow.h"
ZGenericFuncs::ZGenericFuncs(QObject *parent)
: QObject(parent)
{
}
ZGenericFuncs::~ZGenericFuncs() = default;
void ZGenericFuncs::stdConsoleOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex loggerMutex;
QMutexLocker locker(&loggerMutex);
const QString fmsg = qFormatLogMessage(type,context,msg);
if (fmsg.isEmpty()) return;
fprintf(stderr, "%s\n", fmsg.toLocal8Bit().constData()); // NOLINT
if (mainWindow) {
QMetaObject::invokeMethod(mainWindow,[fmsg](){
mainWindow->addLogMessage(fmsg);
},Qt::QueuedConnection);
}
}
const QStringList &ZGenericFuncs::zCaptureMode() {
static const QStringList res = {
QSL("Full screen"),
QSL("Current screen"),
QSL("Window under cursor"),
QSL("Rectangle region"),
QSL("Child window")
};
return res;
}
const QStringList &ZGenericFuncs::zImageFormats() {
static const QStringList res = {
QSL("PNG"),
QSL("JPG")
};
return res;
}
QStringList ZGenericFuncs::getSuffixesFromFilter(const QString& filter)
{
QStringList res;
if (filter.isEmpty()) return res;
res = filter.split(QSL(";;"),Qt::SkipEmptyParts);
if (res.isEmpty()) return res;
QString ex = res.first();
res.clear();
if (ex.isEmpty()) return res;
ex.remove(QRegularExpression(QSL("^.*\\(")));
ex.remove(QRegularExpression(QSL("\\).*$")));
ex.remove(QRegularExpression(QSL("^.*\\.")));
res = ex.split(QSL(" "));
return res;
}
QString ZGenericFuncs::getOpenFileNameD(QWidget * parent, const QString & caption, const QString & dir,
const QString & filter, QString * selectedFilter,
QFileDialog::Options options)
{
return QFileDialog::getOpenFileName(parent,caption,dir,filter,selectedFilter,options);
}
QString ZGenericFuncs::getSaveFileNameD(QWidget * parent, const QString & caption, const QString & dir,
const QString & filter, QString * selectedFilter, QFileDialog::Options options,
const QString & preselectFileName)
{
QFileDialog d(parent,caption,dir,filter);
d.setFileMode(QFileDialog::AnyFile);
d.setOptions(options);
d.setAcceptMode(QFileDialog::AcceptSave);
QStringList sl;
if (selectedFilter!=nullptr && !selectedFilter->isEmpty()) {
sl=getSuffixesFromFilter(*selectedFilter);
} else {
sl=getSuffixesFromFilter(filter);
}
if (!sl.isEmpty())
d.setDefaultSuffix(sl.first());
if (selectedFilter && !selectedFilter->isEmpty())
d.selectNameFilter(*selectedFilter);
if (!preselectFileName.isEmpty())
d.selectFile(preselectFileName);
QString res;
if (d.exec()==QDialog::Accepted) {
QString userFilter = d.selectedNameFilter();
if (selectedFilter)
*selectedFilter=userFilter;
if (!userFilter.isEmpty()) {
const auto suffixes = getSuffixesFromFilter(userFilter);
d.setDefaultSuffix(suffixes.first());
}
const auto selectedFiles = d.selectedFiles();
if (!selectedFiles.isEmpty())
res = selectedFiles.first();
}
return res;
}
QString ZGenericFuncs::getExistingDirectoryD ( QWidget * parent, const QString & caption,
const QString & dir, QFileDialog::Options options )
{
return QFileDialog::getExistingDirectory(parent,caption,dir,options);
}
QString ZGenericFuncs::generateUniqName(QSpinBox *counter, const QString& tmpl, const QPixmap& snapshot, const QString &dir,
const QString& format, bool withoutPath)
{
const int numberBase = 10;
counter->setValue(counter->value() + 1);
QString uniq = tmpl;
if (uniq.isEmpty())
uniq = QSL("%NN");
QRegularExpression exp(QSL("%(\\w+)"));
QRegularExpressionMatch mexp = exp.match(uniq);
for (int i=0; i < mexp.lastCapturedIndex(); i++) {
const int length = mexp.capturedLength(i);
const int pos = mexp.capturedStart(i);
if (length>=2) {
const QString tl = uniq.mid(pos+1,length-1);
if (tl.contains(QRegularExpression(QSL("N+")))) {
uniq.replace(pos, length, QSL("%1").arg(counter->value(),tl.length(),numberBase,QChar('0')));
} else if (tl == QSL("w")) {
uniq.replace(pos, length, QSL("%1").arg(snapshot.width()));
} else if (tl == QSL("h")) {
uniq.replace(pos, length, QSL("%1").arg(snapshot.height()));
} else if (tl == QSL("y")) {
uniq.replace(pos, length, QDateTime::currentDateTime().toString(QSL("yyyy")));
} else if (tl == QSL("m")) {
uniq.replace(pos, length, QDateTime::currentDateTime().toString(QSL("MM")));
} else if (tl == QSL("d")) {
uniq.replace(pos, length, QDateTime::currentDateTime().toString(QSL("dd")));
} else if (tl == QSL("t")) {
uniq.replace(pos, length, QDateTime::currentDateTime().toString(QSL("hh-mm-ss")));
}
}
}
QDir d(dir);
QString ext;
if (!format.isEmpty())
ext = QSL(".%1").arg(format.toLower());
QFileInfo fi(d.absoluteFilePath(QSL("%1%2").arg(uniq,ext)));
int idx = 1;
while (fi.exists()) {
fi.setFile(d.absoluteFilePath(QSL("%1-%3%2").arg(uniq,ext).arg(idx)));
idx++;
}
if (withoutPath)
return fi.fileName();
return fi.absoluteFilePath();
}
QString ZGenericFuncs::generateFilter(const QStringList &ext)
{
QString filter;
for (int i=0;i<ext.count();i++) {
if (i>0)
filter.append(QSL(";;"));
const QString itm = ext.at(i);
filter.append(QSL("%1 (*.%2)").arg(itm.toUpper(),itm.toLower()));
}
return filter;
}
struct iiibiiay
{
explicit iiibiiay(const QImage& pic);
iiibiiay() = default;
int width { 0 };
int height { 0 };
int bytesPerLine { 0 };
bool alpha { false };
int channels { 0 };
int bitPerPixel { 0 };
QByteArray data;
};
Q_DECLARE_METATYPE(iiibiiay)
iiibiiay::iiibiiay(const QImage &pic)
{
QImage img(pic);
if(img.format()!=QImage::Format_ARGB32)
img = img.convertToFormat(QImage::Format_ARGB32);
img = img.rgbSwapped();
width = img.width();
height = img.height();
bytesPerLine = img.bytesPerLine();
alpha = img.hasAlphaChannel();
channels = (img.isGrayscale()?1:(alpha?4:3));
bitPerPixel = img.depth()/channels;
data.append(reinterpret_cast<const char *>(img.constBits()),
static_cast<int>(img.sizeInBytes()));
}
QDBusArgument &operator<<(QDBusArgument &a, const iiibiiay &i)
{
a.beginStructure();
a << i.width <<i.height << i.bytesPerLine << i.alpha << i.bitPerPixel << i.channels << i.data;
a.endStructure();
return a;
}
const QDBusArgument &operator>>(const QDBusArgument &a, iiibiiay &i)
{
a.beginStructure();
a >> i.width >> i.height >> i.bytesPerLine >> i.alpha >> i.bitPerPixel >> i.channels >> i.data;
a.endStructure();
return a;
}
void ZGenericFuncs::sendDENotification(QWidget* parent, const QString &text, const QString &title, int timeout_ms)
{
static auto dbusID = qDBusRegisterMetaType<iiibiiay>(); // static initialization on first call, run once
Q_UNUSED(dbusID);
const int iconSize = 48;
QDBusInterface dbus(QSL("org.freedesktop.Notifications"),
QSL("/org/freedesktop/Notifications"),
QSL("org.freedesktop.Notifications"),
QDBusConnection::sessionBus());
quint32 replacesID = 0U;
QVariantList args;
args.append(QSL("scrcap")); // Application name
args.append(QVariant::fromValue(replacesID)); // Replaces ID
args.append(QVariant::fromValue(QString())); // icon
if (!title.isEmpty()) {
args.append(title); // Message title
} else {
args.append(text);
}
args.append(text); // Message body
args.append(QStringList()); // Actions
QVariantMap map;
iiibiiay img(parent->windowIcon().pixmap(iconSize).toImage());
map.insert(QSL("image-data"),QVariant::fromValue(img));
args.append(map); // Hints
args.append(timeout_ms); // Expiration timeout
dbus.callWithArgumentList(QDBus::AutoDetect, QSL("Notify"), args);
}