kabc
picture.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "picture.h"
00022
00023 #include <QtCore/QBuffer>
00024 #include <QtCore/QSharedData>
00025
00026 using namespace KABC;
00027
00028 class Picture::Private : public QSharedData
00029 {
00030 public:
00031 Private()
00032 : mIntern( false )
00033 {
00034 }
00035
00036 Private( const Private &other )
00037 : QSharedData( other )
00038 {
00039 mUrl = other.mUrl;
00040 mType = other.mType;
00041 mData = other.mData;
00042 mIntern = other.mIntern;
00043 }
00044
00045 QString mUrl;
00046 QString mType;
00047 QImage mData;
00048
00049 bool mIntern;
00050 };
00051
00052 Picture::Picture()
00053 : d( new Private )
00054 {
00055 }
00056
00057 Picture::Picture( const QString &url )
00058 : d( new Private )
00059 {
00060 d->mUrl = url;
00061 }
00062
00063 Picture::Picture( const QImage &data )
00064 : d( new Private )
00065 {
00066 d->mIntern = true;
00067 d->mData = data;
00068 }
00069
00070 Picture::Picture( const Picture &other )
00071 : d( other.d )
00072 {
00073 }
00074
00075 Picture::~Picture()
00076 {
00077 }
00078
00079 Picture &Picture::operator=( const Picture &other )
00080 {
00081 if ( this != &other ) {
00082 d = other.d;
00083 }
00084
00085 return *this;
00086 }
00087
00088 bool Picture::operator==( const Picture &p ) const
00089 {
00090 if ( d->mIntern != p.d->mIntern ) {
00091 return false;
00092 }
00093
00094 if ( d->mIntern ) {
00095 if ( d->mData != p.d->mData ) {
00096 return false;
00097 }
00098 } else {
00099 if ( d->mUrl != p.d->mUrl ) {
00100 return false;
00101 }
00102 }
00103
00104 return true;
00105 }
00106
00107 bool Picture::operator!=( const Picture &p ) const
00108 {
00109 return !( p == *this );
00110 }
00111
00112 bool Picture::isEmpty() const
00113 {
00114 return
00115 ( ( d->mIntern == false && d->mUrl.isEmpty() ) || ( d->mIntern == true && d->mData.isNull() ) );
00116 }
00117
00118 void Picture::setUrl( const QString &url )
00119 {
00120 d->mUrl = url;
00121 d->mIntern = false;
00122 }
00123
00124 void Picture::setData( const QImage &data )
00125 {
00126 d->mData = data;
00127 d->mIntern = true;
00128 }
00129
00130 void Picture::setType( const QString &type )
00131 {
00132 d->mType = type;
00133 }
00134
00135 bool Picture::isIntern() const
00136 {
00137 return d->mIntern;
00138 }
00139
00140 QString Picture::url() const
00141 {
00142 return d->mUrl;
00143 }
00144
00145 QImage Picture::data() const
00146 {
00147 return d->mData;
00148 }
00149
00150 QString Picture::type() const
00151 {
00152 return d->mType;
00153 }
00154
00155 QString Picture::toString() const
00156 {
00157 QString str;
00158
00159 str += QLatin1String( "Picture {\n" );
00160 str += QString::fromLatin1( " Type: %1\n" ).arg( d->mType );
00161 str += QString::fromLatin1( " IsIntern: %1\n" ).
00162 arg( d->mIntern ? QLatin1String( "true" ) : QLatin1String( "false" ) );
00163 if ( d->mIntern ) {
00164 QByteArray data;
00165 QBuffer buffer( &data );
00166 buffer.open( QIODevice::WriteOnly );
00167 d->mData.save( &buffer, "PNG" );
00168 str += QString::fromLatin1( " Data: %1\n" ).arg( QString::fromLatin1( data.toBase64() ) );
00169 } else {
00170 str += QString::fromLatin1( " Url: %1\n" ).arg( d->mUrl );
00171 }
00172 str += QLatin1String( "}\n" );
00173
00174 return str;
00175 }
00176
00177 QDataStream &KABC::operator<<( QDataStream &s, const Picture &picture )
00178 {
00179 return s << picture.d->mIntern << picture.d->mUrl << picture.d->mType << picture.d->mData;
00180 }
00181
00182 QDataStream &KABC::operator>>( QDataStream &s, Picture &picture )
00183 {
00184 s >> picture.d->mIntern >> picture.d->mUrl >> picture.d->mType >> picture.d->mData;
00185
00186 return s;
00187 }