wadseekersitestable.cpp
1 //------------------------------------------------------------------------------
2 // wadseekersitestable.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 <QUrl>
28 
29 WadseekerSitesTable::WadseekerSitesTable(QWidget* pParent)
30 : TableWidgetMouseAware(pParent)
31 {
32  d.bAlreadyShownOnce = false;
33 }
34 
35 void WadseekerSitesTable::addUrl(const QUrl& url)
36 {
37  // Add new row to table, but only if URL is not yet added.
38  if (findUrlRow(url) < 0)
39  {
40  insertRow(rowCount());
41  int rowIndex = rowCount() - 1;
42 
43  // Create the row contents.
44  setSortingEnabled(false);
45 
46  QProgressBar* pBar = new QProgressBar();
47  pBar->setAlignment(Qt::AlignCenter);
48  pBar->setMinimum(0);
49  pBar->setMaximum(0);
50 
51  setItem(rowIndex, IDX_URL_COLUMN, new QTableWidgetItem(url.toString()));
52  setCellWidget(rowIndex, IDX_PROGRESS_COLUMN, pBar);
53 
54  setSortingEnabled(true);
55  }
56 }
57 
58 int WadseekerSitesTable::findUrlRow(const QUrl& url)
59 {
60  QList<QTableWidgetItem *> list = findItems(url.toString(), Qt::MatchFixedString);
61  if (!list.isEmpty())
62  {
63  return list.first()->row();
64  }
65 
66  return -1;
67 }
68 
69 void WadseekerSitesTable::removeUrl(const QUrl& url)
70 {
71  int row = findUrlRow(url);
72 
73  if (row >= 0)
74  {
75  this->removeRow(row);
76  }
77 }
78 
79 void WadseekerSitesTable::setUrlProgress(const QUrl& url, qint64 current, qint64 total)
80 {
81  int row = findUrlRow(url);
82 
83  if (row >= 0)
84  {
85  QProgressBar* pBar = (QProgressBar*) this->cellWidget(row, IDX_PROGRESS_COLUMN);
86  pBar->setMaximum(total);
87  pBar->setValue(current);
88  }
89 }
90 
91 void WadseekerSitesTable::showEvent(QShowEvent* pEvent)
92 {
93  if (!d.bAlreadyShownOnce)
94  {
95  // Events in this block must occur after the widget has been
96  // constructed, but only once.
97  QHeaderView* pHeader = horizontalHeader();
98 
99  // Setup resizing
100  pHeader->setResizeMode(IDX_URL_COLUMN, QHeaderView::Stretch);
101 
102  pHeader->resizeSection(IDX_PROGRESS_COLUMN, 85);
103  }
104 }