2023-05-22 16:17:51 +08:00
|
|
|
#include "FluTools.h"
|
2023-09-13 15:11:22 +08:00
|
|
|
|
2023-04-27 17:29:39 +08:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QUuid>
|
2023-06-26 18:12:58 +08:00
|
|
|
#include <QCursor>
|
2023-08-17 17:14:31 +08:00
|
|
|
#include <QScreen>
|
2023-08-24 15:50:37 +08:00
|
|
|
#include <QColor>
|
2023-08-16 13:14:00 +08:00
|
|
|
#include <QFileInfo>
|
2023-09-06 14:05:29 +08:00
|
|
|
#include <QProcess>
|
2023-09-04 18:37:55 +08:00
|
|
|
#include <QDir>
|
2023-09-17 20:36:33 +08:00
|
|
|
#include <QOpenGLContext>
|
2023-09-01 18:38:21 +08:00
|
|
|
#include <QCryptographicHash>
|
2023-08-10 16:08:27 +08:00
|
|
|
#include <QTextDocument>
|
2023-09-17 20:36:33 +08:00
|
|
|
#include <QQuickWindow>
|
2023-09-21 18:29:09 +08:00
|
|
|
#include <QDateTime>
|
2023-12-22 12:39:04 +08:00
|
|
|
#include <QSettings>
|
2023-09-17 20:36:33 +08:00
|
|
|
|
2024-04-12 16:26:32 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#pragma comment (lib, "user32.lib")
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <windowsx.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
FluTools::FluTools(QObject *parent) : QObject{parent} {
|
2023-09-17 20:36:33 +08:00
|
|
|
|
2023-04-27 17:29:39 +08:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
void FluTools::clipText(const QString &text) {
|
2023-04-27 17:29:39 +08:00
|
|
|
QGuiApplication::clipboard()->setText(text);
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::uuid() {
|
2024-03-01 00:19:12 +08:00
|
|
|
return QUuid::createUuid().toString().remove('-').remove('{').remove('}');
|
2023-04-27 17:29:39 +08:00
|
|
|
}
|
2023-05-12 19:26:49 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::readFile(const QString &fileName) {
|
2023-05-12 19:26:49 +08:00
|
|
|
QString content;
|
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
QTextStream stream(&file);
|
|
|
|
content = stream.readAll();
|
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::isMacos() {
|
2023-05-18 20:57:57 +08:00
|
|
|
#if defined(Q_OS_MACOS)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::isLinux() {
|
2023-05-18 20:57:57 +08:00
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::isWin() {
|
2023-05-18 20:57:57 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2023-05-12 19:26:49 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
int FluTools::qtMajor() {
|
2023-06-25 23:17:44 +08:00
|
|
|
const QString qtVersion = QString::fromLatin1(qVersion());
|
|
|
|
const QStringList versionParts = qtVersion.split('.');
|
|
|
|
return versionParts[0].toInt();
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
int FluTools::qtMinor() {
|
2023-06-25 23:17:44 +08:00
|
|
|
const QString qtVersion = QString::fromLatin1(qVersion());
|
|
|
|
const QStringList versionParts = qtVersion.split('.');
|
|
|
|
return versionParts[1].toInt();
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
void FluTools::setQuitOnLastWindowClosed(bool val) {
|
|
|
|
QGuiApplication::setQuitOnLastWindowClosed(val);
|
2023-05-31 15:39:59 +08:00
|
|
|
}
|
2023-06-26 18:12:58 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
void FluTools::setOverrideCursor(Qt::CursorShape shape) {
|
|
|
|
QGuiApplication::setOverrideCursor(QCursor(shape));
|
2023-06-26 18:12:58 +08:00
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
void FluTools::restoreOverrideCursor() {
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
2023-06-26 18:12:58 +08:00
|
|
|
}
|
2023-06-28 02:28:34 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
void FluTools::deleteLater(QObject *p) {
|
|
|
|
if (p) {
|
2024-03-27 00:36:56 +08:00
|
|
|
p->deleteLater();
|
2023-06-28 13:13:39 +08:00
|
|
|
}
|
2023-06-28 02:28:34 +08:00
|
|
|
}
|
2023-07-24 18:23:26 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::toLocalPath(const QUrl &url) {
|
2023-07-24 18:23:26 +08:00
|
|
|
return url.toLocalFile();
|
|
|
|
}
|
2023-08-10 16:08:27 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::getFileNameByUrl(const QUrl &url) {
|
2023-08-16 13:14:00 +08:00
|
|
|
return QFileInfo(url.toLocalFile()).fileName();
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::html2PlantText(const QString &html) {
|
2023-08-10 16:08:27 +08:00
|
|
|
QTextDocument textDocument;
|
|
|
|
textDocument.setHtml(html);
|
|
|
|
return textDocument.toPlainText();
|
|
|
|
}
|
2023-08-17 17:14:31 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QRect FluTools::getVirtualGeometry() {
|
|
|
|
return QGuiApplication::primaryScreen()->virtualGeometry();
|
2023-08-17 17:14:31 +08:00
|
|
|
}
|
2023-08-17 23:03:00 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::getApplicationDirPath() {
|
|
|
|
return QGuiApplication::applicationDirPath();
|
2023-08-17 23:03:00 +08:00
|
|
|
}
|
2023-08-18 19:17:45 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QUrl FluTools::getUrlByFilePath(const QString &path) {
|
2023-08-18 19:17:45 +08:00
|
|
|
return QUrl::fromLocalFile(path);
|
|
|
|
}
|
2023-08-24 15:50:37 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QColor FluTools::withOpacity(const QColor &color, qreal opacity) {
|
2024-04-03 19:30:15 +08:00
|
|
|
int alpha = qRound(opacity * 255) & 0xff;
|
|
|
|
return QColor::fromRgba((alpha << 24) | (color.rgba() & 0xffffff));
|
2023-08-24 15:50:37 +08:00
|
|
|
}
|
2023-09-01 18:38:21 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::md5(const QString &text) {
|
2023-09-01 18:38:21 +08:00
|
|
|
return QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Md5).toHex();
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::toBase64(const QString &text) {
|
2023-09-01 18:38:21 +08:00
|
|
|
return text.toUtf8().toBase64();
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::fromBase64(const QString &text) {
|
2023-09-01 18:38:21 +08:00
|
|
|
return QByteArray::fromBase64(text.toUtf8());
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::removeDir(const QString &dirPath) {
|
2023-09-04 18:37:55 +08:00
|
|
|
QDir qDir(dirPath);
|
|
|
|
return qDir.removeRecursively();
|
|
|
|
}
|
2023-09-05 16:48:04 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::removeFile(const QString &filePath) {
|
2023-09-06 14:05:29 +08:00
|
|
|
QFile file(filePath);
|
|
|
|
return file.remove();
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QString FluTools::sha256(const QString &text) {
|
2023-09-05 16:48:04 +08:00
|
|
|
return QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Sha256).toHex();
|
|
|
|
}
|
2023-09-06 14:05:29 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
void FluTools::showFileInFolder(const QString &path) {
|
2023-09-06 14:05:29 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
QProcess::startDetached("explorer.exe", {"/select,", QDir::toNativeSeparators(path)});
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
QFileInfo fileInfo(path);
|
|
|
|
auto process = "xdg-open";
|
|
|
|
auto arguments = { fileInfo.absoluteDir().absolutePath() };
|
|
|
|
QProcess::startDetached(process, arguments);
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_MACOS)
|
|
|
|
QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to reveal POSIX file \"" + path + "\""});
|
|
|
|
QProcess::execute("/usr/bin/osascript", {"-e", "tell application \"Finder\" to activate"});
|
|
|
|
#endif
|
|
|
|
}
|
2023-09-17 20:36:33 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::isSoftware() {
|
2023-09-17 20:36:33 +08:00
|
|
|
return QQuickWindow::sceneGraphBackend() == "software";
|
|
|
|
}
|
2023-09-19 00:31:49 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QPoint FluTools::cursorPos() {
|
2023-09-19 00:31:49 +08:00
|
|
|
return QCursor::pos();
|
|
|
|
}
|
2023-09-21 18:29:09 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
qint64 FluTools::currentTimestamp() {
|
2023-09-21 18:29:09 +08:00
|
|
|
return QDateTime::currentMSecsSinceEpoch();
|
|
|
|
}
|
2023-12-13 18:13:35 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QIcon FluTools::windowIcon() {
|
2023-12-13 18:13:35 +08:00
|
|
|
return QGuiApplication::windowIcon();
|
|
|
|
}
|
2023-12-20 21:58:59 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
int FluTools::cursorScreenIndex() {
|
2023-12-20 21:58:59 +08:00
|
|
|
int screenIndex = 0;
|
2024-04-11 14:51:43 +08:00
|
|
|
int screenCount = QGuiApplication::screens().count();
|
2023-12-20 21:58:59 +08:00
|
|
|
if (screenCount > 1) {
|
|
|
|
QPoint pos = QCursor::pos();
|
2024-04-11 14:51:43 +08:00
|
|
|
for (int i = 0; i <= screenCount - 1; ++i) {
|
|
|
|
if (QGuiApplication::screens().at(i)->geometry().contains(pos)) {
|
2023-12-20 21:58:59 +08:00
|
|
|
screenIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return screenIndex;
|
|
|
|
}
|
2023-12-22 12:39:04 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
int FluTools::windowBuildNumber() {
|
2023-12-29 23:09:46 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
2024-04-11 14:51:43 +08:00
|
|
|
QSettings regKey{QString::fromUtf8(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion)"), QSettings::NativeFormat};
|
2023-12-29 23:09:46 +08:00
|
|
|
if (regKey.contains(QString::fromUtf8("CurrentBuildNumber"))) {
|
|
|
|
auto buildNumber = regKey.value(QString::fromUtf8("CurrentBuildNumber")).toInt();
|
|
|
|
return buildNumber;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::isWindows11OrGreater() {
|
2023-12-22 12:39:04 +08:00
|
|
|
static QVariant var;
|
2024-04-11 14:51:43 +08:00
|
|
|
if (var.isNull()) {
|
2023-12-22 12:39:04 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
2023-12-29 23:09:46 +08:00
|
|
|
auto buildNumber = windowBuildNumber();
|
2024-04-11 14:51:43 +08:00
|
|
|
if (buildNumber >= 22000) {
|
2023-12-29 23:09:46 +08:00
|
|
|
var = QVariant::fromValue(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
var = QVariant::fromValue(false);
|
2024-04-11 14:51:43 +08:00
|
|
|
return false;
|
|
|
|
} else {
|
2023-12-29 23:09:46 +08:00
|
|
|
return var.toBool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
bool FluTools::isWindows10OrGreater() {
|
2023-12-29 23:09:46 +08:00
|
|
|
static QVariant var;
|
2024-04-11 14:51:43 +08:00
|
|
|
if (var.isNull()) {
|
2023-12-29 23:09:46 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
auto buildNumber = windowBuildNumber();
|
2024-04-11 14:51:43 +08:00
|
|
|
if (buildNumber >= 10240) {
|
2023-12-29 23:09:46 +08:00
|
|
|
var = QVariant::fromValue(true);
|
|
|
|
return true;
|
2023-12-29 11:13:10 +08:00
|
|
|
}
|
2023-12-22 12:39:04 +08:00
|
|
|
#endif
|
|
|
|
var = QVariant::fromValue(false);
|
2024-04-11 14:51:43 +08:00
|
|
|
return false;
|
|
|
|
} else {
|
2023-12-22 12:39:04 +08:00
|
|
|
return var.toBool();
|
|
|
|
}
|
|
|
|
}
|
2024-01-02 18:27:59 +08:00
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
QRect FluTools::desktopAvailableGeometry(QQuickWindow *window) {
|
2024-03-12 22:59:56 +08:00
|
|
|
return window->screen()->availableGeometry();
|
2024-01-02 18:27:59 +08:00
|
|
|
}
|
2024-04-12 16:26:32 +08:00
|
|
|
|
|
|
|
QString FluTools::getWallpaperFilePath() {
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
wchar_t path[MAX_PATH] = {};
|
|
|
|
if (::SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, path, FALSE) == FALSE) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return QString::fromWCharArray(path);
|
2024-04-13 10:51:24 +08:00
|
|
|
#elif defined(Q_OS_LINUX)
|
2024-04-13 10:50:27 +08:00
|
|
|
auto type = QSysInfo::productType();
|
|
|
|
if (type == "uos") {
|
|
|
|
QProcess process;
|
|
|
|
QStringList args;
|
|
|
|
args << "--session";
|
|
|
|
args << "--type=method_call";
|
|
|
|
args << "--print-reply";
|
|
|
|
args << "--dest=com.deepin.wm";
|
|
|
|
args << "/com/deepin/wm";
|
|
|
|
args << "com.deepin.wm.GetCurrentWorkspaceBackgroundForMonitor";
|
|
|
|
args << QString("string:'%1'").arg(currentTimestamp());
|
|
|
|
process.start("dbus-send", args);
|
|
|
|
process.waitForFinished();
|
|
|
|
QByteArray result = process.readAllStandardOutput().trimmed();
|
|
|
|
int startIndex = result.indexOf("file:///");
|
|
|
|
if (startIndex != -1) {
|
|
|
|
auto path = result.mid(startIndex + 7, result.length() - startIndex - 8);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2024-04-12 16:26:32 +08:00
|
|
|
return {};
|
2024-04-13 10:50:27 +08:00
|
|
|
#endif
|
2024-04-12 16:26:32 +08:00
|
|
|
}
|
|
|
|
|
2024-04-13 10:50:27 +08:00
|
|
|
QColor FluTools::imageMainColor(const QImage &image, double bright) {
|
2024-04-12 16:26:32 +08:00
|
|
|
int step = 20;
|
|
|
|
int t = 0;
|
|
|
|
int r = 0, g = 0, b = 0;
|
|
|
|
for (int i = 0; i < image.width(); i += step) {
|
|
|
|
for (int j = 0; j < image.height(); j += step) {
|
|
|
|
if (image.valid(i, j)) {
|
|
|
|
t++;
|
|
|
|
QColor c = image.pixel(i, j);
|
|
|
|
r += c.red();
|
|
|
|
b += c.blue();
|
|
|
|
g += c.green();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QColor(int(bright * r / t) > 255 ? 255 : int(bright * r / t), int(bright * g / t) > 255 ? 255 : int(bright * g / t), int(bright * b / t) > 255 ? 255 : int(bright * b / t));
|
|
|
|
}
|