File indexing completed on 2025-03-09 03:59:02

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-08-22
0007  * Description : Reimplemented QListView for metadata setup, with support for
0008  *               drag-n-drop
0009  *
0010  * SPDX-FileCopyrightText: 2013-2015 by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "namespacelistview.h"
0017 
0018 // Qt includes
0019 
0020 #include <QDrag>
0021 #include <QDropEvent>
0022 #include <QMimeData>
0023 #include <QItemSelectionModel>
0024 #include <QMenu>
0025 #include <QAction>
0026 #include <QIcon>
0027 #include <QStandardItem>
0028 #include <QStandardItemModel>
0029 
0030 // KDE includes
0031 
0032 #include <klocalizedstring.h>
0033 
0034 // Local includes
0035 
0036 #include "digikam_debug.h"
0037 
0038 namespace Digikam
0039 {
0040 
0041 NamespaceListView::NamespaceListView(QWidget* const parent)
0042     : QListView(parent)
0043 {
0044     setAlternatingRowColors(true);
0045     setAcceptDrops(true);
0046     setDragEnabled(true);
0047     setDragDropMode(QAbstractItemView::InternalMove);
0048     setSelectionMode(QAbstractItemView::SingleSelection);
0049 }
0050 
0051 void NamespaceListView::startDrag(Qt::DropActions supportedActions)
0052 {
0053     QListView::startDrag(supportedActions);
0054 }
0055 
0056 QModelIndexList NamespaceListView::mySelectedIndexes()
0057 {
0058     return selectedIndexes();
0059 }
0060 
0061 void NamespaceListView::dropEvent(QDropEvent* e)
0062 {
0063     QListView::dropEvent(e);
0064 
0065     Q_EMIT signalItemsChanged();
0066 }
0067 
0068 QModelIndex NamespaceListView::indexVisuallyAt(const QPoint& p)
0069 {
0070     if (viewport()->rect().contains(p))
0071     {
0072         QModelIndex index = indexAt(p);
0073 
0074         if (index.isValid() && visualRect(index).contains(p))
0075         {
0076             return index;
0077         }
0078     }
0079 
0080     return QModelIndex();
0081 }
0082 
0083 void NamespaceListView::slotDeleteSelected()
0084 {
0085     QModelIndexList sel = selectionModel()->selectedIndexes();
0086 
0087     if (sel.isEmpty())
0088     {
0089         return;
0090     }
0091 
0092     QStandardItemModel* const model = dynamic_cast<QStandardItemModel*>(this->model());
0093 
0094     if (!model)
0095     {
0096         qCDebug(DIGIKAM_GENERAL_LOG) << "Error! no model available!";
0097         return;
0098     }
0099 
0100     Q_FOREACH (const QModelIndex& index, sel)
0101     {
0102         QStandardItem* const root = model->invisibleRootItem();
0103         root->removeRow(index.row());
0104     }
0105 
0106     Q_EMIT signalItemsChanged();
0107 }
0108 
0109 void NamespaceListView::slotMoveItemDown()
0110 {
0111     QModelIndexList sel = selectionModel()->selectedIndexes();
0112 
0113     if (sel.isEmpty())
0114     {
0115         return;
0116     }
0117 
0118     QStandardItemModel* const model = dynamic_cast<QStandardItemModel*>(this->model());
0119 
0120     if (!model)
0121     {
0122         qCDebug(DIGIKAM_GENERAL_LOG) << "Error! no model available!";
0123         return;
0124     }
0125 
0126     QModelIndex index         = sel.first();
0127     QStandardItem* const root = model->invisibleRootItem();
0128 
0129     if (index.row() == (root->rowCount() - 1))
0130     {
0131         return;
0132     }
0133 
0134     root->insertRow(index.row() + 1, root->takeRow(index.row()));
0135     setCurrentIndex(model->index(index.row() + 1, index.column(), index.parent()));
0136 
0137     Q_EMIT signalItemsChanged();
0138 }
0139 
0140 void NamespaceListView::slotMoveItemUp()
0141 {
0142     QModelIndexList sel = selectionModel()->selectedIndexes();
0143 
0144     if (sel.isEmpty())
0145     {
0146         return;
0147     }
0148 
0149     QStandardItemModel* const model = dynamic_cast<QStandardItemModel*>(this->model());
0150 
0151     if (!model)
0152     {
0153         qCDebug(DIGIKAM_GENERAL_LOG) << "Error! no model available!";
0154         return;
0155     }
0156 
0157     QModelIndex index         = sel.first();
0158     QStandardItem* const root = model->invisibleRootItem();
0159 
0160     if (index.row() == 0)
0161     {
0162         return;
0163     }
0164 
0165     root->insertRow(index.row() - 1, root->takeRow(index.row()));
0166     setCurrentIndex(model->index(index.row() - 1, index.column(), index.parent()));
0167 
0168     Q_EMIT signalItemsChanged();
0169 }
0170 
0171 } // namespace Digikam
0172 
0173 #include "moc_namespacelistview.cpp"