qgraphicsanchorlayout.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/graphicsview/qgraphicsanchorlayout.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtWidgets module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34/*!-
35 \class QGraphicsAnchorLayout-
36 \brief The QGraphicsAnchorLayout class provides a layout where one can anchor widgets-
37 together in Graphics View.-
38 \since 4.6-
39 \ingroup appearance-
40 \ingroup geomanagement-
41 \ingroup graphicsview-api-
42 \inmodule QtWidgets-
43-
44 The anchor layout allows developers to specify how widgets should be placed relative to-
45 each other, and to the layout itself. The specification is made by adding anchors to the-
46 layout by calling addAnchor(), addAnchors() or addCornerAnchors().-
47-
48 Existing anchors in the layout can be accessed with the anchor() function.-
49 Items that are anchored are automatically added to the layout, and if items-
50 are removed, all their anchors will be automatically removed.-
51-
52 \div {class="float-left"}-
53 \inlineimage simpleanchorlayout-example.png Using an anchor layout to align simple colored widgets.-
54 \enddiv-
55-
56 Anchors are always set up between edges of an item, where the "center" is also considered to-
57 be an edge. Consider the following example:-
58-
59 \snippet graphicsview/simpleanchorlayout/main.cpp adding anchors-
60-
61 Here, the right edge of item \c a is anchored to the left edge of item \c b and the bottom-
62 edge of item \c a is anchored to the top edge of item \c b, with the result that-
63 item \c b will be placed diagonally to the right and below item \c b.-
64-
65 The addCornerAnchors() function provides a simpler way of anchoring the corners-
66 of two widgets than the two individual calls to addAnchor() shown in the code-
67 above. Here, we see how a widget can be anchored to the top-left corner of the enclosing-
68 layout:-
69-
70 \snippet graphicsview/simpleanchorlayout/main.cpp adding a corner anchor-
71-
72 In cases where anchors are used to match the widths or heights of widgets, it is-
73 convenient to use the addAnchors() function. As with the other functions for specifying-
74 anchors, it can also be used to anchor a widget to a layout.-
75-
76 \section1 Size Hints and Size Policies in an Anchor Layout-
77-
78 QGraphicsAnchorLayout respects each item's size hints and size policies.-
79 Note that there are some properties of QSizePolicy that are \l{Known issues}{not respected}.-
80-
81 \section1 Spacing within an Anchor Layout-
82-
83 The layout may distribute some space between the items. If the spacing has not been-
84 explicitly specified, the actual amount of space will usually be 0.-
85-
86 However, if the first edge is the \e opposite of the second edge (e.g., the right edge-
87 of the first widget is anchored to the left edge of the second widget), the size of the-
88 anchor will be queried from the style through a pixel metric:-
89 \l{QStyle::}{PM_LayoutHorizontalSpacing} for horizontal anchors and-
90 \l{QStyle::}{PM_LayoutVerticalSpacing} for vertical anchors.-
91-
92 If the spacing is negative, the items will overlap to some extent.-
93-
94-
95 \section1 Known Issues-
96 There are some features that QGraphicsAnchorLayout currently does not support.-
97 This might change in the future, so avoid using these features if you want to-
98 avoid any future regressions in behaviour:-
99 \list-
100-
101 \li Stretch factors are not respected.-
102-
103 \li QSizePolicy::ExpandFlag is not respected.-
104-
105 \li Height for width is not respected.-
106-
107 \endlist-
108-
109 \sa QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayout-
110*/-
111-
112/*!-
113 \class QGraphicsAnchor-
114 \brief The QGraphicsAnchor class represents an anchor between two items in a-
115 QGraphicsAnchorLayout.-
116 \since 4.6-
117 \ingroup appearance-
118 \ingroup geomanagement-
119 \ingroup graphicsview-api-
120 \inmodule QtWidgets-
121-
122 The graphics anchor provides an API that enables you to query and manipulate the-
123 properties an anchor has. When an anchor is added to the layout with-
124 QGraphicsAnchorLayout::addAnchor(), a QGraphicsAnchor instance is returned where the properties-
125 are initialized to their default values. The properties can then be further changed, and they-
126 will be picked up the next time the layout is activated.-
127-
128 \sa QGraphicsAnchorLayout::anchor()-
129-
130*/-
131#include "qgraphicsanchorlayout_p.h"-
132#ifndef QT_NO_GRAPHICSVIEW-
133QT_BEGIN_NAMESPACE-
134-
135QGraphicsAnchor::QGraphicsAnchor(QGraphicsAnchorLayout *parentLayout)-
136 : QObject(*(new QGraphicsAnchorPrivate))-
137{-
138 Q_D(QGraphicsAnchor);-
139 Q_ASSERT(parentLayout);-
140 d->layoutPrivate = parentLayout->d_func();-
141}
never executed: end of block
0
142-
143/*!-
144 Removes the QGraphicsAnchor object from the layout and destroys it.-
145*/-
146QGraphicsAnchor::~QGraphicsAnchor()-
147{-
148}-
149-
150/*!-
151 \property QGraphicsAnchor::sizePolicy-
152 \brief the size policy for the QGraphicsAnchor.-
153-
154 By setting the size policy on an anchor you can configure how the anchor can resize itself-
155 from its preferred spacing. For instance, if the anchor has the size policy-
156 QSizePolicy::Minimum, the spacing is the minimum size of the anchor. However, its size-
157 can grow up to the anchors maximum size. If the default size policy is QSizePolicy::Fixed,-
158 the anchor can neither grow or shrink, which means that the only size the anchor can have-
159 is the spacing. QSizePolicy::Fixed is the default size policy.-
160 QGraphicsAnchor always has a minimum spacing of 0 and a very large maximum spacing.-
161-
162 \sa QGraphicsAnchor::spacing-
163*/-
164-
165void QGraphicsAnchor::setSizePolicy(QSizePolicy::Policy policy)-
166{-
167 Q_D(QGraphicsAnchor);-
168 d->setSizePolicy(policy);-
169}
never executed: end of block
0
170-
171QSizePolicy::Policy QGraphicsAnchor::sizePolicy() const-
172{-
173 Q_D(const QGraphicsAnchor);-
174 return d->sizePolicy;
never executed: return d->sizePolicy;
0
175}-
176-
177/*!-
178 \property QGraphicsAnchor::spacing-
179 \brief the preferred space between items in the QGraphicsAnchorLayout.-
180-
181 Depending on the anchor type, the default spacing is either-
182 0 or a value returned from the style.-
183-
184 \sa QGraphicsAnchorLayout::addAnchor()-
185*/-
186void QGraphicsAnchor::setSpacing(qreal spacing)-
187{-
188 Q_D(QGraphicsAnchor);-
189 d->setSpacing(spacing);-
190}
never executed: end of block
0
191-
192qreal QGraphicsAnchor::spacing() const-
193{-
194 Q_D(const QGraphicsAnchor);-
195 return d->spacing();
never executed: return d->spacing();
0
196}-
197-
198void QGraphicsAnchor::unsetSpacing()-
199{-
200 Q_D(QGraphicsAnchor);-
201 d->unsetSpacing();-
202}
never executed: end of block
0
203-
204/*!-
205 Constructs a QGraphicsAnchorLayout instance. \a parent is passed to-
206 QGraphicsLayout's constructor.-
207 */-
208QGraphicsAnchorLayout::QGraphicsAnchorLayout(QGraphicsLayoutItem *parent)-
209 : QGraphicsLayout(*new QGraphicsAnchorLayoutPrivate(), parent)-
210{-
211 Q_D(QGraphicsAnchorLayout);-
212 d->createLayoutEdges();-
213}
never executed: end of block
0
214-
215/*!-
216 Destroys the QGraphicsAnchorLayout object.-
217*/-
218QGraphicsAnchorLayout::~QGraphicsAnchorLayout()-
219{-
220 Q_D(QGraphicsAnchorLayout);-
221-
222 for (int i = count() - 1; i >= 0; --i) {
i >= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
223 QGraphicsLayoutItem *item = d->items.at(i);-
224 removeAt(i);-
225 if (item) {
itemDescription
TRUEnever evaluated
FALSEnever evaluated
0
226 if (item->ownedByLayout())
item->ownedByLayout()Description
TRUEnever evaluated
FALSEnever evaluated
0
227 delete item;
never executed: delete item;
0
228 }
never executed: end of block
0
229 }
never executed: end of block
0
230-
231 d->removeCenterConstraints(this, QGraphicsAnchorLayoutPrivate::Horizontal);-
232 d->removeCenterConstraints(this, QGraphicsAnchorLayoutPrivate::Vertical);-
233 d->deleteLayoutEdges();-
234-
235 Q_ASSERT(d->itemCenterConstraints[0].isEmpty());-
236 Q_ASSERT(d->itemCenterConstraints[1].isEmpty());-
237 Q_ASSERT(d->items.isEmpty());-
238 Q_ASSERT(d->m_vertexList.isEmpty());-
239}
never executed: end of block
0
240-
241/*!-
242 Creates an anchor between the edge \a firstEdge of item \a firstItem and the edge \a secondEdge-
243 of item \a secondItem. The spacing of the anchor is picked up from the style. Anchors-
244 between a layout edge and an item edge will have a size of 0.-
245 If there is already an anchor between the edges, the new anchor will replace the old one.-
246-
247 \a firstItem and \a secondItem are automatically added to the layout if they are not part-
248 of the layout. This means that count() can increase by up to 2.-
249-
250 The spacing an anchor will get depends on the type of anchor. For instance, anchors from the-
251 Right edge of one item to the Left edge of another (or vice versa) will use the default-
252 horizontal spacing. The same behaviour applies to Bottom to Top anchors, (but they will use-
253 the default vertical spacing). For all other anchor combinations, the spacing will be 0.-
254 All anchoring functions will follow this rule.-
255-
256 The spacing can also be set manually by using QGraphicsAnchor::setSpacing() method.-
257-
258 Calling this function where \a firstItem or \a secondItem are ancestors of the layout have-
259 undefined behaviour.-
260-
261 \sa addAnchors(), addCornerAnchors()-
262 */-
263QGraphicsAnchor *-
264QGraphicsAnchorLayout::addAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge,-
265 QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)-
266{-
267 Q_D(QGraphicsAnchorLayout);-
268 QGraphicsAnchor *a = d->addAnchor(firstItem, firstEdge, secondItem, secondEdge);-
269 invalidate();-
270 return a;
never executed: return a;
0
271}-
272-
273/*!-
274 Returns the anchor between the anchor points defined by \a firstItem and \a firstEdge and-
275 \a secondItem and \a secondEdge. If there is no such anchor, the function will return 0.-
276*/-
277QGraphicsAnchor *-
278QGraphicsAnchorLayout::anchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge,-
279 QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)-
280{-
281 Q_D(QGraphicsAnchorLayout);-
282 return d->getAnchor(firstItem, firstEdge, secondItem, secondEdge);
never executed: return d->getAnchor(firstItem, firstEdge, secondItem, secondEdge);
0
283}-
284-
285/*!-
286 Creates two anchors between \a firstItem and \a secondItem specified by the corners,-
287 \a firstCorner and \a secondCorner, where one is for the horizontal edge and another-
288 one for the vertical edge.-
289-
290 This is a convenience function, since anchoring corners can be expressed as anchoring-
291 two edges. For instance:-
292-
293 \snippet graphicsview/simpleanchorlayout/main.cpp adding a corner anchor in two steps-
294-
295 This can also be achieved with the following line of code:-
296-
297 \snippet graphicsview/simpleanchorlayout/main.cpp adding a corner anchor-
298-
299 If there is already an anchor between the edge pairs, it will be replaced by the anchors that-
300 this function specifies.-
301-
302 \a firstItem and \a secondItem are automatically added to the layout if they are not part of the-
303 layout. This means that count() can increase by up to 2.-
304-
305 \sa addAnchor(), addAnchors()-
306*/-
307void QGraphicsAnchorLayout::addCornerAnchors(QGraphicsLayoutItem *firstItem,-
308 Qt::Corner firstCorner,-
309 QGraphicsLayoutItem *secondItem,-
310 Qt::Corner secondCorner)-
311{-
312 Q_D(QGraphicsAnchorLayout);-
313-
314 // Horizontal anchor-
315 Qt::AnchorPoint firstEdge = (firstCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft);
firstCorner & 1Description
TRUEnever evaluated
FALSEnever evaluated
0
316 Qt::AnchorPoint secondEdge = (secondCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft);
secondCorner & 1Description
TRUEnever evaluated
FALSEnever evaluated
0
317 if (d->addAnchor(firstItem, firstEdge, secondItem, secondEdge)) {
d->addAnchor(f...m, secondEdge)Description
TRUEnever evaluated
FALSEnever evaluated
0
318 // Vertical anchor-
319 firstEdge = (firstCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop);
firstCorner & 2Description
TRUEnever evaluated
FALSEnever evaluated
0
320 secondEdge = (secondCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop);
secondCorner & 2Description
TRUEnever evaluated
FALSEnever evaluated
0
321 d->addAnchor(firstItem, firstEdge, secondItem, secondEdge);-
322-
323 invalidate();-
324 }
never executed: end of block
0
325}
never executed: end of block
0
326-
327/*!-
328 Anchors two or four edges of \a firstItem with the corresponding-
329 edges of \a secondItem, so that \a firstItem has the same size as-
330 \a secondItem in the dimensions specified by \a orientations.-
331-
332 For example, the following example anchors the left and right edges of two items-
333 to match their widths:-
334-
335 \snippet graphicsview/simpleanchorlayout/main.cpp adding anchors to match sizes in two steps-
336-
337 This can also be achieved using the following line of code:-
338-
339 \snippet graphicsview/simpleanchorlayout/main.cpp adding anchors to match sizes-
340-
341 \sa addAnchor(), addCornerAnchors()-
342*/-
343void QGraphicsAnchorLayout::addAnchors(QGraphicsLayoutItem *firstItem,-
344 QGraphicsLayoutItem *secondItem,-
345 Qt::Orientations orientations)-
346{-
347 bool ok = true;-
348 if (orientations & Qt::Horizontal) {
orientations & Qt::HorizontalDescription
TRUEnever evaluated
FALSEnever evaluated
0
349 // Currently, if the first is ok, then the rest of the calls should be ok-
350 ok = addAnchor(secondItem, Qt::AnchorLeft, firstItem, Qt::AnchorLeft) != 0;-
351 if (ok)
okDescription
TRUEnever evaluated
FALSEnever evaluated
0
352 addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight);
never executed: addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight);
0
353 }
never executed: end of block
0
354 if (orientations & Qt::Vertical && ok) {
orientations & Qt::VerticalDescription
TRUEnever evaluated
FALSEnever evaluated
okDescription
TRUEnever evaluated
FALSEnever evaluated
0
355 addAnchor(secondItem, Qt::AnchorTop, firstItem, Qt::AnchorTop);-
356 addAnchor(firstItem, Qt::AnchorBottom, secondItem, Qt::AnchorBottom);-
357 }
never executed: end of block
0
358}
never executed: end of block
0
359-
360/*!-
361 Sets the default horizontal spacing for the anchor layout to \a spacing.-
362-
363 \sa horizontalSpacing(), setVerticalSpacing(), setSpacing()-
364*/-
365void QGraphicsAnchorLayout::setHorizontalSpacing(qreal spacing)-
366{-
367 Q_D(QGraphicsAnchorLayout);-
368-
369 d->spacings[0] = spacing;-
370 invalidate();-
371}
never executed: end of block
0
372-
373/*!-
374 Sets the default vertical spacing for the anchor layout to \a spacing.-
375-
376 \sa verticalSpacing(), setHorizontalSpacing(), setSpacing()-
377*/-
378void QGraphicsAnchorLayout::setVerticalSpacing(qreal spacing)-
379{-
380 Q_D(QGraphicsAnchorLayout);-
381-
382 d->spacings[1] = spacing;-
383 invalidate();-
384}
never executed: end of block
0
385-
386/*!-
387 Sets the default horizontal and the default vertical spacing for the anchor layout to \a spacing.-
388-
389 If an item is anchored with no spacing associated with the anchor, it will use the default-
390 spacing.-
391-
392 QGraphicsAnchorLayout does not support negative spacings. Setting a negative value will unset the-
393 previous spacing and make the layout use the spacing provided by the current widget style.-
394-
395 \sa setHorizontalSpacing(), setVerticalSpacing()-
396*/-
397void QGraphicsAnchorLayout::setSpacing(qreal spacing)-
398{-
399 Q_D(QGraphicsAnchorLayout);-
400-
401 d->spacings[0] = d->spacings[1] = spacing;-
402 invalidate();-
403}
never executed: end of block
0
404-
405/*!-
406 Returns the default horizontal spacing for the anchor layout.-
407-
408 \sa verticalSpacing(), setHorizontalSpacing()-
409*/-
410qreal QGraphicsAnchorLayout::horizontalSpacing() const-
411{-
412 Q_D(const QGraphicsAnchorLayout);-
413 return d->styleInfo().defaultSpacing(Qt::Horizontal);
never executed: return d->styleInfo().defaultSpacing(Qt::Horizontal);
0
414}-
415-
416/*!-
417 Returns the default vertical spacing for the anchor layout.-
418-
419 \sa horizontalSpacing(), setVerticalSpacing()-
420*/-
421qreal QGraphicsAnchorLayout::verticalSpacing() const-
422{-
423 Q_D(const QGraphicsAnchorLayout);-
424 return d->styleInfo().defaultSpacing(Qt::Vertical);
never executed: return d->styleInfo().defaultSpacing(Qt::Vertical);
0
425}-
426-
427/*!-
428 \reimp-
429*/-
430void QGraphicsAnchorLayout::setGeometry(const QRectF &geom)-
431{-
432 Q_D(QGraphicsAnchorLayout);-
433-
434 QGraphicsLayout::setGeometry(geom);-
435 d->calculateVertexPositions(QGraphicsAnchorLayoutPrivate::Horizontal);-
436 d->calculateVertexPositions(QGraphicsAnchorLayoutPrivate::Vertical);-
437 d->setItemsGeometries(geom);-
438}
never executed: end of block
0
439-
440/*!-
441 Removes the layout item at \a index without destroying it. Ownership of-
442 the item is transferred to the caller.-
443-
444 Removing an item will also remove any of the anchors associated with it.-
445-
446 \sa itemAt(), count()-
447*/-
448void QGraphicsAnchorLayout::removeAt(int index)-
449{-
450 Q_D(QGraphicsAnchorLayout);-
451 QGraphicsLayoutItem *item = d->items.value(index);-
452-
453 if (!item)
!itemDescription
TRUEnever evaluated
FALSEnever evaluated
0
454 return;
never executed: return;
0
455-
456 // Removing an item affects both horizontal and vertical graphs-
457 d->removeCenterConstraints(item, QGraphicsAnchorLayoutPrivate::Horizontal);-
458 d->removeCenterConstraints(item, QGraphicsAnchorLayoutPrivate::Vertical);-
459 d->removeAnchors(item);-
460 d->items.remove(index);-
461-
462 item->setParentLayoutItem(0);-
463 invalidate();-
464}
never executed: end of block
0
465-
466/*!-
467 \reimp-
468*/-
469int QGraphicsAnchorLayout::count() const-
470{-
471 Q_D(const QGraphicsAnchorLayout);-
472 return d->items.size();
never executed: return d->items.size();
0
473}-
474-
475/*!-
476 \reimp-
477*/-
478QGraphicsLayoutItem *QGraphicsAnchorLayout::itemAt(int index) const-
479{-
480 Q_D(const QGraphicsAnchorLayout);-
481 return d->items.value(index);
never executed: return d->items.value(index);
0
482}-
483-
484/*!-
485 \reimp-
486*/-
487void QGraphicsAnchorLayout::invalidate()-
488{-
489 Q_D(QGraphicsAnchorLayout);-
490 QGraphicsLayout::invalidate();-
491 d->calculateGraphCacheDirty = true;-
492 d->styleInfoDirty = true;-
493}
never executed: end of block
0
494-
495/*!-
496 \reimp-
497*/-
498QSizeF QGraphicsAnchorLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const-
499{-
500 Q_UNUSED(constraint);-
501 Q_D(const QGraphicsAnchorLayout);-
502-
503 // Some setup calculations are delayed until the information is-
504 // actually needed, avoiding unnecessary recalculations when-
505 // adding multiple anchors.-
506-
507 // sizeHint() / effectiveSizeHint() already have a cache-
508 // mechanism, using invalidate() to force recalculation. However-
509 // sizeHint() is called three times after invalidation (for max,-
510 // min and pref), but we just need do our setup once.-
511-
512 const_cast<QGraphicsAnchorLayoutPrivate *>(d)->calculateGraphs();-
513-
514 // ### apply constraint!-
515 QSizeF engineSizeHint(-
516 d->sizeHints[QGraphicsAnchorLayoutPrivate::Horizontal][which],-
517 d->sizeHints[QGraphicsAnchorLayoutPrivate::Vertical][which]);-
518-
519 qreal left, top, right, bottom;-
520 getContentsMargins(&left, &top, &right, &bottom);-
521-
522 return engineSizeHint + QSizeF(left + right, top + bottom);
never executed: return engineSizeHint + QSizeF(left + right, top + bottom);
0
523}-
524-
525QT_END_NAMESPACE-
526-
527#include "moc_qgraphicsanchorlayout.cpp"-
528-
529#endif //QT_NO_GRAPHICSVIEW-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9