Skip to content

Commit

Permalink
fix bugs and update coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
nishanthkarthik authored and trollixx committed Feb 7, 2020
1 parent c9635c5 commit 72c73b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
29 changes: 17 additions & 12 deletions src/libs/ui/docsetsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ void DocsetsDialog::downloadSelectedDocsets()

QAbstractItemModel *model = ui->availableDocsetList->model();
model->setData(index, tr("Downloading: %p%"), ProgressItemDelegate::FormatRole);
model->setData(index, true, ProgressItemDelegate::CancellableRole);
model->setData(index, 0, ProgressItemDelegate::ValueRole);
model->setData(index, true, ProgressItemDelegate::ShowProgressRole);

Expand Down Expand Up @@ -392,6 +393,7 @@ void DocsetsDialog::downloadCompleted()
QListWidgetItem *item = findDocsetListItem(docsetName);
if (item) {
item->setData(ProgressItemDelegate::ValueRole, 0);
item->setData(ProgressItemDelegate::CancellableRole, false);
item->setData(ProgressItemDelegate::FormatRole, tr("Installing: %p%"));
}

Expand Down Expand Up @@ -573,9 +575,9 @@ void DocsetsDialog::setupAvailableDocsetsTab()
{
using Registry::DocsetRegistry;

ui->availableDocsetList->setItemDelegate(new ProgressItemDelegate(this));
ProgressItemDelegate* del = static_cast<ProgressItemDelegate*>(ui->availableDocsetList->itemDelegate());
connect(del, &ProgressItemDelegate::cancelButtonClicked, this, &DocsetsDialog::cancelDownload);
ProgressItemDelegate *delegate = new ProgressItemDelegate(this);
connect(delegate, &ProgressItemDelegate::cancelButtonClicked, this, &DocsetsDialog::cancelDownload);
ui->availableDocsetList->setItemDelegate(delegate);

connect(m_docsetRegistry, &DocsetRegistry::docsetUnloaded, this, [this](const QString name) {
QListWidgetItem *item = findDocsetListItem(name);
Expand Down Expand Up @@ -610,6 +612,7 @@ void DocsetsDialog::setupAvailableDocsetsTab()

QAbstractItemModel *model = ui->availableDocsetList->model();
model->setData(index, tr("Downloading: %p%"), ProgressItemDelegate::FormatRole);
model->setData(index, true, ProgressItemDelegate::CancellableRole);
model->setData(index, 0, ProgressItemDelegate::ValueRole);
model->setData(index, true, ProgressItemDelegate::ShowProgressRole);

Expand Down Expand Up @@ -710,16 +713,18 @@ void DocsetsDialog::cancelDownload(const QModelIndex &index)
{
// Find and delete download jobs corresponding to index
for (QNetworkReply *reply : m_replies) {
if (reply->property(ListItemIndexProperty).toInt() == index.row()
&& reply->property(DownloadTypeProperty).toInt() == DownloadDocset) {
QListWidgetItem *listItem = ui->availableDocsetList->item(index.row());
listItem->setData(ProgressItemDelegate::ShowProgressRole, false);
delete m_tmpFiles.take(reply->property(DocsetNameProperty).toString());
reply->abort();

m_combinedReceived -= reply->property(DownloadPreviousReceived).toLongLong();
m_combinedTotal -= reply->property(DownloadTotalSize).toLongLong();
if (reply->property(ListItemIndexProperty).toInt() != index.row()
|| reply->property(DownloadTypeProperty).toInt() != DownloadDocset) {
continue;
}

QListWidgetItem *listItem = ui->availableDocsetList->item(index.row());
listItem->setData(ProgressItemDelegate::ShowProgressRole, false);
delete m_tmpFiles.take(reply->property(DocsetNameProperty).toString());
reply->abort();

m_combinedReceived -= reply->property(DownloadPreviousReceived).toLongLong();
m_combinedTotal -= reply->property(DownloadTotalSize).toLongLong();
}

// As the current download is cancelled, unselect the current selected item
Expand Down
21 changes: 12 additions & 9 deletions src/libs/ui/progressitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

#include "progressitemdelegate.h"

#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QProgressBar>
#include <QPushButton>
#include <QMouseEvent>

using namespace Zeal::WidgetUi;

Expand Down Expand Up @@ -63,6 +63,7 @@ void ProgressItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
renderer->setValue(value);

const QString format = index.model()->data(index, FormatRole).toString();
const bool isCancellable = index.model()->data(index, CancellableRole).toBool();
if (!format.isEmpty())
renderer->setFormat(format);

Expand All @@ -73,11 +74,12 @@ void ProgressItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
renderer->render(painter);

// Button
QScopedPointer<QPushButton> buttonRenderer(new QPushButton(tr("Cancel")));
buttonRenderer->resize(cancelButtonWidth, styleOption.rect.height());

painter->translate(progressBarWidth, 0);
buttonRenderer->render(painter);
if (isCancellable) {
QScopedPointer<QPushButton> buttonRenderer(new QPushButton(tr("Cancel")));
buttonRenderer->resize(cancelButtonWidth, styleOption.rect.height());
painter->translate(progressBarWidth, 0);
buttonRenderer->render(painter);
}
painter->restore();

QStyledItemDelegate::paint(painter, styleOption, index);
Expand All @@ -86,14 +88,15 @@ void ProgressItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
bool ProgressItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
QRect cancelBounds = option.rect;
cancelBounds.setLeft(cancelBounds.right() - cancelButtonWidth);

if (event->type() == QEvent::MouseButtonRelease
&& index.model()->data(index, ShowProgressRole).toBool()
&& cancelBounds.contains(mouseEvent->pos()))
&& cancelBounds.contains(mouseEvent->pos())) {
emit cancelButtonClicked(index);
}

return QStyledItemDelegate::editorEvent(event, model, option, index);
}
4 changes: 3 additions & 1 deletion src/libs/ui/progressitemdelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ class ProgressItemDelegate : public QStyledItemDelegate
enum ProgressRoles {
ValueRole = Qt::UserRole + 10,
FormatRole,
ShowProgressRole
ShowProgressRole,
CancellableRole
};

explicit ProgressItemDelegate(QObject *parent = nullptr);

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

protected:
bool editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index) override;

Expand Down

0 comments on commit 72c73b0

Please sign in to comment.