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