43 */ |
43 */ |
44 HsPageNewWidgetLayout::HsPageNewWidgetLayout(const QPointF &touchPoint, |
44 HsPageNewWidgetLayout::HsPageNewWidgetLayout(const QPointF &touchPoint, |
45 QGraphicsLayoutItem *parent) |
45 QGraphicsLayoutItem *parent) |
46 : QGraphicsLayout(parent), |
46 : QGraphicsLayout(parent), |
47 mTouchPoint(touchPoint) |
47 mTouchPoint(touchPoint) |
|
48 // add mWidgetRects |
48 { |
49 { |
49 mSize = HsGui::instance()->layoutRect().size(); |
50 mSize = HsGui::instance()->layoutRect().size(); |
50 } |
51 } |
51 |
52 |
52 /*! |
53 /*! |
102 */ |
103 */ |
103 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect) |
104 void HsPageNewWidgetLayout::setGeometry(const QRectF &rect) |
104 { |
105 { |
105 QGraphicsLayout::setGeometry(rect); |
106 QGraphicsLayout::setGeometry(rect); |
106 |
107 |
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 |
108 // get rects for new widgets |
117 QList<QRectF> newRects; |
109 QList<QRectF> newRects; |
118 foreach (HsWidgetHost *newWidget, mNewWidgets) { |
110 foreach (HsWidgetHost *newWidget, mNewWidgets) { |
119 newRects << QRectF(QPointF(), newWidget->visual()->preferredSize()); |
111 newRects << QRectF(QPointF(), newWidget->visual()->preferredSize()); |
120 } |
112 } |
121 |
113 |
122 /* if there is touch point defined (widget added from context menu) |
114 /* if there is touch point defined (widget added from context menu) |
123 then there is only one widget in the list |
115 then there is only one widget in the list |
124 -> set widget center point to this touch point |
116 -> set widget center point to this touch point |
125 */ |
117 */ |
126 if (mTouchPoint != QPointF() && mNewWidgets.count() == 1) { |
118 if (mTouchPoint != QPointF() && newRects.count() == 1) { |
127 QRectF widgetRect = newRects.at(0); |
119 QRectF widgetRect = newRects.at(0); |
128 widgetRect.moveCenter(mTouchPoint); |
120 widgetRect.moveCenter(mTouchPoint); |
129 widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect)); |
121 widgetRect.moveTopLeft(HsScene::instance()->activePage()->adjustedWidgetPosition(widgetRect)); |
130 mNewWidgets.at(0)->visual()->setGeometry(widgetRect); |
122 mNewWidgets.at(0)->visual()->setGeometry(widgetRect); |
131 /* we have to save widget presentation data here after drawing |
123 /* we have to save widget presentation data here after drawing |
151 // calculate new widget positions with "stuck 'em all"-algorithm |
143 // calculate new widget positions with "stuck 'em all"-algorithm |
152 HsWidgetPositioningOnWidgetAdd *algorithm = |
144 HsWidgetPositioningOnWidgetAdd *algorithm = |
153 HsWidgetPositioningOnWidgetAdd::instance(); |
145 HsWidgetPositioningOnWidgetAdd::instance(); |
154 QList<QRectF> calculatedRects = |
146 QList<QRectF> calculatedRects = |
155 algorithm->convert(pageRect, existingRects, newRects, QPointF()); |
147 algorithm->convert(pageRect, existingRects, newRects, QPointF()); |
156 |
148 // set new widgets to screen and save presentation for each widget |
157 for ( int i=0; i<mNewWidgets.count(); i++) { |
149 for (int i=0; i<mNewWidgets.count(); i++) { |
158 mNewWidgets.at(i)->visual()->setGeometry(calculatedRects.at(i)); |
150 mNewWidgets.at(i)->visual()->setGeometry(calculatedRects.at(i)); |
159 mNewWidgets.at(i)->savePresentation(); |
151 mNewWidgets.at(i)->savePresentation(); |
160 } |
152 } |
161 } |
153 } |
162 } |
154 } |
166 */ |
158 */ |
167 void HsPageNewWidgetLayout::addItem(HsWidgetHost *item) |
159 void HsPageNewWidgetLayout::addItem(HsWidgetHost *item) |
168 { |
160 { |
169 mNewWidgets.append(item); |
161 mNewWidgets.append(item); |
170 } |
162 } |
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 |
|