1
/***************************************************************************
2
* Copyright 2009 by Jacopo De Simoi <wilderkde@gmail.com> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program 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 General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18
***************************************************************************/
22
#include <QGraphicsView>
23
#include <QToolButton>
24
#include <QPaintEvent>
28
#include <Plasma/AbstractRunner>
32
#include "resultscene.h"
33
#include "resultsview.h"
34
#include "resultitem.h"
36
ResultsView::ResultsView(ResultScene *scene, SharedResultData *resultData, QWidget *parent)
37
: QGraphicsView(scene, parent),
39
m_resultData(resultData)
41
setFrameStyle(QFrame::NoFrame);
42
viewport()->setAutoFillBackground(false);
44
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46
setOptimizationFlag(QGraphicsView::DontSavePainterState);
47
setAlignment(Qt::AlignLeft | Qt::AlignTop);
49
m_arrowSvg = new Plasma::Svg(this);
50
m_arrowSvg->setImagePath(QLatin1String( "widgets/arrows" ));
52
m_previousPage = new QToolButton(this);
53
m_previousPage->setAutoRaise(true);
54
m_previousPage->setVisible(false);
55
connect(m_previousPage, SIGNAL(clicked(bool)), SLOT(previousPage()));
57
m_nextPage = new QToolButton(this);
58
m_nextPage->setAutoRaise(true);
59
m_nextPage->setVisible(false);
60
connect(m_nextPage, SIGNAL(clicked(bool)), SLOT(nextPage()));
62
connect(m_arrowSvg, SIGNAL(repaintNeeded()), this, SLOT(updateArrowsIcons()));
65
connect(verticalScrollBar(), SIGNAL(rangeChanged(int, int)), this, SLOT(updateArrowsVisibility()));
66
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateArrowsVisibility()));
67
connect(m_resultScene, SIGNAL(ensureVisibility(QGraphicsItem *)), this, SLOT(ensureVisibility(QGraphicsItem *)));
70
ResultsView::~ResultsView()
74
void ResultsView::ensureVisibility(QGraphicsItem* item)
76
m_resultData->processHoverEvents = false;
77
ensureVisible(item, 0, 0);
78
m_resultData->processHoverEvents = true;
81
void ResultsView::previousPage()
83
QGraphicsItem *currentItem = m_resultScene->selectedItems().first();
84
QGraphicsItem *item = itemAt(0, -height()*0.4);
87
item = m_resultScene->itemAt(0,0);
90
if (item && (item != currentItem)) {
91
m_resultScene->setFocusItem(item);
93
verticalScrollBar()->setValue(verticalScrollBar()->value()-height()*0.4);
98
void ResultsView::nextPage()
100
QGraphicsItem *currentItem = m_resultScene->selectedItems().first();
101
QGraphicsItem *item = itemAt(0, height()*1.4);
103
item = m_resultScene->itemAt(0,sceneRect().height()-1);
106
ResultItem *rItem = dynamic_cast<ResultItem *>(item);
107
if (rItem && !rItem->isValid()) {
108
item = m_resultScene->itemAt(0, m_resultScene->viewableHeight() - 1);
111
if (item && (item != currentItem)) {
112
m_resultScene->setFocusItem(item);
114
verticalScrollBar()->setValue(qMin(m_resultScene->viewableHeight(),
115
int(verticalScrollBar()->value()+ (height() * 0.4))));
119
void ResultsView::resizeEvent(QResizeEvent * event)
121
updateArrowsVisibility();
122
QGraphicsView::resizeEvent(event);
125
void ResultsView::updateArrowsIcons()
127
m_previousPage->setIcon(m_arrowSvg->pixmap(QLatin1String( "up-arrow" )));
128
m_previousPage->adjustSize();
130
m_nextPage->setIcon(m_arrowSvg->pixmap(QLatin1String( "down-arrow" )));
131
m_nextPage->adjustSize();
133
updateArrowsVisibility();
136
void ResultsView::updateArrowsVisibility()
138
m_previousPage->move((width() / 2) - (m_previousPage->width() / 2), 0);
139
m_nextPage->move((width() / 2) - (m_nextPage->width() / 2), height() - m_nextPage->height());
141
m_previousPage->setVisible(mapFromScene(QPointF(0,0)).y() < 0);
142
m_nextPage->setVisible(mapFromScene(QPointF(0, m_resultScene->viewableHeight())).y() > height());
143
//kDebug() << m_resultScene->viewableHeight() << height() << mapFromScene(QPointF(0, m_resultScene->viewableHeight()));
146
void ResultsView::wheelEvent(QWheelEvent *e)
148
if (e->delta() < 0 && !m_nextPage->isVisible()) {
152
QGraphicsView::wheelEvent(e);
155
void ResultsView::paintEvent(QPaintEvent *event)
157
QPixmap backBuffer(viewport()->size());
158
backBuffer.fill(Qt::transparent);
159
QPainter backBufferPainter(&backBuffer);
161
QPainter painter(viewport());
163
// Here we need to redirect to a backBuffer since krunnerdialog
164
// draws its own background; the QPainter is then propagated to the widgets so that
165
// we cannot directly blit with destinationIn because the destination has
166
// already the (plasma-themed) background painted.
168
painter.setRedirected(viewport(),&backBuffer);
169
QGraphicsView::paintEvent(event);
170
painter.restoreRedirected(viewport());
172
if (m_previousFadeout.isNull() || m_previousFadeout.width() != width()) {
173
QLinearGradient g(0, 0, 0, m_previousPage->height());
174
g.setColorAt(1, Qt::white );
175
g.setColorAt(0, Qt::transparent );
176
m_previousFadeout = QPixmap(width(), m_previousPage->height());
177
m_previousFadeout.fill(Qt::transparent);
178
QPainter p(&m_previousFadeout);
179
p.setCompositionMode(QPainter::CompositionMode_Source);
180
p.fillRect(m_previousFadeout.rect(), g);
183
if (m_nextFadeout.isNull() || m_nextFadeout.width() != width()) {
184
QLinearGradient g(0, 0, 0, m_nextPage->height());
185
g.setColorAt(0, Qt::white );
186
g.setColorAt(1, Qt::transparent );
187
m_nextFadeout = QPixmap(width(), m_nextPage->height());
188
m_nextFadeout.fill(Qt::transparent);
189
QPainter p(&m_nextFadeout);
190
p.setCompositionMode(QPainter::CompositionMode_Source);
191
p.fillRect(m_nextFadeout.rect(), g);
194
backBufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
195
if (m_previousPage->isVisible()) {
196
backBufferPainter.drawPixmap(QPoint(0,0), m_previousFadeout);
199
if (m_nextPage->isVisible()) {
200
backBufferPainter.drawPixmap(QPoint(0,height()-m_nextFadeout.height()), m_nextFadeout);
202
painter.drawPixmap(event->rect(), backBuffer, event->rect());
205
#include "resultsview.moc"