mirror of
https://gitee.com/drabel/LibQQt.git
synced 2025-01-04 10:18:44 +08:00
update qqt dict
This commit is contained in:
parent
f08268437c
commit
64da436354
3
QQt.pro
3
QQt.pro
@ -1,7 +1,8 @@
|
||||
TEMPLATE = subdirs
|
||||
CONFIG += ordered
|
||||
|
||||
SUBDIRS = src/qqt.pro
|
||||
SUBDIRS = src/qqt.pro \
|
||||
test/qqtdicttest
|
||||
|
||||
##-----------------------------------------------------------------
|
||||
##basic example
|
||||
|
@ -29,7 +29,50 @@ int main ( int argc, char* argv[] )
|
||||
//MainWindow w;
|
||||
//w.show();
|
||||
|
||||
QQtDictionary dict;
|
||||
QQtDict d0;
|
||||
d0["cc"] = "dd";
|
||||
qDebug() << d0["cc"].getValue();
|
||||
|
||||
return a.exec();
|
||||
QQtDict d1;
|
||||
QQtDict d2 ( QVariant ( "CCCC" ) );
|
||||
d1.appendValue ( d2 );
|
||||
qDebug() << d1[0].getValue();
|
||||
d1[0] = "ff";
|
||||
qDebug() << d1[0].getValue();
|
||||
|
||||
QQtDict d3;
|
||||
d3["cc"]["dd"] = "ee";
|
||||
qDebug() << d3["cc"]["dd"].getValue().toString();
|
||||
|
||||
QQtDict d4;
|
||||
|
||||
for ( int i = 0; i < 5; i++ )
|
||||
{
|
||||
QQtDict d ( QVariant ( QString::number ( i ) ) );
|
||||
d4.appendValue ( d );
|
||||
}
|
||||
|
||||
qDebug() << d4.count();
|
||||
|
||||
for ( int i = 0; i < d4.count(); i++ )
|
||||
{
|
||||
qDebug() << d4[i].getValue().toString();
|
||||
}
|
||||
|
||||
QQtDict d5;
|
||||
d5.appendChild ( "5.7" );
|
||||
d5.appendChild ( "5.8" );
|
||||
d5.appendChild ( "5.9" );
|
||||
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"];
|
||||
|
||||
return 0;//a.exec();
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
#include "qqtdictionary.h"
|
||||
|
||||
QQtDictionary::QQtDictionary ( QObject* parent ) :
|
||||
QQtDict::QQtDict ( QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
m_type = DictMax;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValue()
|
||||
bool QQtDict::isValue() const
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
@ -16,80 +16,89 @@ bool QQtDictionary::isValue()
|
||||
return is;
|
||||
}
|
||||
|
||||
QQtDictionary::EDictType QQtDictionary::getType() { return m_type; }
|
||||
QQtDict::EDictType QQtDict::getType() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
void QQtDictionary::setType ( QQtDictionary::EDictType type )
|
||||
void QQtDict::setType ( QQtDict::EDictType type )
|
||||
{
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
void QQtDictionary::setValue ( QVariant& value )
|
||||
void QQtDict::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 )
|
||||
void QQtDict::setValue ( QList<QQtDict>& list )
|
||||
{
|
||||
m_type = DictList;
|
||||
m_list = list;
|
||||
}
|
||||
|
||||
void QQtDictionary::setChild ( QMap<QString, QQtDictionary>& map )
|
||||
void QQtDict::setValue ( QMap<QString, QQtDict>& map )
|
||||
{
|
||||
m_type = DictMap;
|
||||
m_map = map;
|
||||
}
|
||||
|
||||
void QQtDictionary::addChild ( QQtDictionary& dict )
|
||||
void QQtDict::setChild ( QList<QQtDict>& list )
|
||||
{
|
||||
setValue ( list );
|
||||
}
|
||||
|
||||
void QQtDict::setChild ( QMap<QString, QQtDict>& 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 )
|
||||
{
|
||||
m_type = DictList;
|
||||
m_list.append ( dict );
|
||||
}
|
||||
|
||||
void QQtDictionary::insertChild ( QString key, QQtDictionary& dict )
|
||||
void QQtDict::appendChild ( const QQtDict& dict )
|
||||
{
|
||||
appendValue ( dict );
|
||||
}
|
||||
|
||||
void QQtDict::insertValue ( const QString& key, QQtDict& dict )
|
||||
{
|
||||
m_type = DictMap;
|
||||
m_map.insert ( key, dict );
|
||||
}
|
||||
|
||||
void QQtDictionary::addChild ( int index, QQtDictionary& dict )
|
||||
void QQtDict::insertChild ( const QString& key, QQtDict& dict )
|
||||
{
|
||||
insertValue ( key, dict );
|
||||
}
|
||||
|
||||
void QQtDict::insertValue ( int index, QQtDict& dict )
|
||||
{
|
||||
m_type = DictList;
|
||||
m_list.insert ( index, dict );
|
||||
}
|
||||
|
||||
int QQtDictionary::count()
|
||||
void QQtDict::appendChild ( const QString& value )
|
||||
{
|
||||
appendValue ( value );
|
||||
}
|
||||
|
||||
void QQtDict::insertChild ( int index, QQtDict& dict )
|
||||
{
|
||||
insertValue ( index, dict );
|
||||
}
|
||||
|
||||
int QQtDict::count() const
|
||||
{
|
||||
int cnt = -1;
|
||||
|
||||
@ -97,17 +106,13 @@ int QQtDictionary::count()
|
||||
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()
|
||||
bool QQtDict::isNull() const
|
||||
{
|
||||
if ( m_type == DictMax )
|
||||
return true;
|
||||
@ -115,11 +120,11 @@ bool QQtDictionary::isNull()
|
||||
return false;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValid()
|
||||
bool QQtDict::isValid() const
|
||||
{
|
||||
return isNull();
|
||||
}
|
||||
bool QQtDictionary::isEmpty()
|
||||
bool QQtDict::isEmpty() const
|
||||
{
|
||||
bool isEmpty = true;
|
||||
|
||||
@ -131,18 +136,6 @@ bool QQtDictionary::isEmpty()
|
||||
|
||||
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;
|
||||
@ -163,7 +156,7 @@ bool QQtDictionary::isEmpty()
|
||||
}
|
||||
|
||||
|
||||
bool QQtDictionary::isList()
|
||||
bool QQtDict::isList() const
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
@ -173,17 +166,7 @@ bool QQtDictionary::isList()
|
||||
return is;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValueList()
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( !m_type == DictValueList )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
bool QQtDictionary::isMap()
|
||||
bool QQtDict::isMap() const
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
@ -194,44 +177,12 @@ bool QQtDictionary::isMap()
|
||||
|
||||
}
|
||||
|
||||
bool QQtDictionary::isValueMap()
|
||||
QString& QQtDict::getName() const
|
||||
{
|
||||
bool is = false;
|
||||
|
||||
if ( !m_type == DictValueMap )
|
||||
is = true;
|
||||
|
||||
return is;
|
||||
return ( QString& ) m_name;
|
||||
}
|
||||
|
||||
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 QQtDict::hasKey ( const QString& key ) const
|
||||
{
|
||||
bool has = false;
|
||||
|
||||
@ -242,7 +193,7 @@ bool QQtDictionary::hasChild ( QString& key )
|
||||
return has;
|
||||
}
|
||||
|
||||
bool QQtDictionary::hasChild ( QQtDictionary& value )
|
||||
bool QQtDict::hasKey ( const QQtDict& value ) const
|
||||
{
|
||||
bool has = false;
|
||||
|
||||
@ -253,7 +204,17 @@ bool QQtDictionary::hasChild ( QQtDictionary& value )
|
||||
return has;
|
||||
}
|
||||
|
||||
void QQtDictionary::modValue ( QVariant& value )
|
||||
bool QQtDict::hasChild ( const QString& key ) const
|
||||
{
|
||||
return hasKey ( key );
|
||||
}
|
||||
|
||||
bool QQtDict::hasChild ( const QQtDict& value ) const
|
||||
{
|
||||
return hasKey ( value );
|
||||
}
|
||||
|
||||
void QQtDict::modValue ( QVariant& value )
|
||||
{
|
||||
if ( DictValue == m_type )
|
||||
{
|
||||
@ -261,31 +222,15 @@ void QQtDictionary::modValue ( QVariant& 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 )
|
||||
void QQtDict::modValue ( int index, QQtDict& value )
|
||||
{
|
||||
if ( DictList == m_type )
|
||||
{
|
||||
m_list[key] = value;
|
||||
m_list[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::modChild ( QString key, QQtDictionary& value )
|
||||
void QQtDict::modValue ( QString key, QQtDict& value )
|
||||
{
|
||||
if ( DictMap == m_type )
|
||||
{
|
||||
@ -293,20 +238,22 @@ void QQtDictionary::modChild ( QString key, QQtDictionary& value )
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::clear()
|
||||
void QQtDict::modChild ( int index, QQtDict& value )
|
||||
{
|
||||
modValue ( index, value );
|
||||
}
|
||||
|
||||
void QQtDict::modChild ( QString key, QQtDict& value )
|
||||
{
|
||||
modValue ( key, value );
|
||||
}
|
||||
|
||||
void QQtDict::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();
|
||||
@ -317,116 +264,71 @@ void QQtDictionary::clear()
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::remove ( int index )
|
||||
void QQtDict::remove ( int index )
|
||||
{
|
||||
if ( DictValueList == m_type )
|
||||
{
|
||||
m_valueList.removeAt ( index );
|
||||
}
|
||||
else if ( DictList == m_type )
|
||||
if ( DictList == m_type )
|
||||
{
|
||||
m_list.removeAt ( index );
|
||||
}
|
||||
}
|
||||
|
||||
void QQtDictionary::remove ( QString key )
|
||||
void QQtDict::remove ( const 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 ) :
|
||||
QQtDict::QQtDict ( const QQtDict& 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;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
QQtDictionary::QQtDictionary ( QString& name, QQtDictionary::EDictType type, QObject* parent ) :
|
||||
QQtDict::QQtDict ( const QString name, QQtDict::EDictType type, QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
m_name = name;
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QQtDictionary::QQtDictionary ( QQtDictionary::EDictType type, QObject* parent ) :
|
||||
QQtDict::QQtDict ( const QQtDict::EDictType type, QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator [] ( int index )
|
||||
QQtDict::QQtDict ( const QVariant& value, QObject* parent ) :
|
||||
QObject ( parent )
|
||||
{
|
||||
return ( QQtDictionary& ) m_list.operator [] ( index );
|
||||
m_value = value;
|
||||
m_type = DictValue;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator [] ( QString key )
|
||||
QQtDict& QQtDict::operator [] ( int index )
|
||||
{
|
||||
return ( QQtDict& ) m_list.operator [] ( index );
|
||||
}
|
||||
|
||||
QQtDict& QQtDict::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 )
|
||||
QQtDict& QQtDict::operator = ( const QMap<QString, QQtDict>& map )
|
||||
{
|
||||
m_map = map;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QList<QVariant>& list )
|
||||
{
|
||||
m_valueList = list;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QList<QQtDictionary>& list )
|
||||
QQtDict& QQtDict::operator = ( const QList<QQtDict>& list )
|
||||
{
|
||||
m_list = list;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::operator = ( QQtDictionary& other )
|
||||
QQtDict& QQtDict::operator = ( const QQtDict& other )
|
||||
{
|
||||
EDictType type = other.getType();
|
||||
|
||||
@ -436,15 +338,6 @@ QQtDictionary& QQtDictionary::operator = ( QQtDictionary& other )
|
||||
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();
|
||||
|
||||
@ -461,50 +354,72 @@ QQtDictionary& QQtDictionary::operator = ( QQtDictionary& other )
|
||||
|
||||
m_name = other.getName();
|
||||
m_type = type;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QMap<QString, QQtDictionary>& QQtDictionary::getMap()
|
||||
QQtDict& QQtDict::operator = ( const QVariant& value )
|
||||
{
|
||||
return m_map;
|
||||
m_value = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
QList<QQtDictionary>& QQtDictionary::getList()
|
||||
bool QQtDict::operator == ( const QQtDict& other ) const
|
||||
{
|
||||
return m_list;
|
||||
if ( m_type == other.getType() &&
|
||||
other.getName() == m_name &&
|
||||
other.getList() == m_list &&
|
||||
other.getMap() == m_map &&
|
||||
other.getValue() == m_value )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QMap<QString, QVariant>& QQtDictionary::getValueMap()
|
||||
QMap<QString, QQtDict>& QQtDict::getMap() const
|
||||
{
|
||||
return m_valueMap;
|
||||
return ( QMap<QString, QQtDict>& ) m_map;
|
||||
}
|
||||
|
||||
QList<QVariant>& QQtDictionary::getValueList()
|
||||
QList<QQtDict>& QQtDict::getList() const
|
||||
{
|
||||
return m_valueList;
|
||||
return ( QList<QQtDict>& ) m_list;
|
||||
}
|
||||
|
||||
QVariant& QQtDictionary::getValue()
|
||||
QVariant& QQtDict::getValue() const
|
||||
{
|
||||
return m_value;
|
||||
return ( QVariant& ) m_value;
|
||||
}
|
||||
|
||||
QVariant& QQtDictionary::getValue ( int index )
|
||||
QQtDict& QQtDict::getValue ( int index ) const
|
||||
{
|
||||
return ( QVariant& ) m_valueList[index];
|
||||
return ( QQtDict& ) m_list[index];
|
||||
}
|
||||
|
||||
QVariant& QQtDictionary::getValue ( QString key )
|
||||
{
|
||||
return m_valueMap[key];
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::getChild ( int index )
|
||||
{
|
||||
return m_list[key];
|
||||
}
|
||||
|
||||
QQtDictionary& QQtDictionary::getChild ( QString key )
|
||||
QQtDict& QQtDict::getValue ( const QString& key )
|
||||
{
|
||||
return m_map[key];
|
||||
}
|
||||
|
||||
QQtDict& QQtDict::getChild ( int index )
|
||||
{
|
||||
return m_list[index];
|
||||
}
|
||||
|
||||
QQtDict& QQtDict::getChild ( QString key )
|
||||
{
|
||||
return m_map[key];
|
||||
}
|
||||
|
||||
|
||||
QDebug operator<< ( QDebug dbg, const QQtDict& 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}";
|
||||
return dbg.space();
|
||||
}
|
||||
|
@ -7,23 +7,6 @@
|
||||
#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 字典
|
||||
@ -37,7 +20,7 @@ private:
|
||||
* QVariant 不能直接获取到真实数据,改变必须使用临时变量。
|
||||
* 而且,接口设计也不够灵活,存入和取出都不太方便。
|
||||
*/
|
||||
class QQTSHARED_EXPORT QQtDictionary : public QObject
|
||||
class QQTSHARED_EXPORT QQtDict : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS ( EDictType )
|
||||
@ -45,103 +28,115 @@ class QQTSHARED_EXPORT QQtDictionary : public QObject
|
||||
public:
|
||||
typedef enum tagDictType
|
||||
{
|
||||
/*只有一个值*/
|
||||
DictValue,
|
||||
DictValueList,
|
||||
DictValueMap,
|
||||
/*是个列表,List不为空,name根据用户设置或许为空*/
|
||||
DictList,
|
||||
/*是个映射,可能嵌套映射,Map不空,name根据用户设置或许为空*/
|
||||
DictMap,
|
||||
DictMax
|
||||
} EDictType;
|
||||
|
||||
explicit QQtDictionary ( QObject* parent = 0 );
|
||||
virtual ~QQtDictionary() {}
|
||||
explicit QQtDict ( QObject* parent = 0 );
|
||||
virtual ~QQtDict() {}
|
||||
|
||||
bool isNull();
|
||||
bool isValid();
|
||||
bool isEmpty();
|
||||
bool isNull() const;
|
||||
bool isValid() const;
|
||||
bool isEmpty() const;
|
||||
|
||||
bool isValue( );
|
||||
bool isList();
|
||||
bool isValueList();
|
||||
bool isMap();
|
||||
bool isValueMap();
|
||||
bool isValue( ) const;
|
||||
bool isList() const;
|
||||
bool isMap() const;
|
||||
|
||||
/*获取数据*/
|
||||
QString& getName();
|
||||
QString& getName() const;
|
||||
/*获取全部数据*/
|
||||
QMap<QString, QQtDictionary>& getMap();
|
||||
QList<QQtDictionary>& getList();
|
||||
QMap<QString, QVariant>& getValueMap();
|
||||
QList<QVariant>& getValueList();
|
||||
QMap<QString, QQtDict>& getMap() const;
|
||||
QList<QQtDict>& getList() const ;
|
||||
|
||||
/*获取单个数据*/
|
||||
QVariant& getValue();
|
||||
QVariant& getValue ( int index );
|
||||
QVariant& getValue ( QString key );
|
||||
QVariant& getValue() const;
|
||||
QQtDict& getValue ( int index ) const;
|
||||
QQtDict& getValue ( const QString& key );
|
||||
/*获取一个个孩子*/
|
||||
/*list item*/
|
||||
QQtDictionary& getChild ( int index );
|
||||
QQtDict& getChild ( int index );
|
||||
/*map item*/
|
||||
QQtDictionary& getChild ( QString key );
|
||||
QQtDict& getChild ( QString key );
|
||||
|
||||
/*类型*/
|
||||
EDictType getType();
|
||||
EDictType getType() const;
|
||||
/*如果设置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 );
|
||||
/*自己本身有孩子,添加全部孩子*/
|
||||
/*whole value list*/
|
||||
void setValue ( QList<QQtDict>& list );
|
||||
/*whole value map*/
|
||||
void setValue ( QMap<QString, QQtDict>& map );
|
||||
/*list*/
|
||||
void setChild ( QList<QQtDictionary>& list );
|
||||
void setChild ( QList<QQtDict>& list );
|
||||
/*map*/
|
||||
void setChild ( QMap<QString, QQtDictionary>& map );
|
||||
void setChild ( QMap<QString, QQtDict>& map );
|
||||
|
||||
/*自己本身没有孩子,添加一个个的孩子*/
|
||||
void appendValue ( const QString& value );
|
||||
void appendValue ( const QQtDict& dict );
|
||||
void appendChild ( const QString& value );
|
||||
void appendChild ( const QQtDict& dict );
|
||||
/*自己本身有孩子,添加一个个的孩子*/
|
||||
void addChild ( QQtDictionary& dict );
|
||||
void insertChild ( QString key, QQtDictionary& dict );
|
||||
void addChild ( int index, QQtDictionary& 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();
|
||||
bool hasValue ( QString key );
|
||||
bool hasValue ( QVariant& value );
|
||||
/*这个说的就是map和valuemap了*/
|
||||
bool hasChild ( QString& key );
|
||||
bool hasChild ( QQtDictionary& value );
|
||||
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 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 modValue ( int index, QQtDict& value );
|
||||
void modValue ( QString key, QQtDict& value );
|
||||
void modChild ( int index, QQtDict& value );
|
||||
void modChild ( QString key, QQtDict& value );
|
||||
|
||||
/*删除数据*/
|
||||
void clear ( );
|
||||
void remove ( int index );
|
||||
void remove ( QString key );
|
||||
void remove ( const 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 );
|
||||
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 );
|
||||
|
||||
/*操作符*/
|
||||
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 );
|
||||
/*don't out of range*/
|
||||
QQtDict& operator [] ( int index );
|
||||
/**/
|
||||
QQtDict& operator [] ( QString key );
|
||||
QQtDict& operator = ( const QMap<QString, QQtDict>& map );
|
||||
QQtDict& operator = ( const QList<QQtDict>& list );
|
||||
QQtDict& operator = ( const QQtDict& other );
|
||||
QQtDict& operator = ( const QVariant& value );
|
||||
bool operator == ( const QQtDict& other ) const;
|
||||
|
||||
/*与其他数据结构兼容*/
|
||||
QString toXML();
|
||||
QString toJson();
|
||||
void fromJson ( QString& jsonContent );
|
||||
void fromXML ( QString& xmlContent );
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
@ -155,15 +150,13 @@ private:
|
||||
/*节点的可能内容枚举*/
|
||||
/*叶子:是个数据*/
|
||||
QVariant m_value;
|
||||
/*叶子:是个值列表*/
|
||||
QList<QVariant> m_valueList; //[index]
|
||||
/*叶子:是个值字典*/
|
||||
QMap<QString, QVariant> m_valueMap;
|
||||
/*不是叶子,是个列表*/
|
||||
QList<QQtDictionary> m_list; //[index]
|
||||
/*不是叶子,是个子字典*/
|
||||
QMap<QString, QQtDictionary> m_map;
|
||||
/*不是叶子列表,是个叶子列表,是个叶子列表的值*/
|
||||
QList<QQtDict> m_list; //[index]
|
||||
/*不是叶子映射,是个子字典,是个叶子,是个叶子的值组合*/
|
||||
QMap<QString, QQtDict> m_map;
|
||||
/*是个列表和子字典,这是错误的,不可能的*/
|
||||
};
|
||||
|
||||
QDebug operator<< ( QDebug dbg, const QQtDict& d );
|
||||
|
||||
#endif // QQTDICTIONARY_H
|
||||
|
37
test/qqtdicttest/qqtdicttest.pro
Normal file
37
test/qqtdicttest/qqtdicttest.pro
Normal file
@ -0,0 +1,37 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2017-11-24T21:13:26
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += widgets network xml testlib
|
||||
|
||||
TARGET = tst_qqtdicttesttest
|
||||
CONFIG -= console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any feature of Qt which has been marked as deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if you use deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
|
||||
SOURCES += \
|
||||
tst_qqtdicttesttest.cpp
|
||||
|
||||
SOURCES += $$PWD/../../examples/qqthttpdownload/qqtdictionary.cpp
|
||||
HEADERS += $$PWD/../../examples/qqthttpdownload/qqtdictionary.h
|
||||
INCLUDEPATH += $$PWD/../../examples/qqthttpdownload
|
||||
|
||||
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
||||
|
||||
|
||||
include (../../examples/qqtframe2/link_qqt_library.pri)
|
69
test/qqtdicttest/tst_qqtdicttesttest.cpp
Normal file
69
test/qqtdicttest/tst_qqtdicttesttest.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include <QString>
|
||||
#include <QtTest>
|
||||
#include <QCoreApplication>
|
||||
#include "qqtdictionary.h"
|
||||
|
||||
class QqtdicttestTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QqtdicttestTest();
|
||||
|
||||
private Q_SLOTS:
|
||||
void initTestCase();
|
||||
void cleanupTestCase();
|
||||
void testCase1_data();
|
||||
void testCase1();
|
||||
void testCase2_data();
|
||||
void testCase2();
|
||||
|
||||
private:
|
||||
QQtDict dict;
|
||||
};
|
||||
|
||||
QqtdicttestTest::QqtdicttestTest()
|
||||
{
|
||||
}
|
||||
|
||||
void QqtdicttestTest::initTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void QqtdicttestTest::cleanupTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void QqtdicttestTest::testCase1_data()
|
||||
{
|
||||
QTest::addColumn<QString> ( "data" );
|
||||
QTest::addColumn<QString> ( "result" );
|
||||
QTest::newRow ( "0" ) << "cc" << "dd";
|
||||
QTest::newRow ( "1" ) << "dd" << "ee";
|
||||
QTest::newRow ( "2" ) << "gg" << "ff";
|
||||
}
|
||||
|
||||
void QqtdicttestTest::testCase1()
|
||||
{
|
||||
QFETCH ( QString, data );
|
||||
QFETCH ( QString, result );
|
||||
dict[data] = result;
|
||||
QVERIFY2 ( dict[data].getValue() == result, "Failure" );
|
||||
}
|
||||
|
||||
void QqtdicttestTest::testCase2_data()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void QqtdicttestTest::testCase2()
|
||||
{
|
||||
QFETCH ( QString, data );
|
||||
QFETCH ( QString, result );
|
||||
dict[0] = result;
|
||||
QVERIFY2 ( dict[0].getValue() == result, "Failure" );
|
||||
}
|
||||
|
||||
QTEST_MAIN ( QqtdicttestTest )
|
||||
|
||||
#include "tst_qqtdicttesttest.moc"
|
Loading…
x
Reference in New Issue
Block a user