From d58c928b26e0b095d30ebbe3ee5407dc2d6c6f43 Mon Sep 17 00:00:00 2001 From: tianduanrui <2407223896@qq.com> Date: Sat, 25 Nov 2017 10:58:30 +0800 Subject: [PATCH] update qqt dict --- QQt.pro | 4 +- examples/qqthttpdownload/main.cpp | 66 +++-- examples/qqthttpdownload/qqtdictionary.cpp | 294 ++++++++------------- examples/qqthttpdownload/qqtdictionary.h | 117 ++++---- test/qqtdicttest/tst_qqtdicttesttest.cpp | 2 +- 5 files changed, 223 insertions(+), 260 deletions(-) diff --git a/QQt.pro b/QQt.pro index 1c03bed3..b0f7f1ba 100644 --- a/QQt.pro +++ b/QQt.pro @@ -1,8 +1,7 @@ TEMPLATE = subdirs CONFIG += ordered -SUBDIRS = src/qqt.pro \ - test/qqtdicttest +SUBDIRS = src/qqt.pro ##----------------------------------------------------------------- ##basic example @@ -81,3 +80,4 @@ greaterThan(QT_MAJOR_VERSION, 4):SUBDIRS += examples/qqthttpdownload #SUBDIRS += test/svgtest #SUBDIRS += test/framelesshelperwidget #SUBDIRS += test/treeviewtest +#SUBDIRS += test/qqtdicttest diff --git a/examples/qqthttpdownload/main.cpp b/examples/qqthttpdownload/main.cpp index 672d118a..0f839c9a 100644 --- a/examples/qqthttpdownload/main.cpp +++ b/examples/qqthttpdownload/main.cpp @@ -8,6 +8,7 @@ #include "qqtqtiowebpageparser.h" #include "qqtdictionary.h" +#define plinen() pline() << "\n" int main ( int argc, char* argv[] ) { QQtApplication a ( argc, argv ); @@ -29,27 +30,27 @@ int main ( int argc, char* argv[] ) //MainWindow w; //w.show(); - QQtDict d0; + QQtDictNode d0; d0["cc"] = "dd"; qDebug() << d0["cc"].getValue(); - QQtDict d1; - QQtDict d2 ( QVariant ( "CCCC" ) ); - d1.appendValue ( d2 ); + QQtDictNode d1; + QQtDictNode d2 ( QVariant ( "CCCC" ) ); + d1.appendChild ( d2 ); qDebug() << d1[0].getValue(); d1[0] = "ff"; qDebug() << d1[0].getValue(); - QQtDict d3; + QQtDictNode d3; d3["cc"]["dd"] = "ee"; qDebug() << d3["cc"]["dd"].getValue().toString(); - QQtDict d4; + QQtDictNode d4; for ( int i = 0; i < 5; i++ ) { - QQtDict d ( QVariant ( QString::number ( i ) ) ); - d4.appendValue ( d ); + QQtDictNode d ( QVariant ( QString::number ( i ) ) ); + d4.appendChild ( d ); } qDebug() << d4.count(); @@ -59,20 +60,55 @@ int main ( int argc, char* argv[] ) qDebug() << d4[i].getValue().toString(); } - QQtDict d5; + QQtDictNode d5; + + /*后续有map操作,这一步就没有用了*/ d5.appendChild ( "5.7" ); d5.appendChild ( "5.8" ); d5.appendChild ( "5.9" ); + + /*后续有map操作,这一步就没有用了*/ d5["5.7"].appendChild ( "5.7.2" ); d5["5.7"].appendChild ( "5.7.3" ); d5["5.7"].appendChild ( "5.7.4" ); - d5["5.7"]["5.7.4"].appendChild ( "xxx.dmg" ); - d5["5.7"]["5.7.4"]["xxx.dmg"].appendValue ( "xxx.dmg" ); - d5["5.7"]["5.7.4"]["xxx.dmg"].appendValue ( "2017-12-12" ); - d5["5.7"]["5.7.4"]["xxx.dmg"].appendValue ( "1.2G" ); - d5["5.7"]["5.7.4"]["xxx.dmg"].appendValue ( "Detail" ); - pline() << "\n" << d5["5.7"]["5.7.4"]; + d5["5.7"]["5.7.4"].appendChild ( "xxx.dmg" ); + d5["5.7"]["5.7.4"][0].appendChild ( "xxx.dmg" ); + d5["5.7"]["5.7.4"][0].appendChild ( "2017-12-12" ); + d5["5.7"]["5.7.4"][0].appendChild ( "1.2G" ); + d5["5.7"]["5.7.4"][0].appendChild ( "Detail" ); + d5["5.7"]["5.7.4"][0].insertChild ( 4, "Detail2" ); + + //pline() << "\n" << d5; + pline() << "\n" << d5["5.7"]; + + QQtMapNodeIterator itor ( d5["5.7"].getMap() ); + + while ( itor.hasNext() ) + { + itor.next(); + pline() << "\n" << itor.key() << itor.value(); + } + + pline () << d5["5.7"]["5.7.4"][0].getType(); + pline () << d5["5.7"]["5.7.4"][0].count(); + + for ( int i = 0; i < d5["5.7"]["5.7.4"][0].count(); i++ ) + { + plinen() << d5["5.7"]["5.7.4"][0][i].getValue().toString(); + } + + QQtDictNode node = d5; + plinen() << node["5.7"]["5.7.4"][0][0].getValue().toString(); + + /*在这里有list操作,前边"5.7"的map就没有用了*/ + /*但是再过去做过的list类型的操作都会被保留,也就是说中间出现过概念错误,没问题还保留着*/ + d5["5.7"].appendChild ( "5.7.5" ); + + for ( int i = 0; i < d5["5.7"].count(); i++ ) + { + pline() << d5["5.7"][i].getValue().toString(); + } return 0;//a.exec(); } diff --git a/examples/qqthttpdownload/qqtdictionary.cpp b/examples/qqthttpdownload/qqtdictionary.cpp index 02a9bbc7..969d41c0 100644 --- a/examples/qqthttpdownload/qqtdictionary.cpp +++ b/examples/qqthttpdownload/qqtdictionary.cpp @@ -1,12 +1,12 @@ #include "qqtdictionary.h" -QQtDict::QQtDict ( QObject* parent ) : +QQtDictNode::QQtDictNode ( QObject* parent ) : QObject ( parent ) { m_type = DictMax; } -bool QQtDict::isValue() const +bool QQtDictNode::isValue() const { bool is = false; @@ -16,89 +16,68 @@ bool QQtDict::isValue() const return is; } -QQtDict::EDictType QQtDict::getType() const +QQtDictNode::EDictType QQtDictNode::getType() const { return m_type; } -void QQtDict::setType ( QQtDict::EDictType type ) +void QQtDictNode::setType ( QQtDictNode::EDictType type ) { m_type = type; } -void QQtDict::setValue ( QVariant& value ) +void QQtDictNode::setChild ( const QVariant& value ) { m_type = DictValue; m_value = value; } -void QQtDict::setValue ( QList& list ) +void QQtDictNode::setChild ( const QList& list ) { m_type = DictList; m_list = list; } -void QQtDict::setValue ( QMap& map ) +void QQtDictNode::setChild ( const QMap& map ) { m_type = DictMap; m_map = map; } -void QQtDict::setChild ( QList& list ) +void QQtDictNode::appendChild ( const QString& value ) { - setValue ( list ); + appendChild ( QQtDictNode ( QVariant ( value ) ) ); } -void QQtDict::setChild ( QMap& map ) -{ - setValue ( map ); -} - -void QQtDict::appendValue ( const QString& value ) -{ - m_type = DictList; - m_list.push_back ( QQtDict ( QVariant ( value ) ) ); -} - -void QQtDict::appendValue ( const QQtDict& dict ) +void QQtDictNode::appendChild ( const QQtDictNode& dict ) { m_type = DictList; m_list.append ( dict ); } -void QQtDict::appendChild ( const QQtDict& dict ) -{ - appendValue ( dict ); -} - -void QQtDict::insertValue ( const QString& key, QQtDict& dict ) +void QQtDictNode::insertChild ( const QString& key, const QQtDictNode& dict ) { m_type = DictMap; m_map.insert ( key, dict ); } -void QQtDict::insertChild ( const QString& key, QQtDict& dict ) +void QQtDictNode::insertChild ( int index, const QString& value ) { - insertValue ( key, dict ); + insertChild ( index, QQtDictNode ( QVariant ( value ) ) ); } -void QQtDict::insertValue ( int index, QQtDict& dict ) +void QQtDictNode::insertChild ( int index, const QQtDictNode& dict ) { m_type = DictList; m_list.insert ( index, dict ); } -void QQtDict::appendChild ( const QString& value ) +void QQtDictNode::insertChild ( const QString& key, const QString& value ) { - appendValue ( value ); + insertChild ( key, QQtDictNode ( QVariant ( value ) ) ); } -void QQtDict::insertChild ( int index, QQtDict& dict ) -{ - insertValue ( index, dict ); -} - -int QQtDict::count() const +int QQtDictNode::count() const { int cnt = -1; @@ -112,7 +91,7 @@ int QQtDict::count() const return cnt; } -bool QQtDict::isNull() const +bool QQtDictNode::isNull() const { if ( m_type == DictMax ) return true; @@ -120,11 +99,11 @@ bool QQtDict::isNull() const return false; } -bool QQtDict::isValid() const +bool QQtDictNode::isValid() const { return isNull(); } -bool QQtDict::isEmpty() const +bool QQtDictNode::isEmpty() const { bool isEmpty = true; @@ -156,179 +135,137 @@ bool QQtDict::isEmpty() const } -bool QQtDict::isList() const +bool QQtDictNode::isList() const { bool is = false; - if ( !m_type == DictList ) - is = true; - - return is; -} - -bool QQtDict::isMap() const -{ - bool is = false; - - if ( !m_type == DictMap ) - is = true; - - return is; - -} - -QString& QQtDict::getName() const -{ - return ( QString& ) m_name; -} - -bool QQtDict::hasKey ( const QString& key ) const -{ - bool has = false; - - if ( m_type == DictMap ) - if ( m_map.contains ( key ) ) - has = true; - - return has; -} - -bool QQtDict::hasKey ( const QQtDict& value ) const -{ - bool has = false; - if ( m_type == DictList ) - if ( m_list.contains ( value ) ) - has = true; + is = true; + + return is; +} + +bool QQtDictNode::isMap() const +{ + bool is = false; + + if ( m_type == DictMap ) + is = true; + + return is; + +} + +bool QQtDictNode::hasChild ( const QString& key ) const +{ + bool has = false; + + if ( m_map.contains ( key ) ) + has = true; return has; } -bool QQtDict::hasChild ( const QString& key ) const +bool QQtDictNode::hasChild ( const QQtDictNode& value ) const { - return hasKey ( key ); + bool has = false; + + if ( m_list.contains ( value ) ) + has = true; + + return has; } -bool QQtDict::hasChild ( const QQtDict& value ) const +void QQtDictNode::modValue ( const QVariant& value ) { - return hasKey ( value ); + m_type = DictValue; + m_value = value; } -void QQtDict::modValue ( QVariant& value ) +void QQtDictNode::modChild ( int index, const QQtDictNode& value ) { - if ( DictValue == m_type ) - { - m_value = value; - } + m_type = DictList; + m_list[index] = value; } -void QQtDict::modValue ( int index, QQtDict& value ) +void QQtDictNode::modChild ( QString key, const QQtDictNode& value ) { - if ( DictList == m_type ) - { - m_list[index] = value; - } + m_type = DictMap; + m_map[key] = value; } -void QQtDict::modValue ( QString key, QQtDict& value ) +void QQtDictNode::clear() { - if ( DictMap == m_type ) - { - m_map[key] = value; - } + m_value.clear(); + m_list.clear(); + m_map.clear(); } -void QQtDict::modChild ( int index, QQtDict& value ) +void QQtDictNode::remove ( int index ) { - modValue ( index, value ); + m_list.removeAt ( index ); } -void QQtDict::modChild ( QString key, QQtDict& value ) +void QQtDictNode::remove ( const QString& key ) { - modValue ( key, value ); + m_map.remove ( key ); } -void QQtDict::clear() -{ - if ( DictValue == m_type ) - { - m_value.clear(); - } - else if ( DictList == m_type ) - { - m_list.clear(); - } - else if ( DictMap == m_type ) - { - m_map.clear(); - } -} - -void QQtDict::remove ( int index ) -{ - if ( DictList == m_type ) - { - m_list.removeAt ( index ); - } -} - -void QQtDict::remove ( const QString& key ) -{ - if ( DictMap == m_type ) - { - m_map.remove ( key ); - } -} - -QQtDict::QQtDict ( const QQtDict& other, QObject* parent ) : +QQtDictNode::QQtDictNode ( const QQtDictNode& other, QObject* parent ) : QObject ( parent ) { *this = other; } -QQtDict::QQtDict ( const QString name, QQtDict::EDictType type, QObject* parent ) : - QObject ( parent ) -{ - m_name = name; - m_type = type; -} - -QQtDict::QQtDict ( const QQtDict::EDictType type, QObject* parent ) : +QQtDictNode::QQtDictNode ( const QQtDictNode::EDictType type, QObject* parent ) : QObject ( parent ) { m_type = type; } -QQtDict::QQtDict ( const QVariant& value, QObject* parent ) : +const QQtDictNode& QQtDictNode::operator[] ( const QString& key ) const +{ + return m_map[key]; +} + +const QQtDictNode& QQtDictNode::operator[] ( int index ) const +{ + return m_list[index]; +} + +QQtDictNode::QQtDictNode ( const QVariant& value, QObject* parent ) : QObject ( parent ) { - m_value = value; - m_type = DictValue; + *this = value; } -QQtDict& QQtDict::operator [] ( int index ) +QQtDictNode& QQtDictNode::operator [] ( int index ) { - return ( QQtDict& ) m_list.operator [] ( index ); + m_type = DictList; + return ( QQtDictNode& ) m_list.operator [] ( index ); } -QQtDict& QQtDict::operator [] ( QString key ) +QQtDictNode& QQtDictNode::operator [] ( const QString& key ) { + m_type = DictMap; return m_map.operator [] ( key ); } -QQtDict& QQtDict::operator = ( const QMap& map ) +QQtDictNode& QQtDictNode::operator = ( const QMap& map ) { + m_type = DictMap; m_map = map; return *this; } -QQtDict& QQtDict::operator = ( const QList& list ) +QQtDictNode& QQtDictNode::operator = ( const QList& list ) { + m_type = DictList; m_list = list; return *this; } -QQtDict& QQtDict::operator = ( const QQtDict& other ) +QQtDictNode& QQtDictNode::operator = ( const QQtDictNode& other ) { EDictType type = other.getType(); @@ -352,21 +289,20 @@ QQtDict& QQtDict::operator = ( const QQtDict& other ) break; } - m_name = other.getName(); m_type = type; return *this; } -QQtDict& QQtDict::operator = ( const QVariant& value ) +QQtDictNode& QQtDictNode::operator = ( const QVariant& value ) { + m_type = DictValue; m_value = value; return *this; } -bool QQtDict::operator == ( const QQtDict& other ) const +bool QQtDictNode::operator == ( const QQtDictNode& other ) const { if ( m_type == other.getType() && - other.getName() == m_name && other.getList() == m_list && other.getMap() == m_map && other.getValue() == m_value ) @@ -375,51 +311,41 @@ bool QQtDict::operator == ( const QQtDict& other ) const return false; } -QMap& QQtDict::getMap() const +QMap& QQtDictNode::getMap() const { - return ( QMap& ) m_map; + return ( QMap& ) m_map; } -QList& QQtDict::getList() const +QList& QQtDictNode::getList() const { - return ( QList& ) m_list; + return ( QList& ) m_list; } -QVariant& QQtDict::getValue() const +QVariant& QQtDictNode::getValue() const { return ( QVariant& ) m_value; } -QQtDict& QQtDict::getValue ( int index ) const -{ - return ( QQtDict& ) m_list[index]; -} - -QQtDict& QQtDict::getValue ( const QString& key ) -{ - return m_map[key]; -} - -QQtDict& QQtDict::getChild ( int index ) +QQtDictNode& QQtDictNode::getChild ( int index ) { return m_list[index]; } -QQtDict& QQtDict::getChild ( QString key ) +QQtDictNode& QQtDictNode::getChild ( const QString& key ) { return m_map[key]; } -QDebug operator<< ( QDebug dbg, const QQtDict& d ) + +QDebug operator<< ( QDebug dbg, const QQtDictNode& d ) { - dbg.nospace() << "{" << - "\n Type:" << d.getType() << - "\n Count:" << d.count() << - "\n Name:" << d.getName() << - "\n Value:" << d.getValue() << - "\n List:" << d.getList() << - "\n Map:" << d.getMap() << - "\n}"; + dbg.nospace() << "\n{" << + " Type:" << d.getType() << + " Count:" << d.count() << + " Value:" << d.getValue() << + " List:" << d.getList() << + " Map:" << d.getMap() << + "}"; return dbg.space(); } diff --git a/examples/qqthttpdownload/qqtdictionary.h b/examples/qqthttpdownload/qqtdictionary.h index 129cc272..7896f977 100644 --- a/examples/qqthttpdownload/qqtdictionary.h +++ b/examples/qqthttpdownload/qqtdictionary.h @@ -7,6 +7,17 @@ #include #include +/* + * 简化使用 + * 遍历时 + */ +class QQtDictNode; +typedef QMap QQtMapNode; +typedef QMapIterator QQtMapNodeIterator; + +typedef QList QQtListNode; +typedef QListIterator QQtListNodeIterator; + /** * @brief The QQtDictionary class * QQt 字典 @@ -20,7 +31,7 @@ * QVariant 不能直接获取到真实数据,改变必须使用临时变量。 * 而且,接口设计也不够灵活,存入和取出都不太方便。 */ -class QQTSHARED_EXPORT QQtDict : public QObject +class QQTSHARED_EXPORT QQtDictNode : public QObject { Q_OBJECT Q_ENUMS ( EDictType ) @@ -37,8 +48,9 @@ public: DictMax } EDictType; - explicit QQtDict ( QObject* parent = 0 ); - virtual ~QQtDict() {} + /*explicit 函数只能作为构造函数,不能作为拷贝构造函数,拷贝构造函数不可加*/ + explicit QQtDictNode ( QObject* parent = 0 ); + virtual ~QQtDictNode() {} bool isNull() const; bool isValid() const; @@ -48,66 +60,52 @@ public: bool isList() const; bool isMap() const; + /*遍历字典*/ + int count() const; + + bool hasChild ( const QString& key ) const; + bool hasChild ( const QQtDictNode& value ) const; + /*获取数据*/ - QString& getName() const; /*获取全部数据*/ - QMap& getMap() const; - QList& getList() const ; + QMap& getMap() const; + QList& getList() const ; /*获取单个数据*/ QVariant& getValue() const; - QQtDict& getValue ( int index ) const; - QQtDict& getValue ( const QString& key ); + QQtDictNode& getChild ( int index ); + QQtDictNode& getChild ( const QString& key ); /*获取一个个孩子*/ - /*list item*/ - QQtDict& getChild ( int index ); - /*map item*/ - QQtDict& getChild ( QString key ); /*类型*/ EDictType getType() const; + /*如果设置Value的时候改变了Type,将会以新的Type为准*/ void setType ( EDictType type ); /*插入数据,自动设置type*/ - /*自己本身没有孩子,是个叶子,添加值*/ - void setValue ( QVariant& value ); + void setChild ( const QVariant& value ); /*自己本身有孩子,添加全部孩子*/ /*whole value list*/ - void setValue ( QList& list ); + void setChild ( const QList& list ); /*whole value map*/ - void setValue ( QMap& map ); - /*list*/ - void setChild ( QList& list ); - /*map*/ - void setChild ( QMap& map ); + void setChild ( const QMap& map ); /*自己本身没有孩子,添加一个个的孩子*/ - void appendValue ( const QString& value ); - void appendValue ( const QQtDict& dict ); void appendChild ( const QString& value ); - void appendChild ( const QQtDict& dict ); + void appendChild ( const QQtDictNode& dict ); + /*自己本身有孩子,添加一个个的孩子*/ - void insertValue ( const QString& key, QQtDict& dict ); - void insertChild ( const QString& key, QQtDict& dict ); - void insertValue ( int index, QQtDict& dict ); - void insertChild ( int index, QQtDict& dict ); - - /*遍历字典*/ - int count() const; - - bool hasKey ( const QString& key ) const; - bool hasKey ( const QQtDict& value ) const; - bool hasChild ( const QString& key ) const; - bool hasChild ( const QQtDict& value ) const; + void insertChild ( int index, const QString& value ); + void insertChild ( int index, const QQtDictNode& dict ); + void insertChild ( const QString& key, const QString& value ); + void insertChild ( const QString& key, const QQtDictNode& dict ); /*操作数据,改变数据*/ - void modValue ( QVariant& value ); - void modValue ( int index, QQtDict& value ); - void modValue ( QString key, QQtDict& value ); - void modChild ( int index, QQtDict& value ); - void modChild ( QString key, QQtDict& value ); + void modValue ( const QVariant& value ); + void modChild ( int index, const QQtDictNode& value ); + void modChild ( QString key, const QQtDictNode& value ); /*删除数据*/ void clear ( ); @@ -115,21 +113,23 @@ public: void remove ( const QString& key ); /*深拷贝*/ - explicit QQtDict ( const QQtDict& other, QObject* parent = 0 ); - explicit QQtDict ( const QVariant& value, QObject* parent = 0 ); - explicit QQtDict ( const QString name, EDictType type = DictMap, QObject* parent = 0 ); - explicit QQtDict ( const EDictType type, QObject* parent = 0 ); + QQtDictNode ( const QQtDictNode& other, QObject* parent = 0 ); + QQtDictNode ( const QVariant& value, QObject* parent = 0 ); + QQtDictNode ( const EDictType type, QObject* parent = 0 ); /*操作符*/ + /*警告:可读、可写*/ /*don't out of range*/ - QQtDict& operator [] ( int index ); - /**/ - QQtDict& operator [] ( QString key ); - QQtDict& operator = ( const QMap& map ); - QQtDict& operator = ( const QList& list ); - QQtDict& operator = ( const QQtDict& other ); - QQtDict& operator = ( const QVariant& value ); - bool operator == ( const QQtDict& other ) const; + QQtDictNode& operator [] ( int index ); + const QQtDictNode& operator[] ( int index ) const; + QQtDictNode& operator [] ( const QString& key ); + const QQtDictNode& operator[] ( const QString& key ) const; + + QQtDictNode& operator = ( const QMap& map ); + QQtDictNode& operator = ( const QList& list ); + QQtDictNode& operator = ( const QQtDictNode& other ); + QQtDictNode& operator = ( const QVariant& value ); + bool operator == ( const QQtDictNode& other ) const; /*与其他数据结构兼容*/ QString toXML(); @@ -142,21 +142,22 @@ signals: public slots: private: - /*节点类型*/ + /*节点类型,指示性变量*/ EDictType m_type; - /*节点名字*/ - QString m_name; /*节点的可能内容枚举*/ /*叶子:是个数据*/ + /*值保存在这里*/ QVariant m_value; /*不是叶子列表,是个叶子列表,是个叶子列表的值*/ - QList m_list; //[index] + /*列表保存在这里*//*不如仅仅使用map方便*/ + QList m_list; //[index] /*不是叶子映射,是个子字典,是个叶子,是个叶子的值组合*/ - QMap m_map; + /*映射保存在这里,QStirng可以升级为QVariant*/ + QMap m_map; /*是个列表和子字典,这是错误的,不可能的*/ }; -QDebug operator<< ( QDebug dbg, const QQtDict& d ); +QDebug operator<< ( QDebug dbg, const QQtDictNode& d ); #endif // QQTDICTIONARY_H diff --git a/test/qqtdicttest/tst_qqtdicttesttest.cpp b/test/qqtdicttest/tst_qqtdicttesttest.cpp index a0e17c5c..d7b3db2a 100644 --- a/test/qqtdicttest/tst_qqtdicttesttest.cpp +++ b/test/qqtdicttest/tst_qqtdicttesttest.cpp @@ -19,7 +19,7 @@ private Q_SLOTS: void testCase2(); private: - QQtDict dict; + QQtDictNode dict; }; QqtdicttestTest::QqtdicttestTest()