automatic picture download is now optional
This commit is contained in:
parent
d653152bdc
commit
ef60ed292e
4 changed files with 89 additions and 35 deletions
|
@ -96,6 +96,12 @@ QString CardInfo::getMainCardType() const
|
|||
return result;
|
||||
}
|
||||
|
||||
QString CardInfo::getCorrectedName() const
|
||||
{
|
||||
// Fire // Ice, Circle of Protection: Red
|
||||
return getName().remove(" // ").remove(":");
|
||||
}
|
||||
|
||||
void CardInfo::addToSet(CardSet *set)
|
||||
{
|
||||
set->append(this);
|
||||
|
@ -119,47 +125,58 @@ QPixmap *CardInfo::loadPixmap()
|
|||
debugOutput.append(QString("%1, ").arg(sets[i]->getShortName()));
|
||||
qDebug(debugOutput.toLatin1());
|
||||
|
||||
QString correctedName = getCorrectedName();
|
||||
for (int i = 0; i < sets.size(); i++) {
|
||||
// Fire // Ice, Circle of Protection: Red
|
||||
QString correctedName = getName().remove(" // ").remove(":");
|
||||
if (pixmap->load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(sets[i]->getShortName()).arg(correctedName)))
|
||||
return pixmap;
|
||||
if (pixmap->load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(sets[i]->getShortName()).arg(correctedName).arg(1)))
|
||||
return pixmap;
|
||||
if(pixmap->load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("downloadedPics").arg(correctedName)))
|
||||
return pixmap;
|
||||
|
||||
startDownload(picsPath, correctedName);
|
||||
|
||||
|
||||
}
|
||||
if (db->getPicDownload())
|
||||
startDownload();
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
void CardInfo::startDownload(QString picsPath, QString cardName)
|
||||
{ if(!QDir(QString(picsPath+"/downloadedPics/")).exists())
|
||||
{
|
||||
QDir dir(picsPath);
|
||||
dir.mkdir("downloadedPics");
|
||||
}
|
||||
newPic = new QFile(picsPath+"/downloadedPics/"+cardName+".full.jpg");
|
||||
newPic->open(QIODevice::WriteOnly);
|
||||
connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(picDownloadFinished(int, bool)));
|
||||
QUrl url(picURL);
|
||||
http.setHost(url.host(), url.port(80));
|
||||
dlID = http.get(url.path(), newPic);
|
||||
void CardInfo::startDownload()
|
||||
{
|
||||
downloadBuffer = new QBuffer(this);
|
||||
downloadBuffer->open(QIODevice::ReadWrite);
|
||||
http = new QHttp(this);
|
||||
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(picDownloadFinished(int, bool)));
|
||||
QUrl url(picURL);
|
||||
http->setHost(url.host(), url.port(80));
|
||||
dlID = http->get(url.path(), downloadBuffer);
|
||||
}
|
||||
|
||||
void CardInfo::picDownloadFinished(int id, bool result)
|
||||
void CardInfo::picDownloadFinished(int id, bool error)
|
||||
{
|
||||
if(id == dlID){
|
||||
newPic->flush();
|
||||
newPic->close();
|
||||
http.close();
|
||||
updatePixmapCache();
|
||||
disconnect(&http, 0, this, 0);
|
||||
delete newPic;
|
||||
if (id != dlID)
|
||||
return;
|
||||
http->close();
|
||||
disconnect(http, 0, this, 0);
|
||||
http->deleteLater();
|
||||
|
||||
downloadBuffer->close();
|
||||
if (!error) {
|
||||
QString picsPath = db->getPicsPath();
|
||||
const QByteArray &picData = downloadBuffer->data();
|
||||
QPixmap testPixmap;
|
||||
if (testPixmap.loadFromData(picData)) {
|
||||
if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) {
|
||||
QDir dir(picsPath);
|
||||
dir.mkdir("downloadedPics");
|
||||
}
|
||||
QFile newPic(picsPath + "/downloadedPics/" + getCorrectedName() + ".full.jpg");
|
||||
newPic.open(QIODevice::WriteOnly);
|
||||
newPic.write(picData);
|
||||
newPic.close();
|
||||
|
||||
updatePixmapCache();
|
||||
}
|
||||
}
|
||||
delete downloadBuffer;
|
||||
}
|
||||
|
||||
QPixmap *CardInfo::getPixmap(QSize size)
|
||||
|
@ -237,6 +254,7 @@ CardDatabase::CardDatabase(QObject *parent)
|
|||
: QObject(parent), noCard(0)
|
||||
{
|
||||
updateDatabasePath();
|
||||
updatePicDownload();
|
||||
updatePicsPath();
|
||||
|
||||
noCard = new CardInfo(this);
|
||||
|
@ -424,6 +442,15 @@ bool CardDatabase::saveToFile(const QString &fileName)
|
|||
return true;
|
||||
}
|
||||
|
||||
void CardDatabase::updatePicDownload(int _picDownload)
|
||||
{
|
||||
if (_picDownload == -1) {
|
||||
QSettings settings;
|
||||
picDownload = settings.value("personal/picturedownload", 0).toInt();
|
||||
} else
|
||||
picDownload = _picDownload;
|
||||
}
|
||||
|
||||
void CardDatabase::updatePicsPath(const QString &path)
|
||||
{
|
||||
if (path.isEmpty()) {
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <QList>
|
||||
#include <QXmlStreamReader>
|
||||
#include <QHttp>
|
||||
#include <QFile>
|
||||
#include <QBuffer>
|
||||
|
||||
class CardDatabase;
|
||||
class CardInfo;
|
||||
|
@ -33,7 +33,7 @@ public:
|
|||
void sortByKey();
|
||||
};
|
||||
|
||||
class CardInfo : QObject{
|
||||
class CardInfo : QObject {
|
||||
Q_OBJECT
|
||||
private:
|
||||
CardDatabase *db;
|
||||
|
@ -46,14 +46,14 @@ private:
|
|||
QString text;
|
||||
QStringList colors;
|
||||
QString picURL;
|
||||
QHttp http;
|
||||
QFile *newPic;
|
||||
QHttp *http;
|
||||
QBuffer *downloadBuffer;
|
||||
int dlID;
|
||||
int tableRow;
|
||||
QPixmap *pixmap;
|
||||
QMap<int, QPixmap *> scaledPixmapCache;
|
||||
|
||||
void startDownload(QString, QString);
|
||||
void startDownload();
|
||||
public:
|
||||
CardInfo(CardDatabase *_db,
|
||||
const QString &_name = QString(),
|
||||
|
@ -75,16 +75,17 @@ public:
|
|||
QStringList getColors() const { return colors; }
|
||||
QString getPicURL() const { return picURL; }
|
||||
QString getMainCardType() const;
|
||||
QString getCorrectedName() const;
|
||||
int getTableRow() const { return tableRow; }
|
||||
void setTableRow(int _tableRow) { tableRow = _tableRow; }
|
||||
void setPicURL(QString _picURL) { picURL = _picURL; }
|
||||
void setPicURL(const QString &_picURL) { picURL = _picURL; }
|
||||
void addToSet(CardSet *set);
|
||||
QPixmap *loadPixmap();
|
||||
QPixmap *getPixmap(QSize size);
|
||||
void clearPixmapCache();
|
||||
void updatePixmapCache();
|
||||
public slots:
|
||||
void picDownloadFinished(int, bool);
|
||||
private slots:
|
||||
void picDownloadFinished(int id, bool error);
|
||||
};
|
||||
|
||||
class CardDatabase : public QObject {
|
||||
|
@ -97,6 +98,7 @@ protected:
|
|||
private:
|
||||
void loadCardsFromXml(QXmlStreamReader &xml);
|
||||
void loadSetsFromXml(QXmlStreamReader &xml);
|
||||
bool picDownload;
|
||||
public:
|
||||
CardDatabase(QObject *parent = 0);
|
||||
~CardDatabase();
|
||||
|
@ -105,6 +107,7 @@ public:
|
|||
CardSet *getSet(const QString &setName);
|
||||
QList<CardInfo *> getCardList() const { return cardHash.values(); }
|
||||
SetList getSetList() const;
|
||||
bool getPicDownload() const { return picDownload; }
|
||||
void clearPixmapCache();
|
||||
int loadFromFile(const QString &fileName);
|
||||
bool saveToFile(const QString &fileName);
|
||||
|
@ -112,6 +115,7 @@ public:
|
|||
public slots:
|
||||
void updatePicsPath(const QString &path = QString());
|
||||
void updateDatabasePath(const QString &path = QString());
|
||||
void updatePicDownload(int _picDownload = -1);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -8,6 +8,7 @@ GeneralSettingsPage::GeneralSettingsPage()
|
|||
QSettings settings;
|
||||
|
||||
personalGroupBox = new QGroupBox;
|
||||
|
||||
languageLabel = new QLabel;
|
||||
languageBox = new QComboBox;
|
||||
|
||||
|
@ -20,12 +21,19 @@ GeneralSettingsPage::GeneralSettingsPage()
|
|||
if ((qmFiles[i] == settings.value("lang").toString()) || (setLanguage.isEmpty() && langName == tr("English")))
|
||||
languageBox->setCurrentIndex(i);
|
||||
}
|
||||
|
||||
picDownloadCheckBox = new QCheckBox;
|
||||
picDownloadCheckBox->setChecked(settings.value("picturedownload", 0).toInt());
|
||||
|
||||
settings.endGroup();
|
||||
connect(languageBox, SIGNAL(currentIndexChanged(int)), this, SLOT(languageBoxChanged(int)));
|
||||
connect(picDownloadCheckBox, SIGNAL(stateChanged(int)), this, SLOT(picDownloadCheckBoxChanged(int)));
|
||||
|
||||
QGridLayout *personalGrid = new QGridLayout;
|
||||
personalGrid->addWidget(languageLabel, 0, 0);
|
||||
personalGrid->addWidget(languageBox, 0, 1);
|
||||
personalGrid->addWidget(picDownloadCheckBox, 1, 0, 1, 2);
|
||||
|
||||
personalGroupBox->setLayout(personalGrid);
|
||||
|
||||
pathsGroupBox = new QGroupBox;
|
||||
|
@ -160,10 +168,20 @@ void GeneralSettingsPage::languageBoxChanged(int index)
|
|||
emit changeLanguage(qmFile);
|
||||
}
|
||||
|
||||
void GeneralSettingsPage::picDownloadCheckBoxChanged(int state)
|
||||
{
|
||||
QSettings settings;
|
||||
settings.beginGroup("personal");
|
||||
settings.setValue("picturedownload", state);
|
||||
|
||||
emit picDownloadChanged(state);
|
||||
}
|
||||
|
||||
void GeneralSettingsPage::retranslateUi()
|
||||
{
|
||||
personalGroupBox->setTitle(tr("Personal settings"));
|
||||
languageLabel->setText(tr("Language:"));
|
||||
picDownloadCheckBox->setText(tr("Download card pictures on the fly"));
|
||||
pathsGroupBox->setTitle(tr("Paths"));
|
||||
deckPathLabel->setText(tr("Decks directory:"));
|
||||
picsPathLabel->setText(tr("Pictures directory:"));
|
||||
|
@ -250,7 +268,7 @@ DlgSettings::DlgSettings(CardDatabase *_db, QTranslator *_translator, QWidget *p
|
|||
contentsWidget->setIconSize(QSize(96, 84));
|
||||
contentsWidget->setMovement(QListView::Static);
|
||||
contentsWidget->setMinimumWidth(115);
|
||||
contentsWidget->setMaximumWidth(115);
|
||||
contentsWidget->setMaximumWidth(130);
|
||||
contentsWidget->setSpacing(12);
|
||||
|
||||
pagesWidget = new QStackedWidget;
|
||||
|
@ -258,6 +276,7 @@ DlgSettings::DlgSettings(CardDatabase *_db, QTranslator *_translator, QWidget *p
|
|||
connect(general, SIGNAL(picsPathChanged(const QString &)), db, SLOT(updatePicsPath(const QString &)));
|
||||
connect(general, SIGNAL(cardDatabasePathChanged(const QString &)), db, SLOT(updateDatabasePath(const QString &)));
|
||||
connect(general, SIGNAL(changeLanguage(const QString &)), this, SLOT(changeLanguage(const QString &)));
|
||||
connect(general, SIGNAL(picDownloadChanged(int)), db, SLOT(updatePicDownload(int)));
|
||||
pagesWidget->addWidget(general);
|
||||
pagesWidget->addWidget(new AppearanceSettingsPage);
|
||||
pagesWidget->addWidget(new MessagesSettingsPage);
|
||||
|
|
|
@ -12,6 +12,7 @@ class QLineEdit;
|
|||
class QPushButton;
|
||||
class QComboBox;
|
||||
class QGroupBox;
|
||||
class QCheckBox;
|
||||
class QLabel;
|
||||
|
||||
class AbstractSettingsPage : public QWidget {
|
||||
|
@ -30,17 +31,20 @@ private slots:
|
|||
void cardDatabasePathButtonClicked();
|
||||
void cardBackgroundPathButtonClicked();
|
||||
void languageBoxChanged(int index);
|
||||
void picDownloadCheckBoxChanged(int state);
|
||||
signals:
|
||||
void picsPathChanged(const QString &path);
|
||||
void cardDatabasePathChanged(const QString &path);
|
||||
void cardBackgroundPathChanged(const QString &path);
|
||||
void changeLanguage(const QString &qmFile);
|
||||
void picDownloadChanged(int state);
|
||||
private:
|
||||
QStringList findQmFiles();
|
||||
QString languageName(const QString &qmFile);
|
||||
QLineEdit *deckPathEdit, *picsPathEdit, *cardDatabasePathEdit, *cardBackgroundPathEdit;
|
||||
QGroupBox *personalGroupBox, *pathsGroupBox;
|
||||
QComboBox *languageBox;
|
||||
QCheckBox *picDownloadCheckBox;
|
||||
QLabel *languageLabel, *deckPathLabel, *picsPathLabel, *cardDatabasePathLabel, *cardBackgroundPathLabel;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue