HOWTO Drag and drop

Moderator: Rathinagiri

Post Reply
Ricci
Posts: 255
Joined: Thu Nov 19, 2009 2:23 pm

HOWTO Drag and drop

Post by Ricci » Fri Oct 14, 2011 8:55 am

One of the big advantages of HMG4 is that you can use all the possibilities of the Qt system without waiting for someone who will put in into the HMG code.

Here is one example: How to drag a file from the windows filemanager (or something else) and drop it into a bitmap control. You can use any other control or the program window too. This example shows the power of using OOP.

This would create a common bitmap in HMG4 syntax (without assigning a picture)

Code: Select all

		With Object BITMAP():New( "oMyImage" )
			:Row				 := 10
			:Col				 := 10
			:Width			  := 200
			:Height			 := 200
			:Stretch			:= .F.
			:ScaledToHeight  := .F.
			:ScaledToWidth   := .F.
			:Alignment		 := LBL_CENTER
			:VAlignment		:= LBL_VCENTER
			:Smoothing		 := 1
		end
The bitmap control knows nothing from drag or drop (until yet), so we have to extend it with these new possibilies.
In OOP this is made by inheritance. This means you copy all properties and methods of a given class, add some new oder change some. You can do this often without knowing or understanding the code of the class. ;)

In this example we want to create a new class called DROPBITMAP that is derived from the BITMAP class.
The creation of our bitmap need only a little change:

Code: Select all

		With Object DROPBITMAP():New( "oMyImage" )
			:Row				 := 10
			:Col				 := 10
			.....
		end
Now we have to create the new class, that´s simple:

Code: Select all

/*==============================================================================
   DROPBITMAP class
==============================================================================*/
CLASS DROPBITMAP FROM BITMAP

   
// Methods
	METHOD New														SETGET
	
ENDCLASS

/*..............................................................................
   New
..............................................................................*/
METHOD New( cName, oParent ) CLASS DROPBITMAP

   Super:New( cName, oParent )

   ::oQTObject:setAcceptDrops(.T.)
   
   ::oQTObject:connect( QEvent_DragEnter ,{|oEvent|oEvent:acceptProposedAction()} )
   ::oQTObject:connect( QEvent_DragMove , {|oEvent|oEvent:acceptProposedAction()} )
   ::oQTObject:connect( QEvent_Drop , 	 {|oEvent|DropEvent( oEvent )} )
   ::oQTObject:connect( QEvent_DragLeave ,{|oEvent|oEvent:accept()} )
	
RETURN Self
As we can see, nothing in the BITMAP class was changed, only some event handler were added. You need all 4 event handler for drag and drop, 3 of them doing a standard action, only the drop event is needed for us. It will invoke the function DropEvent().
DropEvent() is no build in function, this is where we can react.

Here is a sample:

Code: Select all

FUNCTION DropEvent( oEvent ) 

	LOCAL oMimeData, oUrlList, oURL, cFile, i

	oMimeData := oEvent:mimeData()
	
	IF oMimeData:hasUrls()                 // <-- a file reference is dropped with one or more files in it
		oUrlList := oMimeData:urls()

		FOR i := 0 TO oUrlList:size() - 1 
			oURL := oUrlList:at(i)
			cFile := oURL:toLocalFile()      // <-- here you get the file name(s)

			.....
		NEXT    
	ENDIF	

	oEvent:acceptProposedAction()

RETURN .F.
You can drag and drop other mime types than URL, e.g. images, text, html. Here you will find more information: http://doc.qt.nokia.com/4.7-snapshot/dr ... psite.html

Ricci

Post Reply