Update 更新文档
This commit is contained in:
143
cleaned_source_code/Sqbase/qconfigmanager.h
Normal file
143
cleaned_source_code/Sqbase/qconfigmanager.h
Normal file
@@ -0,0 +1,143 @@
|
||||
#ifndef QCONFIGMANAGER_H
|
||||
#define QCONFIGMANAGER_H
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QFile>
|
||||
#include <QMutex>
|
||||
#include <QTimer>
|
||||
#include <QMap>
|
||||
enum class ConfigCategory {
|
||||
System,
|
||||
Connection,
|
||||
Data,
|
||||
Performance,
|
||||
Monitoring,
|
||||
Security,
|
||||
User
|
||||
};
|
||||
struct ConfigItemMeta {
|
||||
QString key;
|
||||
QString displayName;
|
||||
QString description;
|
||||
QVariant defaultValue;
|
||||
QVariant minValue;
|
||||
QVariant maxValue;
|
||||
QStringList options;
|
||||
bool requiresRestart = false;
|
||||
bool advanced = false;
|
||||
ConfigItemMeta(const QString& k, const QString& name, const QString& desc,
|
||||
const QVariant& def, const QVariant& min = QVariant(),
|
||||
const QVariant& max = QVariant(), bool restart = false, bool adv = false)
|
||||
: key(k), displayName(name), description(desc), defaultValue(def),
|
||||
minValue(min), maxValue(max), requiresRestart(restart), advanced(adv) {}
|
||||
};
|
||||
struct ConfigChangeRecord {
|
||||
QString key;
|
||||
QVariant oldValue;
|
||||
QVariant newValue;
|
||||
QDateTime timestamp;
|
||||
QString user;
|
||||
QString reason;
|
||||
ConfigChangeRecord(const QString& k, const QVariant& oldVal, const QVariant& newVal,
|
||||
const QString& u = "system", const QString& r = "")
|
||||
: key(k), oldValue(oldVal), newValue(newVal), timestamp(QDateTime::currentDateTime()),
|
||||
user(u), reason(r) {}
|
||||
};
|
||||
class QConfigManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static QConfigManager* instance();
|
||||
void setValue(const QString& key, const QVariant& value, const QString& user = "system", const QString& reason = "");
|
||||
QVariant getValue(const QString& key, const QVariant& defaultValue = QVariant()) const;
|
||||
bool contains(const QString& key) const;
|
||||
void remove(const QString& key);
|
||||
void setValues(const QMap<QString, QVariant>& values, const QString& user = "system", const QString& reason = "");
|
||||
QMap<QString, QVariant> getValues(const QStringList& keys) const;
|
||||
QString getString(const QString& key, const QString& defaultValue = "") const;
|
||||
int getInt(const QString& key, int defaultValue = 0) const;
|
||||
double getDouble(const QString& key, double defaultValue = 0.0) const;
|
||||
bool getBool(const QString& key, bool defaultValue = false) const;
|
||||
QJsonArray getArray(const QString& key, const QJsonArray& defaultValue = QJsonArray()) const;
|
||||
QJsonObject getObject(const QString& key, const QJsonObject& defaultValue = QJsonObject()) const;
|
||||
void setCategoryValue(ConfigCategory category, const QString& key, const QVariant& value);
|
||||
QVariant getCategoryValue(ConfigCategory category, const QString& key, const QVariant& defaultValue = QVariant()) const;
|
||||
QMap<QString, QVariant> getCategoryValues(ConfigCategory category) const;
|
||||
void registerConfigItem(const ConfigItemMeta& meta);
|
||||
ConfigItemMeta getConfigMeta(const QString& key) const;
|
||||
QList<ConfigItemMeta> getAllConfigMeta() const;
|
||||
QList<ConfigItemMeta> getCategoryConfigMeta(ConfigCategory category) const;
|
||||
void loadFromFile(const QString& filePath = "");
|
||||
void saveToFile(const QString& filePath = "");
|
||||
void loadDefaults();
|
||||
void exportConfig(const QString& filePath);
|
||||
void importConfig(const QString& filePath);
|
||||
QList<ConfigChangeRecord> getChangeHistory(const QString& key = "", int limit = 100) const;
|
||||
void clearHistory(const QString& key = "");
|
||||
bool rollback(const QString& key, int steps = 1);
|
||||
bool validateValue(const QString& key, const QVariant& value) const;
|
||||
QVariant sanitizeValue(const QString& key, const QVariant& value) const;
|
||||
void setServerAddress(const QString& address);
|
||||
QString getServerAddress() const;
|
||||
void setServerPort(int port);
|
||||
int getServerPort() const;
|
||||
void setConnectionTimeout(int timeoutMs);
|
||||
int getConnectionTimeout() const;
|
||||
void setBufferSize(int size);
|
||||
int getBufferSize() const;
|
||||
void setMaxPacketsPerSecond(int rate);
|
||||
int getMaxPacketsPerSecond() const;
|
||||
void setDataRetentionDays(int days);
|
||||
int getDataRetentionDays() const;
|
||||
void setThreadPoolSize(int size);
|
||||
int getThreadPoolSize() const;
|
||||
void setCacheSize(int size);
|
||||
int getCacheSize() const;
|
||||
void setProcessingBatchSize(int size);
|
||||
int getProcessingBatchSize() const;
|
||||
void setMonitorInterval(int intervalMs);
|
||||
int getMonitorInterval() const;
|
||||
void setAlertEnabled(bool enabled);
|
||||
bool isAlertEnabled() const;
|
||||
void setLogLevel(int level);
|
||||
int getLogLevel() const;
|
||||
signals:
|
||||
void configChanged(const QString& key, const QVariant& oldValue, const QVariant& newValue);
|
||||
void configLoaded();
|
||||
void configSaved();
|
||||
void configError(const QString& error);
|
||||
void configValidationFailed(const QString& key, const QVariant& value, const QString& reason);
|
||||
private:
|
||||
explicit QConfigManager(QObject *parent = nullptr);
|
||||
~QConfigManager();
|
||||
void initializeDefaults();
|
||||
void registerSystemConfigItems();
|
||||
QString getCategoryPrefix(ConfigCategory category) const;
|
||||
QString getFullKey(ConfigCategory category, const QString& key) const;
|
||||
void addChangeRecord(const ConfigChangeRecord& record);
|
||||
mutable QMutex m_mutex;
|
||||
QMap<QString, QVariant> m_config;
|
||||
QMap<QString, ConfigItemMeta> m_configMeta;
|
||||
QList<ConfigChangeRecord> m_changeHistory;
|
||||
QString m_configPath;
|
||||
QTimer* m_autoSaveTimer;
|
||||
bool m_autoSaveEnabled = true;
|
||||
struct SystemDefaults {
|
||||
QString serverAddress = "127.0.0.1";
|
||||
int serverPort = 8000;
|
||||
int connectionTimeout = 30000;
|
||||
int bufferSize = 10000;
|
||||
int maxPacketsPerSecond = 1000;
|
||||
int dataRetentionDays = 30;
|
||||
int threadPoolSize = 4;
|
||||
int cacheSize = 1000;
|
||||
int processingBatchSize = 100;
|
||||
int monitorInterval = 5000;
|
||||
bool alertEnabled = true;
|
||||
int logLevel = 2;
|
||||
} m_defaults;
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user