Skip to content

Commit

Permalink
Merge pull request picotorrent#749 from vktr/f/multiselect
Browse files Browse the repository at this point in the history
Allow multi selection in torrent list
  • Loading branch information
vktr authored Nov 4, 2019
2 parents 1970995 + 6d57bb8 commit bc33ca2
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 28 deletions.
38 changes: 21 additions & 17 deletions src/picotorrent/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ MainWindow::MainWindow(std::shared_ptr<pt::Environment> env, std::shared_ptr<pt:
{
th->remove();
}

m_selectedTorrents.clear();
});

// Session signals
Expand All @@ -146,26 +148,22 @@ MainWindow::MainWindow(std::shared_ptr<pt::Environment> env, std::shared_ptr<pt:
});

QObject::connect(m_session, &Session::torrentAdded,
m_torrentListModel, &TorrentListModel::addTorrent);

QObject::connect(m_session, &Session::torrentAdded,
[this](TorrentHandle*)
[this](TorrentHandle* torrent)
{
m_torrentsCount++;
m_statusBar->updateTorrentCount(m_torrentsCount);
m_torrentListModel->addTorrent(torrent);
});

QObject::connect(m_session, &Session::torrentFinished,
this, &MainWindow::showTorrentFinishedNotification);

QObject::connect(m_session, &Session::torrentRemoved,
m_torrentListModel, &TorrentListModel::removeTorrent);

QObject::connect(m_session, &Session::torrentRemoved,
[this](TorrentHandle*)
[this](TorrentHandle* torrent)
{
m_torrentsCount--;
m_statusBar->updateTorrentCount(m_torrentsCount);
m_torrentListModel->removeTorrent(torrent);
});

QObject::connect(m_session, &Session::torrentStatsUpdated,
Expand All @@ -190,7 +188,7 @@ MainWindow::MainWindow(std::shared_ptr<pt::Environment> env, std::shared_ptr<pt:
{
if (m_selectedTorrents.contains(torrent))
{
m_torrentDetails->update({ torrent });
m_torrentDetails->update(m_selectedTorrents);
}
});

Expand Down Expand Up @@ -223,15 +221,21 @@ MainWindow::MainWindow(std::shared_ptr<pt::Environment> env, std::shared_ptr<pt:
this, &MainWindow::setTorrentFilter);

// Torrent list signals
QObject::connect(m_torrentList, &TorrentListWidget::torrentsSelected,
m_torrentDetails, &TorrentDetailsWidget::update);
QObject::connect(m_torrentList, &TorrentListWidget::torrentsDeselected,
[this](QList<TorrentHandle*> const& torrents)
{
for (auto th : torrents)
{
m_selectedTorrents.removeAll(th);
}
});

QObject::connect(m_torrentList, &TorrentListWidget::torrentsSelected,
[this](QList<TorrentHandle*> const& torrents)
{
m_selectedTorrents.clear();
m_selectedTorrents.append(torrents);
});
QObject::connect(m_torrentList, &TorrentListWidget::torrentsSelected,
[this](QList<TorrentHandle*> const& torrents)
{
m_selectedTorrents.append(torrents);
m_torrentDetails->update(m_selectedTorrents);
});

QObject::connect(m_torrentList, &QTreeView::customContextMenuRequested,
this, &MainWindow::onTorrentContextMenu);
Expand Down
3 changes: 2 additions & 1 deletion src/picotorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,11 @@ void Session::readAlerts()
}

auto handle = m_torrents.at(tra->info_hash);
m_torrents.erase(tra->info_hash);

emit torrentRemoved(handle);

m_torrents.erase(tra->info_hash);

