Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

playlist.h 23.2 KB
Newer Older
1 2
/***************************************************************************
    begin                : Sat Feb 16 2002
3
    copyright            : (C) 2002 - 2004 by Scott Wheeler
4
    email                : wheeler@kde.org
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include <klistview.h>
Laurent Montel's avatar
Laurent Montel committed
20
#include <k3urldrag.h>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
21
#include <kdebug.h>
22
#include <kglobalsettings.h>
23

Laurent Montel's avatar
Laurent Montel committed
24
#include <q3valuevector.h>
Dirk Mueller's avatar
compile  
Dirk Mueller committed
25
#include <qfileinfo.h>
Laurent Montel's avatar
Laurent Montel committed
26 27 28 29 30 31 32 33 34 35 36
//Added by qt3to4:
#include <QPaintEvent>
#include <QKeyEvent>
#include <QEvent>
#include <QDropEvent>
#include <Q3ValueList>
#include <QShowEvent>
#include <QResizeEvent>
#include <QDragEnterEvent>
#include <QMouseEvent>
#include <QCustomEvent>
37

38
#include "stringhash.h"
Scott Wheeler's avatar
Scott Wheeler committed
39
#include "playlistsearch.h"
40
#include "tagguesser.h"
41
#include "playlistinterface.h"
42
#include "playlistitem.h"
43

Laurent Montel's avatar
Laurent Montel committed
44
class KMenu;
45
class KActionMenu;
46 47

class QEvent;
Scott Wheeler's avatar
Scott Wheeler committed
48

49
class PlaylistCollection;
Scott Wheeler's avatar
Scott Wheeler committed
50

51
class PlaylistToolTip;
52 53
class UpcomingPlaylist;

Laurent Montel's avatar
Laurent Montel committed
54
typedef Q3ValueList<PlaylistItem *> PlaylistItemList;
55

56
class Playlist : public KListView, public PlaylistInterface
57 58
{
    Q_OBJECT
59

60
public:
61

62 63 64 65 66 67
    Playlist(PlaylistCollection *collection, const QString &name = QString::null,
	     const QString &iconName = "midi");
    Playlist(PlaylistCollection *collection, const PlaylistItemList &items,
	     const QString &name = QString::null, const QString &iconName = "midi");
    Playlist(PlaylistCollection *collection, const QFileInfo &playlistFile,
	     const QString &iconName = "midi");
68

69
    /**
70 71 72
     * This constructor should generally only be used either by the cache
     * restoration methods or by subclasses that want to handle calls to
     * PlaylistCollection::setupPlaylist() differently.
73
     */
74
    Playlist(PlaylistCollection *collection, bool delaySetup);
75

76 77
    virtual ~Playlist();

78 79 80 81 82 83 84 85 86 87 88

    // The following group of functions implement the PlaylistInterface API.

    virtual QString name() const;
    virtual FileHandle currentFile() const;
    virtual int count() const { return childCount(); }
    virtual int time() const;
    virtual void playNext();
    virtual void playPrevious();
    virtual void stop();

89 90 91
    /**
     * Plays the top item of the playlist.
     */
92 93
    void playFirst();

94 95 96 97 98 99
    /**
     * Plays the next album in the playlist.  Only useful when in album random
     * play mode.
     */
    void playNextAlbum();

100 101 102
    /**
     * Saves the file to the currently set file name.  If there is no filename
     * currently set, the default behavior is to prompt the user for a file
103
     * name.
104
     */
105
    virtual void save();
Scott Wheeler's avatar
Scott Wheeler committed
106 107 108 109 110

    /**
     * Standard "save as".  Prompts the user for a location where to save the
     * playlist to.
     */
111
    virtual void saveAs();
Scott Wheeler's avatar
Scott Wheeler committed
112 113 114 115 116 117 118 119 120 121 122

    /**
     * Removes \a item from the Playlist, but not from the disk.  If
     * \a emitChanged is true this will also notify relevant classes
     * that the content of the list has changed.
     *
     * In some situations, for instance when removing items in a loop, it is
     * preferable to delay this notification until after other operations have
     * completed.  In those cases set \a emitChanged to false and call the
     * signal directly.
     */
123
    virtual void clearItem(PlaylistItem *item, bool emitChanged = true);
Scott Wheeler's avatar
Scott Wheeler committed
124 125 126 127 128

    /**
     * Remove \a items from the playlist and emit a signal indicating
     * that the number of items in the list has changed.
     */
129
    virtual void clearItems(const PlaylistItemList &items);
130

131 132 133 134 135 136
    /**
     * Accessor function to return a pointer to the currently playing file.
     *
     * @return 0 if no file is playing, otherwise a pointer to the PlaylistItem
     *     of the track that is currently playing.
     */
137
    static PlaylistItem *playingItem();
138

139 140
    /**
     * All of the (media) files in the list.
141
     */
142
    QStringList files() const;
143

144
    /**
Scott Wheeler's avatar
Scott Wheeler committed
145
     * Returns a list of all of the items in the playlist.
146
     */
147
    virtual PlaylistItemList items();
148

149
    /**
Scott Wheeler's avatar
Scott Wheeler committed
150
     * Returns a list of all of the \e visible items in the playlist.
151
     */
152
    PlaylistItemList visibleItems();
153

154 155 156
    /**
     * Returns a list of the currently selected items.
     */
157
    PlaylistItemList selectedItems();
158

159 160 161 162 163
    /**
     * Returns properly casted first child item in list.
     */
    PlaylistItem *firstChild() const;

164 165
    /**
     * Allow duplicate files in the playlist.
166
     */
167
    void setAllowDuplicates(bool allow) { m_allowDuplicates = allow; }
168

169
    /**
170
     * This is being used as a mini-factory of sorts to make the construction
171 172
     * of PlaylistItems virtual.  In this case it allows for the creation of
     * both PlaylistItems and CollectionListItems.
173
     */
174
    virtual PlaylistItem *createItem(const FileHandle &file,
Laurent Montel's avatar
Laurent Montel committed
175
				     Q3ListViewItem *after = 0,
176
				     bool emitChanged = true);
177

Scott Wheeler's avatar
Scott Wheeler committed
178 179 180 181 182 183 184
    /**
     * This is implemented as a template method to allow subclasses to
     * instantiate their PlaylistItem subclasses using the same method.  Some
     * of the types here are artificially templatized (i.e. CollectionListType and
     * CollectionItemType) to avoid recursive includes, but in fact will always
     * be the same.     
     */
185
    template <class ItemType, class CollectionItemType, class CollectionListType>
186
    ItemType *createItem(const FileHandle &file,
Laurent Montel's avatar
Laurent Montel committed
187
			 Q3ListViewItem *after = 0,
188 189
			 bool emitChanged = true);

190
    virtual void createItems(const PlaylistItemList &siblings, PlaylistItem *after = 0);
191

192 193 194 195
    /**
     * This handles adding files of various types -- music, playlist or directory
     * files.  Music files that are found will be added to this playlist.  New
     * playlist files that are found will result in new playlists being created.
196 197 198 199
     *
     * Note that this should not be used in the case of adding *only* playlist
     * items since it has the overhead of checking to see if the file is a playlist
     * or directory first.
200
     */
201
    virtual void addFiles(const QStringList &files, PlaylistItem *after = 0);
202

203 204 205 206
    /**
     * Returns the file name associated with this playlist (an m3u file) or
     * QString::null if no such file exists.
     */
207
    QString fileName() const { return m_fileName; }
208 209 210 211 212

    /**
     * Sets the file name to be associated with this playlist; this file should
     * have the "m3u" extension.
     */
213
    void setFileName(const QString &n) { m_fileName = n; }
214

215
    /**
216
     * Hides column \a c.  If \a updateSearch is true then a signal that the
217 218 219
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
220
    void hideColumn(int c, bool updateSearch = true);
221 222

    /**
223
     * Shows column \a c.  If \a updateSearch is true then a signal that the
224 225 226
     * visible columns have changed will be emitted and things like the search
     * will be udated.
     */
227
    void showColumn(int c, bool updateSearch = true);
228 229
    bool isColumnVisible(int c) const;

230 231 232 233
    /**
     * This sets a name for the playlist that is \e different from the file name.
     */
    void setName(const QString &n);
234

235 236 237 238
    /**
     * Returns the KActionMenu that allows this to be embedded in menus outside
     * of the playlist.
     */
239
    KActionMenu *columnVisibleAction() const { return m_columnVisibleAction; }
240

241
    /**
242 243
     * Set item to be the playing item.  If \a item is null then this will clear
     * the playing indicator.
244
     */
245
    static void setPlaying(PlaylistItem *item, bool addToHistory = true);
246 247 248 249

    /**
     * Returns true if this playlist is currently playing.
     */
Scott Wheeler's avatar
Scott Wheeler committed
250
    bool playing() const;
251

Scott Wheeler's avatar
Scott Wheeler committed
252 253 254
    /**
     * This forces an update of the left most visible column, but does not save
     * the settings for this.
255
     */
Scott Wheeler's avatar
Scott Wheeler committed
256
    void updateLeftColumn();
257

258 259 260 261 262
    /**
     * Returns the leftmost visible column of the listview.
     */
    int leftColumn() const { return m_leftColumn; }

263 264 265 266
    /**
     * Sets the items in the list to be either visible based on the value of
     * visible.  This is useful for search operations and such.
     */
267 268
    static void setItemsVisible(const PlaylistItemList &items, bool visible = true);

269 270 271 272
    /**
     * Returns the search associated with this list, or an empty search if one
     * has not yet been set.
     */
Scott Wheeler's avatar
Scott Wheeler committed
273
    PlaylistSearch search() const { return m_search; }
274 275 276 277

    /**
     * Set the search associtated with this playlist.
     */
278 279 280 281 282 283 284
    void setSearch(const PlaylistSearch &s);

    /**
     * If the search is disabled then all items will be shown, not just those that
     * match the current search.
     */
    void setSearchEnabled(bool searchEnabled);
Scott Wheeler's avatar
Scott Wheeler committed
285

Scott Wheeler's avatar
Scott Wheeler committed
286 287 288
    /**
     * Marks \a item as either selected or deselected based.
     */
289 290
    void markItemSelected(PlaylistItem *item, bool selected);

Scott Wheeler's avatar
Scott Wheeler committed
291 292 293 294 295 296
    /**
     * Subclasses of Playlist which add new columns will set this value to
     * specify how many of those colums exist.  This allows the Playlist
     * class to do some internal calculations on the number and positions
     * of columns.
     */
297
    virtual int columnOffset() const { return 0; }
Scott Wheeler's avatar
Scott Wheeler committed
298 299 300 301 302 303

    /**
     * Some subclasses of Playlist will be "read only" lists (i.e. the history
     * playlist).  This is a way for those subclasses to indicate that to the
     * Playlist internals.
     */
304
    virtual bool readOnly() const { return false; }
305

306 307 308 309 310
    /**
     * Returns true if it's possible to reload this playlist.
     */
    virtual bool canReload() const { return !m_fileName.isNull(); }

311 312 313 314 315 316
    /**
     * Returns true if the playlist is a search playlist and the search should be
     * editable.
     */
    virtual bool searchIsEditable() const { return false; }

317 318 319 320 321 322 323
    /**
     * Synchronizes the the playing item in this playlist with the playing item
     * in \a sources.  If \a setMaster is true, this list will become the source
     * for determining the next item.
     */
    void synchronizePlayingItems(const PlaylistList &sources, bool setMaster);

324 325 326 327 328 329 330
    /**
     * Playlists have a common set of shared settings such as visible columns
     * that should be applied just before the playlist is shown.  Calling this
     * method applies those.
     */
    void applySharedSettings();

331 332
    void read(QDataStream &s);

333
public slots:
334
    /**
335
     * Remove the currently selected items from the playlist and disk.
336
     */
