summaryrefslogtreecommitdiffstats
path: root/examples/tooltip/tooltip.cpp
blob: 628c4d94f865bb49c41220515675f65cee9c3560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  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( TQWidget * parent )
    : TQToolTip( parent )
{
    // no explicit initialization needed
}


void DynamicTip::maybeTip( const TQPoint &pos )
{
    if ( !parentWidget()->inherits( "TellMe" ) )
	return;

    TQRect r( ((TellMe*)parentWidget())->tip(pos) );
    if ( !r.isValid() )
	return;

    TQString s;
    s.sprintf( "position: %d,%d", r.center().x(), r.center().y() );
    tip( r, s );
}


TellMe::TellMe( TQWidget * parent , const char * name  )
    : TQWidget( parent, name )
{
    setMinimumSize( 30, 30 );
    r1 = randomRect();
    r2 = randomRect();
    r3 = randomRect();

    t = new DynamicTip( this );

    TQToolTip::add( this, r3, "this color is called red" ); // <- helpful
}


TellMe::~TellMe()
{
    delete t;
    t = 0;
}


void TellMe::paintEvent( TQPaintEvent * e )
{
    TQPainter 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( TQMouseEvent * e )
{
    if ( r1.contains( e->pos() ) )
	r1 = randomRect();
    if ( r2.contains( e->pos() ) )
	r2 = randomRect();
    repaint();
}


void TellMe::resizeEvent( TQResizeEvent * )
{
    if ( !rect().contains( r1 ) )
	 r1 = randomRect();
    if ( !rect().contains( r2 ) )
	 r2 = randomRect();
}


TQRect TellMe::randomRect()
{
    return TQRect( ::rand() % (width() - 20), ::rand() % (height() - 20),
		  20, 20 );
}


TQRect TellMe::tip( const TQPoint & p )
{
    if ( r1.contains( p ) )
	return r1;
    else if ( r2.contains( p ) )
	return r2;
    else
	return TQRect( 0,0, -1,-1 );
}