summaryrefslogtreecommitdiffstats
path: root/examples3/menu.py
blob: 31fadd95ae76b7e6da45bfe027b62981d47c918a (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python

#****************************************************************************
#** $Id: menu.py,v 1.3 2003/06/06 12:59:26 phil Exp $
#**
#** Copyright (C) 1992-1998 Troll Tech AS.  All rights reserved.
#**
#** This file is part of an example program for PyTQt.  This example
#** program may be used, distributed and modified without limitation.
#**
#*****************************************************************************/

import sys, string
from python_tqt.qt import *

TRUE  = 1
FALSE = 0

# XPM
p1_xpm = [
"16 16 3 1",
" 	c None",
".	c #000000000000",
"X	c #FFFFFFFF0000",
"                ",
"                ",
"         ....   ",
"        .XXXX.  ",
" .............. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .............. ",
"                "
]

# XPM
p2_xpm = [
"16 16 3 1",
" 	c None",
".	c #000000000000",
"X	c #FFFFFFFFFFFF",
"                ",
"   ......       ",
"   .XXX.X.      ",
"   .XXX.XX.     ",
"   .XXX.XXX.    ",
"   .XXX.....    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .........    ",
"                ",
"                "
]

# XPM
p3_xpm = [
"16 16 3 1",
" 	c None",
".	c #000000000000",
"X	c #FFFFFFFFFFFF",
"                ",
"                ",
"   .........    ",
"  ...........   ",
"  ........ ..   ",
"  ...........   ",
"  ...........   ",
"  ...........   ",
"  ...........   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"   .........    ",
"                ",
"                "
]


# Auxiliary class to provide fancy menu items with different fonts.
# Used for the "bold" and "underline" menu items in the options menu.

class MyMenuItem( TQCustomMenuItem ):
    def __init__( self, s, f ):
        TQCustomMenuItem.__init__( self )
        self.string = s
        self.font   = f

    def paint( self, p, cg, act, enabled, x, y, w, h ) :
        p.setFont ( self.font )
        p.drawText( x, y, w, h, TQt.AlignLeft | TQt.AlignVCenter | TQt.ShowPrefix | TQt.DontClip, self.string )

    def sizeHint( self ):
        return TQFontMetrics( self.font ).size( TQt.AlignLeft | TQt.AlignVCenter | TQt.ShowPrefix | TQt.DontClip,  self.string )


# Implementation of MenuExample class

class MenuExample( TQWidget ):
    def __init__( self, parent=None, name=None ):
        TQWidget.__init__( self, parent, name )
        self.p1 = TQIconSet( TQPixmap ( p1_xpm ) )
        self.p2 = TQIconSet( TQPixmap ( p2_xpm ) )
        self.p3 = TQIconSet( TQPixmap ( p3_xpm ) )

        self.printer = TQPopupMenu( self )
        self.printer.insertTearOffHandle()
        self.printer.insertItem( "&Print to printer", self.printDoc )
        self.printer.insertItem( "Print to &file", self.printFile )
        self.printer.insertItem( "Print to fa&x", self.fax )
        self.printer.insertSeparator()
        self.printer.insertItem( "Printer &Setup", self.printerSetup )

        self.file = TQPopupMenu( self )
        self.file.insertItem( self.p1, "&Open", self.open, TQt.CTRL+TQt.Key_O )
        self.file.insertItem( self.p2, "&New",  self.news, TQt.CTRL+TQt.Key_N )
        self.file.insertItem( self.p3, "&Save", self.save, TQt.CTRL+TQt.Key_S )
        self.file.insertItem( "&Close", self.closeDoc, TQt.CTRL+TQt.Key_W )
        self.file.insertSeparator()
        self.file.insertItem( "&Print", self.printer, TQt.CTRL+TQt.Key_P )
        self.file.insertSeparator()
        self.file.insertItem( "E&xit",  tqApp, SLOT( "quit()" ), TQt.CTRL+TQt.Key_Q )

        self.edit = TQPopupMenu( self )
        undoID = self.edit.insertItem( "&Undo", self.undo )
        redoID = self.edit.insertItem( "&Redo", self.redo )
        self.edit.setItemEnabled( undoID, FALSE )
        self.edit.setItemEnabled( redoID, FALSE )

        self.options = TQPopupMenu( self )
        self.options.insertTearOffHandle()
        self.options.setCaption( "Options" )
        self.options.insertItem( "&Normal Font", self.normal )
        self.options.insertSeparator()

        self.options.polish()  # adjust system settings
        f = self.options.font()
        f.setBold( TRUE )
        self.boldID = self.options.insertItem( MyMenuItem( "Bold", f ) )
        self.options.setAccel( TQt.CTRL+TQt.Key_B, self.boldID )
        self.options.connectItem( self.boldID, self.bold )

        f = self.options.font()
        f.setUnderline( TRUE )
        self.underlineID = self.options.insertItem( MyMenuItem( "Underline", f ) )
        self.options.setAccel( TQt.CTRL+TQt.Key_U, self.underlineID )
        self.options.connectItem( self.underlineID, self.underline )

        self.isBold = FALSE
        self.isUnderline = FALSE
        self.options.setCheckable( TRUE )

        self.help = TQPopupMenu( self )
        self.help.insertItem( "&About", self.about, TQt.CTRL+TQt.Key_H )
        self.help.insertItem( "About &TQt", self.aboutTQt )

        self.menu = TQMenuBar( self )
        self.menu.insertItem( "&File", self.file )
        self.menu.insertItem( "&Edit", self.edit )
        self.menu.insertItem( "&Options", self.options )
        self.menu.insertSeparator()
        self.menu.insertItem( "&Help", self.help )
        self.menu.setSeparator( TQMenuBar.InWindowsStyle )

        msg = TQLabel( self )
        msg.setText( "A context menu is available.\n"
                     "Invoke it by right-clicking or by"
                     " pressing the 'context' button." )
        msg.setGeometry( 0, self.height() - 60, self.width(), 60 )
        msg.setAlignment( TQt.AlignCenter )

        self.label = TQLabel( self )
        self.label.setGeometry( 20, self.rect().center().y()-20, self.width()-40, 40 )
        self.label.setFrameStyle( TQFrame.Box | TQFrame.Raised )
        self.label.setLineWidth( 1 )
        self.label.setAlignment( TQt.AlignCenter )

        self.connect( self, PYSIGNAL( "explain" ), self.label, SLOT( "setText(const TQString &" ) )

        self.setMinimumSize( 100, 80 )
        self.setFocusPolicy( TQWidget.ClickFocus )

    def contextMenuEvent(self, cmev):
        contextMenu = TQPopupMenu( self )
        caption = TQLabel( "<font color=darkblue><u><b>Context Menu</b></u></font>", self )
        caption.setAlignment( TQt.AlignCenter )
        contextMenu.insertItem( caption )
        contextMenu.insertItem( "&New", self.news, TQt.CTRL+TQt.Key_N )
        contextMenu.insertItem( "&Open...", self.open, TQt.CTRL+TQt.Key_O )
        contextMenu.insertItem( "&Save", self.save, TQt.CTRL+TQt.Key_S )
        submenu = TQPopupMenu( self )
        submenu.insertItem( "&Print to printer", self.printDoc )
        submenu.insertItem( "Print to &file", self.printFile )
        submenu.insertItem( "Print to fa&x", self.fax )
        contextMenu.insertItem( "&Print", submenu )
        contextMenu.exec_loop( TQCursor.pos() )

    def open( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Open selected", ) )

    def news( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/New selected", ) )

    def save( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Save selected", ) )

    def closeDoc( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Close selected", ) )

    def undo( self ):
        self.emit( PYSIGNAL( "explain" ), ( "Edit/Undo selected", ) )

    def redo( self ):
        self.emit( PYSIGNAL( "explain" ), ( "Edit/Redo selected", ) )

    def normal( self ):
        self.isBold = FALSE
        self.isUnderline = FALSE
        font = TQFont()
        self.label.setFont( font )
        self.options.setItemChecked( self.boldID, self.isBold )
        self.options.setItemChecked( self.underlineID, self.isUnderline )
        self.emit( PYSIGNAL( "explain" ), ( "Options/Normal selected", ) )

    def bold( self ):
        self.isBold = not self.isBold
        font = TQFont()
        font.setBold( self.isBold )
        font.setUnderline( self.isUnderline )
        self.label.setFont( font )
        self.options.setItemChecked( self.boldID, self.isBold )
        self.emit( PYSIGNAL( "explain" ), ( "Options/Bold selected", ) )

    def underline( self ):
        self.isUnderline = not self.isUnderline
        font = TQFont()
        font.setBold( self.isBold )
        font.setUnderline( self.isUnderline )
        self.label.setFont( font )
        self.options.setItemChecked( self.underlineID, self.isUnderline )
        self.emit( PYSIGNAL("explain"), ( "Options/Underline selected", ))

    def about( self ):
        TQMessageBox.about( self, "TQt Menu Example",
                       "This example demonstrates simple use of TQt menus.\n"
                       "You can cut and paste lines from it to your own\n"
                       "programs." )

    def aboutTQt( self ):
        TQMessageBox.aboutTQt( self, "TQt Menu Example" )

    def printDoc( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Print selected", ) )

    def printFile( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Print To File selected", ) )

    def fax( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Print To Fax selected", ) )

    def printerSetup( self ):
        self.emit( PYSIGNAL( "explain" ), ( "File/Printer/Printer Setup selected", ) )

    def resizeEvent( self, ev ):
        self.label.setGeometry( 20, self.rect().center().y()-20, self.width()-40, 40 )


a = TQApplication( sys.argv )
m = MenuExample()

m.setCaption( "TQt Examples - Menus" )
a.setMainWidget( m )
m.show()
a.exec_loop()