diff options
author | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /examples/tooltip/tooltip.cpp | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'examples/tooltip/tooltip.cpp')
-rw-r--r-- | examples/tooltip/tooltip.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/examples/tooltip/tooltip.cpp b/examples/tooltip/tooltip.cpp new file mode 100644 index 0000000..0d08de0 --- /dev/null +++ b/examples/tooltip/tooltip.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "tooltip.h" +#include <qapplication.h> +#include <qpainter.h> +#include <stdlib.h> + + +DynamicTip::DynamicTip( QWidget * parent ) + : QToolTip( parent ) +{ + // no explicit initialization needed +} + + +void DynamicTip::maybeTip( const QPoint &pos ) +{ + if ( !parentWidget()->inherits( "TellMe" ) ) + return; + + QRect r( ((TellMe*)parentWidget())->tip(pos) ); + if ( !r.isValid() ) + return; + + QString s; + s.sprintf( "position: %d,%d", r.center().x(), r.center().y() ); + tip( r, s ); +} + + +TellMe::TellMe( QWidget * parent , const char * name ) + : QWidget( parent, name ) +{ + setMinimumSize( 30, 30 ); + r1 = randomRect(); + r2 = randomRect(); + r3 = randomRect(); + + t = new DynamicTip( this ); + + QToolTip::add( this, r3, "this color is called red" ); // <- helpful +} + + +TellMe::~TellMe() +{ + delete t; + t = 0; +} + + +void TellMe::paintEvent( QPaintEvent * e ) +{ + QPainter p( this ); + + // I try to be efficient here, and repaint only what's needed + + if ( e->rect().intersects( r1 ) ) { + p.setBrush( blue ); + p.drawRect( r1 ); + } + + if ( e->rect().intersects( r2 ) ) { + p.setBrush( blue ); + p.drawRect( r2 ); + } + + if ( e->rect().intersects( r3 ) ) { + p.setBrush( red ); + p.drawRect( r3 ); + } +} + + +void TellMe::mousePressEvent( QMouseEvent * e ) +{ + if ( r1.contains( e->pos() ) ) + r1 = randomRect(); + if ( r2.contains( e->pos() ) ) + r2 = randomRect(); + repaint(); +} + + +void TellMe::resizeEvent( QResizeEvent * ) +{ + if ( !rect().contains( r1 ) ) + r1 = randomRect(); + if ( !rect().contains( r2 ) ) + r2 = randomRect(); +} + + +QRect TellMe::randomRect() +{ + return QRect( ::rand() % (width() - 20), ::rand() % (height() - 20), + 20, 20 ); +} + + +QRect TellMe::tip( const QPoint & p ) +{ + if ( r1.contains( p ) ) + return r1; + else if ( r2.contains( p ) ) + return r2; + else + return QRect( 0,0, -1,-1 ); +} |