]> granicus.if.org Git - transmission/commitdiff
(trunk, qt) #4813: allow launching files in Qt interface
authorJordan Lee <jordan@transmissionbt.com>
Sun, 8 Sep 2013 19:03:25 +0000 (19:03 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sun, 8 Sep 2013 19:03:25 +0000 (19:03 +0000)
qt/details.cc
qt/details.h
qt/file-tree.cc
qt/file-tree.h

index ef40f0a9c282407cd1d219c4e4db241172966723..d18542662f632b6d0179869fdf48924a58120307 100644 (file)
@@ -17,6 +17,7 @@
 #include <QCheckBox>
 #include <QComboBox>
 #include <QDateTime>
+#include <QDesktopServices>
 #include <QDialogButtonBox>
 #include <QDoubleSpinBox>
 #include <QEvent>
@@ -1394,6 +1395,9 @@ Details :: createFilesTab ()
   connect (myFileTreeView, SIGNAL (pathEdited (const QString&, const QString&)),
            this,           SLOT (onPathEdited (const QString&, const QString&)));
 
+  connect (myFileTreeView, SIGNAL (openRequested (const QString&)),
+           this,           SLOT (onOpenRequested (const QString&)));
+
   return myFileTreeView;
 }
 
@@ -1434,3 +1438,24 @@ Details :: onPathEdited (const QString& oldpath, const QString& newname)
 {
   mySession.torrentRenamePath (myIds, oldpath, newname);
 }
+
+void
+Details :: onOpenRequested (const QString& path)
+{
+  if (!mySession.isLocal ())
+    return;
+
+  foreach (const int id, myIds)
+    {
+      const Torrent * const tor = myModel.getTorrentFromId (id);
+      if (tor == NULL)
+        continue;
+
+      const QString localFilePath = tor->getPath () + "/" + path;
+      if (!QFile::exists (localFilePath))
+        continue;
+
+      if (QDesktopServices::openUrl (QUrl::fromLocalFile (localFilePath)))
+        break;
+    }
+}
index 9b08d3146ff1e40bd5ac58e624d96d9bc6c515ec..5a61f0d9633320a50eebf8b87e9d6240ab755d30 100644 (file)
@@ -142,6 +142,7 @@ class Details: public QDialog
     void onFilePriorityChanged (const QSet<int>& fileIndices, int);
     void onFileWantedChanged (const QSet<int>& fileIndices, bool);
     void onPathEdited (const QString& oldpath, const QString& newname);
+    void onOpenRequested (const QString& path);
     void onHonorsSessionLimitsToggled (bool);
     void onDownloadLimitedToggled (bool);
     void onSpinBoxEditingFinished ();
index 8075aa25968497f07149e52f28b1713f0bace2ea..4014d5ef6001a5e8d5ef325d48caa4b0473d5a28 100644 (file)
@@ -380,6 +380,30 @@ FileTreeItem :: twiddleWanted (QSet<int>& ids, bool& wanted)
   setSubtreeWanted (wanted, ids);
 }
 
+QString
+FileTreeItem :: path () const
+{
+  QString itemPath;
+  const FileTreeItem * item = this;
+
+  while (item != NULL && !item->name().isEmpty())
+    {
+      if (itemPath.isEmpty())
+        itemPath = item->name();
+      else
+        itemPath = item->name() + "/" + itemPath;
+      item = item->parent ();
+    }
+
+  return itemPath;
+}
+
+bool
+FileTreeItem :: isComplete () const
+{
+  return myHaveSize == totalSize ();
+}
+
 /***
 ****
 ****
@@ -435,19 +459,9 @@ FileTreeModel :: setData (const QModelIndex& index, const QVariant& newname, int
 {
   if (role == Qt::EditRole)
     {
-      QString oldpath;
       FileTreeItem * item = itemFromIndex (index);
 
-      while (item && !item->name().isEmpty())
-        {
-          if (oldpath.isEmpty())
-            oldpath = item->name();
-          else
-            oldpath = item->name() + "/" + oldpath;
-          item = item->parent ();
-        }
-
-      emit pathEdited (oldpath, newname.toString());
+      emit pathEdited (item->path (), newname.toString ());
     }
 
   return false; // don't update the view until the session confirms the change
@@ -739,6 +753,22 @@ FileTreeModel :: clicked (const QModelIndex& index)
     }
 }
 
+void
+FileTreeModel :: doubleClicked (const QModelIndex& index)
+{
+  if (!index.isValid())
+    return;
+
+  const int column (index.column());
+  if (column == COL_WANTED || column == COL_PRIORITY)
+    return;
+
+  FileTreeItem * item = itemFromIndex (index);
+
+  if (item->childCount () == 0 && item->isComplete ())
+    emit openRequested (item->path ());
+}
+
 /****
 *****
 ****/