std::vector<std::string> statements =
{
"DELETE FROM torrent_resume_data WHERE info_hash = ?;",
Expand Down
12 changes: 7 additions & 5 deletions src/picotorrent/torrentcontextmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,21 @@ TorrentContextMenu::TorrentContextMenu(QWidget* parent, QList<pt::TorrentHandle*

if (torrents.size() == 1)
{
// TODO
// this->addAction(m_sequentialDownload);
this->addSeparator();
this->addAction(m_move);
}

this->addSeparator();
this->addAction(m_move);
this->addSeparator();
this->addMenu(m_removeMenu);
this->addSeparator();
this->addMenu(m_queueMenu);
this->addSeparator();
this->addAction(m_copyHash);
this->addAction(m_openExplorer);

if (torrents.size() == 1)
{
this->addAction(m_openExplorer);
}

for (TorrentHandle* torrent : torrents)
{
Expand Down
3 changes: 2 additions & 1 deletion src/picotorrent/torrentdetails/torrentdetailswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ void TorrentDetailsWidget::update(QList<pt::TorrentHandle*> const& torrents)
QWidget* currWidget = this->widget(i);
DetailsTab* tab = dynamic_cast<DetailsTab*>(currWidget);

if (torrents.count() > 0)
// For now, the details panel only supports showing information for a single torrent.
if (torrents.count() == 1)
{
tab->refresh(torrents);
}
Expand Down
2 changes: 1 addition & 1 deletion src/picotorrent/torrentdetails/torrentfileswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void TorrentFilesWidget::refresh(QList<pt::TorrentHandle*> const& torrents)

m_filesView->setRootIsDecorated(true);
}
else
else if(ti->num_files() > 0)
{
// If the file name is the same as torrent name, this is a single file
// torrent which downloads directly to the save path. In that case, hide
Expand Down
6 changes: 6 additions & 0 deletions src/picotorrent/torrentitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ TorrentItemDelegate::~TorrentItemDelegate()
delete m_font;
}

void TorrentItemDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
{
QStyledItemDelegate::initStyleOption(option, index);
option->state = option->state & ~QStyle::State_HasFocus;
}

void TorrentItemDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
{
QStyledItemDelegate::paint(painter, option, index);
Expand Down
1 change: 1 addition & 0 deletions src/picotorrent/torrentitemdelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace pt
TorrentItemDelegate(QObject* parent);
virtual ~TorrentItemDelegate();

virtual void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const;
void paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const override;

private:
Expand Down
22 changes: 19 additions & 3 deletions src/picotorrent/torrentlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TorrentListWidget::TorrentListWidget(QWidget* parent, QAbstractItemModel* model,
this->setContextMenuPolicy(Qt::CustomContextMenu);
this->setItemDelegate(new TorrentItemDelegate(this));
this->setRootIsDecorated(false);
this->setSelectionMode(QTreeView::ExtendedSelection);
this->setSortingEnabled(true);

auto header = this->header();
Expand Down Expand Up @@ -176,7 +177,8 @@ void TorrentListWidget::showTorrentExplorer(QModelIndex const& index)

void TorrentListWidget::torrentSelectionChanged(QItemSelection const& selected, QItemSelection const& deselected)
{
QList<TorrentHandle*> torrents;
QList<TorrentHandle*> deselectedTorrents;
QList<TorrentHandle*> selectedTorrents;

for (QModelIndex const& idx : selected.indexes())
{
Expand All @@ -188,10 +190,24 @@ void TorrentListWidget::torrentSelectionChanged(QItemSelection const& selected,
auto variant = this->model()->data(idx, Qt::UserRole);
auto torrent = static_cast<TorrentHandle*>(variant.value<void*>());

torrents.append(torrent);
selectedTorrents.append(torrent);
}

emit torrentsSelected(torrents);
for (QModelIndex const& idx : deselected.indexes())
{
if (idx.column() > 0)
{
continue;
}

auto variant = this->model()->data(idx, Qt::UserRole);
auto torrent = static_cast<TorrentHandle*>(variant.value<void*>());

deselectedTorrents.append(torrent);
}

emit torrentsDeselected(deselectedTorrents);
emit torrentsSelected(selectedTorrents);
}

void TorrentListWidget::toggleColumnVisibility(QAction* action)
Expand Down
1 change: 1 addition & 0 deletions src/picotorrent/torrentlistwidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace pt
QSize sizeHint() const override;

signals:
void torrentsDeselected(QList<TorrentHandle*> torrents);
void torrentsSelected(QList<TorrentHandle*> torrents);

private slots:
Expand Down

0 comments on commit bc33ca2

Please sign in to comment.