1 /**
2 * \file playlisteditdialog.cpp
3 * Edit playlist dialog.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 05 Aug 2018
8 *
9 * Copyright (C) 2018 Urs Fleisch
10 *
11 * This file is part of Kid3.
12 *
13 * Kid3 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Kid3 is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include "playlisteditdialog.h"
28 #include <QVBoxLayout>
29 #include <QDialogButtonBox>
30 #include <QPushButton>
31 #include <QMessageBox>
32 #include <QCloseEvent>
33 #include "contexthelp.h"
34 #include "playlistmodel.h"
35 #include "proxyitemselectionmodel.h"
36 #include "playlistview.h"
37
38 /**
39 * Constructor.
40 * @param model playlist model
41 * @param selModel selection model of associated file proxy model
42 * @param parent parent widget
43 */
PlaylistEditDialog(PlaylistModel * model,QItemSelectionModel * selModel,QWidget * parent)44 PlaylistEditDialog::PlaylistEditDialog(PlaylistModel* model,
45 QItemSelectionModel* selModel,
46 QWidget* parent)
47 : QDialog(parent), m_playlistModel(model)
48 {
49 setObjectName(QLatin1String("PlaylistEditDialog"));
50 setModal(false);
51 setSizeGripEnabled(true);
52 setAttribute(Qt::WA_DeleteOnClose);
53 #ifdef Q_OS_MAC
54 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
55 #endif
56
57 auto vlayout = new QVBoxLayout(this);
58 QListView* playlist = new PlaylistView;
59 playlist->setModel(m_playlistModel);
60 playlist->setSelectionMode(QAbstractItemView::ExtendedSelection);
61 playlist->setSelectionBehavior(QAbstractItemView::SelectRows);
62 playlist->setSelectionModel(new ProxyItemSelectionModel(m_playlistModel,
63 selModel, this));
64 playlist->setAcceptDrops(true);
65 playlist->setDragEnabled(true);
66 playlist->setDragDropMode(QAbstractItemView::DragDrop);
67 playlist->setDragDropOverwriteMode(false);
68 playlist->setDefaultDropAction(Qt::MoveAction);
69 playlist->setDropIndicatorShown(true);
70 playlist->viewport()->setAcceptDrops(true);
71
72 vlayout->addWidget(playlist);
73 m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Help |
74 QDialogButtonBox::Save |
75 QDialogButtonBox::Cancel);
76 connect(m_buttonBox, &QDialogButtonBox::helpRequested, this, &PlaylistEditDialog::showHelp);
77 connect(m_buttonBox, &QDialogButtonBox::accepted, m_playlistModel, &PlaylistModel::save);
78 connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
79 vlayout->addWidget(m_buttonBox);
80
81 connect(m_playlistModel, &PlaylistModel::modifiedChanged,
82 this, &PlaylistEditDialog::setModified);
83 setModified(false);
84 }
85
86 /**
87 * Destructor.
88 */
~PlaylistEditDialog()89 PlaylistEditDialog::~PlaylistEditDialog()
90 {
91 // Force rereading the file on the next Kid3Application::playlistModel().
92 m_playlistModel->setPlaylistFile(QString());
93 }
94
95 /**
96 * Show help.
97 */
showHelp()98 void PlaylistEditDialog::showHelp()
99 {
100 ContextHelp::displayHelp(QLatin1String("edit-playlist"));
101 }
102
setModified(bool modified)103 void PlaylistEditDialog::setModified(bool modified)
104 {
105 setWindowCaption();
106 m_buttonBox->button(QDialogButtonBox::Save)->setEnabled(modified);
107 }
108
109 /**
110 * Set window caption.
111 */
setWindowCaption()112 void PlaylistEditDialog::setWindowCaption()
113 {
114 QString title = tr("Playlist");
115 QString fileName = m_playlistModel->playlistFileName();
116 if (!fileName.isEmpty()) {
117 title += QLatin1String(" - ");
118 title += fileName;
119 if (m_playlistModel->isModified()) {
120 title += tr(" [modified]");
121 }
122 }
123 setWindowTitle(title);
124 }
125
closeEvent(QCloseEvent * event)126 void PlaylistEditDialog::closeEvent(QCloseEvent* event)
127 {
128 if (m_playlistModel->isModified()) {
129 int answer = QMessageBox::warning(
130 this, tr("Warning"),
131 tr("A playlist has been modified.\n"
132 "Do you want to save it?"),
133 QMessageBox::Yes | QMessageBox::Default,
134 QMessageBox::No,
135 QMessageBox::Cancel | QMessageBox::Escape);
136 if (answer == QMessageBox::Yes) {
137 m_playlistModel->save();
138 }
139 if (answer != QMessageBox::Yes && answer != QMessageBox::No) {
140 event->ignore();
141 return;
142 }
143 }
144 QDialog::closeEvent(event);
145 }
146