~ubuntu-branches/ubuntu/natty/knemo/natty

« back to all changes in this revision

Viewing changes to src/knemod/statisticsview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-02-22 16:36:22 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110222163622-d8i62gy1stn7tydv
Tags: 0.7.0-0ubuntu1
* New upstream release.
* Switch to source format 3.0 (quilt).
* Make knemo depend on libqt4-sql-sqlite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KNemo
 
2
   Copyright (C) 2010 John Stamp <jstamp@users.sourceforge.net>
 
3
 
 
4
   KNemo is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU Library General Public License as
 
6
   published by the Free Software Foundation; either version 2 of
 
7
   the License, or (at your option) any later version.
 
8
 
 
9
   KNemo is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "statisticsview.h"
 
21
#include "statisticsmodel.h"
 
22
#include <kio/global.h>
 
23
 
 
24
#include <KApplication>
 
25
#include <QDesktopWidget>
 
26
#include <QEvent>
 
27
#include <QHeaderView>
 
28
#include <QLabel>
 
29
#include <QMouseEvent>
 
30
#include <QStylePainter>
 
31
#include <QTimer>
 
32
 
 
33
 
 
34
class StatsTip : public QLabel
 
35
{
 
36
    public:
 
37
        StatsTip();
 
38
        void showText( const bool followMouse, const QString &, QWidget * );
 
39
 
 
40
    private:
 
41
        QTimer statTimer;
 
42
        void placeTip( const QPoint & );
 
43
        void timeout();
 
44
 
 
45
    protected:
 
46
        void paintEvent(QPaintEvent *e);
 
47
        void resizeEvent(QResizeEvent *e);
 
48
};
 
49
 
 
50
StatsTip::StatsTip() : QLabel( 0, Qt::ToolTip )
 
51
{
 
52
    statTimer.setSingleShot( true );
 
53
    connect ( &statTimer, SIGNAL( timeout() ), this, SLOT( hide() ) );
 
54
 
 
55
    setForegroundRole(QPalette::ToolTipText);
 
56
    setBackgroundRole(QPalette::ToolTipBase);
 
57
    setStyleSheet( kapp->styleSheet() );
 
58
    setPalette( kapp->palette() );
 
59
    ensurePolished();
 
60
    setMargin( 1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this ) );
 
61
}
 
62
 
 
63
void StatsTip::showText( const bool followMouse, const QString & tip, QWidget * w )
 
64
{
 
65
    if ( tip != text() )
 
66
    {
 
67
        setText( tip );
 
68
        resize( sizeHint() );
 
69
    }
 
70
    show();
 
71
    if ( followMouse )
 
72
    {
 
73
        placeTip( QCursor::pos() + QPoint( 2, 16 ) );
 
74
    }
 
75
    else
 
76
    {
 
77
        QPoint center = w->rect().center();
 
78
        center.setY( w->rect().height() * 0.4 );
 
79
        QPoint half = QPoint( sizeHint().width()/2, sizeHint().height()/2 );
 
80
        placeTip( w->mapToGlobal( center - half ) );
 
81
    }
 
82
}
 
83
 
 
84
void StatsTip::placeTip( const QPoint & point )
 
85
{
 
86
    int screenNum;
 
87
    if ( QApplication::desktop()->isVirtualDesktop() )
 
88
        screenNum = QApplication::desktop()->screenNumber( point );
 
89
    else
 
90
        screenNum = QApplication::desktop()->screenNumber( static_cast<QWidget*>(parent()) );
 
91
 
 
92
    QRect screen = QApplication::desktop()->screenGeometry( screenNum );
 
93
 
 
94
    QPoint p = point;
 
95
    if ( point.x() + width() > screen.width() )
 
96
        p.setX( screen.width() - width() );
 
97
    if ( point.x() < 0 )
 
98
        p.setX( 0 );
 
99
    if ( point.y() + height() > screen.height() )
 
100
        p.setY( screen.height() - height() );
 
101
    if ( p.y() < 0 )
 
102
        p.setY( 0 );
 
103
 
 
104
    move( p );
 
105
    timeout();
 
106
}
 
107
 
 
108
void StatsTip::paintEvent( QPaintEvent *e )
 
109
{
 
110
    QStylePainter p( this );
 
111
    QStyleOptionFrame opt;
 
112
    opt.init( this );
 
113
    p.drawPrimitive( QStyle::PE_PanelTipLabel, opt );
 
114
    p.end();
 
115
 
 
116
    QLabel::paintEvent( e );
 
117
}
 
118
 
 
119
void StatsTip::resizeEvent( QResizeEvent *e )
 
120
{
 
121
    QStyleHintReturnMask frameMask;
 
122
    QStyleOption option;
 
123
    option.init( this );
 
124
    if ( style()->styleHint( QStyle::SH_ToolTip_Mask, &option, this, &frameMask ) )
 
125
        setMask( frameMask.region );
 
126
 
 
127
    QLabel::resizeEvent( e );
 
128
}
 
129
 
 
130
void StatsTip::timeout()
 
131
{
 
132
    statTimer.start( 2000 );
 
133
}
 
134
 
 
135
static StatsTip * statsTip = NULL;
 
136
 
 
137
 
 
138
StatisticsView::StatisticsView( QWidget * parent ) :
 
139
    QTableView( parent ),
 
140
    mFollow( false ),
 
141
    mOffpeak( false )
 
