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 |
21 |
18 |
|
19 #include "WebChromeContainerSnippet.h" |
22 #include "WebChromeContainerSnippet.h" |
|
23 #include "ChromeItem.h" |
20 #include "WebChromeItem.h" |
24 #include "WebChromeItem.h" |
21 #include "ChromeWidget.h" |
25 #include "ChromeWidget.h" |
22 #include "ChromeRenderer.h" |
26 #include "ChromeRenderer.h" |
23 #include "ChromeSnippet.h" |
27 #include "ChromeSnippet.h" |
24 |
28 |
25 #include <QDebug> |
29 #include <QDebug> |
26 |
30 |
27 namespace GVA { |
31 namespace GVA { |
28 |
32 |
29 WebChromeContainerSnippet::WebChromeContainerSnippet(const QString & elementId, ChromeWidget * chrome, const QRectF& ownerArea, const QWebElement & element, QGraphicsWidget* gwidget) |
33 WebChromeContainerSnippet::WebChromeContainerSnippet(const QString & elementId, ChromeWidget * chrome, const QWebElement & element) |
30 : ChromeSnippet(elementId, chrome, gwidget, element), |
34 : ChromeSnippet(elementId, chrome, 0, element) |
31 m_ownerArea(ownerArea), |
35 ,m_layoutHeight(0) |
32 m_layoutHeight(0) |
|
33 { |
36 { |
34 QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget()); |
37 |
|
38 m_layoutWidth = chrome->layout()->size().width(); |
|
39 |
|
40 //QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget()); |
35 //NB: maybe size should be fixed only in one direction? |
41 //NB: maybe size should be fixed only in one direction? |
36 item->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
42 //item->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
37 //NB: add a method for setting owner area |
43 //NB: add a method for setting owner area |
38 //item->setPreferredSize(m_ownerArea.width(), m_ownerArea.height()); |
44 //item->setPreferredSize(m_ownerArea.width(), m_ownerArea.height()); |
39 //Also resize in case item is not part of anchor layout |
45 //Also resize in case item is not part of anchor layout |
40 //item->resize(item->preferredSize()); |
46 //item->resize(item->preferredSize()); |
41 |
47 |
42 //NB: Linear layout efaults to horizontal: handle vertical layouts too. |
48 //NB: Linear layout efaults to horizontal: handle vertical layouts too. |
43 m_layout = new QGraphicsLinearLayout(); |
49 m_layout = new QGraphicsLinearLayout(); |
44 m_layout->setContentsMargins(0,0,0,0); |
50 m_layout->setContentsMargins(0,0,0,0); |
45 m_layout->setSpacing(0); |
51 m_layout->setSpacing(0); |
46 |
52 |
47 //Add a stretch element at the beginning. |
53 //Add a stretch element at the beginning. |
48 m_layout->addStretch(); |
54 m_layout->addStretch(); |
49 item->setLayout(m_layout); |
55 //item->setLayout(m_layout); |
50 //When chrome is resized owner areas for snippets may change |
56 //When chrome is resized sizes for snippets may change |
51 QObject::connect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea())); |
57 QObject::connect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea())); |
|
58 QObject::connect(m_chrome, SIGNAL(prepareForSizeChange(QSize)), this, SLOT(updateSize(QSize))); |
|
59 |
52 } |
60 } |
53 |
61 |
54 WebChromeContainerSnippet::~WebChromeContainerSnippet() |
62 WebChromeContainerSnippet::~WebChromeContainerSnippet() |
55 { |
63 { |
56 // delete m_layout; |
64 // delete m_layout; |
58 |
66 |
59 void WebChromeContainerSnippet::positionChildren() |
67 void WebChromeContainerSnippet::positionChildren() |
60 { |
68 { |
61 ; //Do nothing since the layout positions children automatically. |
69 ; //Do nothing since the layout positions children automatically. |
62 } |
70 } |
63 |
71 |
|
72 void WebChromeContainerSnippet::setChromeWidget(QGraphicsWidget * widget){ |
|
73 widget->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
|
74 widget->setLayout(m_layout); |
|
75 ChromeSnippet::setChromeWidget(widget); |
|
76 } |
|
77 |
64 void WebChromeContainerSnippet::addChild(ChromeSnippet * child) |
78 void WebChromeContainerSnippet::addChild(ChromeSnippet * child) |
65 { |
79 { |
66 //Prevent layout from stretching the child widgets. NB: Revisit this to make configurable from chrome? |
80 //Prevent layout from stretching the child widgets. NB: Revisit this to make configurable from chrome? |
67 child->widget()->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
81 child->widget()->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
68 //child->widget()->setParentItem(this->widget()); // Shouldn't be needed, right? |
82 //child->widget()->setParentItem(this->widget()); // Shouldn't be needed, right? |
69 m_layout->addItem(child->widget()); |
83 m_layout->addItem(child->widget()); |
70 //Add a stretch after each element so the layout looks like this: |stretch|item|stretch|item . . . . stretch|item| |
84 //Add a stretch after each element so the layout looks like this: |stretch|item|stretch|item . . . . stretch|item| |
71 m_layout->addStretch(); |
85 m_layout->addStretch(); |
72 |
86 |
73 // If child is not a container itself, connect to its mouseEvent |
|
74 WebChromeContainerSnippet * s = qobject_cast <WebChromeContainerSnippet * >(child); |
|
75 if (!s ) { |
|
76 //qDebug() << __PRETTY_FUNCTION__ << s << child->elementId(); |
|
77 // Connect to mouse events of snippets |
|
78 WebChromeItem * item = static_cast<WebChromeItem*> (child->widget()); |
|
79 connect(item, SIGNAL(mouseEvent(QEvent::Type)), this, SIGNAL(snippetMouseEvent(QEvent::Type))); |
|
80 } |
|
81 qreal childHeight = child->widget()->size().height(); |
87 qreal childHeight = child->widget()->size().height(); |
82 if(childHeight > m_layoutHeight){ |
88 if (childHeight > m_layoutHeight){ |
83 m_layoutHeight = childHeight; |
89 m_layoutHeight = childHeight; |
84 updateOwnerArea(); |
90 |
|
91 updateSizes(); |
85 } |
92 } |
|
93 emit childAdded(child); |
|
94 |
|
95 m_layout->activate(); |
86 } |
96 } |
87 |
97 |
88 void WebChromeContainerSnippet:: updateOwnerArea() |
98 void WebChromeContainerSnippet:: updateOwnerArea() |
89 { |
99 { |
90 QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget()); |
100 updateSizes(); |
91 //Resize the item |
101 QObject::disconnect(m_chrome->renderer(), SIGNAL(chromeResized()), this, SLOT(updateOwnerArea())); |
92 m_ownerArea = m_chrome->getSnippetRect(m_elementId); |
102 |
93 item->setPreferredSize(m_ownerArea.width(), m_layoutHeight); |
|
94 //Also resize in case item is not part of anchor layout |
|
95 item->resize(item->preferredSize()); |
|
96 } |
103 } |
97 |
104 |
98 void WebChromeContainerSnippet::setLayoutHeight(int height){ |
105 void WebChromeContainerSnippet::setLayoutHeight(int height){ |
99 if(m_layoutHeight != height){ |
106 if (m_layoutHeight != height){ |
100 m_layoutHeight = height; |
107 m_layoutHeight = height; |
101 updateOwnerArea(); |
108 updateSizes(); |
102 } |
109 } |
103 } |
110 } |
104 |
111 |
|
112 void WebChromeContainerSnippet::setLayoutWidth(qreal width, bool update){ |
|
113 if (m_layoutWidth != width){ |
|
114 m_layoutWidth = width; |
|
115 if (update ) { |
|
116 updateSizes(); |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 void WebChromeContainerSnippet::updateSize(QSize size ) { |
|
122 |
|
123 Q_UNUSED(size); |
|
124 // m_layoutWidth should have been set by now through derived classes. We don't want to set it |
|
125 // here as that would overwrite any width set before. For example, width of the middle snippet |
|
126 // in toolbar is set by the main toolbar. |
|
127 updateSizes(); |
|
128 } |
|
129 |
|
130 void WebChromeContainerSnippet::updateSizes() { |
|
131 |
|
132 QGraphicsWidget * item = static_cast<QGraphicsWidget*> (widget()); |
|
133 //Resize the item |
|
134 item->setPreferredSize(m_layoutWidth, m_layoutHeight); |
|
135 //Also resize in case item is not part of anchor layout |
|
136 item->resize(item->preferredSize()); |
|
137 |
|
138 } |
|
139 |
105 } // endof namespace GVA |
140 } // endof namespace GVA |