From 40fbbc5982fd72d2daa40bab9dd448b145e72cb6 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sun, 1 Apr 2012 14:01:20 +0200 Subject: [PATCH] added size contraints for zone view & scroll bars, fixing issue #34 --- cockatrice/src/zoneviewwidget.cpp | 44 ++++++++++++++++++++++++++++--- cockatrice/src/zoneviewwidget.h | 4 +++ cockatrice/src/zoneviewzone.cpp | 8 ++++++ cockatrice/src/zoneviewzone.h | 3 +++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/cockatrice/src/zoneviewwidget.cpp b/cockatrice/src/zoneviewwidget.cpp index a9e62cfe..580624e7 100644 --- a/cockatrice/src/zoneviewwidget.cpp +++ b/cockatrice/src/zoneviewwidget.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "zoneviewwidget.h" #include "carditem.h" #include "zoneviewzone.h" @@ -104,9 +105,26 @@ ZoneViewWidget::ZoneViewWidget(Player *_player, CardZone *_origZone, int numberC extraHeight = vbox->sizeHint(Qt::PreferredSize).height(); resize(150, 150); - - zone = new ZoneViewZone(player, _origZone, numberCards, _revealZone, _writeableRevealZone, this); - vbox->addItem(zone); + + QGraphicsLinearLayout *zoneHBox = new QGraphicsLinearLayout(Qt::Horizontal); + + zoneContainer = new QGraphicsWidget(this); + zoneContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + zoneContainer->setFlag(QGraphicsItem::ItemClipsChildrenToShape); + zoneHBox->addItem(zoneContainer); + + scrollBar = new QScrollBar(Qt::Vertical); + scrollBar->setMinimum(0); + scrollBar->setSingleStep(50); + connect(scrollBar, SIGNAL(valueChanged(int)), this, SLOT(handleScrollBarChange(int))); + QGraphicsProxyWidget *scrollBarProxy = new QGraphicsProxyWidget; + scrollBarProxy->setWidget(scrollBar); + zoneHBox->addItem(scrollBarProxy); + + vbox->addItem(zoneHBox); + + zone = new ZoneViewZone(player, _origZone, numberCards, _revealZone, _writeableRevealZone, zoneContainer); + connect(zone, SIGNAL(wheelEventReceived(QGraphicsSceneWheelEvent *)), this, SLOT(handleWheelEvent(QGraphicsSceneWheelEvent *))); if (sortByNameCheckBox) { connect(sortByNameCheckBox, SIGNAL(stateChanged(int)), zone, SLOT(setSortByName(int))); @@ -148,13 +166,31 @@ void ZoneViewWidget::moveWidget(QPointF scenePos) void ZoneViewWidget::resizeToZoneContents() { QRectF zoneRect = zone->getOptimumRect(); - QSizeF newSize(qMax(QGraphicsWidget::layout()->effectiveSizeHint(Qt::MinimumSize, QSizeF()).width(), zoneRect.width() + 10), zoneRect.height() + extraHeight + 10); + qreal totalZoneHeight = zoneRect.height(); + if (zoneRect.height() > 500) + zoneRect.setHeight(500); + QSizeF newSize(qMax(QGraphicsWidget::layout()->effectiveSizeHint(Qt::MinimumSize, QSizeF()).width(), zoneRect.width() + scrollBar->width() + 10), zoneRect.height() + extraHeight + 10); setMaximumSize(newSize); resize(newSize); + + zone->setGeometry(QRectF(0, -scrollBar->value(), zoneContainer->size().width(), totalZoneHeight)); + scrollBar->setMaximum(totalZoneHeight - zoneRect.height()); + if (layout()) layout()->invalidate(); } +void ZoneViewWidget::handleWheelEvent(QGraphicsSceneWheelEvent *event) +{ + QWheelEvent wheelEvent(QPoint(), event->delta(), event->buttons(), event->modifiers(), event->orientation()); + scrollBar->event(&wheelEvent); +} + +void ZoneViewWidget::handleScrollBarChange(int value) +{ + zone->setY(-value); +} + void ZoneViewWidget::closeEvent(QCloseEvent *event) { disconnect(zone, SIGNAL(beingDeleted()), this, 0); diff --git a/cockatrice/src/zoneviewwidget.h b/cockatrice/src/zoneviewwidget.h index 0b8eef7a..5148ba74 100644 --- a/cockatrice/src/zoneviewwidget.h +++ b/cockatrice/src/zoneviewwidget.h @@ -14,6 +14,7 @@ class QCheckBox; class GameScene; class ServerInfo_Card; class QGraphicsSceneMouseEvent; +class QGraphicsSceneWheelEvent; class TitleLabel : public QGraphicsWidget { Q_OBJECT @@ -36,6 +37,7 @@ class ZoneViewWidget : public QGraphicsWidget { Q_OBJECT private: ZoneViewZone *zone; + QGraphicsWidget *zoneContainer; TitleLabel *titleLabel; QPushButton *closeButton; @@ -48,6 +50,8 @@ signals: void closePressed(ZoneViewWidget *zv); private slots: void resizeToZoneContents(); + void handleWheelEvent(QGraphicsSceneWheelEvent *event); + void handleScrollBarChange(int value); void zoneDeleted(); void moveWidget(QPointF scenePos); public: diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index 06863b26..cfa1dda2 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "zoneviewzone.h" #include "player.h" #include "carddragitem.h" @@ -82,6 +83,8 @@ void ZoneViewZone::reorganizeCards() cards[i]->setId(i); int cols = floor(sqrt((double) cardCount / 2)); + if (cols > 7) + cols = 7; int rows = ceil((double) cardCount / cols); if (rows < 1) rows = 1; @@ -180,3 +183,8 @@ void ZoneViewZone::setWriteableRevealZone(bool _writeableRevealZone) writeableRevealZone = _writeableRevealZone; } + +void ZoneViewZone::wheelEvent(QGraphicsSceneWheelEvent *event) +{ + emit wheelEventReceived(event); +} diff --git a/cockatrice/src/zoneviewzone.h b/cockatrice/src/zoneviewzone.h index aab0d0f5..25850e38 100644 --- a/cockatrice/src/zoneviewzone.h +++ b/cockatrice/src/zoneviewzone.h @@ -7,6 +7,7 @@ class ZoneViewWidget; class Response; class ServerInfo_Card; +class QGraphicsSceneWheelEvent; class ZoneViewZone : public SelectZone, public QGraphicsLayoutItem { Q_OBJECT @@ -39,9 +40,11 @@ private slots: signals: void beingDeleted(); void optimumRectChanged(); + void wheelEventReceived(QGraphicsSceneWheelEvent *event); protected: void addCardImpl(CardItem *card, int x, int y); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; + void wheelEvent(QGraphicsSceneWheelEvent *event); }; #endif