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