make more use of SettingsCache

This commit is contained in:
Max-Wilhelm Bruker 2010-03-06 23:02:45 +01:00
parent 2c9a8c2b57
commit e9a0203880
10 changed files with 106 additions and 48 deletions

View file

@ -34,6 +34,7 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QPoint getGridPoint() const { return gridPoint; } QPoint getGridPoint() const { return gridPoint; }
void setGridPoint(const QPoint &_gridPoint) { gridPoint = _gridPoint; } void setGridPoint(const QPoint &_gridPoint) { gridPoint = _gridPoint; }
QPoint getGridPos() const { return gridPoint; }
Player *getOwner() const { return owner; } Player *getOwner() const { return owner; }
int getId() const { return id; } int getId() const { return id; }
void setId(int _id) { id = _id; } void setId(int _id) { id = _id; }

View file

@ -169,28 +169,25 @@ AppearanceSettingsPage::AppearanceSettingsPage()
{ {
zoneBgGroupBox = new QGroupBox; zoneBgGroupBox = new QGroupBox;
QSettings settings; QSettings settings;
settings.beginGroup("zonebg");
handBgLabel = new QLabel; handBgLabel = new QLabel;
handBgEdit = new QLineEdit(settings.value("hand").toString()); handBgEdit = new QLineEdit(settingsCache->getHandBgPath());
handBgEdit->setReadOnly(true); handBgEdit->setReadOnly(true);
QPushButton *handBgButton = new QPushButton("..."); QPushButton *handBgButton = new QPushButton("...");
connect(handBgButton, SIGNAL(clicked()), this, SLOT(handBgButtonClicked())); connect(handBgButton, SIGNAL(clicked()), this, SLOT(handBgButtonClicked()));
tableBgLabel = new QLabel; tableBgLabel = new QLabel;
tableBgEdit = new QLineEdit(settings.value("table").toString()); tableBgEdit = new QLineEdit(settingsCache->getTableBgPath());
tableBgEdit->setReadOnly(true); tableBgEdit->setReadOnly(true);
QPushButton *tableBgButton = new QPushButton("..."); QPushButton *tableBgButton = new QPushButton("...");
connect(tableBgButton, SIGNAL(clicked()), this, SLOT(tableBgButtonClicked())); connect(tableBgButton, SIGNAL(clicked()), this, SLOT(tableBgButtonClicked()));
playerAreaBgLabel = new QLabel; playerAreaBgLabel = new QLabel;
playerAreaBgEdit = new QLineEdit(settings.value("playerarea").toString()); playerAreaBgEdit = new QLineEdit(settingsCache->getPlayerBgPath());
playerAreaBgEdit->setReadOnly(true); playerAreaBgEdit->setReadOnly(true);
QPushButton *playerAreaBgButton = new QPushButton("..."); QPushButton *playerAreaBgButton = new QPushButton("...");
connect(playerAreaBgButton, SIGNAL(clicked()), this, SLOT(playerAreaBgButtonClicked())); connect(playerAreaBgButton, SIGNAL(clicked()), this, SLOT(playerAreaBgButtonClicked()));
settings.endGroup();
QGridLayout *zoneBgGrid = new QGridLayout; QGridLayout *zoneBgGrid = new QGridLayout;
zoneBgGrid->addWidget(handBgLabel, 0, 0); zoneBgGrid->addWidget(handBgLabel, 0, 0);
zoneBgGrid->addWidget(handBgEdit, 0, 1); zoneBgGrid->addWidget(handBgEdit, 0, 1);
@ -256,12 +253,9 @@ void AppearanceSettingsPage::handBgButtonClicked()
QString path = QFileDialog::getOpenFileName(this, tr("Choose path")); QString path = QFileDialog::getOpenFileName(this, tr("Choose path"));
if (path.isEmpty()) if (path.isEmpty())
return; return;
QSettings settings;
settings.beginGroup("zonebg");
settings.setValue("hand", path);
handBgEdit->setText(path);
emit handBgChanged(path); handBgEdit->setText(path);
settingsCache->setHandBgPath(path);
} }
void AppearanceSettingsPage::tableBgButtonClicked() void AppearanceSettingsPage::tableBgButtonClicked()
@ -269,12 +263,9 @@ void AppearanceSettingsPage::tableBgButtonClicked()
QString path = QFileDialog::getOpenFileName(this, tr("Choose path")); QString path = QFileDialog::getOpenFileName(this, tr("Choose path"));
if (path.isEmpty()) if (path.isEmpty())
return; return;
QSettings settings;
settings.beginGroup("zonebg");
settings.setValue("table", path);
tableBgEdit->setText(path);
emit tableBgChanged(path); tableBgEdit->setText(path);
settingsCache->setTableBgPath(path);
} }
void AppearanceSettingsPage::playerAreaBgButtonClicked() void AppearanceSettingsPage::playerAreaBgButtonClicked()
@ -282,12 +273,9 @@ void AppearanceSettingsPage::playerAreaBgButtonClicked()
QString path = QFileDialog::getOpenFileName(this, tr("Choose path")); QString path = QFileDialog::getOpenFileName(this, tr("Choose path"));
if (path.isEmpty()) if (path.isEmpty())
return; return;
QSettings settings;
settings.beginGroup("zonebg");
settings.setValue("playerarea", path);
playerAreaBgEdit->setText(path);
emit playerAreaBgChanged(path); playerAreaBgEdit->setText(path);
settingsCache->setPlayerBgPath(path);
} }
void AppearanceSettingsPage::zoneViewSortingCheckBoxChanged(int state) void AppearanceSettingsPage::zoneViewSortingCheckBoxChanged(int state)

