Allow to refresh existing search
[qBittorrent.git] / src / gui / previewselectdialog.cpp
blob00d103cab63e0475b42f18d304c517aa07f08dcb
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "previewselectdialog.h"
32 #include <QAction>
33 #include <QDir>
34 #include <QHeaderView>
35 #include <QMenu>
36 #include <QMessageBox>
37 #include <QPushButton>
38 #include <QShowEvent>
39 #include <QStandardItemModel>
41 #include "base/bittorrent/torrent.h"
42 #include "base/preferences.h"
43 #include "base/utils/fs.h"
44 #include "base/utils/misc.h"
45 #include "previewlistdelegate.h"
46 #include "ui_previewselectdialog.h"
47 #include "utils.h"
49 #define SETTINGS_KEY(name) u"PreviewSelectDialog/" name
51 enum Roles
53 SortRole = Qt::UserRole
56 PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torrent *torrent)
57 : QDialog(parent)
58 , m_ui {new Ui::PreviewSelectDialog}
59 , m_torrent {torrent}
60 , m_storeDialogSize {SETTINGS_KEY(u"Size"_s)}
61 , m_storeTreeHeaderState {u"GUI/Qt6/" SETTINGS_KEY(u"HeaderState"_s)}
63 m_ui->setupUi(this);
65 m_ui->label->setText(tr("The following files from torrent \"%1\" support previewing, please select one of them:")
66 .arg(m_torrent->name()));
68 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Preview"));
69 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &PreviewSelectDialog::previewButtonClicked);
70 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
71 connect(m_ui->previewList, &QAbstractItemView::doubleClicked, this, &PreviewSelectDialog::previewButtonClicked);
73 const Preferences *pref = Preferences::instance();
74 // Preview list
75 auto *previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
76 previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
77 previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
78 previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
80 m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
81 m_ui->previewList->setUniformRowHeights(true);
82 m_ui->previewList->setModel(previewListModel);
84 auto *listDelegate = new PreviewListDelegate(this);
85 m_ui->previewList->setItemDelegate(listDelegate);
87 // Fill list in
88 const QList<qreal> fp = torrent->filesProgress();
89 for (int i = 0; i < torrent->filesCount(); ++i)
91 const Path filePath = torrent->filePath(i);
92 if (Utils::Misc::isPreviewable(filePath))
94 int row = previewListModel->rowCount();
95 previewListModel->insertRow(row);
96 previewListModel->setData(previewListModel->index(row, NAME), filePath.filename(), Qt::DisplayRole);
97 previewListModel->setData(previewListModel->index(row, NAME), filePath.filename(), SortRole);
98 // Sorting file size by bytes, while displaying by human readable format
99 previewListModel->setData(previewListModel->index(row, SIZE), Utils::Misc::friendlyUnit(torrent->fileSize(i)), Qt::DisplayRole);
100 previewListModel->setData(previewListModel->index(row, SIZE), torrent->fileSize(i), SortRole);
101 previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i], Qt::DisplayRole);
102 previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i], SortRole);
103 previewListModel->setData(previewListModel->index(row, FILE_INDEX), i, Qt::DisplayRole);
104 previewListModel->setData(previewListModel->index(row, FILE_INDEX), i, SortRole);
108 previewListModel->setSortRole(SortRole);
109 previewListModel->sort(NAME);
110 m_ui->previewList->header()->setContextMenuPolicy(Qt::CustomContextMenu);
111 m_ui->previewList->header()->setFirstSectionMovable(true);
112 m_ui->previewList->header()->setSortIndicator(0, Qt::AscendingOrder);
113 m_ui->previewList->selectionModel()->select(previewListModel->index(0, NAME), QItemSelectionModel::Select | QItemSelectionModel::Rows);
115 connect(m_ui->previewList->header(), &QWidget::customContextMenuRequested, this, &PreviewSelectDialog::displayColumnHeaderMenu);
117 // Restore dialog state
118 loadWindowState();
119 m_ui->previewList->hideColumn(FILE_INDEX);
122 PreviewSelectDialog::~PreviewSelectDialog()
124 saveWindowState();
126 delete m_ui;
129 void PreviewSelectDialog::previewButtonClicked()
131 const QModelIndexList selectedIndexes = m_ui->previewList->selectionModel()->selectedRows(FILE_INDEX);
132 if (selectedIndexes.isEmpty()) return;
134 // Flush data
135 m_torrent->flushCache();
137 // Only one file should be selected
138 const int fileIndex = selectedIndexes.at(0).data().toInt();
139 const Path path = m_torrent->actualStorageLocation() / m_torrent->actualFilePath(fileIndex);
140 // File
141 if (!path.exists())
143 const bool isSingleFile = (m_ui->previewList->model()->rowCount() == 1);
144 QWidget *parent = isSingleFile ? this->parentWidget() : this;
145 QMessageBox::critical(parent, tr("Preview impossible")
146 , tr("Sorry, we can't preview this file: \"%1\".").arg(path.toString()));
147 if (isSingleFile)
148 reject();
149 return;
152 emit readyToPreviewFile(path);
153 accept();
156 void PreviewSelectDialog::displayColumnHeaderMenu()
158 auto *menu = new QMenu(this);
159 menu->setAttribute(Qt::WA_DeleteOnClose);
160 menu->setToolTipsVisible(true);
162 QAction *resizeAction = menu->addAction(tr("Resize columns"), this, [this]()
164 for (int i = 0, count = m_ui->previewList->header()->count(); i < count; ++i)
166 if (!m_ui->previewList->isColumnHidden(i))
167 m_ui->previewList->resizeColumnToContents(i);
170 resizeAction->setToolTip(tr("Resize all non-hidden columns to the size of their contents"));
172 menu->popup(QCursor::pos());
175 void PreviewSelectDialog::saveWindowState()
177 // Persist dialog size
178 m_storeDialogSize = size();
179 // Persist TreeView Header state
180 m_storeTreeHeaderState = m_ui->previewList->header()->saveState();
183 void PreviewSelectDialog::loadWindowState()
185 // Restore dialog size
186 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
187 resize(dialogSize);
189 // Restore TreeView Header state
190 if (const QByteArray treeHeaderState = m_storeTreeHeaderState; !treeHeaderState.isEmpty())
191 m_headerStateInitialized = m_ui->previewList->header()->restoreState(treeHeaderState);
194 void PreviewSelectDialog::showEvent(QShowEvent *event)
196 // event originated from system
197 if (event->spontaneous())
199 QDialog::showEvent(event);
200 return;
203 // Default size, have to be called after show(), because width is needed
204 // Set Name column width to 60% of TreeView
205 if (!m_headerStateInitialized)
207 const int nameSize = (m_ui->previewList->size().width() * 0.6);
208 m_ui->previewList->header()->resizeSection(0, nameSize);
209 m_headerStateInitialized = true;
212 // Only one file, no choice
213 if (m_ui->previewList->model()->rowCount() <= 1)
214 previewButtonClicked();