Qt IVI Generator Addressbook Example
#include "contact.h"
#include <qiviqmlconversion_helper.h>
class ContactPrivate : public QSharedData
{
public :
ContactPrivate()
: QSharedData ()
, m_forename(QString ())
, m_name(QString ())
, m_phone(int (0 ))
{}
ContactPrivate(const ContactPrivate & other)
: QSharedData (other)
, m_forename(other. m_forename)
, m_name(other. m_name)
, m_phone(other. m_phone)
{}
ContactPrivate(const QString & forename, const QString & name, int phone)
: QSharedData ()
, m_forename(forename)
, m_name(name)
, m_phone(phone)
{}
QString m_forename;
QString m_name;
int m_phone;
};
Contact:: Contact()
: QIviStandardItem ()
, d(new ContactPrivate)
{
}
Contact:: Contact(const Contact & rhs)
: QIviStandardItem (rhs)
, d(rhs. d)
{
}
Contact & Contact:: operator = (const Contact & rhs)
{
QIviStandardItem :: operator = (rhs);
if (this ! = & rhs)
d. operator = (rhs. d);
return * this ;
}
Contact:: Contact(const QString & forename, const QString & name, int phone)
: QIviStandardItem ()
, d(new ContactPrivate(forename, name, phone))
{
}
Contact:: ~ Contact()
{
}
QString Contact:: type() const
{
return QLatin1String("contact" );
}
QString Contact:: forename() const
{
return d- > m_forename;
}
void Contact:: setForename(const QString & forename)
{
d- > m_forename = forename;
}
QString Contact:: name() const
{
return d- > m_name;
}
void Contact:: setName(const QString & name)
{
d- > m_name = name;
}
int Contact:: phone() const
{
return d- > m_phone;
}
void Contact:: setPhone(int phone)
{
d- > m_phone = phone;
}
void Contact:: fromJSON(const QVariant & variant)
{
QVariant value = qtivi_convertFromJSON(variant);
if (value. canConvert(QVariant :: Map))
value. convert(QVariant :: Map);
if (value. canConvert(QVariant :: List))
value. convert(QVariant :: List);
if (value. type() = = QVariant :: Map) {
QVariantMap map = value. toMap();
if (map. contains(QStringLiteral ("forename" )))
d- > m_forename = map. value(QStringLiteral ("forename" )). value< QString > ();
if (map. contains(QStringLiteral ("name" )))
d- > m_name = map. value(QStringLiteral ("name" )). value< QString > ();
if (map. contains(QStringLiteral ("phone" )))
d- > m_phone = map. value(QStringLiteral ("phone" )). value< int > ();
} else if (value. type() = = QVariant :: List) {
QVariantList values = value. toList();
d- > m_forename = values. value(0 ). value< QString > ();
d- > m_name = values. value(1 ). value< QString > ();
d- > m_phone = values. value(2 ). value< int > ();
}
}
bool operator = = (const Contact & left, const Contact & right) Q_DECL_NOTHROW
{
if (left. d = = right. d)
return true ;
return (
left. forename() = = right. forename() & &
left. name() = = right. name() & &
left. phone() = = right. phone()
);
}
bool operator ! = (const Contact & left, const Contact & right) Q_DECL_NOTHROW
{
return ! (left = = right);
}
QDataStream & operator < < (QDataStream & stream, const Contact & obj)
{
stream < < obj. forename();
stream < < obj. name();
stream < < obj. phone();
return stream;
}
QDataStream & operator > > (QDataStream & stream, Contact & obj)
{
stream > > obj. d- > m_forename;
stream > > obj. d- > m_name;
stream > > obj. d- > m_phone;
return stream;
}
QDebug & operator < < (QDebug & dbg, const Contact & obj)
{
Q_UNUSED(obj);
dbg < < "Contact" ;
return dbg;
}