Skip to content

Commit

Permalink
更新MyImage类和MySvgView类,修复在Qt4下无法使用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arjide committed Nov 25, 2014
1 parent a2678f9 commit 8287cee
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 34 deletions.
178 changes: 148 additions & 30 deletions src/mywidgets/myimage.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "myimage.h"
#include <QPainter>
#include <QBitmap>
#include <QDir>
#include <QDebug>
#include <QPixmapCache>

#if(QT_VERSION>=0x050000)
MyImage::MyImage(QQuickItem *parent) :
Expand All @@ -11,10 +13,14 @@ MyImage::MyImage(QDeclarativeItem *parent) :
QDeclarativeItem(parent)
#endif
{
#if(QT_VERSION<0x050000)
setFlag(QGraphicsItem::ItemHasNoContents, false);
#endif
m_status = Null;
m_cache = true;
m_grayscale = false;
connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(onDownImageFinished(QNetworkReply*)));

pixmap = new QPixmap;
bitmap = new QBitmap;
connect (this, SIGNAL(widthChanged()), SLOT(onWidthChanged()));
connect (this, SIGNAL(heightChanged()), SLOT(onHeightChanged()));
Expand Down Expand Up @@ -56,46 +62,123 @@ QImage MyImage::chromaticToGrayscale(const QImage &image)
return iGray;
}

QString MyImage::imageFormatToString(const QByteArray &array)
{
QByteArray str = array.toHex ();

if(str.mid (2,6)=="504e47")
return "png";
if(str.mid (12,8)=="4a464946")
return "jpg";
if(str.left (6)=="474946")
return "gif";
if(str.left (4)=="424d")
return "bmp";
return "";
}

void MyImage::downloadImage(const QUrl &url)
{
setStatus(Loading);

if(reply!=NULL){
reply->abort();
//先结束上次的网络请求
}

QNetworkRequest request;
request.setUrl(url);
reply = manager.get(request);
}

void MyImage::setPixmap(QImage image)
{
if(image.isNull())
return;

if(m_cache){//如果缓存图片
QPixmapCache::insert(m_source.toString(), QPixmap::fromImage(image));
}

if(m_grayscale){//如果为黑白
image = chromaticToGrayscale(image);//转换为黑白图
}
pixmap = QPixmap::fromImage (image);

if(m_maskSource.toString()!=""&&(!bitmap->isNull())){
int max_width = bitmap->width();
int max_height = bitmap->height();
max_width = pixmap.width()>max_width?pixmap.width():max_width;
max_height = pixmap.height()>max_height?pixmap.height():max_height;

pixmap = pixmap.scaled(max_width, max_height);
pixmap.setMask (bitmap->scaled(max_width, max_height));
}

setImplicitWidth(pixmap.size().width());//设置默认大小
setImplicitHeight(pixmap.size().height());

if( width()>0 )
onWidthChanged();
if(height ()>0)
onHeightChanged();

update();
}

void MyImage::onWidthChanged()
{
setImplicitHeight (pixmap->size ().height ()*(width ()/pixmap->size ().width ()));
setImplicitHeight (pixmap.size ().height ()*(width ()/pixmap.size ().width ()));
}

void MyImage::onHeightChanged()
{
setImplicitWidth (pixmap->size ().width ()*(height ()/pixmap->size ().height ()));
setImplicitWidth (pixmap.size ().width ()*(height ()/pixmap.size ().height ()));
}

void MyImage::onDownImageFinished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError){
qDebug()<<QString::fromUtf8("MyImage:图片下载成功");

QImage image;
if( !image.loadFromData(reply->readAll())){
emit loadError ();
setStatus(Error);
qDebug()<<QString::fromUtf8("MyImage:图片加载出错");
return;
}

setPixmap(image);
setStatus(Ready);
}else{
setStatus(Error);
emit loadError();
}
}

