~ubuntu-branches/ubuntu/feisty/psi/feisty

« back to all changes in this revision

Viewing changes to cutestuff/qpassivepopup/qpassivepopup.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2005-09-14 16:33:49 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050914163349-3zacov4afysz5cw5
Tags: 0.9.3-2ubuntu1
* Sync with debian
* Applied patch to psi.desktop to start psi without gpg-agent use (known
  issue)
* Updated README.Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///This is blatently a copy of KPassivePopup I've ripped from KDE, original License below, (Kevin Smith):
2
 
/*
3
 
 *   copyright            : (C) 2001-2002 by Richard Moore
4
 
 *   License              : This file is released under the terms of the LGPL, version 2.
5
 
 *   email                : rich@kde.org
6
 
 */
7
 
 
8
 
//qpen outline?
9
 
 
10
 
#include <qapplication.h>
11
 
#include <qlabel.h>
12
 
#include <qlayout.h>
13
 
#include <qtimer.h>
14
 
#include <qvbox.h>
15
 
#include <qpixmap.h>
16
 
#include <qtooltip.h>
17
 
#include <qpointarray.h>
18
 
 
19
 
#include "qpassivepopup.h"
20
 
 
21
 
static const int DEFAULT_POPUP_TIME = 6*1000;
22
 
static const int POPUP_FLAGS = Qt::WStyle_Customize | Qt::WDestructiveClose | Qt::WX11BypassWM
23
 
                             | Qt::WStyle_StaysOnTop | Qt::WStyle_Tool | Qt::WStyle_NoBorder;
24
 
 
25
 
 
26
 
QPassivePopup::QPassivePopup( QWidget *parent, const char *name, WFlags f )
27
 
    : QFrame( 0, name, f | POPUP_FLAGS ),
28
 
      window( parent->winId() ), msgView( 0 ), layout( 0 ),
29
 
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new QTimer( this, "hide_timer" ) ), m_autoDelete( false ), d( 0 )
30
 
{
31
 
    init();
32
 
}
33
 
 
34
 
QPassivePopup::QPassivePopup( const char *name, WFlags f )
35
 
    : QFrame( 0, name, f | POPUP_FLAGS ),
36
 
      window( 0 ), msgView( 0 ), layout( 0 ),
37
 
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new QTimer( this, "hide_timer" ) ), m_autoDelete( false ), d( 0 )
38
 
{
39
 
    init();
40
 
}
41
 
 
42
 
QPassivePopup::QPassivePopup( WId win, const char *name, WFlags f )
43
 
    : QFrame( 0, name, f | POPUP_FLAGS ),
44
 
      window( win ), msgView( 0 ), layout( 0 ),
45
 
      hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new QTimer( this, "hide_timer" ) ), m_autoDelete( false ), d( 0 )
46
 
{
47
 
    init();
48
 
}
49
 
 
50
 
void QPassivePopup::init()
51
 
{
52
 
    setFrameStyle( QFrame::Box| QFrame::Plain );
53
 
    setLineWidth( 2 );
54
 
    connect( hideTimer, SIGNAL( timeout() ), SLOT( hide() ) );
55
 
    connect( this, SIGNAL( clicked() ), SLOT( hide() ) );
56
 
}
57
 
 
58
 
QPassivePopup::~QPassivePopup()
59
 
{
60
 
}
61
 
 
62
 
void QPassivePopup::setView( QWidget *child )
63
 
{
64
 
    delete msgView;
65
 
    msgView = child;
66
 
    
67
 
    delete layout;
68
 
    //layout = new QVBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() );//KEVIN
69
 
 
70
 
    this->setFrameStyle(PopupPanel);
71
 
    this->setFrameShadow(Raised);
72
 
    this->setLineWidth(3);
73
 
    this->setPalette(QToolTip::palette());
74
 
    this->setAutoMask(true);
75
 
    
76
 
    layout = new QVBoxLayout( this );//KEVIN
77
 
    layout->addWidget( msgView );
78
 
    layout->activate();
79
 
}
80
 
 
81
 
void QPassivePopup::setView( const QString &caption, const QString &text,
82
 
                             const QPixmap &icon )
83
 
{
84
 
    QVBox *vb = new QVBox( this );
85
 
    //vb->setSpacing( KDialog::spacingHint() ); //KEVIN
86
 
    vb->setSpacing(5);//KEVIN
87
 
    QHBox *hb=0;
88
 
    if ( !icon.isNull() ) {
89
 
        hb = new QHBox( vb );
90
 
        //hb->setMargin( KDialog::marginHint() );//KEVIN
91
 
        //hb->setSpacing( KDialog::spacingHint() );//KEVIN
92
 
        hb->setMargin( 5 );//KEVIN
93
 
        hb->setSpacing( 5 );//KEVIN
94
 
        ttlIcon = new QLabel( hb, "title_icon" );
95
 
        ttlIcon->setPixmap( icon );
96
 
    }
97
 
 
98
 
    if ( !caption.isNull() ) {
99
 
        ttl = new QLabel( caption, hb ? hb : vb, "title_label" );
100
 
        QFont fnt = ttl->font();
101
 
        fnt.setBold( true );
102
 
        ttl->setFont( fnt );
103
 
        ttl->setAlignment( Qt::AlignHCenter );
104
 
    }
105
 
 
106
 
    //KEVIN thinks that text should be centred by default.
107
 
    msg = new QLabel( text, vb, "msg_label" );
108
 
    msg->setAlignment(Qt::AlignHCenter);
109
 
    setView( vb );
110
 
}
111
 
 
112
 
