KNewStuff
qprogressindicator.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of KNewStuff2. 00003 Copyright (c) 2007 Josef Spillner <spillner@kde.org> 00004 Copyright (c) 2007 Jeremy Whiting <jpwhiting@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "qprogressindicator.h" 00021 00022 #include <QtGui/QProgressBar> 00023 #include <QtGui/QPushButton> 00024 #include <QtGui/QLabel> 00025 #include <QtGui/QLayout> 00026 00027 #include <kiconloader.h> 00028 00029 QProgressIndicator::QProgressIndicator(QWidget *parent) 00030 : QFrame(parent) 00031 { 00032 setFrameStyle(QFrame::NoFrame); 00033 m_pb = new QProgressBar(); 00034 m_pb->setMinimum(0); 00035 m_pb->setMaximum(100); 00036 00037 m_pbdetails = new QPushButton(); 00038 m_pbdetails->setFixedWidth(32); // FIXME: I want a squared button 00039 m_pbdetails->setIcon(SmallIcon("go-up")); 00040 m_pbdetails->setEnabled(false); 00041 00042 QHBoxLayout *hbox = new QHBoxLayout(this); 00043 hbox->setMargin(0); 00044 hbox->addWidget(m_pbdetails); 00045 hbox->addWidget(m_pb); 00046 00047 m_details = new QWidget(this); 00048 m_details->setWindowFlags(Qt::Popup); 00049 m_details->hide(); 00050 00051 m_detailsvbox = new QVBoxLayout(m_details); 00052 00053 connect(m_pbdetails, SIGNAL(clicked()), SLOT(slotClicked())); 00054 } 00055 00056 void QProgressIndicator::slotClicked() 00057 { 00058 QPoint indicatorpos = mapToGlobal(pos()); 00059 m_details->move(indicatorpos.x(), indicatorpos.y()); 00060 00061 if (m_details->isVisible()) 00062 m_details->hide(); 00063 else 00064 m_details->show(); 00065 } 00066 00067 void QProgressIndicator::addProgress(const QString & message, int percentage) 00068 { 00069 QProgressBar *pb; 00070 00071 m_progress[message] = percentage; 00072 00073 if (!m_progresswidgets.contains(message)) { 00074 QWidget *pbcontainer = new QWidget(); 00075 00076 // make a label to show the url 00077 QLabel * urlLabel = new QLabel(pbcontainer); 00078 urlLabel->setText(message); 00079 00080 // make a progress bar 00081 pb = new QProgressBar(pbcontainer); 00082 pb->setMinimum(0); 00083 pb->setMaximum(100); 00084 m_progresswidgets.insert(message, pb); 00085 00086 // make a cancel button 00087 QPushButton *pbcancel = new QPushButton(); 00088 pbcancel->setFixedWidth(32); // FIXME: I want a squared button 00089 pbcancel->setIcon(SmallIcon("dialog-cancel")); 00090 00091 QGridLayout *layout = new QGridLayout(pbcontainer); 00092 layout->addWidget(urlLabel, 0, 0, 1, 2); 00093 layout->addWidget(pbcancel, 1, 0); 00094 layout->addWidget(pb, 1, 1); 00095 00096 m_detailsvbox->addWidget(pbcontainer); 00097 00098 pbcontainer->show(); 00099 } else { 00100 pb = m_progresswidgets[message]; 00101 } 00102 00103 pb->setValue(percentage); 00104 00105 if (m_progress.count() > 0) 00106 m_pbdetails->setEnabled(true); 00107 00108 if (percentage == 100) 00109 removeProgress(message); 00110 00111 calculateAverage(); 00112 } 00113 00114 void QProgressIndicator::removeProgress(const QString & message) 00115 { 00116 m_progress.remove(message); 00117 00118 if (m_progresswidgets[message]) { 00119 delete m_progresswidgets[message]->parentWidget(); 00120 m_progresswidgets.remove(message); 00121 } 00122 00123 if (m_progress.count() == 0) { 00124 m_pbdetails->setEnabled(false); 00125 m_details->hide(); 00126 } 00127 00128 calculateAverage(); 00129 } 00130 00131 void QProgressIndicator::calculateAverage() 00132 { 00133 if (m_progress.count() == 0) { 00134 m_pb->reset(); 00135 return; 00136 } 00137 00138 int average = 0; 00139 QHashIterator<QString, int> it(m_progress); 00140 while (it.hasNext()) { 00141 it.next(); 00142 average += it.value(); 00143 } 00144 average = (average / m_progress.count()); 00145 00146 m_pb->setValue(average); 00147 } 00148 00149 #include "qprogressindicator.moc"
KDE 4.6 API Reference