~neon/kdeplasma-addons/trunk

« back to all changes in this revision

Viewing changes to applets/kdeobservatory/src/commithistoryview.cpp

  • Committer: asouza
  • Date: 2011-02-01 19:41:58 UTC
  • Revision ID: svn-v4:283d02a7-25f6-0310-bc7c-ecb5cbfe19da:trunk/KDE/kdeplasma-addons:1218275
Move kdeplasma-addons to git

- You can find information about the project in the link below:

https://projects.kde.org/projects/kde/kdeplasma-addons/repository

- And you can clone the repository using:

git clone git://anongit.kde.org/kdeplasma-addons


Thanks eean and all the sysadmins for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************************************
2
 
 * Copyright 2009-2010 Sandro Andrade sandroandrade@kde.org              *
3
 
 *                                                                       *
4
 
 * This program is free software; you can redistribute it and/or         *
5
 
 * modify it under the terms of the GNU General Public License as        *
6
 
 * published by the Free Software Foundation; either version 2 of        *
7
 
 * the License or (at your option) version 3 or any later version        *
8
 
 * accepted by the membership of KDE e.V. (or its successor approved     *
9
 
 * by the membership of KDE e.V.), which shall act as a proxy            *
10
 
 * defined in Section 14 of version 3 of the license.                    *
11
 
 *                                                                       *
12
 
 * This program is distributed in the hope that it will be useful,       *
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 
 * GNU General Public License for more details.                          *
16
 
 *                                                                       *
17
 
 * You should have received a copy of the GNU General Public License     *
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
19
 
 * ***********************************************************************/
20
 
 
21
 
#include "commithistoryview.h"
22
 
 
23
 
#include <QGraphicsProxyWidget>
24
 
 
25
 
#include <KGlobalSettings>
26
 
 
27
 
#include <qwt_plot.h>
28
 
#include <qwt_plot_grid.h>
29
 
#include <qwt_plot_curve.h>
30
 
#include <qwt_scale_widget.h>
31
 
 
32
 
class TimeScaleDraw : public QwtScaleDraw
33
 
{
34
 
public:
35
 
    TimeScaleDraw(const QDate &minDate)
36
 
    {
37
 
        m_minDate = minDate;
38
 
    }
39
 
 
40
 
    virtual QwtText label(double v) const
41
 
    {
42
 
        QString dateFormat = KGlobal::locale()->dateFormatShort();
43
 
        if (dateFormat == "%Y-%m-%d")
44
 
            return QwtText(m_minDate.addDays((int)v).toString("MM/dd"));
45
 
        else
46
 
            return QwtText(m_minDate.addDays((int)v).toString("dd/MM"));
47
 
    }
48
 
 
49
 
private:
50
 
    QDate m_minDate;
51
 
};
52
 
 
53
 
CommitHistoryView::CommitHistoryView(KdeObservatory *kdeObservatory, const QHash<QString, bool> &commitHistoryViewProjects, const QMap<QString, KdeObservatory::Project> &projects, QGraphicsWidget *parent, Qt::WindowFlags wFlags)
54
 
: IViewProvider(kdeObservatory, parent, wFlags),
55
 
  m_commitHistoryViewProjects(commitHistoryViewProjects),
56
 
  m_projects(projects)
57
 
{
58
 
}
59
 
 
60
 
CommitHistoryView::~CommitHistoryView()
61
 
{
62
 
}
63
 
 
64
 
void CommitHistoryView::createViews()
65
 
{
66
 
    deleteViews();
67
 
    QHashIterator<QString, bool> i(m_commitHistoryViewProjects);
68
 
    while (i.hasNext())
69
 
    {
70
 
        i.next();
71
 
        if (i.value())
72
 
        createView(i18nc("Commit history for a given project %1", "Commit History - %1", i.key()), QString("Commit History - ") + i.key());
73
 
    }
74
 
}
75
 
 
76
 
void CommitHistoryView::updateViews(const Plasma::DataEngine::Data &data)
77
 
{
78
 
    QString project = data["project"].toString();
79
 
 
80
 
    const DateCommitList &projectCommits = data[project].value<DateCommitList>();
81
 
 
82
 
    int count = projectCommits.count();
83
 
    if (count > 0)
84
 
    {
85
 
        int maxCommit = 0;
86
 
 
87
 
        QString tmpStr = projectCommits.at(0).first;
88
 
        qlonglong minDate = tmpStr.remove('-').toLongLong();
89
 
        double *x = new double[count];
90
 
        double *y = new double[count];
91
 
 
92
 
        int count = projectCommits.count();
93
 
        int j;
94
 
        for (j = 0; j < count; ++j)
95
 
        {
96
 
            const QPair<QString, int> &pair = projectCommits.at(j);
97
 
            tmpStr = pair.first;
98
 
            x[j] = j;
99
 
            y[j] = pair.second;
100
 
            if (y[j] > maxCommit)
101
 
                maxCommit = y[j];
102
 
        }
103
 
 
104
 
        QGraphicsWidget *container = containerForView(QString("Commit History - ") + project);
105
 
        if (!container)
106
 
            return;
107
 
        
108
 
        QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(container);
109
 
        proxy->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
110
 
 
111
 
        QwtPlot *plot = new QwtPlot;
112
 
        plot->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
113
 
        plot->setAttribute(Qt::WA_TranslucentBackground, true);
114
 
 
115
 
        plot->setAxisScale(QwtPlot::yLeft, 0, qRound((maxCommit/5.)+0.5)*5, qRound((maxCommit/5.)+0.5));
116
 
 
117
 
        plot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(QDate::fromString(QString::number(minDate), "yyyyMMdd")));
118
 
 
119
 
        plot->setAxisFont(QwtPlot::yLeft, KGlobalSettings::smallestReadableFont());
120
 
        plot->setAxisFont(QwtPlot::xBottom, KGlobalSettings::smallestReadableFont());
121
 
 
122
 
        plot->setAxisLabelRotation(QwtPlot::xBottom, -15);
123
 
 
124
 
        plot->setCanvasBackground(QColor(0, 0, 140));
125
 
 
126
 
        QwtPlotCurve *curve = new QwtPlotCurve;
127
 
        curve->setData(x, y, j);
128
 
        delete []x;
129
 
        delete []y;
130
 
 
131
 
        curve->attach(plot);
132
 
        QPen pen = curve->pen();
133
 
        pen.setColor(QColor(255, 255, 0));
134
 
        curve->setPen(pen);
135
 
        plot->replot();
136
 
 
137
 
        QwtPlotGrid *grid = new QwtPlotGrid;
138
 
        grid->enableXMin(true);
139
 
        grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));
140
 
        grid->setMinPen(QPen(Qt::NoPen));
141
 
        grid->attach(plot);
142
 
 
143
 
        proxy->setWidget(plot);
144
 
        plot->setGeometry(0, 0, container->geometry().width(), container->geometry().height());
145
 
    }
146
 
}