• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdeui
 

kdeui

  • kdeui
kpassivepopup.cpp
1 /*
2  * copyright : (C) 2001-2002 by Richard Moore
3  * copyright : (C) 2004-2005 by Sascha Cunz
4  * License : This file is released under the terms of the LGPL, version 2.
5  * email : rich@kde.org
6  * email : sascha.cunz@tiscali.de
7  */
8 
9 #include <kconfig.h>
10 
11 #include <tqapplication.h>
12 #include <tqlabel.h>
13 #include <tqlayout.h>
14 #include <tqtimer.h>
15 #include <tqvbox.h>
16 #include <tqpainter.h>
17 #include <tqtooltip.h>
18 #include <tqbitmap.h>
19 #include <tqpointarray.h>
20 
21 #include <kdebug.h>
22 #include <kdialog.h>
23 #include <kpixmap.h>
24 #include <kpixmapeffect.h>
25 #include <kglobalsettings.h>
26 
27 #include "config.h"
28 #ifdef Q_WS_X11
29 #include <netwm.h>
30 #endif
31 
32 #include "kpassivepopup.h"
33 #include "kpassivepopup.moc"
34 
35 class KPassivePopup::Private
36 {
37 public:
38  int popupStyle;
39  TQPointArray surround;
40  TQPoint anchor;
41  TQPoint fixedPosition;
42 };
43 
44 static const int DEFAULT_POPUP_TYPE = KPassivePopup::Boxed;
45 static const int DEFAULT_POPUP_TIME = 6*1000;
46 static const int POPUP_FLAGS = TQt::WStyle_Customize | TQt::WDestructiveClose | TQt::WX11BypassWM
47  | TQt::WStyle_StaysOnTop | TQt::WStyle_Tool | TQt::WStyle_NoBorder;
48 
49 KPassivePopup::KPassivePopup( TQWidget *parent, const char *name, WFlags f )
50  : TQFrame( 0, name, (WFlags)(f ? (int)f : POPUP_FLAGS) ),
51  window( parent ? parent->winId() : 0L ), msgView( 0 ), topLayout( 0 ),
52  hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
53  m_autoDelete( false )
54 {
55  init( DEFAULT_POPUP_TYPE );
56 }
57 
58 KPassivePopup::KPassivePopup( WId win, const char *name, WFlags f )
59  : TQFrame( 0, name, (WFlags)(f ? (int)f : POPUP_FLAGS) ),
60  window( win ), msgView( 0 ), topLayout( 0 ),
61  hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
62  m_autoDelete( false )
63 {
64  init( DEFAULT_POPUP_TYPE );
65 }
66 
67 KPassivePopup::KPassivePopup( int popupStyle, TQWidget *parent, const char *name, WFlags f )
68  : TQFrame( 0, name, (WFlags)(f ? (int)f : POPUP_FLAGS) ),
69  window( parent ? parent->winId() : 0L ), msgView( 0 ), topLayout( 0 ),
70  hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
71  m_autoDelete( false )
72 {
73  init( popupStyle );
74 }
75 
76 KPassivePopup::KPassivePopup( int popupStyle, WId win, const char *name, WFlags f )
77  : TQFrame( 0, name, (WFlags)(f ? (int)f : POPUP_FLAGS) ),
78  window( win ), msgView( 0 ), topLayout( 0 ),
79  hideDelay( DEFAULT_POPUP_TIME ), hideTimer( new TQTimer( this, "hide_timer" ) ),
80  m_autoDelete( false )
81 {
82  init( popupStyle );
83 }
84 
85 void KPassivePopup::init( int popupStyle )
86 {
87  d = new Private;
88  d->popupStyle = popupStyle;
89  if( popupStyle == Boxed )
90  {
91  setFrameStyle( TQFrame::Box| TQFrame::Plain );
92  setLineWidth( 2 );
93  }
94  else if( popupStyle == Balloon )
95  {
96  setPalette(TQToolTip::palette());
97  setAutoMask(TRUE);
98  }
99  connect( hideTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( hide() ) );
100  connect( this, TQT_SIGNAL( clicked() ), TQT_SLOT( hide() ) );
101 }
102 
103 KPassivePopup::~KPassivePopup()
104 {
105  delete d;
106 }
107 
108 void KPassivePopup::setView( TQWidget *child )
109 {
110  delete msgView;
111  msgView = child;
112 
113  delete topLayout;
114  topLayout = new TQVBoxLayout( this, d->popupStyle == Balloon ? 22 : KDialog::marginHint(), KDialog::spacingHint() );
115  topLayout->addWidget( msgView );
116  topLayout->activate();
117 }
118 
119 void KPassivePopup::setView( const TQString &caption, const TQString &text,
120  const TQPixmap &icon )
121 {
122  // kdDebug() << "KPassivePopup::setView " << caption << ", " << text << endl;
123  setView( standardView( caption, text, icon, this ) );
124 }
125 
126 TQVBox * KPassivePopup::standardView( const TQString& caption,
127  const TQString& text,
128  const TQPixmap& icon,
129  TQWidget *parent )
130 {
131  TQVBox *vb = new TQVBox( parent ? parent : this );
132  vb->setSpacing( KDialog::spacingHint() );
133 
134  TQHBox *hb=0;
135  if ( !icon.isNull() ) {
136  hb = new TQHBox( vb );
137  hb->setMargin( 0 );
138  hb->setSpacing( KDialog::spacingHint() );
139  ttlIcon = new TQLabel( hb, "title_icon" );
140  ttlIcon->setPixmap( icon );
141  ttlIcon->setAlignment( AlignLeft );
142  }
143 
144  if ( !caption.isEmpty() ) {
145  ttl = new TQLabel( caption, hb ? hb : vb, "title_label" );
146  TQFont fnt = ttl->font();
147  fnt.setBold( true );
148  ttl->setFont( fnt );
149  ttl->setAlignment( Qt::AlignHCenter );
150  if ( hb )
151  hb->setStretchFactor( ttl, 10 ); // enforce centering
152  }
153 
154  if ( !text.isEmpty() ) {
155  msg = new TQLabel( text, vb, "msg_label" );
156  msg->setAlignment( AlignLeft );
157  }
158 
159  return vb;
160 }
161 
162 void KPassivePopup::setView( const TQString &caption, const TQString &text )
163 {
164  setView( caption, text, TQPixmap() );
165 }
166 
167 void KPassivePopup::setTimeout( int delay )
168 {
169  hideDelay = delay;
170  if( hideTimer->isActive() )
171  {
172  if( delay ) {
173  hideTimer->changeInterval( delay );
174  } else {
175  hideTimer->stop();
176  }
177  }
178 }
179 
180 void KPassivePopup::setAutoDelete( bool autoDelete )
181 {
182  m_autoDelete = autoDelete;
183 }
184 
185 void KPassivePopup::mouseReleaseEvent( TQMouseEvent *e )
186 {
187  emit clicked();
188  emit clicked( e->pos() );
189 }
190 
191 //
192 // Main Implementation
193 //
194 
195 void KPassivePopup::show()
196 {
197  if ( size() != sizeHint() )
198  resize( sizeHint() );
199 
200  if ( d->fixedPosition.isNull() )
201  positionSelf();
202  else {
203  if( d->popupStyle == Balloon )
204  setAnchor( d->fixedPosition );
205  else
206  move( d->fixedPosition );
207  }
208  TQFrame::show();
209 
210  int delay = hideDelay;
211  if ( delay < 0 ) {
212  delay = DEFAULT_POPUP_TIME;
213  }
214 
215  if ( delay > 0 ) {
216  hideTimer->start( delay );
217  }
218 }
219 
220 void KPassivePopup::show(const TQPoint &p)
221 {
222  d->fixedPosition = p;
223  show();
224 }
225 
226 void KPassivePopup::hideEvent( TQHideEvent * )
227 {
228  hideTimer->stop();
229  emit( hidden( this ) );
230  if ( m_autoDelete )
231  deleteLater();
232 }
233 
234 TQRect KPassivePopup::defaultArea() const
235 {
236 #ifdef Q_WS_X11
237  NETRootInfo info( qt_xdisplay(),
238  NET::NumberOfDesktops |
239  NET::CurrentDesktop |
240  NET::WorkArea,
241  -1, false );
242  info.activate();
243  NETRect workArea = info.workArea( info.currentDesktop() );
244  TQRect r;
245  r.setRect( workArea.pos.x, workArea.pos.y, 0, 0 ); // top left
246 #else
247  // FIX IT
248  TQRect r;
249  r.setRect( 100, 100, 200, 200 ); // top left
250 #endif
251  return r;
252 }
253 
254 void KPassivePopup::positionSelf()
255 {
256  TQRect target;
257 
258 #ifdef Q_WS_X11
259  if ( !window ) {
260  target = defaultArea();
261  }
262 
263  else {
264  NETWinInfo ni( qt_xdisplay(), window, qt_xrootwin(),
265  NET::WMIconGeometry | NET::WMKDESystemTrayWinFor );
266 
267  // Figure out where to put the popup. Note that we must handle
268  // windows that skip the taskbar cleanly
269  if ( ni.kdeSystemTrayWinFor() ) {
270  NETRect frame, win;
271  ni.kdeGeometry( frame, win );
272  target.setRect( win.pos.x, win.pos.y,
273  win.size.width, win.size.height );
274  }
275  else if ( ni.state() & NET::SkipTaskbar ) {
276  target = defaultArea();
277  }
278  else {
279  NETRect r = ni.iconGeometry();
280  target.setRect( r.pos.x, r.pos.y, r.size.width, r.size.height );
281  if ( target.isNull() ) { // bogus value, use the exact position
282  NETRect dummy;
283  ni.kdeGeometry( dummy, r );
284  target.setRect( r.pos.x, r.pos.y,
285  r.size.width, r.size.height);
286  }
287  }
288  }
289 #else
290  target = defaultArea();
291 #endif
292  moveNear( target );
293 }
294 
295 void KPassivePopup::moveNear( TQRect target )
296 {
297  TQPoint pos = target.topLeft();
298  int x = pos.x();
299  int y = pos.y();
300  int w = width();
301  int h = height();
302 
303  TQRect r = KGlobalSettings::desktopGeometry(TQPoint(x+w/2,y+h/2));
304 
305  if( d->popupStyle == Balloon )
306  {
307  // find a point to anchor to
308  if( x + w > r.width() ){
309  x = x + target.width();
310  }
311 
312  if( y + h > r.height() ){
313  y = y + target.height();
314  }
315  } else
316  {
317  if ( x < r.center().x() )
318  x = x + target.width();
319  else
320  x = x - w;
321 
322  // It's apparently trying to go off screen, so display it ALL at the bottom.
323  if ( (y + h) > r.bottom() )
324  y = r.bottom() - h;
325 
326  if ( (x + w) > r.right() )
327  x = r.right() - w;
328  }
329  if ( y < r.top() )
330  y = r.top();
331 
332  if ( x < r.left() )
333  x = r.left();
334 
335  if( d->popupStyle == Balloon )
336  setAnchor( TQPoint( x, y ) );
337  else
338  move( x, y );
339 }
340 
341 void KPassivePopup::setAnchor(const TQPoint &anchor)
342 {
343  d->anchor = anchor;
344  updateMask();
345 }
346 
347 void KPassivePopup::paintEvent( TQPaintEvent* pe )
348 {
349  if( d->popupStyle == Balloon )
350  {
351  TQPainter p;
352  p.begin( this );
353  p.drawPolygon( d->surround );
354  } else
355  TQFrame::paintEvent( pe );
356 }
357 
358 void KPassivePopup::updateMask()
359 {
360  // get screen-geometry for screen our anchor is on
361  // (geometry can differ from screen to screen!
362  TQRect deskRect = KGlobalSettings::desktopGeometry(d->anchor);
363 
364  int xh = 70, xl = 40;
365  if( width() < 80 )
366  xh = xl = 40;
367  else if( width() < 110 )
368  xh = width() - 40;
369 
370  bool bottom = (d->anchor.y() + height()) > ((deskRect.y() + deskRect.height()-48));
371  bool right = (d->anchor.x() + width()) > ((deskRect.x() + deskRect.width()-48));
372 
373  TQPoint corners[4] = {
374  TQPoint( width() - 50, 10 ),
375  TQPoint( 10, 10 ),
376  TQPoint( 10, height() - 50 ),
377  TQPoint( width() - 50, height() - 50 )
378  };
379 
380  TQBitmap mask( width(), height(), true );
381  TQPainter p( &mask );
382  TQBrush brush( Qt::white, Qt::SolidPattern );
383  p.setBrush( brush );
384 
385  int i = 0, z = 0;
386  for (; i < 4; ++i) {
387  TQPointArray corner;
388  corner.makeArc(corners[i].x(), corners[i].y(), 40, 40, i * 16 * 90, 16 * 90);
389 
390  d->surround.resize( z + corner.count() );
391  for (unsigned int s = 0; s < corner.count() - 1; s++) {
392  d->surround.setPoint( z++, corner[s] );
393  }
394 
395  if (bottom && i == 2) {
396  if (right) {
397  d->surround.resize( z + 3 );
398  d->surround.setPoint( z++, TQPoint( width() - xh, height() - 11 ) );
399  d->surround.setPoint( z++, TQPoint( width() - 20, height() ) );
400  d->surround.setPoint( z++, TQPoint( width() - xl, height() - 11 ) );
401  } else {
402  d->surround.resize( z + 3 );
403  d->surround.setPoint( z++, TQPoint( xl, height() - 11 ) );
404  d->surround.setPoint( z++, TQPoint( 20, height() ) );
405  d->surround.setPoint( z++, TQPoint( xh, height() - 11 ) );
406  }
407  } else if (!bottom && i == 0) {
408  if (right) {
409  d->surround.resize( z + 3 );
410  d->surround.setPoint( z++, TQPoint( width() - xl, 10 ) );
411  d->surround.setPoint( z++, TQPoint( width() - 20, 0 ) );
412  d->surround.setPoint( z++, TQPoint( width() - xh, 10 ) );
413  } else {
414  d->surround.resize( z + 3 );
415  d->surround.setPoint( z++, TQPoint( xh, 10 ) );
416  d->surround.setPoint( z++, TQPoint( 20, 0 ) );
417  d->surround.setPoint( z++, TQPoint( xl, 10 ) );
418  }
419  }
420  }
421 
422  d->surround.resize( z + 1 );
423  d->surround.setPoint( z, d->surround[0] );
424  p.drawPolygon( d->surround );
425  setMask(mask);
426 
427  move( right ? d->anchor.x() - width() + 20 : ( d->anchor.x() < 11 ? 11 : d->anchor.x() - 20 ),
428  bottom ? d->anchor.y() - height() : ( d->anchor.y() < 11 ? 11 : d->anchor.y() ) );
429 
430  update();
431 }
432 
433 //
434 // Convenience Methods
435 //
436 
437 KPassivePopup *KPassivePopup::message( const TQString &caption, const TQString &text,
438  const TQPixmap &icon,
439  TQWidget *parent, const char *name, int timeout )
440 {
441  return message( DEFAULT_POPUP_TYPE, caption, text, icon, parent, name, timeout );
442 }
443 
444 KPassivePopup *KPassivePopup::message( const TQString &text, TQWidget *parent, const char *name )
445 {
446  return message( DEFAULT_POPUP_TYPE, TQString::null, text, TQPixmap(), parent, name );
447 }
448 
449 KPassivePopup *KPassivePopup::message( const TQString &caption, const TQString &text,
450  TQWidget *parent, const char *name )
451 {
452  return message( DEFAULT_POPUP_TYPE, caption, text, TQPixmap(), parent, name );
453 }
454 
455 KPassivePopup *KPassivePopup::message( const TQString &caption, const TQString &text,
456  const TQPixmap &icon, WId parent, const char *name, int timeout )
457 {
458  return message( DEFAULT_POPUP_TYPE, caption, text, icon, parent, name, timeout );
459 }
460 
461 KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &caption, const TQString &text,
462  const TQPixmap &icon,
463  TQWidget *parent, const char *name, int timeout )
464 {
465  KPassivePopup *pop = new KPassivePopup( popupStyle, parent, name );
466  pop->setAutoDelete( true );
467  pop->setView( caption, text, icon );
468  pop->hideDelay = timeout;
469  pop->show();
470 
471  return pop;
472 }
473 
474 KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &text, TQWidget *parent, const char *name )
475 {
476  return message( popupStyle, TQString::null, text, TQPixmap(), parent, name );
477 }
478 
479 KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &caption, const TQString &text,
480  TQWidget *parent, const char *name )
481 {
482  return message( popupStyle, caption, text, TQPixmap(), parent, name );
483 }
484 
485 KPassivePopup *KPassivePopup::message( int popupStyle, const TQString &caption, const TQString &text,
486  const TQPixmap &icon, WId parent, const char *name, int timeout )
487 {
488  KPassivePopup *pop = new KPassivePopup( popupStyle, parent, name );
489  pop->setAutoDelete( true );
490  pop->setView( caption, text, icon );
491  pop->hideDelay = timeout;
492  pop->show();
493 
494  return pop;
495 }
496 
497 // Local Variables:
498 // c-basic-offset: 4
499 // End:

kdeui

Skip menu "kdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeui

Skip menu "kdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeui by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |