~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kicker/kicker/ui/addappletvisualfeedback.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-31 19:16:54 UTC
  • Revision ID: james.westby@ubuntu.com-20071031191654-xuof6e1jg6uxqaze
Tags: 3.95.0-0ubuntu1~gutsy1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************
2
 
 
3
 
Copyright (c) 2007, 2006 Rafael Fernández López <ereslibre@kde.org>
4
 
Copyright (c) 2004-2005 Aaron J. Seigo <aseigo@kde.org>
5
 
Copyright (c) 2004 Zack Rusin <zrusin@kde.org>
6
 
                   Sami Kyostil <skyostil@kempele.fi>
7
 
 
8
 
Permission is hereby granted, free of charge, to any person obtaining a copy
9
 
of this software and associated documentation files (the "Software"), to deal
10
 
in the Software without restriction, including without limitation the rights
11
 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 
copies of the Software, and to permit persons to whom the Software is
13
 
furnished to do so, subject to the following conditions:
14
 
 
15
 
The above copyright notice and this permission notice shall be included in
16
 
all copies or substantial portions of the Software.
17
 
 
18
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21
 
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22
 
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
 
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 
 
25
 
******************************************************************/
26
 
 
27
 
#include <QApplication>
28
 
#include <QLabel>
29
 
#include <QPainter>
30
 
#include <QTimer>
31
 
#include <Qt3Support/Q3SimpleRichText>
32
 
#include <QListView>
33
 
 
34
 
#include <kdialog.h>
35
 
#include <klocale.h>
36
 
 
37
 
#include "utils.h"
38
 
#include "appletinfo.h"
39
 
#include "kickerSettings.h"
40
 
#include "addappletvisualfeedback.h"
41
 
#include "addappletvisualfeedback.moc"
42
 
 
43
 
 
44
 
#define DEFAULT_FRAMES_PER_SECOND 30
45
 
 
46
 
AddAppletVisualFeedback::AddAppletVisualFeedback(const QModelIndex& index,
47
 
                                                 QListView* theListView,
48
 
                                                 const QWidget* target,
49
 
                                                 Plasma::Position direction)
50
 
    : QWidget(0, Qt::X11BypassWindowManagerHint),
51
 
      m_target(target),
52
 
      m_direction(direction),
53
 
      m_richText(0),
54
 
      m_dissolveSize(24),
55
 
      m_dissolveDelta(-1),
56
 
      m_frames(1),
57
 
      m_dirty(false)
58
 
{
59
 
    AppletInfo *appletData = static_cast<AppletInfo*>(index.internalPointer());
60
 
 
61
 
    m_icon = KIcon(appletData->icon()).pixmap(QSize(64, 64));
62
 
 
63
 
    setObjectName("animtt");
64
 
    setFocusPolicy(Qt::NoFocus);
65
 
    setAttribute(Qt::WA_NoSystemBackground, true);
66
 
    connect(&m_moveTimer, SIGNAL(timeout()), SLOT(swoopCloser()));
67
 
 
68
 
    QString m = "<qt><h3>" + i18n("%1 Added", appletData->name());
69
 
 
70
 
    if (appletData->name() != appletData->comment())
71
 
    {
72
 
        m += "</h3><p>" + appletData->comment() + "</p></qt>";
73
 
    }
74
 
 
75
 
    m_richText = new Q3SimpleRichText(m, font());
76
 
    m_richText->setWidth(400);
77
 
 
78
 
    displayInternal();
79
 
 
80
 
    m_destination = Plasma::popupPosition(m_direction, this, m_target);
81
 
    QPoint startAt = theListView->visualRect(index).topLeft();
82
 
    startAt = theListView->mapToGlobal(startAt);
83
 
    move(startAt);
84
 
 
85
 
    m_frames = (m_destination - startAt).manhattanLength() / 20;
86
 
    m_moveTimer.start(10);
87
 
 
88
 
    show();
89
 
}
90
 
 
91
 
void AddAppletVisualFeedback::paintEvent(QPaintEvent * e)
92
 
{
93
 
    if (m_dirty)
94
 
    {
95
 
        displayInternal();
96
 
        m_dirty = false;
97
 
    }
98
 
 
99
 
    QPainter p(this);
100
 
    p.drawPixmap(e->rect().topLeft(), m_pixmap, e->rect());
101
 
}
102
 
 
103
 
void AddAppletVisualFeedback::mousePressEvent(QMouseEvent *)
104
 
{
105
 
    m_moveTimer.stop();
106
 
    hide();
107
 
    deleteLater();
108
 
}
109
 
 
110
 
void AddAppletVisualFeedback::makeMask()
111
 
