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_visibleItems
19 * that a list for each of the items in the view.
20 * m_firstVisibleIndex is the index of the first item in m_visibleItems
21 * m_lastInRowIndexPosition is a map that contains the x position
22 * of items that are the last ones of a row so we can reconstruct the rows
26#include "horizontaljournal.h"
28#include <qqmlengine.h>
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-pedantic"
31#include <private/qqmldelegatemodel_p.h>
33#include <private/qquickitem_p.h>
34#pragma GCC diagnostic pop
36static const qreal bufferRatio = 0.5;
38HorizontalJournal::HorizontalJournal()
39 : m_firstVisibleIndex(-1)
44qreal HorizontalJournal::rowHeight() const
49void HorizontalJournal::setRowHeight(qreal rowHeight)
51 if (rowHeight != m_rowHeight) {
52 m_rowHeight = rowHeight;
53 Q_EMIT rowHeightChanged();
55 if (isComponentComplete()) {
56 Q_FOREACH(QQuickItem *item, m_visibleItems) {
57 item->setHeight(rowHeight);
64void HorizontalJournal::findBottomModelIndexToAdd(int *modelIndex, qreal *yPos)
66 if (m_visibleItems.isEmpty()) {
70 *modelIndex = m_firstVisibleIndex + m_visibleItems.count();
71 if (m_lastInRowIndexPosition.contains(*modelIndex - 1)) {
72 *yPos = m_visibleItems.last()->y() + m_rowHeight + rowSpacing();
74 *yPos = m_visibleItems.last()->y();
79void HorizontalJournal::findTopModelIndexToAdd(int *modelIndex, qreal *yPos)
81 if (m_visibleItems.isEmpty()) {
85 *modelIndex = m_firstVisibleIndex - 1;
86 if (m_lastInRowIndexPosition.contains(*modelIndex)) {
87 *yPos = m_visibleItems.first()->y() - rowSpacing() - m_rowHeight;
89 *yPos = m_visibleItems.first()->y();
94bool HorizontalJournal::removeNonVisibleItems(qreal bufferFromY, qreal bufferToY)
98 while (!m_visibleItems.isEmpty() && m_visibleItems.first()->y() + m_rowHeight < bufferFromY) {
99 releaseItem(m_visibleItems.takeFirst());
101 m_firstVisibleIndex++;
104 while (!m_visibleItems.isEmpty() && m_visibleItems.last()->y() > bufferToY) {
105 releaseItem(m_visibleItems.takeLast());
107 m_lastInRowIndexPosition.remove(m_firstVisibleIndex + m_visibleItems.count());
110 if (m_visibleItems.isEmpty()) {
111 m_firstVisibleIndex = -1;
117void HorizontalJournal::addItemToView(int modelIndex, QQuickItem *item)
119 if (item->height() != m_rowHeight) {
120 qWarning() << "Item" << modelIndex << "height is not the one that the rowHeight mandates, resetting it";
121 item->setHeight(m_rowHeight);
124 if (m_visibleItems.isEmpty()) {
125 Q_ASSERT(modelIndex == 0);
128 m_visibleItems << item;
129 m_firstVisibleIndex = 0;
131 // modelIndex has to be either m_firstVisibleIndex - 1 or m_firstVisibleIndex + m_visibleItems.count()
132 if (modelIndex == m_firstVisibleIndex + m_visibleItems.count()) {
133 QQuickItem *lastItem = m_visibleItems.last();
134 if (lastItem->x() + lastItem->width() + columnSpacing() + item->width() <= width()) {
136 item->setY(lastItem->y());
137 item->setX(lastItem->x() + lastItem->width() + columnSpacing());
140 item->setY(lastItem->y() + m_rowHeight + rowSpacing());
142 m_lastInRowIndexPosition[modelIndex - 1] = lastItem->x();
144 m_visibleItems << item;
145 } else if (modelIndex == m_firstVisibleIndex - 1) {
146 QQuickItem *firstItem = m_visibleItems.first();
147 if (m_lastInRowIndexPosition.contains(modelIndex)) {
148 // It is the last item of its row, so start a new one since we're going back
149 item->setY(firstItem->y() - rowSpacing() - m_rowHeight);
150 item->setX(m_lastInRowIndexPosition[modelIndex]);
152 item->setY(firstItem->y());
153 item->setX(firstItem->x() - columnSpacing() - item->width());
155 m_firstVisibleIndex = modelIndex;
156 m_visibleItems.prepend(item);
158 qWarning() << "HorizontalJournal::addItemToView - Got unexpected modelIndex"
159 << modelIndex << m_firstVisibleIndex << m_visibleItems.count();
164void HorizontalJournal::cleanupExistingItems()
166 // Cleanup the existing items
167 Q_FOREACH(QQuickItem *item, m_visibleItems)
169 m_visibleItems.clear();
170 m_lastInRowIndexPosition.clear();
171 m_firstVisibleIndex = -1;
172 setImplicitHeightDirty();
175void HorizontalJournal::calculateImplicitHeight()
177 if (m_firstVisibleIndex >= 0) {
178 const int nIndexes = m_firstVisibleIndex + m_visibleItems.count();
179 const double bottomMostY = m_visibleItems.last()->y() + m_rowHeight;
180 const double averageHeight = bottomMostY / nIndexes;
181 setImplicitHeight(bottomMostY + averageHeight * (model()->rowCount() - nIndexes));
183 setImplicitHeight(0);
187void HorizontalJournal::processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes)
189 Q_FOREACH(const QQmlChangeSet::Remove &remove, removes) {
190 for (int i = remove.count - 1; i >= 0; --i) {
191 const int indexToRemove = remove.index + i;
192 // We only support removing from the end so
193 // any of the last items of a column has to be indexToRemove
194 const int lastIndex = m_firstVisibleIndex + m_visibleItems.count() - 1;
195 if (indexToRemove == lastIndex) {
196 releaseItem(m_visibleItems.takeLast());
197 m_lastInRowIndexPosition.remove(indexToRemove);
199 if (indexToRemove < lastIndex) {
200 qFatal("HorizontalJournal only supports removal from the end of the model");
202 setImplicitHeightDirty();
207 if (m_visibleItems.isEmpty()) {
208 m_firstVisibleIndex = -1;
213void HorizontalJournal::doRelayout()
215 // If m_firstVisibleIndex is not 0 we need to drop all the delegates
216 // since we can't consistently relayout without the first item being there
218 if (m_firstVisibleIndex == 0) {
220 const QList<QQuickItem*> allItems = m_visibleItems;
221 m_visibleItems.clear();
222 m_lastInRowIndexPosition.clear();
223 Q_FOREACH(QQuickItem *item, allItems) {
224 addItemToView(i, item);
228 Q_FOREACH(QQuickItem *item, m_visibleItems) {
231 m_visibleItems.clear();
232 m_lastInRowIndexPosition.clear();
233 m_firstVisibleIndex = -1;
237void HorizontalJournal::updateItemCulling(qreal visibleFromY, qreal visibleToY)
239 Q_FOREACH(QQuickItem *item, m_visibleItems) {
240 QQuickItemPrivate::get(item)->setCulled(item->y() + m_rowHeight <= visibleFromY || item->y() >= visibleToY);
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