OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Resize Image Control based on size of Image

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
janssege
General User
General User


Joined: 10 May 2006
Posts: 19
Location: Belgium

PostPosted: Fri Sep 14, 2007 9:02 am    Post subject: Resize Image Control based on size of Image Reply with quote

Hi,

Let me start by saying that I'm just beginning to learn OO macros and that I am slightly overwhelmed by the vast api.

I am trying to setup a Form in ooBase that should display a picture loaded from an external file. The URL for this external image is read from another text field in the form. This part I managed to accomplish by searching the forums and other help sources.

The problem is that each image that is associated with the different records in my base table has got a different size. One image would be displayed fully, another only partly due to the Image control being fixed size. I tried setting the Scale property for the image control, but this caused images to stretch, which is not acceptable for the images to display.

What I am now trying to accomplish is to dynamically resize the Image control to match the size of the image loaded.

I have found several solutions to resize images, but all of them are for images that are also inserted by the code. I have tried to apply these examples to my specific situation, but I simply can't get it to work. The code I have so far always causes an "Object variable not set' exception.

This is my macro so far, which gets called whenever the user moves to another record:

Code:

Sub RecordNavChanged ( Event as Object )
   dim EntryForm as Object
   dim ImgField as Object
   dim ImgURIField as Object
   dim ImgFieldCtrl as Object
   dim docCtrl as Object

   EntryForm   = Event.Source
   ImgField    = EntryForm.getByName("FImage")
   ImgURIField = EntryForm.getByName("ImgURI")
   ImgField.ImageURL = "/home/janssege/Soulmade/" & ImgURIField.Text
   docCtrl = thisComponent.getCurrentController()
   ImgFieldCtrl= docCtrl.getControl("FImage")
   ImgFieldCtrl.SetSize(ImgField.Graphic.SizePixel)

end sub


Execution always stops on the line

Code:
   ImgFieldCtrl= docCtrl.getControl("FImage")


Following other examples I have also tried to replace this line with

Code:
   ImgFieldCtrl= docCtrl.getControl(ImgField)


but this doesn't change.

I guess my question is how do I get a reference to the Control shape in this situation. Because as I understand it, it's the Control Shape or a control that has to be manipulated to have it display at a new size.

By the way, the SetSize action in the next line still has to be refined. It's only there to show the final intention.

Can anybody tell me what I am doing wrong here ?
Back to top
View user's profile Send private message
vitcaro
OOo Advocate
OOo Advocate


Joined: 20 Feb 2007
Posts: 256
Location: Italy

PostPosted: Sat Sep 15, 2007 12:39 am    Post subject: Reply with quote

May be your way to size the ImageControl is not good by some unknown reason.
I have a code which does the job.
May be you can take some idea from it:
Code:

doc=thisComponent
oDlgModel = createUnoService("com.sun.star.awt.UnoControlDialogModel")
oDlgModel.Title ="XXXXXXXXX"
oDlgControl = createUnoService("com.sun.star.awt.UnoControlDialog")
oDlgControl.setModel(oDlgModel)
x=0
y=0
H=200
W=400
oDlgControl.setPosSize(x,y,W,H, com.sun.star.awt.PosSize.POSSIZE)
oDlgControl.setVisible true

ImgField = oDlgModel.createInstance("com.sun.star.awt.UnoControlImageControlModel")
oDlgModel.insertByName("FImage", ImgField): ImgField.Name = "FImage"
ImgFieldCtrl = oDlgControl.getControl("FImage")
ImgFieldCtrl.setPosSize(x, y, W, H, com.sun.star.awt.PosSize.POSSIZE)
ImgField.ImageURL =converttoURL("c:\aafoto\orto.jpg")

graphic= ImgField.graphic
oDlgControl.setOutputSize graphic.SizePixel
H=graphic.SizePixel.Height
W=graphic.SizePixel.Width
ImgFieldCtrl.setPosSize(0,0,W,H,com.sun.star.awt.PosSize.SIZE)
 
print "CHK"

oDlgControl.dispose
Back to top
View user's profile Send private message
janssege
General User
General User


Joined: 10 May 2006
Posts: 19
Location: Belgium

PostPosted: Sat Sep 15, 2007 1:26 am    Post subject: Reply with quote

Hi vitcaro,

Thanks for the reply.

Looking at your code, I see it does this:

