~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to krunner/interfaces/default/resultsview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright 2009 by Jacopo De Simoi <wilderkde@gmail.com>               *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include <KDebug>
 
21
 
 
22
#include <QGraphicsView>
 
23
#include <QToolButton>
 
24
#include <QPaintEvent>
 
25
#include <QMutex>
 
26
#include <QScrollBar>
 
27
 
 
28
#include <Plasma/AbstractRunner>
 
29
#include <Plasma/Svg>
 
30
 
 
31
 
 
32
#include "resultscene.h"
 
33
#include "resultsview.h"
 
34
#include "resultitem.h"
 
35
 
 
36
ResultsView::ResultsView(ResultScene *scene, SharedResultData *resultData, QWidget *parent)
 
37
    : QGraphicsView(scene, parent),
 
38
      m_resultScene(scene),
 
39
      m_resultData(resultData)
 
40
{
 
41
    setFrameStyle(QFrame::NoFrame);
 
42
    viewport()->setAutoFillBackground(false);
 
43
    setInteractive(true);
 
44
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
45
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
46
    setOptimizationFlag(QGraphicsView::DontSavePainterState);
 
47
    setAlignment(Qt::AlignLeft | Qt::AlignTop);
 
48
 
 
49
    m_arrowSvg = new Plasma::Svg(this);
 
50
    m_arrowSvg->setImagePath(QLatin1String( "widgets/arrows" ));
 
51
 
 
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()));
 
56
 
 
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()));
 
61
 
 
62
    connect(m_arrowSvg, SIGNAL(repaintNeeded()), this, SLOT(updateArrowsIcons()));
 
63
    updateArrowsIcons();
 
64
 
 
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 *)));
 
68
}
 
69
 
 
70
ResultsView::~ResultsView()
 
71
{
 
72
}
 
73
 
 
74
void ResultsView::ensureVisibility(QGraphicsItem* item)
 
75
{
 
76
    m_resultData->processHoverEvents = false;
 
77
    ensureVisible(item, 0, 0);
 
78
    m_resultData->processHoverEvents = true;
 
79
}
 
80
 
 
81
void ResultsView::previousPage()
 
82
{
 
83
    QGraphicsItem *currentItem = m_resultScene->selectedItems().first();
 
84
    QGraphicsItem *item = itemAt(0, -height()*0.4);
 
85
 
 
86
    if (!item) {
 
87
        item = m_resultScene->itemAt(0,0);
 
88
    }
 
89
 
 
90
    if (item && (item != currentItem)) {
 
91
        m_resultScene->setFocusItem(item);
 
92
    } else {
 
93
        verticalScrollBar()->setValue(verticalScrollBar()->value()-height()*0.4);
 
94
    }
 
95
}
 
96
 
 
97
 
 
98
void ResultsView::nextPage()
 
99
{
 
100
    QGraphicsItem *currentItem = m_resultScene->selectedItems().first();
 
101
    QGraphicsItem *item = itemAt(0, height()*1.4);
 
102
    if (!item) {
 
103
        item = m_resultScene->itemAt(0,sceneRect().height()-1);
 
104
    }
 
105
 
 
106
    ResultItem *rItem = dynamic_cast<ResultItem *>(item);
 
107
    if (rItem && !rItem->isValid()) {
 
108
        item = m_resultScene->itemAt(0, m_resultScene->viewableHeight() - 1);
 
109
    }
 
110
 
 
111
    if (item && (item != currentItem)) {
 
112
        m_resultScene->setFocusItem(item);
 
113
    } else {
 
114
        verticalScrollBar()->setValue(qMin(m_resultScene->viewableHeight(),
 
115
                                           int(verticalScrollBar()->value()+ (height() * 0.4))));
 
116
    }
 
117
}
 
118
 
 
119
void ResultsView::resizeEvent(QResizeEvent * event)
 
120
{
 
121
    updateArrowsVisibility();
 
122
    QGraphicsView::resizeEvent(event);
 
123
}
 
124
 
 
125
void ResultsView::updateArrowsIcons()
 
126
{
 
127
    m_previousPage->setIcon(m_arrowSvg->pixmap(QLatin1String( "up-arrow" )));
 
128
    m_previousPage->adjustSize();
 
129
 
 
130
    m_nextPage->setIcon(m_arrowSvg->pixmap(QLatin1String( "down-arrow" )));
 
131
    m_nextPage->adjustSize();
 
132
 
 
133
    updateArrowsVisibility();
 
134
}
 
135
 
 
136
void ResultsView::updateArrowsVisibility()
 
137
{
 
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());
 
140
 
 
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()));
 
144
}
 
145
 
 
146
void ResultsView::wheelEvent(QWheelEvent *e)
 
147
{
 
148
    if (e->delta() < 0 && !m_nextPage->isVisible()) {
 
149
        return;
 
150
    }
 
151
 
 
152
    QGraphicsView::wheelEvent(e);
 
153
}
 
154
 
 
155
void ResultsView::paintEvent(QPaintEvent *event)
 
156
{
 
157
    QPixmap backBuffer(viewport()->size());
 
158
    backBuffer.fill(Qt::transparent);
 
159
    QPainter backBufferPainter(&backBuffer);
 
160
 
 
161
    QPainter painter(viewport());
 
162
 
 
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.
 
167
 
 
168
    painter.setRedirected(viewport(),&backBuffer);
 
169
    QGraphicsView::paintEvent(event);
 
170
    painter.restoreRedirected(viewport());
 
171
 
 
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);
 
181
    }
 
182
 
 
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);
 
192
    }
 
193
 
 
194
    backBufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
 
195
    if (m_previousPage->isVisible()) {
 
196
        backBufferPainter.drawPixmap(QPoint(0,0), m_previousFadeout);
 
197
    }
 
198
 
 
199
    if (m_nextPage->isVisible()) {
 
200
        backBufferPainter.drawPixmap(QPoint(0,height()-m_nextFadeout.height()), m_nextFadeout);
 
201
    }
 
202
     painter.drawPixmap(event->rect(), backBuffer, event->rect());
 
203
}
 
204
 
 
205
#include "resultsview.moc"