You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.0 KiB
106 lines
3.0 KiB
13 years ago
|
#!/usr/bin/env python
|
||
|
|
||
|
"""**************************************************************************
|
||
|
** $Id: tooltip.py,v 1.1 2003/07/01 14:18:37 phil Exp $
|
||
|
**
|
||
|
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
||
|
**
|
||
13 years ago
|
** This file is part of an example program for TQt. This example
|
||
13 years ago
|
** program may be used, distributed and modified without limitation.
|
||
|
**
|
||
|
***************************************************************************"""
|
||
|
|
||
|
import sys
|
||
8 years ago
|
from python_tqt.qt import *
|
||
13 years ago
|
from random import random
|
||
|
|
||
13 years ago
|
class DynamicTip( TQToolTip ):
|
||
13 years ago
|
def __init__( self, parent ):
|
||
13 years ago
|
TQToolTip.__init__( self, parent )
|
||
13 years ago
|
|
||
|
def maybeTip( self, pos ):
|
||
|
#if not self.parent.inherits( "TellMe" ):
|
||
13 years ago
|
if TQToolTip(self).parentWidget().inherits( "TellMe" ) :
|
||
13 years ago
|
return
|
||
13 years ago
|
r = TQRect( TQToolTip(self).parentWidget().tip(pos) )
|
||
13 years ago
|
if not r.isValid():
|
||
|
return
|
||
|
|
||
13 years ago
|
s = TQString( "position: %d,%d" % (r.center().x(), r.center().y()) )
|
||
|
TQToolTip(self).tip( r, s )
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
class TellMe( TQWidget ):
|
||
13 years ago
|
def __init__( self, parent=None, name=None ):
|
||
13 years ago
|
TQWidget.__init__( self, parent, name )
|
||
13 years ago
|
|
||
|
self.setMinimumSize( 30, 30 )
|
||
|
self.r1 = self.randomRect()
|
||
|
self.r2 = self.randomRect()
|
||
|
self.r3 = self.randomRect()
|
||
|
|
||
|
self.t = DynamicTip( self )
|
||
|
|
||
13 years ago
|
TQToolTip.add( self, self.r3, "this color is called red" ) # <- helpful
|
||
13 years ago
|
|
||
|
def paintEvent( self, e ):
|
||
|
|
||
13 years ago
|
p = TQPainter( self )
|
||
13 years ago
|
|
||
|
# I try to be efficient here, and repaint only what's needed
|
||
|
if e.rect().intersects( self.r1 ):
|
||
13 years ago
|
p.setBrush( TQt.blue )
|
||
13 years ago
|
p.drawRect( self.r1 )
|
||
|
|
||
|
if e.rect().intersects( self.r2 ):
|
||
13 years ago
|
p.setBrush( TQt.blue )
|
||
13 years ago
|
p.drawRect( self.r2 )
|
||
|
|
||
|
if e.rect().intersects( self.r3 ):
|
||
13 years ago
|
p.setBrush( TQt.red )
|
||
13 years ago
|
p.drawRect( self.r3 )
|
||
|
|
||
|
def mousePressEvent( self, e ):
|
||
|
|
||
|
if self.r1.contains( e.pos() ):
|
||
|
self.r1 = self.randomRect()
|
||
|
if self.r2.contains( e.pos() ):
|
||
|
self.r2 = self.randomRect()
|
||
|
self.repaint()
|
||
|
|
||
|
def resizeEvent( self, e ):
|
||
|
|
||
|
if not self.rect().contains( self.r1 ):
|
||
|
self.r1 = self.randomRect()
|
||
|
if not self.rect().contains( self.r2 ):
|
||
|
self.r2 = self.randomRect()
|
||
|
|
||
|
def randomRect( self ):
|
||
3 years ago
|
return TQRect( int(random() * (self.width() - 20)), int(random() * (self.height() - 20)), 20, 20 )
|
||
13 years ago
|
|
||
|
def tip( self, p ):
|
||
|
|
||
|
if self.r1.contains( p ):
|
||
|
return self.r1
|
||
|
elif self.r2.contains( p ):
|
||
|
return self.r2
|
||
|
else:
|
||
13 years ago
|
return TQRect( 0,0, -1,-1 )
|
||
13 years ago
|
|
||
|
def __del__( self ):
|
||
|
del self.t
|
||
|
self.t = None
|
||
|
|
||
|
def main( args ):
|
||
13 years ago
|
a = TQApplication( args )
|
||
13 years ago
|
|
||
|
mw = TellMe()
|
||
13 years ago
|
mw.setCaption( "TQt Example - Dynamic Tool Tips" )
|
||
13 years ago
|
a.setMainWidget( mw )
|
||
|
mw.show()
|
||
|
|
||
|
a.exec_loop()
|
||
|
|
||
|
if __name__=="__main__":
|
||
|
main(sys.argv)
|