1. Create a new dialog
- first the dialog model
- then a dialog control which will use the dialog model
- position and size the dialog control
2. Add an Image Control to it
- first the image control model
- next the image control itself, and tell it to use the image control model
- position and size the model
- load an image in the control
3. Resize the dialog and image control based on the size of the loaded image

I can follow this way of working. The difference with my situation is that I don't create a new dialog and image control, but I have to start from an existing form.

More precisely, the only information my macro has to start with is the event that triggered this macro. This event contains a reference to my form's model. And there is my current problem: starting from this information, how do a get a reference to the image control ? Via the form's model, I can only get a reference to the image control's model, not the control itself. At least, I can't figure out how to by looking at the other examples and the api. All examples I have seen so far start out by CREATING the control, so you automatically have a reference to it. I try to get a reference to an EXISTING control, but I have no idea how to navigate to it.

But I'm sure this must be possible, right ?

Geert
Back to top
View user's profile Send private message
janssege
General User
General User


Joined: 10 May 2006
Posts: 19
Location: Belgium

PostPosted: Sat Sep 15, 2007 5:35 am    Post subject: Reply with quote

I have been inspecting my macro using Xray. I find that the ImageControlModel for my Image Control on the ooBase form is of type OImageControlModel.

The getControl() method requires a parameter of type XModel. It seems OImageControlModel doesn't implement the XModel interface.

The closest common part in these two types is XComponent: OImageControlModel implements the XComponent interface, XModel is derived from the XComponent interface. But this doesn't make OImageControlModel implement XModel.

Would this mean that I can't use the ImageControlModel my macro gets via the Event object to find the actual control in the current view ? I find that hard to believe, but so far it does look like it.

It would mean that at least the Image Control and maybe others in ooBase forms use a different interface from Controls in other forms in oo. Can this be ?

Please someone tell me it's not, and I am just making a silly mistake... Sad
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Sat Sep 15, 2007 6:10 am    Post subject: Reply with quote

can you post your odb somewhere ? It is easier to help then ...
Back to top
View user's profile Send private message
vitcaro
OOo Advocate
OOo Advocate


Joined: 20 Feb 2007
Posts: 256
Location: Italy

PostPosted: Sat Sep 15, 2007 6:18 am    Post subject: Reply with quote

With yuor code:
Code:

EntryForm   = Event.Source
   ImgField    = EntryForm.getByName("FImage")
   ImgURIField = EntryForm.getByName("ImgURI")
   ImgField.ImageURL = "/home/janssege/Soulmade/" & ImgURIField.Text
   docCtrl = thisComponent.getCurrentController()
   ImgFieldCtrl= docCtrl.getControl("FImage")

you obtain reference to:
- ImageControlModel (ImgField)
- ImageControl (ImgFieldCtrl)
Then the problem is not there.
Consider following quote fron sdk DevelopersGuide:
Quote:
13.2.4 Form Layer Views
View Modes
An important aspect to know when dealing with forms is that the view for a form layer is in different
modes. More precise, there is a design mode available, opposite to a live mode. In design mode,
you design your form. interactively with OpenOffice.org by inserting new controls, resizing them,
and modifying their properties,together with control models and shapes. although OpenOffice.org
hides this. In live mode, the controls interact with the user for data input.
The live mode is the natural mode for forms views, because usually a form is designed once and
used again.

This mean that you may have problems resizing controls made with the IDE in design mode.

With the following code I was able to resize the ImageControl, but only when I run the macro line-by-line from the Basic-IDE (and starting with the IDE in design mode), but when I return to put focus on the document, the size returns back to the design value (may be necessary to run the macro twice while swithcing the Control Design Mode).
Code:
sub Main
doc=thisComponent

EntryForm = Doc.getDrawPage.getForms.getByName("Standard")
   ImgField    = EntryForm.getByName("FImage")
   ImgField.ImageURL =converttoURL("c:\aafoto\orto.jpg")
   docCtrl = thisComponent.getCurrentController()
   ImgFieldCtrl= docCtrl.getControl(ImgField )
   graphic= ImgField.graphic
   size=graphic.SizePixel
   H=size.Height
   W=size.Width
