File indexing completed on 2025-01-19 03:55:42

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-05-21
0007  * Description : widget to display a list of items
0008  *
0009  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2008-2010 by Andi Clemens <andi dot clemens at googlemail dot com>
0011  * SPDX-FileCopyrightText: 2009-2010 by Luka Renko <lure at kubuntu dot org>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "ditemslist_p.h"
0018 
0019 namespace Digikam
0020 {
0021 
0022 DItemsListView::DItemsListView(DItemsList* const parent)
0023     : QTreeWidget (parent),
0024       m_itemDraged(nullptr)
0025 {
0026     setRootIsDecorated(false);
0027     setItemsExpandable(false);
0028     setUniformRowHeights(true);
0029     setAlternatingRowColors(true);
0030     setExpandsOnDoubleClick(false);
0031     setSelectionMode(QAbstractItemView::ExtendedSelection);
0032 
0033     enableDragAndDrop(true);
0034 
0035     setSortingEnabled(false);
0036     setAllColumnsShowFocus(true);
0037     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0038 
0039     setColumnCount(8);
0040     setHeaderLabels(QStringList() << i18nc("@title", "Thumbnail")
0041                                   << i18nc("@title", "File Name")
0042                                   << i18nc("@title", "User1")
0043                                   << i18nc("@title", "User2")
0044                                   << i18nc("@title", "User3")
0045                                   << i18nc("@title", "User4")
0046                                   << i18nc("@title", "User5")
0047                                   << i18nc("@title", "User6"));
0048     hideColumn(User1);
0049     hideColumn(User2);
0050     hideColumn(User3);
0051     hideColumn(User4);
0052     hideColumn(User5);
0053     hideColumn(User6);
0054 
0055     header()->setSectionResizeMode(User1, QHeaderView::Interactive);
0056     header()->setSectionResizeMode(User2, QHeaderView::Interactive);
0057     header()->setSectionResizeMode(User3, QHeaderView::Interactive);
0058     header()->setSectionResizeMode(User4, QHeaderView::Interactive);
0059     header()->setSectionResizeMode(User5, QHeaderView::Interactive);
0060     header()->setSectionResizeMode(User6, QHeaderView::Stretch);
0061 
0062     connect(this, &DItemsListView::itemClicked,
0063             this, &DItemsListView::slotItemClicked);
0064 }
0065 
0066 DItemsListView::~DItemsListView()
0067 {
0068 }
0069 
0070 DInfoInterface* DItemsListView::iface() const
0071 {
0072     DItemsList* const p = dynamic_cast<DItemsList*>(parent());
0073 
0074     if (p)
0075     {
0076         return p->iface();
0077     }
0078 
0079     return nullptr;
0080 }
0081 
0082 void DItemsListView::enableDragAndDrop(const bool enable)
0083 {
0084     setDragEnabled(enable);
0085     viewport()->setAcceptDrops(enable);
0086     setDragDropMode(enable ? QAbstractItemView::InternalMove : QAbstractItemView::NoDragDrop);
0087     setDragDropOverwriteMode(enable);
0088     setDropIndicatorShown(enable);
0089 }
0090 
0091 void DItemsListView::drawRow(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const
0092 {
0093     DItemsListViewItem* const item = dynamic_cast<DItemsListViewItem*>(itemFromIndex(index));
0094 
0095     if (item && !item->hasValidThumbnail())
0096     {
0097         DItemsList* const view = dynamic_cast<DItemsList*>(parent());
0098 
0099         if (view)
0100         {
0101             view->updateThumbnail(item->url());
0102         }
0103     }
0104 
0105     QTreeWidget::drawRow(p, opt, index);
0106 }
0107 
0108 void DItemsListView::slotItemClicked(QTreeWidgetItem* item, int column)
0109 {
0110     Q_UNUSED(column)
0111 
0112     if (!item)
0113     {
0114         return;
0115     }
0116 
0117     Q_EMIT signalItemClicked(item);
0118 }
0119 
0120 void DItemsListView::setColumnLabel(ColumnType column, const QString& label)
0121 {
0122     headerItem()->setText(column, label);
0123 }
0124 
0125 void DItemsListView::setColumnEnabled(ColumnType column, bool enable)
0126 {
0127     if (enable)
0128     {
0129         showColumn(column);
0130     }
0131     else
0132     {
0133         hideColumn(column);
0134     }
0135 }
0136 
0137 void DItemsListView::setColumn(ColumnType column, const QString& label, bool enable)
0138 {
0139     setColumnLabel(column, label);
0140     setColumnEnabled(column, enable);
0141 }
0142 
0143 DItemsListViewItem* DItemsListView::findItem(const QUrl& url)
0144 {
0145     QTreeWidgetItemIterator it(this);
0146 
0147     while (*it)
0148     {
0149         DItemsListViewItem* const lvItem = dynamic_cast<DItemsListViewItem*>(*it);
0150 
0151         if (lvItem && (lvItem->url() == url))
0152         {
0153             return lvItem;
0154         }
0155 
0156         ++it;
0157     }
0158 
0159     return nullptr;
0160 }
0161 
0162 QModelIndex DItemsListView::indexFromItem(DItemsListViewItem* item, int column) const
0163 {
0164     return QTreeWidget::indexFromItem(item, column);
0165 }
0166 
0167 void DItemsListView::contextMenuEvent(QContextMenuEvent* e)
0168 {
0169     QTreeWidget::contextMenuEvent(e);
0170 
0171     Q_EMIT signalContextMenuRequested();
0172 }
0173 
0174 void DItemsListView::dragEnterEvent(QDragEnterEvent* e)
0175 {
0176     m_itemDraged = QTreeWidget::currentItem();
0177 
0178     QTreeWidget::dragEnterEvent(e);
0179 
0180     if (e->mimeData()->hasUrls())
0181     {
0182         e->acceptProposedAction();
0183     }
0184 }
0185 
0186 void DItemsListView::dragMoveEvent(QDragMoveEvent* e)
0187 {
0188     QTreeWidget::dragMoveEvent(e);
0189 
0190     if (e->mimeData()->hasUrls())
0191     {
0192         e->acceptProposedAction();
0193     }
0194 }
0195 
0196 void DItemsListView::dropEvent(QDropEvent* e)
0197 {
0198     QTreeWidget::dropEvent(e);
0199     QList<QUrl> list = e->mimeData()->urls();
0200     QList<QUrl> urls;
0201 
0202     Q_FOREACH (const QUrl& url, list)
0203     {
0204         QFileInfo fi(url.toLocalFile());
0205 
0206         if (fi.isFile() && fi.exists())
0207         {
0208             urls.append(url);
0209         }
0210     }
0211 
0212     if (!urls.isEmpty())
0213     {
0214         Q_EMIT signalAddedDropedItems(urls);
0215     }
0216 
0217     scrollToItem(m_itemDraged);
0218 }
0219 
0220 DItemsListIsLessThanHandler DItemsListView::isLessThanHandler() const
0221 {
0222     DItemsList* const p = dynamic_cast<DItemsList*>(parent());
0223 
0224     if (p)
0225     {
0226         return p->isLessThanHandler();
0227     }
0228 
0229     return nullptr;
0230 }
0231 
0232 } // namespace Digikam