]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
commited initial version of Dolphin
[dolphin.git] / src / statusbarspaceinfo.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * and Patrice Tremblay *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (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 *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "statusbarspaceinfo.h"
22
23 #include <qpainter.h>
24 #include <qtimer.h>
25 //Added by qt3to4:
26 #include <QPaintEvent>
27 #include <kglobalsettings.h>
28 #include <kdiskfreesp.h>
29 #include <klocale.h>
30 #include <kio/job.h>
31
32 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
33 QWidget(parent),
34 m_gettingSize(false),
35 m_kBSize(0),
36 m_kBAvailable(0)
37 {
38 setMinimumWidth(200);
39
40 // Update the space information each 10 seconds. Polling is useful
41 // here, as files can be deleted/added outside the scope of Dolphin.
42 QTimer* timer = new QTimer(this);
43 connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
44 timer->start(10000);
45 }
46
47 StatusBarSpaceInfo::~StatusBarSpaceInfo()
48 {
49 }
50
51 void StatusBarSpaceInfo::setURL(const KUrl& url)
52 {
53 m_url = url;
54 refresh();
55 update();
56 }
57
58 void StatusBarSpaceInfo::paintEvent(QPaintEvent* /* event */)
59 {
60 QPainter painter(this);
61 const int barWidth = width();
62 const int barTop = 2;
63 const int barHeight = height() - 4;
64
65 QString text;
66
67 const int widthDec = 3; // visual decrement for the available width
68
69 const QColor c1 = colorGroup().background();
70 const QColor c2 = KGlobalSettings::buttonTextColor();
71 const QColor frameColor((c1.red() + c2.red()) / 2,
72 (c1.green() + c2.green()) / 2,
73 (c1.blue() + c2.blue()) / 2);
74 painter.setPen(frameColor);
75
76 const QColor backgrColor = KGlobalSettings::baseColor();
77 painter.setBrush(backgrColor);
78
79 painter.drawRect(QRect(0, barTop + 1 , barWidth - widthDec, barHeight));
80
81 if ((m_kBSize > 0) && (m_kBAvailable > 0)) {
82 // draw 'used size' bar
83 painter.setPen(Qt::NoPen);
84 painter.setBrush(progressColor(backgrColor));
85 int usedWidth = barWidth - static_cast<int>((m_kBAvailable *
86 static_cast<float>(barWidth)) / m_kBSize);
87 const int left = 1;
88 int right = usedWidth - (widthDec + 1);
89 if (right < left) {
90 right = left;
91 }
92 painter.drawRect(QRect(left, barTop + 2, right, barHeight - 2));
93
94 text = i18n("%1% of %2 used")
95 .arg( 100 - (int)(100.0 * m_kBAvailable / m_kBSize))
96 .arg(KIO::convertSizeFromKB(m_kBSize));
97 }
98 else {
99 if (m_gettingSize) {
100 text = i18n("Getting size...");
101 }
102 else {
103 text = "";
104 QTimer::singleShot(0, this, SLOT(hide()));
105 }
106 }
107
108 // draw text (usually 'X% of Y GB used')
109 painter.setPen(KGlobalSettings::textColor());
110 painter.drawText(QRect(1, 1, barWidth - 2, barHeight + 4),
111 Qt::AlignHCenter | Qt::AlignVCenter | Qt::WordBreak,
112 text);
113 }
114
115
116 void StatusBarSpaceInfo::slotFoundMountPoint(const unsigned long& kBSize,
117 const unsigned long& /* kBUsed */,
118 const unsigned long& kBAvailable,
119 const QString& /* mountPoint */)
120 {
121 m_gettingSize = false;
122 m_kBSize = kBSize;
123 m_kBAvailable = kBAvailable;
124
125 // Bypass a the issue (?) of KDiskFreeSp that for protocols like
126 // FTP, SMB the size of root partition is returned.
127 // TODO: check whether KDiskFreeSp is buggy or Dolphin uses it in a wrong way
128 const QString protocol(m_url.protocol());
129 if (!protocol.isEmpty() && (protocol != "file")) {
130 m_kBSize = 0;
131 m_kBAvailable = 0;
132 }
133
134 update();
135 }
136
137 void StatusBarSpaceInfo::slotDone()
138 {
139 m_gettingSize = false;
140 if ((m_kBSize > 0) && (m_kBAvailable > 0)) {
141 show();
142 }
143
144 update();
145 }
146
147 void StatusBarSpaceInfo::refresh()
148 {
149 m_gettingSize = true;
150 m_kBSize = 0;
151 m_kBAvailable = 0;
152
153 const QString mountPoint(KIO::findPathMountPoint(m_url.path()));
154
155 KDiskFreeSp* job = new KDiskFreeSp(this);
156 connect(job, SIGNAL(foundMountPoint(const unsigned long&,
157 const unsigned long&,
158 const unsigned long&,
159 const QString& )),
160 this, SLOT(slotFoundMountPoint(const unsigned long&,
161 const unsigned long&,
162 const unsigned long&,
163 const QString& )));
164 connect(job, SIGNAL(done()),
165 this, SLOT(slotDone()));
166
167 job->readDF(mountPoint);
168 }
169
170 QColor StatusBarSpaceInfo::progressColor(const QColor& bgColor) const
171 {
172 QColor color = KGlobalSettings::buttonBackground();
173
174 // assure that enough contrast is given between the background color
175 // and the progressbar color
176 int bgRed = bgColor.red();
177 int bgGreen = bgColor.green();
178 int bgBlue = bgColor.blue();
179
180 const int backgrBrightness = qGray(bgRed, bgGreen, bgBlue);
181 const int progressBrightness = qGray(color.red(), color.green(), color.blue());
182
183 const int limit = 32;
184 const int diff = backgrBrightness - progressBrightness;
185 bool adjustColor = ((diff >= 0) && (diff < limit)) ||
186 ((diff < 0) && (diff > -limit));
187 if (adjustColor) {
188 const int inc = (backgrBrightness < 2 * limit) ? (2 * limit) : -limit;
189 color = QColor(bgRed + inc, bgGreen + inc, bgBlue + inc);
190 }
191
192 return color;
193 }
194
195 #include "statusbarspaceinfo.moc"