qBittorrent
PreviewSelectDialog Class Referencefinal

#include <previewselectdialog.h>

Inheritance diagram for PreviewSelectDialog:
Collaboration diagram for PreviewSelectDialog:

Public Types

enum  PreviewColumn {
  NAME , SIZE , PROGRESS , FILE_INDEX ,
  NB_COLUMNS
}
 

Signals

void readyToPreviewFile (QString) const
 

Public Member Functions

 PreviewSelectDialog (QWidget *parent, const BitTorrent::Torrent *torrent)
 
 ~PreviewSelectDialog ()
 

Private Slots

void previewButtonClicked ()
 

Private Member Functions

void showEvent (QShowEvent *event) override
 
void loadWindowState ()
 
void saveWindowState ()
 

Private Attributes

Ui::PreviewSelectDialog * m_ui
 
QStandardItemModel * m_previewListModel
 
PreviewListDelegatem_listDelegate
 
const BitTorrent::Torrentm_torrent
 
bool m_headerStateInitialized = false
 
SettingValue< QSize > m_storeDialogSize
 
SettingValue< QByteArray > m_storeTreeHeaderState
 

Detailed Description

Definition at line 47 of file previewselectdialog.h.

Member Enumeration Documentation

◆ PreviewColumn

Enumerator
NAME 
SIZE 
PROGRESS 
FILE_INDEX 
NB_COLUMNS 

Definition at line 53 of file previewselectdialog.h.

Constructor & Destructor Documentation

◆ PreviewSelectDialog()

PreviewSelectDialog::PreviewSelectDialog ( QWidget *  parent,
const BitTorrent::Torrent torrent 
)

Definition at line 50 of file previewselectdialog.cpp.

51  : QDialog(parent)
52  , m_ui(new Ui::PreviewSelectDialog)
53  , m_torrent(torrent)
55 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
56  , m_storeTreeHeaderState("GUI/Qt6/" SETTINGS_KEY("HeaderState"))
57 #else
58  , m_storeTreeHeaderState(SETTINGS_KEY("HeaderState"))
59 #endif
60 {
61  m_ui->setupUi(this);
62 
63  m_ui->label->setText(tr("The following files from torrent \"%1\" support previewing, please select one of them:")
64  .arg(m_torrent->name()));
65 
66  m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Preview"));
67  connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &PreviewSelectDialog::previewButtonClicked);
68  connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
69  connect(m_ui->previewList, &QAbstractItemView::doubleClicked, this, &PreviewSelectDialog::previewButtonClicked);
70 
71  const Preferences *pref = Preferences::instance();
72  // Preview list
73  m_previewListModel = new QStandardItemModel(0, NB_COLUMNS, this);
74  m_previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
75  m_previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
76  m_previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
77 
78  // This hack fixes reordering of first column with Qt5.
79  // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
80  QTableView unused;
81  unused.setVerticalHeader(m_ui->previewList->header());
82  m_ui->previewList->header()->setParent(m_ui->previewList);
83  unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
84 
85  m_ui->previewList->setModel(m_previewListModel);
86  m_ui->previewList->hideColumn(FILE_INDEX);
88  m_ui->previewList->setItemDelegate(m_listDelegate);
89  m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
90  // Fill list in
91  const QVector<qreal> fp = torrent->filesProgress();
92  for (int i = 0; i < torrent->filesCount(); ++i)
93  {
94  const QString fileName = Utils::Fs::fileName(torrent->filePath(i));
96  {
97  int row = m_previewListModel->rowCount();
98  m_previewListModel->insertRow(row);
99  m_previewListModel->setData(m_previewListModel->index(row, NAME), fileName);
100  m_previewListModel->setData(m_previewListModel->index(row, SIZE), torrent->fileSize(i));
101  m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), fp[i]);
102  m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), i);
103  }
104  }
105 
106  m_previewListModel->sort(NAME);
107  m_ui->previewList->header()->setSortIndicator(0, Qt::AscendingOrder);
108  m_ui->previewList->selectionModel()->select(m_previewListModel->index(0, NAME), QItemSelectionModel::Select | QItemSelectionModel::Rows);
109 
110  // Restore dialog state
111  loadWindowState();
112 }
virtual int filesCount() const =0
virtual qlonglong fileSize(int index) const =0
virtual QString filePath(int index) const =0
virtual QVector< qreal > filesProgress() const =0
virtual QString name() const =0
static Preferences * instance()
bool useAlternatingRowColors() const
SettingValue< QByteArray > m_storeTreeHeaderState
PreviewListDelegate * m_listDelegate
QStandardItemModel * m_previewListModel
SettingValue< QSize > m_storeDialogSize
Ui::PreviewSelectDialog * m_ui
const BitTorrent::Torrent * m_torrent
QString fileName(const QString &filePath)
Definition: fs.cpp:87
bool isPreviewable(const QString &filename)
Definition: misc.cpp:295
#define SETTINGS_KEY(name)

References FILE_INDEX, Utils::Fs::fileName(), BitTorrent::AbstractFileStorage::filePath(), BitTorrent::AbstractFileStorage::filesCount(), BitTorrent::AbstractFileStorage::fileSize(), BitTorrent::Torrent::filesProgress(), Preferences::instance(), Utils::Misc::isPreviewable(), loadWindowState(), m_listDelegate, m_previewListModel, m_torrent, m_ui, BitTorrent::Torrent::name(), NAME, NB_COLUMNS, previewButtonClicked(), PROGRESS, SIZE, and Preferences::useAlternatingRowColors().

Here is the call graph for this function:

◆ ~PreviewSelectDialog()

PreviewSelectDialog::~PreviewSelectDialog ( )

