FluentUI/src/FluTools.cpp

162 lines
3.7 KiB
C++
Raw Normal View History

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-01 18:38:21 +08:00
#include <QCryptographicHash>
2023-08-10 16:08:27 +08:00
#include <QTextDocument>
2023-04-27 17:29:39 +08:00
2023-09-13 15:11:22 +08:00
FluTools::FluTools(QObject *parent):QObject{parent}{
2023-04-27 17:29:39 +08:00
}
void FluTools::clipText(const QString& text){
QGuiApplication::clipboard()->setText(text);
}
QString FluTools::uuid(){
return QUuid::createUuid().toString();
}
2023-05-12 19:26:49 +08:00
2023-09-13 15:11:22 +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;
}
2023-05-18 20:57:57 +08:00
bool FluTools::isMacos(){
#if defined(Q_OS_MACOS)
return true;
#else
return false;
#endif
}
bool FluTools::isLinux(){
#if defined(Q_OS_LINUX)
return true;
#else
return false;
#endif
}
bool FluTools::isWin(){
#if defined(Q_OS_WIN)
return true;
#else
return false;
#endif
}
2023-05-12 19:26:49 +08:00
2023-06-25 23:17:44 +08:00
int FluTools::qtMajor(){
const QString qtVersion = QString::fromLatin1(qVersion());
const QStringList versionParts = qtVersion.split('.');
return versionParts[0].toInt();
}
int FluTools::qtMinor(){
const QString qtVersion = QString::fromLatin1(qVersion());
const QStringList versionParts = qtVersion.split('.');
return versionParts[1].toInt();
}
2023-05-31 15:39:59 +08:00
void FluTools::setQuitOnLastWindowClosed(bool val){
qApp->setQuitOnLastWindowClosed(val);
}
2023-06-26 18:12:58 +08:00
void FluTools::setOverrideCursor(Qt::CursorShape shape){
qApp->setOverrideCursor(QCursor(shape));
}
void FluTools::restoreOverrideCursor(){
qApp->restoreOverrideCursor();
}
2023-06-28 02:28:34 +08:00
void FluTools::deleteItem(QObject *p){
2023-06-28 13:13:39 +08:00
if(p){
delete p;
p = nullptr;
}
2023-06-28 02:28:34 +08:00
}
2023-07-24 18:23:26 +08:00
QString FluTools::toLocalPath(const QUrl& url){
return url.toLocalFile();
}
2023-08-10 16:08:27 +08:00
2023-08-16 13:14:00 +08:00
QString FluTools::getFileNameByUrl(const QUrl& url){
return QFileInfo(url.toLocalFile()).fileName();
}
2023-08-10 16:08:27 +08:00
QString FluTools::html2PlantText(const QString& html){
QTextDocument textDocument;
textDocument.setHtml(html);
return textDocument.toPlainText();
}
2023-08-17 17:14:31 +08:00
QRect FluTools::getVirtualGeometry(){
return qApp->primaryScreen()->virtualGeometry();
}
2023-08-17 23:03:00 +08:00
QString FluTools::getApplicationDirPath(){
return qApp->applicationDirPath();
}
2023-08-18 19:17:45 +08:00
QUrl FluTools::getUrlByFilePath(const QString& path){
return QUrl::fromLocalFile(path);
}
2023-08-24 15:50:37 +08:00
QColor FluTools::colorAlpha(const QColor& color,qreal alpha){
return QColor(color.red(),color.green(),color.blue(),255*alpha);
}
2023-09-01 18:38:21 +08:00
2023-09-13 15:11:22 +08:00
QString FluTools::md5(QString text){
2023-09-01 18:38:21 +08:00
return QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Md5).toHex();
}
2023-09-13 15:11:22 +08:00
QString FluTools::toBase64(QString text){
2023-09-01 18:38:21 +08:00
return text.toUtf8().toBase64();
}
2023-09-13 15:11:22 +08:00
QString FluTools::fromBase64(QString text){
2023-09-01 18:38:21 +08:00
return QByteArray::fromBase64(text.toUtf8());
}
2023-09-04 18:37:55 +08:00
bool FluTools::removeDir(QString dirPath){
QDir qDir(dirPath);
return qDir.removeRecursively();
}
2023-09-05 16:48:04 +08:00
2023-09-06 14:05:29 +08:00
bool FluTools::removeFile(QString filePath){
QFile file(filePath);
return file.remove();
}
2023-09-05 16:48:04 +08:00
QString FluTools::sha256(QString text){
return QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Sha256).toHex();
}
2023-09-06 14:05:29 +08:00
void FluTools::showFileInFolder(QString path){
#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
}