good-bye QAspectRatioLayout
authorsheepluva
Wed, 05 Jun 2013 14:00:29 +0200
changeset 9128 19596777eb7a
parent 9125 c542f6e3a133
child 9131 07f3bf8d98a3
good-bye QAspectRatioLayout
QTfrontend/ui/qaspectratiolayout.cpp
QTfrontend/ui/qaspectratiolayout.h
QTfrontend/ui/widget/drawmapwidget.cpp
QTfrontend/ui/widget/drawmapwidget.h
project_files/hedgewars.pro
--- a/QTfrontend/ui/qaspectratiolayout.cpp	Tue Jun 04 22:08:17 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,244 +0,0 @@
-/*
- * Copyright (c) 2009 Nokia Corporation.
- */
-
-#include "qaspectratiolayout.h"
-
-QAspectRatioLayout::QAspectRatioLayout(QWidget* parent, int spacing) : QLayout(parent)
-{
-    init(spacing);
-}
-
-QAspectRatioLayout::QAspectRatioLayout(int spacing)
-{
-    init(spacing);
-}
-
-QAspectRatioLayout::~QAspectRatioLayout()
-{
-    delete item;
-    delete lastReceivedRect;
-    delete _geometry;
-}
-
-void QAspectRatioLayout::init(int spacing)
-{
-    item = 0;
-    lastReceivedRect = new QRect(0, 0, 0, 0);
-    _geometry = new QRect(0, 0, 0, 0);
-    setSpacing(spacing);
-}
-
-
-/* Adds item if place isn't already taken. */
-void QAspectRatioLayout::add(QLayoutItem* item)
-{
-    if(!hasItem())
-    {
-        replaceItem(item);
-    }
-}
-
-/* Adds item if place isn't already taken. */
-void QAspectRatioLayout::addItem(QLayoutItem* item)
-{
-    if(!hasItem())
-    {
-        replaceItem(item);
-    }
-}
-
-/* Adds widget if place isn't already taken. */
-void QAspectRatioLayout::addWidget(QWidget* widget)
-{
-    if(!hasItem())
-    {
-        replaceItem(new QWidgetItem(widget));
-    }
-}
-
-/* Returns the item pointer and dereferences it here. */
-QLayoutItem* QAspectRatioLayout::take()
-{
-    QLayoutItem* item = 0;
-    if(this->hasItem())
-    {
-        item = this->item;
-        this->item = 0;
-    }
-    return item;
-}
-
-/* Returns the item pointer and dereferences it here. */
-QLayoutItem* QAspectRatioLayout::takeAt(int index)
-{
-    if(index != 0)
-    {
-        return 0;
-    }
-    return this->take();
-}
-
-/* Returns the item pointer. */
-QLayoutItem* QAspectRatioLayout::itemAt(int index) const
-{
-    if(index != 0)
-    {
-        return 0;
-    }
-    if(hasItem())
-    {
-        return this->item;
-    }
-    return 0;
-}
-
-/* Checks if we have an item. */
-bool QAspectRatioLayout::hasItem() const
-{
-    return this->item != 0;
-}
-
-/* Returns the count of items which can be either 0 or 1. */
-int QAspectRatioLayout::count() const
-{
-    int returnValue = 0;
-    if(hasItem())
-    {
-        returnValue = 1;
-    }
-    return returnValue;
-}
-
-/* Replaces the item with the new and returns the old. */
-QLayoutItem* QAspectRatioLayout::replaceItem(QLayoutItem* item)
-{
-    QLayoutItem* old = 0;
-    if(this->hasItem())
-    {
-        old = this->item;
-    }
-    this->item = item;
-    setGeometry(*this->_geometry);
-    return old;
-}
-
-/* Tells which way layout expands. */
-Qt::Orientations QAspectRatioLayout::expandingDirections() const
-{
-    return Qt::Horizontal | Qt::Vertical;
-}
-
-/* Tells which size is preferred. */
-QSize QAspectRatioLayout::sizeHint() const
-{
-    return this->item->minimumSize();
-}
-
-/* Tells minimum size. */
-QSize QAspectRatioLayout::minimumSize() const
-{
-    return this->item->minimumSize();
-}
-
-/*
- * Tells if heightForWidth calculations is handled.
- * It isn't since width isn't enough to calculate
- * proper size.
- */
-bool QAspectRatioLayout::hasHeightForWidth() const
-{
-    return false;
-}
-
-/* Replaces lastReceivedRect. */
-void QAspectRatioLayout::setLastReceivedRect(const QRect& rect)
-{
-    QRect* oldRect = this->lastReceivedRect;
-    this->lastReceivedRect = new QRect(rect.topLeft(), rect.size());
-    delete oldRect;
-}
-
-/* Returns geometry */
-QRect QAspectRatioLayout::geometry()
-{
-    return QRect(*this->_geometry);
-}
-
-/* Sets geometry to given size. */
-void QAspectRatioLayout::setGeometry(const QRect& rect)
-{
-    /*
-     * We check if the item is set and
-     * if size is the same previously received.
-     * If either is false nothing is done.
-     */
-    if(!this->hasItem() ||
-            areRectsEqual(*this->lastReceivedRect, rect))
-    {
-        return;
-    }
-    /* Replace the last received rectangle. */
-    setLastReceivedRect(rect);
-    /* Calculate proper size for the item relative to the received size. */
-    QSize properSize = calculateProperSize(rect.size());
-    /* Calculate center location in the rect and with item size. */
-    QPoint properLocation = calculateCenterLocation(rect.size(), properSize);
-    /* Set items geometry */
-    this->item->setGeometry(QRect(properLocation, properSize));
-    QRect* oldRect = this->_geometry;
-    /* Cache the calculated geometry. */
-    this->_geometry = new QRect(properLocation, properSize);
-    delete oldRect;
-    /* Super classes setGeometry */
-    QLayout::setGeometry(*this->_geometry);
-}
-
-/* Takes the shortest side and creates QSize
- * with the shortest side as width and height. */
-QSize QAspectRatioLayout::calculateProperSize(QSize from) const
-{
-    QSize properSize;
-    if(from.height() * 2 < from.width())
-    {
-        properSize.setHeight(from.height() - this->margin());
-        properSize.setWidth(from.height() * 2 - this->margin());
-    }
-    else
-    {
-        properSize.setWidth(from.width() - this->margin());
-        properSize.setHeight(from.width() / 2 - this->margin());
-    }
-    return properSize;
-}
-
-/* Calculates center location from the given height and width for item size. */
-QPoint QAspectRatioLayout::calculateCenterLocation(QSize from,
-        QSize itemSize) const
-{
-    QPoint centerLocation;
-    if((from.width() - itemSize.width()) > 0)
-    {
-        centerLocation.setX((from.width() - itemSize.width())/2);
-    }
-    if((from.height() - itemSize.height()) > 0)
-    {
-        centerLocation.setY((from.height() - itemSize.height())/2);
-    }
-    return centerLocation;
-}
-
-/* Compares if two QRects are equal. */
-bool QAspectRatioLayout::areRectsEqual(const QRect& a,
-                                       const QRect& b) const
-{
-    bool result = false;
-    if(a.x() == b.x() &&
-            a.y() == b.y() &&
-            a.height() == b.height() &&
-            a.width() == b.width())
-    {
-        result = true;
-    }
-    return result;
-}
--- a/QTfrontend/ui/qaspectratiolayout.h	Tue Jun 04 22:08:17 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-/*
- * Copyright (c) 2009 Nokia Corporation.
- */
-
-#ifndef QASPECTRATIOLAYOUT_H_
-#define QASPECTRATIOLAYOUT_H_
-
-#include <QLayout>
-#include <QPointer>
-#include <QRect>
-#include <QWidgetItem>
-#include <QLayoutItem>
-
-
-class QAspectRatioLayout : public QLayout
-{
-        Q_OBJECT
-
-    public:
-        QAspectRatioLayout(QWidget* parent, int spacing =-1);
-        QAspectRatioLayout(int spacing = -1);
-        ~QAspectRatioLayout();
-
-        /* Convenience method */
-        virtual void add(QLayoutItem* item);
-
-        /* http://doc.trolltech.com/qlayout.html#addItem */
-        virtual void addItem(QLayoutItem* item);
-        /* http://doc.trolltech.com/qlayout.html#addWidget */
-        virtual void addWidget(QWidget* widget);
-        /* http://doc.trolltech.com/qlayout.html#takeAt */
-        virtual QLayoutItem* takeAt(int index);
-        /* http://doc.trolltech.com/qlayout.html#itemAt */
-        virtual QLayoutItem* itemAt(int index) const;
-        /* http://doc.trolltech.com/qlayout.html#count */
-        virtual int count() const;
-
-        /*
-         * These are ours since we do have only one item.
-         */
-        virtual QLayoutItem* replaceItem(QLayoutItem* item);
-        virtual QLayoutItem* take();
-        virtual bool hasItem() const;
-
-        /* http://doc.trolltech.com/qlayout.html#expandingDirections */
-        virtual Qt::Orientations expandingDirections() const;
-
-        /*
-         * This method contains most of the juice of this article.
-         * http://doc.trolltech.com/qlayoutitem.html#setGeometry
-         */
-        virtual void setGeometry(const QRect& rect);
-        /* http://doc.trolltech.com/qlayoutitem.html#geometry */
-        virtual QRect geometry();
-
-        /* http://doc.trolltech.com/qlayoutitem.html#sizeHint */
-        virtual QSize sizeHint() const;
-        /* http://doc.trolltech.com/qlayout.html#minimumSize */
-        virtual QSize minimumSize() const;
-        /* http://doc.trolltech.com/qlayoutitem.html#hasHeightForWidth */
-        virtual bool hasHeightForWidth() const;
-
-    private:
-        /* Saves the last received rect. */
-        void setLastReceivedRect(const QRect& rect);
-        /* Used to initialize the object. */
-        void init(int spacing);
-        /* Calculates the maximum size for the item from the assigned size. */
-        QSize calculateProperSize(QSize from) const;
-        /* Calculates the center location from the assigned size and
-         * the items size. */
-        QPoint calculateCenterLocation(QSize from, QSize itemSize) const;
-        /* Check if two QRects are equal */
-        bool areRectsEqual(const QRect& a, const QRect& b) const;
-        /* Contains item reference */
-        QLayoutItem* item;
-        /*
-         * Used for caching so we won't do calculations every time
-         * setGeometry is called.
-         */
-        QRect* lastReceivedRect;
-        /* Contains geometry */
-        QRect* _geometry;
-
-};
-
-#endif /* QASPECTRATIOLAYOUT_H_ */
--- a/QTfrontend/ui/widget/drawmapwidget.cpp	Tue Jun 04 22:08:17 2013 +0200
+++ b/QTfrontend/ui/widget/drawmapwidget.cpp	Wed Jun 05 14:00:29 2013 +0200
@@ -62,8 +62,43 @@
 {
     Q_UNUSED(event);
 
+    int height = this->height();
+    int width = this->width();
+
+    if ((m_scene->height() > 0) && (m_scene->width() > 0) && (height > 0))
+    {
+        qreal saspect = m_scene->width() / m_scene->height();
+
+        qreal h = height;
+        qreal w = width;
+        qreal waspect = w / h;
+
+        if (waspect < saspect)
+        {
+            h = w / saspect;
+        }
+        else if (waspect > saspect)
+        {
+            w = saspect * h;
+        }
+
+        int fixedh = (int)h;
+        int fixedw = (int)w;
+
+        if (ui->graphicsView->width() != fixedw)
+        {
+            ui->graphicsView->setFixedWidth(fixedw);
+        }
+
+        if (ui->graphicsView->height() != fixedh)
+        {
+            ui->graphicsView->setFixedHeight(fixedh);
+        }
+
+    }
+
     if(ui->graphicsView && ui->graphicsView->scene())
-        ui->graphicsView->fitInView(ui->graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
+        ui->graphicsView->fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio);
 }
 
 void DrawMapWidget::showEvent(QShowEvent * event)
