ginebra2/SlidingWidget.cpp
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
     4 *
     9 * Initial Contributors:
     5 * This program is free software: you can redistribute it and/or modify
    10 * Nokia Corporation - initial contribution.
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
    11 *
     8 *
    12 * Contributors:
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
    15 *
    19 *
    16 */
    20 */
    17 
       
    18 
    21 
    19 #include "SlidingWidget.h"
    22 #include "SlidingWidget.h"
    20 #include "ChromeSnippet.h"
    23 #include "ChromeSnippet.h"
    21 
    24 
    22 #include <QDebug>
    25 #include <QDebug>
    29     : QGraphicsWidget(parent),
    32     : QGraphicsWidget(parent),
    30       m_top(0),
    33       m_top(0),
    31       m_window(0),
    34       m_window(0),
    32       m_bottom(0),
    35       m_bottom(0),
    33       m_slideMax(0),
    36       m_slideMax(0),
    34       m_slidePos(0)
    37       m_slidePos(0),
       
    38       m_shrinked(0),
       
    39       m_shrinkMax(0),
       
    40       m_windowSize(QSizeF())
    35   {
    41   {
    36     //Clip child item so that it can "slide" out of view
    42     //Clip child item so that it can "slide" out of view
    37     setFlags(QGraphicsItem::ItemClipsChildrenToShape);
    43     setFlags(QGraphicsItem::ItemClipsChildrenToShape);
    38     //setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    44     //setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    39     m_layout = new QGraphicsAnchorLayout();
    45     m_layout = new QGraphicsAnchorLayout();
    42     setLayout(m_layout);
    48     setLayout(m_layout);
    43   }
    49   }
    44 
    50 
    45   SlidingWidget:: ~SlidingWidget()
    51   SlidingWidget:: ~SlidingWidget()
    46   {
    52   {
       
    53     // delete m_layout;
    47   }
    54   }
    48 
    55 
    49   void SlidingWidget::resizeEvent(QGraphicsSceneResizeEvent *ev)
    56   void SlidingWidget::resizeEvent(QGraphicsSceneResizeEvent *ev)
    50   {
    57   {
    51     qDebug() << "SlidingWidget::resizeEvent: " << ev->newSize();
    58     if (m_window)
    52     if(m_window)
       
    53       m_window->resize(ev->newSize());
    59       m_window->resize(ev->newSize());
       
    60       m_windowSize = ev->newSize();
       
    61       m_shrinked = 0;
    54       //m_window->resize(ev->newSize().width(), m_window->size().height());
    62       //m_window->resize(ev->newSize().width(), m_window->size().height());
    55     QGraphicsWidget::resizeEvent(ev);
    63     QGraphicsWidget::resizeEvent(ev);
       
    64   }
       
    65 
       
    66   /* shrink the viewport at the bottom, to show content on top of toolbar */
       
    67   qreal SlidingWidget::shrink(qreal delta)
       
    68   {
       
    69 
       
    70       qreal newDelta = m_shrinked + delta;
       
    71       if (newDelta < -m_shrinkMax) {
       
    72           newDelta = -m_shrinkMax;
       
    73       }
       
    74       if (newDelta > 0) {
       
    75           newDelta = 0;
       
    76       }
       
    77       delta = newDelta - m_shrinked;
       
    78       if (delta != 0) {
       
    79           m_window->resize(m_window->size().width(), m_window->size().height() + delta);
       
    80           m_shrinked = newDelta;
       
    81       }
       
    82       return delta;
       
    83 
    56   }
    84   }
    57 
    85 
    58   qreal SlidingWidget::slide(qreal delta)
    86   qreal SlidingWidget::slide(qreal delta)
    59   {
    87   {
    60 
    88 
    61     qreal newPos = m_slidePos + delta;
    89     qreal newPos = m_slidePos + delta;
    62 
    90 
    63     //qDebug() << "SlidingWidget::slide: delta: " << delta << " m_slidePos: " << m_slidePos << " newPos: " << newPos;
    91     //qDebug() << "SlidingWidget::slide: delta: " << delta << " m_slidePos: " << m_slidePos << " newPos: " << newPos;
    64 
    92 
    65     if(newPos < 0) {
    93     if (newPos < 0) {
    66       if(m_slidePos == 0)
    94       if (m_slidePos == 0)
    67 	return 0;
    95     return 0;
    68       delta = -m_slidePos;
    96       delta = -m_slidePos;
    69       m_slidePos = 0;
    97       m_slidePos = 0;
    70     }
    98     }
    71 
    99 
    72     else if(newPos > m_slideMax){
   100     else if (newPos > m_slideMax){
    73       if(m_slidePos == m_slideMax)
   101       if (m_slidePos == m_slideMax)
    74 	return 0;
   102     return 0;
    75       delta = m_slideMax - m_slidePos;
   103       delta = m_slideMax - m_slidePos;
    76       m_slidePos = m_slideMax;
   104       m_slidePos = m_slideMax;
    77     }
   105     }
    78 
   106 
    79     else m_slidePos = newPos;
   107     else m_slidePos = newPos;
    89     return delta;
   117     return delta;
    90   }
   118   }
    91 
   119 
    92   void SlidingWidget::setWindow(QGraphicsWidget * window)
   120   void SlidingWidget::setWindow(QGraphicsWidget * window)
    93   {
   121   {
    94     if(m_window)
   122     if (m_window)
    95       m_window->setParentItem(0);
   123       m_window->setParentItem(0);
    96     m_window = window;
   124     m_window = window;
    97     m_window->setParentItem(this);
   125     m_window->setParentItem(this);
    98     m_window->resize(size());
   126     /* set the window size to saved value, before the viewport is shrinked */
    99     if(m_top) {
   127     m_window->resize(m_windowSize);
   100     	slide(0);
   128     m_shrinked = 0;
   101     	QGraphicsWidget* item=m_top;
   129 
   102     	attachItem(item);
   130     if (m_top) {
       
   131         slide(0);
       
   132         QGraphicsWidget* item=m_top;
       
   133         attachItem(item);
   103     }
   134     }
   104     else
   135     else
   105       m_window->setPos(0,0);
   136       m_window->setPos(0,0);
   106     //m_layout->addAnchors(m_window, m_layout, Qt::Horizontal);
   137     //m_layout->addAnchors(m_window, m_layout, Qt::Horizontal);
   107   }
   138   }
   108 
   139 
   109   void SlidingWidget::attachItem(QGraphicsWidget * item)
   140   void SlidingWidget::attachItem(QGraphicsWidget * item)
   110   {
   141   {
   111     if(m_top)
   142     if (m_top)
   112       detachItem(m_top);
   143       detachItem(m_top);
   113     m_top = item;
   144     m_top = item;
   114     m_top->setParentItem(this);
   145     m_top->setParentItem(this);
   115     m_top->setPos(0,0);
   146     m_top->setPos(0,0);
   116     m_slideMax =  m_top->size().height();
   147     m_slideMax =  m_top->size().height();
   117     m_slidePos = m_slideMax;
   148     m_slidePos = m_slideMax;
   118     if(m_window)
   149     if (m_window)
   119       m_window->setPos(0, m_top->size().height());
   150       m_window->setPos(0, m_top->size().height());
   120   }
   151   }
   121 
   152 
   122 
   153 
   123   void SlidingWidget::detachItem(QGraphicsWidget * item)
   154   void SlidingWidget::detachItem(QGraphicsWidget * item)
   124   {
   155   {
   125     if(m_top != item)
   156     if (m_top != item)
   126       return;
   157       return;
   127     m_top->setParentItem(0);
   158     m_top->setParentItem(0);
   128     m_top = 0;
   159     m_top = 0;
   129     if(m_window)
   160     if (m_window)
   130       m_window->setPos(0,0);
   161       m_window->setPos(0,0);
   131   }
   162   }
   132 
   163 
   133 
   164 
   134 } // end of namespace GVA
   165 } // end of namespace GVA