void QPassivePopup::setView( const QString &caption, const QString &text )
113
 
{
114
 
    setView( caption, text, QPixmap() );
115
 
}
116
 
 
117
 
void QPassivePopup::setTimeout( int delay )
118
 
{
119
 
    hideDelay = delay;
120
 
        if( hideTimer->isActive() )
121
 
                hideTimer->changeInterval( delay );
122
 
}
123
 
 
124
 
void QPassivePopup::setAutoDelete( bool autoDelete )
125
 
{
126
 
    m_autoDelete = autoDelete;
127
 
}
128
 
 
129
 
void QPassivePopup::mouseReleaseEvent( QMouseEvent *e )
130
 
{
131
 
    emit clicked();
132
 
    emit clicked( e->pos() );
133
 
}
134
 
 
135
 
//
136
 
// Main Implementation
137
 
//
138
 
 
139
 
void QPassivePopup::show()
140
 
{
141
 
    if ( size() != sizeHint() )
142
 
        resize( sizeHint() );
143
 
 
144
 
    positionSelf();
145
 
    QFrame::show();
146
 
 
147
 
    int delay = hideDelay;
148
 
    if ( delay < 0 )
149
 
        delay = DEFAULT_POPUP_TIME;
150
 
 
151
 
    if ( delay > 0 ) {
152
 
        hideTimer->start( delay );
153
 
    }
154
 
}
155
 
 
156
 
void QPassivePopup::hideEvent( QHideEvent * )
157
 
{
158
 
    hideTimer->stop();
159
 
    if ( m_autoDelete )
160
 
        deleteLater();
161
 
}
162
 
 
163
 
void QPassivePopup::positionSelf()
164
 
{
165
 
    //This is all KDE-specifix stuff, I've had to comment out (KEVIN)
166
 
    /*
167
 
    NETWinInfo ni( qt_xdisplay(), window, qt_xrootwin(),
168
 
                   NET::WMIconGeometry | NET::WMKDESystemTrayWinFor );
169
 
 
170
 
    // Figure out where to put the popup. Note that we must handle
171
 
    // windows that skip the taskbar cleanly
172
 
    QRect target;
173
 
    if ( ni.kdeSystemTrayWinFor() ) {
174
 
        NETRect frame, win;
175
 
        ni.kdeGeometry( frame, win );
176
 
        target.setRect( win.pos.x, win.pos.y, win.size.width, win.size.height );
177
 
    }
178
 
    else if ( ni.state() & NET::SkipTaskbar ) {
179
 
        target.setRect( 0, 0, 0, 0 );
180
 
    }
181
 
    else {
182
 
        NETRect r = ni.iconGeometry();
183
 
        target.setRect( r.pos.x, r.pos.y, r.size.width, r.size.height );
184
 
    }
185
 
    */
186
 
    //KEVIN Hack added
187
 
 
188
 
    QRect target;
189
 
    target.setRect((qApp->desktop()->width()/20)*19,qApp->desktop()->height(),0,0);
190
 
    
191
 
    //</hack>
192
 
    moveNear( target );
193
 
}
194
 
 
195
 
void QPassivePopup::moveNear( QRect target )
196
 
{
197
 
    QPoint pos = target.topLeft();
198
 
    int x = pos.x();
199
 
    int y = pos.y();
200
 
    int w = width();
201
 
    int h = height();
202
 
 
203
 
    if ( x < ( qApp->desktop()->width() / 2 ) )
204
 
        x = x + target.width();
205
 
    else
206
 
        x = x - w;
207
 
 
208
 
    // It's apparently trying to go off screen, so display it ALL at the bottom.
209
 
    if ( (y + h) > qApp->desktop()->height() )
210
 
        y = qApp->desktop()->height() - h;
211
 
 
212
 
#ifdef OLD_BITS
213
 
    if ( (x - w) >= 0  )
214
 
        x = x - w;
215
 
#endif
216
 
 
217
 
    move( x, y );
218
 
}
219
 
 
220
 
//
221
 
// Convenience Methods
222
 
//
223
 
 
224
 
QPassivePopup *QPassivePopup::message( const QString &caption, const QString &text,
225
 
                                       const QPixmap &icon,
226
 
                                       QWidget *parent, const char *name, int timeout )
227
 
{
228
 
    QPassivePopup *pop = new QPassivePopup( parent, name );
229
 
    pop->setAutoDelete( true );
230
 
    pop->setView( caption, text, icon );
231
 
    pop->hideDelay = timeout;
232
 
    pop->show();
233
 
 
234
 
    return pop;
235
 
}
236
 
 
237
 
QPassivePopup *QPassivePopup::message( const QString &text, QWidget *parent, const char *name )
238
 
