Qt 4.8
qdeclarativeanchors.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativeanchors_p_p.h"
43 
44 #include "qdeclarativeitem.h"
45 #include "private/qdeclarativeitem_p.h"
46 
47 #include <qdeclarativeinfo.h>
48 
49 #include <QDebug>
50 
52 
53 //TODO: should we cache relationships, so we don't have to check each time (parent-child or sibling)?
54 //TODO: support non-parent, non-sibling (need to find lowest common ancestor)
55 
57 {
59 
60  qreal width = item->width();
61  int iw = width;
62  if (iw % 2)
63  return (width + 1) / 2;
64  else
65  return width / 2;
66 }
67 
69 {
71 
72  qreal height = item->height();
73  int ih = height;
74  if (ih % 2)
75  return (height + 1) / 2;
76  else
77  return height / 2;
78 }
79 
80 //### const item?
81 //local position
83 {
84  qreal ret = 0.0;
86  switch(anchorLine) {
88  ret = item->x();
89  break;
91  ret = item->x() + d->width();
92  break;
94  ret = item->y();
95  break;
97  ret = item->y() + d->height();
98  break;
100  ret = item->x() + hcenter(item);
101  break;
103  ret = item->y() + vcenter(item);
104  break;
106  if (d->isDeclarativeItem)
107  ret = item->y() + static_cast<QDeclarativeItem*>(item)->baselineOffset();
108  break;
109  default:
110  break;
111  }
112 
113  return ret;
114 }
115 
116 //position when origin is 0,0
118 {
119  qreal ret = 0.0;
121  switch(anchorLine) {
123  ret = 0.0;
124  break;
126  ret = d->width();
127  break;
129  ret = 0.0;
130  break;
132  ret = d->height();
133  break;
135  ret = hcenter(item);
136  break;
138  ret = vcenter(item);
139  break;
141  if (d->isDeclarativeItem)
142  ret = static_cast<QDeclarativeItem*>(item)->baselineOffset();
143  break;
144  default:
145  break;
146  }
147 
148  return ret;
149 }
150 
152  : QObject(*new QDeclarativeAnchorsPrivate(0), parent)
153 {
154  qFatal("QDeclarativeAnchors::QDeclarativeAnchors(QObject*) called");
155 }
156 
158  : QObject(*new QDeclarativeAnchorsPrivate(item), parent)
159 {
160 }
161 
163 {
165  d->remDepend(d->fill);
166  d->remDepend(d->centerIn);
167  d->remDepend(d->left.item);
168  d->remDepend(d->right.item);
169  d->remDepend(d->top.item);
170  d->remDepend(d->bottom.item);
171  d->remDepend(d->vCenter.item);
172  d->remDepend(d->hCenter.item);
173  d->remDepend(d->baseline.item);
174 }
175 
177 {
179  if (!fill || !isItemComplete())
180  return;
181 
182  if (updatingFill < 2) {
183  ++updatingFill;
184 
185  qreal horizontalMargin = q->mirrored() ? rightMargin : leftMargin;
186 
187  if (fill == item->parentItem()) { //child-parent
188  setItemPos(QPointF(horizontalMargin, topMargin));
189  } else if (fill->parentItem() == item->parentItem()) { //siblings
190  setItemPos(QPointF(fill->x()+horizontalMargin, fill->y()+topMargin));
191  }
193  setItemSize(QSizeF(fillPrivate->width()-leftMargin-rightMargin, fillPrivate->height()-topMargin-bottomMargin));
194 
195  --updatingFill;
196  } else {
197  // ### Make this certain :)
198  qmlInfo(item) << QDeclarativeAnchors::tr("Possible anchor loop detected on fill.");
199  }
200 
201 }
202 
204 {
206  if (!centerIn || fill || !isItemComplete())
207  return;
208 
209  if (updatingCenterIn < 2) {
210  ++updatingCenterIn;
211 
212  qreal effectiveHCenterOffset = q->mirrored() ? -hCenterOffset : hCenterOffset;
213  if (centerIn == item->parentItem()) {
214  QPointF p(hcenter(item->parentItem()) - hcenter(item) + effectiveHCenterOffset,
215  vcenter(item->parentItem()) - vcenter(item) + vCenterOffset);
216  setItemPos(p);
217 
218  } else if (centerIn->parentItem() == item->parentItem()) {
219  QPointF p(centerIn->x() + hcenter(centerIn) - hcenter(item) + effectiveHCenterOffset,
220  centerIn->y() + vcenter(centerIn) - vcenter(item) + vCenterOffset);
221  setItemPos(p);
222  }
223 
224  --updatingCenterIn;
225  } else {
226  // ### Make this certain :)
227  qmlInfo(item) << QDeclarativeAnchors::tr("Possible anchor loop detected on centerIn.");
228  }
229 }
230 
232 {
233  if (!item)
234  return;
235  if (fill == item)
236  fill = 0;
237  if (centerIn == item)
238  centerIn = 0;
239  if (left.item == item) {
240  left.item = 0;
242  }
243  if (right.item == item) {
244  right.item = 0;
246  }
247  if (top.item == item) {
248  top.item = 0;
250  }
251  if (bottom.item == item) {
252  bottom.item = 0;
254  }
255  if (vCenter.item == item) {
256  vCenter.item = 0;
258  }
259  if (hCenter.item == item) {
260  hCenter.item = 0;
262  }
263  if (baseline.item == item) {
264  baseline.item = 0;
266  }
267 }
268 
270 {
271  if (!item)
272  return;
273  QGraphicsItemPrivate * itemPrivate = QGraphicsItemPrivate::get(item);
274  if (itemPrivate->isDeclarativeItem) {
278  } else if(itemPrivate->isWidget) {
280  QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
281  QObject::connect(widget, SIGNAL(destroyed(QObject*)), q, SLOT(_q_widgetDestroyed(QObject*)));
282  QObject::connect(widget, SIGNAL(geometryChanged()), q, SLOT(_q_widgetGeometryChanged()));
283  }
284 }
285 
287 {
288  if (!item)
289  return;
290  QGraphicsItemPrivate * itemPrivate = QGraphicsItemPrivate::get(item);
291  if (itemPrivate->isDeclarativeItem) {
293  static_cast<QDeclarativeItemPrivate *>(itemPrivate);
295  } else if(itemPrivate->isWidget) {
297  QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
298  QObject::disconnect(widget, SIGNAL(destroyed(QObject*)), q, SLOT(_q_widgetDestroyed(QObject*)));
299  QObject::disconnect(widget, SIGNAL(geometryChanged()), q, SLOT(_q_widgetGeometryChanged()));
300  }
301 }
302 
304 {
305  return componentComplete;
306 }
307 
309 {
311  d->componentComplete = false;
312 }
313 
315 {
317  d->componentComplete = true;
318 }
319 
321 {
323  QGraphicsItemPrivate * itemPrivate = QGraphicsItemPrivate::get(d->item);
324  return itemPrivate->isDeclarativeItem ? static_cast<QDeclarativeItemPrivate *>(itemPrivate)->effectiveLayoutMirror : false;
325 }
326 
328 {
329  updatingMe = true;
331  updatingMe = false;
332 }
333 
335 {
336  updatingMe = true;
338  updatingMe = false;
339 }
340 
342 {
343  updatingMe = true;
344  item->setX(v);
345  updatingMe = false;
346 }
347 
349 {
350  updatingMe = true;
351  item->setY(v);
352  updatingMe = false;
353 }
354 
356 {
357  updatingMe = true;
358  item->setPos(v);
359  updatingMe = false;
360 }
361 
363 {
364  updatingMe = true;
365  if(QGraphicsItemPrivate::get(item)->isWidget)
366  static_cast<QGraphicsWidget *>(item)->resize(v);
368  static_cast<QDeclarativeItem *>(item)->setSize(v);
369  updatingMe = false;
370 }
371 
373 {
374  if (updatingMe) {
375  updatingMe = false;
376  return;
377  }
378 
379  fillChanged();
380  centerInChanged();
381  updateHorizontalAnchors();
382  updateVerticalAnchors();
383 }
384 
386 {
387  fillChanged();
388  centerInChanged();
389  updateHorizontalAnchors();
390  updateVerticalAnchors();
391 }
392 
394 {
395  clearItem(qobject_cast<QGraphicsObject*>(obj));
396 }
397 
399 {
400  fillChanged();
401  centerInChanged();
402  updateHorizontalAnchors();
403  updateVerticalAnchors();
404 }
405 
407 {
408  fillChanged();
409  centerInChanged();
410  if (newG.x() != oldG.x() || newG.width() != oldG.width())
411  updateHorizontalAnchors();
412  if (newG.y() != oldG.y() || newG.height() != oldG.height())
413  updateVerticalAnchors();
414 }
415 
417 {
418  Q_D(const QDeclarativeAnchors);
419  return d->fill;
420 }
421 
423 {
425  if (d->fill == f)
426  return;
427 
428  if (!f) {
429  d->remDepend(d->fill);
430  d->fill = f;
431  emit fillChanged();
432  return;
433  }
434  if (f != d->item->parentItem() && f->parentItem() != d->item->parentItem()){
435  qmlInfo(d->item) << tr("Cannot anchor to an item that isn't a parent or sibling.");
436  return;
437  }
438  d->remDepend(d->fill);
439  d->fill = f;
440  d->addDepend(d->fill);
441  emit fillChanged();
442  d->fillChanged();
443 }
444 
446 {
447  setFill(0);
448 }
449 
451 {
452  Q_D(const QDeclarativeAnchors);
453  return d->centerIn;
454 }
455 
457 {
459  if (d->centerIn == c)
460  return;
461 
462  if (!c) {
463  d->remDepend(d->centerIn);
464  d->centerIn = c;
466  return;
467  }
468  if (c != d->item->parentItem() && c->parentItem() != d->item->parentItem()){
469  qmlInfo(d->item) << tr("Cannot anchor to an item that isn't a parent or sibling.");
470  return;
471  }
472 
473  d->remDepend(d->centerIn);
474  d->centerIn = c;
475  d->addDepend(d->centerIn);
477  d->centerInChanged();
478 }
479 
481 {
482  setCenterIn(0);
483 }
484 
486  const QDeclarativeAnchorLine &edge2,
487  qreal offset1,
488  qreal offset2,
490  qreal &stretch)
491 {
492  bool edge1IsParent = (edge1.item == item->parentItem());
493  bool edge2IsParent = (edge2.item == item->parentItem());
494  bool edge1IsSibling = (edge1.item->parentItem() == item->parentItem());
495  bool edge2IsSibling = (edge2.item->parentItem() == item->parentItem());
496 
497  bool invalid = false;
498  if ((edge2IsParent && edge1IsParent) || (edge2IsSibling && edge1IsSibling)) {
499  stretch = (position(edge2.item, edge2.anchorLine) + offset2)
500  - (position(edge1.item, edge1.anchorLine) + offset1);
501  } else if (edge2IsParent && edge1IsSibling) {
502  stretch = (position(edge2.item, edge2.anchorLine) + offset2)
503  - (position(item->parentObject(), line)
504  + position(edge1.item, edge1.anchorLine) + offset1);
505  } else if (edge2IsSibling && edge1IsParent) {
506  stretch = (position(item->parentObject(), line) + position(edge2.item, edge2.anchorLine) + offset2)
507  - (position(edge1.item, edge1.anchorLine) + offset1);
508  } else
509  invalid = true;
510 
511  return invalid;
512 }
513 
515 {
516  if (fill || centerIn || !isItemComplete())
517  return;
518 
519  if (updatingVerticalAnchor < 2) {
520  ++updatingVerticalAnchor;
521  QGraphicsItemPrivate *itemPrivate = QGraphicsItemPrivate::get(item);
523  //Handle stretching
524  bool invalid = true;
525  qreal height = 0.0;
527  invalid = calcStretch(top, bottom, topMargin, -bottomMargin, QDeclarativeAnchorLine::Top, height);
529  invalid = calcStretch(top, vCenter, topMargin, vCenterOffset, QDeclarativeAnchorLine::Top, height);
530  height *= 2;
531  }
532  if (!invalid)
533  setItemHeight(height);
534 
535  //Handle top
536  if (top.item == item->parentItem()) {
538  } else if (top.item->parentItem() == item->parentItem()) {
539  setItemY(position(top.item, top.anchorLine) + topMargin);
540  }
542  //Handle stretching (top + bottom case is handled above)
544  qreal height = 0.0;
545  bool invalid = calcStretch(vCenter, bottom, vCenterOffset, -bottomMargin,
547  if (!invalid)
548  setItemHeight(height*2);
549  }
550 
551  //Handle bottom
552  if (bottom.item == item->parentItem()) {
553  setItemY(adjustedPosition(bottom.item, bottom.anchorLine) - itemPrivate->height() - bottomMargin);
554  } else if (bottom.item->parentItem() == item->parentItem()) {
555  setItemY(position(bottom.item, bottom.anchorLine) - itemPrivate->height() - bottomMargin);
556  }
558  //(stetching handled above)
559 
560  //Handle vCenter
561  if (vCenter.item == item->parentItem()) {
562  setItemY(adjustedPosition(vCenter.item, vCenter.anchorLine)
563  - vcenter(item) + vCenterOffset);
564  } else if (vCenter.item->parentItem() == item->parentItem()) {
565  setItemY(position(vCenter.item, vCenter.anchorLine) - vcenter(item) + vCenterOffset);
566  }
568  //Handle baseline
569  if (baseline.item == item->parentItem()) {
570  if (itemPrivate->isDeclarativeItem)
572  - static_cast<QDeclarativeItem *>(item)->baselineOffset() + baselineOffset);
573  } else if (baseline.item->parentItem() == item->parentItem()) {
574  if (itemPrivate->isDeclarativeItem)
576  - static_cast<QDeclarativeItem *>(item)->baselineOffset() + baselineOffset);
577  }
578  }
579  --updatingVerticalAnchor;
580  } else {
581  // ### Make this certain :)
582  qmlInfo(item) << QDeclarativeAnchors::tr("Possible anchor loop detected on vertical anchor.");
583  }
584 }
585 
587  if (anchorLine == QDeclarativeAnchorLine::Left) {
589  } else if (anchorLine == QDeclarativeAnchorLine::Right) {
591  } else {
592  return anchorLine;
593  }
594 }
595 
597 {
599  if (fill || centerIn || !isItemComplete())
600  return;
601 
602  if (updatingHorizontalAnchor < 3) {
603  ++updatingHorizontalAnchor;
604  qreal effectiveRightMargin, effectiveLeftMargin, effectiveHorizontalCenterOffset;
605  QDeclarativeAnchorLine effectiveLeft, effectiveRight, effectiveHorizontalCenter;
606  QDeclarativeAnchors::Anchor effectiveLeftAnchor, effectiveRightAnchor;
607  if (q->mirrored()) {
608  effectiveLeftAnchor = QDeclarativeAnchors::RightAnchor;
609  effectiveRightAnchor = QDeclarativeAnchors::LeftAnchor;
610  effectiveLeft.item = right.item;
611  effectiveLeft.anchorLine = reverseAnchorLine(right.anchorLine);
612  effectiveRight.item = left.item;
613  effectiveRight.anchorLine = reverseAnchorLine(left.anchorLine);
614  effectiveHorizontalCenter.item = hCenter.item;
615  effectiveHorizontalCenter.anchorLine = reverseAnchorLine(hCenter.anchorLine);
616  effectiveLeftMargin = rightMargin;
617  effectiveRightMargin = leftMargin;
618  effectiveHorizontalCenterOffset = -hCenterOffset;
619  } else {
620  effectiveLeftAnchor = QDeclarativeAnchors::LeftAnchor;
621  effectiveRightAnchor = QDeclarativeAnchors::RightAnchor;
622  effectiveLeft = left;
623  effectiveRight = right;
624  effectiveHorizontalCenter = hCenter;
625  effectiveLeftMargin = leftMargin;
626  effectiveRightMargin = rightMargin;
627  effectiveHorizontalCenterOffset = hCenterOffset;
628  }
629 
630  QGraphicsItemPrivate *itemPrivate = QGraphicsItemPrivate::get(item);
631  if (usedAnchors & effectiveLeftAnchor) {
632  //Handle stretching
633  bool invalid = true;
634  qreal width = 0.0;
635  if (usedAnchors & effectiveRightAnchor) {
636  invalid = calcStretch(effectiveLeft, effectiveRight, effectiveLeftMargin, -effectiveRightMargin, QDeclarativeAnchorLine::Left, width);
638  invalid = calcStretch(effectiveLeft, effectiveHorizontalCenter, effectiveLeftMargin, effectiveHorizontalCenterOffset, QDeclarativeAnchorLine::Left, width);
639  width *= 2;
640  }
641  if (!invalid)
642  setItemWidth(width);
643 
644  //Handle left
645  if (effectiveLeft.item == item->parentItem()) {
646  setItemX(adjustedPosition(effectiveLeft.item, effectiveLeft.anchorLine) + effectiveLeftMargin);
647  } else if (effectiveLeft.item->parentItem() == item->parentItem()) {
648  setItemX(position(effectiveLeft.item, effectiveLeft.anchorLine) + effectiveLeftMargin);
649  }
650  } else if (usedAnchors & effectiveRightAnchor) {
651  //Handle stretching (left + right case is handled in updateLeftAnchor)
653  qreal width = 0.0;
654  bool invalid = calcStretch(effectiveHorizontalCenter, effectiveRight, effectiveHorizontalCenterOffset, -effectiveRightMargin,
656  if (!invalid)
657  setItemWidth(width*2);
658  }
659 
660  //Handle right
661  if (effectiveRight.item == item->parentItem()) {
662  setItemX(adjustedPosition(effectiveRight.item, effectiveRight.anchorLine) - itemPrivate->width() - effectiveRightMargin);
663  } else if (effectiveRight.item->parentItem() == item->parentItem()) {
664  setItemX(position(effectiveRight.item, effectiveRight.anchorLine) - itemPrivate->width() - effectiveRightMargin);
665  }
667  //Handle hCenter
668  if (effectiveHorizontalCenter.item == item->parentItem()) {
669  setItemX(adjustedPosition(effectiveHorizontalCenter.item, effectiveHorizontalCenter.anchorLine) - hcenter(item) + effectiveHorizontalCenterOffset);
670  } else if (effectiveHorizontalCenter.item->parentItem() == item->parentItem()) {
671  setItemX(position(effectiveHorizontalCenter.item, effectiveHorizontalCenter.anchorLine) - hcenter(item) + effectiveHorizontalCenterOffset);
672  }
673  }
674  --updatingHorizontalAnchor;
675  } else {
676  // ### Make this certain :)
677  qmlInfo(item) << QDeclarativeAnchors::tr("Possible anchor loop detected on horizontal anchor.");
678  }
679 }
680 
682 {
683  Q_D(const QDeclarativeAnchors);
684  return d->top;
685 }
686 
688 {
690  if (!d->checkVAnchorValid(edge) || d->top == edge)
691  return;
692 
693  d->usedAnchors |= TopAnchor;
694 
695  if (!d->checkVValid()) {
696  d->usedAnchors &= ~TopAnchor;
697  return;
698  }
699 
700  d->remDepend(d->top.item);
701  d->top = edge;
702  d->addDepend(d->top.item);
703  emit topChanged();
704  d->updateVerticalAnchors();
705 }
706 
708 {
710  d->usedAnchors &= ~TopAnchor;
711  d->remDepend(d->top.item);
712  d->top = QDeclarativeAnchorLine();
713  emit topChanged();
714  d->updateVerticalAnchors();
715 }
716 
718 {
719  Q_D(const QDeclarativeAnchors);
720  return d->bottom;
721 }
722 
724 {
726  if (!d->checkVAnchorValid(edge) || d->bottom == edge)
727  return;
728 
729  d->usedAnchors |= BottomAnchor;
730 
731  if (!d->checkVValid()) {
732  d->usedAnchors &= ~BottomAnchor;
733  return;
734  }
735 
736  d->remDepend(d->bottom.item);
737  d->bottom = edge;
738  d->addDepend(d->bottom.item);
740  d->updateVerticalAnchors();
741 }
742 
744 {
746  d->usedAnchors &= ~BottomAnchor;
747  d->remDepend(d->bottom.item);
748  d->bottom = QDeclarativeAnchorLine();
750  d->updateVerticalAnchors();
751 }
752 
754 {
755  Q_D(const QDeclarativeAnchors);
756  return d->vCenter;
757 }
758 
760 {
762  if (!d->checkVAnchorValid(edge) || d->vCenter == edge)
763  return;
764 
765  d->usedAnchors |= VCenterAnchor;
766 
767  if (!d->checkVValid()) {
768  d->usedAnchors &= ~VCenterAnchor;
769  return;
770  }
771 
772  d->remDepend(d->vCenter.item);
773  d->vCenter = edge;
774  d->addDepend(d->vCenter.item);
776  d->updateVerticalAnchors();
777 }
778 
780 {
782  d->usedAnchors &= ~VCenterAnchor;
783  d->remDepend(d->vCenter.item);
784  d->vCenter = QDeclarativeAnchorLine();
786  d->updateVerticalAnchors();
787 }
788 
790 {
791  Q_D(const QDeclarativeAnchors);
792  return d->baseline;
793 }
794 
796 {
798  if (!d->checkVAnchorValid(edge) || d->baseline == edge)
799  return;
800 
801  d->usedAnchors |= BaselineAnchor;
802 
803  if (!d->checkVValid()) {
804  d->usedAnchors &= ~BaselineAnchor;
805  return;
806  }
807 
808  d->remDepend(d->baseline.item);
809  d->baseline = edge;
810  d->addDepend(d->baseline.item);
812  d->updateVerticalAnchors();
813 }
814 
816 {
818  d->usedAnchors &= ~BaselineAnchor;
819  d->remDepend(d->baseline.item);
820  d->baseline = QDeclarativeAnchorLine();
822  d->updateVerticalAnchors();
823 }
824 
826 {
827  Q_D(const QDeclarativeAnchors);
828  return d->left;
829 }
830 
832 {
834  if (!d->checkHAnchorValid(edge) || d->left == edge)
835  return;
836 
837  d->usedAnchors |= LeftAnchor;
838 
839  if (!d->checkHValid()) {
840  d->usedAnchors &= ~LeftAnchor;
841  return;
842  }
843 
844  d->remDepend(d->left.item);
845  d->left = edge;
846  d->addDepend(d->left.item);
847  emit leftChanged();
848  d->updateHorizontalAnchors();
849 }
850 
852 {
854  d->usedAnchors &= ~LeftAnchor;
855  d->remDepend(d->left.item);
856  d->left = QDeclarativeAnchorLine();
857  emit leftChanged();
858  d->updateHorizontalAnchors();
859 }
860 
862 {
863  Q_D(const QDeclarativeAnchors);
864  return d->right;
865 }
866 
868 {
870  if (!d->checkHAnchorValid(edge) || d->right == edge)
871  return;
872 
873  d->usedAnchors |= RightAnchor;
874 
875  if (!d->checkHValid()) {
876  d->usedAnchors &= ~RightAnchor;
877  return;
878  }
879 
880  d->remDepend(d->right.item);
881  d->right = edge;
882  d->addDepend(d->right.item);
883  emit rightChanged();
884  d->updateHorizontalAnchors();
885 }
886 
888 {
890  d->usedAnchors &= ~RightAnchor;
891  d->remDepend(d->right.item);
892  d->right = QDeclarativeAnchorLine();
893  emit rightChanged();
894  d->updateHorizontalAnchors();
895 }
896 
898 {
899  Q_D(const QDeclarativeAnchors);
900  return d->hCenter;
901 }
902 
904 {
906  if (!d->checkHAnchorValid(edge) || d->hCenter == edge)
907  return;
908 
909  d->usedAnchors |= HCenterAnchor;
910 
911  if (!d->checkHValid()) {
912  d->usedAnchors &= ~HCenterAnchor;
913  return;
914  }
915 
916  d->remDepend(d->hCenter.item);
917  d->hCenter = edge;
918  d->addDepend(d->hCenter.item);
920  d->updateHorizontalAnchors();
921 }
922 
924 {
926  d->usedAnchors &= ~HCenterAnchor;
927  d->remDepend(d->hCenter.item);
928  d->hCenter = QDeclarativeAnchorLine();
930  d->updateHorizontalAnchors();
931 }
932 
934 {
935  Q_D(const QDeclarativeAnchors);
936  return d->leftMargin;
937 }
938 
940 {
942  if (d->leftMargin == offset)
943  return;
944  d->leftMargin = offset;
945  if(d->fill)
946  d->fillChanged();
947  else
948  d->updateHorizontalAnchors();
950 }
951 
953 {
954  Q_D(const QDeclarativeAnchors);
955  return d->rightMargin;
956 }
957 
959 {
961  if (d->rightMargin == offset)
962  return;
963  d->rightMargin = offset;
964  if(d->fill)
965  d->fillChanged();
966  else
967  d->updateHorizontalAnchors();
969 }
970 
972 {
973  Q_D(const QDeclarativeAnchors);
974  return d->margins;
975 }
976 
978 {
980  if (d->margins == offset)
981  return;
982  //###Is it significantly faster to set them directly so we can call fillChanged only once?
983  if(!d->rightMargin || d->rightMargin == d->margins)
984  setRightMargin(offset);
985  if(!d->leftMargin || d->leftMargin == d->margins)
986  setLeftMargin(offset);
987  if(!d->topMargin || d->topMargin == d->margins)
988  setTopMargin(offset);
989  if(!d->bottomMargin || d->bottomMargin == d->margins)
990  setBottomMargin(offset);
991  d->margins = offset;
993 
994 }
995 
997 {
998  Q_D(const QDeclarativeAnchors);
999  return d->hCenterOffset;
1000 }
1001 
1003 {
1005  if (d->hCenterOffset == offset)
1006  return;
1007  d->hCenterOffset = offset;
1008  if(d->centerIn)
1009  d->centerInChanged();
1010  else
1011  d->updateHorizontalAnchors();
1013 }
1014 
1016 {
1017  Q_D(const QDeclarativeAnchors);
1018  return d->topMargin;
1019 }
1020 
1022 {
1024  if (d->topMargin == offset)
1025  return;
1026  d->topMargin = offset;
1027  if(d->fill)
1028  d->fillChanged();
1029  else
1030  d->updateVerticalAnchors();
1032 }
1033 
1035 {
1036  Q_D(const QDeclarativeAnchors);
1037  return d->bottomMargin;
1038 }
1039 
1041 {
1043  if (d->bottomMargin == offset)
1044  return;
1045  d->bottomMargin = offset;
1046  if(d->fill)
1047  d->fillChanged();
1048  else
1049  d->updateVerticalAnchors();
1051 }
1052 
1054 {
1055  Q_D(const QDeclarativeAnchors);
1056  return d->vCenterOffset;
1057 }
1058 
1060 {
1062  if (d->vCenterOffset == offset)
1063  return;
1064  d->vCenterOffset = offset;
1065  if(d->centerIn)
1066  d->centerInChanged();
1067  else
1068  d->updateVerticalAnchors();
1070 }
1071 
1073 {
1074  Q_D(const QDeclarativeAnchors);
1075  return d->baselineOffset;
1076 }
1077 
1079 {
1081  if (d->baselineOffset == offset)
1082  return;
1083  d->baselineOffset = offset;
1084  d->updateVerticalAnchors();
1086 }
1087 
1088 QDeclarativeAnchors::Anchors QDeclarativeAnchors::usedAnchors() const
1089 {
1090  Q_D(const QDeclarativeAnchors);
1091  return d->usedAnchors;
1092 }
1093 
1095 {
1099  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot specify left, right, and hcenter anchors.");
1100  return false;
1101  }
1102 
1103  return true;
1104 }
1105 
1107 {
1108  if (!anchor.item) {
1109  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor to a null item.");
1110  return false;
1111  } else if (anchor.anchorLine & QDeclarativeAnchorLine::Vertical_Mask) {
1112  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor a horizontal edge to a vertical edge.");
1113  return false;
1114  } else if (anchor.item != item->parentItem() && anchor.item->parentItem() != item->parentItem()){
1115  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor to an item that isn't a parent or sibling.");
1116  return false;
1117  } else if (anchor.item == item) {
1118  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor item to self.");
1119  return false;
1120  }
1121 
1122  return true;
1123 }
1124 
1126 {
1130  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot specify top, bottom, and vcenter anchors.");
1131  return false;
1135  usedAnchors & QDeclarativeAnchors::VCenterAnchor)) {
1136  qmlInfo(item) << QDeclarativeAnchors::tr("Baseline anchor cannot be used in conjunction with top, bottom, or vcenter anchors.");
1137  return false;
1138  }
1139 
1140  return true;
1141 }
1142 
1144 {
1145  if (!anchor.item) {
1146  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor to a null item.");
1147  return false;
1149  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor a vertical edge to a horizontal edge.");
1150  return false;
1151  } else if (anchor.item != item->parentItem() && anchor.item->parentItem() != item->parentItem()){
1152  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor to an item that isn't a parent or sibling.");
1153  return false;
1154  } else if (anchor.item == item){
1155  qmlInfo(item) << QDeclarativeAnchors::tr("Cannot anchor item to self.");
1156  return false;
1157  }
1158 
1159  return true;
1160 }
1161 
1163 
1164 #include <moc_qdeclarativeanchors_p.cpp>
1165 
QDeclarativeAnchorLine baseline() const
double d
Definition: qnumeric_p.h:62
void verticalCenterChanged()
qreal y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:667
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void addDepend(QGraphicsObject *)
QPointer< QWidget > widget
qreal y
the y position of the item
virtual qreal height() const
QDeclarativeAnchorLine horizontalCenter() const
void itemGeometryChanged(QDeclarativeItem *, const QRectF &, const QRectF &)
void setCenterIn(QGraphicsObject *)
qreal rightMargin() const
void remDepend(QGraphicsObject *)
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
QDeclarativeAnchorLine bottom() const
QGraphicsObject * centerIn() const
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
qreal leftMargin() const
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
QDeclarativeAnchorLine verticalCenter() const
static qreal vcenter(QGraphicsItem *i)
static qreal adjustedPosition(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
#define Q_Q(Class)
Definition: qglobal.h:2483
virtual void setWidth(qreal)
bool calcStretch(const QDeclarativeAnchorLine &edge1, const QDeclarativeAnchorLine &edge2, qreal offset1, qreal offset2, QDeclarativeAnchorLine::AnchorLine line, qreal &stretch)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void setLeft(const QDeclarativeAnchorLine &edge)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
The QDeclarativeItem class provides the most basic of all visual items in QML.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void addItemChangeListener(QDeclarativeItemChangeListener *listener, ChangeTypes types)
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
#define emit
Definition: qobjectdefs.h:76
bool checkHAnchorValid(QDeclarativeAnchorLine anchor) const
QDeclarativeAnchorLine::AnchorLine reverseAnchorLine(QDeclarativeAnchorLine::AnchorLine anchorLine)
void setBaseline(const QDeclarativeAnchorLine &edge)
qreal horizontalCenterOffset() const
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
void clearItem(QGraphicsObject *)
bool checkVAnchorValid(QDeclarativeAnchorLine anchor) const
void setTop(const QDeclarativeAnchorLine &edge)
QDeclarativeAnchorLine top() const
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void removeItemChangeListener(QDeclarativeItemChangeListener *, ChangeTypes types)
void setItemSize(const QSizeF &)
qreal verticalCenterOffset() const
Q_CORE_EXPORT void qFatal(const char *,...)
qreal topMargin() const
QGraphicsItem * parentItem() const
Returns a pointer to this item&#39;s parent item.
void setHorizontalCenter(const QDeclarativeAnchorLine &edge)
qreal margins() const
qreal x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:664
virtual qreal width() const
void setVerticalCenter(const QDeclarativeAnchorLine &edge)
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void verticalCenterOffsetChanged()
QDeclarativeAnchorLine left() const
QDeclarativeAnchors(QObject *parent=0)
void horizontalCenterOffsetChanged()
static const QGraphicsItemPrivate * get(const QGraphicsItem *item)
static qreal hcenter(QGraphicsItem *i)
QGraphicsObject * fill() const
qreal bottomMargin() const
void baselineOffsetChanged()
qreal x
the x position of the item
QDeclarativeInfo qmlInfo(const QObject *me)
QDeclarativeAnchorLine right() const
void setItemPos(const QPointF &)
void horizontalCenterChanged()
The QGraphicsObject class provides a base class for all graphics items that require signals...
void setFill(QGraphicsObject *)
void setRight(const QDeclarativeAnchorLine &edge)
qreal baselineOffset() const
virtual void setHeight(qreal)
void setBottom(const QDeclarativeAnchorLine &edge)
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.