Commit 92ffcf96 authored by Marcel Wiesweg's avatar Marcel Wiesweg
Browse files

Some work to stay on the same photo if one of the faces is removed,

unfortunately not sufficient yet.

CCBUG: 265025
parent f756b5a9
......@@ -362,6 +362,40 @@ ImageInfo ImageCategorizedView::nextInOrder(const ImageInfo& startingPoint, int
return d->filterModel->imageInfo(d->filterModel->index(index.row() + nth, 0, QModelIndex()));
}
QModelIndex ImageCategorizedView::nextIndexHint(const QModelIndex& anchor, const QItemSelectionRange& removed) const
{
QModelIndex hint = DCategorizedView::nextIndexHint(anchor, removed);
ImageInfo info = d->filterModel->imageInfo(anchor);
kDebug() << "Having initial hint" << hint << "for" << anchor << d->model->numberOfIndexesForImageInfo(info);
// Fixes a special case of multiple (face) entries for the same image.
// If one is removed, any entry of the same image shall be preferred.
if (d->model->numberOfIndexesForImageInfo(info) > 1)
{
// The hint is for a different info, but we may have a hint for the same info
if (info != d->filterModel->imageInfo(hint))
{
int minDiff = d->filterModel->rowCount();
QList<QModelIndex> indexesForImageInfo =
d->filterModel->mapListFromSource(d->model->indexesForImageInfo(info));
foreach (const QModelIndex& index, indexesForImageInfo)
{
if (index == anchor || !index.isValid() || removed.contains(index))
{
continue;
}
int distance = qAbs(index.row() - anchor.row());
if (distance < minDiff)
{
minDiff = distance;
hint = index;
kDebug() << "Chose index" << hint << "at distance" << minDiff << "to" << anchor;
}
}
}
}
return hint;
}
void ImageCategorizedView::openAlbum(Album* album)
{
ImageAlbumModel* albumModel = imageAlbumModel();
......
......@@ -172,6 +172,7 @@ protected:
void selectionChanged(const QItemSelection&, const QItemSelection&);
void updateGeometries();
AbstractItemDragDropHandler* dragDropHandler() const;
QModelIndex nextIndexHint(const QModelIndex& indexToAnchor, const QItemSelectionRange& removed) const;
private Q_SLOTS:
......
......@@ -258,6 +258,28 @@ QList<QModelIndex> ImageModel::indexesForImageId(qlonglong id) const
return indexes;
}
int ImageModel::numberOfIndexesForImageInfo(const ImageInfo& info) const
{
return numberOfIndexesForImageId(info.id());
}
int ImageModel::numberOfIndexesForImageId(qlonglong id) const
{
if (d->extraValues.isEmpty())
{
return 0;
}
int count = 0;
QHash<qlonglong,int>::iterator it;
for (it = d->idHash.find(id); it != d->idHash.end() && it.key() == id; ++it)
{
++count;
}
return count;
}
// static method
ImageInfo ImageModel::retrieveImageInfo(const QModelIndex& index)
{
......
......@@ -117,6 +117,8 @@ public:
QModelIndex indexForImageId(qlonglong id, const QVariant& extraValue) const;
QList<QModelIndex> indexesForImageInfo(const ImageInfo& info) const;
QList<QModelIndex> indexesForImageId(qlonglong id) const;
int numberOfIndexesForImageInfo(const ImageInfo& info) const;
int numberOfIndexesForImageId(qlonglong id) const;
/** Returns the index or ImageInfo object from the underlying data
* for the given file path. This is fast if keepsFilePathCache is enabled.
* The file path is as returned by ImageInfo.filePath().
......
......@@ -420,22 +420,24 @@ void DCategorizedView::rowsAboutToBeRemoved(const QModelIndex& parent, int start
{
// find out which selected indexes are left after rows are removed
QItemSelection selected = selectionModel()->selection();
QModelIndex current = currentIndex();
QModelIndex indexToAnchor;
if (selected.contains(current))
{
indexToAnchor = current;
}
else if (!selected.isEmpty())
{
indexToAnchor = selected.first().topLeft();
}
QItemSelection removed(model()->index(start, 0), model()->index(end, 0));
selected.merge(removed, QItemSelectionModel::Deselect);
if (selected.isEmpty())
{
QModelIndex newCurrent;
if (end == model()->rowCount(parent) - 1)
{
newCurrent = model()->index(start - 1, 0); // last remaining, no next one left
}
else
{
newCurrent = model()->index(end + 1, 0); // next remaining
}
QModelIndex newCurrent = nextIndexHint(indexToAnchor, removed.first());
selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::SelectCurrent);
}
}
......@@ -444,6 +446,7 @@ void DCategorizedView::rowsAboutToBeRemoved(const QModelIndex& parent, int start
void DCategorizedView::layoutAboutToBeChanged()
{
d->ensureOneSelectedItem = selectionModel()->hasSelection();
kDebug() << d->ensureOneSelectedItem;
// store some hints so that if all selected items were removed dont need to default to 0,0.
if (d->ensureOneSelectedItem)
......@@ -464,12 +467,28 @@ void DCategorizedView::layoutAboutToBeChanged()
if (indexToAnchor.isValid())
{
d->hintAtSelectionRow = indexToAnchor.row();
d->hintAtSelectionIndex = model()->index(d->hintAtSelectionRow == model()->rowCount()
? d->hintAtSelectionRow : d->hintAtSelectionRow + 1, 0);
d->hintAtSelectionIndex = nextIndexHint(indexToAnchor, QItemSelectionRange(indexToAnchor));
}
}
}
QModelIndex DCategorizedView::nextIndexHint(const QModelIndex& indexToAnchor, const QItemSelectionRange& removed) const
{
Q_UNUSED(indexToAnchor);
if (removed.bottomRight().row() == model()->rowCount() - 1)
{
if (removed.topLeft().row() == 0)
{
return QModelIndex();
}
return model()->index(removed.topLeft().row() - 1, 0); // last remaining, no next one left
}
else
{
return model()->index(removed.bottomRight().row() + 1, 0); // next remaining
}
}
void DCategorizedView::layoutWasChanged()
{
// connected queued to layoutChanged()
......
......@@ -146,6 +146,13 @@ protected:
virtual QModelIndex mapIndexForDragDrop(const QModelIndex& index) const;
virtual QPixmap pixmapForDrag(const QList<QModelIndex>& indexes) const;
/**
* Assuming the given indexes would be removed (hypothetically!),
* return the index to be selected instead, starting from anchor.
* The default implementation returns the next remaining sibling.
*/
virtual QModelIndex nextIndexHint(const QModelIndex& indexToAnchor, const QItemSelectionRange& removed) const;
// reimplemented from parent class
void contextMenuEvent(QContextMenuEvent* event);
void keyPressEvent(QKeyEvent* event);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment