Right click menu for the file view

This commit is contained in:
David Sansome 2009-12-24 22:26:58 +00:00
parent 15158805af
commit 66478974cd
13 changed files with 146 additions and 49 deletions

BIN
data/copy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 485 B

View File

@ -1,5 +1,5 @@
<RCC>
<qresource prefix="/">
<qresource prefix="/" >
<file>clear.png</file>
<file>go-home.png</file>
<file>go-next.png</file>
@ -31,5 +31,7 @@
<file>folder.png</file>
<file>configure.png</file>
<file>exit.png</file>
<file>copy.png</file>
<file>move.png</file>
</qresource>
</RCC>

BIN
data/move.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 B

View File

@ -24,6 +24,9 @@ FileView::FileView(QWidget* parent)
connect(ui_.list, SIGNAL(activated(QModelIndex)), SLOT(ItemActivated(QModelIndex)));
connect(ui_.list, SIGNAL(doubleClicked(QModelIndex)), SLOT(ItemDoubleClick(QModelIndex)));
connect(ui_.list, SIGNAL(AddToPlaylist(QList<QUrl>)), SIGNAL(Queue(QList<QUrl>)));
connect(ui_.list, SIGNAL(CopyToLibrary(QList<QUrl>)), SIGNAL(CopyToLibrary(QList<QUrl>)));
connect(ui_.list, SIGNAL(MoveToLibrary(QList<QUrl>)), SIGNAL(MoveToLibrary(QList<QUrl>)));
}
void FileView::SetPath(const QString& path) {
@ -83,5 +86,5 @@ void FileView::ItemDoubleClick(const QModelIndex& index) {
if (model_->isDir(index))
return;
emit PlayFile(model_->filePath(index));
emit Queue(QList<QUrl>() << QUrl::fromLocalFile(model_->filePath(index)));
}

View File

@ -19,8 +19,9 @@ class FileView : public QWidget {
signals:
void PathChanged(const QString& path);
void PlayFile(const QString& path);
void PlayDirectory(const QString& path);
void Queue(const QList<QUrl>& urls);
void CopyToLibrary(const QList<QUrl>& urls);
void MoveToLibrary(const QList<QUrl>& urls);
private slots:
void FileUp();

View File

@ -31,7 +31,7 @@
<bool>false</bool>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/go-previous.png</normaloff>:/go-previous.png</iconset>
</property>
<property name="autoRaise">
@ -48,7 +48,7 @@
<string>...</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/go-next.png</normaloff>:/go-next.png</iconset>
</property>
<property name="autoRaise">
@ -62,7 +62,7 @@
<string>...</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/go-up.png</normaloff>:/go-up.png</iconset>
</property>
<property name="autoRaise">
@ -76,7 +76,7 @@
<string>...</string>
</property>
<property name="icon">
<iconset>
<iconset resource="../data/data.qrc">
<normaloff>:/go-home.png</normaloff>:/go-home.png</iconset>
</property>
<property name="autoRaise">
@ -90,7 +90,7 @@
</layout>
</item>
<item>
<widget class="QListView" name="list">
<widget class="FileViewList" name="list">
<property name="dragEnabled">
<bool>true</bool>
</property>
@ -107,6 +107,15 @@
</item>
</layout>
</widget>
<resources/>
<customwidgets>
<customwidget>
<class>FileViewList</class>
<extends>QListView</extends>
<header>fileviewlist.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../data/data.qrc"/>
</resources>
<connections/>
</ui>

48
src/fileviewlist.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "fileviewlist.h"
#include <QMenu>
#include <QContextMenuEvent>
#include <QFileSystemModel>
#include <QtDebug>
FileViewList::FileViewList(QWidget* parent)
: QListView(parent),
menu_(new QMenu(this))
{
menu_->addAction(QIcon(":media-playback-start.png"), "Add to playlist",
this, SLOT(AddToPlaylistSlot()));
menu_->addSeparator();
menu_->addAction(QIcon(":copy.png"), "Copy to library...",
this, SLOT(CopyToLibrarySlot()));
menu_->addAction(QIcon(":move.png"),"Move to library...",
this, SLOT(MoveToLibrarySlot()));
}
void FileViewList::contextMenuEvent(QContextMenuEvent* e) {
menu_selection_ = selectionModel()->selection();
menu_->popup(e->globalPos());
e->accept();
}
QList<QUrl> FileViewList::UrlListFromSelection() const {
QList<QUrl> urls;
foreach (const QModelIndex& index, menu_selection_.indexes()) {
if (index.column() == 0)
urls << QUrl::fromLocalFile(
static_cast<QFileSystemModel*>(model())->filePath(index));
}
return urls;
}
void FileViewList::AddToPlaylistSlot() {
emit AddToPlaylist(UrlListFromSelection());
}
void FileViewList::CopyToLibrarySlot() {
emit CopyToLibrary(UrlListFromSelection());
}
void FileViewList::MoveToLibrarySlot() {
emit MoveToLibrary(UrlListFromSelection());
}

33
src/fileviewlist.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef FILEVIEWLIST_H
#define FILEVIEWLIST_H
#include <QListView>
#include <QUrl>
class FileViewList : public QListView {
Q_OBJECT
public:
FileViewList(QWidget* parent = 0);
signals:
void AddToPlaylist(const QList<QUrl>& urls);
void CopyToLibrary(const QList<QUrl>& urls);
void MoveToLibrary(const QList<QUrl>& urls);
protected:
void contextMenuEvent(QContextMenuEvent* e);
private slots:
void AddToPlaylistSlot();
void CopyToLibrarySlot();
void MoveToLibrarySlot();
QList<QUrl> UrlListFromSelection() const;
private:
QMenu* menu_;
QItemSelection menu_selection_;
};
#endif // FILEVIEWLIST_H

View File

@ -48,7 +48,7 @@ MainWindow::MainWindow(QWidget *parent)
ui_.library_view->SetLibrary(library_);
// File view connections
connect(ui_.file_view, SIGNAL(PlayFile(QString)), SLOT(PlayFile(QString)));
connect(ui_.file_view, SIGNAL(Queue(QList<QUrl>)), SLOT(QueueFiles(QList<QUrl>)));
connect(ui_.file_view, SIGNAL(PathChanged(QString)), SLOT(FilePathChanged(QString)));
// Action connections
@ -176,14 +176,8 @@ MainWindow::~MainWindow() {
SaveGeometry();
}
void MainWindow::PlayFile(const QString& path) {
Song song;
song.InitFromFile(path, -1);
if (!song.is_valid())
return;
QModelIndex playlist_index = playlist_->InsertSongs(SongList() << song);
void MainWindow::QueueFiles(const QList<QUrl>& urls) {
QModelIndex playlist_index = playlist_->InsertPaths(urls);
if (playlist_index.isValid() && player_->GetState() != Engine::Playing)
player_->PlayAt(playlist_index.row());

View File

@ -28,7 +28,7 @@ class MainWindow : public QMainWindow {
void closeEvent(QCloseEvent* event);
private slots:
void PlayFile(const QString& path);
void QueueFiles(const QList<QUrl>& urls);
void FilePathChanged(const QString& path);
void ReportError(const QString& message);

View File

@ -158,48 +158,52 @@ bool Playlist::dropMimeData(const QMimeData* data, Qt::DropAction action, int ro
layoutChanged();
} else if (data->hasUrls()) {
// URL list dragged from some other app probably
SongList songs;
foreach (const QUrl& url, data->urls()) {
if (url.scheme() != "file")
continue;
// URL list dragged from the file list or some other app
InsertPaths(data->urls(), row);
}
QString filename(url.toLocalFile());
QFileInfo info(filename);
return true;
}
if (!info.exists())
continue;
QModelIndex Playlist::InsertPaths(const QList<QUrl>& urls, int after) {
SongList songs;
foreach (const QUrl& url, urls) {
if (url.scheme() != "file")
continue;
if (info.isDir()) {
// Add all the songs in the directory
QDirIterator it(filename,
QDir::Files | QDir::NoDotAndDotDot | QDir::Readable,
QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
QString filename(url.toLocalFile());
QFileInfo info(filename);
while (it.hasNext()) {
QString path(it.next());
if (!info.exists())
continue;
Song song;
song.InitFromFile(path, -1);
if (!song.is_valid())
continue;
if (info.isDir()) {
// Add all the songs in the directory
QDirIterator it(filename,
QDir::Files | QDir::NoDotAndDotDot | QDir::Readable,
QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
while (it.hasNext()) {
QString path(it.next());
songs << song;
}
} else {
Song song;
song.InitFromFile(filename, -1);
song.InitFromFile(path, -1);
if (!song.is_valid())
continue;
songs << song;
}
}
} else {
Song song;
song.InitFromFile(filename, -1);
if (!song.is_valid())
continue;
InsertSongs(songs, row);
songs << song;
}
}
return true;
return InsertSongs(songs, after);
}
QModelIndex Playlist::InsertItems(const QList<PlaylistItem*>& items, int after) {

View File

@ -49,6 +49,7 @@ class Playlist : public QAbstractListModel {
// Changing the playlist
QModelIndex InsertItems(const QList<PlaylistItem*>& items, int after = -1);
QModelIndex InsertSongs(const SongList& items, int after = -1);
QModelIndex InsertPaths(const QList<QUrl>& urls, int after = -1);
void StopAfter(int row);
// QAbstractListModel

View File

@ -31,7 +31,8 @@ SOURCES += main.cpp \
libraryconfig.cpp \
systemtrayicon.cpp \
libraryquery.cpp \
fileview.cpp
fileview.cpp \
fileviewlist.cpp
HEADERS += mainwindow.h \
player.h \
library.h \
@ -59,7 +60,8 @@ HEADERS += mainwindow.h \
libraryconfig.h \
systemtrayicon.h \
libraryquery.h \
fileview.h
fileview.h \
fileviewlist.h
FORMS += mainwindow.ui \
libraryconfig.ui \
fileview.ui