Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / previewselectdialog.cpp
blob73f326edb75374d3b1fa8c80020d8715eb3e351b
1 /*
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"
31 #include <QFile>
32 #include <QHeaderView>
33 #include <QMessageBox>
34 #include <QPushButton>
35 #include <QShowEvent>
36 #include <QStandardItemModel>
37 #include <QTableView>
39 #include "base/bittorrent/common.h"
40 #include "base/bittorrent/torrent.h"
41 #include "base/preferences.h"
42 #include "base/utils/fs.h"
43 #include "base/utils/misc.h"
44 #include "previewlistdelegate.h"
45 #include "ui_previewselectdialog.h"
46 #include "utils.h"
48 #define SETTINGS_KEY(name) "PreviewSelectDialog/" name
50 PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torrent *torrent)
51 : QDialog(parent)
52 , m_ui(new Ui::PreviewSelectDialog)
53 , m_torrent(torrent)
54 , m_storeDialogSize(SETTINGS_KEY("Size"))
55 , m_storeTreeHeaderState(SETTINGS_KEY("HeaderState"))
57 m_ui->setupUi(this);
59 m_ui->label->setText(tr("The following files from torrent \"%1\" support previewing, please select one of them:")
60 .arg(m_torrent->name()));
62 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Preview"));
63 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &PreviewSelectDialog::previewButtonClicked);
64 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
65 connect(m_ui->previewList, &QAbstractItemView::doubleClicked, this, &PreviewSelectDialog::previewButtonClicked);
67 const Preferences *pref = Preferences::instance();
68 // Preview list
69 m_previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
70 m_previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
71 m_previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
72 m_previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
74 // This hack fixes reordering of first column with Qt5.
75 // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
76 QTableView unused;
77 unused.setVerticalHeader(m_ui->previewList->header());
78 m_ui->previewList->header()->setParent(m_ui->previewList);
79 unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
81 m_ui->previewList->setModel(m_previewListModel);
82 m_ui->previewList->hideColumn(FILE_INDEX);
83 m_listDelegate = new PreviewListDelegate(this);
84 m_ui->previewList->setItemDelegate(m_listDelegate);
85 m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
86 // Fill list in
87 const QVector<qreal> fp = torrent->filesProgress();
88 int nbFiles = torrent->filesCount();
89 for (int i = 0; i < nbFiles; ++i)
91 QString fileName = torrent->fileName(i);
92 if (fileName.endsWith(QB_EXT))
93 fileName.chop(QB_EXT.length());
94 if (Utils::Misc::isPreviewable(fileName))
96 int row = m_previewListModel->rowCount();
97 m_previewListModel->insertRow(row);
98 m_previewListModel->setData(m_previewListModel->index(row, NAME), fileName);
99 m_previewListModel->setData(m_previewListModel->index(row, SIZE), torrent->fileSize(i));
100 m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), fp[i]);
101 m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), i);
105 m_previewListModel->sort(NAME);
106 m_ui->previewList->header()->setSortIndicator(0, Qt::AscendingOrder);
107 m_ui->previewList->selectionModel()->select(m_previewListModel->index(0, NAME), QItemSelectionModel::Select | QItemSelectionModel::Rows);
109 // Restore dialog state
110 loadWindowState();
113 PreviewSelectDialog::~PreviewSelectDialog()
115 saveWindowState();
117 delete m_ui;
120 void PreviewSelectDialog::previewButtonClicked()
122 const QModelIndexList selectedIndexes = m_ui->previewList->selectionModel()->selectedRows(FILE_INDEX);
123 if (selectedIndexes.isEmpty()) return;
125 // Flush data
126 m_torrent->flushCache();
128 const QStringList absolutePaths = m_torrent->absoluteFilePaths();
129 // Only one file should be selected
130 const QString path = absolutePaths.at(selectedIndexes.at(0).data().toInt());
131 // File
132 if (!QFile::exists(path))
134 const bool isSingleFile = (m_previewListModel->rowCount() == 1);
135 QWidget *parent = isSingleFile ? this->parentWidget() : this;
136 QMessageBox::critical(parent, tr("Preview impossible")
137 , tr("Sorry, we can't preview this file: \"%1\".").arg(Utils::Fs::toNativePath(path)));
138 if (isSingleFile)
139 reject();
140 return;
143 emit readyToPreviewFile(path);
144 accept();
147 void PreviewSelectDialog::saveWindowState()
149 // Persist dialog size
150 m_storeDialogSize = size();
151 // Persist TreeView Header state
152 m_storeTreeHeaderState = m_ui->previewList->header()->saveState();
155 void PreviewSelectDialog::loadWindowState()
157 // Restore dialog size
158 Utils::Gui::resize(this, m_storeDialogSize);
160 // Restore TreeView Header state
161 if (!m_storeTreeHeaderState.get().isEmpty())
163 m_headerStateInitialized = m_ui->previewList->header()->restoreState(m_storeTreeHeaderState);
167 void PreviewSelectDialog::showEvent(QShowEvent *event)
169 // event originated from system
170 if (event->spontaneous())
172 QDialog::showEvent(event);
173 return;
176 // Default size, have to be called after show(), because width is needed
177 // Set Name column width to 60% of TreeView
178 if (!m_headerStateInitialized)
180 const int nameSize = (m_ui->previewList->size().width() * 0.6);
181 m_ui->previewList->header()->resizeSection(0, nameSize);
182 m_headerStateInitialized = true;
185 // Only one file, no choice
186 if (m_previewListModel->rowCount() <= 1)
187 previewButtonClicked();