File indexing completed on 2025-02-23 04:34:18

0001 /**
0002  * \file playlisteditdialog.cpp
0003  * Edit playlist dialog.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 05 Aug 2018
0008  *
0009  * Copyright (C) 2018-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "playlisteditdialog.h"
0028 #include <QVBoxLayout>
0029 #include <QDialogButtonBox>
0030 #include <QPushButton>
0031 #include <QMessageBox>
0032 #include <QCloseEvent>
0033 #include "contexthelp.h"
0034 #include "playlistmodel.h"
0035 #include "proxyitemselectionmodel.h"
0036 #include "playlistview.h"
0037 
0038 /**
0039  * Constructor.
0040  * @param model playlist model
0041  * @param selModel selection model of associated file proxy model
0042  * @param parent parent widget
0043  */
0044 PlaylistEditDialog::PlaylistEditDialog(PlaylistModel* model,
0045                                        QItemSelectionModel* selModel,
0046                                        QWidget* parent)
0047   : QDialog(parent), m_playlistModel(model)
0048 {
0049   setObjectName(QLatin1String("PlaylistEditDialog"));
0050   setModal(false);
0051   setSizeGripEnabled(true);
0052   setAttribute(Qt::WA_DeleteOnClose);
0053 #ifdef Q_OS_MAC
0054   setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
0055 #endif
0056 
0057   auto vlayout = new QVBoxLayout(this);
0058   QListView* playlist = new PlaylistView;
0059   playlist->setModel(m_playlistModel);
0060   playlist->setSelectionMode(QAbstractItemView::ExtendedSelection);
0061   playlist->setSelectionBehavior(QAbstractItemView::SelectRows);
0062   playlist->setSelectionModel(new ProxyItemSelectionModel(m_playlistModel,
0063                                                           selModel, this));
0064   playlist->setAcceptDrops(true);
0065   playlist->setDragEnabled(true);
0066   playlist->setDragDropMode(QAbstractItemView::DragDrop);
0067   playlist->setDragDropOverwriteMode(false);
0068   playlist->setDefaultDropAction(Qt::MoveAction);
0069   playlist->setDropIndicatorShown(true);
0070   playlist->viewport()->setAcceptDrops(true);
0071 
0072   vlayout->addWidget(playlist);
0073   m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Help |
0074                                      QDialogButtonBox::Save |
0075                                      QDialogButtonBox::Cancel);
0076   connect(m_buttonBox, &QDialogButtonBox::helpRequested, this, &PlaylistEditDialog::showHelp);
0077   connect(m_buttonBox, &QDialogButtonBox::accepted, m_playlistModel, &PlaylistModel::save);
0078   connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0079   vlayout->addWidget(m_buttonBox);
0080 
0081   connect(m_playlistModel, &PlaylistModel::modifiedChanged,
0082           this, &PlaylistEditDialog::setModified);
0083   setModified(false);
0084 }
0085 
0086 /**
0087  * Destructor.
0088  */
0089 PlaylistEditDialog::~PlaylistEditDialog()
0090 {
0091   // Force rereading the file on the next Kid3Application::playlistModel().
0092   m_playlistModel->setPlaylistFile(QString());
0093 }
0094 
0095 /**
0096  * Show help.
0097  */
0098 void PlaylistEditDialog::showHelp()
0099 {
0100   ContextHelp::displayHelp(QLatin1String("edit-playlist"));
0101 }
0102 
0103 void PlaylistEditDialog::setModified(bool modified)
0104 {
0105   setWindowCaption();
0106   m_buttonBox->button(QDialogButtonBox::Save)->setEnabled(modified);
0107 }
0108 
0109 /**
0110  * Set window caption.
0111  */
0112 void PlaylistEditDialog::setWindowCaption()
0113 {
0114   QString title = tr("Playlist");
0115   if (QString fileName = m_playlistModel->playlistFileName();
0116       !fileName.isEmpty()) {
0117     title += QLatin1String(" - ");
0118     title += fileName;
0119     if (m_playlistModel->isModified()) {
0120       title += tr(" [modified]");
0121     }
0122   }
0123   setWindowTitle(title);
0124 }
0125 
0126 void PlaylistEditDialog::closeEvent(QCloseEvent* event)
0127 {
0128   if (m_playlistModel->isModified()) {
0129     int answer = QMessageBox::warning(
0130           this, tr("Warning"),
0131           tr("A playlist has been modified.\n"
0132              "Do you want to save it?"),
0133           QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
0134           QMessageBox::Yes);
0135     if (answer == QMessageBox::Yes) {
0136       m_playlistModel->save();
0137     }
0138     if (answer != QMessageBox::Yes && answer != QMessageBox::No) {
0139       event->ignore();
0140       return;
0141     }
0142   }
0143   QDialog::closeEvent(event);
0144 }