|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
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 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "hsdomainmodeldatastructures.h" |
|
19 #include "hspagenewwidgetlayout.h" |
|
20 #include "hsscene.h" |
|
21 #include "hspage.h" |
|
22 #include "hsdatabase.h" |
|
23 #include "hswidgethost.h" |
|
24 #include "hswidgethostvisual.h" |
|
25 #include "hswallpaper.h" |
|
26 #include "hswidgetpositioningonwidgetadd.h" |
|
27 #include "hswidgetpositioningonorientationchange.h" |
|
28 #include "hsconfiguration.h" |
|
29 #include "hsgui.h" |
|
30 |
|
31 |
|
32 /*! |
|
33 \class HsPageNewWidgetLayout |
|
34 \ingroup group_hsdomainmodel |
|
35 \brief Represents a page in the framework. |
|
36 HsPage is a parent for a group of widgets. HsPage can have a wallpaper. |
|
37 */ |
|
38 |
|
39 /*! |
|
40 Constructor. |
|
41 |
|
42 \a parent Owner. |
|
43 */ |
|
44 HsPageNewWidgetLayout::HsPageNewWidgetLayout(const QPointF &touchPoint, |
|
45 QGraphicsLayoutItem *parent) |
|
46 : QGraphicsLayout(parent), |
|
47 mTouchPoint(touchPoint) |
|
48 { |
|
49 mSize = HsGui::instance()->layoutRect().size(); |
|
50 } |
|
51 |
|
52 /*! |
|
53 Destructor. |
|
54 */ |
|
55 HsPageNewWidgetLayout::~HsPageNewWidgetLayout() |
|
56 { |
|
57 } |
|
58 |
|
59 /*! |
|
60 Returns children count. |
|
61 */ |
|
62 int HsPageNewWidgetLayout::count() const |
|
63 { |
|
64 return mNewWidgets.count(); |
|
65 } |
|
66 |
|
67 /*! |
|
68 Returns item index of \a i. |
|
69 */ |
|
70 QGraphicsLayoutItem *HsPageNewWidgetLayout::itemAt(int i) const |
|
71 { |
|
72 return mNewWidgets.at(i)->visual(); |
|
73 } |
|
74 |
|
75 /*! |
|
76 Removes item \a index. |
|
77 */ |
|
78 void HsPageNewWidgetLayout::removeAt(int index) |
|
79 { |
|
80 mNewWidgets.removeAt(index); |
|
81 } |
|
82 |
|
83 /*! |
|
84 Size hint. |
|
85 */ |
|
86 QSizeF HsPageNewWidgetLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
87 { |
|
88 switch (which) { |
|
89 case Qt::MinimumSize: |
|
90 case Qt::PreferredSize: |
|
91 return mSize; |
|
92 case Qt::MaximumSize: |
|
93 return mSize; |
|
94 default: |
|
95 break; |
|
96 } |
|
97 return constraint; |
|
98 } |
|
99 |
|
100 /*! |
|
101 Recalculates children positions. |
|
102 */ |
|
103 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect) |
|
104 { |
|
105 QGraphicsLayout::setGeometry(rect); |
|
106 |
|
107 #ifdef HSWIDGETORGANIZER_ALGORITHM |
|
108 // sort new widgets in order |
|
109 if (mNewWidgets.count() > 1) { |
|
110 // TODO: read from configuration? or just use height for portrait and width for landscape (currently only height is used) |
|
111 sortOrder order(height); |
|
112 mNewWidgets = sortWidgets(order); |
|
113 } |
|
114 #endif |
|
115 |
|
116 // get rects for new widgets |
|
117 QList<QRectF> newRects; |
|
118 foreach (HsWidgetHost *newWidget, mNewWidgets) { |
|
119 newRects << QRectF(QPointF(), newWidget->visual()->preferredSize()); |
|
120 } |
|
121 |
|
122 /* if there is touch point defined (widget added from context menu) |
|
123 then there is only one widget in the list |
|
124 -> set widget center point to this touch point |
|
125 */ |
|
126 if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) { |
|
127 QRectF widgetRect = newRects.at(0); |
|
128 widgetRect.moveCenter(mTouchPoint); |
|
129 widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect)); |
|
130 mNewWidgets.at(0)->visual()->setGeometry(widgetRect); |
|
131 /* we have to save widget presentation data here after drawing |
|
132 to get correct position for later use |
|
133 */ |
|
134 mNewWidgets.at(0)->savePresentation(); |
|
135 } |
|
136 // otherwise calculate widget positions with algorithm |
|
137 else { |
|
138 // get page rect |
|
139 QRectF pageRect = HsScene::instance()->activePage()->contentGeometry(); |
|
140 |
|
141 // scan existing widgets rects |
|
142 QList<HsWidgetHost*> existingWidgets; |
|
143 existingWidgets = HsScene::instance()->activePage()->widgets(); |
|
144 QList<QRectF> existingRects; |
|
145 foreach (HsWidgetHost *widget, existingWidgets) { |
|
146 if (!mNewWidgets.contains(widget)) { |
|
147 existingRects << QRectF(widget->visual()->pos(), widget->visual()->preferredSize()); |
|
148 } |
|
149 } |
|
150 |
|
151 // calculate new widget positions with "stuck 'em all"-algorithm |
|
152 HsWidgetPositioningOnWidgetAdd *algorithm = |
|
153 HsWidgetPositioningOnWidgetAdd::instance(); |
|
154 QList<QRectF> calculatedRects = |
|
155 algorithm->convert(pageRect, existingRects, newRects, QPointF()); |
|
156 |
|
157 for ( int i=0; i<mNewWidgets.count(); i++) { |
|
158 mNewWidgets.at(i)->visual()->setGeometry(calculatedRects.at(i)); |
|
159 mNewWidgets.at(i)->savePresentation(); |
|
160 } |
|
161 } |
|
162 } |
|
163 |
|
164 /*! |
|
165 Adds item to layout. |
|
166 */ |
|
167 void HsPageNewWidgetLayout::addItem(HsWidgetHost *item) |
|
168 { |
|
169 mNewWidgets.append(item); |
|
170 } |
|
171 |
|
172 #ifdef HSWIDGETORGANIZER_ALGORITHM |
|
173 // TODO: sorting should be done in algorithm class, make widget<->rect mapping here and move sortWidgets function to algorithm side |
|
174 /*! |
|
175 Sorts widgets in height/width order |
|
176 */ |
|
177 QList<HsWidgetHost*> HsPageNewWidgetLayout::sortWidgets(sortOrder order) |
|
178 { |
|
179 QList<HsWidgetHost*> tmpWidgets; |
|
180 |
|
181 for ( int i = 0; i < mNewWidgets.count(); i++) { |
|
182 int index = 0; |
|
183 // add first widget to sorted list |
|
184 if (i == 0) { |
|
185 tmpWidgets << mNewWidgets.at(i); |
|
186 } else { |
|
187 // go through existing widgets in the sorted list |
|
188 for ( int j = 0; j < tmpWidgets.count(); j++) { |
|
189 // sort widgets in height order |
|
190 if (order == height) { |
|
191 /* if widgets heigth is smaller on already |
|
192 existing ones in the list -> increment index |
|
193 */ |
|
194 if (mNewWidgets.at(i)->visual()->preferredHeight() <= tmpWidgets.at(j)->visual()->preferredHeight()) { |
|
195 index++; |
|
196 } |
|
197 // sort widgets in width order |
|
198 } else { |
|
199 /* if widgets width is smaller on already |
|
200 existing ones in the sorted list -> increment index |
|
201 */ |
|
202 if (mNewWidgets.at(i)->visual()->preferredWidth() <= tmpWidgets.at(j)->visual()->preferredWidth()) { |
|
203 index++; |
|
204 } |
|
205 } |
|
206 } |
|
207 // add widget to its correct index in sorted list |
|
208 tmpWidgets.insert(index, mNewWidgets.at(i)); |
|
209 } |
|
210 } |
|
211 return tmpWidgets; |
|
212 } |
|
213 #endif // HSWIDGETORGANIZER_ALGORITHM |
|
214 |