]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * and Patrice Tremblay *
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. *
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. *
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 ***************************************************************************/
21 #include "statusbarspaceinfo.h"
23 #include <QtCore/QTimer>
24 #include <QtGui/QPainter>
25 #include <QtGui/QPaintEvent>
27 #include <kglobalsettings.h>
28 #include <kdiskfreesp.h>
32 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget
* parent
) :
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()));
47 StatusBarSpaceInfo::~StatusBarSpaceInfo()
51 void StatusBarSpaceInfo::setUrl(const KUrl
& url
)
55 QTimer::singleShot(300, this, SLOT(update()));
58 void StatusBarSpaceInfo::paintEvent(QPaintEvent
* /* event */)
60 QPainter
painter(this);
61 const int barWidth
= width();
63 const int barHeight
= height() - 5;
67 const int widthDec
= 3; // visual decrement for the available width
69 const QColor c1
= palette().brush(QPalette::Background
).color();
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
);
76 const QColor backgrColor
= KGlobalSettings::baseColor();
77 painter
.setBrush(backgrColor
);
79 painter
.drawRect(QRect(0, barTop
+ 1 , barWidth
- widthDec
, barHeight
));
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
);
88 int right
= usedWidth
- widthDec
;
92 painter
.drawRect(QRect(left
, barTop
+ 2, right
, barHeight
- 1));
94 text
= i18n("%1 free", KIO::convertSizeFromKiB(m_kBAvailable
));
97 text
= i18n("Getting size...");
100 QTimer::singleShot(0, this, SLOT(hide()));
105 painter
.setPen(KGlobalSettings::textColor());
106 painter
.drawText(QRect(1, 1, barWidth
- 2, barHeight
+ 6),
107 Qt::AlignCenter
| Qt::TextWordWrap
,
112 void StatusBarSpaceInfo::slotFoundMountPoint(const unsigned long& kBSize
,
113 const unsigned long& kBUsed
,
114 const unsigned long& kBAvailable
,
115 const QString
& mountPoint
)
118 Q_UNUSED(mountPoint
);
120 m_gettingSize
= false;
122 m_kBAvailable
= kBAvailable
;
124 // Bypass a the issue (?) of KDiskFreeSp that for protocols like
125 // FTP, SMB the size of root partition is returned.
126 // TODO: check whether KDiskFreeSp is buggy or Dolphin uses it in a wrong way
127 const QString
protocol(m_url
.protocol());
128 if (!protocol
.isEmpty() && (protocol
!= "file")) {
136 void StatusBarSpaceInfo::showResult()
138 m_gettingSize
= false;
142 void StatusBarSpaceInfo::refresh()
144 m_gettingSize
= true;
148 const QString
mountPoint(KIO::findPathMountPoint(m_url
.path()));
150 KDiskFreeSp
* job
= new KDiskFreeSp(this);
151 connect(job
, SIGNAL(foundMountPoint(const unsigned long&,
152 const unsigned long&,
153 const unsigned long&,
155 this, SLOT(slotFoundMountPoint(const unsigned long&,
156 const unsigned long&,
157 const unsigned long&,
159 connect(job
, SIGNAL(done()),
160 this, SLOT(showResult()));
162 job
->readDF(mountPoint
);
165 QColor
StatusBarSpaceInfo::progressColor(const QColor
& bgColor
) const
167 QColor color
= KGlobalSettings::buttonBackground();
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();
175 const int backgrBrightness
= qGray(bgRed
, bgGreen
, bgBlue
);
176 const int progressBrightness
= qGray(color
.red(), color
.green(), color
.blue());
178 const int limit
= 32;
179 const int diff
= backgrBrightness
- progressBrightness
;
180 bool adjustColor
= ((diff
>= 0) && (diff
< limit
)) ||
181 ((diff
< 0) && (diff
> -limit
));
183 const int inc
= (backgrBrightness
< 2 * limit
) ? (2 * limit
) : -limit
;
184 color
= QColor(bgRed
+ inc
, bgGreen
+ inc
, bgBlue
+ inc
);
190 #include "statusbarspaceinfo.moc"