DownloadProgressBar.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "DownloadProgressBar.h"
00025 
00026 using UIs::DownloadProgressBar;
00027 
00028 DownloadProgressBar::DownloadProgressBar ()
00029  :  fileSize (0), requestedRanges (new QList<Range>),
00030     downloadedRanges (new QList<Range>), verifiedRanges (new QList<Range>)
00031 {
00032 }
00033 
00034 void DownloadProgressBar::setFileSize (int size)
00035 {
00036     fileSize = size;
00037 }
00038 
00039 void DownloadProgressBar::addRequestedRange (int start, int end)
00040 {
00041     Range newRequestedRange (start, end);
00042     requestedRanges->append (newRequestedRange);
00043 }
00044 
00045 void DownloadProgressBar::addDownloadedRange (int start, int end)
00046 {
00047     Range newDownloadedRange (start, end);
00048     downloadedRanges->append (newDownloadedRange);
00049 }
00050 
00051 void DownloadProgressBar::addVerifiedRange (int start, int end)
00052 {
00053     Range newVerifiedRange (start, end);
00054     verifiedRanges->append (newVerifiedRange);
00055 }
00056 
00057 void DownloadProgressBar::removeRequestedRange (int, int)
00058 {
00059 
00060 }
00061 
00062 void DownloadProgressBar::removeDownloadedRange (int, int)
00063 {
00064 
00065 }
00066 
00067 void DownloadProgressBar::removeVerifiedRange (int, int)
00068 {
00069 
00070 }
00071 
00072 void DownloadProgressBar::paint (QPainter *painter, const QRect *rect, const QStyleOptionViewItem & option)
00073 {
00074 //  QBrush brush_(QColor(100,100,100,100));
00075 //  painter->fillRect (rect->adjusted (2, 2, -2, -2), brush_);
00076 
00077     int height = rect->height();
00078     QBrush brush = painter->brush();
00079     brush.setStyle (Qt::SolidPattern);
00080 
00081     if (option.state & QStyle::State_Selected) {
00082         QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00083                                   ? QPalette::Normal : QPalette::Disabled;
00084         painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
00085         painter->fillRect(option.rect.adjusted (2, 2, -2, -2) , QBrush (QColor (255,255,255)));
00086     }
00087 
00088     brush.setColor (QColor (0,0,0));
00089     QPoint p1 = rect->topLeft() + QPoint (1, 1);
00090     QPoint p2 = rect->topRight() + QPoint (-1, 1);
00091     QPoint p4 = rect->bottomLeft() + QPoint (1, -1);
00092     QPoint p3 = rect->bottomRight() + QPoint (-1, -1);
00093 
00094     painter->drawLine (p1, p2);
00095     painter->drawLine (p2, p3);
00096     painter->drawLine (p3, p4);
00097     painter->drawLine (p4, p1);
00098 
00099 //  brush.setColor (QColor (216, 226, 3));
00100     brush.setColor (QColor (218, 117, 0));
00101     painter->setBrush (brush);
00102     paintRanges (painter, rect->adjusted (2, 2, -2, -(3*height/4 )), requestedRanges);
00103 
00104 //  brush.setColor (QColor (0, 28, 200));
00105     brush.setColor (QColor (47, 53, 81));
00106     painter->setBrush (brush);
00107 //  paintRanges (painter, rect->adjusted (0, height/6+2, 0, -(height/4 + 2)), downloadedRanges);
00108     paintRanges (painter, rect->adjusted (2, 2, -2, -2), downloadedRanges);
00109 
00110     brush.setColor (QColor (37, 223, 5));
00111     painter->setBrush (brush);
00112     paintRanges (painter, rect->adjusted (2, 3*height/4,-2, -2), verifiedRanges);
00113 
00114 }
00115 
00116 void DownloadProgressBar::paintRanges (QPainter *painter, const QRect &rect, QList<Range> *ranges)
00117 {
00118     int width = rect.width();
00119     Range range(0,0);
00120     int start, end;
00121 
00122     for (int i=0; i<ranges->count(); i++){
00123         range = ranges->at(i);
00124         start = range.start()*width/fileSize;
00125         end   = range.end()*width/fileSize;
00126 
00127         QRect fillRect (rect.left() + start, rect.top(), end-start, rect.height());
00128         painter->fillRect (fillRect, painter->brush());
00129     }
00130 }