Merge branch 'devel' of git://github.com/mbruker/Cockatrice into servernetwork

This commit is contained in:
Max-Wilhelm Bruker 2012-03-17 17:08:47 +01:00
commit d792c3ddc6
3 changed files with 29 additions and 65 deletions

View file

@ -78,8 +78,8 @@ bool PictureToLoad::nextSet()
return true;
}
PictureLoader::PictureLoader(QObject *parent)
: QObject(parent), downloadRunning(false), loadQueueRunning(false)
PictureLoader::PictureLoader(const QString &__picsPath, bool _picDownload, QObject *parent)
: QObject(parent), _picsPath(__picsPath), picDownload(_picDownload), downloadRunning(false), loadQueueRunning(false)
{
connect(this, SIGNAL(startLoadQueue()), this, SLOT(processLoadQueue()), Qt::QueuedConnection);
@ -87,6 +87,12 @@ PictureLoader::PictureLoader(QObject *parent)
connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(picDownloadFinished(QNetworkReply *)));
}
PictureLoader::~PictureLoader()
{
// This does not work with the destroyed() signal as this destructor is called after the main event loop is done.
thread()->quit();
}
void PictureLoader::processLoadQueue()
{
if (loadQueueRunning)
@ -224,40 +230,6 @@ void PictureLoader::setPicDownload(bool _picDownload)
picDownload = _picDownload;
}
PictureLoadingThread::PictureLoadingThread(const QString &_picsPath, bool _picDownload, QObject *parent)
: QThread(parent), picsPath(_picsPath), picDownload(_picDownload)
{
}
PictureLoadingThread::~PictureLoadingThread()
{
quit();
wait();
}
void PictureLoadingThread::run()
{
pictureLoader = new PictureLoader;
connect(pictureLoader, SIGNAL(imageLoaded(CardInfo *, const QImage &)), this, SIGNAL(imageLoaded(CardInfo *, const QImage &)));
pictureLoader->setPicsPath(picsPath);
pictureLoader->setPicDownload(picDownload);
usleep(100000);
initWaitCondition.wakeAll();
exec();
delete pictureLoader;
}
void PictureLoadingThread::waitForInit()
{
QMutex mutex;
mutex.lock();
initWaitCondition.wait(&mutex);
mutex.unlock();
}
CardInfo::CardInfo(CardDatabase *_db,
const QString &_name,
const QString &_manacost,
@ -466,10 +438,11 @@ CardDatabase::CardDatabase(QObject *parent)
loadCardDatabase();
loadingThread = new PictureLoadingThread(settingsCache->getPicsPath(), settingsCache->getPicDownload(), this);
connect(loadingThread, SIGNAL(imageLoaded(CardInfo *, QImage)), this, SLOT(imageLoaded(CardInfo *, QImage)));
loadingThread->start(QThread::LowPriority);
loadingThread->waitForInit();
pictureLoaderThread = new QThread;
pictureLoader = new PictureLoader(settingsCache->getPicsPath(), settingsCache->getPicDownload());
pictureLoader->moveToThread(pictureLoaderThread);
connect(pictureLoader, SIGNAL(imageLoaded(CardInfo *, const QImage &)), this, SLOT(imageLoaded(CardInfo *, const QImage &)));
pictureLoaderThread->start(QThread::LowPriority);
noCard = new CardInfo(this);
noCard->loadPixmap(); // cache pixmap for card back
@ -480,6 +453,10 @@ CardDatabase::~CardDatabase()
{
clear();
delete noCard;
pictureLoader->deleteLater();
pictureLoaderThread->wait();
delete pictureLoaderThread;
}
void CardDatabase::clear()
@ -673,7 +650,7 @@ bool CardDatabase::saveToFile(const QString &fileName)
void CardDatabase::picDownloadChanged()
{
loadingThread->getPictureLoader()->setPicDownload(settingsCache->getPicDownload());
pictureLoader->setPicDownload(settingsCache->getPicDownload());
if (settingsCache->getPicDownload()) {
QHashIterator<QString, CardInfo *> cardIterator(cardHash);
while (cardIterator.hasNext())
@ -739,7 +716,7 @@ void CardDatabase::cacheCardPixmaps(const QStringList &cardNames)
void CardDatabase::loadImage(CardInfo *card)
{
loadingThread->getPictureLoader()->loadImage(card, false);
pictureLoader->loadImage(card, false);
}
void CardDatabase::imageLoaded(CardInfo *card, QImage image)
@ -749,6 +726,6 @@ void CardDatabase::imageLoaded(CardInfo *card, QImage image)
void CardDatabase::picsPathChanged()
{
loadingThread->getPictureLoader()->setPicsPath(settingsCache->getPicsPath());
pictureLoader->setPicsPath(settingsCache->getPicsPath());
clearPixmapCache();
}

View file

@ -71,7 +71,8 @@ private:
bool picDownload, downloadRunning, loadQueueRunning;
void startNextPicDownload();
public:
PictureLoader(QObject *parent = 0);
PictureLoader(const QString &__picsPath, bool _picDownload, QObject *parent = 0);
~PictureLoader();
void setPicsPath(const QString &path);
void setPicDownload(bool _picDownload);
void loadImage(CardInfo *card, bool stripped);
@ -84,24 +85,6 @@ signals:
void imageLoaded(CardInfo *card, const QImage &image);
};
class PictureLoadingThread : public QThread {
Q_OBJECT
private:
QString picsPath;
bool picDownload;
PictureLoader *pictureLoader;
QWaitCondition initWaitCondition;
protected:
void run();
public:
PictureLoadingThread(const QString &_picsPath, bool _picDownload, QObject *parent);
~PictureLoadingThread();
PictureLoader *getPictureLoader() const { return pictureLoader; }
void waitForInit();
signals:
void imageLoaded(CardInfo *card, const QImage &image);
};
class CardInfo : public QObject {
Q_OBJECT
private:
@ -178,7 +161,9 @@ protected:
QHash<QString, CardSet *> setHash;
bool loadSuccess;
CardInfo *noCard;
PictureLoadingThread *loadingThread;
QThread *pictureLoaderThread;
PictureLoader *pictureLoader;
private:
void loadCardsFromXml(QXmlStreamReader &xml);
void loadSetsFromXml(QXmlStreamReader &xml);

View file

@ -4,6 +4,7 @@
#include <QPushButton>
#include <QStyle>
#include <QMouseEvent>
#include <QDesktopWidget>
#include "cardinfowidget.h"
#include "carditem.h"
#include "carddatabase.h"
@ -74,8 +75,9 @@ CardInfoWidget::CardInfoWidget(ResizeMode _mode, const QString &cardName, QWidge
textLabel->setMinimumHeight(100);
setFixedWidth(sizeHint().width());
} else if (mode == ModePopUp) {
setFixedWidth(350);
pixmapWidth = 250;
QDesktopWidget desktopWidget;
pixmapWidth = desktopWidget.screenGeometry().height() / 3 / aspectRatio;
setFixedWidth(pixmapWidth + 150);
} else
setFixedWidth(250);