337
    void slotRemoveSelectedItems() { removeFromDisk(selectedItems()); };
Scott Wheeler's avatar
Scott Wheeler committed
338

339 340 341 342
    /*
     * The edit slots are required to use the canonical names so that they are
     * detected by the application wide framework.
     */
343
    virtual void cut() { copy(); clear(); }
Scott Wheeler's avatar
Scott Wheeler committed
344 345 346 347 348

    /**
     * Puts a list of URLs pointing to the files in the current selection on the
     * clipboard.
     */
349
    virtual void copy();
Scott Wheeler's avatar
Scott Wheeler committed
350 351 352 353

    /**
     * Checks the clipboard for local URLs to be inserted into this playlist.
     */
354
    virtual void paste();
Scott Wheeler's avatar
Scott Wheeler committed
355 356 357 358 359 360 361

    /**
     * Removes the selected items from the list, but not the disk.
     *
     * @see clearItem()
     * @see clearItems()
     */
362
    virtual void clear();
363
    virtual void selectAll() { KListView::selectAll(true); }
364

365 366 367 368 369 370
    /**
     * Refreshes the tags of the selection from disk, or all of the files in the
     * list if there is no selection.
     */
    virtual void slotRefresh();

371
    void slotGuessTagInfo(TagGuesser::Type type);
Scott Wheeler's avatar
Scott Wheeler committed
372 373 374 375 376 377

    /**
     * Renames the selected items' files based on their tags contents.
     *
     * @see PlaylistItem::renameFile()
     */
378 379
    void slotRenameFile();

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    /**
     * Sets the cover of the selected items, pass in true if you want to load from the local system,
     * false if you want to load from the internet.
     */
    void slotAddCover(bool fromLocal);

    /**
     * Shows a large image of the cover
     */
    void slotViewCover();

    /**
     * Removes covers from the selected items
     */
    void slotRemoveCover();

396 397 398 399 400
    /**
     * Shows the cover manager GUI dialog
     */
    void slotShowCoverManager();

401 402 403
    /**
     * Reload the playlist contents from the m3u file.
     */
404
    virtual void slotReload();
405

406 407 408 409 410
    /**
     * Tells the listview that the next time that it paints that the weighted
     * column widths must be recalculated.  If this is called without a column
     * all visible columns are marked as dirty.
     */
411
    void slotWeightDirty(int column = -1);
412

413 414
    void slotShowPlaying();

415 416
    void slotColumnResizeModeChanged();

417 418
    virtual void dataChanged();

419
protected:
420 421 422 423
    /**
     * Remove \a items from the playlist and disk.  This will ignore items that
     * are not actually in the list.
     */
424
    void removeFromDisk(const PlaylistItemList &items);
425

426 427 428
    // the following are all reimplemented from base classes

    virtual bool eventFilter(QObject *watched, QEvent *e);
429
    virtual void keyPressEvent(QKeyEvent *e);
Laurent Montel's avatar
Laurent Montel committed
430 431
    virtual Q3DragObject *dragObject(QWidget *parent);
    virtual Q3DragObject *dragObject() { return dragObject(this); }
432
    virtual bool canDecode(QMimeSource *s);
433
    virtual void decode(QMimeSource *s, PlaylistItem *item = 0);
434
    virtual void contentsDropEvent(QDropEvent *e);
435
    virtual void contentsMouseDoubleClickEvent(QMouseEvent *e);
Michael Pyne's avatar
Michael Pyne committed
436
    virtual void contentsDragEnterEvent(QDragEnterEvent *e);
Scott Wheeler's avatar
Scott Wheeler committed
437
    virtual void showEvent(QShowEvent *e);
Michael Pyne's avatar
Michael Pyne committed
438
    virtual bool acceptDrag(QDropEvent *e) const;
439 440 441
    virtual void viewportPaintEvent(QPaintEvent *pe);
    virtual void viewportResizeEvent(QResizeEvent *re);