Definition at line 114 of file previewselectdialog.cpp.

115 {
116  saveWindowState();
117 
118  delete m_ui;
119 }

References m_ui, and saveWindowState().

Here is the call graph for this function:

Member Function Documentation

◆ loadWindowState()

void PreviewSelectDialog::loadWindowState ( )
private

Definition at line 156 of file previewselectdialog.cpp.

157 {
158  // Restore dialog size
160 
161  // Restore TreeView Header state
162  if (!m_storeTreeHeaderState.get().isEmpty())
163  {
164  m_headerStateInitialized = m_ui->previewList->header()->restoreState(m_storeTreeHeaderState);
165  }
166 }
T get(const T &defaultValue={}) const
Definition: settingvalue.h:46
void resize(QWidget *widget, const QSize &newSize={})
Definition: utils.cpp:54

References SettingValue< T >::get(), m_headerStateInitialized, m_storeDialogSize, m_storeTreeHeaderState, m_ui, and Utils::Gui::resize().

Referenced by PreviewSelectDialog().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ previewButtonClicked

void PreviewSelectDialog::previewButtonClicked ( )
privateslot

Definition at line 121 of file previewselectdialog.cpp.

122 {
123  const QModelIndexList selectedIndexes = m_ui->previewList->selectionModel()->selectedRows(FILE_INDEX);
124  if (selectedIndexes.isEmpty()) return;
125 
126  // Flush data
128 
129  // Only one file should be selected
130  const int fileIndex = selectedIndexes.at(0).data().toInt();
131  const QString path = QDir(m_torrent->actualStorageLocation()).absoluteFilePath(m_torrent->actualFilePath(fileIndex));
132  // File
133  if (!QFile::exists(path))
134  {
135  const bool isSingleFile = (m_previewListModel->rowCount() == 1);
136  QWidget *parent = isSingleFile ? this->parentWidget() : this;
137  QMessageBox::critical(parent, tr("Preview impossible")
138  , tr("Sorry, we can't preview this file: \"%1\".").arg(Utils::Fs::toNativePath(path)));
139  if (isSingleFile)
140  reject();
141  return;
142  }
143 
144  emit readyToPreviewFile(path);
145  accept();
146 }
virtual void flushCache() const =0
virtual QString actualStorageLocation() const =0
virtual QString actualFilePath(int index) const =0
void readyToPreviewFile(QString) const
QString toNativePath(const QString &path)
Definition: fs.cpp:64

References BitTorrent::Torrent::actualFilePath(), BitTorrent::Torrent::actualStorageLocation(), FILE_INDEX, BitTorrent::Torrent::flushCache(), m_previewListModel, m_torrent, m_ui, readyToPreviewFile(), and Utils::Fs::toNativePath().

Referenced by PreviewSelectDialog(), and showEvent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readyToPreviewFile

void PreviewSelectDialog::readyToPreviewFile ( QString  ) const
signal

◆ saveWindowState()

void PreviewSelectDialog::saveWindowState ( )
private

Definition at line 148 of file previewselectdialog.cpp.

149 {
150  // Persist dialog size
151  m_storeDialogSize = size();
152  // Persist TreeView Header state
153  m_storeTreeHeaderState = m_ui->previewList->header()->saveState();
154 }

References m_storeDialogSize, m_storeTreeHeaderState, and m_ui.

Referenced by ~PreviewSelectDialog().

Here is the caller graph for this function:

◆ showEvent()

void PreviewSelectDialog::showEvent ( QShowEvent *  event)
overrideprivate

Definition at line 168 of file previewselectdialog.cpp.

169 {
170  // event originated from system
171  if (event->spontaneous())
172  {
173  QDialog::showEvent(event);
174  return;
175  }
176 
177  // Default size, have to be called after show(), because width is needed
178  // Set Name column width to 60% of TreeView
180  {
181  const int nameSize = (m_ui->previewList->size().width() * 0.6);
182  m_ui->previewList->header()->resizeSection(0, nameSize);
184  }
185 
186  // Only one file, no choice
187  if (m_previewListModel->rowCount() <= 1)
189 }

References m_headerStateInitialized, m_previewListModel, m_ui, and previewButtonClicked().

Here is the call graph for this function:

Member Data Documentation

◆ m_headerStateInitialized

bool PreviewSelectDialog::m_headerStateInitialized = false
private

Definition at line 82 of file previewselectdialog.h.

Referenced by loadWindowState(), and showEvent().

◆ m_listDelegate

PreviewListDelegate* PreviewSelectDialog::m_listDelegate
private

Definition at line 80 of file previewselectdialog.h.

Referenced by PreviewSelectDialog().

◆ m_previewListModel

QStandardItemModel* PreviewSelectDialog::m_previewListModel
private

Definition at line 79 of file previewselectdialog.h.

Referenced by previewButtonClicked(), PreviewSelectDialog(), and showEvent().

◆ m_storeDialogSize

SettingValue<QSize> PreviewSelectDialog::m_storeDialogSize
private

Definition at line 85 of file previewselectdialog.h.

Referenced by loadWindowState(), and saveWindowState().

◆ m_storeTreeHeaderState

SettingValue<QByteArray> PreviewSelectDialog::m_storeTreeHeaderState
private

Definition at line 86 of file previewselectdialog.h.

Referenced by loadWindowState(), and saveWindowState().

◆ m_torrent

const BitTorrent::Torrent* PreviewSelectDialog::m_torrent
private

Definition at line 81 of file previewselectdialog.h.

Referenced by previewButtonClicked(), and PreviewSelectDialog().

◆ m_ui

Ui::PreviewSelectDialog* PreviewSelectDialog::m_ui
private

The documentation for this class was generated from the following files: