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  setSortingEnabled(false);
42 
43  insertRow(rowCount());
44  int rowIndex = rowCount() - 1;
45 
46  auto pBar = new QProgressBar();
47  pBar->setAlignment(Qt::AlignCenter);
48  pBar->setMinimum(0);
49  pBar->setMaximum(0);
50 
51  QPushButton *abortButton = new QPushButton(tr("Abort"));
52 
53  connect (abortButton, &QPushButton::clicked, [this, url]()
54  {
55  this->requestUrlAbort(url.toString());
56  });
57 
58  setItem(rowIndex, IDX_URL_COLUMN, new QTableWidgetItem(url.toString()));
59  setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
60  setCellWidget(rowIndex, IDX_ABORT_COLUMN, abortButton);
61 
62  setSortingEnabled(true);
63  }
64 }
65 
66 int WadseekerSitesTable::findRow(const QString &text)
67 {
68  QList<QTableWidgetItem *> list = findItems(text, Qt::MatchFixedString);
69  if (!list.isEmpty())
70  return list.first()->row();
71 
72  return -1;
73 }
74 
75 int WadseekerSitesTable::findRow(const QUrl &url)
76 {
77  return findRow(url.toString());
78 }
79 
80 
81 void WadseekerSitesTable::removeUrl(const QUrl &url)
82 {
83  int row = findRow(url);
84  if (row >= 0)
85  removeRow(row);
86 }
87 
88 void WadseekerSitesTable::addService(const QString &service)
89 {
90  if (findRow(service) < 0)
91  {
92  setSortingEnabled(false);
93 
94  insertRow(rowCount());
95  int rowIndex = rowCount() - 1;
96 
97  auto pBar = new QProgressBar();
98  pBar->setAlignment(Qt::AlignCenter);
99  pBar->setMinimum(0);
100  pBar->setMaximum(0);
101 
102  QPushButton *abortButton = new QPushButton(tr("Abort"));
103 
104  connect (abortButton, &QPushButton::clicked, [this, service]()
105  {
106  this->serviceAbortRequested(service);
107  });
108 
109  setItem(rowIndex, IDX_URL_COLUMN, new QTableWidgetItem(service));
110  setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
111  setCellWidget(rowIndex, IDX_ABORT_COLUMN, abortButton);
112 
113  setSortingEnabled(true);
114  }
115 }
116 
117 void WadseekerSitesTable::removeService(const QString &service)
118 {
119  int row = findRow(service);
120  if (row >= 0)
121  removeRow(row);
122 }
123 
124 void WadseekerSitesTable::requestUrlAbort(const QString &urlAsString)
125 {
126  emit urlAbortRequested(urlAsString);
127 }
128 
129 void WadseekerSitesTable::setUrlProgress(const QUrl &url, qint64 current, qint64 total)
130 {
131  int row = findRow(url);
132 
133  if (row >= 0)
134  {
135  auto pBar = (QProgressBar *) this->cellWidget(row, IDX_PROGRESS_COLUMN);
136  pBar->setMaximum(total);
137  pBar->setValue(current);
138  }
139 }
140 
141 void WadseekerSitesTable::showEvent(QShowEvent *pEvent)
142 {
143  Q_UNUSED(pEvent);
144  if (!d.bAlreadyShownOnce)
145  {
146  // Events in this block must occur after the widget has been
147  // constructed, but only once.
148  QHeaderView *pHeader = horizontalHeader();
149 
150  // Setup resizing
151  pHeader->setSectionResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
152 
153  pHeader->resizeSection(IDX_PROGRESS_COLUMN, 85);
154  }
155 }