Laurent Montel's avatar
Laurent Montel committed
442 443
    virtual void insertItem(Q3ListViewItem *item);
    virtual void takeItem(Q3ListViewItem *item);
444

445 446
    virtual bool hasItem(const QString &file) const { return m_members.contains(file); }

447
    void addColumn(const QString &label);
Scott Wheeler's avatar
Scott Wheeler committed
448 449 450 451 452 453 454 455

    /**
     * Here I'm using delayed setup of some things that aren't quite intuitive.
     * Creating columns and setting up connections are both time consuming if
     * there are a lot of playlists to initialize.  This moves that cost from the
     * startup time to the time when the widget is "polished" -- i.e. just before
     * it's painted the first time.
     */
456
    virtual void polish();
457

458 459 460 461 462 463 464 465
    /**
     * Do some finial initialization of created items.  Notably ensure that they
     * are shown or hidden based on the contents of the current PlaylistSearch.
     *
     * This is called by the PlaylistItem constructor.
     */
    void setupItem(PlaylistItem *item);

466 467 468 469 470 471
    /**
     * Forwards the call to the parent to enable or disable automatic deletion
     * of tree view playlists.  Used by CollectionListItem.
     */
    void setCanDeletePlaylist(bool canDelete);

472 473 474
    template <class ItemType, class SiblingType>
    ItemType *createItem(SiblingType *sibling, ItemType *after = 0);

475 476 477 478 479 480
    /**
     * As a template this allows us to use the same code to initialize the items
     * in subclasses.  CollectionItemType should always be CollectionListItem and
     * ItemType should be a PlaylistItem subclass.
     */
    template <class CollectionItemType, class ItemType, class SiblingType>
Laurent Montel's avatar
Laurent Montel committed
481
    void createItems(const Q3ValueList<SiblingType *> &siblings, ItemType *after = 0);
482

483 484 485 486
protected slots:
    void slotPopulateBackMenu() const;
    void slotPlayFromBackMenu(int number) const;

487
signals:
488

489
    /**
490
     * This is connected to the PlaylistBox::Item to let it know when the
491 492
     * playlist's name has changed.
     */
493
    void signalNameChanged(const QString &name);
494

495
    /**
496
     * This signal is emitted just before a playlist item is removed from the
497 498 499
     * list allowing for any cleanup that needs to happen.  Typically this
     * is used to remove the item from the history and safeguard against
     * dangling pointers.
500
     */
501
    void signalAboutToRemove(PlaylistItem *item);
502

503 504
    void signalEnableDirWatch(bool enable);

505 506
    void coverChanged();

507 508
    void signalPlaylistItemsDropped(Playlist *p);

509 510
private:
    void setup();
511 512 513 514 515 516 517

    /**
     * Load the playlist from a file.  \a fileName should be the absolute path.
     * \a fileInfo should point to the same file as \a fileName.  This is a
     * little awkward API-wise, but keeps us from throwing away useful
     * information.
     */
518
    void loadFile(const QString &fileName, const QFileInfo &fileInfo);
519

520
    /**
Scott Wheeler's avatar
Scott Wheeler committed
521
     * Writes \a text to \a item in \a column.  This is used by the inline tag
522
     * editor.  Returns false if the tag update failed.
523
     */
524
    bool editTag(PlaylistItem *item, const QString &text, int column);
525 526 527 528 529 530

    /**
     * Returns the index of the left most visible column in the playlist.
     *
     * \see isColumnVisible()
     */
531
    int leftMostVisibleColumn() const;
532

533 534 535 536 537 538 539 540
    /**
     * This method is used internally to provide the backend to the other item
     * lists.
     *
     * \see items()
     * \see visibleItems()
     * \see selectedItems()
     */
Laurent Montel's avatar
Laurent Montel committed
541
    PlaylistItemList items(Q3ListViewItemIterator::IteratorFlag flags);
542

543 544 545 546 547
    /**
     * Build the column "weights" for the weighted width mode.
     */
    void calculateColumnWeights();

548 549
    void addFile(const QString &file, FileHandleList &files, bool importPlaylists,
		 PlaylistItem **after);
