ginebra2/ChromeLayout.cpp
changeset 6 1c3b8676e58c
child 16 3c88a81ff781
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     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.
       
     8 *
       
     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 *
       
    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:
       
    19 *
       
    20 */
       
    21 
       
    22 #include <QList>
       
    23 #include <QDebug>
       
    24 #include <QGraphicsSceneContextMenuEvent>
       
    25 
       
    26 #include "ChromeSnippet.h"
       
    27 #include "WebChromeContainerSnippet.h"
       
    28 #include "ChromeLayout.h"
       
    29 #include "SlidingWidget.h"
       
    30 #include "ViewController.h"
       
    31 #include "ObjectCharm.h"
       
    32 #include "bedrockprovisioning.h"
       
    33 
       
    34 namespace GVA {
       
    35 
       
    36 // -----------------------------
       
    37 
       
    38   ChromeLayout::ChromeLayout(QGraphicsItem * parent, Qt::WindowFlags wFlags)
       
    39     : QGraphicsWidget(parent, wFlags),
       
    40       m_topBar(0),
       
    41       m_bottomBar(0),
       
    42       m_leftBar(0),
       
    43       m_rightBar(0),
       
    44       m_aspect(portrait),
       
    45       m_bottomBarHeight(0)
       
    46   {
       
    47     m_layout = new QGraphicsAnchorLayout();
       
    48     m_layout->setContentsMargins(0,0,0,0);
       
    49     m_layout->setSpacing(0);
       
    50     setLayout(m_layout);
       
    51     addAnchors();
       
    52     m_viewPort = new SlidingWidget(this);
       
    53     m_layout->addAnchor(m_viewPort, Qt::AnchorTop, m_topBar, Qt::AnchorBottom);
       
    54     m_layout->addAnchor(m_viewPort, Qt::AnchorBottom, m_bottomBar, Qt::AnchorTop);
       
    55     m_layout->addAnchor(m_viewPort, Qt::AnchorLeft, m_leftBar, Qt::AnchorRight);
       
    56     m_layout->addAnchor(m_viewPort, Qt::AnchorRight, m_rightBar, Qt::AnchorLeft);
       
    57   }
       
    58   
       
    59   ChromeLayout::~ChromeLayout()
       
    60   {
       
    61     delete m_bottomBar;
       
    62     delete m_topBar;
       
    63     delete m_leftBar;
       
    64     delete m_rightBar;
       
    65     delete m_viewPort;
       
    66   }
       
    67   
       
    68   void ChromeLayout::addAnchors(){
       
    69     if (!m_bottomBar){
       
    70       m_bottomBar = new QGraphicsWidget(this);
       
    71       m_bottomBar->setPreferredHeight(0);
       
    72       m_bottomBar->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
    73       m_layout->addAnchor(m_bottomBar, Qt::AnchorBottom, m_layout, Qt::AnchorBottom);
       
    74       m_layout->addAnchors(m_bottomBar, m_layout, Qt::Horizontal);
       
    75     }
       
    76     if (!m_topBar){
       
    77       m_topBar = new QGraphicsWidget(this);
       
    78       m_topBar->setPreferredHeight(0);
       
    79       m_topBar->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
       
    80       m_layout->addAnchor(m_topBar, Qt::AnchorTop, m_layout, Qt::AnchorTop);
       
    81       m_layout->addAnchors(m_topBar, m_layout, Qt::Horizontal);
       
    82     }
       
    83     if (!m_leftBar){
       
    84       m_leftBar = new QGraphicsWidget(this);
       
    85       m_leftBar->setPreferredWidth(0);
       
    86       m_leftBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
    87       m_layout->addAnchor(m_leftBar, Qt::AnchorLeft, m_layout, Qt::AnchorLeft);
       
    88       m_layout->addAnchors(m_leftBar, m_layout, Qt::Vertical);
       
    89     }
       
    90     if (!m_rightBar){
       
    91       m_rightBar = new QGraphicsWidget(this);
       
    92       m_rightBar->setPreferredWidth(0);
       
    93       m_rightBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
       
    94       m_layout->addAnchor(m_rightBar, Qt::AnchorRight, m_layout, Qt::AnchorRight);
       
    95       m_layout->addAnchors(m_rightBar, m_layout, Qt::Vertical);
       
    96     }
       
    97   }
       
    98   
       
    99   void ChromeLayout::resizeEvent(QGraphicsSceneResizeEvent *ev)
       
   100   {
       
   101     emit resizing(ev->newSize());
       
   102     int aspect = m_aspect;
       
   103     m_aspect = (ev->newSize().width() > ev->newSize().height())?landscape:portrait;
       
   104     if (m_aspect != aspect) {
       
   105       QString mode = (m_aspect == landscape ? "Landscape" : "Portrait");
       
   106       emit aspectChanged(m_aspect);
       
   107     }
       
   108     QGraphicsWidget::resizeEvent(ev);
       
   109   }
       
   110   
       
   111   void ChromeLayout::addView(ControllableViewBase * controllableView){
       
   112     if (controllableView->widget()) {
       
   113       m_viewPort->setWindow(controllableView->widget());
       
   114       controllableView->widget()->setFocusPolicy(Qt::ClickFocus); //NB: Is this needed? Does it break anything?
       
   115       controllableView->widget()->setZValue(-1.0); //Make sure we are behind any center anchored snippets
       
   116     }
       
   117   }
       
   118   
       
   119   void ChromeLayout::anchorToView(ChromeSnippet* snippet, const QString& where)
       
   120   {
       
   121     Q_UNUSED(where)
       
   122       m_viewPort->attachItem(snippet->widget());
       
   123   }
       
   124   
       
   125   void ChromeLayout::detachFromView(ChromeSnippet* snippet, const QString& where)
       
   126   {
       
   127     Q_UNUSED(where)
       
   128       m_viewPort->detachItem(snippet->widget());
       
   129   }
       
   130   
       
   131   void ChromeLayout::anchorTogether(ChromeSnippet * first, ChromeSnippet * second, qreal x, qreal y)
       
   132   {
       
   133       first->widget()->setParentItem(second->widget());
       
   134       first->widget()->setPos(x,y);
       
   135   }
       
   136   
       
   137   void ChromeLayout::unAnchor(ChromeSnippet* snippet)
       
   138   {
       
   139     snippet->widget()->setParentItem(0);
       
   140     snippet->widget()->setParentItem(this);
       
   141   }
       
   142   
       
   143   qreal ChromeLayout::shrinkView(qreal delta)
       
   144   {
       
   145     m_viewPort->setShrinkMax(bottomBarHeight());
       
   146     return m_viewPort->shrink(delta);
       
   147   }
       
   148   
       
   149   qreal ChromeLayout::slideView(qreal delta)
       
   150   {
       
   151     return m_viewPort->slide(delta);
       
   152   }
       
   153   
       
   154   //Check to see if snippet has an anchor and lay it out accordingly.
       
   155   //Otherwise snippets determine their own positions from html
       
   156   
       
   157   void ChromeLayout::anchorSnippet(ChromeSnippet * snippet)
       
   158   {
       
   159     if (snippet->anchor() == anchorBottom){
       
   160       if (snippet->hidesContent()){
       
   161 	m_layout->addAnchor(snippet->widget(), Qt::AnchorBottom, m_bottomBar, Qt::AnchorTop);
       
   162 	m_layout->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, m_bottomBar, Qt::AnchorHorizontalCenter);
       
   163       } else {
       
   164 	snippet->widget()->setParentItem(m_bottomBar);
       
   165 	snippet->widget()->setY(-snippet->anchorOffset());
       
   166       }
       
   167     }
       
   168     else if (snippet->anchor() == anchorTop){
       
   169       if (snippet->hidesContent()){
       
   170 	m_layout->addAnchor(snippet->widget(),Qt::AnchorTop, m_topBar, Qt::AnchorBottom);
       
   171 	m_layout->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, m_topBar, Qt::AnchorHorizontalCenter);
       
   172       }
       
   173       else {
       
   174 	snippet->widget()->setParentItem(m_topBar);
       
   175 	snippet->widget()->setY(snippet->anchorOffset());
       
   176       }
       
   177     }
       
   178     else if (snippet->anchor() == anchorLeft){
       
   179       if (snippet->hidesContent())
       
   180 	m_layout->addAnchor(snippet->widget(),Qt::AnchorLeft, m_leftBar, Qt::AnchorRight);
       
   181       else {
       
   182 	snippet->widget()->setParentItem(m_leftBar);
       
   183 	snippet->widget()->setX(snippet->anchorOffset());
       
   184       }
       
   185     }
       
   186     else if (snippet->anchor() == anchorRight){
       
   187       if (snippet->hidesContent())
       
   188 	m_layout->addAnchor(snippet->widget(),Qt::AnchorRight, m_rightBar, Qt::AnchorLeft);
       
   189       else {
       
   190 	snippet->widget()->setParentItem(m_rightBar);
       
   191 	snippet->widget()->setX(-snippet->anchorOffset());
       
   192       }
       
   193     }
       
   194     else if (snippet->anchor() == anchorCenter) {
       
   195       snippet->widget()->setParentItem(m_viewPort);
       
   196       snippet->widget()->setZValue(0.0);
       
   197       QGraphicsAnchorLayout * vl = static_cast<QGraphicsAnchorLayout*>(m_viewPort->layout());
       
   198       vl->addAnchor(snippet->widget(), Qt::AnchorVerticalCenter, vl, Qt::AnchorVerticalCenter);
       
   199       vl->addAnchor(snippet->widget(), Qt::AnchorHorizontalCenter, vl, Qt::AnchorHorizontalCenter);
       
   200     }
       
   201     else if (snippet->anchor() == anchorTopLeft){
       
   202       m_layout->addCornerAnchors(snippet->widget(), Qt::TopLeftCorner, m_layout, Qt::TopLeftCorner);
       
   203     }
       
   204     else if (snippet->anchor() == anchorTopRight)
       
   205       m_layout->addCornerAnchors(snippet->widget(), Qt::TopRightCorner, m_layout, Qt::TopRightCorner);
       
   206     else if (snippet->anchor() == anchorBottomLeft)
       
   207       m_layout->addCornerAnchors(snippet->widget(), Qt::BottomLeftCorner, m_layout, Qt::BottomLeftCorner);
       
   208     else if (snippet->anchor() == anchorBottomRight)
       
   209       m_layout->addCornerAnchors(snippet->widget(), Qt::BottomRightCorner, m_layout, Qt::BottomRightCorner);
       
   210   }
       
   211   
       
   212   void ChromeLayout:: addSnippet(ChromeSnippet * snippet, ChromeSnippet * container )
       
   213   {
       
   214     snippet->widget()->setParentItem(this);
       
   215     if (container){
       
   216 	container->addChild(snippet);
       
   217     }
       
   218     anchorSnippet(snippet);
       
   219   }
       
   220   
       
   221   // Call after a snippet changes to visible state
       
   222     
       
   223   void ChromeLayout::snippetShown(ChromeSnippet * snippet)
       
   224   {
       
   225     if (snippet->hidesContent()) {
       
   226       if (snippet->anchor() == anchorBottom) {
       
   227 	/* snippet->widget()->size().height() is zero for  WebChromeContainerSnippet
       
   228 	   when default visible container snippet is shown, get ownerArea height instead */
       
   229 	WebChromeContainerSnippet * s = dynamic_cast<WebChromeContainerSnippet *>(snippet);
       
   230 	if (!s) {
       
   231 	  m_bottomBarHeight = snippet->widget()->size().height();
       
   232 	}
       
   233 	else {
       
   234 	  m_bottomBarHeight = s->ownerArea().height();
       
   235 	}
       
   236       }
       
   237       return;
       
   238     }
       
   239     if (snippet->anchor() == anchorBottom)
       
   240       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() + snippet->widget()->preferredHeight());
       
   241     else if (snippet->anchor() == anchorTop)
       
   242       m_topBar->setPreferredHeight(m_topBar->preferredHeight() + snippet->widget()->preferredHeight());
       
   243     else if (snippet->anchor() == anchorLeft){
       
   244       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() + snippet->widget()->preferredWidth());
       
   245     }
       
   246     else if (snippet->anchor() == anchorRight){
       
   247       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() + snippet->widget()->preferredWidth());
       
   248     }
       
   249   }
       
   250   
       
   251   // Call before a snippet changes to invisible state
       
   252     
       
   253   void ChromeLayout::snippetHiding(ChromeSnippet * snippet)
       
   254   {
       
   255     if (snippet->hidesContent())
       
   256       return;
       
   257     if (snippet->anchor() == anchorBottom)
       
   258       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() - snippet->widget()->preferredHeight());
       
   259     else if (snippet->anchor() == anchorTop)
       
   260       m_topBar->setPreferredHeight(m_topBar->preferredHeight() - snippet->widget()->preferredHeight());
       
   261     else if (snippet->anchor() == anchorLeft){
       
   262       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() - snippet->widget()->preferredWidth());
       
   263     }
       
   264     else if (snippet->anchor() == anchorRight){
       
   265       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() - snippet->widget()->preferredWidth());
       
   266     }
       
   267   }
       
   268   
       
   269   QString ChromeLayout::getDisplayMode() const {
       
   270     return (m_aspect==portrait)?"portrait":"landscape";
       
   271   }
       
   272   
       
   273   void ChromeLayout::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
       
   274   {
       
   275     event->accept();
       
   276   }
       
   277 
       
   278   //NB: Deprecate. This really shouldn't be necessary: anchor bars should be implemented as an expanding
       
   279   //widget class.
       
   280 
       
   281   void ChromeLayout::adjustAnchorOffset(ChromeSnippet * snippet, qreal delta)
       
   282   {
       
   283     if (snippet->anchor() == anchorBottom)
       
   284       m_bottomBar->setPreferredHeight(m_bottomBar->preferredHeight() + delta);
       
   285     else if (snippet->anchor() == anchorTop)
       
   286       m_topBar->setPreferredHeight(m_topBar->preferredHeight() + delta);
       
   287     else if (snippet->anchor() == anchorLeft){
       
   288       m_leftBar->setPreferredWidth(m_leftBar->preferredWidth() + delta);
       
   289     }
       
   290     else if (snippet->anchor() == anchorRight){
       
   291       m_rightBar->setPreferredWidth(m_rightBar->preferredWidth() + delta);
       
   292     }
       
   293   }
       
   294 
       
   295   void ChromeLayout::setScene(QGraphicsScene *scene)
       
   296   {
       
   297     m_scene = scene;
       
   298     //This is the root of the scene hierarchy
       
   299     m_scene->addItem(this);
       
   300 
       
   301     // For testing, turn on red dot that appears in response to various mouse events.
       
   302     if(BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsInt("EnableTouchCircle", false)) {
       
   303         new TouchCircleCharm(m_scene, this);
       
   304     }
       
   305 
       
   306   }
       
   307   
       
   308 } // endof namespace GVA