View file

@ -3,17 +3,23 @@
#include "player.h" #include "player.h"
#include "client.h" #include "client.h"
#include "protocol_items.h" #include "protocol_items.h"
#include "settingscache.h"
HandZone::HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent) HandZone::HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent)
: CardZone(_p, "hand", false, false, _p->getLocal(), parent), zoneHeight(_zoneHeight) : CardZone(_p, "hand", false, false, _p->getLocal(), parent), zoneHeight(_zoneHeight)
{ {
QSettings settings; connect(settingsCache, SIGNAL(handBgPathChanged()), this, SLOT(updateBgPixmap()));
QString bgPath = settings.value("zonebg/hand").toString(); updateBgPixmap();
if (!bgPath.isEmpty())
bgPixmap.load(bgPath);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setAcceptsHoverEvents(true); // Awkwardly, this is needed to repaint the cached item after it has been corrupted by buggy rubberband drag. }
void HandZone::updateBgPixmap()
{
QString bgPath = settingsCache->getHandBgPath();
if (!bgPath.isEmpty())
bgPixmap.load(bgPath);
update();
} }
QRectF HandZone::boundingRect() const QRectF HandZone::boundingRect() const

View file

@ -4,9 +4,12 @@
#include "cardzone.h" #include "cardzone.h"
class HandZone : public CardZone { class HandZone : public CardZone {
Q_OBJECT
private: private:
QPixmap bgPixmap; QPixmap bgPixmap;
int zoneHeight; int zoneHeight;
private slots:
void updateBgPixmap();
public: public:
HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent = 0); HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent = 0);
QRectF boundingRect() const; QRectF boundingRect() const;

View file

@ -12,6 +12,7 @@
#include "tab_game.h" #include "tab_game.h"
#include "protocol_items.h" #include "protocol_items.h"
#include "gamescene.h" #include "gamescene.h"
#include "settingscache.h"
#include <QSettings> #include <QSettings>
#include <QPainter> #include <QPainter>
#include <QMenu> #include <QMenu>
@ -19,12 +20,11 @@
Player::Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent) Player::Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent)
: QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), local(_local), client(_client) : QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), local(_local), client(_client)
{ {
QSettings settings;
QString bgPath = settings.value("zonebg/playerarea").toString();
if (!bgPath.isEmpty())
bgPixmap.load(bgPath);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
connect(settingsCache, SIGNAL(playerBgPathChanged()), this, SLOT(updateBgPixmap()));
updateBgPixmap();
QPointF base = QPointF(counterAreaWidth, 50); QPointF base = QPointF(counterAreaWidth, 50);
PileZone *deck = new PileZone(this, "deck", true, false, this); PileZone *deck = new PileZone(this, "deck", true, false, this);
@ -265,6 +265,16 @@ Player::~Player()
delete cardMenu; delete cardMenu;
} }
void Player::updateBgPixmap()
{
QString bgPath = settingsCache->getPlayerBgPath();
if (!bgPath.isEmpty()) {
qDebug() << "loading" << bgPath;
bgPixmap.load(bgPath);
}
update();
}
void Player::updateBoundingRect() void Player::updateBoundingRect()
{ {
prepareGeometryChange(); prepareGeometryChange();

View file

@ -78,6 +78,7 @@ public slots:
void actSayMessage(); void actSayMessage();
private slots: private slots:
void updateBgPixmap();
void updateBoundingRect(); void updateBoundingRect();
void cardMenuAction(); void cardMenuAction();
void actSetCounters(); void actSetCounters();

View file

@ -11,6 +11,10 @@ SettingsCache::SettingsCache()
picsPath = settings->value("paths/pics").toString(); picsPath = settings->value("paths/pics").toString();
cardDatabasePath = settings->value("paths/carddatabase").toString(); cardDatabasePath = settings->value("paths/carddatabase").toString();
handBgPath = settings->value("zonebg/hand").toString();
tableBgPath = settings->value("zonebg/table").toString();
playerBgPath = settings->value("zonebg/playerarea").toString();
picDownload = settings->value("personal/picturedownload", 0).toInt(); picDownload = settings->value("personal/picturedownload", 0).toInt();
doubleClickToPlay = settings->value("interface/doubleclicktoplay", 1).toInt(); doubleClickToPlay = settings->value("interface/doubleclicktoplay", 1).toInt();
economicGrid = settings->value("table/economic", 0).toInt(); economicGrid = settings->value("table/economic", 0).toInt();
@ -43,6 +47,27 @@ void SettingsCache::setCardDatabasePath(const QString &_cardDatabasePath)
emit cardDatabasePathChanged(); emit cardDatabasePathChanged();
} }
void SettingsCache::setHandBgPath(const QString &_handBgPath)
{
handBgPath = _handBgPath;
settings->setValue("zonebg/hand", handBgPath);
emit handBgPathChanged();
}
void SettingsCache::setTableBgPath(const QString &_tableBgPath)
{
tableBgPath = _tableBgPath;
settings->setValue("zonebg/table", tableBgPath);
emit tableBgPathChanged();
}
void SettingsCache::setPlayerBgPath(const QString &_playerBgPath)
{
playerBgPath = _playerBgPath;
settings->setValue("zonebg/player", playerBgPath);
emit playerBgPathChanged();
}
void SettingsCache::setPicDownload(int _picDownload) void SettingsCache::setPicDownload(int _picDownload)
{ {
picDownload = _picDownload; picDownload = _picDownload;
@ -60,4 +85,5 @@ void SettingsCache::setEconomicGrid(int _economicGrid)
{ {
economicGrid = _economicGrid; economicGrid = _economicGrid;
settings->setValue("table/economic", economicGrid); settings->setValue("table/economic", economicGrid);
emit economicGridChanged();
} }

View file

@ -11,12 +11,17 @@ signals:
void langChanged(); void langChanged();
void picsPathChanged(); void picsPathChanged();
void cardDatabasePathChanged(); void cardDatabasePathChanged();
void handBgPathChanged();
void tableBgPathChanged();
void playerBgPathChanged();
void picDownloadChanged(); void picDownloadChanged();
void economicGridChanged();
private: private:
QSettings *settings; QSettings *settings;
QString lang; QString lang;
QString deckPath, picsPath, cardDatabasePath; QString deckPath, picsPath, cardDatabasePath;
QString handBgPath, tableBgPath, playerBgPath;
bool picDownload; bool picDownload;
bool doubleClickToPlay; bool doubleClickToPlay;
bool economicGrid; bool economicGrid;
@ -26,6 +31,9 @@ public:
QString getDeckPath() const { return deckPath; } QString getDeckPath() const { return deckPath; }
QString getPicsPath() const { return picsPath; } QString getPicsPath() const { return picsPath; }
QString getCardDatabasePath() const { return cardDatabasePath; } QString getCardDatabasePath() const { return cardDatabasePath; }
QString getHandBgPath() const { return handBgPath; }
QString getTableBgPath() const { return tableBgPath; }
QString getPlayerBgPath() const { return playerBgPath; }
bool getPicDownload() const { return picDownload; } bool getPicDownload() const { return picDownload; }
bool getDoubleClickToPlay() const { return doubleClickToPlay; } bool getDoubleClickToPlay() const { return doubleClickToPlay; }
bool getEconomicGrid() const { return economicGrid; } bool getEconomicGrid() const { return economicGrid; }
@ -34,6 +42,9 @@ public slots:
void setDeckPath(const QString &_deckPath); void setDeckPath(const QString &_deckPath);
void setPicsPath(const QString &_picsPath); void setPicsPath(const QString &_picsPath);
void setCardDatabasePath(const QString &_cardDatabasePath); void setCardDatabasePath(const QString &_cardDatabasePath);
void setHandBgPath(const QString &_handBgPath);
void setTableBgPath(const QString &_tableBgPath);
void setPlayerBgPath(const QString &_playerBgPath);
void setPicDownload(int _picDownload); void setPicDownload(int _picDownload);
void setDoubleClickToPlay(int _doubleClickToPlay); void setDoubleClickToPlay(int _doubleClickToPlay);
void setEconomicGrid(int _economicGrid); void setEconomicGrid(int _economicGrid);

View file

@ -3,17 +3,16 @@
#include "player.h" #include "player.h"
#include "client.h" #include "client.h"
#include "protocol_items.h" #include "protocol_items.h"
#include "settingscache.h"
TableZone::TableZone(Player *_p, QGraphicsItem *parent) TableZone::TableZone(Player *_p, QGraphicsItem *parent)
: CardZone(_p, "table", true, false, true, parent) : CardZone(_p, "table", true, false, true, parent)
{ {
QSettings settings; connect(settingsCache, SIGNAL(tableBgPathChanged()), this, SLOT(updateBgPixmap()));
QString bgPath = settings.value("zonebg/table").toString(); connect(settingsCache, SIGNAL(economicGridChanged()), this, SLOT(reorganizeCards()));
if (!bgPath.isEmpty()) updateBgPixmap();
bgPixmap.load(bgPath);
economicGrid = settings.value("table/economic", 1).toInt(); if (settingsCache->getEconomicGrid())
if (economicGrid)
height = (int) (14.0 / 3 * CARD_HEIGHT + 3 * paddingY); height = (int) (14.0 / 3 * CARD_HEIGHT + 3 * paddingY);
else else
height = 4 * CARD_HEIGHT + 3 * paddingY; height = 4 * CARD_HEIGHT + 3 * paddingY;
@ -23,6 +22,14 @@ TableZone::TableZone(Player *_p, QGraphicsItem *parent)
setAcceptsHoverEvents(true); setAcceptsHoverEvents(true);
} }
void TableZone::updateBgPixmap()
{
QString bgPath = settingsCache->getTableBgPath();
if (!bgPath.isEmpty())
bgPixmap.load(bgPath);
update();
}
QRectF TableZone::boundingRect() const QRectF TableZone::boundingRect() const
{ {
return QRectF(0, 0, width, height); return QRectF(0, 0, width, height);
@ -43,19 +50,11 @@ void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*optio
void TableZone::addCardImpl(CardItem *card, int _x, int _y) void TableZone::addCardImpl(CardItem *card, int _x, int _y)
{ {
QPointF mapPoint = mapFromGrid(QPoint(_x, _y));
qreal x = mapPoint.x();
qreal y = mapPoint.y();
cards.append(card); cards.append(card);
if (!player->getLocal())
y = height - CARD_HEIGHT - y;
card->setPos(x, y);
card->setGridPoint(QPoint(_x, _y)); card->setGridPoint(QPoint(_x, _y));
resizeToContents(); resizeToContents();
card->setZValue((y + CARD_HEIGHT) * 10000000 + x + 1000);
card->setParentItem(this); card->setParentItem(this);
card->setVisible(true); card->setVisible(true);
card->update(); card->update();
@ -73,6 +72,17 @@ void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPo
void TableZone::reorganizeCards() void TableZone::reorganizeCards()
{ {
for (int i = 0; i < cards.size(); ++i) {
QPointF mapPoint = mapFromGrid(cards[i]->getGridPos());
qreal x = mapPoint.x();
qreal y = mapPoint.y();
if (!player->getLocal())
y = height - CARD_HEIGHT - y;
cards[i]->setPos(x, y);
cards[i]->setZValue((y + CARD_HEIGHT) * 10000000 + x + 1000);
}
update(); update();
} }
@ -130,7 +140,7 @@ CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const
QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const
{ {
if (gridPoint.y() == 3) { if (gridPoint.y() == 3) {
if (economicGrid) if (settingsCache->getEconomicGrid())
return QPointF( return QPointF(
20 + (CARD_WIDTH * gridPoint.x() + CARD_WIDTH * (gridPoint.x() / 3)) / 2, 20 + (CARD_WIDTH * gridPoint.x() + CARD_WIDTH * (gridPoint.x() / 3)) / 2,
(CARD_HEIGHT + paddingY) * gridPoint.y() + (gridPoint.x() % 3 * CARD_HEIGHT) / 3 (CARD_HEIGHT + paddingY) * gridPoint.y() + (gridPoint.x() % 3 * CARD_HEIGHT) / 3
@ -166,7 +176,7 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const
); );
if (result.y() == 3) { if (result.y() == 3) {
if (economicGrid) if (settingsCache->getEconomicGrid())
return QPoint( return QPoint(
(int) (x * 2 / CARD_WIDTH - floor(x / (2 * CARD_WIDTH))), (int) (x * 2 / CARD_WIDTH - floor(x / (2 * CARD_WIDTH))),
3 3

View file

@ -10,7 +10,10 @@ signals:
private: private:
int width, height; int width, height;
QPixmap bgPixmap; QPixmap bgPixmap;
bool economicGrid; private slots:
void updateBgPixmap();
public slots:
void reorganizeCards();
public: public:
static const int paddingY = 20; static const int paddingY = 20;
static const int marginX = 20; static const int marginX = 20;
@ -19,7 +22,6 @@ public:
TableZone(Player *_p, QGraphicsItem *parent = 0); TableZone(Player *_p, QGraphicsItem *parent = 0);
QRectF boundingRect() const; QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void reorganizeCards();
void toggleTapped(); void toggleTapped();
void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown = false); void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown = false);
void handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown = false, bool tapped = false); void handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown = false, bool tapped = false);