550 551
    void addFileHelper(FileHandleList &files, PlaylistItem **after,
		       bool ignoreTimer = false);
552 553 554

    void redisplaySearch() { setSearch(m_search); }

555
    void refreshAlbums(const PlaylistItemList &items, const QImage &image = QImage());
556 557
    void refreshAlbum(const QString &artist, const QString &album);

558 559 560 561 562 563 564
    /**
     * Returns the number of PlaylistItems in @p items that can be assigned a
     * cover.  Used to avoid wasting the users' time setting the cover for 20
     * items when none are eligible.
     */
    int eligibleCoverItems(const PlaylistItemList &items);

565 566
    void updatePlaying() const;

567 568 569 570 571 572
    /**
     * This class is used internally to store settings that are shared by all
     * of the playlists, such as column order.  It is implemented as a singleton.
     */
    class SharedSettings;

573
private slots:
574

575 576
    void slotUpdateColumnWidths();

577 578
    void slotAddToUpcoming();

579 580 581 582
    /**
     * Show the RMB menu.  Matches the signature for the signal 
     * QListView::contextMenuRequested().
     */
Laurent Montel's avatar
Laurent Montel committed
583
    void slotShowRMBMenu(Q3ListViewItem *item, const QPoint &point, int column);
584 585

    /**
Scott Wheeler's avatar
Scott Wheeler committed
586 587
     * This slot is called when the inline tag editor has completed its editing
     * and starts the process of renaming the values.
588
     *
Scott Wheeler's avatar
Scott Wheeler committed
589
     * \see editTag()
590
     */
Laurent Montel's avatar
Laurent Montel committed
591
    void slotInlineEditDone(Q3ListViewItem *, const QString &, int column);
592 593 594 595 596

    /**
     * This starts the renaming process by displaying a line edit if the mouse is in 
     * an appropriate position.
     */
597
    void slotRenameTag();
598 599 600 601 602

    /**
     * Moves the column \a from to the position \a to.  This matches the signature
     * for the signal QHeader::indexChange().
     */
603
    void slotColumnOrderChanged(int, int from, int to);
604 605 606 607 608 609 610

    /**
     * Toggles a columns visible status.  Useful for KActions.
     *
     * \see hideColumn()
     * \see showColumn()
     */
611
    void slotToggleColumnVisible(int column);
612

613 614 615
    /**
     * Prompts the user to create a new playlist with from the selected items.
     */
616
    void slotCreateGroup();
617

618 619 620 621 622 623
    /**
     * This slot is called when the user drags the slider in the listview header
     * to manually set the size of the column.
     */
    void slotColumnSizeChanged(int column, int oldSize, int newSize);

624 625 626 627 628 629 630 631
    /**
     * The slot is called when the completion mode for the line edit in the
     * inline tag editor is changed.  It saves the settings and through the
     * magic of the SharedSettings class will apply it to the other playlists as
     * well.
     */
    void slotInlineCompletionModeChanged(KGlobalSettings::Completion mode);

632 633
    void slotPlayCurrent();

634
private:
635 636
    PlaylistCollection *m_collection;

637
    StringHash m_members;
638

639
    int m_currentColumn;
640
    int m_processed;
641
    int m_rmbEditID;
642
    int m_rmbUpcomingID;
643 644
    int m_selectedCount;

645
    bool m_allowDuplicates;
646
    bool m_polished;
647
    bool m_applySharedSettings;
648
    bool m_mousePressed;
649

Laurent Montel's avatar
Laurent Montel committed
650
    Q3ValueList<int> m_weightDirty;
651
    bool m_disableColumnWidthUpdates;
652

653
    mutable int m_time;
Laurent Montel's avatar
Laurent Montel committed
654 655
    mutable Q3ValueList<PlaylistItem::Pointer> m_addTime;
    mutable Q3ValueList<PlaylistItem::Pointer> m_subtractTime;
656

657
    /**
658
     * The average minimum widths of columns to be used in balancing calculations.
659
     */
