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
|
|
|
#ifndef REDPANDA_QT_UTILS_H
|
|
|
|
#define REDPANDA_QT_UTILS_H
|
2021-04-12 00:00:29 +08:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2021-05-03 10:15:40 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <QString>
|
2021-05-21 23:33:53 +08:00
|
|
|
#include <QRect>
|
2021-08-13 22:53:26 +08:00
|
|
|
#include <QStringList>
|
2021-08-23 17:27:17 +08:00
|
|
|
#include <memory>
|
2021-12-09 09:27:46 +08:00
|
|
|
#include <QThread>
|
2021-12-16 11:36:52 +08:00
|
|
|
#include <QProcessEnvironment>
|
2021-09-16 12:03:10 +08:00
|
|
|
|
2021-04-06 23:10:57 +08:00
|
|
|
class QByteArray;
|
2021-05-03 10:15:40 +08:00
|
|
|
class QTextStream;
|
|
|
|
class QTextCodec;
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
#define ENCODING_AUTO_DETECT "AUTO"
|
|
|
|
#define ENCODING_UTF8 "UTF-8"
|
|
|
|
#define ENCODING_UTF8_BOM "UTF-8 BOM"
|
|
|
|
#define ENCODING_SYSTEM_DEFAULT "SYSTEM"
|
|
|
|
#define ENCODING_ASCII "ASCII"
|
2021-04-12 00:00:29 +08:00
|
|
|
|
2021-05-03 10:15:40 +08:00
|
|
|
enum class FileEndingType {
|
|
|
|
Windows,
|
|
|
|
Linux,
|
|
|
|
Mac
|
|
|
|
};// Windows: CRLF, UNIX: LF, Mac: CR
|
|
|
|
|
|
|
|
class BaseError{
|
|
|
|
public:
|
|
|
|
explicit BaseError(const QString& reason);
|
|
|
|
QString reason() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QString mReason;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IndexOutOfRange:public BaseError {
|
|
|
|
public:
|
|
|
|
explicit IndexOutOfRange(int Index);
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileError: public BaseError {
|
|
|
|
public:
|
|
|
|
explicit FileError(const QString& reason);
|
|
|
|
};
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
/* text processing utils */
|
|
|
|
const QByteArray guessTextEncoding(const QByteArray& text);
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-09-26 22:39:26 +08:00
|
|
|
bool isTextAllAscii(const QByteArray& text);
|
2021-04-07 21:13:15 +08:00
|
|
|
bool isTextAllAscii(const QString& text);
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-15 11:18:14 +08:00
|
|
|
bool isNonPrintableAsciiChar(char ch);
|
|
|
|
|
2021-05-03 10:15:40 +08:00
|
|
|
using LineProcessFunc = std::function<void(const QString&)>;
|
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
QStringList textToLines(const QString& text);
|
|
|
|
void textToLines(const QString& text, LineProcessFunc lineFunc);
|
2022-09-25 17:43:31 +08:00
|
|
|
QString linesToText(const QStringList& lines, const QString& lineBreak="\n");
|
2021-11-12 10:51:00 +08:00
|
|
|
|
|
|
|
QList<QByteArray> splitByteArrayToLines(const QByteArray& content);
|
2021-10-01 21:16:22 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString trimRight(const QString& s);
|
|
|
|
QString trimLeft(const QString& s);
|
|
|
|
|
|
|
|
int countLeadingWhitespaceChars(const QString& line);
|
|
|
|
|
|
|
|
bool stringIsBlank(const QString& s);
|
|
|
|
|
|
|
|
QByteArray toByteArray(const QString& s);
|
|
|
|
QString fromByteArray(const QByteArray& s);
|
|
|
|
|
|
|
|
/* text I/O utils */
|
|
|
|
|
|
|
|
QStringList readStreamToLines(QTextStream* stream);
|
|
|
|
void readStreamToLines(QTextStream* stream, LineProcessFunc lineFunc);
|
2021-05-03 10:15:40 +08:00
|
|
|
|
2022-03-10 15:05:16 +08:00
|
|
|
/**
|
|
|
|
* @brief readFileToLines
|
|
|
|
* @param fileName
|
|
|
|
* @param codec
|
|
|
|
* @return
|
|
|
|
*/
|
2021-11-12 10:51:00 +08:00
|
|
|
QStringList readFileToLines(const QString& fileName, QTextCodec* codec);
|
|
|
|
QStringList readFileToLines(const QString& fileName);
|
|
|
|
void readFileToLines(const QString& fileName, QTextCodec* codec, LineProcessFunc lineFunc);
|
2022-09-25 17:43:31 +08:00
|
|
|
|
|
|
|
QByteArray readFileToByteArray(const QString& fileName);
|
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
void stringsToFile(const QStringList& list, const QString& fileName);
|
|
|
|
void stringToFile(const QString& str, const QString& fileName);
|
2021-05-03 10:15:40 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
/* File I/O utils */
|
|
|
|
bool fileExists(const QString& file);
|
|
|
|
bool fileExists(const QString& dir, const QString& fileName);
|
|
|
|
bool directoryExists(const QString& file);
|
|
|
|
bool removeFile(const QString& filename);
|
|
|
|
void copyFolder(const QString &fromDir, const QString& toDir);
|
|
|
|
bool copyFile(const QString &fromFile, const QString& toFile, bool overwrite);
|
2021-08-23 10:16:06 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
QString includeTrailingPathDelimiter(const QString& path);
|
|
|
|
QString excludeTrailingPathDelimiter(const QString& path);
|
|
|
|
QString changeFileExt(const QString& filename, QString ext);
|
2021-08-26 11:58:29 +08:00
|
|
|
|
2021-12-09 08:10:14 +08:00
|
|
|
QString localizePath(const QString& path);
|
2022-09-25 17:43:31 +08:00
|
|
|
|
|
|
|
QString extractRelativePath(const QString& base, const QString& dest);
|
2021-09-10 12:37:02 +08:00
|
|
|
QString extractFileName(const QString& fileName);
|
2021-09-13 22:45:50 +08:00
|
|
|
QString extractFileDir(const QString& fileName);
|
2021-09-10 12:37:02 +08:00
|
|
|
QString extractFilePath(const QString& filePath);
|
|
|
|
QString extractAbsoluteFilePath(const QString& filePath);
|
2022-01-04 16:50:54 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
bool isReadOnly(const QString& filename);
|
2021-12-24 23:18:20 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
int compareFileModifiedTime(const QString& filename1, const QString& filename2);
|
2021-11-05 11:48:46 +08:00
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
/* UI utils */
|
|
|
|
void inflateRect(QRect& rect, int delta);
|
|
|
|
void inflateRect(QRect& rect, int dx, int dy);
|
2021-08-23 10:16:06 +08:00
|
|
|
|
2022-01-26 21:36:31 +08:00
|
|
|
int screenDPI();
|
|
|
|
void setScreenDPI(int dpi);
|
2021-12-17 09:05:01 +08:00
|
|
|
float pointToPixel(float point);
|
2022-03-01 22:03:54 +08:00
|
|
|
float pointToPixel(float point, float dpi);
|
2021-12-17 09:05:01 +08:00
|
|
|
float pixelToPoint(float pixel);
|
|
|
|
|
2022-09-25 17:43:31 +08:00
|
|
|
void decodeKey(int combinedKey, int& key, Qt::KeyboardModifiers& modifiers);
|
2022-01-27 12:08:57 +08:00
|
|
|
|
2021-10-13 17:20:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* from https://github.com/Microsoft/GSL
|
|
|
|
**/
|
|
|
|
|
2021-04-12 00:00:29 +08:00
|
|
|
template <class F>
|
|
|
|
class final_action
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static_assert(!std::is_reference<F>::value && !std::is_const<F>::value &&
|
|
|
|
!std::is_volatile<F>::value,
|
|
|
|
"Final_action should store its callable by value");
|
|
|
|
|
|
|
|
explicit final_action(F f) noexcept : f_(std::move(f)) {}
|
|
|
|
|
|
|
|
final_action(final_action&& other) noexcept
|
|
|
|
: f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false))
|
|
|
|
{}
|
|
|
|
|
|
|
|
final_action(const final_action&) = delete;
|
|
|
|
final_action& operator=(const final_action&) = delete;
|
|
|
|
final_action& operator=(final_action&&) = delete;
|
|
|
|
|
|
|
|
~final_action() noexcept
|
|
|
|
{
|
|
|
|
if (invoke_) f_();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F f_;
|
|
|
|
bool invoke_{true};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F> final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>
|
|
|
|
finally(F&& f) noexcept
|
|
|
|
{
|
|
|
|
return final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>(
|
|
|
|
std::forward<F>(f));
|
|
|
|
}
|
2021-04-06 23:10:57 +08:00
|
|
|
#endif // UTILS_H
|