File indexing completed on 2025-01-05 03:57:54

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-08-27
0007  * Description : List-view for the Showfoto folder view.
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfotofolderviewlist.h"
0016 
0017 // Qt includes
0018 
0019 #include <QApplication>
0020 #include <QStyle>
0021 #include <QIcon>
0022 #include <QFileInfo>
0023 #include <QHeaderView>
0024 #include <QDir>
0025 #include <QTimer>
0026 #include <QMenu>
0027 #include <QMimeData>
0028 #include <QScrollBar>
0029 
0030 // KDE includes
0031 
0032 #include <klocalizedstring.h>
0033 
0034 // Local includes
0035 
0036 #include "digikam_debug.h"
0037 #include "digikam_globals.h"
0038 #include "showfotofolderviewbar.h"
0039 #include "showfotofolderviewmodel.h"
0040 #include "showfotofolderviewtooltip.h"
0041 #include "showfotosettings.h"
0042 #include "dfileoperations.h"
0043 
0044 namespace ShowFoto
0045 {
0046 
0047 class Q_DECL_HIDDEN ShowfotoFolderViewList::Private
0048 {
0049 
0050 public:
0051 
0052     explicit Private()
0053       : view        (nullptr),
0054         bar         (nullptr),
0055         toolTipTimer(nullptr),
0056         toolTip     (nullptr)
0057     {
0058     }
0059 
0060     ShowfotoFolderViewSideBar* view;
0061     ShowfotoFolderViewBar*     bar;
0062     QTimer*                    toolTipTimer;
0063     ShowfotoFolderViewToolTip* toolTip;
0064     QModelIndex                toolTipIndex;
0065 };
0066 
0067 ShowfotoFolderViewList::ShowfotoFolderViewList(ShowfotoFolderViewSideBar* const view,
0068                                                ShowfotoFolderViewBar* const bar)
0069     : QTreeView(view),
0070       d        (new Private)
0071 {
0072     d->view   = view;
0073     d->bar    = bar;
0074 
0075     setObjectName(QLatin1String("ShowfotoFolderViewList"));
0076     setRootIsDecorated(false);
0077     setItemsExpandable(false);
0078     setUniformRowHeights(false);
0079     setExpandsOnDoubleClick(false);
0080     setAlternatingRowColors(true);
0081     setIconSize(QSize(32, 32));
0082     setSelectionMode(QAbstractItemView::ExtendedSelection);
0083     setSelectionBehavior(QAbstractItemView::SelectRows);
0084     setAllColumnsShowFocus(true);
0085 
0086     resizeColumnToContents(0);
0087 
0088     setDragEnabled(true);
0089     setDragDropMode(QAbstractItemView::DragOnly);
0090     viewport()->setMouseTracking(true);
0091 
0092     d->toolTip       = new ShowfotoFolderViewToolTip(this);
0093     d->toolTipTimer  = new QTimer(this);
0094 
0095     connect(d->toolTipTimer, SIGNAL(timeout()),
0096             this, SLOT(slotToolTip()));
0097 }
0098 
0099 ShowfotoFolderViewList::~ShowfotoFolderViewList()
0100 {
0101     delete d->toolTip;
0102     delete d;
0103 }
0104 
0105 void ShowfotoFolderViewList::slotOpenInFileManager()
0106 {
0107     QModelIndex index  = currentIndex();
0108     QList<QUrl> urls;
0109 
0110     if (index.isValid())
0111     {
0112         ShowfotoFolderViewModel* const model = dynamic_cast<ShowfotoFolderViewModel*>(this->model());
0113 
0114         if (model)
0115         {
0116             urls << QUrl::fromLocalFile(model->filePath(index));
0117         }
0118     }
0119 
0120     if (urls.empty())
0121     {
0122         urls << QUrl::fromLocalFile(d->view->currentFolder());
0123     }
0124 
0125     DFileOperations::openInFileManager(urls);
0126 }
0127 
0128 void ShowfotoFolderViewList::slotIconSizeChanged(int size)
0129 {
0130     setIconSize(QSize(size, size));
0131 }
0132 
0133 void ShowfotoFolderViewList::contextMenuEvent(QContextMenuEvent* e)
0134 {
0135     QMenu* const ctxmenu = new QMenu(this);
0136     ctxmenu->setTitle(i18nc("@title", "Folder-View"));
0137     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("GoPrevious")));
0138     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("GoNext")));
0139     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("GoHome")));
0140     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("GoUp")));
0141     ctxmenu->addSeparator();
0142     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("ShortView")));
0143     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("DetailledView")));
0144     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("ShowBookmarks")));
0145     ctxmenu->addAction(d->bar->toolBarAction(QLatin1String("MoreSettings")));
0146     ctxmenu->addSeparator();
0147     QMenu* const stackMenu = ctxmenu->addMenu(i18nc("@title", "Stack"));
0148     stackMenu->setIcon(QIcon::fromTheme(QLatin1String("layer-visible-on")));
0149     stackMenu->addAction(d->bar->toolBarAction(QLatin1String("LoadContents")));
0150     stackMenu->addAction(d->bar->toolBarAction(QLatin1String("AppendContents")));
0151     stackMenu->addActions(d->bar->pluginActions());
0152     ctxmenu->addSeparator();
0153 
0154     QAction* const addBookmark  = new QAction(QIcon::fromTheme(QLatin1String("list-add")),
0155                                               i18nc("@action: context menu", "Add Bookmark"), ctxmenu);
0156     ctxmenu->addAction(addBookmark);
0157 
0158     connect(addBookmark, SIGNAL(triggered()),
0159             this, SIGNAL(signalAddBookmark()));
0160 
0161     QAction* const openFileMngr = new QAction(QIcon::fromTheme(QLatin1String("folder-open")),
0162                                               i18nc("@action: context menu", "Open in File Manager"), ctxmenu);
0163     ctxmenu->addAction(openFileMngr);
0164 
0165     connect(openFileMngr, SIGNAL(triggered()),
0166             this, SLOT(slotOpenInFileManager()));
0167 
0168     ctxmenu->exec(e->globalPos());
0169 
0170     delete ctxmenu;
0171 
0172     QTreeView::contextMenuEvent(e);
0173 }
0174 
0175 void ShowfotoFolderViewList::mouseDoubleClickEvent(QMouseEvent* e)
0176 {
0177     d->view->loadContents(currentIndex());
0178 
0179     QTreeView::mouseDoubleClickEvent(e);
0180 }
0181 
0182 void ShowfotoFolderViewList::hideToolTip()
0183 {
0184     d->toolTipIndex = QModelIndex();
0185     d->toolTipTimer->stop();
0186     slotToolTip();
0187 }
0188 
0189 void ShowfotoFolderViewList::slotToolTip()
0190 {
0191     d->toolTip->setIndex(d->toolTipIndex);
0192 }
0193 
0194 bool ShowfotoFolderViewList::acceptToolTip(const QModelIndex& index) const
0195 {
0196     ShowfotoFolderViewModel* const model = dynamic_cast<ShowfotoFolderViewModel*>(this->model());
0197 
0198     if (model)
0199     {
0200         QFileInfo info(model->filePath(index));
0201 
0202         if (info.isFile() && !info.isSymLink() && !info.isDir() && !info.isRoot())
0203         {
0204             return true;
0205         }
0206     }
0207 
0208     return false;
0209 }
0210 
0211 void ShowfotoFolderViewList::mouseMoveEvent(QMouseEvent* e)
0212 {
0213     if (e->buttons() == Qt::NoButton)
0214     {
0215         QModelIndex index = indexAt(e->pos());
0216 
0217         if (ShowfotoSettings::instance()->getShowToolTip())
0218         {
0219             if (!isActiveWindow())
0220             {
0221                 hideToolTip();
0222                 return;
0223             }
0224 
0225             if (index != d->toolTipIndex)
0226             {
0227                 hideToolTip();
0228 
0229                 if (acceptToolTip(index))
0230                 {
0231                     d->toolTipIndex = index;
0232                     d->toolTipTimer->setSingleShot(true);
0233                     d->toolTipTimer->start(500);
0234                 }
0235             }
0236 
0237             if ((index == d->toolTipIndex) && !acceptToolTip(index))
0238             {
0239                 hideToolTip();
0240             }
0241         }
0242 
0243         return;
0244     }
0245 
0246     hideToolTip();
0247     QTreeView::mouseMoveEvent(e);
0248 }
0249 
0250 void ShowfotoFolderViewList::wheelEvent(QWheelEvent* e)
0251 {
0252     hideToolTip();
0253     QTreeView::wheelEvent(e);
0254 }
0255 
0256 void ShowfotoFolderViewList::keyPressEvent(QKeyEvent* e)
0257 {
0258     hideToolTip();
0259     QTreeView::keyPressEvent(e);
0260 }
0261 
0262 void ShowfotoFolderViewList::focusOutEvent(QFocusEvent* e)
0263 {
0264     hideToolTip();
0265     QTreeView::focusOutEvent(e);
0266 }
0267 
0268 void ShowfotoFolderViewList::leaveEvent(QEvent* e)
0269 {
0270     hideToolTip();
0271     QTreeView::leaveEvent(e);
0272 }
0273 
0274 } // namespace ShowFoto
0275 
0276 #include "moc_showfotofolderviewlist.cpp"