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);
QPoint getGridPoint() const { return gridPoint; }
void setGridPoint(const QPoint &_gridPoint) { gridPoint = _gridPoint; }
QPoint getGridPos() const { return gridPoint; }
Player *getOwner() const { return owner; }
int getId() const { return id; }
void setId(int _id) { id = _id; }

View file

@ -169,28 +169,25 @@ AppearanceSettingsPage::AppearanceSettingsPage()
{
zoneBgGroupBox = new QGroupBox;
QSettings settings;
settings.beginGroup("zonebg");
handBgLabel = new QLabel;
handBgEdit = new QLineEdit(settings.value("hand").toString());
handBgEdit = new QLineEdit(settingsCache->getHandBgPath());
handBgEdit->setReadOnly(true);
QPushButton *handBgButton = new QPushButton("...");
connect(handBgButton, SIGNAL(clicked()), this, SLOT(handBgButtonClicked()));
tableBgLabel = new QLabel;
tableBgEdit = new QLineEdit(settings.value("table").toString());
tableBgEdit = new QLineEdit(settingsCache->getTableBgPath());
tableBgEdit->setReadOnly(true);
QPushButton *tableBgButton = new QPushButton("...");
connect(tableBgButton, SIGNAL(clicked()), this, SLOT(tableBgButtonClicked()));
playerAreaBgLabel = new QLabel;
playerAreaBgEdit = new QLineEdit(settings.value("playerarea").toString());
playerAreaBgEdit = new QLineEdit(settingsCache->getPlayerBgPath());
playerAreaBgEdit->setReadOnly(true);
QPushButton *playerAreaBgButton = new QPushButton("...");
connect(playerAreaBgButton, SIGNAL(clicked()), this, SLOT(playerAreaBgButtonClicked()));
settings.endGroup();
QGridLayout *zoneBgGrid = new QGridLayout;
zoneBgGrid->addWidget(handBgLabel, 0, 0);
zoneBgGrid->addWidget(handBgEdit, 0, 1);
@ -256,12 +253,9 @@ void AppearanceSettingsPage::handBgButtonClicked()
QString path = QFileDialog::getOpenFileName(this, tr("Choose path"));
if (path.isEmpty())
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()
@ -269,12 +263,9 @@ void AppearanceSettingsPage::tableBgButtonClicked()
QString path = QFileDialog::getOpenFileName(this, tr("Choose path"));
if (path.isEmpty())
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()
@ -282,12 +273,9 @@ void AppearanceSettingsPage::playerAreaBgButtonClicked()
QString path = QFileDialog::getOpenFileName(this, tr("Choose path"));
if (path.isEmpty())
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)

View file

@ -3,17 +3,23 @@
#include "player.h"
#include "client.h"
#include "protocol_items.h"
#include "settingscache.h"
HandZone::HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent)
: CardZone(_p, "hand", false, false, _p->getLocal(), parent), zoneHeight(_zoneHeight)
{
QSettings settings;
QString bgPath = settings.value("zonebg/hand").toString();
if (!bgPath.isEmpty())
bgPixmap.load(bgPath);
connect(settingsCache, SIGNAL(handBgPathChanged()), this, SLOT(updateBgPixmap()));
updateBgPixmap();
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

View file

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

View file

@ -12,6 +12,7 @@
#include "tab_game.h"
#include "protocol_items.h"
#include "gamescene.h"
#include "settingscache.h"
#include <QSettings>
#include <QPainter>
#include <QMenu>
@ -19,12 +20,11 @@
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)
{
QSettings settings;
QString bgPath = settings.value("zonebg/playerarea").toString();
if (!bgPath.isEmpty())
bgPixmap.load(bgPath);
setCacheMode(DeviceCoordinateCache);
connect(settingsCache, SIGNAL(playerBgPathChanged()), this, SLOT(updateBgPixmap()));
updateBgPixmap();
QPointF base = QPointF(counterAreaWidth, 50);
PileZone *deck = new PileZone(this, "deck", true, false, this);
@ -265,6 +265,16 @@ Player::~Player()
delete cardMenu;
}
void Player::updateBgPixmap()
{
QString bgPath = settingsCache->getPlayerBgPath();
if (!bgPath.isEmpty()) {
qDebug() << "loading" << bgPath;
bgPixmap.load(bgPath);
}
update();
}
void Player::updateBoundingRect()
{
prepareGeometryChange();

View file

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

View file

@ -11,6 +11,10 @@ SettingsCache::SettingsCache()
picsPath = settings->value("paths/pics").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();
doubleClickToPlay = settings->value("interface/doubleclicktoplay", 1).toInt();
economicGrid = settings->value("table/economic", 0).toInt();
@ -43,6 +47,27 @@ void SettingsCache::setCardDatabasePath(const QString &_cardDatabasePath)
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)
{
picDownload = _picDownload;
@ -60,4 +85,5 @@ void SettingsCache::setEconomicGrid(int _economicGrid)
{
economicGrid = _economicGrid;
settings->setValue("table/economic", economicGrid);
emit economicGridChanged();
}

View file

@ -11,12 +11,17 @@ signals:
void langChanged();
void picsPathChanged();
void cardDatabasePathChanged();
void handBgPathChanged();
void tableBgPathChanged();
void playerBgPathChanged();
void picDownloadChanged();
void economicGridChanged();
private:
QSettings *settings;
QString lang;
QString deckPath, picsPath, cardDatabasePath;
QString handBgPath, tableBgPath, playerBgPath;
bool picDownload;
bool doubleClickToPlay;
bool economicGrid;
@ -26,6 +31,9 @@ public:
QString getDeckPath() const { return deckPath; }
QString getPicsPath() const { return picsPath; }
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 getDoubleClickToPlay() const { return doubleClickToPlay; }
bool getEconomicGrid() const { return economicGrid; }
@ -34,6 +42,9 @@ public slots:
void setDeckPath(const QString &_deckPath);
void setPicsPath(const QString &_picsPath);
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 setDoubleClickToPlay(int _doubleClickToPlay);
void setEconomicGrid(int _economicGrid);

View file

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

View file

@ -10,7 +10,10 @@ signals:
private:
int width, height;
QPixmap bgPixmap;
bool economicGrid;
private slots:
void updateBgPixmap();
public slots:
void reorganizeCards();
public:
static const int paddingY = 20;
static const int marginX = 20;
@ -19,7 +22,6 @@ public:
TableZone(Player *_p, QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void reorganizeCards();
void toggleTapped();
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);