{
112
 
    QPainter maskPainter(&m_mask);
113
 
 
114
 
    m_mask.fill(Qt::color0);
115
 
 
116
 
    maskPainter.setBrush(Qt::color1);
117
 
    maskPainter.setPen(Qt::color1);
118
 
    maskPainter.drawRoundRect(m_mask.rect(), 1600 / m_mask.rect().width(),
119
 
                              1600 / m_mask.rect().height());
120
 
 
121
 
    setMask(m_mask);
122
 
}
123
 
 
124
 
void AddAppletVisualFeedback::displayInternal()
125
 
{
126
 
    // determine text rectangle
127
 
    QRect textRect(0, 0, 0, 0);
128
 
 
129
 
    if (m_frames < 1)
130
 
    {
131
 
        textRect.setWidth(m_richText->widthUsed());
132
 
        textRect.setHeight(m_richText->height());
133
 
 
134
 
        textRect.translate(-textRect.left(), -textRect.top());
135
 
        textRect.adjust(0, 0, 2, 2);
136
 
    }
137
 
 
138
 
    int margin = KDialog::marginHint();
139
 
    int height = qMax(m_icon.height(), textRect.height()) + 2 * margin;
140
 
    int textX = m_icon.isNull() ? margin : 2 + m_icon.width() + 2 * margin;
141
 
    int width = textX;
142
 
 
143
 
    if (m_frames < 1)
144
 
    {
145
 
        width += textRect.width() + margin;
146
 
    }
147
 
 
148
 
    // resize pixmap, mask and widget
149
 
    m_mask = QBitmap(width, height);
150
 
    m_pixmap = QPixmap(width, height);
151
 
    resize(width, height);
152
 
 
153
 
    if (m_frames < 1)
154
 
    {
155
 
        move(Plasma::popupPosition(m_direction, this, m_target));
156
 
    }
157
 
 
158
 
    // create and set transparency mask
159
 
    makeMask();
160
 
 
161
 
    // draw background
162
 
    QPainter bufferPainter(&m_pixmap);
163
 
    bufferPainter.setPen(Qt::black);
164
 
    bufferPainter.setBrush(palette().window());
165
 
    bufferPainter.drawRoundRect(0, 0, width, height,
166
 
                                1600 / width, 1600 / height);
167
 
 
168
 
    // draw icon if present
169
 
    if (!m_icon.isNull())
170
 
    {
171
 
        bufferPainter.drawPixmap(margin,
172
 
                                 margin,
173
 
                                 m_icon, 0, 0,
174
 
                                 m_icon.width(), m_icon.height());
175
 
    }
176
 
 
177
 
    if (m_frames < 1)
178
 
    {
179
 
        int textY = (height - textRect.height()) / 2;
180
 
 
181
 
        // draw text shadow
182
 
        QPalette pal = palette();
183
 
        pal.setColor(QPalette::Text, pal.window().color().dark(115));
184
 
        int shadowOffset = QApplication::isRightToLeft() ? -1 : 1;
185
 
        m_richText->draw(&bufferPainter, 5 + textX + shadowOffset,
186
 
                         textY + 1, QRect(), pal);
187
 
 
188
 
        // draw text
189
 
        pal = palette();
190
 
        m_richText->draw(&bufferPainter, 5 + textX, textY, rect(), pal);
191
 
    }
192
 
}
193
 
 
194
 
void AddAppletVisualFeedback::swoopCloser()
195
 
{
196
 
    if (m_destination.isNull() || m_frames == 0)
197
 
    {
198
 
        return;
199
 
    }
200
 
 
201
 
    QPoint loc = geometry().topLeft();
202
 
    bool isLeft = m_destination.x() > loc.x();
203
 
    if (loc.x() != m_destination.x())
204
 
    {
205
 
        int newX = loc.x() + ((m_destination.x() - loc.x()) / m_frames * 2);
206
 
        if ((m_destination.x() > newX) != isLeft)
207
 
        {
208
 
            newX = m_destination.x();
209
 
        }
210
 
        loc.setX(newX);
211
 
    }
212
 
 
213
 
    if (loc.y() != m_destination.y())
214
 
    {
215
 
        loc.setY(loc.y() + ((m_destination.y() - loc.y()) / m_frames));
216
 
    }
217
 
 
218
 
    move(loc);
219
 
    --m_frames;
220
 
 
221
 
    if (m_frames < 1)
222
 
    {
223
 
        m_moveTimer.stop();
224
 
        displayInternal();
225
 
        QTimer::singleShot(2000, this, SLOT(deleteLater()));
226
 
    }
227
 
}
228
 
 
229
 
void AddAppletVisualFeedback::internalUpdate()
230
 
{
231
 
    m_dirty = true;
232
 
    repaint();
233
 
}
234