' switch the document view
   Dim oURL as new com.sun.star.util.URL
   oURL.Complete = ".uno:SwitchControlDesignMode"
   createUnoService( "com.sun.star.util.URLTransformer" ).parseStrict( oURL )
   Dim oDispatcher as Object
   oDispatcher = Doc.CurrentController.Frame.queryDispatch( oURL, "", 63 )
   dim oEmptyArgs()
   oDispatcher.dispatch( oURL, oEmptyArgs() )
ImgFieldCtrl.setOutputSize size
doc.lockControllers
print "CHECK"
doc.unlockControllers
End Sub
Back to top
View user's profile Send private message
janssege
General User
General User


Joined: 10 May 2006
Posts: 19
Location: Belgium

PostPosted: Sat Sep 15, 2007 8:44 am    Post subject: Reply with quote

Hmm, quite interesting.

I loaded your code and indeed, it worked more or less, where my code wouldn't work at all. To be clear: in my original code, I never got a reference to ImgFieldCtrl. I always got a "Object not set" error WHILE executing
Code:
 ImgFieldCtrl= docCtrl.getControl(ImgField)


The only difference I could see with your code is that you don't declare your variables. Guess what ? As soon as I deleted the variable declarations, my code worked as well ! A bug ? Or a poor variable declaration on my account ? I don't know, I just decided not to declare my variables anymore.

Next point: the code as you and I have setup do resize the control, but only until the macro ends. No good. Luckily, I did find another thread on the forum that has a solution for this problem.

For the interest of others, my final macro is now
Code:
Sub RecordNavChanged ( Event as Object )
   EntryForm   = Event.Source
   ImgField    = EntryForm.getByName("FImage")
   ImgURIField = EntryForm.getByName("ImgURI")
   ImgField.ImageURL = ConvertToURL("/home/aragorn/vialila/Pictures/Juwelen/(2005-11) Soulmade/" & ImgURIField.Text )
   docCtrl = thisComponent.getCurrentController()
   oShape = GetControlShape(docCtrl.Model, "FImage")
   oShape.setSize(ImgField.Graphic.Size100thMM)

end sub


Thanks a lot for your help. Without it, I would have given up. Very Happy
Back to top
View user's profile Send private message
janssege
General User
General User


Joined: 10 May 2006
Posts: 19
Location: Belgium

PostPosted: Sat Sep 15, 2007 8:53 am    Post subject: Reply with quote

One more final note:

I forgot to mention that to get the right size, I had to use Size100thMM instead of SizePixel.

Using SizePixel would display the images as tiny little boxes, while with Size100thMM, the images are displayed in actual (print-)size. Apparently the Shape's setSize method expects dimensions in 1/100th of mm's (at least in my locale).

I imagine there is also a way to display the images pixel per pixel (probably with some general formula to calculate the size in 1/100th of mm's). However, my images are tif images scanned at 300 dpi and intended to be printed on paper. So displaying them pixel per pixel would result in a very large display. So I don't need that.
Back to top
View user's profile Send private message
lasulu
Newbie
Newbie


Joined: 29 Sep 2007
Posts: 1

PostPosted: Sat Sep 29, 2007 6:05 am    Post subject: Thanks My pictures are better now Reply with quote

I have been struggling with this for a week. I have 850 digitized 35mm slides to catalog. Some are horizontal and others are vertical format although they are all the same size and have a 3:2 aspect ratio. They are shrunken thumbnails actually, but until I read your post I was displaying them in both formats. Now I can remove the excess distorted image from the screen. I would like to make the images a little bigger, but they are not bad at the Size100thMM. I can't find good documentation for this macro language. What are you using?
Back to top
View user's profile Send private message
janssege
General User
General User


Joined: 10 May 2006
Posts: 19
Location: Belgium

PostPosted: Sun Sep 30, 2007 12:38 pm    Post subject: Reply with quote

I'm glad my post helped you further.

I am still a beginner at programming in OOo (although not a beginner at programming in general Wink ).

I didn't find much information about the OOo programming model. To solve my problem, I used a combination of:
* This forum
* The official reference documentation
* The X-ray tool (at the bottom of the page) to evaluate running code
*The OOo built-in debugger

I did also look at other code examples I found using Google or on the openoffice.org pages.

About making your images a little bigger, I thing the easiest would be to take the dimensions found in Graphic.Size100thMM and multiply them with a given factor (depending on how much bigger you want to display them). You probably have to calculate the width and height separatly. Then use the result of this calculation to set the shape's new dimension.

Regards,

Geert
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group