Laurent Montel's avatar
Laurent Montel committed
660 661
    Q3ValueVector<int> m_columnWeights;
    Q3ValueVector<int> m_columnFixedWidths;
662
    bool m_widthsDirty;
663

664
    static PlaylistItemList m_history;
665
    PlaylistSearch m_search;
666

667 668
    bool m_searchEnabled;

669
    PlaylistItem *m_lastSelected;
670

671
    /**
672
     * Used to store the text for inline editing before it is changed so that
673 674 675 676
     * we can know if something actually changed and as such if we need to save
     * the tag.
     */
    QString m_editText;
677 678 679 680 681

    /**
     * This is only defined if the playlist name is something other than the
     * file name.
     */
682
    QString m_playlistName;
683
    QString m_fileName;
684

Laurent Montel's avatar
Laurent Montel committed
685 686
    KMenu *m_rmbMenu;
    KMenu *m_headerMenu;
687
    KActionMenu *m_columnVisibleAction;
688
    PlaylistToolTip *m_toolTip;
689

690 691 692 693 694
    /**
     * This is used to indicate if the list of visible items has changed (via a 
     * call to setVisibleItems()) while random play is playing.
     */
    static bool m_visibleChanged;
695
    static int m_leftColumn;
696
    static QMap<int, PlaylistItem *> m_backMenuItems;
697 698

    bool m_blockDataChanged;
699 700
};

701
bool processEvents();
702

703 704 705 706 707 708 709
class FocusUpEvent : public QCustomEvent
{
public:
    FocusUpEvent() : QCustomEvent(id) {}
    static const int id = 999;
};

710
QDataStream &operator<<(QDataStream &s, const Playlist &p);
711 712
QDataStream &operator>>(QDataStream &s, Playlist &p);

713 714
// template method implementations

715
template <class ItemType, class CollectionItemType, class CollectionListType>
Laurent Montel's avatar
Laurent Montel committed
716
ItemType *Playlist::createItem(const FileHandle &file, Q3ListViewItem *after,
717
			       bool emitChanged)
718
{
719
    CollectionItemType *item = CollectionListType::instance()->lookup(file.absFilePath());
720 721

    if(!item) {
722
	item = new CollectionItemType(file);
723
	setupItem(item);
724 725

	// If a valid tag was not created, destroy the CollectionListItem.
726

727
	if(!item->isValid()) {
Laurent Montel's avatar
Laurent Montel committed
728
	    kError(65432) << "Playlist::createItem() -- A valid tag was not created for \""
729
			   << file.absFilePath() << "\"" << endl;
730 731 732 733 734
	    delete item;
	    return 0;
	}
    }

735
    if(item && !m_members.insert(file.absFilePath()) || m_allowDuplicates) {
736

737
	ItemType *i = after ? new ItemType(item, this, after) : new ItemType(item, this);
738 739
	setupItem(i);

740
	if(emitChanged)
741
	    dataChanged();
742 743 744 745

	return i;
    }
    else
746
	return 0;
747 748
}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
template <class ItemType, class SiblingType>
ItemType *Playlist::createItem(SiblingType *sibling, ItemType *after)
{
    m_disableColumnWidthUpdates = true;
    
    if(!m_members.insert(sibling->file().absFilePath()) || m_allowDuplicates) {
	after = new ItemType(sibling->collectionItem(), this, after);
	setupItem(after);
    }

    m_disableColumnWidthUpdates = false;

    return after;
}

764
template <class CollectionItemType, class ItemType, class SiblingType>
Laurent Montel's avatar
Laurent Montel committed
765
void Playlist::createItems(const Q3ValueList<SiblingType *> &siblings, ItemType *after)
766
{
767 768 769
    if(siblings.isEmpty())
	return;

Laurent Montel's avatar
Laurent Montel committed
770
    Q3ValueListConstIterator<SiblingType *> it = siblings.begin();
771 772
    for(; it != siblings.end(); ++it)
	after = createItem(*it, after);
773

774
    dataChanged();
775
    slotWeightDirty();
776 777
}

778
#endif