@@ -896,6 +926,9 @@ FileTreeView :: FileTreeView (QWidget * parent, bool isEditable):
   connect (this, SIGNAL(clicked(const QModelIndex&)),
            this, SLOT(onClicked(const QModelIndex&)));
 
+  connect (this, SIGNAL(doubleClicked(const QModelIndex&)),
+           this, SLOT(onDoubleClicked(const QModelIndex&)));
+
   connect (&myModel, SIGNAL(priorityChanged(const QSet<int>&, int)),
            this,     SIGNAL(priorityChanged(const QSet<int>&, int)));
 
@@ -904,6 +937,10 @@ FileTreeView :: FileTreeView (QWidget * parent, bool isEditable):
 
   connect (&myModel, SIGNAL(pathEdited(const QString&, const QString&)),
            this,     SIGNAL(pathEdited(const QString&, const QString&)));
+
+  connect (&myModel, SIGNAL (openRequested (const QString&)),
+           this,     SLOT (onOpenRequested (const QString&)),
+           Qt::QueuedConnection);
 }
 
 FileTreeView :: ~FileTreeView ()
@@ -918,6 +955,22 @@ FileTreeView :: onClicked (const QModelIndex& proxyIndex)
   myModel.clicked (modelIndex);
 }
 
+void
+FileTreeView :: onDoubleClicked (const QModelIndex& proxyIndex)
+{
+  const QModelIndex modelIndex = myProxy->mapToSource (proxyIndex);
+  myModel.doubleClicked (modelIndex);
+}
+
+void
+FileTreeView :: onOpenRequested (const QString& path)
+{
+  if (state () == EditingState)
+    return;
+
+  emit openRequested (path);
+}
+
 bool
 FileTreeView :: eventFilter (QObject * o, QEvent * event)
 {
index c65000953ba15d4aa51a086e8010961a3d34ad01..02486172b20e72e0a36c906c35e17b72d560acaf 100644 (file)
@@ -68,6 +68,8 @@ class FileTreeItem: public QObject
     void twiddlePriority (QSet<int>& fileIds, int&);
     int fileIndex () const { return myFileIndex; }
     uint64_t totalSize () const { return myTotalSize; }
+    QString path () const;
+    bool isComplete () const;
 
   private:
     void setSubtreePriority (int priority, QSet<int>& fileIds);
@@ -115,6 +117,7 @@ class FileTreeModel: public QAbstractItemModel
     void priorityChanged (const QSet<int>& fileIndices, int);
     void wantedChanged (const QSet<int>& fileIndices, bool);
     void pathEdited (const QString& oldpath, const QString& newname);
+    void openRequested (const QString& path);
 
   public:
     void clear ();
@@ -138,6 +141,7 @@ class FileTreeModel: public QAbstractItemModel
 
   public slots:
     void clicked (const QModelIndex & index);
+    void doubleClicked (const QModelIndex & index);
 };
 
 class FileTreeDelegate: public QItemDelegate
@@ -167,6 +171,7 @@ class FileTreeView: public QTreeView
     void priorityChanged (const QSet<int>& fileIndices, int priority);
     void wantedChanged (const QSet<int>& fileIndices, bool wanted);
     void pathEdited (const QString& oldpath, const QString& newname);
+    void openRequested (const QString& path);
 
   protected:
     bool eventFilter (QObject *, QEvent *);
@@ -178,6 +183,8 @@ class FileTreeView: public QTreeView
 
   public slots:
     void onClicked (const QModelIndex& index);
+    void onDoubleClicked (const QModelIndex& index);
+    void onOpenRequested (const QString& path);
 };
 
 #endif