QtBase  v6.3.1
abstractitemcontainer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #include <QDebug>
29 #include <QGraphicsLayout>
30 
31 #include "abstractitemcontainer.h"
32 #include "abstractitemview.h"
33 #include "abstractviewitem.h"
34 #include "scrollbar.h"
35 
37  : GvbWidget(parent),
38  m_items(),
39  m_itemView(0),
40  m_prototype(0),
41  m_bufferSize(bufferSize),
42  m_twoColumns(false)
43 {
44 }
45 
47 {
48  delete m_prototype;
49  m_prototype = 0;
50 }
51 
53 {
54  return m_prototype;
55 }
56 
58 {
59  return m_bufferSize;
60 }
61 
63 {
64  if (e->type() == QEvent::LayoutRequest)
66 
67  return QGraphicsWidget::event(e);
68 }
69 
70 
72 {
74  const bool caching = m_itemView->listItemCaching();
76 
77  QSizeF s = m_itemView->size();
80 
83 
85  }
86 
88 }
89 
91 {
92  QVariant ichange = QGraphicsWidget::itemChange(change,value);
93 
94  if (change == ItemPositionChange) {
95  if (m_itemView && layout() && !layout()->isActivated())
97  }
98  return ichange;
99  }
100 
101 /*virtual*/
103 {
104  m_itemView = view;
105 
106  if (m_itemView) {
109  }
110 }
111 /*virtual*/
113 {
114  m_prototype = ptype;
116  m_prototype->hide();
117 }
118 
119 /*virtual*/
121 {
123  m_items.clear();
125 }
126 
127 
128 /*virtual*/
130 {
132  (m_items.count() > 0 &&
133  m_items.first()->modelIndex().row()-1 <= index.row() &&
134  m_items.last()->modelIndex().row() >= index.row())) {
135  int itemPos = 0;
136  if (m_items.count() != 0)
137  itemPos = qMax(0, index.row() - m_items.first()->modelIndex().row());
138 
139  if (itemPos >= m_items.count() || m_items.at(itemPos)->modelIndex() != index) {
140  AbstractViewItem *item = 0;
141  if (m_prototype)
143 
144  if (item) {
145  item->setModel(m_itemView->model());
146  item->setTwoColumns(m_twoColumns);
147  m_items.insert(itemPos, item);
148  addItemToVisibleLayout(itemPos, item);
149 
150  if (item->modelIndex() != index) {
151  item->setModelIndex(index);
152  }
153  }
154  }
156  }
157 }
159 {
161 
162  if (item) {
166 
167  delete item;
168  }
169  else {
172 
174  if (newIndex.isValid()) {
175  // Item readded as last item in buffer.
178  item->setModelIndex(newIndex);
179  } else {
180  // Item readded as first item in buffer.
181  newIndex = m_itemView->previousIndex(m_items.first()->modelIndex());
182 
185  item->setModelIndex(newIndex);
186  }
187  }
188  }
189 }
190 
191 /*virtual*/
193 {
194  return m_items.count();
195 }
196 
198 {
199  return m_items.first();
200 }
201 
202 /*virtual*/
204 {
206  return 0;
207  return m_items.at(row);
208 }
209 
211 {
212  AbstractViewItem *item = 0;
213  for (int i = 0; i < m_items.count(); ++i) {
214  if (m_items.at(i)->modelIndex() == index) {
215  item = m_items.at(i);
216  break;
217  }
218  }
219  return item;
220 }
221 
222 bool AbstractItemContainer::itemVisibleInView(AbstractViewItem* item, const QRectF &viewRect, bool fullyVisible) const
223 {
224  if (!item || !m_itemView)
225  return false;
226 
227  QRectF itemRectBoundingRect = item->mapToItem(m_itemView, item->boundingRect()).boundingRect();
228 
229  if (fullyVisible && viewRect.contains(itemRectBoundingRect))
230  return true;
231  else if (viewRect.intersects(itemRectBoundingRect))
232  return true;
233 
234  return false;
235 }
236 
238 {
240  return;
241 
243 
244  if (m_items.count() < maxCount) {
245  // New items needs to be added.
247  if (m_items.count() > 0)
248  index = m_items.last()->modelIndex();
249  while (m_items.count() < maxCount) {
251 
252  if (!index.isValid())
253  break;
254 
255  insertItem(m_items.count(), index);
256  }
257 
258  index = m_items.first()->modelIndex();
259  while (m_items.count() < maxCount) {
261 
262  if (!index.isValid())
263  break;
264 
265  insertItem(0, index);
266  }
267  }
268 
269  QRectF viewRect = boundingRect();
270 
271  while (m_items.count() > maxCount) {
272  int firstVisible = 0;
273  int lastVisible = 0;
274  findFirstAndLastVisibleBufferIndex(firstVisible, lastVisible, viewRect, false);
275 
276  AbstractViewItem* item = 0;
277  if (lastVisible != m_items.count() - 1) {
278  item = m_items.takeLast();
279  }
280  else if (firstVisible != 0 && m_items.first()->modelIndex().row() != firstVisible-1) {
281  item = m_items.takeFirst();
282  }
283  else {
284  // All the items are visible. Take the item at the end of the buffer.
285  item = m_items.takeLast();
286  }
287 
290  delete item;
291  }
292 }
293 
294 void AbstractItemContainer::insertItem(int pos, const QModelIndex &index)
295 {
296  AbstractViewItem *item = 0;
297  if (m_prototype)
299 
300  if (item) {
301  item->setModel(m_itemView->model());
302  item->setModelIndex(index);
303  item->setTwoColumns(m_twoColumns);
306  item->updateItemContents();
307  if (pos == 0)
309  item->effectiveSizeHint(Qt::PreferredSize).height());
310  }
311 }
312 
314  int &lastVisibleBufferIndex,
315  const QRectF &viewRect,
316  bool fullyVisible) const
317 {
318  if (layout() && !layout()->isActivated())
319  layout()->activate();
320 
321  firstVisibleBufferIndex = -1;
322  lastVisibleBufferIndex = -1;
323 
324  int count = m_items.count();
325  for (int i = 0; i < count; ++i) {
326  if (itemVisibleInView(m_items.at(i), viewRect, fullyVisible)) {
327  if (firstVisibleBufferIndex == -1)
328  firstVisibleBufferIndex = i;
329  lastVisibleBufferIndex = i;
330  }
331  else if ( lastVisibleBufferIndex != -1 )
332  break; // lastVisibleBufferIndex is already set
333  }
334 }
335 
336 /*virtual*/
338 {
340  {
341  return m_itemView->indexCount();
342  }
343  return 0;
344 }
345 
346 
348 {
349  for (int i = 0; i <m_items.count(); ++i)
350  m_items.at(i)->themeChange();
351 }
352 
354 {
355  for (int i = 0; i <m_items.count(); ++i)
357 }
358 
360 {
361  for (int i = 0; i <m_items.count(); ++i)
363  if (m_prototype)
365 }
366 
368 {
369  if (m_twoColumns == enabled)
370  return;
371 
372  m_twoColumns = enabled;
373 
374  for (int i = 0; i < m_items.count(); ++i)
376 }
377 
379 {
380  return m_twoColumns;
381 }
382 
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
QList< AbstractViewItem * > m_items
AbstractViewItem * m_prototype
virtual void addItem(const QModelIndex &index)
virtual void removeItemFromVisibleLayout(AbstractViewItem *item)=0
void setSubtreeCacheEnabled(const bool enabled)
virtual int itemCount() const
virtual void addItemToVisibleLayout(int index, AbstractViewItem *item)=0
AbstractViewItem * firstItem()
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *obj, QEvent *event)
AbstractItemContainer(int bufferSize, QGraphicsWidget *parent=0)
bool itemVisibleInView(AbstractViewItem *item, const QRectF &viewRect, bool fullyVisible=true) const
virtual void adjustVisibleContainerSize(const QSizeF &size)=0
virtual void removeItem(const QModelIndex &index)
virtual AbstractViewItem * itemAt(const int row) const
virtual void setItemView(AbstractItemView *view)
virtual int maxItemCountInItemBuffer() const
AbstractItemView * m_itemView
AbstractViewItem * prototype()
virtual void setTwoColumns(const bool enabled)
void findFirstAndLastVisibleBufferIndex(int &firstVisibleBufferIndex, int &lastVisibleBufferIndex, const QRectF &viewRect, bool fullyVisible) const
AbstractViewItem * findItemByIndex(const QModelIndex &index) const
virtual void setItemPrototype(AbstractViewItem *ptype)
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
virtual QModelIndex nextIndex(const QModelIndex &index) const
virtual QAbstractItemModel * model() const
virtual void setListItemCaching(bool enabled)=0
virtual QModelIndex previousIndex(const QModelIndex &index) const
virtual void scrollContentsBy(qreal dx, qreal dy)
virtual int indexCount() const
virtual bool listItemCaching() const =0
ScrollBar * verticalScrollBar() const
virtual void setSubtreeCacheEnabled(bool enabled)
virtual void updateItemContents()
virtual AbstractViewItem * newItemInstance()=0
virtual void themeChange()
QModelIndex modelIndex() const
virtual void setTwoColumns(const bool enabled)=0
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Definition: qcoreevent.h:58
@ LayoutRequest
Definition: qcoreevent.h:125
@ GraphicsSceneResize
Definition: qcoreevent.h:238
QPointF mapToItem(const QGraphicsItem *item, const QPointF &point) const
virtual QRectF boundingRect() const =0
void setParentItem(QGraphicsItem *parent)
bool enabled
whether the item is enabled or not
QPointF pos
the position of the item
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
bool event(QEvent *event) override
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
QRectF boundingRect() const override
QGraphicsLayout * layout
The layout of the widget.
QSizeF size
the size of the widget
iterator insert(qsizetype i, parameter_type t)
Definition: qlist.h:499
bool removeOne(const AT &t)
Definition: qlist.h:596
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
value_type takeFirst()
Definition: qlist.h:564
T & last()
Definition: qlist.h:646
value_type takeLast()
Definition: qlist.h:565
qsizetype count() const noexcept
Definition: qlist.h:415
void prepend(rvalue_ref t)
Definition: qlist.h:484
T & first()
Definition: qlist.h:643
void append(parameter_type t)
Definition: qlist.h:469
void clear()
Definition: qlist.h:445
The QModelIndex class is used to locate data in a data model.
constexpr int row() const noexcept
constexpr bool isValid() const noexcept
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
void installEventFilter(QObject *filterObj)
Definition: qobject.cpp:2235
virtual bool eventFilter(QObject *watched, QEvent *event)
Definition: qobject.cpp:1484
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
constexpr bool isEmpty() const noexcept
Definition: qrect.h:670
bool contains(const QRectF &r) const noexcept
Definition: qrect.cpp:2006
bool intersects(const QRectF &r) const noexcept
Definition: qrect.cpp:2284
constexpr bool isValid() const noexcept
Definition: qrect.h:675
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
constexpr void setWidth(qreal w) noexcept
Definition: qsize.h:355
constexpr qreal width() const noexcept
Definition: qsize.h:349
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
qDeleteAll(list.begin(), list.end())
double e
@ PreferredSize
Definition: qnamespace.h:1591
EGLOutputLayerEXT EGLint EGLAttrib value
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
GLuint index
[2]
GLenum GLenum GLsizei count
GLenum GLenum GLsizei const GLuint GLboolean enabled
struct _cl_event * event
Definition: qopenglext.h:2998
GLhandleARB obj
[2]
Definition: qopenglext.h:4164
GLsizei maxCount
Definition: qopenglext.h:677
GLenum GLenum GLsizei void * row
Definition: qopenglext.h:2747
GLdouble s
[6]
Definition: qopenglext.h:235
QGraphicsItem * item
QQuickView * view
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent