playlistbox.cpp 10.3 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          playlistbox.cpp  -  description
                             -------------------
    begin                : Thu Sep 12 2002
    copyright            : (C) 2002 by Scott Wheeler, 
6
    email                : wheeler@kde.org
7 8 9 10 11 12 13 14 15 16 17 18
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kiconloader.h>
19
#include <kurldrag.h>
20
#include <kmessagebox.h>
21 22
#include <klocale.h>
#include <kdebug.h>
23

24
#include <qfile.h>
25
#include <qdrawutil.h>
26
#include <qinputdialog.h>
27

28
#include "playlist.h"
29
#include "playlistbox.h"
30
#include "collectionlist.h"
31
#include "playlistsplitter.h"
32 33 34 35 36

////////////////////////////////////////////////////////////////////////////////
// PlaylistBox public methods
////////////////////////////////////////////////////////////////////////////////

37
PlaylistBox::PlaylistBox(PlaylistSplitter *parent, const char *name) : KListBox(parent, name)
38
{
39
    splitter = parent;
40
    updatePlaylistStack = true;
41 42

    collectionContextMenu = new KPopupMenu();
43
    collectionContextMenu->insertItem(SmallIcon("editcopy"), i18n("Duplicate..."), this, SLOT(contextDuplicate()));
44 45 46 47 48

    playlistContextMenu = new KPopupMenu();
    playlistContextMenu->insertItem(SmallIcon("filesave"), i18n("Save"), this, SLOT(contextSave()));
    playlistContextMenu->insertItem(SmallIcon("filesaveas"), i18n("Save As..."), this, SLOT(contextSaveAs()));
    playlistContextMenu->insertItem(i18n("Rename..."), this, SLOT(contextRename()));
49
    playlistContextMenu->insertItem(SmallIcon("editcopy"), i18n("Duplicate..."), this, SLOT(contextDuplicate()));
50 51
    playlistContextMenu->insertItem(SmallIcon("editdelete"), i18n("Delete"), this, SLOT(contextDeleteItem()));

52
    setAcceptDrops(true);
53 54

    connect(this, SIGNAL(currentChanged(QListBoxItem *)), 
55 56 57 58
	    this, SLOT(playlistChanged(QListBoxItem *)));

    connect(this, SIGNAL(doubleClicked(QListBoxItem *)), 
	    this, SLOT(playlistDoubleClicked(QListBoxItem *)));
59 60 61 62
}

