From 02271ed40ba4d8a2422671a712fc9d3738cfb11c Mon Sep 17 00:00:00 2001 From: Boyuan Yang Date: Mon, 12 Nov 2018 19:56:16 -0500 Subject: [PATCH] New upstream version 2.0.9.8 --- CHANGELOG.md | 10 + src/dsysinfo.cpp | 42 +++-- src/filesystem/dpathbuf.cpp | 26 ++- src/filesystem/dpathbuf.h | 31 ++++ src/filesystem/dstandardpaths.cpp | 29 ++- src/settings/backend/gsettingsbackend.cpp | 25 ++- src/settings/backend/gsettingsbackend.h | 2 +- src/settings/backend/qsettingbackend.cpp | 27 +++ src/settings/dsettings.cpp | 216 ++++++++++++++++++++++ src/settings/dsettingsbackend.h | 2 +- src/settings/dsettingsgroup.cpp | 61 +++++- src/settings/dsettingsgroup.h | 3 - src/settings/dsettingsoption.cpp | 107 +++++++++-- src/settings/dsettingsoption.h | 2 +- src/util/dabstractunitformatter.cpp | 57 ++++++ src/util/ddisksizeformatter.cpp | 44 +++++ src/util/dtimeunitformatter.cpp | 39 ++++ tests/dutiltester.cpp | 49 ++--- 18 files changed, 704 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fdaa58..422ab19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ + +## 2.0.9.8 (2018-11-09) + + +#### Bug Fixes + +* can't get correct disk size in some case ([20a12b62](https://github.com/linuxdeepin/dtkcore/commit/20a12b622ea7b01f0616c15a8af85e31fc2d36cb)) + + + ## 2.0.9.5 (2018-10-26) diff --git a/src/dsysinfo.cpp b/src/dsysinfo.cpp index 658af35..1fbdb7d 100644 --- a/src/dsysinfo.cpp +++ b/src/dsysinfo.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #ifdef Q_OS_LINUX #include @@ -315,27 +317,35 @@ void DSysInfoPrivate::ensureComputerInfo() memoryTotalSize = get_phys_pages() * sysconf(_SC_PAGESIZE); - const QString &root_part = QStorageInfo::root().device(); - - if (root_part.isEmpty()) - return; - + // Getting Disk Size + const QString &deviceName = QStorageInfo::root().device(); QProcess lsblk; - lsblk.start("lsblk", {"-prno", "pkname", root_part}, QIODevice::ReadOnly); - - if (!lsblk.waitForFinished()) - return; - - const QString &root_disk = QString::fromLatin1(lsblk.readAllStandardOutput().trimmed()); + lsblk.start("lsblk", {"-Jlpb", "-oNAME,KNAME,PKNAME,SIZE"}, QIODevice::ReadOnly); - lsblk.start("lsblk", {"-prnbdo", "size", root_disk}, QIODevice::ReadOnly); - - if (!lsblk.waitForFinished()) + if (!lsblk.waitForFinished()) { return; + } - const QByteArray &disk_size = lsblk.readAllStandardOutput().trimmed(); - diskSize = disk_size.toLongLong(); + const QByteArray &diskStatusJson = lsblk.readAllStandardOutput(); + QJsonDocument diskStatus = QJsonDocument::fromJson(diskStatusJson); + QJsonValue diskStatusJsonValue = diskStatus["blockdevices"]; + QMap> deviceParentAndSizeMap; + + if (!diskStatusJsonValue.isUndefined()) { + QJsonArray diskStatusArray = diskStatusJsonValue.toArray(); + QString keyName; + for (const QJsonValue &oneValue : diskStatusArray) { + if (keyName.isNull() && deviceName == oneValue["name"].toString()) { + keyName = oneValue["kname"].toString(); + } + deviceParentAndSizeMap[oneValue["kname"].toString()] = QPair(oneValue["pkname"].toString(), oneValue["size"].toString().toULongLong()); + } + while (!deviceParentAndSizeMap[keyName].first.isNull()) { + keyName = deviceParentAndSizeMap[keyName].first; + } + diskSize = deviceParentAndSizeMap[keyName].second; + } #endif } diff --git a/src/filesystem/dpathbuf.cpp b/src/filesystem/dpathbuf.cpp index 9fe11b2..cd5aa62 100644 --- a/src/filesystem/dpathbuf.cpp +++ b/src/filesystem/dpathbuf.cpp @@ -1,6 +1,30 @@ #include "dpathbuf.h" -Dtk::Core::DPathBuf::DPathBuf(const QString &path) +/*! + * \~english \class Dtk::Core::DPathBuf + * \brief Dtk::Core::DPathBuf cat path friendly and supoort multiplatform. + */ + +/*! + * \~chinese \class Dtk::Core::DPathBuf + * \brief Dtk::Core::DPathBuf是一个用于跨平台拼接路径的辅助类。 + * 它能够方便的写出链式结构的路径拼接代码。 +``` +DPathBuf logPath(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); +logPath = logPath / ".cache" / "deepin" / "deepin-test-dtk" / "deepin-test-dtk.log"; +``` + */ + + +DCORE_BEGIN_NAMESPACE + +/*! + * \brief Create Dtk::Core::DPathBuf from string. + * \param path + */ +DPathBuf::DPathBuf(const QString &path) { m_path = QDir(path).absolutePath(); } + +DCORE_END_NAMESPACE diff --git a/src/filesystem/dpathbuf.h b/src/filesystem/dpathbuf.h index 3aa7609..aa5c997 100644 --- a/src/filesystem/dpathbuf.h +++ b/src/filesystem/dpathbuf.h @@ -28,26 +28,53 @@ class LIBDTKCORESHARED_EXPORT DPathBuf public: DPathBuf(const QString &path); + /*! + * \brief join path with operator / + * \param p is subpath + * \return a new DPathBuf with subpath p + */ DPathBuf operator/(const QString &p) const { return DPathBuf(m_path + "/" + p); } + /*! + * \brief join path to self with operator /= + * \param p is subpath to join + * \return self object + */ DPathBuf &operator/=(const QString &p) { return join(p); } + /*! + * \brief join path with operator / + * \param p is subpath + * \return a new DPathBuf with subpath p + * \sa Dtk::Core::DPathBuf::operator/(const QString &p) const + */ DPathBuf operator/(const char *p) const { return operator /(QString(p)); } + /*! + * \brief join path to self with operator /= + * \param p is subpath to join + * \return self object + * \sa Dtk::Core::DPathBuf::operator/=(const QString &p) + */ DPathBuf &operator/=(const char *p) { return operator /=(QString(p)); } + /*! + * \brief join add subpath p to self + * \param p is subpath to join + * \return slef object with subpath joined + */ DPathBuf &join(const QString &p) { m_path += "/" + p; @@ -55,6 +82,10 @@ public: return *this; } + /*! + * \brief toString export native separators format string. + * \return string with native separators + */ QString toString() const { return QDir::toNativeSeparators(m_path); diff --git a/src/filesystem/dstandardpaths.cpp b/src/filesystem/dstandardpaths.cpp index bad435d..bf4080e 100644 --- a/src/filesystem/dstandardpaths.cpp +++ b/src/filesystem/dstandardpaths.cpp @@ -21,8 +21,7 @@ DCORE_BEGIN_NAMESPACE - -class DSnapStandardPaths +class DSnapStandardPathsPrivate { public: inline static QString writableLocation(QStandardPaths::StandardLocation /*type*/) @@ -49,12 +48,28 @@ public: } private: - DSnapStandardPaths(); - ~DSnapStandardPaths(); - Q_DISABLE_COPY(DSnapStandardPaths) + DSnapStandardPathsPrivate(); + ~DSnapStandardPathsPrivate(); + Q_DISABLE_COPY(DSnapStandardPathsPrivate) }; +/*! + * \~chinese \class Dtk::Core::DStandardPaths + * \brief DStandardPaths提供兼容Snap/Dtk标准的路径模式。DStandardPaths实现了Qt的QStandardPaths主要接口。 + * \sa QStandardPaths + * + * \enum DStandardPaths::Mode + * \brief DStandardPaths支持的路径产生模式。 + * \var DStandardPaths::Mode DStandardPaths::Auto + * \brief 和Qt标准的行为表现一致。 + * \var DStandardPaths::Mode DStandardPaths::Snap + * \brief 读取SNAP相关的环境变量,支持将配置存储在SNAP对应目录。 + * \var DStandardPaths::Mode DStandardPaths::Test + * \brief 和Qt标准的行为表现一致,但是会开启测试模式,参考QStandardPaths::setTestModeEnabled。 + */ + + static DStandardPaths::Mode s_mode = DStandardPaths::Auto; QString DStandardPaths::writableLocation(QStandardPaths::StandardLocation type) @@ -64,7 +79,7 @@ QString DStandardPaths::writableLocation(QStandardPaths::StandardLocation type) case Test: return QStandardPaths::writableLocation(type); case Snap: - return DSnapStandardPaths::writableLocation(type); + return DSnapStandardPathsPrivate::writableLocation(type); } return QStandardPaths::writableLocation(type); } @@ -76,7 +91,7 @@ QStringList DStandardPaths::standardLocations(QStandardPaths::StandardLocation t case Test: return QStandardPaths::standardLocations(type); case Snap: - return DSnapStandardPaths::standardLocations(type); + return DSnapStandardPathsPrivate::standardLocations(type); } return QStandardPaths::standardLocations(type); } diff --git a/src/settings/backend/gsettingsbackend.cpp b/src/settings/backend/gsettingsbackend.cpp index 526c5c0..0b076bc 100644 --- a/src/settings/backend/gsettingsbackend.cpp +++ b/src/settings/backend/gsettingsbackend.cpp @@ -11,7 +11,6 @@ DCORE_BEGIN_NAMESPACE - QString unqtifyName(const QString &name) { QString ret; @@ -45,6 +44,15 @@ public: Q_DECLARE_PUBLIC(GSettingsBackend) }; +/*! + * \class GSettingsBackend + * \brief Storage backend of DSettings use gsettings. + * + * You should generate gsetting schema with /usr/lib/dtk2/dtk-settings. + * + * You can find this tool from libdtkcore-bin. use /usr/lib/dtk2/dtk-settings -h for help. + */ + GSettingsBackend::GSettingsBackend(DSettings *settings, QObject *parent) : DSettingsBackend(parent), d_ptr(new GSettingsBackendPrivate(this)) { @@ -75,18 +83,30 @@ GSettingsBackend::~GSettingsBackend() } +/*! + * \brief List all gsettings keys. + * \return + */ QStringList GSettingsBackend::keys() const { Q_D(const GSettingsBackend); return d->gsettings->keys(); } +/*! + * \brief Get value of key. + * \return + */ QVariant GSettingsBackend::getOption(const QString &key) const { Q_D(const GSettingsBackend); return d->gsettings->get(qtifyName(key)); } +/*! + * \brief Set value to gsettings + * \return + */ void GSettingsBackend::doSetOption(const QString &key, const QVariant &value) { Q_D(GSettingsBackend); @@ -96,6 +116,9 @@ void GSettingsBackend::doSetOption(const QString &key, const QVariant &value) } } +/*! + * \brief Trigger DSettings to sync option to storage. + */ void GSettingsBackend::doSync() { Q_EMIT sync(); diff --git a/src/settings/backend/gsettingsbackend.h b/src/settings/backend/gsettingsbackend.h index 94abdc0..8b02ebb 100644 --- a/src/settings/backend/gsettingsbackend.h +++ b/src/settings/backend/gsettingsbackend.h @@ -12,7 +12,7 @@ class LIBDTKCORESHARED_EXPORT GSettingsBackend: public DSettingsBackend { Q_OBJECT public: - explicit GSettingsBackend(DSettings *settings, QObject *parent = 0); + explicit GSettingsBackend(DSettings *settings, QObject *parent = nullptr); ~GSettingsBackend(); virtual QStringList keys() const Q_DECL_OVERRIDE; diff --git a/src/settings/backend/qsettingbackend.cpp b/src/settings/backend/qsettingbackend.cpp index ae15b4f..ec30198 100644 --- a/src/settings/backend/qsettingbackend.cpp +++ b/src/settings/backend/qsettingbackend.cpp @@ -35,6 +35,16 @@ public: Q_DECLARE_PUBLIC(QSettingBackend) }; +/*! + * \class QSettingBackend + * \brief Storage DSetttings to an QSettings + */ + +/*! + * \brief Save data to filepath with QSettings::NativeFormat format. + * \param filepath is path to storage data. + * \param parent + */ QSettingBackend::QSettingBackend(const QString &filepath, QObject *parent) : DSettingsBackend(parent), d_ptr(new QSettingBackendPrivate(this)) { @@ -49,12 +59,21 @@ QSettingBackend::~QSettingBackend() } +/*! + * \brief List all keys of QSettings + * \return + */ QStringList QSettingBackend::keys() const { Q_D(const QSettingBackend); return d->settings->childGroups(); } +/*! + * \brief Get value of key from QSettings + * \param key + * \return + */ QVariant QSettingBackend::getOption(const QString &key) const { Q_D(const QSettingBackend); @@ -64,6 +83,11 @@ QVariant QSettingBackend::getOption(const QString &key) const return value; } +/*! + * \brief Set value of key to QSettings + * \param key + * \param value + */ void QSettingBackend::doSetOption(const QString &key, const QVariant &value) { Q_D(QSettingBackend); @@ -78,6 +102,9 @@ void QSettingBackend::doSetOption(const QString &key, const QVariant &value) d->writeLock.unlock(); } +/*! + * \brief Trigger DSettings to save option value to QSettings + */ void QSettingBackend::doSync() { Q_D(QSettingBackend); diff --git a/src/settings/dsettings.cpp b/src/settings/dsettings.cpp index d5a6636..ae13a6d 100644 --- a/src/settings/dsettings.cpp +++ b/src/settings/dsettings.cpp @@ -48,6 +48,222 @@ public: }; +/*! + * \~english \class DSettingsBackend + * \brief DSettingsBackend is interface of DSettings storage class. + * + * Simaple example: + +```json +{ + "groups": [{ + "key": "base", + "name": "Basic settings", + "groups": [{ + "key": "open_action", + "name": "Open Action", + "options": [{ + "key": "alway_open_on_new", + "type": "checkbox", + "text": "Always Open On New Windows", + "default": true + }, + { + "key": "open_file_action", + "name": "Open File:", + "type": "combobox", + "default": "" + } + ] + }, + { + "key": "new_tab_windows", + "name": "New Tab & Window", + "options": [{ + "key": "new_window_path", + "name": "New Window Open:", + "type": "combobox", + "default": "" + }, + { + "key": "new_tab_path", + "name": "New Tab Open:", + "type": "combobox", + "default": "" + } + ] + } + ] + }] +} +``` + + * How to read/write key and value: + +```c++ + // init a storage backend + QTemporaryFile tmpFile; + tmpFile.open(); + auto backend = new Dtk::Core::QSettingBackend(tmpFile.fileName()); + + // read settings from json + auto settings = Dtk::Core::DSettings::fromJsonFile(":/resources/data/dfm-settings.json"); + settings->setBackend(backend); + + // read value + auto opt = settings->option("base.new_tab_windows.new_window_path"); + qDebug() << opt->value(); + + // modify value + opt->setValue("Test") + qDebug() << opt->value(); +``` + * \sa Dtk::Core::DSettingsOption + * \sa Dtk::Core::DSettingsGroup + * \sa Dtk::Core::DSettingsBackend + * \sa Dtk::Widget::DSettingsWidgetFactory + * \sa Dtk::Widget::DSettingsDialog + */ + + +/*! + * \~english \class DSettings + * \brief DSettings support base config storage and ui create tool for dtk applications. + * \sa Dtk::Core::DSettings + * \sa Dtk::Core::DSettingsBackend + * + * \fn virtual QStringList DSettingsBackend::keys() const = 0; + * \brief return all key of storage. + * + * \fn virtual QVariant DSettingsBackend::getOption(const QString &key) const = 0; + * \brief get value by key. + * + * \fn virtual void DSettingsBackend::doSync() = 0; + * \brief do the real sync action. + * + * \fn virtual void DSettingsBackend::doSetOption(const QString &key, const QVariant &value) = 0; + * \brief write key/value to storage. + * + * \fn void DSettingsBackend::optionChanged(const QString &key, const QVariant &value); + * \brief emited when option value changed. + * + * \fn void DSettingsBackend::sync(); + * \brief private signal, please do not use it. + * + * \fn void DSettingsBackend::setOption(const QString &key, const QVariant &value); + * \brief private signal, please do not use it. + */ + +/*! + * \~chinese \class DSettingsBackend + * \brief DSettingsBackend是一个纯虚类, 用来描述DSettings的存储接口。 + * \sa Dtk::Core::DSettings + * \sa Dtk::Core::DSettingsBackend + * + * \fn virtual QStringList DSettingsBackend::keys() const = 0; + * \brief 返回全部键值。 + * + * \fn virtual QVariant DSettingsBackend::getOption(const QString &key) const = 0; + * \brief 获取key对应的值。 + * + * \fn virtual void DSettingsBackend::doSync() = 0; + * \brief 开始进行同步。 + * + * \fn virtual void DSettingsBackend::doSetOption(const QString &key, const QVariant &value) = 0; + * \brief 设置key对应的值,并使用存储后端进行存储。 + * + * \fn void DSettingsBackend::optionChanged(const QString &key, const QVariant &value); + * \brief DSettingsOption的值发生变化时发出的信号。 + * + * \fn void DSettingsBackend::sync(); + * \brief 私有信号,请勿使用。 + * + * \fn void DSettingsBackend::setOption(const QString &key, const QVariant &value); + * \brief 私有信号,请勿使用。 + */ + + +/*! + * \~chinese \class DSettings + * \brief DSettings是设计上为Dtk的应用程序提供统一的配置存储以及界面生成工具的基础库。 + * DSetting使用json作为应用配置程序的描述文件。简单来说,应用查询的配置分为组/键值二个基础层级, + * 对于一个标准的Dtk配置控件,一般只包含组/子组/键值三个层级,对于超过三个层级的键值,可以通过 + * DSettings的API接口进行读取和写入,但是不能在标准的DSettingsDialogs上显示出来。 + * + * 一个简单的配置文件如下: +```json +{ + "groups": [{ + "key": "base", + "name": "Basic settings", + "groups": [{ + "key": "open_action", + "name": "Open Action", + "options": [{ + "key": "alway_open_on_new", + "type": "checkbox", + "text": "Always Open On New Windows", + "default": true + }, + { + "key": "open_file_action", + "name": "Open File:", + "type": "combobox", + "default": "" + } + ] + }, + { + "key": "new_tab_windows", + "name": "New Tab & Window", + "options": [{ + "key": "new_window_path", + "name": "New Window Open:", + "type": "combobox", + "default": "" + }, + { + "key": "new_tab_path", + "name": "New Tab Open:", + "type": "combobox", + "default": "" + } + ] + } + ] + }] +} +``` + + * 改组中包含一个base的root组,两个子组: open_action/new_tab_windows,每个子组有包含若干选项。 + * 对于"New Window Open:"这个配置,其完整的访问id为base.new_tab_windows.new_window_path。 + * 读取/设置其值的示例如下: + +```c++ + // 初始化一个存储后端 + QTemporaryFile tmpFile; + tmpFile.open(); + auto backend = new Dtk::Core::QSettingBackend(tmpFile.fileName()); + + // 从json中初始化配置 + auto settings = Dtk::Core::DSettings::fromJsonFile(":/resources/data/dfm-settings.json"); + settings->setBackend(backend); + + // 读取配置 + auto opt = settings->option("base.new_tab_windows.new_window_path"); + qDebug() << opt->value(); + + // 修改配置 + opt->setValue("Test") + qDebug() << opt->value(); +``` + * \sa Dtk::Core::DSettingsOption + * \sa Dtk::Core::DSettingsGroup + * \sa Dtk::Core::DSettingsBackend + * \sa Dtk::Widget::DSettingsWidgetFactory + * \sa Dtk::Widget::DSettingsDialog + */ + DSettings::DSettings(QObject *parent) : QObject(parent), dd_ptr(new DSettingsPrivate(this)) { diff --git a/src/settings/dsettingsbackend.h b/src/settings/dsettingsbackend.h index d4e7889..45e49ae 100644 --- a/src/settings/dsettingsbackend.h +++ b/src/settings/dsettingsbackend.h @@ -29,7 +29,7 @@ class LIBDTKCORESHARED_EXPORT DSettingsBackend : public QObject { Q_OBJECT public: - explicit DSettingsBackend(QObject *parent = 0): QObject(parent) + explicit DSettingsBackend(QObject *parent = Q_NULLPTR): QObject(parent) { connect(this, &DSettingsBackend::sync, this, &DSettingsBackend::doSync, Qt::QueuedConnection); connect(this, &DSettingsBackend::setOption, this, &DSettingsBackend::doSetOption, Qt::QueuedConnection); diff --git a/src/settings/dsettingsgroup.cpp b/src/settings/dsettingsgroup.cpp index 28109a2..1b49d88 100644 --- a/src/settings/dsettingsgroup.cpp +++ b/src/settings/dsettingsgroup.cpp @@ -47,6 +47,12 @@ public: Q_DECLARE_PUBLIC(DSettingsGroup) }; +/*! + * \class DSettingsGroup + * \brief A group of DSettingsOption and DSettingsGroup. + * DSettingsGroup can contain a lost option and subgroup. + */ + DSettingsGroup::DSettingsGroup(QObject *parent) : QObject(parent), dd_ptr(new DSettingsGroupPrivate(this)) { @@ -57,49 +63,82 @@ DSettingsGroup::~DSettingsGroup() { } - +/*! + * \brief Get direct parent group of this group. + * \return + */ QPointer DSettingsGroup::parentGroup() const { Q_D(const DSettingsGroup); return d->parent; } +/*! + * \brief Change the direct parent group of this group. + * \param parentGroup + */ void DSettingsGroup::setParentGroup(QPointer parentGroup) { Q_D(DSettingsGroup); d->parent = parentGroup; } +/*! + * \brief Return the full key of this group, include all parent. + * \return + */ QString DSettingsGroup::key() const { Q_D(const DSettingsGroup); return d->key; } +/*! + * \brief Get display name of this group, it may be translated. + * \return + */ QString DSettingsGroup::name() const { Q_D(const DSettingsGroup); return d->name; } +/*! + * \brief Check this group will show on DSettings dialog. + * \return true if group not bind to ui element. + */ bool DSettingsGroup::isHidden() const { Q_D(const DSettingsGroup); return d->hide; } +/*! + * \brief Get the child group of groupKey + * \param groupKey is child group key + * \return + */ QPointer DSettingsGroup::childGroup(const QString &groupKey) const { Q_D(const DSettingsGroup); return d->childGroups.value(groupKey); } +/*! + * \brief Get the child option of key + * \param key is child option key + * \return + */ QPointer DSettingsGroup::option(const QString &key) const { Q_D(const DSettingsGroup); return d->childOptions.value(key); } +/*! + * \brief Enum all direct child group of this group + * \return + */ QList > DSettingsGroup::childGroups() const { Q_D(const DSettingsGroup); @@ -110,6 +149,10 @@ QList > DSettingsGroup::childGroups() const return grouplist; } +/*! + * \brief Enum all direct child option with the raw order in json description file. + * \return + */ QList > DSettingsGroup::childOptions() const { Q_D(const DSettingsGroup); @@ -120,12 +163,22 @@ QList > DSettingsGroup::childOptions() const return optionlist; } +/*! + * \brief Enum all direct child option of this group. + * \return + */ QList > DSettingsGroup::options() const { Q_D(const DSettingsGroup); return d->options.values(); } +/*! + * \brief Convert QJsonObject to DSettingsGroup. + * \param prefixKey instead parse prefix key from parent. + * \param json is an QJsonObejct instance. + * \sa QPointer Dtk::Core::DSettingsGroup::parseJson(const QString &prefixKey, const QJsonObject &json) + */ QPointer DSettingsGroup::fromJson(const QString &prefixKey, const QJsonObject &group) { auto groupPtr = QPointer(new DSettingsGroup); @@ -133,6 +186,12 @@ QPointer DSettingsGroup::fromJson(const QString &prefixKey, cons return groupPtr; } +/*! + * \brief Parse QJsonObject to DSettingsGroup. + * \param prefixKey instead parse prefix key from parent. + * \param json is an QJsonObejct instance. + * \sa QPointer Dtk::Core::DSettingsGroup::fromJson(const QString &prefixKey, const QJsonObject &json) + */ void DSettingsGroup::parseJson(const QString &prefixKey, const QJsonObject &group) { Q_D(DSettingsGroup); diff --git a/src/settings/dsettingsgroup.h b/src/settings/dsettingsgroup.h index b11a942..ab09a81 100644 --- a/src/settings/dsettingsgroup.h +++ b/src/settings/dsettingsgroup.h @@ -49,9 +49,6 @@ public: QList > options() const; static QPointer fromJson(const QString &prefixKey, const QJsonObject &group); -Q_SIGNALS: - -public Q_SLOTS: private: void parseJson(const QString &prefixKey, const QJsonObject &group); diff --git a/src/settings/dsettingsoption.cpp b/src/settings/dsettingsoption.cpp index fb2644c..82419f9 100644 --- a/src/settings/dsettingsoption.cpp +++ b/src/settings/dsettingsoption.cpp @@ -45,6 +45,22 @@ public: Q_DECLARE_PUBLIC(DSettingsOption) }; +/*! + * \class DSettingsOption + * \brief DSettingsOption is the base key/value item of DSettings. + * + * \fn void DSettingsOption::valueChanged(QVariant value); + * \brief Emit when option value change. + * + * \fn void DSettingsOption::dataChanged(const QString &dataType, QVariant value); + * \brief Emit when option data change. + * + * + + * \property DSettingsOption::value + * \brief Current value of this option. + */ + DSettingsOption::DSettingsOption(QObject *parent) : QObject(parent), dd_ptr(new DSettingsOptionPrivate(this)) { @@ -55,73 +71,128 @@ DSettingsOption::~DSettingsOption() } +/*! + * \brief Get direct parent group of this option. + * \return + */ QPointer DSettingsOption::parentGroup() const { Q_D(const DSettingsOption); return d->parent; } +/*! + * \brief Change the direct parent group of this option. + * \param parentGroup + */ void DSettingsOption::setParentGroup(QPointer parentGroup) { Q_D(DSettingsOption); d->parent = parentGroup; } +/*! + * \brief Return the full key of this option, include all parent. + * \return + */ QString DSettingsOption::key() const { Q_D(const DSettingsOption); return d->key; } +/*! + * \brief Get display name of the option, it may be translated. + * \return + */ QString DSettingsOption::name() const { Q_D(const DSettingsOption); return d->name; } +/*! + * \brief Check this option can be reset to default value. if false, reset action will not take effect. + * \return true if can be reset. + * \sa Dtk::Core::DSettings::reset + */ bool DSettingsOption::canReset() const { Q_D(const DSettingsOption); return d->canReset; } +/*! + * \brief Default value of this option, must config in this json desciption file. + * \return + */ QVariant DSettingsOption::defaultValue() const { Q_D(const DSettingsOption); return d->defalutValue; } +/*! + * \brief Get current value of option. + * \return + */ QVariant DSettingsOption::value() const { Q_D(const DSettingsOption); return (!d->value.isValid() || d->value.isNull()) ? d->defalutValue : d->value; } +/*! + * \brief Custom data of option, like QObject::property. + * \param dataType + * \return + * \sa QObject::property + * \sa Dtk::Core::DSettingsOption::setData + */ QVariant DSettingsOption::data(const QString &dataType) const { Q_D(const DSettingsOption); return d->datas.value(dataType); } +/*! + * \brief UI widget type of this option + * \return + * \sa Dtk::Widget::DSettingsWidgetFactory + */ QString DSettingsOption::viewType() const { Q_D(const DSettingsOption); return d->viewType; } +/*! + * \brief Check this option will show on DSettings dialog. + * \return true if option not bind to ui element. + */ bool DSettingsOption::isHidden() const { Q_D(const DSettingsOption); return d->hidden; } -QPointer DSettingsOption::fromJson(const QString &prefixKey, const QJsonObject &group) +/*! + * \brief Convert QJsonObject to DSettingsOption. + * \param prefixKey instead parse prefix key from parent. + * \param json is an QJsonObejct instance. + * \return + */ +QPointer DSettingsOption::fromJson(const QString &prefixKey, const QJsonObject &json) { auto optionPtr = QPointer(new DSettingsOption); - optionPtr->parseJson(prefixKey, group); + optionPtr->parseJson(prefixKey, json); return optionPtr; } +/*! + * \brief Set current value of option + * \param value + */ void DSettingsOption::setValue(QVariant value) { Q_D(DSettingsOption); @@ -134,16 +205,22 @@ void DSettingsOption::setValue(QVariant value) Q_EMIT valueChanged(value); } -//! -//! \brief DSettingsOption::setDefault will override default value of json -//! \param value -//! -// void DSettingsOption::setDefault(QVariant value) -// { -// Q_D(DSettingsOption); -// d->defalutValue = value; -// } - +///*! +// * \brief Override default value of json +// * \param value +// */ +//void DSettingsOption::setDefault(QVariant value) +//{ +// Q_D(DSettingsOption); +// d->defalutValue = value; +//} + +/*! + * \brief Set custom data + * \param dataType is data id, just a unique string. + * \param value of the data id. + * \sa Dtk::Core::DSettingsOption::data + */ void DSettingsOption::setData(const QString &dataType, QVariant value) { Q_D(DSettingsOption); @@ -157,6 +234,12 @@ void DSettingsOption::setData(const QString &dataType, QVariant value) Q_EMIT dataChanged(dataType, value); } +/*! + * \brief Parse QJsonObject to DSettingsOption. + * \param prefixKey instead parse prefix key from parent. + * \param json is an QJsonObejct instance. + * \sa QPointer Dtk::Core::DSettingsOption::fromJson(const QString &prefixKey, const QJsonObject &json) + */ void DSettingsOption::parseJson(const QString &prefixKey, const QJsonObject &option) { Q_D(DSettingsOption); diff --git a/src/settings/dsettingsoption.h b/src/settings/dsettingsoption.h index 0087c0d..72448df 100644 --- a/src/settings/dsettingsoption.h +++ b/src/settings/dsettingsoption.h @@ -49,7 +49,7 @@ public: QString viewType() const; bool isHidden() const; - static QPointer fromJson(const QString &prefixKey, const QJsonObject &group); + static QPointer fromJson(const QString &prefixKey, const QJsonObject &json); Q_SIGNALS: void valueChanged(QVariant value); void dataChanged(const QString &dataType, QVariant value); diff --git a/src/util/dabstractunitformatter.cpp b/src/util/dabstractunitformatter.cpp index 418ed31..6522d8d 100644 --- a/src/util/dabstractunitformatter.cpp +++ b/src/util/dabstractunitformatter.cpp @@ -19,16 +19,57 @@ DCORE_BEGIN_NAMESPACE +/*! + * \~chinese \class DAbstractUnitFormatter + * \~chinese \brief DAbstractUnitFormatter 类是对拥有相同类型数据管理的接口类 + * 接口定义了最大值、最小值、转换单位和单位对应的字符串。 + * + * \~chinese \fn DAbstractUnitFormatter::unitMax + * \~chinese \brief 返回列表中最大的单位 + * + * \~chinese \fn DAbstractUnitFormatter::unitMin + * \~chinese \brief 返回列表中最小的单位 + * + * \~chinese \fn DAbstractUnitFormatter::unitConvertRate + * \~chinese \brief 返回当前设置的转换单位 + * + * \~chinese \fn DAbstractUnitFormatter::unitValueMax + * \~chinese \brief 返回列表中根据当前设置的转换单位的最大值 + * + * \~chinese \fn DAbstractUnitFormatter::unitValueMin + * \~chinese \brief 返回列表中根据当前设置的转换单位的最小值 + * + * \~chinese \fn DAbstractUnitFormatter::unitStr + * \~chinese \brief 传入id,返回列表中对应的字符串 + */ + +/*! + * \~chinese \brief DAbstractUnitFormatter 的构造函数 + * + */ DAbstractUnitFormatter::DAbstractUnitFormatter() { } +/*! + * \~chinese \brief DAbstractUnitFormatter 的析构函数 + * + */ DAbstractUnitFormatter::~DAbstractUnitFormatter() { } +/*! + * \~chinese \brief 将传入的值从当前转换单位转换到目标单位上,返回转换过的值 + * 如果当前转换单位小于目标单位,值会被缩小,反之会放大,当前转换单位也会被缩小和放大,直至当前转换单位等于目标单位。 + * + * @param value 原始数值 + * @param currentUnit 当前的转换比率 + * @param targetUnit 目标的转换比率 + * @return qreal 返回转换过的值 + */ qreal DAbstractUnitFormatter::formatAs(qreal value, int currentUnit, const int targetUnit) const { while (currentUnit < targetUnit) @@ -39,6 +80,15 @@ qreal DAbstractUnitFormatter::formatAs(qreal value, int currentUnit, const int t return value; } +/*! + * \~chinese \brief 将值转换到最合适的单位上 + * + * 如果值大于 unitMin() 或者小于 unitMax() ,会尽量保证值被转换到接近最小值的合适单位上。 + * + * @param value 原始数值 + * @param unit 当前的转换单位 + * @return QPair 转换过的数值和转化单位 + */ QPair DAbstractUnitFormatter::format(const qreal value, const int unit) const { // can convert to smaller unit @@ -52,6 +102,13 @@ QPair DAbstractUnitFormatter::format(const qreal value, const int un return QPair(value, unit); } +/*! + * \~chinese \brief 是 format() ,但是包含了完整的转换数据 + * + * @param value + * @param unit + * @return QList > + */ QList > DAbstractUnitFormatter::formatAsUnitList(const qreal value, int unit) const { if (qFuzzyIsNull(value)) diff --git a/src/util/ddisksizeformatter.cpp b/src/util/ddisksizeformatter.cpp index fbd9806..f5c408f 100644 --- a/src/util/ddisksizeformatter.cpp +++ b/src/util/ddisksizeformatter.cpp @@ -21,12 +21,50 @@ DCORE_BEGIN_NAMESPACE +/*! + * \~chinese \class DDiskSizeFormatter + * + * \~chinese \brief DDiskSizeFormatter 是用来获取磁盘容量单位的类, 通过枚举值 + * 获取不同类型磁盘容量的单位 + * + * \~chinese \enum DDiskSizeFormatter::DiskUnits 磁盘容量单位的枚举 + * \~chinese \var DDiskSizeFormatter::DiskUnits DDiskSizeFormatter::B + * \~chinese \brief 字节 + * \~chinese \var DDiskSizeFormatter::DiskUnits DDiskSizeFormatter::K + * \~chinese \brief 千字节 + * \~chinese \var DDiskSizeFormatter::DiskUnits DDiskSizeFormatter::M + * \~chinese \brief 兆字节 + * \~chinese \var DDiskSizeFormatter::DiskUnits DDiskSizeFormatter::G + * \~chinese \brief 吉字节 + * \~chinese \var DDiskSizeFormatter::DiskUnits DDiskSizeFormatter::T + * \~chinese \brief 太字节 + * + * \~chinese \fn DDiskSizeFormatter::unitMax + * \~chinese \brief 返回最大磁盘容量单位的枚举 + * + * \~chinese \fn DDiskSizeFormatter::unitMin + * \~chinese \brief 返回最小磁盘容量单位的枚举 + * + * \~chinese \fn DDiskSizeFormatter::unitConvertRate + * \~chinese \brief 返回当前的单位转换比率 + */ + +/*! + * \~chinese \brief DDiskSizeFormatter的构造函数 + * + */ DDiskSizeFormatter::DDiskSizeFormatter() : DAbstractUnitFormatter() { } +/*! + * \~chinese \brief 根据枚举返回对应单位的字符串 + * + * @param unitId DDiskSizeFormatter::DiskUnits 的枚举值 + * @return QString 对应单位的字符串 + */ QString DDiskSizeFormatter::unitStr(int unitId) const { switch (unitId) @@ -41,6 +79,12 @@ QString DDiskSizeFormatter::unitStr(int unitId) const return QString(); } +/*! + * \~chinese \brief 设置当前的单位转换比率 + * + * @param rate 转换比率 + * @return DDiskSizeFormatter 返回 DDiskSizeFormatter 对象 + */ DDiskSizeFormatter DDiskSizeFormatter::rate(int rate) { m_rate = rate; diff --git a/src/util/dtimeunitformatter.cpp b/src/util/dtimeunitformatter.cpp index 79cc831..508e6db 100644 --- a/src/util/dtimeunitformatter.cpp +++ b/src/util/dtimeunitformatter.cpp @@ -21,12 +21,45 @@ DCORE_BEGIN_NAMESPACE +/*! + * \~chinese \class DTimeUnitFormatter + * + * \~chinese \brief DTimeUnitFormatter是用来获取时间单位的类, 通过枚举值 + * 获取不同类型时间单位的进制 + * + * \~chinese \enum DTimeUnitFormatter::TimeUnits 时间单位的枚举 + * \~chinese \var DTimeUnitFormatter::TimeUnits DTimeUnitFormatter::Seconds + * \~chinese \brief 返回分钟单位的进制 + * \~chinese \var DTimeUnitFormatter::TimeUnits DTimeUnitFormatter::Minute + * \~chinese \brief 返回秒单位的进制 + * \~chinese \var DTimeUnitFormatter::TimeUnits DTimeUnitFormatter::Hour + * \~chinese \brief 返回小时单位的进制 + * \~chinese \var DTimeUnitFormatter::TimeUnits DTimeUnitFormatter::Day + * \~chinese \brief 返回天单位的进制 + * + * \~chinese \fn DTimeUnitFormatter::unitMax + * \~chinese \brief 返回最大时间单位的枚举 + * + * \~chinese \fn DTimeUnitFormatter::unitMin + * \~chinese \brief 返回最小时间单位的枚举 + */ + +/*! + * \~chinese \brief DTimeUnitFormatter的构造函数 + * + */ DTimeUnitFormatter::DTimeUnitFormatter() : DAbstractUnitFormatter() { } +/*! + * \~chinese \brief 根据枚举返回对应的单位进制 + * + * @param unitId DTimeUnitFormatter::TimeUnits 的枚举值 + * @return uint 对应的单位进制 + */ uint DTimeUnitFormatter::unitConvertRate(int unitId) const { switch (unitId) @@ -40,6 +73,12 @@ uint DTimeUnitFormatter::unitConvertRate(int unitId) const return 0; } +/*! + * \~chinese \brief 根据枚举返回对应单位的缩写 + * + * @param unitId DTimeUnitFormatter::TimeUnits 的枚举值 + * @return QString 对应单位的缩写 + */ QString DTimeUnitFormatter::unitStr(int unitId) const { switch (unitId) diff --git a/tests/dutiltester.cpp b/tests/dutiltester.cpp index 69edb3f..9ccd992 100644 --- a/tests/dutiltester.cpp +++ b/tests/dutiltester.cpp @@ -42,7 +42,7 @@ void TestDUtil::testLogPath() qApp->setApplicationName("deepin-test-dtk"); DPathBuf logPath(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); - logPath = logPath / ".cache/deepin/deepin-test-dtk/deepin-test-dtk.log"; + logPath = logPath / ".cache" / "deepin" / "deepin-test-dtk" / "deepin-test-dtk.log"; QCOMPARE(DLogManager::getlogFilePath(), logPath.toString()); } @@ -160,40 +160,41 @@ void TestDUtil::testDBusSender() { // basic method call DDBusSender() - .service("com.deepin.dde.ControlCenter") - .interface("com.deepin.dde.ControlCenter") - .path("/com/deepin/dde/ControlCenter") - .method("ShowPage") - .arg(QString("update")) - .arg(QString("available-updates")) - .call(); + .service("com.deepin.dde.ControlCenter") + .interface("com.deepin.dde.ControlCenter") + .path("/com/deepin/dde/ControlCenter") + .method("ShowPage") + .arg(QString("update")) + .arg(QString("available-updates")) + .call(); // property set QDBusPendingReply<> r1 = DDBusSender() - .service("com.deepin.dde.daemon.Dock") - .interface("com.deepin.dde.daemon.Dock") - .path("/com/deepin/dde/daemon/Dock") - .property("DisplayMode") - .set(1); // set to efficient mode + .service("com.deepin.dde.daemon.Dock") + .interface("com.deepin.dde.daemon.Dock") + .path("/com/deepin/dde/daemon/Dock") + .property("DisplayMode") + .set(1); // set to efficient mode // property get QDBusPendingReply r2 = DDBusSender() - .service("com.deepin.dde.daemon.Dock") - .interface("com.deepin.dde.daemon.Dock") - .path("/com/deepin/dde/daemon/Dock") - .property("DisplayMode") - .get(); // read mode + .service("com.deepin.dde.daemon.Dock") + .interface("com.deepin.dde.daemon.Dock") + .path("/com/deepin/dde/daemon/Dock") + .property("DisplayMode") + .get(); // read mode - if (!r2.isError() && !r1.isError()) + if (!r2.isError() && !r1.isError()) { Q_ASSERT(r2.value().toInt() == 1); + } // complex type property get QDBusPendingReply r3 = DDBusSender() - .service("com.deepin.dde.ControlCenter") - .interface("com.deepin.dde.ControlCenter") - .path("/com/deepin/dde/ControlCenter") - .property("Rect") - .get(); + .service("com.deepin.dde.ControlCenter") + .interface("com.deepin.dde.ControlCenter") + .path("/com/deepin/dde/ControlCenter") + .property("Rect") + .get(); QVariant variant = r3.value(); const QDBusArgument v = variant.value(); -- 2.30.2