Clamp zone view widget's titlebar inside the viewable area

This commit is contained in:
Fabio Bas 2015-09-10 23:29:01 +02:00
parent 3bc61eb2e9
commit a7616835e3
2 changed files with 20 additions and 63 deletions

View file

@ -20,44 +20,6 @@
#include "pb/command_stop_dump_zone.pb.h"
#include "pb/command_shuffle.pb.h"
TitleLabel::TitleLabel()
: QGraphicsWidget(), text(" ")
{
setAcceptHoverEvents(true);
}
void TitleLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
QBrush windowBrush = palette().window();
windowBrush.setColor(windowBrush.color().darker(150));
painter->fillRect(boundingRect(), windowBrush);
painter->drawText(boundingRect(), Qt::AlignLeft | Qt::AlignVCenter, text);
}
QSizeF TitleLabel::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
QFont f;
QFontMetrics fm(f);
if (which == Qt::MaximumSize)
return QSizeF(constraint.width(), fm.size(Qt::TextSingleLine, text).height() + 10);
else
return fm.size(Qt::TextSingleLine, text) + QSizeF(10, 10);
}
void TitleLabel::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
buttonDownPos = static_cast<GameScene *>(scene())->getViewTransform().inverted().map(event->pos());
event->accept();
} else
event->ignore();
}
void TitleLabel::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
emit mouseMoved(event->scenePos() - buttonDownPos);
}
ZoneViewWidget::ZoneViewWidget(Player *_player, CardZone *_origZone, int numberCards, bool _revealZone, bool _writeableRevealZone, const QList<const ServerInfo_Card *> &cardList)
: QGraphicsWidget(0, Qt::Window), canBeShuffled(_origZone->getIsShufflable()), player(_player)
{
@ -174,8 +136,20 @@ void ZoneViewWidget::retranslateUi()
pileViewCheckBox.setText(tr("pile view"));
}
void ZoneViewWidget::moveWidget(QPointF scenePos)
void ZoneViewWidget::moveEvent(QGraphicsSceneMoveEvent * /* event */)
{
if(!scene())
return;
static int titleBarHeight = 0;
if(titleBarHeight == 0)
{
QStyleOptionTitleBar so;
titleBarHeight = style()->pixelMetric(QStyle::PM_TitleBarHeight, &so, (QWidget*) this);
}
QPointF scenePos = pos();
if(scenePos.x() < 0)
{
scenePos.setX(0);
@ -185,16 +159,17 @@ void ZoneViewWidget::moveWidget(QPointF scenePos)
scenePos.setX(maxw);
}
if(scenePos.y() < 0)
if(scenePos.y() < titleBarHeight)
{
scenePos.setY(0);
scenePos.setY(titleBarHeight);
} else {
qreal maxh = scene()->sceneRect().height() - 100;
qreal maxh = scene()->sceneRect().height() - titleBarHeight;
if(scenePos.y() > maxh)
scenePos.setY(maxh);
}
setPos(scenePos);
if(scenePos != pos())
setPos(scenePos);
}
void ZoneViewWidget::resizeToZoneContents()

View file

@ -18,30 +18,12 @@ class QGraphicsSceneMouseEvent;
class QGraphicsSceneWheelEvent;
class QStyleOption;
class TitleLabel : public QGraphicsWidget {
Q_OBJECT
private:
QString text;
QPointF buttonDownPos;
public:
TitleLabel();
void paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/);
void setText(const QString &_text) { text = _text; update(); }
signals:
void mouseMoved(QPointF scenePos);
protected:
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
class ZoneViewWidget : public QGraphicsWidget {
Q_OBJECT
private:
ZoneViewZone *zone;
QGraphicsWidget *zoneContainer;
TitleLabel *titleLabel;
QPushButton *closeButton;
QScrollBar *scrollBar;
QCheckBox sortByNameCheckBox;
@ -62,7 +44,7 @@ private slots:
void handleWheelEvent(QGraphicsSceneWheelEvent *event);
void handleScrollBarChange(int value);
void zoneDeleted();
void moveWidget(QPointF scenePos);
void moveEvent(QGraphicsSceneMoveEvent * /* event */);
public:
ZoneViewWidget(Player *_player, CardZone *_origZone, int numberCards = 0, bool _revealZone = false, bool _writeableRevealZone = false, const QList<const ServerInfo_Card *> &cardList = QList<const ServerInfo_Card *>());
ZoneViewZone *getZone() const { return zone; }