chore: add interfaces of project

This commit is contained in:
Qsaker 2024-01-09 10:40:28 +08:00
parent 99ca6ca0f9
commit a2cfdf40df
2 changed files with 309 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
@ -22,21 +22,28 @@
#endif
#endif
#include <QAbstractTableModel>
#include <QHostAddress>
#include <QLineEdit>
#include <QList>
#include <QNetworkInterface>
#include <QRegularExpressionValidator>
#include <QSettings>
#include <QStandardItemModel>
#include "sakdatastructure.h"
#include "sakinterface.h"
SAKInterface::SAKInterface(QObject* parent)
SAKInterface::SAKInterface(QObject *parent)
: QObject{parent}
{}
void SAKInterface::setMaximumBlockCount(QVariant doc, int maximum)
{
auto obj = doc.value<QObject*>();
auto obj = doc.value<QObject *>();
if (obj) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
#ifdef SAK_IMPORT_MODULE_QML
auto quickTextDoc = qobject_cast<QQuickTextDocument*>(obj);
auto quickTextDoc = qobject_cast<QQuickTextDocument *>(obj);
if (quickTextDoc) {
auto textDoc = quickTextDoc->textDocument();
textDoc->setMaximumBlockCount(maximum);
@ -52,12 +59,12 @@ void SAKInterface::setMaximumBlockCount(QVariant doc, int maximum)
}
}
void SAKInterface::setAppFont(const QString& fontFamily)
void SAKInterface::setAppFont(const QString &fontFamily)
{
qGuiApp->setFont(fontFamily);
}
void SAKInterface::setClipboardText(const QString& text)
void SAKInterface::setClipboardText(const QString &text)
{
QGuiApplication::clipboard()->setText(text);
}
@ -78,9 +85,9 @@ bool SAKInterface::isQtHighDpiScalePolicy(int policy)
#endif
}
QString SAKInterface::arrayToString(const QByteArray& array, int format)
QString SAKInterface::arrayToString(const QByteArray &array, int format)
{
auto cookedArray = [](const QByteArray& array, int base, int len) -> QString {
auto cookedArray = [](const QByteArray &array, int base, int len) -> QString {
QString str, numStr;
for (int i = 0; i < array.length(); i++) {
if (base == 10 || base == 8) {
@ -109,12 +116,12 @@ QString SAKInterface::arrayToString(const QByteArray& array, int format)
}
}
QString SAKInterface::dateTimeString(const QString& format)
QString SAKInterface::dateTimeString(const QString &format)
{
return QDateTime::currentDateTime().toString(format);
}
QString SAKInterface::cookedFileName(const QString& fileName)
QString SAKInterface::cookedFileName(const QString &fileName)
{
QString cookedFileName = fileName;
#ifdef Q_OS_WIN
@ -124,18 +131,18 @@ QString SAKInterface::cookedFileName(const QString& fileName)
return cookedFileName;
}
QString SAKInterface::string2hexString(const QString& str)
QString SAKInterface::string2hexString(const QString &str)
{
return QString::fromLatin1(str.toUtf8().toHex());
}
QString SAKInterface::hexString2String(const QString& str)
QString SAKInterface::hexString2String(const QString &str)
{
QByteArray arr = QByteArray::fromHex(str.toUtf8());
return QString::fromUtf8(arr);
}
QString SAKInterface::buildDateTime(const QString& format)
QString SAKInterface::buildDateTime(const QString &format)
{
QString str = QString(__DATE__);
QDate date = QDate::fromString(str, "MMM d yyyy");
@ -156,7 +163,7 @@ QString SAKInterface::timeFormat()
return QLocale::system().timeFormat();
}
QByteArray SAKInterface::string2array(const QString& input, int format)
QByteArray SAKInterface::string2array(const QString &input, int format)
{
QByteArray data;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
@ -170,28 +177,28 @@ QByteArray SAKInterface::string2array(const QString& input, int format)
for (int i = 0; i < strList.length(); i++) {
QString str = strList.at(i);
qint8 value = QString(str).toInt(Q_NULLPTR, 2);
data.append(reinterpret_cast<char*>(&value), 1);
data.append(reinterpret_cast<char *>(&value), 1);
}
} else if (format == SAKDataStructure::TextFormatOct) {
QStringList strList = input.split(' ', behavior);
for (int i = 0; i < strList.length(); i++) {
QString str = strList.at(i);
qint8 value = QString(str).toInt(Q_NULLPTR, 8);
data.append(reinterpret_cast<char*>(&value), 1);
data.append(reinterpret_cast<char *>(&value), 1);
}
} else if (format == SAKDataStructure::TextFormatDec) {
QStringList strList = input.split(' ', behavior);
for (int i = 0; i < strList.length(); i++) {
QString str = strList.at(i);
qint8 value = QString(str).toInt(Q_NULLPTR, 10);
data.append(reinterpret_cast<char*>(&value), 1);
data.append(reinterpret_cast<char *>(&value), 1);
}
} else if (format == SAKDataStructure::TextFormatHex) {
QStringList strList = input.split(' ', behavior);
for (int i = 0; i < strList.length(); i++) {
QString str = strList.at(i);
qint8 value = QString(str).toInt(Q_NULLPTR, 16);
data.append(reinterpret_cast<char*>(&value), 1);
data.append(reinterpret_cast<char *>(&value), 1);
}
} else if (format == SAKDataStructure::TextFormatAscii) {
data = input.toLatin1();
@ -202,12 +209,12 @@ QByteArray SAKInterface::string2array(const QString& input, int format)
return data;
}
QByteArray SAKInterface::arrayAppendArray(const QByteArray& a1, const QByteArray& a2)
QByteArray SAKInterface::arrayAppendArray(const QByteArray &a1, const QByteArray &a2)
{
return a1 + a2;
}
QByteArray SAKInterface::arrayToHex(const QByteArray& array, char separator)
QByteArray SAKInterface::arrayToHex(const QByteArray &array, char separator)
{
if (array.isEmpty()) {
return array;
@ -217,8 +224,8 @@ QByteArray SAKInterface::arrayToHex(const QByteArray& array, char separator)
const int length = separator ? (array.size() * 3 - 1) : (array.size() * 2);
QByteArray hex(length, Qt::Uninitialized);
char* hexData = hex.data();
const uchar* data = reinterpret_cast<const uchar*>(array.constData());
char *hexData = hex.data();
const uchar *data = reinterpret_cast<const uchar *>(array.constData());
for (int i = 0, o = 0; i < array.size(); ++i) {
hexData[o++] = toHex(data[i] >> 4);
hexData[o++] = toHex(data[i] & 0xf);
@ -228,3 +235,191 @@ QByteArray SAKInterface::arrayToHex(const QByteArray& array, char separator)
}
return hex;
}
void SAKInterface::setLineEditValidator(QLineEdit *lineEdit,
SAKEnumValidatorType type,
int maxLength)
{
QMap<int, QRegularExpression> regExpMap;
regExpMap.insert(ValidatorBin, QRegularExpression("([01][01][01][01][01][01][01][01][ ])*"));
regExpMap.insert(ValidatorOtc, QRegularExpression("([0-7][0-7][ ])*"));
regExpMap.insert(ValidatorDec, QRegularExpression("([0-9][0-9][ ])*"));
regExpMap.insert(ValidatorHex, QRegularExpression("([0-9a-fA-F][0-9a-fA-F][ ])*"));
regExpMap.insert(ValidatorAscii, QRegularExpression("([ -~])*"));
regExpMap.insert(ValidatorFloat, QRegularExpression("^[-+]?[0-9]*\\.?[0-9]+$"));
if (lineEdit) {
if (lineEdit->validator()) {
delete lineEdit->validator();
}
if (regExpMap.contains(type)) {
if (type != ValidatorNone) {
auto regExpValidator = new QRegularExpressionValidator(regExpMap.value(type),
lineEdit);
lineEdit->setValidator(regExpValidator);
lineEdit->setMaxLength(maxLength);
}
}
}
}
#ifdef SAK_IMPORT_MODULE_SERIALPORT
void SAKInterface::addSerialPortNametItemsToComboBox(QComboBox *comboBox)
{
if (comboBox) {
comboBox->clear();
QList<QSerialPortInfo> coms = QSerialPortInfo::availablePorts();
QStandardItemModel *itemModel = new QStandardItemModel(comboBox);
for (auto &var : coms) {
QStandardItem *item = new QStandardItem(var.portName());
item->setToolTip(var.description());
itemModel->appendRow(item);
}
comboBox->setModel(itemModel);
}
}
#endif
#ifdef SAK_IMPORT_MODULE_SERIALPORT
void SAKInterface::addSerialPortBaudRateItemsToComboBox(QComboBox *comboBox)
{
if (comboBox) {
comboBox->clear();
QList<qint32> bd = QSerialPortInfo::standardBaudRates();
for (auto &var : bd) {
comboBox->addItem(QString::number(var), QVariant::fromValue(var));
}
comboBox->setCurrentText("9600");
}
}
#endif
#ifdef SAK_IMPORT_MODULE_SERIALPORT
void SAKInterface::addSerialPortDataBitItemsToComboBox(QComboBox *comboBox)
{
if (comboBox) {
comboBox->clear();
comboBox->addItem("8", QVariant::fromValue(int(QSerialPort::Data8)));
comboBox->addItem("7", QVariant::fromValue(int(QSerialPort::Data7)));
comboBox->addItem("6", QVariant::fromValue(int(QSerialPort::Data6)));
comboBox->addItem("5", QVariant::fromValue(int(QSerialPort::Data5)));
}
}
#endif
#ifdef SAK_IMPORT_MODULE_SERIALPORT
void SAKInterface::addSerialPortStopBitItemsToComboBox(QComboBox *comboBox)
{
if (comboBox) {
comboBox->clear();
comboBox->addItem("1", QVariant::fromValue(int(QSerialPort::OneStop)));
#ifdef Q_OS_WINDOWS
comboBox->addItem("1.5", QVariant::fromValue(int(QSerialPort::OneAndHalfStop)));
#endif
comboBox->addItem("2", QVariant::fromValue(int(QSerialPort::TwoStop)));
}
}
#endif
#ifdef SAK_IMPORT_MODULE_SERIALPORT
void SAKInterface::addSerialPortParityItemsToComboBox(QComboBox *comboBox)
{
if (comboBox) {
comboBox->clear();
comboBox->addItem(tr("No"), QVariant::fromValue(int(QSerialPort::NoParity)));
comboBox->addItem(tr("Even"), QVariant::fromValue(int(QSerialPort::EvenParity)));
comboBox->addItem(tr("Odd"), QVariant::fromValue(int(QSerialPort::OddParity)));
comboBox->addItem(tr("Space"), QVariant::fromValue(int(QSerialPort::SpaceParity)));
comboBox->addItem(tr("Mark"), QVariant::fromValue(int(QSerialPort::MarkParity)));
}
}
#endif
#ifdef SAK_IMPORT_MODULE_SERIALPORT
void SAKInterface::addSerialPortFlowControlItemsToComboBox(QComboBox *comboBox)
{
if (comboBox) {
comboBox->clear();
comboBox->addItem(tr("No"), QVariant::fromValue(int(QSerialPort::NoFlowControl)));
comboBox->addItem(tr("Hardware"), QVariant::fromValue(int(QSerialPort::HardwareControl)));
comboBox->addItem(tr("Software"), QVariant::fromValue(int(QSerialPort::SoftwareControl)));
}
}
#endif
void SAKInterface::addIpItemsToComboBox(QComboBox *comboBox, bool appendHostAny)
{
QString localHost("127.0.0.1");
if (comboBox) {
comboBox->clear();
comboBox->addItem(QString("::"));
comboBox->addItem(QString("::1"));
comboBox->addItem(QString("0.0.0.0"));
comboBox->addItem(localHost);
QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
for (auto &var : addresses) {
if (var.protocol() == QAbstractSocket::IPv4Protocol) {
if (var.toString().compare(localHost) == 0) {
continue;
}
comboBox->addItem(var.toString());
}
}
if (appendHostAny) {
comboBox->addItem(QString(SAK_HOST_ADDRESS_ANY));
}
comboBox->setCurrentText(localHost);
}
}
void SAKInterface::setComboBoxIndexFromSettings(QSettings *settings,
QString key,
QComboBox *comboBox)
{
int index = settings->value(key).toInt();
if (index >= 0 && index < comboBox->count()) {
comboBox->setCurrentIndex(index);
}
}
void SAKInterface::setSettingsValueFromComboBoxIndex(QSettings *settings,
QString key,
QComboBox *comboBox)
{
int currentIndex = comboBox->currentIndex();
settings->setValue(key, currentIndex);
}
void SAKInterface::setLineEditTextFromSettings(QSettings *settings, QString key, QLineEdit *lineEdit)
{
QString text = settings->value(key).toString();
if (!text.isEmpty()) {
lineEdit->setText(text);
}
}
void SAKInterface::setSettingsValueFromLineEditText(QSettings *settings,
QString key,
QLineEdit *lineEdit)
{
QString value = lineEdit->text();
settings->setValue(key, value);
}
void SAKInterface::setCheckBoxValueFromSettings(QSettings *settings,
QString key,
QCheckBox *checkBox)
{
checkBox->setChecked(settings->value(key).toBool());
}
void SAKInterface::setSettingsValueFromCheckBox(QSettings *settings,
QString key,
QCheckBox *checkBox)
{
settings->setValue(key, checkBox->isChecked());
}

View File

@ -1,5 +1,5 @@
/***************************************************************************************************
* Copyright 2023 Qsaker(qsaker@foxmail.com). All rights reserved.
* Copyright 2023-2024 Qsaker(qsaker@foxmail.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
*
@ -9,35 +9,114 @@
#ifndef SAKINTERFACE_H
#define SAKINTERFACE_H
#include <QCheckBox>
#include <QComboBox>
#include <QLineEdit>
#include <QObject>
#include <QSettings>
#include <QVariant>
#define SAK_STATIC Q_INVOKABLE static
#define microIni2CoB(settings, settingsGroup, structMember, comboBox) \
SAKCommonInterface::setComboBoxIndexFromSettings(settings, \
settingsGroup + QString("/") \
+ QString(#structMember).split('.').last(), \
comboBox)
#define microCoB2Ini(settings, settingsGroup, structMember, comboBox) \
SAKCommonInterface::setSettingsValueFromComboBoxIndex(settings, \
settingsGroup + QString("/") \
+ QString(#structMember) \
.split('.') \
.last(), \
comboBox)
#define microIni2LE(settings, settingsGroup, structMember, lineEdit) \
SAKCommonInterface::setLineEditTextFromSettings(settings, \
settingsGroup + QString("/") \
+ QString(#structMember).split('.').last(), \
lineEdit)
#define microLE2Ini(settings, settingsGroup, structMember, lineEdit) \
SAKCommonInterface::setSettingsValueFromLineEditText(settings, \
settingsGroup + QString("/") \
+ QString(#structMember) \
.split('.') \
.last(), \
lineEdit)
#define microIni2ChB(settings, settingsGroup, structMember, checkBox) \
SAKCommonInterface::setCheckBoxValueFromSettings(settings, \
settingsGroup + QString("/") \
+ QString(#structMember).split('.').last(), \
checkBox)
#define microChB2Ini(settings, settingsGroup, structMember, checkBox) \
SAKCommonInterface::setSettingsValueFromCheckBox(settings, \
settingsGroup + QString("/") \
+ QString(#structMember).split('.').last(), \
checkBox)
class SAKInterface : public QObject
{
Q_OBJECT
public:
explicit SAKInterface(QObject* parent = nullptr);
explicit SAKInterface(QObject *parent = nullptr);
Q_INVOKABLE static void setMaximumBlockCount(QVariant doc, int maximum);
Q_INVOKABLE static void setAppFont(const QString& fontFamily);
Q_INVOKABLE static void setClipboardText(const QString& text);
Q_INVOKABLE static void setAppFont(const QString &fontFamily);
Q_INVOKABLE static void setClipboardText(const QString &text);
Q_INVOKABLE static bool isQtHighDpiScalePolicy(int policy);
Q_INVOKABLE static QString arrayToString(const QByteArray& array, int format);
Q_INVOKABLE static QString dateTimeString(const QString& format);
Q_INVOKABLE static QString cookedFileName(const QString& fileName);
Q_INVOKABLE static QString string2hexString(const QString& str);
Q_INVOKABLE static QString hexString2String(const QString& str);
Q_INVOKABLE static QString buildDateTime(const QString& format);
Q_INVOKABLE static QString arrayToString(const QByteArray &array, int format);
Q_INVOKABLE static QString dateTimeString(const QString &format);
Q_INVOKABLE static QString cookedFileName(const QString &fileName);
Q_INVOKABLE static QString string2hexString(const QString &str);
Q_INVOKABLE static QString hexString2String(const QString &str);
Q_INVOKABLE static QString buildDateTime(const QString &format);
Q_INVOKABLE static QString dateFormat();
Q_INVOKABLE static QString timeFormat();
Q_INVOKABLE static QByteArray string2array(const QString& input, int format);
Q_INVOKABLE static QByteArray arrayAppendArray(const QByteArray& a1, const QByteArray& a2);
Q_INVOKABLE static QByteArray arrayToHex(const QByteArray& array, char separator = '\0');
Q_INVOKABLE static QByteArray string2array(const QString &input, int format);
Q_INVOKABLE static QByteArray arrayAppendArray(const QByteArray &a1, const QByteArray &a2);
Q_INVOKABLE static QByteArray arrayToHex(const QByteArray &array, char separator = '\0');
enum SAKEnumValidatorType {
ValidatorBin,
ValidatorOtc,
ValidatorDec,
ValidatorHex,
ValidatorAscii,
ValidatorFloat,
ValidatorNone
};
/**
* @brief setLineEditValidator: Set validator to a line edit, i
* t will delete the old validator.
* @param lineEdit: Target line edit.
* @param type: See ValidatorType for more information.
*/
static void setLineEditValidator(QLineEdit *lineEdit,
SAKEnumValidatorType type,
int maxLength = INT_MAX);
#ifdef SAK_IMPORT_MODULE_SERIALPORT
// Serialport interfaces
static void addSerialPortNametItemsToComboBox(QComboBox *comboBox);
static void addSerialPortBaudRateItemsToComboBox(QComboBox *comboBox);
static void addSerialPortDataBitItemsToComboBox(QComboBox *comboBox);
static void addSerialPortStopBitItemsToComboBox(QComboBox *comboBox);
static void addSerialPortParityItemsToComboBox(QComboBox *comboBox);
static void addSerialPortFlowControlItemsToComboBox(QComboBox *comboBox);
#endif
static void addIpItemsToComboBox(QComboBox *comboBox, bool appendHostAny = false);
static void setComboBoxIndexFromSettings(QSettings *settings, QString key, QComboBox *comboBox);
static void setSettingsValueFromComboBoxIndex(QSettings *settings,
QString key,
QComboBox *comboBox);
static void setLineEditTextFromSettings(QSettings *settings, QString key, QLineEdit *lineEdit);
static void setSettingsValueFromLineEditText(QSettings *settings,
QString key,
QLineEdit *lineEdit);
static void setCheckBoxValueFromSettings(QSettings *settings, QString key, QCheckBox *checkBox);
static void setSettingsValueFromCheckBox(QSettings *settings, QString key, QCheckBox *checkBox);
};
#endif // SAKINTERFACE_H