summaryrefslogtreecommitdiffstats
path: root/digikam/utilities/cameragui/freespacewidget.cpp
blob: 0d7a26b1079e748a56044eaea6348a0b89b25340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2007-08-31
 * Description : a widget to display free space for a mount-point.
 * 
 * Copyright (C) 2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * ============================================================ */

// TQt includes.

#include <tqwhatsthis.h>
#include <tqtooltip.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqpalette.h>
#include <tqcolor.h>
#include <tqtimer.h>
#include <tqfont.h> 
#include <tqfontmetrics.h> 

// KDE includes.

#include <kurl.h>
#include <tdelocale.h>
#include <kdiskfreesp.h>
#include <tdeio/global.h>
#include <kiconloader.h>

// Local includes.

#include "albumsettings.h"
#include "freespacewidget.h"
#include "freespacewidget.moc"

namespace Digikam
{

class FreeSpaceWidgetPriv
{
public:

    FreeSpaceWidgetPriv()
    {
        timer       = 0;
        isValid     = false;
        kBSize      = 0;
        kBUsed      = 0;
        kBAvail     = 0;
        dSizeKb     = 0;
        percentUsed = 0;
    }

    bool           isValid;
    
    int            percentUsed;

    unsigned long  dSizeKb;
    unsigned long  kBSize;
    unsigned long  kBUsed;
    unsigned long  kBAvail;
    
    TQString        mountPoint;

    TQTimer        *timer;
    
    TQPixmap        pix;
};

FreeSpaceWidget::FreeSpaceWidget(TQWidget* parent, int width)
               : TQWidget(parent, 0, WResizeNoErase|WRepaintNoErase)
{
    d = new FreeSpaceWidgetPriv;
    setBackgroundMode(TQt::NoBackground);
    setFixedWidth(width);
    setMaximumHeight(fontMetrics().height()+4);
    slotTimeout();
    
    d->timer = new TQTimer(this);
    
    connect(d->timer, TQ_SIGNAL(timeout()),
            this, TQ_SLOT(slotTimeout()));

    d->timer->start(10000);
}

FreeSpaceWidget::~FreeSpaceWidget()
{
    d->timer->stop();
    delete d->timer;
    delete d;
}

void FreeSpaceWidget::setEstimatedDSizeKb(unsigned long dSize)
{
    d->dSizeKb = dSize;
    updatePixmap();
    repaint();
}

unsigned long FreeSpaceWidget::estimatedDSizeKb()
{
    return d->dSizeKb;
}

bool FreeSpaceWidget::isValid()
{
    return d->isValid;
}

int FreeSpaceWidget::percentUsed() 
{
    return d->percentUsed;
}

unsigned long FreeSpaceWidget::kBSize()
{
    return d->kBSize;
}

unsigned long FreeSpaceWidget::kBUsed()
{
    return d->kBUsed;
}

unsigned long FreeSpaceWidget::kBAvail()
{
    return d->kBAvail;
}

TQString FreeSpaceWidget::mountPoint()
{
    return d->mountPoint;
}

void FreeSpaceWidget::updatePixmap()
{
    TQPixmap fimgPix = SmallIcon("folder_image");
    d->pix          = TQPixmap(size());
    d->pix.fill(colorGroup().background());
    
    TQPainter p(&d->pix);
    p.setPen(colorGroup().mid());
    p.drawRect(0, 0, d->pix.width(), d->pix.height());
    p.drawPixmap(2, d->pix.height()/2-fimgPix.height()/2, 
                 fimgPix, 0, 0, fimgPix.width(), fimgPix.height());
                 
    if (isValid())
    {
        // We will compute the estimated % of space size used to download and process.
        unsigned long eUsedKb = d->dSizeKb + d->kBUsed;
        int peUsed            = (int)(100.0*((double)eUsedKb/(double)d->kBSize));
        int pClamp            = peUsed > 100 ? 100 : peUsed;
        p.setBrush(peUsed > 95 ? TQt::red : TQt::darkGreen);
        p.setPen(TQt::white);
        TQRect gRect(fimgPix.height()+2, 1,
                    (int)(((double)d->pix.width()-2.0-fimgPix.width()-2.0)*(pClamp/100.0)),
                    d->pix.height()-2);
        p.drawRect(gRect);

        TQRect tRect(fimgPix.height()+2, 1, d->pix.width()-2-fimgPix.width()-2, d->pix.height()-2);
        TQString text = TQString("%1%").arg(peUsed);
        p.setPen(colorGroup().text());
        TQFontMetrics fontMt = p.fontMetrics();
        TQRect fontRect      = fontMt.boundingRect(tRect.x(), tRect.y(),
                                                  tRect.width(), tRect.height(), 0, text);
        p.drawText(tRect, TQt::AlignCenter, text);

        TQString tipText, value; 
        TQString header = i18n("Album Library");
        TQString headBeg("<tr bgcolor=\"orange\"><td colspan=\"2\">"
                        "<nobr><font size=\"-1\" color=\"black\"><b>");
        TQString headEnd("</b></font></nobr></td></tr>");
        TQString cellBeg("<tr><td><nobr><font size=-1>");
        TQString cellMid("</font></nobr></td><td><nobr><font size=-1>");
        TQString cellEnd("</font></nobr></td></tr>");
        tipText  = "<table cellspacing=0 cellpadding=0>";
        tipText += headBeg + header + headEnd;

        if (d->dSizeKb > 0)
        {
            tipText += cellBeg + i18n("Capacity:") + cellMid;
            tipText += TDEIO::convertSizeFromKB(d->kBSize) + cellEnd;

            tipText += cellBeg + i18n("Available:") + cellMid;
            tipText += TDEIO::convertSizeFromKB(d->kBAvail) + cellEnd;

            tipText += cellBeg + i18n("Require:") + cellMid;
            tipText += TDEIO::convertSizeFromKB(d->dSizeKb) + cellEnd;
        }
        else
        {
            tipText += cellBeg + i18n("Capacity:") + cellMid;
            tipText += TDEIO::convertSizeFromKB(d->kBSize) + cellEnd;

            tipText += cellBeg + i18n("Available:") + cellMid;
            tipText += TDEIO::convertSizeFromKB(d->kBAvail) + cellEnd;
        }

        tipText += "</table>";

        TQWhatsThis::add(this, tipText);
        TQToolTip::add(this, tipText);
    }
    
    p.end();
}    

void FreeSpaceWidget::paintEvent(TQPaintEvent*)
{
    bitBlt(this, 0, 0, &d->pix);
}

void FreeSpaceWidget::slotTimeout()
{
    TQString mountPoint = TDEIO::findPathMountPoint(AlbumSettings::instance()->getAlbumLibraryPath());
    KDiskFreeSp *job   = new KDiskFreeSp;
    connect(job, TQ_SIGNAL(foundMountPoint(const unsigned long&, const unsigned long&,
                                        const unsigned long&, const TQString&)),
            this, TQ_SLOT(slotAvailableFreeSpace(const unsigned long&, const unsigned long&,
                                              const unsigned long&, const TQString&)));
    job->readDF(mountPoint);
}

void FreeSpaceWidget::slotAvailableFreeSpace(const unsigned long& kBSize, const unsigned long& kBUsed,
                                             const unsigned long& kBAvail, const TQString& mountPoint)
{
    d->mountPoint  = mountPoint;
    d->kBSize      = kBSize;
    d->kBUsed      = kBUsed;
    d->kBAvail     = kBAvail;
    d->percentUsed = 100 - (int)(100.0*kBAvail/kBSize);
    d->isValid     = true;
    updatePixmap();
    repaint();
}

}  // namespace Digikam