From: APTX Date: Sat, 2 Jun 2012 22:13:14 +0000 (+0200) Subject: Some Fun with context menus X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=14288ee3825aa112f1f9570a277a80dea93c98b5;p=localmylist.git Some Fun with context menus --- diff --git a/management-gui/mainwindow.cpp b/management-gui/mainwindow.cpp index b0c6f1d..d6b379b 100644 --- a/management-gui/mainwindow.cpp +++ b/management-gui/mainwindow.cpp @@ -2,8 +2,10 @@ #include "ui_mainwindow.h" #include -#include +#include #include +#include +#include #include "mylist.h" #include "database.h" @@ -111,3 +113,73 @@ void MainWindow::on_actionHandlePendingRequests_triggered() { } + +void MainWindow::on_myListView_doubleClicked(const QModelIndex &index) +{ + MyListModel::NodeType type = animeModel->type(index); + int id = animeModel->id(index); + + if (!id) + return; + + QString path; + QSqlQuery q(MyList::instance()->database()->connection()); + + if (type == MyListModel::Anime) + { + q.prepare( + "SELECT fl.path, e.epno, a.title_romaji, e.title_english FROM file f " + " LEFT JOIN anime a ON f.aid = a.aid " + " LEFT JOIN episode e ON f.eid = e.eid " + " LEFT JOIN file_location fl ON fl.fid = f.fid " + " WHERE f.my_watched IS NULL " + " AND f.aid = :aid " + " AND fl.path IS NOT NULL " + " GROUP BY fl.path, a.title_romaji, e.epno, e.title_english " + " ORDER BY epno ASC "); + q.bindValue(":aid", id); + } + else if (type == MyListModel::Episode) + { + q.prepare( + "SELECT fl.path FROM file f " + " LEFT JOIN file_location fl ON fl.fid = f.fid " + " WHERE f.my_watched IS NULL " + " AND f.eid = :eid " + " AND fl.path IS NOT NULL " + " ORDER BY f.version DESC "); + q.bindValue(":eid", id); + } + else if (type == MyListModel::File) + { + q.prepare( + "SELECT fl.path FROM file f " + " LEFT JOIN file_location fl ON fl.fid = f.fid " + " WHERE f.my_watched IS NULL " + " AND f.fid = :fid " + " AND fl.path IS NOT NULL "); + q.bindValue(":fid", id); + } + else + { + return; + } + + if (!q.exec()) + { + qDebug() << q.lastError(); + return; + } + + if (!q.next()) + { + ui->statusBar->showMessage(tr("No file found.")); + return; + } + + path = q.value(0).toString(); + + QDesktopServices::openUrl(QUrl("file:///" + path, QUrl::TolerantMode)); + ui->statusBar->showMessage(tr("Openieng file: %1").arg(path)); + +} diff --git a/management-gui/mainwindow.h b/management-gui/mainwindow.h index 2c8be5b..73917ac 100644 --- a/management-gui/mainwindow.h +++ b/management-gui/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include namespace Ui { class MainWindow; @@ -37,6 +38,8 @@ private slots: void on_actionClearAnimeTitleData_triggered(); void on_actionHandlePendingRequests_triggered(); + void on_myListView_doubleClicked(const QModelIndex &index); + private: Ui::MainWindow *ui; diff --git a/management-gui/mainwindow.ui b/management-gui/mainwindow.ui index 12f156b..aa9631d 100644 --- a/management-gui/mainwindow.ui +++ b/management-gui/mainwindow.ui @@ -11,12 +11,12 @@ - MainWindow + LocalMyList Management - + @@ -129,6 +129,13 @@ + + + MyListView + QTreeView +
mylistview.h
+
+
diff --git a/management-gui/management-gui.pro b/management-gui/management-gui.pro index 8f21f52..a662923 100644 --- a/management-gui/management-gui.pro +++ b/management-gui/management-gui.pro @@ -9,10 +9,12 @@ TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ - databaseconnectiondialog.cpp + databaseconnectiondialog.cpp \ + mylistview.cpp HEADERS += mainwindow.h \ - databaseconnectiondialog.h + databaseconnectiondialog.h \ + mylistview.h FORMS += mainwindow.ui \ databaseconnectiondialog.ui diff --git a/management-gui/mylistview.cpp b/management-gui/mylistview.cpp new file mode 100644 index 0000000..27665a1 --- /dev/null +++ b/management-gui/mylistview.cpp @@ -0,0 +1,34 @@ +#include "mylistview.h" + +#include "mylistmodel.h" + +#include + +MyListView::MyListView(QWidget *parent) : + QTreeView(parent) +{ + setContextMenuPolicy(Qt::CustomContextMenu); + connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showCustomContextMenu(QPoint))); +} + +LocalMyList::MyListModel *MyListView::myListModel() const +{ + return qobject_cast(model()); +} + +void MyListView::showCustomContextMenu(const QPoint &pos) +{ + using namespace LocalMyList; + + const QModelIndex idx = indexAt(pos); + if (!idx.isValid()) + return; + + QAction a(tr("Dummyaction r=%1, c=%2").arg(idx.row()).arg(idx.column()), this); + MyListModel::NodeType t = myListModel()->type(idx); + QAction b(tr("%1 = %2").arg(t == MyListModel::Anime ? "aid" : t == MyListModel::Episode ? "eid" : t == MyListModel::File ? "fid" : "none").arg(myListModel()->id(idx)), this); + + QList actions; + actions << &a << &b; + QMenu::exec(actions, viewport()->mapToGlobal(pos)); +} diff --git a/management-gui/mylistview.h b/management-gui/mylistview.h new file mode 100644 index 0000000..c384aae --- /dev/null +++ b/management-gui/mylistview.h @@ -0,0 +1,22 @@ +#ifndef MYLISTVIEW_H +#define MYLISTVIEW_H + +#include + +namespace LocalMyList { + class MyListModel; +} + +class MyListView : public QTreeView +{ + Q_OBJECT +public: + explicit MyListView(QWidget *parent = 0); + +private slots: + LocalMyList::MyListModel *myListModel() const; + void showCustomContextMenu(const QPoint &pos); + +}; + +#endif // MYLISTVIEW_H