1
/* This file is part of KNemo
2
Copyright (C) 2010 John Stamp <jstamp@users.sourceforge.net>
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.
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.
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.
20
#include "statisticsview.h"
21
#include "statisticsmodel.h"
22
#include <kio/global.h>
24
#include <KApplication>
25
#include <QDesktopWidget>
27
#include <QHeaderView>
29
#include <QMouseEvent>
30
#include <QStylePainter>
34
class StatsTip : public QLabel
38
void showText( const bool followMouse, const QString &, QWidget * );
42
void placeTip( const QPoint & );
46
void paintEvent(QPaintEvent *e);
47
void resizeEvent(QResizeEvent *e);
50
StatsTip::StatsTip() : QLabel( 0, Qt::ToolTip )
52
statTimer.setSingleShot( true );
53
connect ( &statTimer, SIGNAL( timeout() ), this, SLOT( hide() ) );
55
setForegroundRole(QPalette::ToolTipText);
56
setBackgroundRole(QPalette::ToolTipBase);
57
setStyleSheet( kapp->styleSheet() );
58
setPalette( kapp->palette() );
60
setMargin( 1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this ) );
63
void StatsTip::showText( const bool followMouse, const QString & tip, QWidget * w )
73
placeTip( QCursor::pos() + QPoint( 2, 16 ) );
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 ) );
84
void StatsTip::placeTip( const QPoint & point )
87
if ( QApplication::desktop()->isVirtualDesktop() )
88
screenNum = QApplication::desktop()->screenNumber( point );
90
screenNum = QApplication::desktop()->screenNumber( static_cast<QWidget*>(parent()) );
92
QRect screen = QApplication::desktop()->screenGeometry( screenNum );
95
if ( point.x() + width() > screen.width() )
96
p.setX( screen.width() - width() );
99
if ( point.y() + height() > screen.height() )
100
p.setY( screen.height() - height() );
108
void StatsTip::paintEvent( QPaintEvent *e )
110
QStylePainter p( this );
111
QStyleOptionFrame opt;
113
p.drawPrimitive( QStyle::PE_PanelTipLabel, opt );
116
QLabel::paintEvent( e );
119
void StatsTip::resizeEvent( QResizeEvent *e )
121
QStyleHintReturnMask frameMask;
124
if ( style()->styleHint( QStyle::SH_ToolTip_Mask, &option, this, &frameMask ) )
125
setMask( frameMask.region );
127
QLabel::resizeEvent( e );
130
void StatsTip::timeout()
132
statTimer.start( 2000 );
135
static StatsTip * statsTip = NULL;
138
StatisticsView::StatisticsView( QWidget * parent ) :
139
QTableView( parent ),
144
statsTip = new StatsTip();
147
StatisticsView::~StatisticsView()
151
void StatisticsView::setModel( QAbstractItemModel *m )
153
QTableView::setModel( m );
154
horizontalHeader()->setMovable( true );
155
connect( selectionModel(), SIGNAL( selectionChanged ( const QItemSelection &, const QItemSelection & ) ),
156
this, SLOT( updateSum() ) );
159
void StatisticsView::updateSum()
162
foreach( QModelIndex i, selectionModel()->selectedIndexes() )
163
total += selectionModel()->model()->data( i, StatisticsModel::DataRole ).toULongLong();
164
totalString = KIO::convertSize( total );
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 );
179
void StatisticsView::showSum( const QPoint &p )
182
QString pStr = i18n( "Peak:" );
183
QString opStr = i18n( "Off-Peak:" );
184
QString tStr = i18n( "Total:" );
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>";
188
sumString = "<table><tr><td>%1</td><td>%2</td></tr></table>";
191
if ( selectionModel()->selectedIndexes().count() > 0 && selectionModel()->isSelected( indexAt( p ) ) )
194
statsTip->showText( mFollow, sumString.arg( pStr ).arg( peakString )
195
.arg( opStr ).arg( offpeakString )
196
.arg( tStr ).arg( totalString ), this );
198
statsTip->showText( mFollow, sumString.arg( tStr ).arg( totalString ), this );
200
else if ( indexAt( p ).isValid() )
202
quint64 totalBytes = model()->data( indexAt( p ), StatisticsModel::DataRole ).toULongLong();
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 );
215
statsTip->showText( mFollow, sumString.arg( tStr ).arg( KIO::convertSize( totalBytes ) ), this );
224
void StatisticsView::hideEvent( QHideEvent * e )
227
QTableView::hideEvent( e );
230
void StatisticsView::mousePressEvent( QMouseEvent * e )
233
QTableView::mousePressEvent( e );
236
void StatisticsView::keyPressEvent( QKeyEvent * e )
240
QTableView::keyPressEvent( e );
243
bool StatisticsView::viewportEvent( QEvent * e )
247
case QEvent::HoverEnter:
248
case QEvent::HoverMove:
250
showSum( static_cast<QHoverEvent*>(e)->pos() );
252
case QEvent::HoverLeave:
257
return QTableView::viewportEvent( e );
260
#include "statisticsview.moc"