]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
Take in count item sizes. As we want all elements of the same size we check for the...
[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 <kcolorscheme.h>
24 #include <kdiskfreespace.h>
25 #include <kmountpoint.h>
26 #include <klocale.h>
27 #include <kio/job.h>
28
29 #include <QTimer>
30 #include <QPainter>
31 #include <QKeyEvent>
32
33 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
34 QWidget(parent),
35 m_gettingSize(false),
36 m_kBSize(0),
37 m_kBAvailable(0)
38 {
39 setMinimumWidth(200);
40
41 QPalette palette;
42 palette.setColor(QPalette::Background, Qt::transparent);
43 setPalette(palette);
44
45 // Update the space information each 10 seconds. Polling is useful
46 // here, as files can be deleted/added outside the scope of Dolphin.
47 QTimer* timer = new QTimer(this);
48 connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
49 timer->start(10000);
50 }
51
52 StatusBarSpaceInfo::~StatusBarSpaceInfo()
53 {
54 }
55
56 void StatusBarSpaceInfo::setUrl(const KUrl& url)
57 {
58 m_url = url;
59 refresh();
60 QTimer::singleShot(300, this, SLOT(update()));
61 }
62
63 void StatusBarSpaceInfo::paintEvent(QPaintEvent* /* event */)
64 {
65 QPainter painter(this);
66 const int barWidth = width();
67 const int barTop = 1;
68 const int barHeight = height() - 5;
69
70 QString text;
71
72 const int widthDec = 3; // visual decrement for the available width
73
74 QColor frameColor = palette().brush(QPalette::Background).color();
75 frameColor.setAlpha(128);
76 painter.setPen(frameColor);
77
78 const QColor backgrColor = KColorScheme(KColorScheme::View).background();
79 painter.setBrush(backgrColor);
80
81 painter.drawRect(QRect(0, barTop + 1 , barWidth - widthDec, barHeight));
82
83 if ((m_kBSize > 0) && (m_kBAvailable > 0)) {
84 // draw 'used size' bar
85 painter.setPen(Qt::NoPen);
86 painter.setBrush(progressColor(backgrColor));
87 int usedWidth = barWidth - static_cast<int>((m_kBAvailable *
88 static_cast<float>(barWidth)) / m_kBSize);
89 const int left = 1;
90 int right = usedWidth - widthDec;
91 if (right < left) {
92 right = left;
93 }
94 painter.drawRect(QRect(left, barTop + 2, right, barHeight - 1));
95
96 text = i18nc("@info:status", "%1 free", KIO::convertSizeFromKiB(m_kBAvailable));
97 } else {
98 if (m_gettingSize) {
99 text = i18nc("@info:status", "Getting size...");
100 } else {
101 text = QString();
102 QTimer::singleShot(0, this, SLOT(hide()));
103 }
104 }
105
106 // draw text
107 painter.setPen(KColorScheme(KColorScheme::View).foreground());
108 painter.drawText(QRect(1, 1, barWidth - 2, barHeight + 6),
109 Qt::AlignCenter | Qt::TextWordWrap,
110 text);
111 }
112
113
114 void StatusBarSpaceInfo::slotFoundMountPoint(const QString& mountPoint,
115 quint64 kBSize,
116 quint64 kBUsed,
117 quint64 kBAvailable)
118 {
119 Q_UNUSED(kBUsed);
120 Q_UNUSED(mountPoint);
121
122 m_gettingSize = false;
123 m_kBSize = kBSize;
124 m_kBAvailable = kBAvailable;
125
126 update();
127 }
128
129 void StatusBarSpaceInfo::showResult()
130 {
131 m_gettingSize = false;
132 update();
133 }
134
135 void StatusBarSpaceInfo::refresh()
136 {
137 m_kBSize = 0;
138 m_kBAvailable = 0;
139
140 // KDiskFreeSpace is for local paths only
141 if (!m_url.isLocalFile()) {
142 return;
143 }
144
145 m_gettingSize = true;
146 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(m_url.path());
147 if (!mp)
148 return;
149
150 KDiskFreeSpace* job = new KDiskFreeSpace(this);
151 connect(job, SIGNAL(foundMountPoint(const QString&,
152 quint64,
153 quint64,
154 quint64)),
155 this, SLOT(slotFoundMountPoint(const QString&,
156 quint64,
157 quint64,
158 quint64)));
159 connect(job, SIGNAL(done()),
160 this, SLOT(showResult()));
161
162 job->readDF(mp->mountPoint());
163 }
164
165 QColor StatusBarSpaceInfo::progressColor(const QColor& bgColor) const
166 {
167 QColor color = KColorScheme(KColorScheme::Button).background();
168
169 // assure that enough contrast is given between the background color
170 // and the progressbar color
171 int bgRed = bgColor.red();
172 int bgGreen = bgColor.green();
173 int bgBlue = bgColor.blue();
174
175 const int backgrBrightness = qGray(bgRed, bgGreen, bgBlue);
176 const int progressBrightness = qGray(color.red(), color.green(), color.blue());
177
178 const int limit = 32;
179 const int diff = backgrBrightness - progressBrightness;
180 bool adjustColor = ((diff >= 0) && (diff < limit)) ||
181 ((diff < 0) && (diff > -limit));
182 if (adjustColor) {
183 const int inc = (backgrBrightness < 2 * limit) ? (2 * limit) : -limit;
184 color = QColor(bgRed + inc, bgGreen + inc, bgBlue + inc);
185 }
186
187 return color;
188 }
189
190 #include "statusbarspaceinfo.moc"