2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2022-09-26 11:18:43 +08:00
|
|
|
#include "qt_utils/utils.h"
|
2021-04-13 22:17:18 +08:00
|
|
|
#include <QApplication>
|
2021-04-06 23:10:57 +08:00
|
|
|
#include <QByteArray>
|
2021-04-15 11:18:14 +08:00
|
|
|
#include <QDir>
|
2021-04-13 22:17:18 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2021-04-15 11:18:14 +08:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QProcessEnvironment>
|
2021-04-06 23:10:57 +08:00
|
|
|
#include <QString>
|
|
|
|
#include <QTextCodec>
|
2021-04-20 22:24:33 +08:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QDebug>
|
2021-06-18 21:48:40 +08:00
|
|
|
#include <QStyleFactory>
|
2021-06-25 12:40:11 +08:00
|
|
|
#include <QDateTime>
|
2021-11-03 23:27:05 +08:00
|
|
|
#include <QColor>
|
2022-01-26 21:36:31 +08:00
|
|
|
#include <QWindow>
|
|
|
|
#include <QScreen>
|
2022-09-26 15:10:31 +08:00
|
|
|
#include <QDirIterator>
|
2021-12-26 21:26:00 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2022-09-25 17:43:31 +08:00
|
|
|
#include <QDirIterator>
|
2022-12-20 11:01:04 +08:00
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
2022-09-23 10:27:44 +08:00
|
|
|
#include <QMimeDatabase>
|
2021-12-26 21:26:00 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2022-10-23 00:39:24 +08:00
|
|
|
#include "charsetinfo.h"
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2022-09-26 14:54:28 +08:00
|
|
|
BaseError::BaseError(const QString &reason):
|
|
|
|
mReason(reason)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseError::reason() const
|
|
|
|
{
|
|
|
|
return mReason;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexOutOfRange::IndexOutOfRange(int Index):
|
|
|
|
BaseError(QObject::tr("Index %1 out of range").arg(Index))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FileError::FileError(const QString &reason): BaseError(reason)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
const QByteArray guessTextEncoding(const QByteArray& text){
|
2021-04-06 23:10:57 +08:00
|
|
|
bool allAscii;
|
|
|
|
int ii;
|
|
|
|
int size;
|
2021-04-11 12:39:22 +08:00
|
|
|
const QByteArray& s=text;
|
2021-04-06 23:10:57 +08:00
|
|
|
size = s.length();
|
|
|
|
if ( (size >= 3) && ((unsigned char)s[0]==0xEF) && ((unsigned char)s[1]==0xBB) && ((unsigned char)s[2]==0xBF)) {
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_UTF8_BOM;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
allAscii = true;
|
|
|
|
ii = 0;
|
|
|
|
while (ii < size) {
|
|
|
|
unsigned char ch = s[ii];
|
|
|
|
if (ch < 0x80 ) {
|
|
|
|
ii++; // is an ascii char
|
|
|
|
} else if (ch < 0xC0) { // value between 0x80 and 0xC0 is an invalid UTF-8 char
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_SYSTEM_DEFAULT;
|
2021-04-06 23:10:57 +08:00
|
|
|
} else if (ch < 0xE0) { // should be an 2-byte UTF-8 char
|
|
|
|
if (ii>=size-1) {
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_SYSTEM_DEFAULT;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
unsigned char ch2=s[ii+1];
|
|
|
|
if ((ch2 & 0xC0) !=0x80) {
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_SYSTEM_DEFAULT;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
allAscii = false;
|
|
|
|
ii+=2;
|
|
|
|
} else if (ch < 0xF0) { // should be an 3-byte UTF-8 char
|
|
|
|
if (ii>=size-2) {
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_SYSTEM_DEFAULT;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
unsigned char ch2=s[ii+1];
|
|
|
|
unsigned char ch3=s[ii+2];
|
|
|
|
if (((ch2 & 0xC0)!=0x80) || ((ch3 & 0xC0)!=0x80)) {
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_SYSTEM_DEFAULT;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
allAscii = false;
|
|
|
|
ii+=3;
|
|
|
|
} else { // invalid UTF-8 char
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_SYSTEM_DEFAULT;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allAscii)
|
2021-04-08 10:29:21 +08:00
|
|
|
return ENCODING_ASCII;
|
|
|
|
return ENCODING_UTF8;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
|
|
|
|
|
2021-09-26 22:39:26 +08:00
|
|
|
bool isTextAllAscii(const QByteArray& text) {
|
|
|
|
for (char c:text) {
|
2023-01-14 21:51:55 +08:00
|
|
|
if (c<=0) {
|
2021-09-26 22:39:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-07 21:13:15 +08:00
|
|
|
bool isTextAllAscii(const QString& text) {
|
|
|
|
for (QChar c:text) {
|
|
|
|
if (c.unicode()>127) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2021-04-06 23:10:57 +08:00
|
|
|
}
|
2021-04-07 21:13:15 +08:00
|
|
|
|
2021-04-15 11:18:14 +08:00
|
|
|
bool isNonPrintableAsciiChar(char ch)
|
|
|
|
{
|
2021-08-16 23:17:48 +08:00
|
|
|
return (ch<=32) && (ch>=0);
|
2021-04-15 11:18:14 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QStringList textToLines(const QString &text)
|
2021-04-15 11:18:14 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QTextStream stream(&((QString&)text),QIODevice::ReadOnly);
|
|
|
|
return readStreamToLines(&stream);
|
2021-04-15 11:18:14 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
|
|
|
|
void textToLines(const QString &text, LineProcessFunc lineFunc)
|
2021-04-15 11:18:14 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QTextStream stream(&((QString&)text),QIODevice::ReadOnly);
|
|
|
|
readStreamToLines(&stream,lineFunc);
|
2021-04-15 11:18:14 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString linesToText(const QStringList &lines, const QString& lineBreak)
|
2021-04-15 11:18:14 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
return lines.join(lineBreak);
|
2021-04-15 11:18:14 +08:00
|
|
|
}
|
2021-04-15 21:32:45 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QList<QByteArray> splitByteArrayToLines(const QByteArray &content)
|
2021-04-15 21:32:45 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QList<QByteArray> lines;
|
|
|
|
const char* p =content.constData();
|
|
|
|
const char* end = p+content.length();
|
|
|
|
const char* lineStart = p;
|
|
|
|
QByteArray line;
|
|
|
|
while (p<=end) {
|
|
|
|
char ch=*p;
|
|
|
|
switch(ch) {
|
|
|
|
case '\r':
|
|
|
|
line = QByteArray(lineStart, p-lineStart);
|
|
|
|
lines.append(line);
|
|
|
|
p++;
|
|
|
|
if (*p=='\n')
|
|
|
|
p++;
|
|
|
|
lineStart = p;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
line = QByteArray(lineStart, p-lineStart);
|
|
|
|
lines.append(line);
|
|
|
|
p++;
|
|
|
|
lineStart = p;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lineStart>end) {
|
|
|
|
lines.append("");
|
2021-04-15 21:32:45 +08:00
|
|
|
} else {
|
2022-09-25 17:43:31 +08:00
|
|
|
line = QByteArray(lineStart, end-lineStart+1);
|
|
|
|
lines.append(line);
|
2021-04-15 21:32:45 +08:00
|
|
|
}
|
2022-09-25 17:43:31 +08:00
|
|
|
return lines;
|
2021-04-15 21:32:45 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString trimRight(const QString &s)
|
2021-04-20 22:24:33 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
if (s.isEmpty())
|
|
|
|
return s;
|
|
|
|
int i = s.length()-1;
|
|
|
|
// while ((i>=0) && ((s[i] == '\r') || (s[i]=='\n') || (s[i] == '\t') || (s[i]==' '))) {
|
|
|
|
while ((i>=0) && (s[i]<=32)) {
|
|
|
|
i--;
|
|
|
|
};
|
|
|
|
if (i>=0) {
|
|
|
|
return s.left(i+1);
|
|
|
|
} else {
|
|
|
|
return QString();
|
2022-09-23 10:27:44 +08:00
|
|
|
}
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString trimLeft(const QString &s)
|
2021-04-20 22:24:33 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
if (s.isEmpty())
|
|
|
|
return s;
|
|
|
|
int i=0;
|
|
|
|
// while ((i<s.length()) && ((s[i] == '\r') || (s[i]=='\n') || (s[i] == '\t') || (s[i]==' '))) {
|
|
|
|
// i++;
|
|
|
|
// };
|
|
|
|
while ((i<s.length()) && (s[i]<=32)) {
|
|
|
|
i++;
|
|
|
|
};
|
|
|
|
if (i<s.length()) {
|
|
|
|
return s.mid(i);
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
int countLeadingWhitespaceChars(const QString &line)
|
2021-04-20 22:24:33 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
int n=0;
|
|
|
|
while (n<line.length()) {
|
|
|
|
if (line[n].unicode()>32)
|
|
|
|
break;
|
|
|
|
n++;
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
2022-09-25 17:43:31 +08:00
|
|
|
return n;
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
2021-04-21 18:58:35 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool stringIsBlank(const QString &s)
|
2021-04-21 18:58:35 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
for (QChar ch:s) {
|
|
|
|
if (ch != ' ' && ch != '\t')
|
|
|
|
return false;
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
2021-12-24 23:18:20 +08:00
|
|
|
return true;
|
2021-04-21 18:58:35 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QByteArray toByteArray(const QString &s)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
//return s.toLocal8Bit();
|
|
|
|
return s.toUtf8();
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString fromByteArray(const QByteArray &s)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QTextCodec* codec = QTextCodec::codecForName(ENCODING_UTF8);
|
|
|
|
QTextCodec::ConverterState state;
|
|
|
|
if (!codec)
|
|
|
|
return QString(s);
|
|
|
|
QString tmp = codec->toUnicode(s,s.length(),&state);
|
|
|
|
if (state.invalidChars>0)
|
|
|
|
tmp = QString::fromLocal8Bit(s);
|
|
|
|
return tmp;
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
QStringList readStreamToLines(QTextStream *stream)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
QString s;
|
|
|
|
while (stream->readLineInto(&s)) {
|
|
|
|
list.append(s);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
void readStreamToLines(QTextStream *stream,
|
2021-05-03 10:15:40 +08:00
|
|
|
LineProcessFunc lineFunc)
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
while (stream->readLineInto(&s)) {
|
|
|
|
lineFunc(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QStringList readFileToLines(const QString& fileName, QTextCodec* codec)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream.setCodec(codec);
|
|
|
|
stream.setAutoDetectUnicode(false);
|
|
|
|
return readStreamToLines(&stream);
|
|
|
|
}
|
|
|
|
return QStringList();
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
void readFileToLines(const QString &fileName, QTextCodec *codec, LineProcessFunc lineFunc)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream.setCodec(codec);
|
|
|
|
stream.setAutoDetectUnicode(false);
|
2021-11-12 10:51:00 +08:00
|
|
|
readStreamToLines(&stream, lineFunc);
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 00:39:24 +08:00
|
|
|
static QStringList tryLoadFileByEncoding(QByteArray encodingName, QFile& file, bool* isOk) {
|
|
|
|
QStringList result;
|
|
|
|
*isOk=false;
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName(encodingName);
|
|
|
|
if (!codec)
|
|
|
|
return result;
|
|
|
|
file.reset();
|
|
|
|
QTextCodec::ConverterState state;
|
|
|
|
while (true) {
|
|
|
|
if (file.atEnd()){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
if (line.endsWith("\r\n")) {
|
|
|
|
line.remove(line.length()-2,2);
|
|
|
|
} else if (line.endsWith("\r")) {
|
|
|
|
line.remove(line.length()-1,1);
|
|
|
|
} else if (line.endsWith("\n")){
|
|
|
|
line.remove(line.length()-1,1);
|
|
|
|
}
|
|
|
|
QString newLine = codec->toUnicode(line.constData(),line.length(),&state);
|
|
|
|
if (state.invalidChars>0) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
result.append(newLine);
|
|
|
|
}
|
|
|
|
*isOk=true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QStringList readFileToLines(const QString &fileName)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFile file(fileName);
|
|
|
|
if (file.size()<=0)
|
|
|
|
return QStringList();
|
|
|
|
QStringList result;
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
2022-10-23 00:39:24 +08:00
|
|
|
bool ok;
|
|
|
|
result = tryLoadFileByEncoding("UTF-8",file,&ok);
|
|
|
|
if (ok) {
|
|
|
|
return result;
|
2022-09-25 17:43:31 +08:00
|
|
|
}
|
2022-10-23 00:39:24 +08:00
|
|
|
|
|
|
|
QByteArray realEncoding = pCharsetInfoManager->getDefaultSystemEncoding();
|
|
|
|
result = tryLoadFileByEncoding(realEncoding,file,&ok);
|
|
|
|
if (ok) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
QList<PCharsetInfo> charsets = pCharsetInfoManager->findCharsetByLocale(pCharsetInfoManager->localeName());
|
|
|
|
if (!charsets.isEmpty()) {
|
|
|
|
QSet<QByteArray> encodingSet;
|
|
|
|
for (int i=0;i<charsets.size();i++) {
|
|
|
|
encodingSet.insert(charsets[i]->name);
|
|
|
|
}
|
|
|
|
encodingSet.remove(realEncoding);
|
|
|
|
foreach (const QByteArray& encodingName,encodingSet) {
|
|
|
|
if (encodingName == ENCODING_UTF8)
|
|
|
|
continue;
|
|
|
|
result = tryLoadFileByEncoding("UTF-8",file,&ok);
|
|
|
|
if (ok) {
|
|
|
|
return result;
|
2022-09-25 17:43:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QByteArray readFileToByteArray(const QString &fileName)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
return file.readAll();
|
|
|
|
}
|
|
|
|
return QByteArray();
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 22:52:04 +08:00
|
|
|
bool stringToFile(const QString &str, const QString &fileName)
|
2021-05-03 10:15:40 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFile file(fileName);
|
2023-01-14 22:52:04 +08:00
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
|
|
|
return false;
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream<<str;
|
|
|
|
return true;
|
2021-05-03 10:15:40 +08:00
|
|
|
}
|
|
|
|
|
2021-05-06 20:55:55 +08:00
|
|
|
|
2023-01-14 22:52:04 +08:00
|
|
|
bool stringsToFile(const QStringList &list, const QString &fileName)
|
2021-10-10 22:19:48 +08:00
|
|
|
{
|
|
|
|
QFile file(fileName);
|
2023-01-15 15:47:08 +08:00
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
2023-01-14 22:52:04 +08:00
|
|
|
return false;
|
|
|
|
QTextStream stream(&file);
|
|
|
|
for (const QString& s:list) {
|
|
|
|
stream<<s
|
2022-09-25 17:43:31 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
|
2023-01-14 22:52:04 +08:00
|
|
|
<<Qt::endl;
|
2022-09-25 17:43:31 +08:00
|
|
|
#else
|
2023-01-14 22:52:04 +08:00
|
|
|
<<endl;
|
2022-09-25 17:43:31 +08:00
|
|
|
#endif
|
2021-11-12 10:51:00 +08:00
|
|
|
}
|
2023-01-14 22:52:04 +08:00
|
|
|
return true;
|
2021-11-12 10:51:00 +08:00
|
|
|
}
|
2021-12-09 08:10:14 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool fileExists(const QString &file)
|
2021-12-09 08:10:14 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
if (file.isEmpty())
|
|
|
|
return false;
|
|
|
|
return QFile(file).exists();
|
2021-12-09 08:10:14 +08:00
|
|
|
}
|
2021-12-17 09:05:01 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool fileExists(const QString &dir, const QString &fileName)
|
2021-12-17 09:05:01 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
if (dir.isEmpty() || fileName.isEmpty())
|
|
|
|
return false;
|
|
|
|
QDir dirInfo(dir);
|
|
|
|
return dirInfo.exists(fileName);
|
2021-12-17 09:05:01 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool directoryExists(const QString &file)
|
2021-12-17 09:05:01 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
if (file.isEmpty())
|
|
|
|
return false;
|
|
|
|
QFileInfo dir(file);
|
|
|
|
return dir.exists() && dir.isDir();
|
2021-12-17 09:05:01 +08:00
|
|
|
}
|
2022-01-04 16:50:54 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool removeFile(const QString &filename)
|
2022-01-04 16:50:54 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFile file(filename);
|
|
|
|
return file.remove();
|
2022-01-04 16:50:54 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool copyFile(const QString &fromPath, const QString &toPath, bool overwrite)
|
2022-01-04 16:50:54 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFile fromFile(fromPath);
|
|
|
|
QFile toFile(toPath);
|
|
|
|
if (!fromFile.exists())
|
|
|
|
return false;
|
|
|
|
if (toFile.exists()) {
|
|
|
|
if (!overwrite)
|
|
|
|
return false;
|
|
|
|
if (!toFile.remove())
|
|
|
|
return false;
|
2022-01-26 21:36:31 +08:00
|
|
|
}
|
2022-01-11 22:29:03 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
if (!fromFile.open(QFile::ReadOnly))
|
|
|
|
return false;
|
|
|
|
if (!toFile.open(QFile::WriteOnly | QFile::Truncate))
|
|
|
|
return false;
|
2022-01-24 18:37:16 +08:00
|
|
|
|
2022-10-28 12:43:47 +08:00
|
|
|
constexpr int bufferSize=64*1024;
|
2022-09-25 17:43:31 +08:00
|
|
|
char buffer[bufferSize];
|
|
|
|
|
|
|
|
while (!fromFile.atEnd()) {
|
|
|
|
int readed = fromFile.read(buffer,bufferSize);
|
|
|
|
toFile.write(buffer,readed);
|
|
|
|
}
|
|
|
|
toFile.close();
|
|
|
|
fromFile.close();
|
|
|
|
return true;
|
2022-01-24 18:37:16 +08:00
|
|
|
}
|
2022-01-27 12:08:57 +08:00
|
|
|
|
|
|
|
void copyFolder(const QString &fromDir, const QString &toDir)
|
|
|
|
{
|
|
|
|
QDirIterator it(fromDir);
|
|
|
|
QDir dir(fromDir);
|
|
|
|
QDir targetDir(toDir);
|
|
|
|
const int absSourcePathLength = dir.absolutePath().length();
|
|
|
|
|
|
|
|
|
|
|
|
if (targetDir.exists())
|
|
|
|
return;
|
|
|
|
targetDir.mkpath(targetDir.absolutePath());
|
|
|
|
|
|
|
|
while (it.hasNext()){
|
|
|
|
it.next();
|
|
|
|
const auto fileInfo = it.fileInfo();
|
2022-02-08 12:33:10 +08:00
|
|
|
if(!fileInfo.isHidden() && !fileInfo.fileName().startsWith('.')) { //filters dot and dotdot
|
2022-01-27 12:08:57 +08:00
|
|
|
const QString subPathStructure = fileInfo.absoluteFilePath().mid(absSourcePathLength);
|
|
|
|
const QString constructedAbsolutePath = targetDir.absolutePath() + subPathStructure;
|
|
|
|
if(fileInfo.isDir()){
|
|
|
|
//Create directory in target folder
|
|
|
|
dir.mkpath(constructedAbsolutePath);
|
|
|
|
copyFolder(fileInfo.absoluteFilePath(), constructedAbsolutePath);
|
|
|
|
} else if(fileInfo.isFile()) {
|
|
|
|
//Copy File to target directory
|
|
|
|
|
|
|
|
//Remove file at target location, if it exists, or QFile::copy will fail
|
|
|
|
QFile::remove(constructedAbsolutePath);
|
|
|
|
QFile::copy(fileInfo.absoluteFilePath(), constructedAbsolutePath);
|
2022-01-27 12:33:00 +08:00
|
|
|
QFile newFile(constructedAbsolutePath);
|
|
|
|
QFile::Permissions permissions = newFile.permissions();
|
|
|
|
permissions |= (QFile::Permission::WriteOwner
|
|
|
|
| QFile::Permission::WriteUser
|
|
|
|
| QFile::Permission::WriteGroup
|
|
|
|
| QFile::Permission::WriteOther);
|
|
|
|
newFile.setPermissions(permissions);
|
2022-01-27 12:08:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 00:01:50 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString includeTrailingPathDelimiter(const QString &path)
|
|
|
|
{
|
|
|
|
if (path.endsWith('/') || path.endsWith(QDir::separator())) {
|
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
return path + "/";
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 00:01:50 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString excludeTrailingPathDelimiter(const QString &path)
|
2022-03-01 22:03:54 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
int pos = path.length()-1;
|
|
|
|
while (pos>=0 && (path[pos]=='/' || path[pos]==QDir::separator()))
|
|
|
|
pos--;
|
|
|
|
return path.mid(0,pos+1);
|
2022-03-01 22:03:54 +08:00
|
|
|
}
|
2022-03-10 14:58:53 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString changeFileExt(const QString& filename, QString ext)
|
2022-03-10 14:58:53 +08:00
|
|
|
{
|
2022-09-25 17:43:31 +08:00
|
|
|
QFileInfo fileInfo(filename);
|
|
|
|
QString suffix = fileInfo.suffix();
|
|
|
|
QString name = fileInfo.fileName();
|
|
|
|
QString path;
|
|
|
|
if (!ext.isEmpty() && !ext.startsWith(".")) {
|
|
|
|
ext = "."+ext;
|
|
|
|
}
|
|
|
|
if (fileInfo.path() != ".") {
|
|
|
|
path = includeTrailingPathDelimiter(fileInfo.path());
|
|
|
|
}
|
|
|
|
if (suffix.isEmpty()) {
|
|
|
|
return path+filename+ext;
|
|
|
|
} else {
|
|
|
|
return path+fileInfo.completeBaseName()+ext;
|
2022-03-10 14:58:53 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-07 22:25:52 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString extractRelativePath(const QString &base, const QString &dest)
|
2022-08-07 22:25:52 +08:00
|
|
|
{
|
2022-11-10 14:47:44 +08:00
|
|
|
if (dest.isEmpty())
|
|
|
|
return QString();
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QFileInfo baseInfo(base);
|
|
|
|
QDir baseDir;
|
|
|
|
if (baseInfo.isDir()) {
|
|
|
|
baseDir = QDir(baseInfo.absoluteFilePath());
|
|
|
|
} else {
|
|
|
|
baseDir = baseInfo.absoluteDir();
|
2022-08-07 22:25:52 +08:00
|
|
|
}
|
2022-09-25 17:43:31 +08:00
|
|
|
return baseDir.relativeFilePath(dest);
|
|
|
|
}
|
2022-08-07 22:25:52 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString localizePath(const QString &path)
|
|
|
|
{
|
|
|
|
QString result = path;
|
|
|
|
result.replace("/",QDir::separator());
|
|
|
|
return result;
|
|
|
|
}
|
2022-08-07 22:25:52 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString extractFileName(const QString &fileName)
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo(fileName);
|
|
|
|
return fileInfo.fileName();
|
|
|
|
}
|
2022-08-07 22:25:52 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString extractFileDir(const QString &fileName)
|
|
|
|
{
|
|
|
|
return extractFilePath(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString extractFilePath(const QString &filePath)
|
|
|
|
{
|
|
|
|
QFileInfo info(filePath);
|
|
|
|
return info.path();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString extractAbsoluteFilePath(const QString &filePath)
|
|
|
|
{
|
|
|
|
QFileInfo info(filePath);
|
|
|
|
return info.absoluteFilePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReadOnly(const QString &filename)
|
|
|
|
{
|
|
|
|
return QFile(filename).isWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
int compareFileModifiedTime(const QString &filename1, const QString &filename2)
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo1(filename1);
|
|
|
|
QFileInfo fileInfo2(filename2);
|
|
|
|
qint64 time1=fileInfo1.lastModified().toMSecsSinceEpoch();
|
|
|
|
qint64 time2=fileInfo2.lastModified().toMSecsSinceEpoch();
|
|
|
|
if (time1 > time2)
|
|
|
|
return 1;
|
|
|
|
if (time1 < time2)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void inflateRect(QRect &rect, int delta)
|
|
|
|
{
|
|
|
|
inflateRect(rect,delta,delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
void inflateRect(QRect &rect, int dx, int dy)
|
|
|
|
{
|
|
|
|
rect.setLeft(rect.left()-dx);
|
|
|
|
rect.setRight(rect.right()+dx);
|
|
|
|
rect.setTop(rect.top()-dy);
|
|
|
|
rect.setBottom(rect.bottom()+dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int defaultScreenDPI = -1;
|
|
|
|
|
|
|
|
int screenDPI()
|
|
|
|
{
|
|
|
|
if (defaultScreenDPI<1) {
|
|
|
|
defaultScreenDPI = qApp->primaryScreen()->logicalDotsPerInch();
|
2022-08-07 22:25:52 +08:00
|
|
|
}
|
2022-09-25 17:43:31 +08:00
|
|
|
return defaultScreenDPI;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setScreenDPI(int dpi)
|
|
|
|
{
|
|
|
|
defaultScreenDPI = dpi;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pointToPixel(float point, float dpi)
|
|
|
|
{
|
|
|
|
return point * dpi / 72;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pointToPixel(float point)
|
|
|
|
{
|
|
|
|
return pointToPixel(point,screenDPI());
|
|
|
|
}
|
|
|
|
|
|
|
|
float pixelToPoint(float pixel)
|
|
|
|
{
|
|
|
|
return pixel * 72 / screenDPI();
|
2022-08-07 22:25:52 +08:00
|
|
|
}
|
2022-09-25 17:43:31 +08:00
|
|
|
|
|
|
|
|
2022-09-26 14:54:28 +08:00
|
|
|
void decodeKey(const int combinedKey, int &key, Qt::KeyboardModifiers &modifiers)
|
|
|
|
{
|
|
|
|
modifiers = Qt::NoModifier;
|
|
|
|
if (combinedKey & Qt::ShiftModifier) {
|
|
|
|
modifiers|=Qt::ShiftModifier;
|
|
|
|
}
|
|
|
|
if (combinedKey & Qt::ControlModifier) {
|
|
|
|
modifiers|=Qt::ControlModifier;
|
|
|
|
}
|
|
|
|
if (combinedKey & Qt::AltModifier) {
|
|
|
|
modifiers|=Qt::AltModifier;
|
|
|
|
}
|
|
|
|
if (combinedKey & Qt::MetaModifier) {
|
|
|
|
modifiers|=Qt::MetaModifier;
|
|
|
|
}
|
|
|
|
if (combinedKey & Qt::KeypadModifier) {
|
|
|
|
modifiers|= Qt::KeypadModifier;
|
|
|
|
}
|
|
|
|
key = combinedKey & ~(Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier);
|
|
|
|
}
|
2022-09-30 17:51:26 +08:00
|
|
|
|
|
|
|
bool isInFolder(const QString &folderpath, const QString &filepath)
|
|
|
|
{
|
|
|
|
QDir folder(folderpath);
|
|
|
|
QFileInfo fileInfo(filepath);
|
|
|
|
return fileInfo.absoluteFilePath().startsWith(includeTrailingPathDelimiter(folder.absolutePath()));
|
|
|
|
}
|
2022-10-21 12:43:02 +08:00
|
|
|
|
|
|
|
void createFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
stringToFile("",fileName);
|
|
|
|
}
|
2022-10-24 17:23:37 +08:00
|
|
|
|
|
|
|
QString cleanPath(const QString &dirPath)
|
|
|
|
{
|
|
|
|
return QDir::cleanPath(dirPath);
|
|
|
|
}
|
|
|
|
|
2022-11-16 09:24:42 +08:00
|
|
|
QString generateAbsolutePath(const QString &dirPath, const QString &relativePath)
|
2022-10-24 17:23:37 +08:00
|
|
|
{
|
2022-11-10 14:47:44 +08:00
|
|
|
if (relativePath.isEmpty())
|
|
|
|
return QString();
|
2022-10-24 17:23:37 +08:00
|
|
|
return QDir::cleanPath(QDir(dirPath).absoluteFilePath(relativePath));
|
|
|
|
}
|
2022-10-29 17:21:38 +08:00
|
|
|
|
|
|
|
QString escapeSpacesInString(const QString &str)
|
|
|
|
{
|
|
|
|
QString result=str;
|
|
|
|
return result.replace(' ',"%20");
|
|
|
|
}
|
2022-11-10 14:47:44 +08:00
|
|
|
|
|
|
|
QStringList extractRelativePaths(const QString &base, const QStringList &destList)
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
foreach(const QString& dest,destList) {
|
|
|
|
list.append(extractRelativePath(base,dest));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList absolutePaths(const QString &dirPath, const QStringList &relativePaths)
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
foreach(const QString& path,relativePaths) {
|
2022-11-16 09:24:42 +08:00
|
|
|
list.append(generateAbsolutePath(dirPath,path));
|
2022-11-10 14:47:44 +08:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2022-12-20 11:01:04 +08:00
|
|
|
|
|
|
|
|