Skip to content

Commit 72c73b0

Browse files
nishanthkarthiktrollixx
authored andcommitted
fix bugs and update coding style
1 parent c9635c5 commit 72c73b0

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

src/libs/ui/docsetsdialog.cpp

+17-12
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ void DocsetsDialog::downloadSelectedDocsets()
237237

238238
QAbstractItemModel *model = ui->availableDocsetList->model();
239239
model->setData(index, tr("Downloading: %p%"), ProgressItemDelegate::FormatRole);
240+
model->setData(index, true, ProgressItemDelegate::CancellableRole);
240241
model->setData(index, 0, ProgressItemDelegate::ValueRole);
241242
model->setData(index, true, ProgressItemDelegate::ShowProgressRole);
242243

@@ -392,6 +393,7 @@ void DocsetsDialog::downloadCompleted()
392393
QListWidgetItem *item = findDocsetListItem(docsetName);
393394
if (item) {
394395
item->setData(ProgressItemDelegate::ValueRole, 0);
396+
item->setData(ProgressItemDelegate::CancellableRole, false);
395397
item->setData(ProgressItemDelegate::FormatRole, tr("Installing: %p%"));
396398
}
397399

@@ -573,9 +575,9 @@ void DocsetsDialog::setupAvailableDocsetsTab()
573575
{
574576
using Registry::DocsetRegistry;
575577

576-
ui->availableDocsetList->setItemDelegate(new ProgressItemDelegate(this));
577-
ProgressItemDelegate* del = static_cast<ProgressItemDelegate*>(ui->availableDocsetList->itemDelegate());
578-
connect(del, &ProgressItemDelegate::cancelButtonClicked, this, &DocsetsDialog::cancelDownload);
578+
ProgressItemDelegate *delegate = new ProgressItemDelegate(this);
579+
connect(delegate, &ProgressItemDelegate::cancelButtonClicked, this, &DocsetsDialog::cancelDownload);
580+
ui->availableDocsetList->setItemDelegate(delegate);
579581

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

611613
QAbstractItemModel *model = ui->availableDocsetList->model();
612614
model->setData(index, tr("Downloading: %p%"), ProgressItemDelegate::FormatRole);
615+
model->setData(index, true, ProgressItemDelegate::CancellableRole);
613616
model->setData(index, 0, ProgressItemDelegate::ValueRole);
614617
model->setData(index, true, ProgressItemDelegate::ShowProgressRole);
615618

@@ -710,16 +713,18 @@ void DocsetsDialog::cancelDownload(const QModelIndex &index)
710713
{
711714
// Find and delete download jobs corresponding to index
712715
for (QNetworkReply *reply : m_replies) {
713-
if (reply->property(ListItemIndexProperty).toInt() == index.row()
714-
&& reply->property(DownloadTypeProperty).toInt() == DownloadDocset) {
715-
QListWidgetItem *listItem = ui->availableDocsetList->item(index.row());
716-
listItem->setData(ProgressItemDelegate::ShowProgressRole, false);
717-
delete m_tmpFiles.take(reply->property(DocsetNameProperty).toString());
718-
reply->abort();
719-
720-
m_combinedReceived -= reply->property(DownloadPreviousReceived).toLongLong();
721-
m_combinedTotal -= reply->property(DownloadTotalSize).toLongLong();
716+
if (reply->property(ListItemIndexProperty).toInt() != index.row()
717+
|| reply->property(DownloadTypeProperty).toInt() != DownloadDocset) {
718+
continue;
722719
}
720+
721+
QListWidgetItem *listItem = ui->availableDocsetList->item(index.row());
722+
listItem->setData(ProgressItemDelegate::ShowProgressRole, false);
723+
delete m_tmpFiles.take(reply->property(DocsetNameProperty).toString());
724+
reply->abort();
725+
726+
m_combinedReceived -= reply->property(DownloadPreviousReceived).toLongLong();
727+
m_combinedTotal -= reply->property(DownloadTotalSize).toLongLong();
723728
}
724729

725730
// As the current download is cancelled, unselect the current selected item

src/libs/ui/progressitemdelegate.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
#include "progressitemdelegate.h"
2525

26-
#include <QPainter>
2726
#include <QEvent>
27+
#include <QMouseEvent>
28+
#include <QPainter>
2829
#include <QProgressBar>
2930
#include <QPushButton>
30-
#include <QMouseEvent>
3131

3232
using namespace Zeal::WidgetUi;
3333

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

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

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

7576
// Button
76-
QScopedPointer<QPushButton> buttonRenderer(new QPushButton(tr("Cancel")));
77-
buttonRenderer->resize(cancelButtonWidth, styleOption.rect.height());
78-
79-
painter->translate(progressBarWidth, 0);
80-
buttonRenderer->render(painter);
77+
if (isCancellable) {
78+
QScopedPointer<QPushButton> buttonRenderer(new QPushButton(tr("Cancel")));
79+
buttonRenderer->resize(cancelButtonWidth, styleOption.rect.height());
80+
painter->translate(progressBarWidth, 0);
81+
buttonRenderer->render(painter);
82+
}
8183
painter->restore();
8284

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

9395
if (event->type() == QEvent::MouseButtonRelease
9496
&& index.model()->data(index, ShowProgressRole).toBool()
95-
&& cancelBounds.contains(mouseEvent->pos()))
97+
&& cancelBounds.contains(mouseEvent->pos())) {
9698
emit cancelButtonClicked(index);
99+
}
97100

98101
return QStyledItemDelegate::editorEvent(event, model, option, index);
99102
}

src/libs/ui/progressitemdelegate.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ class ProgressItemDelegate : public QStyledItemDelegate
3636
enum ProgressRoles {
3737
ValueRole = Qt::UserRole + 10,
3838
FormatRole,
39-
ShowProgressRole
39+
ShowProgressRole,
40+
CancellableRole
4041
};
4142

4243
explicit ProgressItemDelegate(QObject *parent = nullptr);
4344

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

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

0 commit comments

Comments
 (0)