--- a/QTfrontend/ui/widget/drawmapwidget.h	Tue Jun 04 22:08:17 2013 +0200
+++ b/QTfrontend/ui/widget/drawmapwidget.h	Wed Jun 05 14:00:29 2013 +0200
@@ -25,7 +25,6 @@
 #include <QGraphicsView>
 #include <QLabel>
 
-#include "qaspectratiolayout.h"
 #include "drawmapscene.h"
 
 
@@ -61,12 +60,13 @@
                 QVBoxLayout * vbox = new QVBoxLayout(drawMapWidget);
                 vbox->setMargin(0);
                 lblPoints = new QLabel("0", drawMapWidget);
-                vbox->addWidget(lblPoints);
-                QAspectRatioLayout * arLayout = new QAspectRatioLayout();
-                arLayout->setMargin(0);
+                QLayout * arLayout = new QVBoxLayout();
+                arLayout->setAlignment(Qt::AlignCenter);
                 vbox->addLayout(arLayout);
 
                 graphicsView = new DrawMapView(drawMapWidget);
+                graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+                graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                 arLayout->addWidget(graphicsView);
 
                 retranslateUi(drawMapWidget);
--- a/project_files/hedgewars.pro	Tue Jun 04 22:08:17 2013 +0200
+++ b/project_files/hedgewars.pro	Wed Jun 05 14:00:29 2013 +0200
@@ -44,7 +44,6 @@
     ../QTfrontend/ui/page/pagenetserver.h \
     ../QTfrontend/ui/page/pagegamestats.h \
     ../QTfrontend/ui/dialog/input_ip.h \
-    ../QTfrontend/ui/qaspectratiolayout.h \
     ../QTfrontend/ui/widget/bgwidget.h \
     ../QTfrontend/ui/widget/fpsedit.h \
     ../QTfrontend/ui/widget/FreqSpinBox.h \
@@ -123,7 +122,6 @@
     ../QTfrontend/model/MapModel.cpp \
     ../QTfrontend/model/ThemeModel.cpp \
     ../QTfrontend/model/netserverslist.cpp \
-    ../QTfrontend/ui/qaspectratiolayout.cpp \
     ../QTfrontend/ui/page/pagemain.cpp \
     ../QTfrontend/ui/page/pagetraining.cpp \
     ../QTfrontend/ui/page/pageroomslist.cpp \