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
36HorizontalJournal::HorizontalJournal()
37 : m_firstVisibleIndex(-1)
42qreal HorizontalJournal::rowHeight() const
47void HorizontalJournal::setRowHeight(qreal rowHeight)
49 if (rowHeight != m_rowHeight) {
50 m_rowHeight = rowHeight;
51 Q_EMIT rowHeightChanged();
53 if (isComponentComplete()) {
54 Q_FOREACH(QQuickItem *item, m_visibleItems) {
55 item->setHeight(rowHeight);
62void HorizontalJournal::findBottomModelIndexToAdd(int *modelIndex, qreal *yPos)
64 if (m_visibleItems.isEmpty()) {
68 *modelIndex = m_firstVisibleIndex + m_visibleItems.count();
69 if (m_lastInRowIndexPosition.contains(*modelIndex - 1)) {
70 *yPos = m_visibleItems.last()->y() + m_rowHeight + rowSpacing();
72 *yPos = m_visibleItems.last()->y();
77void HorizontalJournal::findTopModelIndexToAdd(int *modelIndex, qreal *yPos)
79 if (m_visibleItems.isEmpty()) {
83 *modelIndex = m_firstVisibleIndex - 1;
84 if (m_lastInRowIndexPosition.contains(*modelIndex)) {
85 *yPos = m_visibleItems.first()->y() - rowSpacing() - m_rowHeight;
87 *yPos = m_visibleItems.first()->y();
92bool HorizontalJournal::removeNonVisibleItems(qreal bufferFromY, qreal bufferToY)
96 while (!m_visibleItems.isEmpty() && m_visibleItems.first()->y() + m_rowHeight < bufferFromY) {
97 releaseItem(m_visibleItems.takeFirst());
99 m_firstVisibleIndex++;
102 while (!m_visibleItems.isEmpty() && m_visibleItems.last()->y() > bufferToY) {
103 releaseItem(m_visibleItems.takeLast());
105 m_lastInRowIndexPosition.remove(m_firstVisibleIndex + m_visibleItems.count());
108 if (m_visibleItems.isEmpty()) {
109 m_firstVisibleIndex = -1;
115void HorizontalJournal::addItemToView(int modelIndex, QQuickItem *item)
117 if (item->height() != m_rowHeight) {
118 qWarning() << "Item" << modelIndex << "height is not the one that the rowHeight mandates, resetting it";
119 item->setHeight(m_rowHeight);
122 if (m_visibleItems.isEmpty()) {
123 Q_ASSERT(modelIndex == 0);
126 m_visibleItems << item;
127 m_firstVisibleIndex = 0;
129 // modelIndex has to be either m_firstVisibleIndex - 1 or m_firstVisibleIndex + m_visibleItems.count()
130 if (modelIndex == m_firstVisibleIndex + m_visibleItems.count()) {
131 QQuickItem *lastItem = m_visibleItems.last();
132 if (lastItem->x() + lastItem->width() + columnSpacing() + item->width() <= width()) {
134 item->setY(lastItem->y());
135 item->setX(lastItem->x() + lastItem->width() + columnSpacing());
138 item->setY(lastItem->y() + m_rowHeight + rowSpacing());
140 m_lastInRowIndexPosition[modelIndex - 1] = lastItem->x();
142 m_visibleItems << item;
143 } else if (modelIndex == m_firstVisibleIndex - 1) {
144 QQuickItem *firstItem = m_visibleItems.first();
145 if (m_lastInRowIndexPosition.contains(modelIndex)) {
146 // It is the last item of its row, so start a new one since we're going back
147 item->setY(firstItem->y() - rowSpacing() - m_rowHeight);
148 item->setX(m_lastInRowIndexPosition[modelIndex]);
150 item->setY(firstItem->y());
151 item->setX(firstItem->x() - columnSpacing() - item->width());
153 m_firstVisibleIndex = modelIndex;
154 m_visibleItems.prepend(item);
156 qWarning() << "HorizontalJournal::addItemToView - Got unexpected modelIndex"
157 << modelIndex << m_firstVisibleIndex << m_visibleItems.count();
162void HorizontalJournal::cleanupExistingItems()
164 // Cleanup the existing items
165 Q_FOREACH(QQuickItem *item, m_visibleItems)
167 m_visibleItems.clear();
168 m_lastInRowIndexPosition.clear();
169 m_firstVisibleIndex = -1;
170 setImplicitHeightDirty();
173void HorizontalJournal::calculateImplicitHeight()
175 if (m_firstVisibleIndex >= 0) {
176 const int nIndexes = m_firstVisibleIndex + m_visibleItems.count();
177 const double bottomMostY = m_visibleItems.last()->y() + m_rowHeight;
178 const double averageHeight = bottomMostY / nIndexes;
179 setImplicitHeight(bottomMostY + averageHeight * (model()->rowCount() - nIndexes));
181 setImplicitHeight(0);
185void HorizontalJournal::processModelRemoves(const QVector<QQmlChangeSet::Change> &removes)
187 Q_FOREACH(const QQmlChangeSet::Change remove, removes) {
188 for (int i = remove.count - 1; i >= 0; --i) {
189 const int indexToRemove = remove.index + i;
190 // We only support removing from the end so
191 // any of the last items of a column has to be indexToRemove
192 const int lastIndex = m_firstVisibleIndex + m_visibleItems.count() - 1;
193 if (indexToRemove == lastIndex) {
194 releaseItem(m_visibleItems.takeLast());
195 m_lastInRowIndexPosition.remove(indexToRemove);
197 if (indexToRemove < lastIndex) {
198 qDebug() << "HorizontalJournal only supports removal from the end of the model, resetting instead";
199 cleanupExistingItems();
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