wadseekersitestable.cpp
1 //------------------------------------------------------------------------------
2 // wadseekersitestable.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "wadseekersitestable.h"
24 
25 #include <QHeaderView>
26 #include <QProgressBar>
27 #include <QPushButton>
28 #include <QUrl>
29 
30 WadseekerSitesTable::WadseekerSitesTable(QWidget *pParent)
31  : TableWidgetMouseAware(pParent)
32 {
33  d.bAlreadyShownOnce = false;
34 }
35 
36 void WadseekerSitesTable::addUrl(const QUrl &url)
37 {
38  // Add new row to table, but only if URL is not yet added.
39  if (findRow(url) < 0)
40  {
41  addSite(url.toString(), [this, url]() {
42  this->requestUrlAbort(url.toString());
43  });
44  }
45 }
46 
47 int WadseekerSitesTable::findRow(const QString &text)
48 {
49  QList<QTableWidgetItem *> list = findItems(text, Qt::MatchFixedString);
50  if (!list.isEmpty())
51  return list.first()->row();
52 
53  return -1;
54 }
55 
56 int WadseekerSitesTable::findRow(const QUrl &url)
57 {
58  return findRow(url.toString());
59 }
60 
61 
62 void WadseekerSitesTable::removeUrl(const QUrl &url)
63 {
64  int row = findRow(url);
65  if (row >= 0)
66  removeRow(row);
67 }
68 
69 void WadseekerSitesTable::addService(const QString &service)
70 {
71  if (findRow(service) < 0)
72  {
73  addSite(service, [this, service]() {
74  this->serviceAbortRequested(service);
75  });
76  }
77 }
78 
79 void WadseekerSitesTable::addSite(const QString &text, std::function<void(void)> onAbort)
80 {
81  setSortingEnabled(false);
82 
83  insertRow(rowCount());
84  int rowIndex = rowCount() - 1;
85 
86  auto pBar = new QProgressBar();
87  pBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
88  pBar->setFixedWidth(PROGRESS_COLUMN_WIDTH);
89  pBar->setAlignment(Qt::AlignCenter);
90  pBar->setMinimum(0);
91  pBar->setMaximum(0);
92 
93  QPushButton *abortButton = new QPushButton(tr("Abort"));
94  connect(abortButton, &QPushButton::clicked, onAbort);
95 
96  setItem(rowIndex, IDX_URL_COLUMN, new QTableWidgetItem(text));
97  setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
98  setCellWidget(rowIndex, IDX_ABORT_COLUMN, abortButton);
99 
100  setSortingEnabled(true);
101 }
102 
103 void WadseekerSitesTable::removeService(const QString &service)
104 {
105  int row = findRow(service);
106  if (row >= 0)
107  removeRow(row);
108 }
109 
110 void WadseekerSitesTable::requestUrlAbort(const QString &urlAsString)
111 {
112  emit urlAbortRequested(urlAsString);
113 }
114 
115 void WadseekerSitesTable::setUrlProgress(const QUrl &url, qint64 current, qint64 total)
116 {
117  int row = findRow(url);
118 
119  if (row >= 0)
120  {
121  auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
122  pBar->setMaximum(total);
123  pBar->setValue(current);
124  }
125 }
126 
127 void WadseekerSitesTable::showEvent(QShowEvent *pEvent)
128 {
129  Q_UNUSED(pEvent);
130  if (!d.bAlreadyShownOnce)
131  {
132  // Events in this block must occur after the widget has been
133  // constructed, but only once.
134  QHeaderView *pHeader = horizontalHeader();
135 
136  // Setup resizing
137  pHeader->setSectionResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
138  pHeader->setSectionResizeMode(IDX_PROGRESS_COLUMN, QHeaderView::Fixed);
139 
140  pHeader->resizeSection(IDX_PROGRESS_COLUMN, PROGRESS_COLUMN_WIDTH);
141  }
142 }