PlaylistBox::~PlaylistBox()
{
63
    delete(playlistContextMenu);
64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
void PlaylistBox::sort()
{
    QListBoxItem *collectionItem = firstItem();

    bool collectionSelected = isSelected(collectionItem);

    updatePlaylistStack = false;

    if(collectionSelected)
	setSelected(collectionItem, false);

    takeItem(collectionItem);
    KListBox::sort();
    insertItem(collectionItem, 0);

    if(collectionSelected)
	setSelected(collectionItem, true);

    updatePlaylistStack = true;
}

87 88 89 90 91
QStringList PlaylistBox::names() const
{
    return(nameList);
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
////////////////////////////////////////////////////////////////////////////////
// PlaylistBox public slots
////////////////////////////////////////////////////////////////////////////////

void PlaylistBox::save()
{
    save(static_cast<PlaylistBoxItem *>(selectedItem()));
}

void PlaylistBox::save(PlaylistBoxItem *item)
{
    if(item)
	item->playlist()->save();    
}

void PlaylistBox::saveAs()
{
    saveAs(static_cast<PlaylistBoxItem *>(selectedItem()));
}

void PlaylistBox::saveAs(PlaylistBoxItem *item)
{
    if(item)
        item->playlist()->saveAs();
}

void PlaylistBox::rename()
{
    rename(static_cast<PlaylistBoxItem *>(selectedItem()));
}

void PlaylistBox::rename(PlaylistBoxItem *item)
{
    if(item) {
	bool ok;

	QString name = QInputDialog::getText(i18n("Rename..."), i18n("Please enter a name for this playlist:"),
					     QLineEdit::Normal, item->text(), &ok);
	if(ok) {
	    item->setText(name);
132 133 134 135 136 137
	   
	    // Telling the playlist to change it's name will emit a signal that
	    // is connected to PlaylistItem::setName().

	    if(item->playlist())
		item->playlist()->setName(name);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	}
    }
}

void PlaylistBox::duplicate()
{
    duplicate(static_cast<PlaylistBoxItem *>(selectedItem()));
}

void PlaylistBox::duplicate(PlaylistBoxItem *item)
{
    if(item) {
	bool ok;

	// If this text is changed, please also change it in PlaylistSplitter::createPlaylist().

	QString name = QInputDialog::getText(i18n("New Playlist..."), i18n("Please enter a name for the new playlist:"),
					     QLineEdit::Normal, splitter->uniquePlaylistName(item->text(), true), &ok);
	if(ok) {
	    Playlist *p = splitter->createPlaylist(name);
	    splitter->add(item->playlist()->files(), p);
	}
    }
}

void PlaylistBox::deleteItem()
{
    deleteItem(static_cast<PlaylistBoxItem *>(selectedItem()));
}

void PlaylistBox::deleteItem(PlaylistBoxItem *item)
{
170 171 172 173 174 175 176
    if(item && item->playlist()) {

	// If the file is "internal" (not loaded from a file and not yet saved),
	// or the file name is null, or the user specifically chooses to delete 
	// the file then delete it.  Otherwise, just remove the file from the
	// PlaylistBox.

177
	if(item->playlist()->fileName() != QString::null) {
178
	    if(item->playlist()->isInternalFile())
179
		QFile::remove(item->playlist()->fileName());
180 181 182 183
	    else {
		int remove = KMessageBox::warningYesNoCancel(this, i18n("Do you want to delete this file from the disk as well?"));
		
		if(remove == KMessageBox::Yes) {
184
		    if(!QFile::remove(item->playlist()->fileName()))
185 186 187 188 189 190 191
			KMessageBox::sorry(this, i18n("Could not delete the specified file."));
		}
		else if(remove == KMessageBox::Cancel)
		    return;
	    }
	}

192 193 194 195 196 197
	nameList.remove(item->text());
	delete(item->playlist());
	delete(item);
    }
}

198
////////////////////////////////////////////////////////////////////////////////
199
// PlaylistBox private methods
200 201
////////////////////////////////////////////////////////////////////////////////

202 203 204 205
void PlaylistBox::resizeEvent(QResizeEvent *e)
{
    // hack-ish, but functional 

206 207 208
//    for(int i = 0; i <= count(); i++)
//	updateItem(i);
    triggerUpdate(true);
209 210 211 212

    KListBox::resizeEvent(e);
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226
void PlaylistBox::dropEvent(QDropEvent *e)
{
    KURL::List urls;
    
    if(KURLDrag::decode(e, urls) && !urls.isEmpty()) {

	QStringList files;

	for(KURL::List::Iterator it = urls.begin(); it != urls.end(); it++)
	    files.append((*it).path());

	PlaylistBoxItem *i = static_cast<PlaylistBoxItem *>(itemAt(e->pos()));

	if(i && i->playlist())
227
	    splitter->add(files, i->playlist());
228 229 230 231 232 233 234 235 236 237 238 239 240
    }
}

void PlaylistBox::dragMoveEvent(QDragMoveEvent *e)
{
    // If we can decode the input source, there is a non-null item at the "move"
    // position, the playlist for that PlaylistBoxItem is non-null, is not the 
    // selected playlist and is not the CollectionList, then accept the event.
    //
    // Otherwise, do not accept the event.
    
    if(KURLDrag::canDecode(e) && itemAt(e->pos())) {
	PlaylistBoxItem *i = static_cast<PlaylistBoxItem *>(itemAt(e->pos()));
241 242 243 244 245 246 247 248 249 250 251 252

	// This is a semi-dirty hack to check if the items are coming from within
	// JuK.  If they are not coming from a Playlist (or subclass) then the
	// dynamic_cast will fail and we can safely assume that the item is 
	// coming from outside of JuK.

	if(dynamic_cast<Playlist *>(e->source())) {
	    if(i->playlist() && i->playlist() != CollectionList::instance())
		if(selectedItem() && i != selectedItem())
		    e->accept(true);
		else
		    e->accept(false);
253 254
	    else
		e->accept(false);
255 256 257
	}
	else // the dropped items are coming from outside of JuK
	    e->accept(true);
258 259 260
    }
    else
	e->accept(false);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

}

void PlaylistBox::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::RightButton) {
	QListBoxItem *i = itemAt(e->pos());
	if(i)
	    drawContextMenu(i, e->globalPos());
	e->accept();
    }
    else {
	e->ignore();
	QListBox::mousePressEvent(e);
    }
276 277 278 279 280 281 282
}

void PlaylistBox::addName(const QString &name)
{
    nameList.append(name);
}

283 284 285 286
////////////////////////////////////////////////////////////////////////////////
// PlaylistBox private slots
////////////////////////////////////////////////////////////////////////////////

287
void PlaylistBox::playlistChanged(QListBoxItem *item)
288 289
{
    PlaylistBoxItem *i = dynamic_cast<PlaylistBoxItem *>(item);
290
    if(updatePlaylistStack && i)
291 292 293
	emit(currentChanged(i));
}

294 295 296 297 298 299 300
void PlaylistBox::playlistDoubleClicked(QListBoxItem *item)
{
    PlaylistBoxItem *i = dynamic_cast<PlaylistBoxItem *>(item);
    if(i)
	emit(doubleClicked(i));
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
void PlaylistBox::drawContextMenu(QListBoxItem *item, const QPoint &point)
{
    PlaylistBoxItem *i = static_cast<PlaylistBoxItem *>(item);

    contextMenuOn = i;

    if(i)
	if(i->playlist() == CollectionList::instance())
	    collectionContextMenu->popup(point);
	else
	    playlistContextMenu->popup(point);
}

void PlaylistBox::contextSave()
{
316
    save(contextMenuOn);
317 318 319 320
}

void PlaylistBox::contextSaveAs()
{
321
    saveAs(contextMenuOn);
322 323 324 325
}

void PlaylistBox::contextRename()
{
326
    rename(contextMenuOn);
327 328
}

329
void PlaylistBox::contextDuplicate()
330
{
331
    duplicate(contextMenuOn);
332 333 334 335
}

void PlaylistBox::contextDeleteItem()
{
336 337
    deleteItem(contextMenuOn);
    contextMenuOn = 0;
338 339
}

340 341 342 343
////////////////////////////////////////////////////////////////////////////////
// PlaylistBoxItem public methods
////////////////////////////////////////////////////////////////////////////////

344
PlaylistBoxItem::PlaylistBoxItem(PlaylistBox *listbox, const QPixmap &pix, const QString &text, Playlist *l) 
345
    : QObject(listbox), ListBoxPixmap(listbox, pix, text)
346 347 348
{
    list = l;
    setOrientation(Qt::Vertical);
349
    listbox->addName(text);
350

351
    connect(l, SIGNAL(nameChanged(const QString &)), this, SLOT(setName(const QString &)));
352 353
}

354 355
PlaylistBoxItem::PlaylistBoxItem(PlaylistBox *listbox, const QString &text, Playlist *l) 
    : ListBoxPixmap(listbox, SmallIcon("midi", 32), text)
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
    list = l;
    setOrientation(Qt::Vertical);
}

PlaylistBoxItem::~PlaylistBoxItem()
{

}

Playlist *PlaylistBoxItem::playlist() const
{
    return(list);
}

371
PlaylistBox *PlaylistBoxItem::listBox() const
372
{
373 374
    return(dynamic_cast<PlaylistBox *>(ListBoxPixmap::listBox()));
}
375

376 377 378 379 380
void PlaylistBoxItem::setName(const QString &name)
{
    if(listBox()) {
	listBox()->nameList.remove(text());
	listBox()->nameList.append(name);
381

382 383
	setText(name);
	listBox()->updateItem(this);
384 385 386
    }
}

387
#include "playlistbox.moc"