142
{
 
143
    if ( !statsTip )
 
144
        statsTip = new StatsTip();
 
145
}
 
146
 
 
147
StatisticsView::~StatisticsView()
 
148
{
 
149
}
 
150
 
 
151
void StatisticsView::setModel( QAbstractItemModel *m )
 
152
{
 
153
    QTableView::setModel( m );
 
154
    horizontalHeader()->setMovable( true );
 
155
    connect( selectionModel(), SIGNAL( selectionChanged ( const QItemSelection &, const QItemSelection & ) ),
 
156
             this, SLOT( updateSum() ) );
 
157
}
 
158
 
 
159
void StatisticsView::updateSum()
 
160
{
 
161
    quint64 total = 0;
 
162
     foreach( QModelIndex i, selectionModel()->selectedIndexes() )
 
163
        total += selectionModel()->model()->data( i, StatisticsModel::DataRole ).toULongLong();
 
164
    totalString = KIO::convertSize( total );
 
165
 
 
166
    quint64 offpeak = 0;
 
167
    quint64 peak = 0;
 
168
    if ( mOffpeak )
 
169
    {
 
170
        foreach( QModelIndex i, selectionModel()->selectedIndexes() )
 
171
            offpeak += selectionModel()->model()->data( i, StatisticsModel::DataRole + KNemoStats::OffpeakTraffic ).toULongLong();
 
172
        offpeakString = KIO::convertSize( offpeak );
 
173
        if ( total > offpeak )
 
174
            peak = total - offpeak;
 
175
        peakString = KIO::convertSize( peak );
 
176
    }
 
177
}
 
178
 
 
179
void StatisticsView::showSum( const QPoint &p )
 
180
{
 
181
    QString sumString;
 
182
    QString pStr = i18n( "Peak:" );
 
183
    QString opStr = i18n( "Off-Peak:" );
 
184
    QString tStr = i18n( "Total:" );
 
185
    if ( mOffpeak )
 
186
        sumString = "<table><tr><td>%1</td><td>%2</td></tr><tr><td>%3</td><td>%4</td></tr><tr><td><b>%5</b></td><td><b>%6</b></td></tr></table>";
 
187
    else
 
188
        sumString = "<table><tr><td>%1</td><td>%2</td></tr></table>";
 
189
 
 
190
 
 
191
    if ( selectionModel()->selectedIndexes().count() > 0 && selectionModel()->isSelected( indexAt( p ) ) )
 
192
    {
 
193
        if ( mOffpeak )
 
194
            statsTip->showText( mFollow, sumString.arg( pStr ).arg( peakString )
 
195
                    .arg( opStr ).arg( offpeakString )
 
196
                    .arg( tStr ).arg( totalString ), this );
 
197
        else
 
198
            statsTip->showText( mFollow, sumString.arg( tStr ).arg( totalString ), this );
 
199
    }
 
200
    else if ( indexAt( p ).isValid() )
 
201
    {
 
202
        quint64 totalBytes = model()->data( indexAt( p ), StatisticsModel::DataRole ).toULongLong();
 
203
        if ( mOffpeak )
 
204
        {
 
205
            quint64 offpeakBytes = model()->data( indexAt( p ), StatisticsModel::DataRole + KNemoStats::OffpeakTraffic ).toULongLong();
 
206
            quint64 peakBytes = 0;
 
207
            if ( totalBytes > offpeakBytes )
 
208
                peakBytes = totalBytes - offpeakBytes;
 
209
            statsTip->showText( mFollow, sumString.arg( pStr ).arg( KIO::convertSize( peakBytes ) )
 
210
                    .arg( opStr ).arg( KIO::convertSize( offpeakBytes ) )
 
211
                    .arg( tStr ).arg( KIO::convertSize( totalBytes ) ), this );
 
212
        }
 
213
        else
 
214
        {
 
215
            statsTip->showText( mFollow, sumString.arg( tStr ).arg( KIO::convertSize( totalBytes ) ), this );
 
216
        }
 
217
    }
 
218
    else
 
219
    {
 
220
        statsTip->hide();
 
221
    }
 
222
}
 
223
 
 
224
void StatisticsView::hideEvent( QHideEvent * e )
 
225
{
 
226
    statsTip->hide();
 
227
    QTableView::hideEvent( e );
 
228
}
 
229
 
 
230
void StatisticsView::mousePressEvent( QMouseEvent * e )
 
231
{
 
232
    mFollow = true;
 
233
    QTableView::mousePressEvent( e );
 
234
}
 
235
 
 
236
void StatisticsView::keyPressEvent( QKeyEvent * e )
 
237
{
 
238
 
 
239
    mFollow = false;
 
240
    QTableView::keyPressEvent( e );
 
241
}
 
242
 
 
243
bool StatisticsView::viewportEvent( QEvent * e )
 
244
{
 
245
    switch ( e->type() )
 
246
    {
 
247
        case QEvent::HoverEnter:
 
248
        case QEvent::HoverMove:
 
249
            mFollow = true;
 
250
            showSum( static_cast<QHoverEvent*>(e)->pos() );
 
251
            break;
 
252
        case QEvent::HoverLeave:
 
253
            statsTip->hide();
 
254
        default:
 
255
            ;;
 
256
    }
 
257
    return QTableView::viewportEvent( e );
 
258
}
 
259
 
 
260
#include "statisticsview.moc"