qBittorrent
torrentcontenttreeview.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2014 Ivan Sorokin <vanyacpp@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "torrentcontenttreeview.h"
30 
31 #include <QDir>
32 #include <QHeaderView>
33 #include <QKeyEvent>
34 #include <QLineEdit>
35 #include <QMessageBox>
36 #include <QModelIndexList>
37 #include <QTableView>
38 #include <QThread>
39 
41 #include "base/bittorrent/common.h"
45 #include "base/exceptions.h"
46 #include "base/global.h"
47 #include "base/utils/fs.h"
48 #include "autoexpandabledialog.h"
49 #include "raisedmessagebox.h"
52 
53 namespace
54 {
55  QString getFullPath(const QModelIndex &idx)
56  {
57  QStringList paths;
58  for (QModelIndex i = idx; i.isValid(); i = i.parent())
59  paths.prepend(i.data().toString());
60  return paths.join(QLatin1Char {'/'});
61  }
62 }
63 
65  : QTreeView(parent)
66 {
67  setExpandsOnDoubleClick(false);
68 
69  // This hack fixes reordering of first column with Qt5.
70  // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
71  QTableView unused;
72  unused.setVerticalHeader(header());
73  header()->setParent(this);
74  header()->setStretchLastSection(false);
75  header()->setTextElideMode(Qt::ElideRight);
76  unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
77 }
78 
80 {
81  if ((event->key() != Qt::Key_Space) && (event->key() != Qt::Key_Select))
82  {
83  QTreeView::keyPressEvent(event);
84  return;
85  }
86 
87  event->accept();
88 
89  QModelIndex current = currentNameCell();
90 
91  QVariant value = current.data(Qt::CheckStateRole);
92  if (!value.isValid())
93  {
94  Q_ASSERT(false);
95  return;
96  }
97 
98  Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
99  ? Qt::Unchecked : Qt::Checked);
100 
101  const QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME);
102 
103  for (const QModelIndex &index : selection)
104  {
105  Q_ASSERT(index.column() == TorrentContentModelItem::COL_NAME);
106  model()->setData(index, state, Qt::CheckStateRole);
107  }
108 }
109 
111 {
112  const QModelIndexList selectedIndexes = selectionModel()->selectedRows(0);
113  if (selectedIndexes.size() != 1) return;
114 
115  const QPersistentModelIndex modelIndex = selectedIndexes.first();
116  if (!modelIndex.isValid()) return;
117 
118  auto model = dynamic_cast<TorrentContentFilterModel *>(TorrentContentTreeView::model());
119  if (!model) return;
120 
121  const bool isFile = (model->itemType(modelIndex) == TorrentContentModelItem::FileType);
122 
123  // Ask for new name
124  bool ok = false;
125  QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal
126  , modelIndex.data().toString(), &ok, isFile).trimmed();
127  if (!ok || !modelIndex.isValid()) return;
128 
129  const QString oldName = modelIndex.data().toString();
130  if (newName == oldName)
131  return; // Name did not change
132 
133  const QString parentPath = getFullPath(modelIndex.parent());
134  const QString oldPath {parentPath.isEmpty() ? oldName : parentPath + QLatin1Char {'/'} + oldName};
135  const QString newPath {parentPath.isEmpty() ? newName : parentPath + QLatin1Char {'/'} + newName};
136 
137  try
138  {
139  if (isFile)
140  fileStorage.renameFile(oldPath, newPath);
141  else
142  fileStorage.renameFolder(oldPath, newPath);
143 
144  model->setData(modelIndex, newName);
145  }
146  catch (const RuntimeError &error)
147  {
148  RaisedMessageBox::warning(this, tr("Rename error"), error.message(), QMessageBox::Ok);
149  }
150 }
151 
153 {
154  QModelIndex current = currentIndex();
155  if (!current.isValid())
156  {
157  Q_ASSERT(false);
158  return {};
159  }
160 
161  return model()->index(current.row(), TorrentContentModelItem::COL_NAME, current.parent());
162 }
static QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode=QLineEdit::Normal, const QString &text={}, bool *ok=nullptr, bool excludeExtension=false, Qt::InputMethodHints inputMethodHints=Qt::ImhNone)
void renameFolder(const QString &oldPath, const QString &newPath)
virtual void renameFile(int index, const QString &name)=0
QString message() const noexcept
Definition: exceptions.cpp:36
static QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons=QMessageBox::Ok, QMessageBox::StandardButton defaultButton=QMessageBox::NoButton)
TorrentContentTreeView(QWidget *parent=nullptr)
void keyPressEvent(QKeyEvent *event) override
void renameSelectedFile(BitTorrent::AbstractFileStorage &fileStorage)
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64