Skip to content

Commit

Permalink
Merge pull request #996 from vktr/feature/piece-progress
Browse files Browse the repository at this point in the history
Add piece progress bar
  • Loading branch information
vktr committed Oct 25, 2020
2 parents 129868e + 2cae478 commit 806b2ed
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ add_executable(
src/picotorrent/ui/torrentlistview
src/picotorrent/ui/translator

# Widgets
src/picotorrent/ui/widgets/pieceprogressbar

# Win32 specific stuff
src/picotorrent/ui/win32/openfiledialog
)
Expand Down
60 changes: 30 additions & 30 deletions src/picotorrent/bittorrent/torrentstatus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,36 @@ namespace BitTorrent
UploadingQueued
};

wxDateTime addedOn;
std::int64_t allTimeDownload;
std::int64_t allTimeUpload;
float availability;
wxDateTime completedOn;
int downloadPayloadRate;
bool forced;
std::string error;
std::string errorDetails;
std::chrono::seconds eta;
std::string infoHash;
std::string labelName;
std::chrono::seconds lastDownload;
std::chrono::seconds lastUpload;
std::string name;
bool paused;
int peersCurrent;
int peersTotal;
libtorrent::bitfield pieces;
float progress;
int queuePosition;
float ratio;
std::string savePath;
int seedsCurrent;
int seedsTotal;
State state;
std::weak_ptr<const libtorrent::torrent_info> torrentFile;
std::int64_t totalWanted;
std::int64_t totalWantedRemaining;
int uploadPayloadRate;
wxDateTime addedOn;
std::int64_t allTimeDownload;
std::int64_t allTimeUpload;
float availability;
wxDateTime completedOn;
int downloadPayloadRate;
bool forced;
std::string error;
std::string errorDetails;
std::chrono::seconds eta;
std::string infoHash;
std::string labelName;
std::chrono::seconds lastDownload;
std::chrono::seconds lastUpload;
std::string name;
bool paused;
int peersCurrent;
int peersTotal;
libtorrent::typed_bitfield<libtorrent::piece_index_t> pieces;
float progress;
int queuePosition;
float ratio;
std::string savePath;
int seedsCurrent;
int seedsTotal;
State state;
std::weak_ptr<const libtorrent::torrent_info> torrentFile;
std::int64_t totalWanted;
std::int64_t totalWantedRemaining;
int uploadPayloadRate;
};
}
}
9 changes: 9 additions & 0 deletions src/picotorrent/ui/torrentdetailsoverviewpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

#include <fmt/format.h>
#include <wx/clipbrd.h>
#include <wx/dcbuffer.h>
#include <wx/sizer.h>

#include <libtorrent/torrent_status.hpp>

#include "../bittorrent/torrenthandle.hpp"
#include "../bittorrent/torrentstatus.hpp"
#include "../core/utils.hpp"
#include "translator.hpp"
#include "widgets/pieceprogressbar.hpp"

using pt::UI::TorrentDetailsOverviewPanel;

Expand Down Expand Up @@ -81,6 +85,7 @@ class CopyableStaticText : public wxStaticText

