]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
fix #144021: show free space in GB instead of used space in percent
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "statusbarspaceinfo.h"
22
23 #include <QPainter>
24 #include <QPaintEvent>
25 #include <QTimer>
26
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 void StatusBarSpaceInfo::setUrl(const KUrl& url)
51 {
52 m_url = url;
53 refresh();
54 QTimer::singleShot(300, this, SLOT(update()));
55 }
56
57 void StatusBarSpaceInfo::paintEvent(QPaintEvent* /* event */)
58 {
59 QPainter painter(this);
60 const int barWidth = width();
61 const int barTop = 1;
62 const int barHeight = height() - 5;
63
64 QString text;
65
66 const int widthDec = 3; // visual decrement for the available width
67
68 const QColor c1 = palette().brush(QPalette::Background).color();
69 const QColor c2 = KGlobalSettings::buttonTextColor();
70 const QColor frameColor((c1.red() + c2.red()) / 2,
71 (c1.green() + c2.green()) / 2,
72 (c1.blue() + c2.blue()) / 2);
73 painter.setPen(frameColor);
74
75 const QColor backgrColor = KGlobalSettings::baseColor();
76 painter.setBrush(backgrColor);
77
78 painter.drawRect(QRect(0, barTop + 1 , barWidth - widthDec, barHeight));
79
80 if ((m_kBSize > 0) && (m_kBAvailable > 0)) {
81 // draw 'used size' bar
82 painter.setPen(Qt::NoPen);
83 painter.setBrush(progressColor(backgrColor));
84 int usedWidth = barWidth - static_cast<int>((m_kBAvailable *
85 static_cast<float>(barWidth)) / m_kBSize);
86 const int left = 1;
87 int right = usedWidth - widthDec;
88 if (right < left) {
89 right = left;
90 }
91 painter.drawRect(QRect(left, barTop + 2, right, barHeight - 1));
92
93 text = i18n("%1 free", KIO::convertSizeFromKiB(m_kBAvailable));
94 } else {
95 if (m_gettingSize) {
96 text = i18n("Getting size...");
97 } else {
98 text = QString();
99 QTimer::singleShot(0, this, SLOT(hide()));
100 }
101 }
102
103 // draw text
104 painter.setPen(KGlobalSettings::textColor());
105 painter.drawText(QRect(1, 1, barWidth - 2, barHeight + 6),
106 Qt::AlignCenter | Qt::TextWordWrap,
107 text);
108 }
109
110
111 void StatusBarSpaceInfo::slotFoundMountPoint(const unsigned long& kBSize,
112 const unsigned long& /* kBUsed */,
113 const unsigned long& kBAvailable,
114 const QString& /* mountPoint */)
115 {
116 m_gettingSize = false;
117 m_kBSize = kBSize;
118 m_kBAvailable = kBAvailable;
119
120 // Bypass a the issue (?) of KDiskFreeSp that for protocols like
121 // FTP, SMB the size of root partition is returned.
122 // TODO: check whether KDiskFreeSp is buggy or Dolphin uses it in a wrong way
123 const QString protocol(m_url.protocol());
124 if (!protocol.isEmpty() && (protocol != "file")) {
125 m_kBSize = 0;
126 m_kBAvailable = 0;
127 }
128
129 update();
130 }
131
132 void StatusBarSpaceInfo::showResult()
133 {
134 m_gettingSize = false;
135 update();
136 }
137
138 void StatusBarSpaceInfo::refresh()
139 {
140 m_gettingSize = true;
141 m_kBSize = 0;
142 m_kBAvailable = 0;
143
144 const QString mountPoint(KIO::findPathMountPoint(m_url.path()));
145
146 KDiskFreeSp* job = new KDiskFreeSp(this);
147 connect(job, SIGNAL(foundMountPoint(const unsigned long&,
148 const unsigned long&,
149 const unsigned long&,
150 const QString&)),
151 this, SLOT(slotFoundMountPoint(const unsigned long&,
152 const unsigned long&,
153 const unsigned long&,
154 const QString&)));
155 connect(job, SIGNAL(done()),
156 this, SLOT(showResult()));
157
158 job->readDF(mountPoint);
159 }
160
161 QColor StatusBarSpaceInfo::progressColor(const QColor& bgColor) const
162 {
163 QColor color = KGlobalSettings::buttonBackground();
164
165 // assure that enough contrast is given between the background color
166 // and the progressbar color
167 int bgRed = bgColor.red();
168 int bgGreen = bgColor.green();
169 int bgBlue = bgColor.blue();
170
171 const int backgrBrightness = qGray(bgRed, bgGreen, bgBlue);
172 const int progressBrightness = qGray(color.red(), color.green(), color.blue());
173
174 const int limit = 32;
175 const int diff = backgrBrightness - progressBrightness;
176 bool adjustColor = ((diff >= 0) && (diff < limit)) ||
177 ((diff < 0) && (diff > -limit));
178 if (adjustColor) {
179 const int inc = (backgrBrightness < 2 * limit) ? (2 * limit) : -limit;
180 color = QColor(bgRed + inc, bgGreen + inc, bgBlue + inc);
181 }
182
183 return color;
184 }
185
186 #include "statusbarspaceinfo.moc"