2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
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 "previewselectdialog.h"
34 #include <QHeaderView>
36 #include <QMessageBox>
37 #include <QPushButton>
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"
49 #define SETTINGS_KEY(name) u"PreviewSelectDialog/" name
51 PreviewSelectDialog::PreviewSelectDialog(QWidget
*parent
, const BitTorrent::Torrent
*torrent
)
53 , m_ui(new Ui::PreviewSelectDialog
)
55 , m_storeDialogSize(SETTINGS_KEY(u
"Size"_qs
))
56 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
57 , m_storeTreeHeaderState(u
"GUI/Qt6/" SETTINGS_KEY(u
"HeaderState"_qs
))
59 , m_storeTreeHeaderState(SETTINGS_KEY(u
"HeaderState"_qs
))
64 m_ui
->label
->setText(tr("The following files from torrent \"%1\" support previewing, please select one of them:")
65 .arg(m_torrent
->name()));
67 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setText(tr("Preview"));
68 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &PreviewSelectDialog::previewButtonClicked
);
69 connect(m_ui
->buttonBox
, &QDialogButtonBox::rejected
, this, &QDialog::reject
);
70 connect(m_ui
->previewList
, &QAbstractItemView::doubleClicked
, this, &PreviewSelectDialog::previewButtonClicked
);
72 const Preferences
*pref
= Preferences::instance();
74 m_previewListModel
= new QStandardItemModel(0, NB_COLUMNS
, this);
75 m_previewListModel
->setHeaderData(NAME
, Qt::Horizontal
, tr("Name"));
76 m_previewListModel
->setHeaderData(SIZE
, Qt::Horizontal
, tr("Size"));
77 m_previewListModel
->setHeaderData(PROGRESS
, Qt::Horizontal
, tr("Progress"));
79 m_ui
->previewList
->setAlternatingRowColors(pref
->useAlternatingRowColors());
80 m_ui
->previewList
->setModel(m_previewListModel
);
81 m_ui
->previewList
->hideColumn(FILE_INDEX
);
82 m_listDelegate
= new PreviewListDelegate(this);
83 m_ui
->previewList
->setItemDelegate(m_listDelegate
);
85 const QVector
<qreal
> fp
= torrent
->filesProgress();
86 for (int i
= 0; i
< torrent
->filesCount(); ++i
)
88 const Path filePath
= torrent
->filePath(i
);
89 if (Utils::Misc::isPreviewable(filePath
))
91 int row
= m_previewListModel
->rowCount();
92 m_previewListModel
->insertRow(row
);
93 m_previewListModel
->setData(m_previewListModel
->index(row
, NAME
), filePath
.filename());
94 m_previewListModel
->setData(m_previewListModel
->index(row
, SIZE
), torrent
->fileSize(i
));
95 m_previewListModel
->setData(m_previewListModel
->index(row
, PROGRESS
), fp
[i
]);
96 m_previewListModel
->setData(m_previewListModel
->index(row
, FILE_INDEX
), i
);
100 m_previewListModel
->sort(NAME
);
101 m_ui
->previewList
->header()->setContextMenuPolicy(Qt::CustomContextMenu
);
102 m_ui
->previewList
->header()->setFirstSectionMovable(true);
103 m_ui
->previewList
->header()->setSortIndicator(0, Qt::AscendingOrder
);
104 m_ui
->previewList
->selectionModel()->select(m_previewListModel
->index(0, NAME
), QItemSelectionModel::Select
| QItemSelectionModel::Rows
);
106 connect(m_ui
->previewList
->header(), &QWidget::customContextMenuRequested
, this, &PreviewSelectDialog::displayColumnHeaderMenu
);
108 // Restore dialog state
112 PreviewSelectDialog::~PreviewSelectDialog()
119 void PreviewSelectDialog::previewButtonClicked()
121 const QModelIndexList selectedIndexes
= m_ui
->previewList
->selectionModel()->selectedRows(FILE_INDEX
);
122 if (selectedIndexes
.isEmpty()) return;
125 m_torrent
->flushCache();
127 // Only one file should be selected
128 const int fileIndex
= selectedIndexes
.at(0).data().toInt();
129 const Path path
= m_torrent
->actualStorageLocation() / m_torrent
->actualFilePath(fileIndex
);
133 const bool isSingleFile
= (m_previewListModel
->rowCount() == 1);
134 QWidget
*parent
= isSingleFile
? this->parentWidget() : this;
135 QMessageBox::critical(parent
, tr("Preview impossible")
136 , tr("Sorry, we can't preview this file: \"%1\".").arg(path
.toString()));
142 emit
readyToPreviewFile(path
);
146 void PreviewSelectDialog::displayColumnHeaderMenu()
148 auto menu
= new QMenu(this);
149 menu
->setAttribute(Qt::WA_DeleteOnClose
);
150 menu
->setToolTipsVisible(true);
152 QAction
*resizeAction
= menu
->addAction(tr("Resize columns"), this, [this]()
154 for (int i
= 0, count
= m_ui
->previewList
->header()->count(); i
< count
; ++i
)
156 if (!m_ui
->previewList
->isColumnHidden(i
))
157 m_ui
->previewList
->resizeColumnToContents(i
);
160 resizeAction
->setToolTip(tr("Resize all non-hidden columns to the size of their contents"));
162 menu
->popup(QCursor::pos());
165 void PreviewSelectDialog::saveWindowState()
167 // Persist dialog size
168 m_storeDialogSize
= size();
169 // Persist TreeView Header state
170 m_storeTreeHeaderState
= m_ui
->previewList
->header()->saveState();
173 void PreviewSelectDialog::loadWindowState()
175 // Restore dialog size
176 if (const QSize dialogSize
= m_storeDialogSize
; dialogSize
.isValid())
179 // Restore TreeView Header state
180 if (const QByteArray treeHeaderState
= m_storeTreeHeaderState
; !treeHeaderState
.isEmpty())
181 m_headerStateInitialized
= m_ui
->previewList
->header()->restoreState(treeHeaderState
);
184 void PreviewSelectDialog::showEvent(QShowEvent
*event
)
186 // event originated from system
187 if (event
->spontaneous())
189 QDialog::showEvent(event
);
193 // Default size, have to be called after show(), because width is needed
194 // Set Name column width to 60% of TreeView
195 if (!m_headerStateInitialized
)
197 const int nameSize
= (m_ui
->previewList
->size().width() * 0.6);
198 m_ui
->previewList
->header()->resizeSection(0, nameSize
);
199 m_headerStateInitialized
= true;
202 // Only one file, no choice
203 if (m_previewListModel
->rowCount() <= 1)
204 previewButtonClicked();