mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
add qqt dictionary
This commit is contained in:
parent
f21e3a71a5
commit
f08268437c
@ -6,6 +6,7 @@
|
||||
#include "qqthttpdownloadmanager.h"
|
||||
#include "qqtapplication.h"
|
||||
#include "qqtqtiowebpageparser.h"
|
||||
#include "qqtdictionary.h"
|
||||
|
||||
int main ( int argc, char* argv[] )
|
||||
{
|
||||
@ -22,11 +23,13 @@ int main ( int argc, char* argv[] )
|
||||
QQtObjectParcel::registerObject ( obj );
|
||||
*/
|
||||
|
||||
QQtQtIOWebPageParser webparser;
|
||||
webparser.startNewParse ( );
|
||||
//QQtQtIOWebPageParser webparser;
|
||||
//webparser.startNewParse ( );
|
||||
|
||||
//MainWindow w;
|
||||
//w.show();
|
||||
|
||||
QQtDictionary dict;
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
510
examples/qqthttpdownload/qqtdictionary.cpp
Normal file
510
examples/qqthttpdownload/qqtdictionary.cpp
Normal file
@ -0,0 +1,510 @@
|
||||
#include "qqtdictionary.h"
|
||||
|
||||
QQtDictionary::QQtDictionary ( QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
m_type = DictMax;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValue()
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( m_type == DictValue )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
QQtDictionary::EDictType QQtDictionary::getType() { return m_type; }
|
||||
|
||||
void QQtDictionary::setType ( QQtDictionary::EDictType type )
|
||||
{
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
void QQtDictionary::setValue ( QVariant& value )
|
||||
{
|
||||
m_type = DictValue;
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
void QQtDictionary::setValue ( QList<QVariant>& list )
|
||||
{
|
||||
m_type = DictValueList;
|
||||
m_valueList = list;
|
||||
}
|
||||
|
||||
void QQtDictionary::setValue ( QMap<QString, QVariant>& map )
|
||||
{
|
||||
m_type = DictValueMap;
|
||||
m_valueMap = map;
|
||||
}
|
||||
|
||||
void QQtDictionary::addValue ( QVariant& value )
|
||||
{
|
||||
m_type = DictValueList;
|
||||
m_valueList.append ( value );
|
||||
}
|
||||
|
||||
void QQtDictionary::insertValue ( QString key, QVariant& value )
|
||||
{
|
||||
m_type = DictValueMap;
|
||||
m_valueMap.insert ( key, value );
|
||||
}
|
||||
|
||||
void QQtDictionary::insertValue ( int index, QVariant& value )
|
||||
{
|
||||
m_type = DictValueList;
|
||||
m_valueList.insert ( index, value );
|
||||
}
|
||||
|
||||
void QQtDictionary::setChild ( QList<QQtDictionary>& list )
|
||||
{
|
||||
m_type = DictList;
|
||||
m_list = list;
|
||||
}
|
||||
|
||||
void QQtDictionary::setChild ( QMap<QString, QQtDictionary>& map )
|
||||
{
|
||||
m_type = DictMap;
|
||||
m_map = map;
|
||||
}
|
||||
|
||||
void QQtDictionary::addChild ( QQtDictionary& dict )
|
||||
{
|
||||
m_type = DictList;
|
||||
m_list.append ( dict );
|
||||
}
|
||||
|
||||
void QQtDictionary::insertChild ( QString key, QQtDictionary& dict )
|
||||
{
|
||||
m_type = DictMap;
|
||||
m_map.insert ( key, dict );
|
||||
}
|
||||
|
||||
void QQtDictionary::addChild ( int index, QQtDictionary& dict )
|
||||
{
|
||||
m_type = DictList;
|
||||
m_list.insert ( index, dict );
|
||||
}
|
||||
|
||||
int QQtDictionary::count()
|
||||
{
|
||||
int cnt = -1;
|
||||
|
||||
if ( DictList == m_type )
|
||||
cnt = m_list.count();
|
||||
else if ( DictMap == m_type )
|
||||
cnt = m_map.count();
|
||||
else if ( DictValueList == m_type )
|
||||
cnt = m_valueList.count();
|
||||
else if ( DictValueMap == m_type )
|
||||
cnt = m_valueMap.count();
|
||||
else if ( DictValue == m_type )
|
||||
cnt = 1;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isNull()
|
||||
{
|
||||
if ( m_type == DictMax )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValid()
|
||||
{
|
||||
return isNull();
|
||||
}
|
||||
bool QQtDictionary::isEmpty()
|
||||
{
|
||||
bool isEmpty = true;
|
||||
|
||||
switch ( m_type )
|
||||
{
|
||||
case DictValue:
|
||||
if ( !m_value.isNull() )
|
||||
isEmpty = false;
|
||||
|
||||
break;
|
||||
|
||||
case DictValueList:
|
||||
if ( !m_valueList.isEmpty() )
|
||||
isEmpty = false;
|
||||
|
||||
break;
|
||||
|
||||
case DictValueMap:
|
||||
if ( !m_valueMap.isEmpty() )
|
||||
isEmpty = false;
|
||||
|
||||
break;
|
||||
|
||||
case DictList:
|
||||
if ( !m_list.isEmpty() )
|
||||
isEmpty = false;
|
||||
|
||||
break;
|
||||
|
||||
case DictMap:
|
||||
if ( !m_map.isEmpty() )
|
||||
isEmpty = false;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return isEmpty;
|
||||
}
|
||||
|
||||
|
||||
bool QQtDictionary::isList()
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( !m_type == DictList )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValueList()
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( !m_type == DictValueList )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isMap()
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( !m_type == DictMap )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValueMap()
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( !m_type == DictValueMap )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
QString& QQtDictionary::getName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
bool QQtDictionary::hasValue ( QString key )
|
||||
{
|
||||
bool has = false;
|
||||
|
||||
if ( m_type == DictValueMap )
|
||||
if ( m_valueMap.contains ( key ) )
|
||||
has = true;
|
||||
|
||||
return has;
|
||||
}
|
||||
|
||||
bool QQtDictionary::hasValue ( QVariant& value )
|
||||
{
|
||||
bool has = false;
|
||||
|
||||
if ( m_type == DictValueList )
|
||||
if ( m_valueList.contains ( value ) )
|
||||
has = true;
|
||||
|
||||
return has;
|
||||
}
|
||||
|
||||
bool QQtDictionary::hasChild ( QString& key )
|
||||
{
|
||||
bool has = false;
|
||||
|
||||
if ( m_type == DictMap )
|
||||
if ( m_map.contains ( key ) )
|
||||
has = true;
|
||||
|
||||
return has;
|
||||
}
|
||||
|
||||
bool QQtDictionary::hasChild ( QQtDictionary& value )
|
||||
{
|
||||
bool has = false;
|
||||
|
||||
if ( m_type == DictList )
|
||||
if ( m_list.contains ( value ) )
|
||||
has = true;
|
||||
|
||||
return has;
|
||||
}
|
||||
|
||||
void QQtDictionary::modValue ( QVariant& value )
|
||||
{
|
||||
if ( DictValue == m_type )
|
||||
{
|
||||
m_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::modValue ( int index, QVariant& value )
|
||||
{
|
||||
if ( DictValueList == m_type )
|
||||
{
|
||||
m_valueList[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::modValue ( QString key, QVariant& value )
|
||||
{
|
||||
if ( DictValueMap == m_type )
|
||||
{
|
||||
m_valueMap[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::modChild ( int index, QQtDictionary& value )
|
||||
{
|
||||
if ( DictList == m_type )
|
||||
{
|
||||
m_list[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::modChild ( QString key, QQtDictionary& value )
|
||||
{
|
||||
if ( DictMap == m_type )
|
||||
{
|
||||
m_map[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::clear()
|
||||
{
|
||||
if ( DictValue == m_type )
|
||||
{
|
||||
m_value.clear();
|
||||
}
|
||||
else if ( DictValueList == m_type )
|
||||
{
|
||||
m_valueList.clear();
|
||||
}
|
||||
else if ( DictValueMap == m_type )
|
||||
{
|
||||
m_valueMap.clear();
|
||||
}
|
||||
else if ( DictList == m_type )
|
||||
{
|
||||
m_list.clear();
|
||||
}
|
||||
else if ( DictMap == m_type )
|
||||
{
|
||||
m_map.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::remove ( int index )
|
||||
{
|
||||
if ( DictValueList == m_type )
|
||||
{
|
||||
m_valueList.removeAt ( index );
|
||||
}
|
||||
else if ( DictList == m_type )
|
||||
{
|
||||
m_list.removeAt ( index );
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::remove ( QString key )
|
||||
{
|
||||
if ( DictMap == m_type )
|
||||
{
|
||||
m_map.remove ( key );
|
||||
}
|
||||
else if ( DictValueMap == m_type )
|
||||
{
|
||||
m_valueMap.remove ( key );
|
||||
}
|
||||
}
|
||||
|
||||
QQtDictionary::QQtDictionary ( QQtDictionary& other, QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
EDictType type = other.getType();
|
||||
|
||||
switch ( type )
|
||||
{
|
||||
case DictValue:
|
||||
m_value = other.getValue() ;
|
||||
break;
|
||||
|
||||
case DictValueList:
|
||||
m_valueList = other.getValueList();
|
||||
break;
|
||||
|
||||
case DictValueMap:
|
||||
m_valueMap = other.getValueMap();
|
||||
|
||||
break;
|
||||
|
||||
case DictList:
|
||||
m_list = other.getList();
|
||||
|
||||
break;
|
||||
|
||||
case DictMap:
|
||||
m_map = other.getMap();
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
m_name = other.getName();
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QQtDictionary::QQtDictionary ( QString& name, QQtDictionary::EDictType type, QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
m_name = name;
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QQtDictionary::QQtDictionary ( QQtDictionary::EDictType type, QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator [] ( int index )
|
||||
{
|
||||
return ( QQtDictionary& ) m_list.operator [] ( index );
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator [] ( QString key )
|
||||
{
|
||||
return m_map.operator [] ( key );
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QMap<QString, QVariant>& map )
|
||||
{
|
||||
m_valueMap = map;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QMap<QString, QQtDictionary>& map )
|
||||
{
|
||||
m_map = map;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QList<QVariant>& list )
|
||||
{
|
||||
m_valueList = list;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QList<QQtDictionary>& list )
|
||||
{
|
||||
m_list = list;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QQtDictionary& other )
|
||||
{
|
||||
EDictType type = other.getType();
|
||||
|
||||
switch ( type )
|
||||
{
|
||||
case DictValue:
|
||||
m_value = other.getValue() ;
|
||||
break;
|
||||
|
||||
case DictValueList:
|
||||
m_valueList = other.getValueList();
|
||||
break;
|
||||
|
||||
case DictValueMap:
|
||||
m_valueMap = other.getValueMap();
|
||||
|
||||
break;
|
||||
|
||||
case DictList:
|
||||
m_list = other.getList();
|
||||
|
||||
break;
|
||||
|
||||
case DictMap:
|
||||
m_map = other.getMap();
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
m_name = other.getName();
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QMap<QString, QQtDictionary>& QQtDictionary::getMap()
|
||||
{
|
||||
return m_map;
|
||||
}
|
||||
|
||||
QList<QQtDictionary>& QQtDictionary::getList()
|
||||
{
|
||||
return m_list;
|
||||
}
|
||||
|
||||
QMap<QString, QVariant>& QQtDictionary::getValueMap()
|
||||
{
|
||||
return m_valueMap;
|
||||
}
|
||||
|
||||
QList<QVariant>& QQtDictionary::getValueList()
|
||||
{
|
||||
return m_valueList;
|
||||
}
|
||||
|
||||
QVariant& QQtDictionary::getValue()
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
QVariant& QQtDictionary::getValue ( int index )
|
||||
{
|
||||
return ( QVariant& ) m_valueList[index];
|
||||
}
|
||||
|
||||
QVariant& QQtDictionary::getValue ( QString key )
|
||||
{
|
||||
return m_valueMap[key];
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::getChild ( int index )
|
||||
{
|
||||
return m_list[key];
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::getChild ( QString key )
|
||||
{
|
||||
return m_map[key];
|
||||
}
|
||||
|
169
examples/qqthttpdownload/qqtdictionary.h
Normal file
169
examples/qqthttpdownload/qqtdictionary.h
Normal file
@ -0,0 +1,169 @@
|
||||
#ifndef QQTDICTIONARY_H
|
||||
#define QQTDICTIONARY_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <qqtcore.h>
|
||||
#include <qqt-local.h>
|
||||
|
||||
|
||||
class QQTSHARED_EXPORT QQtDictNode : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QQtDictNode ( QObject* parent = nullptr ) :
|
||||
QObject ( parent ) {
|
||||
m_list.clear();
|
||||
m_map.clear();
|
||||
}
|
||||
virtual ~QQtDictNode() {}
|
||||
private:
|
||||
/*是个list*/
|
||||
QList<QVariant> m_list;
|
||||
/*是个map*/
|
||||
QMap<QString, QVariant> m_map;
|
||||
};
|
||||
/**
|
||||
* @brief The QQtDictionary class
|
||||
* QQt 字典
|
||||
* 字典当中包含
|
||||
* 有序tuple 操作方式 dict[0] = ["","","",5] dict[max-1] = {""="",""="",""="",""=""}
|
||||
* 不支持无序tuple 操作方式 无
|
||||
* 包含具名映射 操作方式 dict["cccc"] = {""="", ""=""} dict["eeee"] = ["", "", ""]
|
||||
* 接受嵌套访问 操作方式 dict["cccc"][0]["eeeee"]
|
||||
* 通过重载函数来实现类型的变化,不建议使用中更改类型。
|
||||
* 比json和xml的数据结构要庞大。toJson toXML fromJson fromXML
|
||||
* QVariant 不能直接获取到真实数据,改变必须使用临时变量。
|
||||
* 而且,接口设计也不够灵活,存入和取出都不太方便。
|
||||
*/
|
||||
class QQTSHARED_EXPORT QQtDictionary : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS ( EDictType )
|
||||
|
||||
public:
|
||||
typedef enum tagDictType
|
||||
{
|
||||
DictValue,
|
||||
DictValueList,
|
||||
DictValueMap,
|
||||
DictList,
|
||||
DictMap,
|
||||
DictMax
|
||||
} EDictType;
|
||||
|
||||
explicit QQtDictionary ( QObject* parent = 0 );
|
||||
virtual ~QQtDictionary() {}
|
||||
|
||||
bool isNull();
|
||||
bool isValid();
|
||||
bool isEmpty();
|
||||
|
||||
bool isValue( );
|
||||
bool isList();
|
||||
bool isValueList();
|
||||
bool isMap();
|
||||
bool isValueMap();
|
||||
|
||||
/*获取数据*/
|
||||
QString& getName();
|
||||
/*获取全部数据*/
|
||||
QMap<QString, QQtDictionary>& getMap();
|
||||
QList<QQtDictionary>& getList();
|
||||
QMap<QString, QVariant>& getValueMap();
|
||||
QList<QVariant>& getValueList();
|
||||
/*获取单个数据*/
|
||||
QVariant& getValue();
|
||||
QVariant& getValue ( int index );
|
||||
QVariant& getValue ( QString key );
|
||||
/*获取一个个孩子*/
|
||||
/*list item*/
|
||||
QQtDictionary& getChild ( int index );
|
||||
/*map item*/
|
||||
QQtDictionary& getChild ( QString key );
|
||||
|
||||
/*类型*/
|
||||
EDictType getType();
|
||||
/*如果设置Value的时候改变了Type,将会以新的Type为准*/
|
||||
void setType ( EDictType type );
|
||||
|
||||
/*插入数据,自动设置type*/
|
||||
/*自己本身没有孩子,是个叶子,添加值*/
|
||||
void setValue ( QVariant& value );
|
||||
/*whole value list*/
|
||||
void setValue ( QList<QVariant>& list );
|
||||
/*whole value map*/
|
||||
void setValue ( QMap<QString, QVariant>& map );
|
||||
/*自己本身没有孩子,添加一个个的孩子*/
|
||||
void addValue ( QVariant& value );
|
||||
void insertValue ( QString key, QVariant& value );
|
||||
void insertValue ( int index, QVariant& value );
|
||||
/*自己本身有孩子,添加全部孩子*/
|
||||
/*list*/
|
||||
void setChild ( QList<QQtDictionary>& list );
|
||||
/*map*/
|
||||
void setChild ( QMap<QString, QQtDictionary>& map );
|
||||
/*自己本身有孩子,添加一个个的孩子*/
|
||||
void addChild ( QQtDictionary& dict );
|
||||
void insertChild ( QString key, QQtDictionary& dict );
|
||||
void addChild ( int index, QQtDictionary& dict );
|
||||
|
||||
/*遍历字典*/
|
||||
int count();
|
||||
bool hasValue ( QString key );
|
||||
bool hasValue ( QVariant& value );
|
||||
/*这个说的就是map和valuemap了*/
|
||||
bool hasChild ( QString& key );
|
||||
bool hasChild ( QQtDictionary& value );
|
||||
|
||||
/*操作数据,改变数据*/
|
||||
void modValue ( QVariant& value );
|
||||
void modValue ( int index, QVariant& value );
|
||||
void modValue ( QString key, QVariant& value );
|
||||
void modChild ( int index, QQtDictionary& value );
|
||||
void modChild ( QString key, QQtDictionary& value );
|
||||
|
||||
/*删除数据*/
|
||||
void clear ( );
|
||||
void remove ( int index );
|
||||
void remove ( QString key );
|
||||
|
||||
/*深拷贝*/
|
||||
explicit QQtDictionary ( QQtDictionary& other, QObject* parent = 0 );
|
||||
explicit QQtDictionary ( QString& name, EDictType type = DictMap, QObject* parent = 0 );
|
||||
explicit QQtDictionary ( EDictType type, QObject* parent = 0 );
|
||||
|
||||
/*操作符*/
|
||||
QQtDictionary& operator [] ( int index );
|
||||
QQtDictionary& operator [] ( QString key );
|
||||
QQtDictionary& operator = ( QMap<QString, QVariant>& map );
|
||||
QQtDictionary& operator = ( QMap<QString, QQtDictionary>& map );
|
||||
QQtDictionary& operator = ( QList<QVariant>& list );
|
||||
QQtDictionary& operator = ( QList<QQtDictionary>& list );
|
||||
QQtDictionary& operator = ( QQtDictionary& other );
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
/*节点类型*/
|
||||
EDictType m_type;
|
||||
/*节点名字*/
|
||||
QString m_name;
|
||||
|
||||
/*节点的可能内容枚举*/
|
||||
/*叶子:是个数据*/
|
||||
QVariant m_value;
|
||||
/*叶子:是个值列表*/
|
||||
QList<QVariant> m_valueList; //[index]
|
||||
/*叶子:是个值字典*/
|
||||
QMap<QString, QVariant> m_valueMap;
|
||||
/*不是叶子,是个列表*/
|
||||
QList<QQtDictionary> m_list; //[index]
|
||||
/*不是叶子,是个子字典*/
|
||||
QMap<QString, QQtDictionary> m_map;
|
||||
/*是个列表和子字典,这是错误的,不可能的*/
|
||||
};
|
||||
|
||||
#endif // QQTDICTIONARY_H
|
@ -27,12 +27,14 @@ SOURCES += \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
qqthttpdownloadmanager.cpp \
|
||||
qqtqtiowebpageparser.cpp
|
||||
qqtqtiowebpageparser.cpp \
|
||||
qqtdictionary.cpp
|
||||
|
||||
HEADERS += \
|
||||
mainwindow.h \
|
||||
qqthttpdownloadmanager.h \
|
||||
qqtqtiowebpageparser.h
|
||||
qqtqtiowebpageparser.h \
|
||||
qqtdictionary.h
|
||||
|
||||
FORMS += \
|
||||
mainwindow.ui
|
||||
|
Loading…
x
Reference in New Issue
Block a user