2017-11-24 19:43:36 +08:00
|
|
|
#include "qqtdictionary.h"
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict::QQtDict ( QObject* parent ) :
|
2017-11-24 19:43:36 +08:00
|
|
|
QObject ( parent )
|
|
|
|
{
|
|
|
|
m_type = DictMax;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::isValue() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
bool is = false;
|
|
|
|
|
|
|
|
if ( m_type == DictValue )
|
|
|
|
is = true;
|
|
|
|
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict::EDictType QQtDict::getType() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
2017-11-24 19:43:36 +08:00
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::setType ( QQtDict::EDictType type )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_type = type;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::setValue ( QVariant& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_type = DictValue;
|
|
|
|
m_value = value;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::setValue ( QList<QQtDict>& list )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_type = DictList;
|
|
|
|
m_list = list;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::setValue ( QMap<QString, QQtDict>& map )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_type = DictMap;
|
|
|
|
m_map = map;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::setChild ( QList<QQtDict>& list )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
setValue ( list );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::setChild ( QMap<QString, QQtDict>& map )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
setValue ( map );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::appendValue ( const QString& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_type = DictList;
|
|
|
|
m_list.push_back ( QQtDict ( QVariant ( value ) ) );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::appendValue ( const QQtDict& dict )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_type = DictList;
|
2017-11-24 22:43:12 +08:00
|
|
|
m_list.append ( dict );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::appendChild ( const QQtDict& dict )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
appendValue ( dict );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::insertValue ( const QString& key, QQtDict& dict )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_type = DictMap;
|
|
|
|
m_map.insert ( key, dict );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::insertChild ( const QString& key, QQtDict& dict )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
insertValue ( key, dict );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::insertValue ( int index, QQtDict& dict )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_type = DictList;
|
|
|
|
m_list.insert ( index, dict );
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::appendChild ( const QString& value )
|
|
|
|
{
|
|
|
|
appendValue ( value );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQtDict::insertChild ( int index, QQtDict& dict )
|
|
|
|
{
|
|
|
|
insertValue ( index, dict );
|
|
|
|
}
|
|
|
|
|
|
|
|
int QQtDict::count() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
int cnt = -1;
|
|
|
|
|
|
|
|
if ( DictList == m_type )
|
|
|
|
cnt = m_list.count();
|
|
|
|
else if ( DictMap == m_type )
|
|
|
|
cnt = m_map.count();
|
|
|
|
else if ( DictValue == m_type )
|
|
|
|
cnt = 1;
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::isNull() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
if ( m_type == DictMax )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::isValid() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
return isNull();
|
|
|
|
}
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::isEmpty() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
bool isEmpty = true;
|
|
|
|
|
|
|
|
switch ( m_type )
|
|
|
|
{
|
|
|
|
case DictValue:
|
|
|
|
if ( !m_value.isNull() )
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::isList() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
bool is = false;
|
|
|
|
|
|
|
|
if ( !m_type == DictList )
|
|
|
|
is = true;
|
|
|
|
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::isMap() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
bool is = false;
|
|
|
|
|
|
|
|
if ( !m_type == DictMap )
|
|
|
|
is = true;
|
|
|
|
|
|
|
|
return is;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QString& QQtDict::getName() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return ( QString& ) m_name;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::hasKey ( const QString& key ) const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
bool has = false;
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
if ( m_type == DictMap )
|
|
|
|
if ( m_map.contains ( key ) )
|
2017-11-24 19:43:36 +08:00
|
|
|
has = true;
|
|
|
|
|
|
|
|
return has;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::hasKey ( const QQtDict& value ) const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
bool has = false;
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
if ( m_type == DictList )
|
|
|
|
if ( m_list.contains ( value ) )
|
2017-11-24 19:43:36 +08:00
|
|
|
has = true;
|
|
|
|
|
|
|
|
return has;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::hasChild ( const QString& key ) const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return hasKey ( key );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::hasChild ( const QQtDict& value ) const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return hasKey ( value );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::modValue ( QVariant& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
if ( DictValue == m_type )
|
|
|
|
{
|
|
|
|
m_value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::modValue ( int index, QQtDict& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
if ( DictList == m_type )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_list[index] = value;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::modValue ( QString key, QQtDict& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
if ( DictMap == m_type )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_map[key] = value;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::modChild ( int index, QQtDict& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
modValue ( index, value );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::modChild ( QString key, QQtDict& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
modValue ( key, value );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::clear()
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
if ( DictValue == m_type )
|
|
|
|
{
|
|
|
|
m_value.clear();
|
|
|
|
}
|
|
|
|
else if ( DictList == m_type )
|
|
|
|
{
|
|
|
|
m_list.clear();
|
|
|
|
}
|
|
|
|
else if ( DictMap == m_type )
|
|
|
|
{
|
|
|
|
m_map.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::remove ( int index )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
if ( DictList == m_type )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_list.removeAt ( index );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
void QQtDict::remove ( const QString& key )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
if ( DictMap == m_type )
|
|
|
|
{
|
|
|
|
m_map.remove ( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict::QQtDict ( const QQtDict& other, QObject* parent ) :
|
2017-11-24 19:43:36 +08:00
|
|
|
QObject ( parent )
|
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
*this = other;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict::QQtDict ( const QString name, QQtDict::EDictType type, QObject* parent ) :
|
2017-11-24 19:43:36 +08:00
|
|
|
QObject ( parent )
|
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
m_type = type;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict::QQtDict ( const QQtDict::EDictType type, QObject* parent ) :
|
2017-11-24 19:43:36 +08:00
|
|
|
QObject ( parent )
|
|
|
|
{
|
|
|
|
m_type = type;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict::QQtDict ( const QVariant& value, QObject* parent ) :
|
|
|
|
QObject ( parent )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_value = value;
|
|
|
|
m_type = DictValue;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::operator [] ( int index )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return ( QQtDict& ) m_list.operator [] ( index );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::operator [] ( QString key )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return m_map.operator [] ( key );
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::operator = ( const QMap<QString, QQtDict>& map )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_map = map;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::operator = ( const QList<QQtDict>& list )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
m_list = list;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::operator = ( const QQtDict& other )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
EDictType type = other.getType();
|
|
|
|
|
|
|
|
switch ( type )
|
|
|
|
{
|
|
|
|
case DictValue:
|
|
|
|
m_value = other.getValue() ;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DictList:
|
|
|
|
m_list = other.getList();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DictMap:
|
|
|
|
m_map = other.getMap();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_name = other.getName();
|
|
|
|
m_type = type;
|
2017-11-24 22:43:12 +08:00
|
|
|
return *this;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::operator = ( const QVariant& value )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
m_value = value;
|
|
|
|
return *this;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
bool QQtDict::operator == ( const QQtDict& other ) const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
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;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QMap<QString, QQtDict>& QQtDict::getMap() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return ( QMap<QString, QQtDict>& ) m_map;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QList<QQtDict>& QQtDict::getList() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return ( QList<QQtDict>& ) m_list;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QVariant& QQtDict::getValue() const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return ( QVariant& ) m_value;
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::getValue ( int index ) const
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return ( QQtDict& ) m_list[index];
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::getValue ( const QString& key )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return m_map[key];
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::getChild ( int index )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
2017-11-24 22:43:12 +08:00
|
|
|
return m_list[index];
|
2017-11-24 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
QQtDict& QQtDict::getChild ( QString key )
|
2017-11-24 19:43:36 +08:00
|
|
|
{
|
|
|
|
return m_map[key];
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:43:12 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|