Merge pull request #18104 from sledgehammer999/remove_dead_code
[qBittorrent.git] / src / gui / torrentcontenttreeview.cpp
blob81a90e2eef2b9656748b71d57112a86285e0decd
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 Ivan Sorokin <vanyacpp@gmail.com>
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.
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.
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.
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.
29 #include "torrentcontenttreeview.h"
31 #include <QDir>
32 #include <QHeaderView>
33 #include <QKeyEvent>
34 #include <QLineEdit>
35 #include <QMessageBox>
36 #include <QModelIndexList>
37 #include <QThread>
38 #include <QWheelEvent>
40 #include "base/bittorrent/abstractfilestorage.h"
41 #include "base/bittorrent/common.h"
42 #include "base/bittorrent/session.h"
43 #include "base/bittorrent/torrent.h"
44 #include "base/bittorrent/torrentinfo.h"
45 #include "base/exceptions.h"
46 #include "base/global.h"
47 #include "base/path.h"
48 #include "base/utils/fs.h"
49 #include "autoexpandabledialog.h"
50 #include "raisedmessagebox.h"
51 #include "torrentcontentfiltermodel.h"
52 #include "torrentcontentmodelitem.h"
54 namespace
56 Path getFullPath(const QModelIndex &idx)
58 Path path;
59 for (QModelIndex i = idx; i.isValid(); i = i.parent())
60 path = Path(i.data().toString()) / path;
61 return path;
65 TorrentContentTreeView::TorrentContentTreeView(QWidget *parent)
66 : QTreeView(parent)
68 setExpandsOnDoubleClick(false);
69 header()->setFirstSectionMovable(true);
72 void TorrentContentTreeView::keyPressEvent(QKeyEvent *event)
74 if ((event->key() != Qt::Key_Space) && (event->key() != Qt::Key_Select))
76 QTreeView::keyPressEvent(event);
77 return;
80 event->accept();
82 const QVariant value = currentNameCell().data(Qt::CheckStateRole);
83 if (!value.isValid())
85 Q_ASSERT(false);
86 return;
89 const Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked)
90 ? Qt::Unchecked : Qt::Checked;
91 const QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME);
93 for (const QModelIndex &index : selection)
94 model()->setData(index, state, Qt::CheckStateRole);
97 void TorrentContentTreeView::renameSelectedFile(BitTorrent::AbstractFileStorage &fileStorage)
99 const QModelIndexList selectedIndexes = selectionModel()->selectedRows(0);
100 if (selectedIndexes.size() != 1) return;
102 const QPersistentModelIndex modelIndex = selectedIndexes.first();
103 if (!modelIndex.isValid()) return;
105 auto model = dynamic_cast<TorrentContentFilterModel *>(TorrentContentTreeView::model());
106 if (!model) return;
108 const bool isFile = (model->itemType(modelIndex) == TorrentContentModelItem::FileType);
110 // Ask for new name
111 bool ok = false;
112 QString newName = AutoExpandableDialog::getText(this, tr("Renaming"), tr("New name:"), QLineEdit::Normal
113 , modelIndex.data().toString(), &ok, isFile).trimmed();
114 if (!ok || !modelIndex.isValid()) return;
116 const QString oldName = modelIndex.data().toString();
117 if (newName == oldName)
118 return; // Name did not change
120 const Path parentPath = getFullPath(modelIndex.parent());
121 const Path oldPath = parentPath / Path(oldName);
122 const Path newPath = parentPath / Path(newName);
126 if (isFile)
127 fileStorage.renameFile(oldPath, newPath);
128 else
129 fileStorage.renameFolder(oldPath, newPath);
131 model->setData(modelIndex, newName);
133 catch (const RuntimeError &error)
135 RaisedMessageBox::warning(this, tr("Rename error"), error.message(), QMessageBox::Ok);
139 QModelIndex TorrentContentTreeView::currentNameCell() const
141 const QModelIndex current = currentIndex();
142 if (!current.isValid())
144 Q_ASSERT(false);
145 return {};
148 return current.siblingAtColumn(TorrentContentModelItem::COL_NAME);
151 void TorrentContentTreeView::wheelEvent(QWheelEvent *event)
153 if (event->modifiers() & Qt::ShiftModifier)
155 // Shift + scroll = horizontal scroll
156 event->accept();
157 QWheelEvent scrollHEvent {event->position(), event->globalPosition()
158 , event->pixelDelta(), event->angleDelta().transposed(), event->buttons()
159 , event->modifiers(), event->phase(), event->inverted(), event->source()};
160 QTreeView::wheelEvent(&scrollHEvent);
161 return;
164 QTreeView::wheelEvent(event); // event delegated to base class