File: qtbars.cpp

package info (click to toggle)
turqstat 2.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,768 kB
  • ctags: 1,148
  • sloc: cpp: 16,876; perl: 250; makefile: 193; sh: 8
file content (168 lines) | stat: -rw-r--r-- 4,426 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2000-2001 Peter Karlsson
//
// $Id: qtbars.cpp,v 1.4 2001/05/15 19:13:48 peter Exp $
//
// 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, version 2
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include <config.h>

#include <qlayout.h>
#include <qlabel.h>
#include <qprogressbar.h>
#include <qpushbutton.h>

#include "qtbars.h"
#include "statengine.h"

/** Class used to display bars for frequency graphs. */
class DataBar : public QProgressBar
{
    Q_OBJECT

public:
    /**
     * Standard constructor.
     * @param parent Parent widget owning the bar.
     */
    DataBar(QWidget *parent) : QProgressBar(parent) {};

protected:
    /**
     * Set the value to display with this bar. This overrides the percentage
     * display that is used by default by QProgressBar.
     *
     * @param indicator Reference to string where the caption that should be
     *                  displayed is written into.
     * @param progress  The number of entries associated with this bar.
     * @return True on success (always succeeds).
     */ 
    bool setIndicator(QString &indicator, int progress, int)
    {
        if (progress >= 0)
        {
            indicator = QString::number(progress);
        }
        else
        {
            indicator = "0";
        }
        return true;
    }
};

#include "qtbars.moc"

BarWindow::BarWindow(QWidget *parent, const char *name, bar_e bartype)
{
    static const char *days[7] =
    {
        QT_TR_NOOP("Monday"),
        QT_TR_NOOP("Tuesday"),
        QT_TR_NOOP("Wednesday"),
        QT_TR_NOOP("Thursday"),
        QT_TR_NOOP("Friday"),
        QT_TR_NOOP("Saturday"),
        QT_TR_NOOP("Sunday"),
    };

    // Determine number of bars and create the pointer array
    type = bartype;
    numentries = (Days == type) ? 7 : 24;
    bars = new QProgressBar *[numentries];

    // Create layout
    QGridLayout *layout = new QGridLayout(this, numentries + 1, 2);

    if (Days == type)
    {
        // Set window caption
        setCaption(tr("Daily breakdown"));

        // Seven bars, one per weekday
        for (int i = 0; i < 7; i ++)
        {
            // Create data label and bar
            QLabel *l = new QLabel(tr(days[i]), this);
            bars[i] = new DataBar(this);
            bars[i]->setCenterIndicator(false);
            bars[i]->setMinimumSize(200, 5);
            layout->addWidget(l, i, 0);
            layout->addWidget(bars[i], i, 1);
        }
    }
    else
    {
        // Set window caption
        setCaption(tr("Hourly breakdown"));

        QString s;

        // 24 bars, one per hour per day
        for (int i = 0; i < 24; i ++)
        {
            // Create data label and bar
            s.sprintf("%02d:00-%02d:00", i, i + 1);
            QLabel *l = new QLabel(s, this);
            bars[i] = new DataBar(this);
            bars[i]->setCenterIndicator(false);
            bars[i]->setMinimumSize(200, 5);
            layout->addWidget(l, i, 0);
            layout->addWidget(bars[i], i, 1);
        }
    }

    // Add close button
    QPushButton *ok = new QPushButton(tr("Dismiss"), this);
    layout->addMultiCellWidget(ok, numentries, numentries, 0, 1);
    connect(ok, SIGNAL(clicked()), SLOT(accept()));
}

BarWindow::~BarWindow()
{
    delete[] bars;
}

void BarWindow::fillOut(StatEngine *engine)
{
    int maximum = 1;

    // Load data
    int *data = new int[numentries];

    for (int i = 0; i < numentries; i ++)
    {
        if (Days == type)
        {
            data[i] = engine->GetDayMsgs((i + 1) % 7);
        }
        else
        {
            data[i] = engine->GetHourMsgs(i);
        }

        if (data[i] > maximum)
        {
            maximum = data[i];
        }
    }

    // Setup the bars
    for (int i = 0; i < numentries; i ++)
    {
        bars[i]->setTotalSteps(maximum);
        bars[i]->setProgress(data[i]);
    }

    delete[] data;
}