Skip to content

Commit

Permalink
优化了qml TextEdit中gif图的显示
Browse files Browse the repository at this point in the history
  • Loading branch information
arjide committed Nov 6, 2014
1 parent 1a1eef5 commit c8a42b4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
93 changes: 73 additions & 20 deletions src/utility/texteditplaygif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TextEditPlayGif::TextEditPlayGif(QObject *parent) :
QObject(parent)
{
m_enabled=true;
}

QQuickTextEdit *TextEditPlayGif::target() const
Expand All @@ -24,6 +25,11 @@ QUrl TextEditPlayGif::cachePath() const
return m_cachePath;
}

bool TextEditPlayGif::enabled() const
{
return m_enabled;
}

void TextEditPlayGif::setTarget(QQuickTextEdit *arg)
{
if (m_target == arg)
Expand All @@ -49,6 +55,22 @@ void TextEditPlayGif::removeErrorUrl(const QString &url)
list_errorUrl.removeOne (url);
}

void TextEditPlayGif::setEnabled(bool arg)
{
if (m_enabled == arg)
return;

m_enabled = arg;
if(arg){
startAllMovie ();//启动所有动画
connect (m_target, SIGNAL(textChanged()), this, SLOT(onTextChanged()));//启动分析
}else{
disconnect (m_target, SIGNAL(textChanged()), this, SLOT(onTextChanged()));//停止分析
stopAllMovie ();//停止所有动画
}
emit enabledChanged(arg);
}

