summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qtscribble/scribble.rb
blob: a2ce927b97dab1cfd26990b9ca78d0571a596af3 (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
#!/usr/bin/env ruby -w

 #
 # A class that lets the user draw with the mouse. The
 # window knows how to redraw itself.
 #

require 'Qt'
	
	class ScribbleArea < TQt::Widget
	
		slots "setColor(TQColor)", "slotLoad(const TQString&)", "slotSave(const TQString&)", "slotClearArea()"

		  #
		  # The constructor. Initializes the member variables.
		  #
		def initialize()
			super
			# initialize member variables
			@_buffer = TQt::Pixmap.new()
			@_last = TQt::Point.new()
			@_currentcolor = black
			
			# don't blank the window before repainting
			setBackgroundMode( NoBackground )
			
			# create a pop-up menu
			@_popupmenu = TQt::PopupMenu.new()
			@_popupmenu.insertItem( "&Clear", self, TQ_SLOT( "slotClearArea()" ) )
		end
		
		  #
		  # This slot sets the curren color for the scribble area. It will be
		  # connected with the colorChanged( TQt::Color ) signal from the
		  # ScribbleWindow.
		  #
		def setColor( new_color )
			@_currentcolor = new_color
		end
		
		 #
		 # This slot clears the drawing area by filling the off-screen buffer with
		 # white and copying it over to the window.
		 #
		def slotClearArea()
			# fill the off screen buffer with plain white
			@_buffer.fill( white )
			
			# and copy it over to the window
			bitBlt( self, 0, 0, @_buffer )
		end
		
		
		  #
		  # This method does the actual loading. It relies on TQt::Pixmap (and the
		  # underlying I/O machinery) to determine the filetype.
		  #
		def slotLoad( filename )
			if !@_buffer.load( filename )
				TQt::MessageBox.warning( nil, "Load error", "Could not load file" )
			end
				
			repaint()  # refresh the window
		end
		
		
		  #
		 # This method does the actual saving. We hard-code the file type as
		 # BMP. Unix users might want to replace this with something like XPM.
		 #
		def slotSave( filename )
			if !@_buffer.save( filename, "BMP" )
				TQt::MessageBox.warning( nil, "Save error", "Could not save file" )
			end
		end
		
		
		  #
		  # This method is called whenever the user presses the
		  # mouse over the window. It just records the position of the mouse
		  # at the time of the click.
		  #
		def mousePressEvent(event)
			if event.button() == RightButton
				@_popupmenu.exec( TQt::Cursor.pos() )
			else
				@_last = event.pos()	# retrieve the coordinates from the event
			end
		end
		
		
		  #
		  # The method is called whenever the usr moves the mouse
		  # while the mouse button is pressed. If we had called
		  # setMouseTracking(true) before, the method would also be called
		  # when the mouse was moved with any button pressed. We know that
		  # we haven't, and thus don't have to check whether any buttons are
		  # pressed.
		  #
		def mouseMoveEvent(event)
			# create a TQt::Painter object for drawing onto the window
			windowpainter = TQt::Painter.new()
			# and another TQt::Painter object for drawing int an off-screen pixmap
			bufferpainter = TQt::Painter.new()
			
			# start painting
			windowpainter.begin( self ) # This painter paints onto the window
			bufferpainter.begin( @_buffer )  # and this one paints in the buffer
		
			# set a standard pen with the currently selected color
			windowpainter.setPen( @_currentcolor )
			bufferpainter.setPen( @_currentcolor )
		
			# draw a line in both the window and the buffer
			windowpainter.drawLine( @_last, event.pos() )
			bufferpainter.drawLine( @_last, event.pos() )
		
			# done with painting
			windowpainter.end()
			bufferpainter.end()
		
			# remember the current mouse position
			@_last = event.pos()						
		end
		
		  #
		  # This method is called whenever the widget needs
		  # painting, for example when it has been obscured and then revealed again.
		  #
		def paintEvent(event)
			bitBlt(self, 0, 0, @_buffer)
		end
	
		  #
		  # This method get called whenever the widget needs
		  # painting, for example, when it has been obscured and then revealed again.
		  #
		def resizeEvent(event)
			save = TQt::Pixmap.new( @_buffer )
			@_buffer.resize( event.size() )
			@_buffer.fill( white )
			bitBlt( @_buffer, 0, 0, save )
		end
	end

class ScribbleWindow < TQt::Widget

	slots "slotAbout()", "slotAboutQt()", "slotColorMenu(int)", "slotLoad()", "slotSave()"
	signals "colorChanged(TQColor)", "load(const TQString&)", "save(const TQString&)"
	
	COLOR_MENU_ID_BLACK = 0
	COLOR_MENU_ID_RED = 1
	COLOR_MENU_ID_BLUE = 2
	COLOR_MENU_ID_GREEN = 3
	COLOR_MENU_ID_YELLOW = 4
	
	def initialize()
		super
		# The next lines build the menu bar. We first create the menus
		# one by one, then add them to the menu bar. #
		@_filemenu = TQt::PopupMenu.new()  # create a file menu
		@_filemenu.insertItem( "&Load", self, TQ_SLOT( "slotLoad()" ) )
		@_filemenu.insertItem( "&Save", self, TQ_SLOT( "slotSave()" ) )
		@_filemenu.insertSeparator()
		@_filemenu.insertItem( "&Quit", $qApp, TQ_SLOT( "quit()" ) )
		
		@_colormenu = TQt::PopupMenu.new() # create a color menu
		@_colormenu.insertItem( "B&lack", COLOR_MENU_ID_BLACK)
		@_colormenu.insertItem( "&Red", COLOR_MENU_ID_RED)
		@_colormenu.insertItem( "&Blue", COLOR_MENU_ID_BLUE)
		@_colormenu.insertItem( "&Green", COLOR_MENU_ID_GREEN)
		@_colormenu.insertItem( "&Yellow", COLOR_MENU_ID_YELLOW)
		TQt::Object.connect( @_colormenu, TQ_SIGNAL( "activated( int )" ),
						 self, TQ_SLOT( "slotColorMenu( int )" ) )
						
		@_helpmenu = TQt::PopupMenu.new()  # create a help menu
		@_helpmenu.insertItem( "&About QtScribble", self, TQ_SLOT( "slotAbout()" ) )
		@_helpmenu.insertItem( "&About Qt", self, TQ_SLOT( "slotAboutQt()" ) )
		
		@_menubar = TQt::MenuBar.new( self, "" )  # create a menu bar
		@_menubar.insertItem( "&File", @_filemenu )
		@_menubar.insertItem( "&Color", @_colormenu )
		@_menubar.insertItem( "&Help", @_helpmenu )
		
		 # We create a TQt::ScrollView and a ScribbleArea. The ScribbleArea will
		 # be managed by the scroll view.#
		@_scrollview = TQt::ScrollView.new( self )
		@_scrollview.setGeometry( 0, @_menubar.height(),
		 							width(), height() - @_menubar.height() )
		@_scribblearea = ScribbleArea.new()
		@_scribblearea.setGeometry( 0, 0, 1000, 1000 )
		@_scrollview.addChild( @_scribblearea )
		TQt::Object.connect( self, TQ_SIGNAL( "colorChanged(TQColor)" ),
						 @_scribblearea, TQ_SLOT( "setColor(TQColor)" ) )
		TQt::Object.connect( self, TQ_SIGNAL( "save(const TQString&)" ),
						 @_scribblearea, TQ_SLOT( "slotSave(const TQString&)" ) )
		TQt::Object.connect( self, TQ_SIGNAL( "load(const TQString&)" ),
						 @_scribblearea, TQ_SLOT( "slotLoad(const TQString&)" ) )
	end
	
	def resizeEvent( event )
		 # When the whole window is resized, we have to rearrange the geometry
		 # in the ScribbleWindow as well. Note that the ScribbleArea does not need
		 # to be changed.
		 @_scrollview.setGeometry( 0, @_menubar.height(),
		 							width(), height() - @_menubar.height() )
	end

	
	
	def slotAbout()
		TQt::MessageBox.information( self, "About QtScribble 5",
										"This is the Scribble 5 application\n" +
										"Copyright 1998 by Mathias Kalle Dalheimer\n")
	end
	
	def slotAboutQt()
		TQt::MessageBox.aboutTQt( self, "About Qt" )
	end
	
	def slotColorMenu( item )
		case item
			when COLOR_MENU_ID_BLACK
				emit colorChanged( black )
			when COLOR_MENU_ID_RED
				emit colorChanged( darkRed )
			when COLOR_MENU_ID_BLUE
				emit colorChanged( darkBlue )
			when COLOR_MENU_ID_GREEN
				emit colorChanged( darkGreen )
			when COLOR_MENU_ID_YELLOW
				emit colorChanged( yellow )
		end
	end
	
	
	  #
	  # This is the slot for the menu item File/Load. It opens a
	  # TQt::FileDialog to ask the user for a filename, then emits a save()
	  # signal with the filename as parameter.
	  #
	def slotLoad()
		 # Open a file dialog for loading. The default directory is the
		 # current directory, the filter *.bmp.
		 #
		filename = TQt::FileDialog.getOpenFileName( ".", "*.bmp", self )
		if !filename.nil?
			emit load( filename )
		end
	end
	
	  #
	  # This is the slot for the menu item File/Load. It opens a
	  # TQt::FileDialog to ask the user for a filename, then emits a save()
	  # signal with the filename as parameter.
	  #
	def slotSave()
		 # Open a file dialog for saving. The default directory is the
		 # current directory, the filter *.bmp.
		 #
		filename = TQt::FileDialog.getSaveFileName( ".", "*.bmp", self )
		if !filename.nil?
			emit save( filename )
		end
	end
end
	
myapp = TQt::Application.new(ARGV)
mywidget = ScribbleWindow.new()
mywidget.setGeometry(50, 500, 400, 400)

myapp.setMainWidget(mywidget)
mywidget.show()
myapp.exec()