2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The implementation is centered around m_columnVisibleItems
19 * that holds a vector of lists. There's a list for each of the
20 * columns the view has. In the list the items of the column are
21 * ordered as they appear topdown in the view. m_indexColumnMap is
22 * used when re-building the list up since given a position
23 * in the middle of the list and the need to create the previous does
24 * not give us enough information to know in which column we have
25 * to position the item so that when we reach the item the view is
26 * correctly layouted at 0 for all the columns
28#include "verticaljournal.h"
30#include <qqmlengine.h>
31#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-pedantic"
33#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
34#include <private/qquickvisualdatamodel_p.h>
36#include <private/qqmldelegatemodel_p.h>
39#include <private/qquickitem_p.h>
40#pragma GCC diagnostic pop
42static const qreal bufferRatio = 0.5;
44VerticalJournal::VerticalJournal()
45 : m_delegateModel(nullptr)
46 , m_asyncRequestedIndex(-1)
50 , m_delegateCreationBegin(0)
51 , m_delegateCreationEnd(0)
52 , m_delegateCreationBeginValid(false)
53 , m_delegateCreationEndValid(false)
54 , m_needsRelayout(false)
55 , m_delegateValidated(false)
56 , m_implicitHeightDirty(false)
58 connect(this, SIGNAL(widthChanged()), this, SLOT(relayout()));
59 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
62QAbstractItemModel *VerticalJournal::model() const
64 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
67void VerticalJournal::setModel(QAbstractItemModel *model)
69 if (model != this->model()) {
70 if (!m_delegateModel) {
71 createDelegateModel();
73 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
75 cleanupExistingItems();
77 Q_EMIT modelChanged();
82QQmlComponent *VerticalJournal::delegate() const
84 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
87void VerticalJournal::setDelegate(QQmlComponent *delegate)
89 if (delegate != this->delegate()) {
90 if (!m_delegateModel) {
91 createDelegateModel();
94 cleanupExistingItems();
96 m_delegateModel->setDelegate(delegate);
98 Q_EMIT delegateChanged();
99 m_delegateValidated = false;
104qreal VerticalJournal::columnWidth() const
106 return m_columnWidth;
109void VerticalJournal::setColumnWidth(qreal columnWidth)
111 if (columnWidth != m_columnWidth) {
112 m_columnWidth = columnWidth;
113 Q_EMIT columnWidthChanged();
115 if (isComponentComplete()) {
116 Q_FOREACH(const auto &column, m_columnVisibleItems) {
117 Q_FOREACH(const ViewItem &item, column) {
118 item.m_item->setWidth(columnWidth);
126qreal VerticalJournal::columnSpacing() const
128 return m_columnSpacing;
131void VerticalJournal::setColumnSpacing(qreal columnSpacing)
133 if (columnSpacing != m_columnSpacing) {
134 m_columnSpacing = columnSpacing;
135 Q_EMIT columnSpacingChanged();
137 if (isComponentComplete()) {
143qreal VerticalJournal::rowSpacing() const
148void VerticalJournal::setRowSpacing(qreal rowSpacing)
150 if (rowSpacing != m_rowSpacing) {
151 m_rowSpacing = rowSpacing;
152 Q_EMIT rowSpacingChanged();
154 if (isComponentComplete()) {
160qreal VerticalJournal::delegateCreationBegin() const
162 return m_delegateCreationBegin;
165void VerticalJournal::setDelegateCreationBegin(qreal begin)
167 m_delegateCreationBeginValid = true;
168 if (m_delegateCreationBegin == begin)
170 m_delegateCreationBegin = begin;
171 if (isComponentComplete()) {
174 emit delegateCreationBeginChanged();
177void VerticalJournal::resetDelegateCreationBegin()
179 m_delegateCreationBeginValid = false;
180 if (m_delegateCreationBegin == 0)
182 m_delegateCreationBegin = 0;
183 if (isComponentComplete()) {
186 emit delegateCreationBeginChanged();
189qreal VerticalJournal::delegateCreationEnd() const
191 return m_delegateCreationEnd;
194void VerticalJournal::setDelegateCreationEnd(qreal end)
196 m_delegateCreationEndValid = true;
197 if (m_delegateCreationEnd == end)
199 m_delegateCreationEnd = end;
200 if (isComponentComplete()) {
203 emit delegateCreationEndChanged();
206void VerticalJournal::resetDelegateCreationEnd()
208 m_delegateCreationEndValid = false;
209 if (m_delegateCreationEnd == 0)
211 m_delegateCreationEnd = 0;
212 if (isComponentComplete()) {
215 emit delegateCreationEndChanged();
218void VerticalJournal::createDelegateModel()
220#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
221 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
222 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
224 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
225 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
227 if (isComponentComplete())
228 m_delegateModel->componentComplete();
231void VerticalJournal::refill()
233 if (!isComponentComplete()) {
237 const bool delegateRangesValid = m_delegateCreationBeginValid && m_delegateCreationEndValid;
238 const qreal from = delegateRangesValid ? m_delegateCreationBegin : 0;
239 const qreal to = delegateRangesValid ? m_delegateCreationEnd : from + height();
240 const qreal buffer = (to - from) * bufferRatio;
241 const qreal bufferFrom = from - buffer;
242 const qreal bufferTo = to + buffer;
244 bool added = addVisibleItems(from, to, false);
245 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
246 added |= addVisibleItems(bufferFrom, bufferTo, true);
248 if (added || removed) {
249 m_implicitHeightDirty = true;
253void VerticalJournal::findBottomModelIndexToAdd(int *modelIndex, double *yPos)
258 Q_FOREACH(const auto &column, m_columnVisibleItems) {
259 if (!column.isEmpty()) {
260 const ViewItem &item = column.last();
261 *yPos = qMin(*yPos, static_cast<double>(item.y() + item.height() + m_rowSpacing));
262 *modelIndex = qMax(*modelIndex, item.m_modelIndex + 1);
269void VerticalJournal::findTopModelIndexToAdd(int *modelIndex, double *yPos)
271 *modelIndex = INT_MAX;
273 int columnToAddTo = -1;
275 // Find the topmost free column
276 for (int i = 0; i < m_columnVisibleItems.count(); ++i) {
277 const auto &column = m_columnVisibleItems[i];
278 if (!column.isEmpty()) {
279 const ViewItem &item = column.first();
280 const auto itemTopPos = item.y() - m_rowSpacing;
281 if (itemTopPos > *yPos) {
283 *modelIndex = item.m_modelIndex - 1;
289 if (*modelIndex > 0) {
290 Q_ASSERT(m_indexColumnMap.contains(*modelIndex));
291 while (m_indexColumnMap[*modelIndex] != columnToAddTo) {
292 // We found out that we have to add to columnToAddTo
293 // and thought that we had to add *modelIndex, but history tells
294 // it is not correct, so find up from *modelIndex until we found the index
295 // that has to end up in columnToAddTo
296 *modelIndex = *modelIndex - 1;
297 Q_ASSERT(m_indexColumnMap.contains(*modelIndex));
302bool VerticalJournal::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
307 if (m_delegateModel->count() == 0)
312 findBottomModelIndexToAdd(&modelIndex, &yPos);
313 bool changed = false;
314 while (modelIndex < m_delegateModel->count() && yPos <= fillTo) {
315 if (!createItem(modelIndex, asynchronous))
319 findBottomModelIndexToAdd(&modelIndex, &yPos);
322 findTopModelIndexToAdd(&modelIndex, &yPos);
323 while (modelIndex >= 0 && yPos > fillFrom) {
324 if (!createItem(modelIndex, asynchronous))
328 findTopModelIndexToAdd(&modelIndex, &yPos);
334bool VerticalJournal::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
336 bool changed = false;
338 for (int i = 0; i < m_columnVisibleItems.count(); ++i) {
339 QList<ViewItem> &column = m_columnVisibleItems[i];
340 while (!column.isEmpty() && column.first().y() + column.first().height() < bufferFrom) {
341 releaseItem(column.takeFirst());
345 while (!column.isEmpty() && column.last().y() > bufferTo) {
346 releaseItem(column.takeLast());
354QQuickItem *VerticalJournal::createItem(int modelIndex, bool asynchronous)
356 if (asynchronous && m_asyncRequestedIndex != -1)
359 m_asyncRequestedIndex = -1;
360#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
361 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
363 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
364 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
367#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
368 m_asyncRequestedIndex = modelIndex;
371 m_delegateModel->release(object);
372 if (!m_delegateValidated) {
373 m_delegateValidated = true;
374 QObject* delegateObj = delegate();
375 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
378 m_asyncRequestedIndex = modelIndex;
383 if (item->width() != m_columnWidth) {
384 qWarning() << "Item" << modelIndex << "width is not the one that the columnWidth mandates, resetting it";
385 item->setWidth(m_columnWidth);
387 positionItem(modelIndex, item);
392void VerticalJournal::positionItem(int modelIndex, QQuickItem *item)
394 // Check if we add it to the bottom of existing column items
395 const QList<ViewItem> &firstColumn = m_columnVisibleItems[0];
396 qreal columnToAddY = !firstColumn.isEmpty() ? firstColumn.last().y() + firstColumn.last().height() : -m_rowSpacing;
397 int columnToAddTo = 0;
398 for (int i = 1; i < m_columnVisibleItems.count(); ++i) {
399 const QList<ViewItem> &column = m_columnVisibleItems[i];
400 const qreal iY = !column.isEmpty() ? column.last().y() + column.last().height() : -m_rowSpacing;
401 if (iY < columnToAddY) {
407 const QList<ViewItem> &columnToAdd = m_columnVisibleItems[columnToAddTo];
408 if (columnToAdd.isEmpty() || columnToAdd.last().m_modelIndex < modelIndex) {
409 item->setX(columnToAddTo * (m_columnWidth + m_columnSpacing));
410 item->setY(columnToAddY + m_rowSpacing);
412 m_columnVisibleItems[columnToAddTo] << ViewItem(item, modelIndex);
413 m_indexColumnMap[modelIndex] = columnToAddTo;
415 Q_ASSERT(m_indexColumnMap.contains(modelIndex));
416 columnToAddTo = m_indexColumnMap[modelIndex];
417 columnToAddY = m_columnVisibleItems[columnToAddTo].first().y();
419 item->setX(columnToAddTo * (m_columnWidth + m_columnSpacing));
420 item->setY(columnToAddY - m_rowSpacing - item->height());
422 m_columnVisibleItems[columnToAddTo].prepend(ViewItem(item, modelIndex));
426void VerticalJournal::cleanupExistingItems()
428 // Cleanup the existing items
429 for (int i = 0; i < m_columnVisibleItems.count(); ++i) {
430 QList<ViewItem> &column = m_columnVisibleItems[i];
431 Q_FOREACH(const ViewItem &item, column)
435 m_indexColumnMap.clear();
438void VerticalJournal::releaseItem(const ViewItem &item)
440#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
441 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item.m_item);
442 if (flags & QQuickVisualModel::Destroyed) {
444 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item.m_item);
445 if (flags & QQmlDelegateModel::Destroyed) {
447 item.m_item->setParentItem(nullptr);
451void VerticalJournal::calculateImplicitHeight()
453 m_implicitHeightDirty = false;
455 int lastModelIndex = -1;
456 qreal bottomMostY = 0;
457 Q_FOREACH(const auto &column, m_columnVisibleItems) {
458 if (!column.isEmpty()) {
459 const ViewItem &item = column.last();
460 lastModelIndex = qMax(lastModelIndex, item.m_modelIndex);
461 bottomMostY = qMax(bottomMostY, item.y() + item.height());
464 if (lastModelIndex >= 0) {
465 const double averageHeight = bottomMostY / (lastModelIndex + 1);
466 setImplicitHeight(bottomMostY + averageHeight * (model()->rowCount() - lastModelIndex - 1));
468 setImplicitHeight(0);
472#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
473void VerticalJournal::itemCreated(int modelIndex, QQuickItem *item)
476void VerticalJournal::itemCreated(int modelIndex, QObject *object)
478 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
480 qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
484 item->setParentItem(this);
485 if (modelIndex == m_asyncRequestedIndex) {
486 createItem(modelIndex, false);
487 m_implicitHeightDirty = true;
492void VerticalJournal::relayout()
494 m_needsRelayout = true;
498void VerticalJournal::onHeightChanged()
503void VerticalJournal::updatePolish()
508 if (m_needsRelayout) {
509 QList<ViewItem> allItems;
510 Q_FOREACH(const auto &column, m_columnVisibleItems)
515 const int nColumns = qMax(1., floor((double)(width() + m_columnSpacing) / (m_columnWidth + m_columnSpacing)));
516 m_columnVisibleItems.resize(nColumns);
517 m_indexColumnMap.clear();
518 for (int i = 0; i < nColumns; ++i)
519 m_columnVisibleItems[i].clear();
521 // If the first of allItems doesn't contain index 0 we need to drop them
522 // all since we can't consistently relayout without the first item being there
524 if (!allItems.isEmpty()) {
525 if (allItems.first().m_modelIndex == 0) {
526 Q_FOREACH(const ViewItem &item, allItems)
527 positionItem(item.m_modelIndex, item.m_item);
529 Q_FOREACH(const ViewItem &item, allItems)
534 m_needsRelayout = false;
539 const bool delegateRangesValid = m_delegateCreationBeginValid && m_delegateCreationEndValid;
540 const qreal from = delegateRangesValid ? m_delegateCreationBegin : 0;
541 const qreal to = delegateRangesValid ? m_delegateCreationEnd : from + height();
542 Q_FOREACH(const auto &column, m_columnVisibleItems) {
543 Q_FOREACH(const ViewItem &item, column) {
544 QQuickItemPrivate::get(item.m_item)->setCulled(item.y() + item.height() <= from || item.y() >= to);
548 if (m_implicitHeightDirty) {
549 calculateImplicitHeight();
553void VerticalJournal::componentComplete()
556 m_delegateModel->componentComplete();
558 QQuickItem::componentComplete();
560 m_needsRelayout = true;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list