TorrentDetailsOverviewPanel::TorrentDetailsOverviewPanel(wxWindow* parent, wxWindowID id)
: wxScrolledWindow(parent, id),
m_pieceProgress(new Widgets::PieceProgressBar(this, wxID_ANY)),
m_name(new CopyableStaticText(this)),
m_infoHash(new CopyableStaticText(this)),
m_savePath(new CopyableStaticText(this)),
Expand Down Expand Up @@ -129,6 +134,7 @@ TorrentDetailsOverviewPanel::TorrentDetailsOverviewPanel(wxWindow* parent, wxWin
sizer->Add(m_totalUpload, 1, wxEXPAND);

auto mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(m_pieceProgress, 0, wxEXPAND | wxTOP | wxRIGHT | wxLEFT, FromDIP(5));
mainSizer->Add(sizer, 1, wxALL | wxEXPAND, FromDIP(5));

this->SetSizer(mainSizer);
Expand All @@ -140,6 +146,8 @@ void TorrentDetailsOverviewPanel::Refresh(BitTorrent::TorrentHandle* torrent)
{
auto status = torrent->Status();

m_pieceProgress->UpdateBitfield(status.pieces);

m_name->SetLabel(Utils::toStdWString(status.name));
m_savePath->SetLabel(Utils::toStdWString(status.savePath));
m_infoHash->SetLabel(status.infoHash);
Expand Down Expand Up @@ -203,6 +211,7 @@ void TorrentDetailsOverviewPanel::Refresh(BitTorrent::TorrentHandle* torrent)

void TorrentDetailsOverviewPanel::Reset()
{
m_pieceProgress->UpdateBitfield({});
m_name->SetLabel("-");
m_infoHash->SetLabel("-");
m_savePath->SetLabel("-");
Expand Down
3 changes: 3 additions & 0 deletions src/picotorrent/ui/torrentdetailsoverviewpanel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <wx/wx.h>
#endif

namespace pt::UI::Widgets { class PieceProgressBar; }

namespace pt
{
namespace BitTorrent
Expand All @@ -22,6 +24,7 @@ namespace UI
void Reset();

private:
Widgets::PieceProgressBar* m_pieceProgress;
wxStaticText* m_name;
wxStaticText* m_infoHash;
wxStaticText* m_savePath;
Expand Down
78 changes: 78 additions & 0 deletions src/picotorrent/ui/widgets/pieceprogressbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "pieceprogressbar.hpp"

#include <wx/dcbuffer.h>

namespace lt = libtorrent;
using pt::UI::Widgets::PieceProgressBar;

PieceProgressBar::PieceProgressBar(wxWindow* parent, wxWindowID id, lt::typed_bitfield<lt::piece_index_t> field)
: wxPanel(parent, id, wxDefaultPosition, wxSize(-1, parent->FromDIP(15)), wxTAB_TRAVERSAL | wxNO_BORDER | wxBG_STYLE_PAINT),
m_bitfield(field)
{
Connect(wxEVT_ERASE_BACKGROUND, wxEraseEventHandler(PieceProgressBar::OnEraseBackground));
Connect(wxEVT_PAINT, wxPaintEventHandler(PieceProgressBar::OnPaint));
Connect(wxEVT_SIZE, wxSizeEventHandler(PieceProgressBar::OnSize));
}

void PieceProgressBar::UpdateBitfield(lt::typed_bitfield<lt::piece_index_t> const& field)
{
m_bitfield = field;
Refresh();
}

void PieceProgressBar::OnEraseBackground(wxEraseEvent&)
{
}

void PieceProgressBar::OnSize(wxSizeEvent&)
{
Refresh();
}

void PieceProgressBar::OnPaint(wxPaintEvent&)
{
wxBufferedPaintDC dc(this);
RenderProgress(dc);
}

void PieceProgressBar::RenderProgress(wxDC& dc)
{
static wxColor bar("#35b1e1");
static wxColor darkBorder(50, 50, 50);

if (m_bitfield.size() > 0)
{
wxBitmap prg(m_bitfield.size() + 2, this->GetClientSize().GetHeight());
wxMemoryDC memDC;

memDC.SelectObject(prg);
memDC.SetBrush(*wxWHITE);
memDC.SetPen(darkBorder);
memDC.DrawRectangle({ 0, 0 }, prg.GetSize());

memDC.SetPen(bar);

for (int idx = 0; idx < m_bitfield.size(); idx++)
{
lt::piece_index_t pcs{ idx };

if (m_bitfield[pcs])
{
memDC.DrawLine(idx + 1, 1, idx + 1, prg.GetHeight() - 1);
}
}

dc.StretchBlit(
{ 0, 0 },
this->GetClientSize(),
&memDC,
{ 0, 0 },
prg.GetSize());
}
else
{
dc.SetBrush(*wxWHITE);
dc.SetPen(wxColor(190, 190, 190));
dc.DrawRectangle(this->GetClientRect());
}
}
29 changes: 29 additions & 0 deletions src/picotorrent/ui/widgets/pieceprogressbar.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <libtorrent/bitfield.hpp>
#include <libtorrent/fwd.hpp>

namespace pt::UI::Widgets
{
class PieceProgressBar : public wxPanel
{
public:
PieceProgressBar(wxWindow* parent, wxWindowID id, libtorrent::typed_bitfield<libtorrent::piece_index_t> field = {});
void UpdateBitfield(libtorrent::typed_bitfield<libtorrent::piece_index_t> const& field);

protected:
void OnEraseBackground(wxEraseEvent&);
void OnSize(wxSizeEvent&);
void OnPaint(wxPaintEvent&);

private:
void RenderProgress(wxDC& dc);

libtorrent::typed_bitfield<libtorrent::piece_index_t> m_bitfield;
};
}

0 comments on commit 806b2ed

Please sign in to comment.