{
239
 
    return message( QString::null, text, QPixmap(), parent, name );
240
 
}
241
 
 
242
 
QPassivePopup *QPassivePopup::message( const QString &caption, const QString &text,
243
 
                                       QWidget *parent, const char *name )
244
 
{
245
 
    return message( caption, text, QPixmap(), parent, name );
246
 
}
247
 
 
248
 
QPassivePopup *QPassivePopup::message( const QString &caption, const QString &text,
249
 
                                       const QPixmap &icon, WId parent, const char *name, int timeout )
250
 
{
251
 
    QPassivePopup *pop = new QPassivePopup( parent, name );
252
 
    pop->setAutoDelete( true );
253
 
 
254
 
    pop->setView( caption, text, icon );
255
 
    pop->hideDelay = timeout;
256
 
    pop->show();
257
 
    return pop;
258
 
}
259
 
 
260
 
QString *QPassivePopup::splitMessage(QString caption, uint splitsize, uint maxsize){
261
 
    QString *splitcaption;
262
 
    printf("Splitting message of length %d with splitsize %d and maxsize %d \n", caption.length(), splitsize, maxsize);
263
 
    if (caption.length()>maxsize){
264
 
        splitcaption=new QString(caption.left(maxsize));}
265
 
    else{
266
 
        splitcaption=new QString(caption);}
267
 
    for (uint i=splitsize;i<caption.length();i+=splitsize){
268
 
        bool truncated=false;
269
 
        for (uint j=i-5;j<i+5;j++){
270
 
            if (splitcaption->at(j).isSpace()) {
271
 
                QString* old=splitcaption;
272
 
                printf("Split on char %c, number %d\n", splitcaption->at(j).latin1(),j);
273
 
                splitcaption=new QString(splitcaption->left(j)+"\n"+splitcaption->right(splitcaption->length()-j));
274
 
                truncated=true;
275
 
                delete old;
276
 
                i=j;
277
 
                break;
278
 
            }
279
 
        }
280
 
        if (!truncated){
281
 
            QString *old=splitcaption;
282
 
            printf("forced to split at %d\n",i);
283
 
            splitcaption=new QString(splitcaption->left(i)+"-\n "+splitcaption->right(splitcaption->length()-i));
284
 
            delete old;
285
 
        }
286
 
    }
287
 
    return splitcaption;
288
 
}
289
 
 
290
 
void QPassivePopup::updateMask()
291
 
{
292
 
        //QRegion mask(10, 10, width() - 20, height() - 20);
293
 
        QRegion mask(0, 0, width(), height());
294
 
        /*QPoint corners[8] = {
295
 
                QPoint(width() - 50, 10),
296
 
                QPoint(10, 10),
297
 
                QPoint(10, height() - 50),
298
 
                QPoint(width() - 50, height() - 50),
299
 
                QPoint(width() - 10, 10),
300
 
                QPoint(10, 10),
301
 
                QPoint(10, height() - 10),
302
 
                QPoint(width() - 10, height() - 10)
303
 
        };*/
304
 
        QPoint corners[8] = {
305
 
                QPoint(width()-40, 0),
306
 
                QPoint(0, 0),
307
 
                QPoint(0, height()-40),
308
 
                QPoint(width()-40, height()-40),
309
 
                QPoint(width(), 0),
310
 
                QPoint(0, 0),
311
 
                QPoint(0, height()),
312
 
                QPoint(width(), height())
313
 
        };
314
 
        for (int i = 0; i < 2; ++i)
315
 
        {
316
 
                QPointArray corner;
317
 
                corner.makeArc(corners[i].x(), corners[i].y(), 40, 40, i * 16 * 90, 16 * 90);
318
 
                corner.resize(corner.size() + 1);
319
 
                corner.setPoint(corner.size() - 1, corners[i + 4]);
320
 
                mask -= corner;
321
 
        }
322
 
        bool bottom, right;
323
 
        QDesktopWidget* tmp = QApplication::desktop();
324
 
        QRect deskRect;
325
 
        // get screen-geometry for screen our anchor is on
326
 
        // (geometry can differ from screen to screen!
327
 
        deskRect = tmp->screenGeometry( tmp->screenNumber(this->pos()) );
328
 
        bottom = this->pos().y() + height() > deskRect.height() - 48;
329
 
        right = this->pos().x() + width() > deskRect.width() - 48;
330
 
 
331
 
        /*QPointArray arrow(4);
332
 
        arrow.setPoint(0, QPoint(right ? width() : 0, bottom ? height() : 0));
333
 
        arrow.setPoint(1, QPoint(right ? width() - 10 : 10, bottom ? height() - 30 : 30));
334
 
        arrow.setPoint(2, QPoint(right ? width() - 30 : 30, bottom ? height() - 10 : 10));
335
 
        arrow.setPoint(3, arrow[0]);
336
 
        mask += arrow;*/
337
 
        setMask(mask);
338
 
        /*move(right ? this->pos().x() - width() : this->pos().x(),
339
 
          bottom ? this->pos().y() - height() : this->pos().y());*/
340
 
}
341