Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / previewselectdialog.cpp
blob1d8dd2d3ff7bff15cdb19161a37600313d3fd7ea
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 PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torrent *torrent)
52 : QDialog(parent)
53 , m_ui {new Ui::PreviewSelectDialog}
54 , m_torrent {torrent}
55 , m_storeDialogSize {SETTINGS_KEY(u"Size"_s)}
56 , m_storeTreeHeaderState {u"GUI/Qt6/" SETTINGS_KEY(u"HeaderState"_s)}
58 m_ui->setupUi(this);
60 m_ui->label->setText(tr("The following files from torrent \"%1\" support previewing, please select one of them:")
61 .arg(m_torrent->name()));
63 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Preview"));
64 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &PreviewSelectDialog::previewButtonClicked);
65 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
66 connect(m_ui->previewList, &QAbstractItemView::doubleClicked, this, &PreviewSelectDialog::previewButtonClicked);
68 const Preferences *pref = Preferences::instance();
69 // Preview list
70 auto *previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
71 previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
72 previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
73 previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
75 m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
76 m_ui->previewList->setUniformRowHeights(true);
77 m_ui->previewList->setModel(previewListModel);
78 m_ui->previewList->hideColumn(FILE_INDEX);
80 auto *listDelegate = new PreviewListDelegate(this);
81 m_ui->previewList->setItemDelegate(listDelegate);
83 // Fill list in
84 const QList<qreal> fp = torrent->filesProgress();
85 for (int i = 0; i < torrent->filesCount(); ++i)
87 const Path filePath = torrent->filePath(i);
88 if (Utils::Misc::isPreviewable(filePath))
90 int row = previewListModel->rowCount();
91 previewListModel->insertRow(row);
92 previewListModel->setData(previewListModel->index(row, NAME), filePath.filename());
93 previewListModel->setData(previewListModel->index(row, SIZE), Utils::Misc::friendlyUnit(torrent->fileSize(i)));
94 previewListModel->setData(previewListModel->index(row, PROGRESS), fp[i]);
95 previewListModel->setData(previewListModel->index(row, FILE_INDEX), i);
99 previewListModel->sort(NAME);
100 m_ui->previewList->header()->setContextMenuPolicy(Qt::CustomContextMenu);
101 m_ui->previewList->header()->setFirstSectionMovable(true);
102 m_ui->previewList->header()->setSortIndicator(0, Qt::AscendingOrder);
103 m_ui->previewList->selectionModel()->select(previewListModel->index(0, NAME), QItemSelectionModel::Select | QItemSelectionModel::Rows);
105 connect(m_ui->previewList->header(), &QWidget::customContextMenuRequested, this, &PreviewSelectDialog::displayColumnHeaderMenu);
107 // Restore dialog state
108 loadWindowState();
111 PreviewSelectDialog::~PreviewSelectDialog()
113 saveWindowState();
115 delete m_ui;
118 void PreviewSelectDialog::previewButtonClicked()
120 const QModelIndexList selectedIndexes = m_ui->previewList->selectionModel()->selectedRows(FILE_INDEX);
121 if (selectedIndexes.isEmpty()) return;
123 // Flush data
124 m_torrent->flushCache();
126 // Only one file should be selected
127 const int fileIndex = selectedIndexes.at(0).data().toInt();
128 const Path path = m_torrent->actualStorageLocation() / m_torrent->actualFilePath(fileIndex);
129 // File
130 if (!path.exists())
132 const bool isSingleFile = (m_ui->previewList->model()->rowCount() == 1);
133 QWidget *parent = isSingleFile ? this->parentWidget() : this;
134 QMessageBox::critical(parent, tr("Preview impossible")
135 , tr("Sorry, we can't preview this file: \"%1\".").arg(path.toString()));
136 if (isSingleFile)
137 reject();
138 return;
141 emit readyToPreviewFile(path);
142 accept();
145 void PreviewSelectDialog::displayColumnHeaderMenu()
147 auto *menu = new QMenu(this);
148 menu->setAttribute(Qt::WA_DeleteOnClose);
149 menu->setToolTipsVisible(true);
151 QAction *resizeAction = menu->addAction(tr("Resize columns"), this, [this]()
153 for (int i = 0, count = m_ui->previewList->header()->count(); i < count; ++i)
155 if (!m_ui->previewList->isColumnHidden(i))
156 m_ui->previewList->resizeColumnToContents(i);
159 resizeAction->setToolTip(tr("Resize all non-hidden columns to the size of their contents"));
161 menu->popup(QCursor::pos());
164 void PreviewSelectDialog::saveWindowState()
166 // Persist dialog size
167 m_storeDialogSize = size();
168 // Persist TreeView Header state
169 m_storeTreeHeaderState = m_ui->previewList->header()->saveState();
172 void PreviewSelectDialog::loadWindowState()
174 // Restore dialog size
175 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
176 resize(dialogSize);
178 // Restore TreeView Header state
179 if (const QByteArray treeHeaderState = m_storeTreeHeaderState; !treeHeaderState.isEmpty())
180 m_headerStateInitialized = m_ui->previewList->header()->restoreState(treeHeaderState);
183 void PreviewSelectDialog::showEvent(QShowEvent *event)
185 // event originated from system
186 if (event->spontaneous())
188 QDialog::showEvent(event);
189 return;
192 // Default size, have to be called after show(), because width is needed
193 // Set Name column width to 60% of TreeView
194 if (!m_headerStateInitialized)
196 const int nameSize = (m_ui->previewList->size().width() * 0.6);
197 m_ui->previewList->header()->resizeSection(0, nameSize);
198 m_headerStateInitialized = true;
201 // Only one file, no choice
202 if (m_ui->previewList->model()->rowCount() <= 1)
203 previewButtonClicked();