RedPanda-CPP/libs/redpanda_qt_utils/qt_utils/utils.h

204 lines
6.1 KiB
C
Raw Normal View History

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
#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>
#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
#define ENCODING_AUTO_DETECT "AUTO"
#define ENCODING_UTF8 "UTF-8"
#define ENCODING_UTF16 "UTF-16"
#define ENCODING_UTF32 "UTF-32"
#define ENCODING_UTF8_BOM "UTF-8 BOM"
#define ENCODING_UTF16_BOM "UTF-16 BOM"
#define ENCODING_UTF32_BOM "UTF-32 BOM"
#define ENCODING_SYSTEM_DEFAULT "SYSTEM"
#define ENCODING_ASCII "ASCII"
2023-01-24 11:31:30 +08:00
#define ENCODING_PROJECT "PROJECT"
enum class NewlineType {
2021-05-03 10:15:40 +08:00
Windows,
Unix,
MacOld
};// Windows: CRLF, UNIX: LF, Old Mac OS: CR (old mac os, not mac os x)
2021-05-03 10:15:40 +08:00
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
bool isBinaryContent(const QByteArray& text);
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);
class QTextEdit;
void clearQPlainTextEditFormat(QTextEdit* editor);
2022-09-25 17:43:31 +08:00
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
/**
* @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);
bool stringsToFile(const QStringList& list, const QString& fileName);
bool stringToFile(const QString& str, const QString& fileName);
void createFile(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);
bool isInFolder(const QString& folderpath, const QString& filepath);
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
QString localizePath(const QString& path);
2022-09-25 17:43:31 +08:00
QString extractRelativePath(const QString& base, const QString& dest);
QStringList extractRelativePaths(const QString& base, const QStringList& destList);
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);
QString cleanPath(const QString& dirPath);
QString generateAbsolutePath(const QString& dirPath, const QString& relativePath);
QStringList absolutePaths(const QString& dirPath, const QStringList& relativePaths);
QString escapeSpacesInString(const QString& str);
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);
int compareFileModifiedTime(const QString& filename1, qint64 timestamp);
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
int screenDPI();
void setScreenDPI(int dpi);
float pointToPixel(float point);
float pointToPixel(float point, float dpi);
float pixelToPoint(float pixel);
2022-09-25 17:43:31 +08:00
void decodeKey(int combinedKey, int& key, Qt::KeyboardModifiers& modifiers);
/**
* from https://github.com/Microsoft/GSL
**/
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