void TextEditPlayGif::clearMovie()
{
foreach (MovieData data, list_movie) {
Expand Down Expand Up @@ -85,16 +107,17 @@ QString TextEditPlayGif::getGifNameByMovie(QMovie *movie)
return "";
}

QMovie *TextEditPlayGif::getMovieByUrl(const QString &url)
TextEditPlayGif::MovieData *TextEditPlayGif::getDataByGifNameAndSize(const QString &name, QSize size)
{
foreach (MovieData data, list_movie) {
if(data.url==url)
return data.movie;
for(int i=0;i<list_movie.size ();++i){
MovieData* data = &list_movie[i];
if(data->gifName==name&&data->size==size)
return data;
}
return NULL;
}

TextEditPlayGif::MovieData *TextEditPlayGif::getMovieDataByMovie(const QMovie *movie)
TextEditPlayGif::MovieData *TextEditPlayGif::getDataByMovie(const QMovie *movie)
{
for(int i=0;i<list_movie.size ();++i){
MovieData* data = &list_movie[i];
Expand Down Expand Up @@ -129,6 +152,34 @@ void TextEditPlayGif::addErrorUrl(const QString url)
list_errorUrl<<url;
}

void TextEditPlayGif::startAllMovie()
{
for(int i=0;i<list_movie.size ();++i){
MovieData* data = &list_movie[i];
if(data->movie->state () != QMovie::Running)
data->movie->start ();
}
}

void TextEditPlayGif::stopAllMovie()
{
for(int i=0;i<list_movie.size ();++i){
MovieData* data = &list_movie[i];
if(data->movie->state () == QMovie::Running)
data->movie->stop ();
}
}

void TextEditPlayGif::setTextEditContent(const QString &data)
{
int pos = m_target->cursorPosition ();
int select_start = m_target->selectionStart ();
int select_end = m_target->selectionEnd ();
m_target->setText (data);
m_target->setCursorPosition (pos);
m_target->select (select_start, select_end);
}

void TextEditPlayGif::onTextChanged()
{
QString content = m_target->text ();
Expand Down Expand Up @@ -193,14 +244,18 @@ void TextEditPlayGif::onTextChanged()
addErrorUrl (src);
break;
}
//qDebug()<<width<<height;

QMovie *movie=new QMovie(src, "", this);
movie->setCacheMode(QMovie::CacheAll);
addMovie (movie, img, gifName, QSize(width, height));
connect (movie, SIGNAL(frameChanged(int)), SLOT(onMovie(int)));
connect (movie, SIGNAL(finished()), SLOT(onMovieFinished()));
movie->start ();
MovieData* data = getDataByGifNameAndSize (gifName, QSize(width, height));
if(data!=NULL){
QString html = m_target->text ().replace (img, data->url);
setTextEditContent (html);
}else{
QMovie *movie=new QMovie(src, "", this);
movie->setCacheMode(QMovie::CacheAll);
addMovie (movie, img, gifName, QSize(width, height));
connect (movie, SIGNAL(frameChanged(int)), SLOT(onMovie(int)));
connect (movie, SIGNAL(finished()), SLOT(onMovieFinished()));
movie->start ();
}
}
}
}
Expand All @@ -213,7 +268,7 @@ void saveImage(const QString& name, QImage image)
void TextEditPlayGif::onMovie(int frame)
{
QMovie* movie = qobject_cast<QMovie*>(sender());
MovieData* movie_data = getMovieDataByMovie (movie);
MovieData* movie_data = getDataByMovie (movie);

if(movie&&movie_data&&movie_data->url!=""){
QString name = QDir::homePath ()+"/webqq/cacheImage/"+movie_data->gifName+"/";
Expand All @@ -231,12 +286,7 @@ void TextEditPlayGif::onMovie(int frame)
QString data = m_target->text ().replace (movie_data->url, name);
//qDebug()<<m_target->text ()<<data;
setUrlByMovie (movie, name);
int pos = m_target->cursorPosition ();
int select_start = m_target->selectionStart ();
int select_end = m_target->selectionEnd ();
m_target->setText (data);
m_target->setCursorPosition (pos);
m_target->select (select_start, select_end);
setTextEditContent (data);
}else{
image = image.scaled (movie_data->size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QtConcurrent::run(saveImage, name, image);
Expand All @@ -249,6 +299,9 @@ void TextEditPlayGif::onMovie(int frame)

void TextEditPlayGif::onMovieFinished()
{
if(!m_enabled)
return;

QMovie* movie = qobject_cast<QMovie*>(sender());
if(movie!=NULL&&movie->currentFrameNumber ()!=-1)
movie->start ();
Expand Down
19 changes: 16 additions & 3 deletions src/utility/texteditplaygif.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ class TextEditPlayGif : public QObject
Q_OBJECT

Q_PROPERTY(QQuickTextEdit* target READ target WRITE setTarget NOTIFY targetChanged)
Q_PROPERTY(QUrl cachePath READ cachePath WRITE setCachePath NOTIFY cachePathChanged)
Q_PROPERTY(QUrl cachePath READ cachePath WRITE setCachePath NOTIFY cachePathChanged)//此属性暂时未使用
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)

public:
TextEditPlayGif(QObject *parent=0);

QQuickTextEdit* target() const;
QUrl cachePath() const;
bool enabled() const;

private:
struct MovieData{
QMovie* movie;
Expand All @@ -33,17 +37,23 @@ class TextEditPlayGif : public QObject
QStringList list_errorUrl;//记录那些解析出错的gif的路径
QUrl m_cachePath;
QString old_content;
bool m_enabled;

void clearMovie();
void addMovie(QMovie *movie, const QString &url, const QString& gif_name, QSize size);
QString getUrlByMovie(QMovie* movie);
QString getGifNameByMovie(QMovie* movie);
QMovie* getMovieByUrl(const QString& url);
MovieData* getMovieDataByMovie(const QMovie* movie);
MovieData* getDataByGifNameAndSize(const QString& name, QSize size);
MovieData* getDataByMovie(const QMovie* movie);
void setUrlByMovie(QMovie* movie, const QString &url);

bool isErrorUrl(const QString url);
void addErrorUrl(const QString url);

void startAllMovie();
void stopAllMovie();

void setTextEditContent(const QString& data);
public slots:
void onTextChanged();
private slots:
Expand All @@ -53,11 +63,14 @@ private slots:
void targetChanged(QQuickTextEdit* arg);
void cachePathChanged(QUrl arg);
void error(const QString& errorString);
void enabledChanged(bool arg);

public slots:
void setTarget(QQuickTextEdit* arg);
void setCachePath(QUrl arg);

void removeErrorUrl(const QString& url);
void setEnabled(bool arg);
};

#endif // TEXTEDITPLAYGIF_H

0 comments on commit c8a42b4

Please sign in to comment.