#if(QT_VERSION>=0x050000)
void MyImage::paint(QPainter *painter)
#else
void MyImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
#endif
{
painter->drawPixmap (0,0,pixmap->scaled (width(), height (), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
painter->drawPixmap (0,0,pixmap.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}

MyImage::State MyImage::status() const
{
return m_status;
}

void MyImage::setSource(QUrl arg)
{
if (!m_cache||m_source != arg) {
setStatus(Loading);

m_source = arg;
QString str;
str = arg.toString ();
if( str.mid (0, 3) == "qrc")
str = str.mid (3, str.count ()-3);
QImage image;
if( image.load (str)){
if(m_grayscale){//如果为黑白
image = chromaticToGrayscale(image);//转换为黑白图
}
*pixmap = QPixmap::fromImage (image);
pixmap->setMask (bitmap->scaled (pixmap->size ()));
setImplicitWidth(pixmap->size().width());//设置默认大小
setImplicitHeight(pixmap->size().height());

if( width()>0 )
onWidthChanged();
if(height ()>0)
onHeightChanged();
update ();
}else{
emit loadError ();
}
reLoad();
//加载图片
if(m_source != arg)
emit sourceChanged(arg);
}
Expand All @@ -109,6 +192,7 @@ void MyImage::setMaskSource(QUrl arg)
if( str.mid (0, 3) == "qrc")
str = str.mid (3, str.count ()-3);
bitmap->load (str);
reLoad();
emit maskSourceChanged(arg);
}
}
Expand All @@ -125,10 +209,44 @@ void MyImage::setGrayscale(bool arg)
{
if(m_grayscale!=arg){
m_grayscale = arg;
bool old_cache = cache ();
m_cache = false;
setSource (source ());
m_cache = old_cache;
reLoad();
emit grayscaleChanged(arg);
}
}

void MyImage::setStatus(MyImage::State arg)
{
if (m_status != arg) {
m_status = arg;
emit statusChanged(arg);
}
}

void MyImage::reLoad()
{
QString str = m_source.toString();

QPixmap *temp_pximap = QPixmapCache::find(str);
if(temp_pximap!=NULL){//如果缓存区已经有图片
setPixmap(temp_pximap->toImage());
setStatus(Ready);
return;
}

if(str.indexOf("http")==0){//如果是网络图片
downloadImage(m_source);
return;
}

if( str.mid (0, 3) == "qrc")
str = str.mid (3, str.count ()-3);

QImage image;
if( !image.load (str)){
emit loadError ();
setStatus(Error);
return;
}

setPixmap(image);
}
37 changes: 34 additions & 3 deletions src/mywidgets/myimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QImage>
#include <QPixmap>
#include <QBitmap>
#include <QtNetwork>

#if(QT_VERSION>=0x050000)
#include <QQuickPaintedItem>
Expand All @@ -22,7 +23,17 @@ class MyImage : public QDeclarativeItem
Q_PROPERTY(QUrl maskSource READ maskSource WRITE setMaskSource NOTIFY maskSourceChanged)
Q_PROPERTY(bool cache READ cache WRITE setCache NOTIFY cacheChanged)
Q_PROPERTY(bool grayscale READ grayscale WRITE setGrayscale NOTIFY grayscaleChanged)
Q_PROPERTY(State status READ status NOTIFY statusChanged FINAL)

Q_ENUMS(State)
public:
enum State{
Null,
Ready,
Loading,
Error
};

#if(QT_VERSION>=0x050000)
explicit MyImage(QQuickItem *parent = 0);
#else
Expand All @@ -35,30 +46,50 @@ class MyImage : public QDeclarativeItem
bool grayscale() const;

QImage chromaticToGrayscale(const QImage &image);

static QString imageFormatToString(const QByteArray& array);

#if(QT_VERSION>=0x050000)
void paint(QPainter * painter);
#else
void paint(QPainter *painter, const QStyleOptionGraphicsItem *new_style, QWidget *new_widget=0);
#endif
State status() const;

private:
QUrl m_source;
QPixmap *pixmap;
QPixmap pixmap;
QBitmap *bitmap;
QUrl m_maskSource;
bool m_cache;
bool m_grayscale;
QNetworkAccessManager manager;
QNetworkReply *reply;

void downloadImage(const QUrl& url);
void setPixmap(QImage image);
State m_status;

protected:
void paint(QPainter * painter);
signals:
void sourceChanged(QUrl arg);
void maskSourceChanged(QUrl arg);
void loadError();//加载图片出错
void cacheChanged(bool arg);
void grayscaleChanged(bool arg);
void statusChanged(State arg);

private slots:
void onWidthChanged();
void onHeightChanged();
void onDownImageFinished(QNetworkReply* reply);
public slots:
void setSource(QUrl arg);
void setMaskSource(QUrl arg);
void setCache(bool arg);
void setGrayscale(bool arg);
void setStatus(State arg);

void reLoad();
};

#endif // MYIMAGE_H
7 changes: 7 additions & 0 deletions src/mywidgets/mysvgview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ MySvgView::MySvgView(QDeclarativeItem *parent) :
QDeclarativeItem(parent)
#endif
{
#if(QT_VERSION<0x050000)
setFlag(QGraphicsItem::ItemHasNoContents, false);
#endif
m_defaultSize = QSize(0,0);
svg = new QSvgRenderer(this);
connect (this, SIGNAL(widthChanged()), SLOT(onWidthChanged()));
Expand Down Expand Up @@ -45,7 +48,11 @@ void MySvgView::onHeightChanged()
setImplicitWidth (svg->defaultSize ().width ()*(height ()/svg->defaultSize ().height ()));
}

#if(QT_VERSION>=0x050000)
void MySvgView::paint(QPainter *painter)
#else
void MySvgView::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
#endif
{
svg->render (painter);
}
Expand Down
5 changes: 4 additions & 1 deletion src/mywidgets/mysvgview.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ private slots:
void onWidthChanged();
void onHeightChanged();
protected:
#if(QT_VERSION>=0x050000)
void paint(QPainter * painter);
//QSGNode *updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData * updatePaintNodeData);
#else
void paint(QPainter *painter, const QStyleOptionGraphicsItem *new_style, QWidget *new_widget=0);
#endif
signals:
void sourceChanged(QUrl arg);
void rotationModeChanged(Qt::Axis arg);
Expand Down

0 comments on commit 8287cee

Please sign in to comment.