2
* fancypopup.cpp - the FancyPopup passive popup widget
3
* Copyright (C) 2003 Michail Pishchagin
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include "fancypopup.h"
24
#include <qapplication.h>
29
#include <qpointarray.h>
32
#include <qtoolbutton.h>
36
#include "fancylabel.h"
38
#define BUTTON_WIDTH 16
39
#define BUTTON_HEIGHT 14
41
/*static int checkComponent(int b)
49
static QColor makeColor(QColor baseColor, int percent)
51
float p = (float)percent/100;
52
int r = checkComponent( (int)(baseColor.red() + ((float)p * baseColor.red())) );
53
int g = checkComponent( (int)(baseColor.green() + ((float)p * baseColor.green())) );
54
int b = checkComponent( (int)(baseColor.blue() + ((float)p * baseColor.blue())) );
56
return QColor(r, g, b);
59
//----------------------------------------------------------------------------
60
// FancyPopup::Private
61
//----------------------------------------------------------------------------
63
class FancyPopup::Private : public QObject
67
Private(FancyPopup *p);
74
void popupDestroyed(QObject *);
78
static int hideTimeout;
79
static QColor backgroundColor;
85
PopupLayout popupLayout;
87
QPtrList<FancyPopup> prevPopups, nextPopups;
93
int FancyPopup::Private::hideTimeout = 5 * 1000; // 5 seconds
94
QColor FancyPopup::Private::backgroundColor = QColor (0x52, 0x97, 0xF9);
96
FancyPopup::Private::Private(FancyPopup *p)
101
hideTimer = new QTimer(this);
102
connect(hideTimer, SIGNAL(timeout()), SLOT(hide()));
105
FancyPopup::Private::~Private()
109
void FancyPopup::Private::hide()
114
void FancyPopup::Private::popupDestroyed(QObject *obj)
116
if ( prevPopups.contains((FancyPopup *)obj) ) {
117
prevPopups.remove((FancyPopup *)obj);
118
popup->move( position() );
120
else if ( nextPopups.contains((FancyPopup *)obj) ) {
121
nextPopups.remove((FancyPopup *)obj);
125
QPoint FancyPopup::Private::position()
127
QRect geom = qApp->desktop()->availableGeometry(popup);
128
QPoint destination(geom.x() + geom.width(), geom.y() + geom.height()); // in which corner popup should appear
130
if ( destination.y() > (qApp->desktop()->screenGeometry().height()/2) )
131
popupLayout = Private::BottomToTop;
133
popupLayout = Private::TopToBottom;
135
if ( (destination.x() + popup->width()) > (geom.x() + geom.width()) )
136
destination.setX( geom.x() + geom.width() - popup->width() );
138
if ( destination.x() < 0 )
139
destination.setX( 0 );
141
if ( (destination.y() + popup->height()) > (geom.y() + geom.height()) )
142
destination.setY( geom.y() + geom.height() - popup->height() );
144
if ( destination.y() < 0 )
145
destination.setY( 0 );
147
QPtrListIterator<FancyPopup> it ( prevPopups );
149
for ( ; (p = it.current()); ++it) {
150
destination.setY( destination.y() + popupLayout * p->height() );
156
//----------------------------------------------------------------------------
158
//----------------------------------------------------------------------------
160
static const int POPUP_FLAGS = Qt::WStyle_Customize | Qt::WDestructiveClose | Qt::WX11BypassWM
161
| Qt::WStyle_StaysOnTop | Qt::WStyle_Tool | Qt::WStyle_NoBorder;
163
FancyPopup::FancyPopup(QString title, const Icon *icon, FancyPopup *prev, bool copyIcon)
164
: QFrame( 0, 0, POPUP_FLAGS | WRepaintNoErase | WResizeNoErase | WDestructiveClose )
166
d = new Private(this);
169
QPtrListIterator<FancyPopup> it ( prev->d->prevPopups );
171
bool finished = false;
172
for ( ; !finished; ++it) {
173
popup = it.current();
179
d->prevPopups.append( popup );
180
connect(popup, SIGNAL(destroyed(QObject *)), d, SLOT(popupDestroyed(QObject *)));
183
prev->d->nextPopups.append( this );
184
connect(this, SIGNAL(destroyed(QObject *)), prev->d, SLOT(popupDestroyed(QObject *)));
187
// TODO: use darker color on popup borders
190
p.setPen( d->backgroundColor );
194
QVBoxLayout *vbox = new QVBoxLayout(this, 0, 0);
197
QHBoxLayout *tophbox = new QHBoxLayout(vbox);
198
QLabel *top1 = new QLabel(this);
199
top1->setFixedWidth(3);
200
top1->setPaletteBackgroundPixmap(back);
201
tophbox->addWidget(top1);
203
QVBoxLayout *topvbox = new QVBoxLayout(tophbox);
204
QLabel *top2 = new QLabel(this);
205
top2->setFixedHeight(1);
206
top2->setPaletteBackgroundPixmap(back);
207
topvbox->addWidget(top2);
209
QHBoxLayout *tophbox2 = new QHBoxLayout(topvbox);
210
IconLabel *titleIcon = new IconLabel(this);
211
titleIcon->setIcon(icon, copyIcon);
212
titleIcon->setPaletteBackgroundPixmap(back);
213
tophbox2->addWidget(titleIcon);
215
QLabel *top5 = new QLabel(this);
216
top5->setFixedWidth(3);
217
top5->setPaletteBackgroundPixmap(back);
218
tophbox2->addWidget(top5);
220
QLabel *titleText = new QLabel(this);
221
QString backgroundColorStr;
222
if ( (d->backgroundColor.red() + d->backgroundColor.green() + d->backgroundColor.blue())/3 > 128 )
223
backgroundColorStr = "white";
225
backgroundColorStr = "black";
226
titleText->setText( QString("<font color='%1'><nobr><b>%1</b></nobr></font>").arg( backgroundColorStr ).arg(title) );
227
titleText->setBackgroundPixmap(back);
228
titleText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
229
tophbox2->addWidget(titleText);
231
QWidget *closeButtonBack = new QWidget(this);
232
closeButtonBack->setPaletteBackgroundPixmap( back );
233
tophbox2->addWidget(closeButtonBack);
235
QVBoxLayout *closeButtonBackLayout = new QVBoxLayout(closeButtonBack);
236
closeButtonBackLayout->addStretch();
238
QToolButton *closeButton = new QToolButton( closeButtonBack, "close" );
239
QToolTip::add( closeButton, tr( "Close" ) );
240
closeButtonBackLayout->addWidget( closeButton );
241
closeButtonBackLayout->addStretch();
242
closeButton->setFocusPolicy( NoFocus );
243
closeButton->setIconSet( style().stylePixmap(QStyle::SP_TitleBarCloseButton) );
244
closeButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
245
connect(closeButton, SIGNAL(clicked()), SLOT(hide()));
247
QLabel *top3 = new QLabel(this);
248
top3->setFixedHeight(1);
249
top3->setPaletteBackgroundPixmap(back);
250
topvbox->addWidget(top3);
252
QLabel *top4 = new QLabel(this);
253
top4->setFixedWidth(3);
254
top4->setPaletteBackgroundPixmap(back);
255
tophbox->addWidget(top4);
258
QHBoxLayout *middlehbox = new QHBoxLayout(vbox);
259
QLabel *middle1 = new QLabel(this);
260
middle1->setFixedWidth(4);
261
middle1->setPaletteBackgroundPixmap(back);
262
middlehbox->addWidget(middle1);
264
middlehbox->addSpacing(5);
265
QVBoxLayout *middlevbox = new QVBoxLayout(middlehbox);
266
middlevbox->addSpacing(5);
267
d->layout = middlevbox; // we'll add more items later in addLayout()
268
middlehbox->addSpacing(5);
270
QLabel *middle3 = new QLabel(this);
271
middle3->setFixedWidth(4);
272
middle3->setPaletteBackgroundPixmap(back);
273
middlehbox->addWidget(middle3);
276
QHBoxLayout *bottomhbox = new QHBoxLayout(vbox);
277
QLabel *bottom1 = new QLabel(this);
278
bottom1->setFixedSize( 4, 4 );
279
bottom1->setPaletteBackgroundPixmap(back);
280
bottomhbox->addWidget(bottom1);
282
QLabel *bottom2 = new QLabel(this);
283
bottom2->setFixedHeight(4);
284
bottom2->setPaletteBackgroundPixmap(back);
285
bottomhbox->addWidget(bottom2);
287
QLabel *bottom3 = new QLabel(this);
288
bottom3->setFixedSize( 4, 4 );
289
bottom3->setPaletteBackgroundPixmap(back);
290
bottomhbox->addWidget(bottom3);
293
FancyPopup::~FancyPopup()
297
void FancyPopup::addLayout(QLayout *layout, int stretch)
299
d->layout->addLayout(layout, stretch);
300
d->layout->addSpacing(5);
303
void FancyPopup::show()
305
if ( size() != sizeHint() )
306
resize( sizeHint() ); // minimumSizeHint()
309
move ( d->position() );
312
d->hideTimer->start( d->hideTimeout );
316
void FancyPopup::hideEvent(QHideEvent *e)
318
d->hideTimer->stop();
321
QFrame::hideEvent(e);
324
void FancyPopup::mouseReleaseEvent(QMouseEvent *e)
326
QWidget *closeButton = (QWidget *)child("closeButton");
328
QPoint p = closeButton->mapFromParent( e->pos() );
329
if ( p.x() >= 0 && p.y() >= 0 && p.x() < closeButton->width() && p.y() < closeButton->height() )
330
QFrame::mouseReleaseEvent(e);
334
emit clicked((int)e->button());
337
void FancyPopup::restartHideTimer()
339
d->hideTimer->stop();
340
d->hideTimer->start( d->hideTimeout );
343
void FancyPopup::setHideTimeout(int time)
345
FancyPopup::Private::hideTimeout = time;
348
void FancyPopup::setBorderColor(QColor c)
350
FancyPopup::Private::backgroundColor = c;
353
#include "fancypopup.moc"