]> Some of my projects - localmylist.git/commitdiff
Some Fun with context menus
authorAPTX <marek321@gmail.com>
Sat, 2 Jun 2012 22:13:14 +0000 (00:13 +0200)
committerAPTX <marek321@gmail.com>
Sat, 2 Jun 2012 22:13:38 +0000 (00:13 +0200)
management-gui/mainwindow.cpp
management-gui/mainwindow.h
management-gui/mainwindow.ui
management-gui/management-gui.pro
management-gui/mylistview.cpp [new file with mode: 0644]
management-gui/mylistview.h [new file with mode: 0644]

index b0c6f1dae64ee9ac53734f37019da30f8aab222e..d6b379bcd7476ee5ee658f26ee289cd17c2a6cc2 100644 (file)
@@ -2,8 +2,10 @@
 #include "ui_mainwindow.h"
 
 #include <QLabel>
-#include <QSqlTableModel>
+#include <QSqlError>
 #include <QFileDialog>
+#include <QDesktopServices>
+#include <QUrl>
 
 #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));
+
+}
index 2c8be5b4ff9782cd38eb7a83f95382b98bf40dd9..73917aca73891fde58bd4571ed6d08ad9aebbd67 100644 (file)
@@ -2,6 +2,7 @@
 #define MAINWINDOW_H
 
 #include <QMainWindow>
+#include <QModelIndex>
 
 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;
 
index 12f156b9baa8d515409fd9636f80afb45c7fb928..aa9631d3b5a06c12a30a612089dcb394162f8cb8 100644 (file)
    </rect>
   </property>
   <property name="windowTitle">
-   <string>MainWindow</string>
+   <string>LocalMyList Management</string>
   </property>
   <widget class="QWidget" name="centralWidget">
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
-     <widget class="QTreeView" name="myListView"/>
+     <widget class="MyListView" name="myListView"/>
     </item>
    </layout>
   </widget>
   </action>
  </widget>
  <layoutdefault spacing="6" margin="11"/>
+ <customwidgets>
+  <customwidget>
+   <class>MyListView</class>
+   <extends>QTreeView</extends>
+   <header>mylistview.h</header>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>
index 8f21f5294028f68ff1c7c600efa9e21a89e94c6b..a662923d01883d842c837709b89070f4dd40f5f5 100644 (file)
@@ -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 (file)
index 0000000..27665a1
--- /dev/null
@@ -0,0 +1,34 @@
+#include "mylistview.h"
+
+#include "mylistmodel.h"
+
+#include <QMenu>
+
+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<LocalMyList::MyListModel *>(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<QAction *> 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 (file)
index 0000000..c384aae
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef MYLISTVIEW_H
+#define MYLISTVIEW_H
+
+#include <QTreeView>
+
+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