File indexing completed on 2022-10-04 17:30:21

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "previewlist.h"
0005 
0006 #include "customdialog.h"
0007 #include "krenamemodel.h"
0008 
0009 #include <QContextMenuEvent>
0010 #include <QMenu>
0011 #include <QPointer>
0012 
0013 PreviewList::PreviewList(QWidget *parent)
0014     : QTreeView(parent), m_model(nullptr)
0015 {
0016     m_menu = new QMenu("KRename", this);   // we need any text here so that we have a title
0017     m_menu->addAction(i18n("&Change filename manually..."), this, &PreviewList::slotManually, QKeySequence("F2"));
0018     m_menu->addSeparator();
0019     m_menu->addAction(i18n("&Open"), this, &PreviewList::slotOpen);
0020     m_menu->addSeparator();
0021     m_menu->addAction(i18n("&Add..."), this, &PreviewList::addFiles);
0022     m_menu->addAction(i18n("&Remove"), this, &PreviewList::slotRemove);
0023 
0024     connect(this, &PreviewList::activated, this, &PreviewList::slotManually);
0025 }
0026 
0027 PreviewList::~PreviewList()
0028 {
0029 
0030 }
0031 
0032 void PreviewList::contextMenuEvent(QContextMenuEvent *e)
0033 {
0034     // only show a context menu if we have model and contents
0035     if (m_model && m_model->rowCount() && currentIndex().isValid()) {
0036         const KRenameFile &file  = m_model->file(this->currentIndex().row());
0037 
0038         m_menu->setTitle(file.srcUrl().toDisplayString(QUrl::PreferLocalFile));
0039         m_menu->popup(e->globalPos());
0040     }
0041 }
0042 
0043 void PreviewList::slotOpen()
0044 {
0045     m_model->run(this->currentIndex(), this);
0046 }
0047 
0048 void PreviewList::slotRemove()
0049 {
0050     QList<int> list;
0051 
0052     list.append(this->currentIndex().row());
0053 
0054     m_model->removeFiles(list);
0055 
0056     Q_EMIT updateCount();
0057 }
0058 
0059 void PreviewList::slotManually()
0060 {
0061     QPointer<CustomDialog> dialog = new CustomDialog(m_model->file(this->currentIndex().row()), this);
0062     if (dialog->exec() == QDialog::Accepted) {
0063         QString manual;
0064         EManualChangeMode mode = eManualChangeMode_None;
0065         if (dialog->hasManualChanges()) {
0066             manual = dialog->manualChanges();
0067             mode = dialog->manualChangeMode();
0068         }
0069 
0070         m_model->file(this->currentIndex().row()).setManualChanges(manual, mode);
0071     }
0072     delete dialog;
0073 }