]>
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/QKeyEvent>
27 #include <kglobalsettings.h>
28 #include <kdiskfreespace.h>
29 #include <kmountpoint.h>
33 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget
* parent
) :
42 palette
.setColor(QPalette::Background
, Qt::transparent
);
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()));
52 StatusBarSpaceInfo::~StatusBarSpaceInfo()
56 void StatusBarSpaceInfo::setUrl(const KUrl
& url
)
60 QTimer::singleShot(300, this, SLOT(update()));
63 void StatusBarSpaceInfo::paintEvent(QPaintEvent
* /* event */)
65 QPainter
painter(this);
66 const int barWidth
= width();
68 const int barHeight
= height() - 5;
72 const int widthDec
= 3; // visual decrement for the available width
74 QColor frameColor
= palette().brush(QPalette::Background
).color();
75 frameColor
.setAlpha(128);
76 painter
.setPen(frameColor
);
78 const QColor backgrColor
= KGlobalSettings::baseColor();
79 painter
.setBrush(backgrColor
);
81 painter
.drawRect(QRect(0, barTop
+ 1 , barWidth
- widthDec
, barHeight
));
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
);
90 int right
= usedWidth
- widthDec
;
94 painter
.drawRect(QRect(left
, barTop
+ 2, right
, barHeight
- 1));
96 text
= i18n("%1 free", KIO::convertSizeFromKiB(m_kBAvailable
));
99 text
= i18n("Getting size...");
102 QTimer::singleShot(0, this, SLOT(hide()));
107 painter
.setPen(KGlobalSettings::textColor());
108 painter
.drawText(QRect(1, 1, barWidth
- 2, barHeight
+ 6),
109 Qt::AlignCenter
| Qt::TextWordWrap
,
114 void StatusBarSpaceInfo::slotFoundMountPoint(const unsigned long& kBSize
,
115 const unsigned long& kBUsed
,
116 const unsigned long& kBAvailable
,
117 const QString
& mountPoint
)
120 Q_UNUSED(mountPoint
);
122 m_gettingSize
= false;
124 m_kBAvailable
= kBAvailable
;
129 void StatusBarSpaceInfo::showResult()
131 m_gettingSize
= false;
135 void StatusBarSpaceInfo::refresh()
140 // KDiskFreeSpace is for local paths only
141 if (!m_url
.isLocalFile())
144 m_gettingSize
= true;
145 KMountPoint::Ptr mp
= KMountPoint::currentMountPoints().findByPath(m_url
.path());
149 KDiskFreeSpace
* job
= new KDiskFreeSpace(this);
150 connect(job
, SIGNAL(foundMountPoint(const unsigned long&,
151 const unsigned long&,
152 const unsigned long&,
154 this, SLOT(slotFoundMountPoint(const unsigned long&,
155 const unsigned long&,
156 const unsigned long&,
158 connect(job
, SIGNAL(done()),
159 this, SLOT(showResult()));
161 job
->readDF(mp
->mountPoint());
164 QColor
StatusBarSpaceInfo::progressColor(const QColor
& bgColor
) const
166 QColor color
= KGlobalSettings::buttonBackground();
168 // assure that enough contrast is given between the background color
169 // and the progressbar color
170 int bgRed
= bgColor
.red();
171 int bgGreen
= bgColor
.green();
172 int bgBlue
= bgColor
.blue();
174 const int backgrBrightness
= qGray(bgRed
, bgGreen
, bgBlue
);
175 const int progressBrightness
= qGray(color
.red(), color
.green(), color
.blue());
177 const int limit
= 32;
178 const int diff
= backgrBrightness
- progressBrightness
;
179 bool adjustColor
= ((diff
>= 0) && (diff
< limit
)) ||
180 ((diff
< 0) && (diff
> -limit
));
182 const int inc
= (backgrBrightness
< 2 * limit
) ? (2 * limit
) : -limit
;
183 color
= QColor(bgRed
+ inc
, bgGreen
+ inc
, bgBlue
+ inc
);
189 #include "statusbarspaceinfo.moc"