QTfrontend/ui/qaspectratiolayout.cpp
changeset 6616 f77bb02b669f
parent 6060 fdfc01419815
equal deleted inserted replaced
6615:65602f1ef0f8 6616:f77bb02b669f
     2  * Copyright (c) 2009 Nokia Corporation.
     2  * Copyright (c) 2009 Nokia Corporation.
     3  */
     3  */
     4 
     4 
     5 #include "qaspectratiolayout.h"
     5 #include "qaspectratiolayout.h"
     6 
     6 
     7 QAspectRatioLayout::QAspectRatioLayout(QWidget* parent, int spacing) : QLayout(parent) {
     7 QAspectRatioLayout::QAspectRatioLayout(QWidget* parent, int spacing) : QLayout(parent)
     8         init(spacing);
     8 {
     9 }
     9     init(spacing);
    10 
    10 }
    11 QAspectRatioLayout::QAspectRatioLayout(int spacing) {
    11 
    12         init(spacing);
    12 QAspectRatioLayout::QAspectRatioLayout(int spacing)
    13 }
    13 {
    14 
    14     init(spacing);
    15 QAspectRatioLayout::~QAspectRatioLayout() {
    15 }
    16         delete item;
    16 
    17         delete lastReceivedRect;
    17 QAspectRatioLayout::~QAspectRatioLayout()
    18         delete _geometry;
    18 {
    19 }
    19     delete item;
    20 
    20     delete lastReceivedRect;
    21 void QAspectRatioLayout::init(int spacing) {
    21     delete _geometry;
    22         item = 0;
    22 }
    23         lastReceivedRect = new QRect(0, 0, 0, 0);
    23 
    24         _geometry = new QRect(0, 0, 0, 0);
    24 void QAspectRatioLayout::init(int spacing)
    25         setSpacing(spacing);
    25 {
       
    26     item = 0;
       
    27     lastReceivedRect = new QRect(0, 0, 0, 0);
       
    28     _geometry = new QRect(0, 0, 0, 0);
       
    29     setSpacing(spacing);
    26 }
    30 }
    27 
    31 
    28 
    32 
    29 /* Adds item if place isn't already taken. */
    33 /* Adds item if place isn't already taken. */
    30 void QAspectRatioLayout::add(QLayoutItem* item) {
    34 void QAspectRatioLayout::add(QLayoutItem* item)
    31         if(!hasItem()) {
    35 {
    32                 replaceItem(item);
    36     if(!hasItem())
    33         }
    37     {
       
    38         replaceItem(item);
       
    39     }
    34 }
    40 }
    35 
    41 
    36 /* Adds item if place isn't already taken. */
    42 /* Adds item if place isn't already taken. */
    37 void QAspectRatioLayout::addItem(QLayoutItem* item) {
    43 void QAspectRatioLayout::addItem(QLayoutItem* item)
    38         if(!hasItem()) {
    44 {
    39                 replaceItem(item);
    45     if(!hasItem())
    40         }
    46     {
       
    47         replaceItem(item);
       
    48     }
    41 }
    49 }
    42 
    50 
    43 /* Adds widget if place isn't already taken. */
    51 /* Adds widget if place isn't already taken. */
    44 void QAspectRatioLayout::addWidget(QWidget* widget) {
    52 void QAspectRatioLayout::addWidget(QWidget* widget)
    45         if(!hasItem()) {
    53 {
    46                 replaceItem(new QWidgetItem(widget));
    54     if(!hasItem())
    47         }
    55     {
       
    56         replaceItem(new QWidgetItem(widget));
       
    57     }
    48 }
    58 }
    49 
    59 
    50 /* Returns the item pointer and dereferences it here. */
    60 /* Returns the item pointer and dereferences it here. */
    51 QLayoutItem* QAspectRatioLayout::take() {
    61 QLayoutItem* QAspectRatioLayout::take()
    52         QLayoutItem* item = 0;
    62 {
    53         if(this->hasItem()) {
    63     QLayoutItem* item = 0;
    54                 item = this->item;
    64     if(this->hasItem())
    55                 this->item = 0;
    65     {
    56         }
    66         item = this->item;
    57         return item;
    67         this->item = 0;
       
    68     }
       
    69     return item;
    58 }
    70 }
    59 
    71 
    60 /* Returns the item pointer and dereferences it here. */
    72 /* Returns the item pointer and dereferences it here. */
    61 QLayoutItem* QAspectRatioLayout::takeAt(int index) {
    73 QLayoutItem* QAspectRatioLayout::takeAt(int index)
    62         if(index != 0) {
    74 {
    63                 return 0;
    75     if(index != 0)
    64         }
    76     {
    65         return this->take();
    77         return 0;
       
    78     }
       
    79     return this->take();
    66 }
    80 }
    67 
    81 
    68 /* Returns the item pointer. */
    82 /* Returns the item pointer. */
    69 QLayoutItem* QAspectRatioLayout::itemAt(int index) const {
    83 QLayoutItem* QAspectRatioLayout::itemAt(int index) const
    70         if(index != 0) {
    84 {
    71                 return 0;
    85     if(index != 0)
    72         }
    86     {
    73         if(hasItem()) {
       
    74                 return this->item;
       
    75         }
       
    76         return 0;
    87         return 0;
       
    88     }
       
    89     if(hasItem())
       
    90     {
       
    91         return this->item;
       
    92     }
       
    93     return 0;
    77 }
    94 }
    78 
    95 
    79 /* Checks if we have an item. */
    96 /* Checks if we have an item. */
    80 bool QAspectRatioLayout::hasItem() const {
    97 bool QAspectRatioLayout::hasItem() const
    81         return this->item != 0;
    98 {
       
    99     return this->item != 0;
    82 }
   100 }
    83 
   101 
    84 /* Returns the count of items which can be either 0 or 1. */
   102 /* Returns the count of items which can be either 0 or 1. */
    85 int QAspectRatioLayout::count() const {
   103 int QAspectRatioLayout::count() const
    86         int returnValue = 0;
   104 {
    87         if(hasItem()) {
   105     int returnValue = 0;
    88                 returnValue = 1;
   106     if(hasItem())
    89         }
   107     {
    90         return returnValue;
   108         returnValue = 1;
       
   109     }
       
   110     return returnValue;
    91 }
   111 }
    92 
   112 
    93 /* Replaces the item with the new and returns the old. */
   113 /* Replaces the item with the new and returns the old. */
    94 QLayoutItem* QAspectRatioLayout::replaceItem(QLayoutItem* item) {
   114 QLayoutItem* QAspectRatioLayout::replaceItem(QLayoutItem* item)
    95         QLayoutItem* old = 0;
   115 {
    96         if(this->hasItem()) {
   116     QLayoutItem* old = 0;
    97                 old = this->item;
   117     if(this->hasItem())
    98         }
   118     {
    99         this->item = item;
   119         old = this->item;
   100         setGeometry(*this->_geometry);
   120     }
   101         return old;
   121     this->item = item;
       
   122     setGeometry(*this->_geometry);
       
   123     return old;
   102 }
   124 }
   103 
   125 
   104 /* Tells which way layout expands. */
   126 /* Tells which way layout expands. */
   105 Qt::Orientations QAspectRatioLayout::expandingDirections() const {
   127 Qt::Orientations QAspectRatioLayout::expandingDirections() const
   106         return Qt::Horizontal | Qt::Vertical;
   128 {
       
   129     return Qt::Horizontal | Qt::Vertical;
   107 }
   130 }
   108 
   131 
   109 /* Tells which size is preferred. */
   132 /* Tells which size is preferred. */
   110 QSize QAspectRatioLayout::sizeHint() const {
   133 QSize QAspectRatioLayout::sizeHint() const
   111         return this->item->minimumSize();
   134 {
       
   135     return this->item->minimumSize();
   112 }
   136 }
   113 
   137 
   114 /* Tells minimum size. */
   138 /* Tells minimum size. */
   115 QSize QAspectRatioLayout::minimumSize() const {
   139 QSize QAspectRatioLayout::minimumSize() const
   116         return this->item->minimumSize();
   140 {
       
   141     return this->item->minimumSize();
   117 }
   142 }
   118 
   143 
   119 /*
   144 /*
   120  * Tells if heightForWidth calculations is handled.
   145  * Tells if heightForWidth calculations is handled.
   121  * It isn't since width isn't enough to calculate
   146  * It isn't since width isn't enough to calculate
   122  * proper size.
   147  * proper size.
   123  */
   148  */
   124 bool QAspectRatioLayout::hasHeightForWidth() const {
   149 bool QAspectRatioLayout::hasHeightForWidth() const
   125         return false;
   150 {
       
   151     return false;
   126 }
   152 }
   127 
   153 
   128 /* Replaces lastReceivedRect. */
   154 /* Replaces lastReceivedRect. */
   129 void QAspectRatioLayout::setLastReceivedRect(const QRect& rect) {
   155 void QAspectRatioLayout::setLastReceivedRect(const QRect& rect)
   130         QRect* oldRect = this->lastReceivedRect;
   156 {
   131         this->lastReceivedRect = new QRect(rect.topLeft(), rect.size());
   157     QRect* oldRect = this->lastReceivedRect;
   132         delete oldRect;
   158     this->lastReceivedRect = new QRect(rect.topLeft(), rect.size());
       
   159     delete oldRect;
   133 }
   160 }
   134 
   161 
   135 /* Returns geometry */
   162 /* Returns geometry */
   136 QRect QAspectRatioLayout::geometry() {
   163 QRect QAspectRatioLayout::geometry()
   137         return QRect(*this->_geometry);
   164 {
       
   165     return QRect(*this->_geometry);
   138 }
   166 }
   139 
   167 
   140 /* Sets geometry to given size. */
   168 /* Sets geometry to given size. */
   141 void QAspectRatioLayout::setGeometry(const QRect& rect) {
   169 void QAspectRatioLayout::setGeometry(const QRect& rect)
   142         /*
   170 {
   143          * We check if the item is set and
   171     /*
   144          * if size is the same previously received.
   172      * We check if the item is set and
   145          * If either is false nothing is done.
   173      * if size is the same previously received.
   146          */
   174      * If either is false nothing is done.
   147         if(!this->hasItem() ||
   175      */
   148            areRectsEqual(*this->lastReceivedRect, rect)) {
   176     if(!this->hasItem() ||
   149                 return;
   177             areRectsEqual(*this->lastReceivedRect, rect))
   150         }
   178     {
   151         /* Replace the last received rectangle. */
   179         return;
   152         setLastReceivedRect(rect);
   180     }
   153         /* Calculate proper size for the item relative to the received size. */
   181     /* Replace the last received rectangle. */
   154         QSize properSize = calculateProperSize(rect.size());
   182     setLastReceivedRect(rect);
   155         /* Calculate center location in the rect and with item size. */
   183     /* Calculate proper size for the item relative to the received size. */
   156         QPoint properLocation = calculateCenterLocation(rect.size(), properSize);
   184     QSize properSize = calculateProperSize(rect.size());
   157         /* Set items geometry */
   185     /* Calculate center location in the rect and with item size. */
   158         this->item->setGeometry(QRect(properLocation, properSize));
   186     QPoint properLocation = calculateCenterLocation(rect.size(), properSize);
   159         QRect* oldRect = this->_geometry;
   187     /* Set items geometry */
   160         /* Cache the calculated geometry. */
   188     this->item->setGeometry(QRect(properLocation, properSize));
   161         this->_geometry = new QRect(properLocation, properSize);
   189     QRect* oldRect = this->_geometry;
   162         delete oldRect;
   190     /* Cache the calculated geometry. */
   163         /* Super classes setGeometry */
   191     this->_geometry = new QRect(properLocation, properSize);
   164         QLayout::setGeometry(*this->_geometry);
   192     delete oldRect;
       
   193     /* Super classes setGeometry */
       
   194     QLayout::setGeometry(*this->_geometry);
   165 }
   195 }
   166 
   196 
   167 /* Takes the shortest side and creates QSize
   197 /* Takes the shortest side and creates QSize
   168  * with the shortest side as width and height. */
   198  * with the shortest side as width and height. */
   169 QSize QAspectRatioLayout::calculateProperSize(QSize from) const {
   199 QSize QAspectRatioLayout::calculateProperSize(QSize from) const
   170         QSize properSize;
   200 {
   171         if(from.height() * 2 < from.width()) {
   201     QSize properSize;
   172                 properSize.setHeight(from.height() - this->margin());
   202     if(from.height() * 2 < from.width())
   173                 properSize.setWidth(from.height() * 2 - this->margin());
   203     {
   174         }
   204         properSize.setHeight(from.height() - this->margin());
   175         else {
   205         properSize.setWidth(from.height() * 2 - this->margin());
   176                 properSize.setWidth(from.width() - this->margin());
   206     }
   177                 properSize.setHeight(from.width() / 2 - this->margin());
   207     else
   178         }
   208     {
   179         return properSize;
   209         properSize.setWidth(from.width() - this->margin());
       
   210         properSize.setHeight(from.width() / 2 - this->margin());
       
   211     }
       
   212     return properSize;
   180 }
   213 }
   181 
   214 
   182 /* Calculates center location from the given height and width for item size. */
   215 /* Calculates center location from the given height and width for item size. */
   183 QPoint QAspectRatioLayout::calculateCenterLocation(QSize from,
   216 QPoint QAspectRatioLayout::calculateCenterLocation(QSize from,
   184                                                    QSize itemSize) const {
   217         QSize itemSize) const
   185         QPoint centerLocation;
   218 {
   186         if((from.width() - itemSize.width()) > 0) {
   219     QPoint centerLocation;
   187                 centerLocation.setX((from.width() - itemSize.width())/2);
   220     if((from.width() - itemSize.width()) > 0)
   188         }
   221     {
   189         if((from.height() - itemSize.height()) > 0) {
   222         centerLocation.setX((from.width() - itemSize.width())/2);
   190                 centerLocation.setY((from.height() - itemSize.height())/2);
   223     }
   191         }
   224     if((from.height() - itemSize.height()) > 0)
   192         return centerLocation;
   225     {
       
   226         centerLocation.setY((from.height() - itemSize.height())/2);
       
   227     }
       
   228     return centerLocation;
   193 }
   229 }
   194 
   230 
   195 /* Compares if two QRects are equal. */
   231 /* Compares if two QRects are equal. */
   196 bool QAspectRatioLayout::areRectsEqual(const QRect& a,
   232 bool QAspectRatioLayout::areRectsEqual(const QRect& a,
   197                                        const QRect& b) const {
   233                                        const QRect& b) const
   198         bool result = false;
   234 {
   199         if(a.x() == b.x() &&
   235     bool result = false;
   200            a.y() == b.y() &&
   236     if(a.x() == b.x() &&
   201            a.height() == b.height() &&
   237             a.y() == b.y() &&
   202            a.width() == b.width()) {
   238             a.height() == b.height() &&
   203                 result = true;
   239             a.width() == b.width())
   204         }
   240     {
   205         return result;
   241         result = true;
   206 }
   242     }
       
   243     return result;
       
   244 }