]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/util/fmtwidgetitem.cpp
This is the patch as sent to me by Ricardo.
[bacula/bacula] / bacula / src / qt-console / util / fmtwidgetitem.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28  
29 /*
30  *   Version $Id$
31  *
32  *  Helper functions for tree widget formatting
33  *
34  *   Riccardo Ghetta, May 2008
35  *
36  */ 
37
38 #include <QTreeWidgetItem>
39 #include <QString>
40 #include <QStringList>
41 #include <math.h>
42 #include "fmtwidgetitem.h"
43
44 ItemFormatter::ItemFormatter(QTreeWidgetItem &parent, int indent_level):
45 wdg(new QTreeWidgetItem(&parent)),
46 level(indent_level)
47 {
48 }
49
50 void ItemFormatter::setBoolFld(int index, const QString &fld, bool center)
51 {
52    if (fld.trimmed().toInt())
53      setTextFld(index, "Yes", center);
54    else
55      setTextFld(index, "No", center);
56 }
57
58 void ItemFormatter::setBoolFld(int index, int fld, bool center)
59 {
60    if (fld)
61      setTextFld(index, "Yes", center);
62    else
63      setTextFld(index, "No", center);
64 }
65
66 void ItemFormatter::setTextFld(int index, const QString &fld, bool center)
67 {
68    wdg->setData(index, Qt::UserRole, level);
69    if (center) {
70       wdg->setTextAlignment(index, Qt::AlignHCenter);
71    }
72    wdg->setText(index, fld.trimmed());
73 }
74
75 void ItemFormatter::setNumericFld(int index, const QString &fld)
76 {
77    wdg->setData(index, Qt::UserRole, level);
78    wdg->setTextAlignment(index, Qt::AlignRight);
79    wdg->setText(index, fld.trimmed());
80 }
81
82 void ItemFormatter::setBytesFld(int index, const QString &fld)
83 {
84    static const double KB = 1024.0;
85    static const double MB = KB * KB;
86    static const double GB = MB * KB;
87    static const double TB = GB * KB;
88
89    double dfld = fld.trimmed().toDouble();
90    QString msg;
91    if (dfld >= TB)
92       msg = QString("%1TB").arg(dfld / TB, 0, 'f', 2);
93    else if (dfld >= GB)
94       msg = QString("%1GB").arg(dfld / GB, 0, 'f', 2);
95    else if (dfld >= MB)
96       msg = QString("%1MB").arg(dfld / MB, 0, 'f', 2);
97    else if (dfld >= KB)
98       msg = QString("%1KB").arg(dfld / KB, 0, 'f', 2);
99    else
100       msg = QString::number(dfld, 'f', 0);
101  
102    wdg->setData(index, Qt::UserRole, level);
103    wdg->setTextAlignment(index, Qt::AlignRight);
104    wdg->setText(index, msg);
105 }
106
107 void ItemFormatter::setDurationFld(int index, const QString &fld)
108 {
109    static const double HOUR = 3600;
110    static const double DAY = HOUR * 24;
111    static const double WEEK = DAY * 7;
112    static const double MONTH = DAY * 30;
113    static const double YEAR = DAY * 365;
114
115    double dfld = fld.trimmed().toDouble();
116    QString msg;
117    if (fmod(dfld, YEAR) == 0)
118       msg = QString("%1y").arg(dfld / YEAR, 0, 'f', 0);
119    else if (fmod(dfld, MONTH) == 0)
120       msg = QString("%1m").arg(dfld / MONTH, 0, 'f', 0);
121    else if (fmod(dfld, WEEK) == 0)
122       msg = QString("%1w").arg(dfld / WEEK, 0, 'f', 0);
123    else if (fmod(dfld, DAY) == 0)
124       msg = QString("%1d").arg(dfld / DAY, 0, 'f', 0);
125    else if (fmod(dfld, HOUR) == 0)
126       msg = QString("%1h").arg(dfld / HOUR, 0, 'f', 0);
127    else
128       msg = QString("%1s").arg(dfld, 0, 'f', 0);
129  
130    wdg->setData(index, Qt::UserRole, level);
131    wdg->setTextAlignment(index, Qt::AlignRight);
132    wdg->setText(index, msg);
133 }
134
135 void ItemFormatter::setVolStatusFld(int index, const QString &fld, bool center)
136 {
137   setTextFld(index, fld, center);
138
139    if (fld == "Append" ) {
140       wdg->setBackground(index, Qt::green);
141    } else if (fld == "Error") {
142       wdg->setBackground(index, Qt::red);
143    } else if (fld == "Used" || fld == "Full"){
144       wdg->setBackground(index, Qt::yellow);
145    }
146 }