How to let an EWO access resources in the project folder?

Find and share HowTos to various installations / configurations!
8 posts • Page 1 of 1
ottemot
Posts:13
Joined: Wed Dec 02, 2015 2:30 pm

How to let an EWO access resources in the project folder?

Post by ottemot »

I've noticed that PictureFlow widget is able to access images in the "pictures" folder of the project just by passing a relative path to the image, e.g.

Code: Select all

this.addSlide("icons/triangle.png")
The really nice thing about this is of course that it's platform and device independent (and it even works in the Android app!)
I haven't found any documentation on how this is done on the widget side though.

So my question is, based on a relative path string received from OA, how can my widget access/read the resources available in the various subfolders of the project folder (the WinCC OA UI Android app included)?
I tried the (very naive) approach to see if I could get the project directory from any of the QDir paths, but to no avail.

I guess there something else going on behind the scenes, please enlighten me :)

Thank you in advance.

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: How to let an EWO access resources in the project folder?

Post by mkoller »

This is a special thingy in the following way:
The PictureFlow EWO defines the addSlide() interface as:

Code: Select all

PictureFlowWidget::methodInterface()
{
  if ( name == QLatin1String("addSlide") )
  {
    retVal = QVariant::Invalid;  // we return void

    args.append(QVariant::Image);
    return true;
  }
The interesting point here is the datatype QVariant::Image, which makes the UI aware of the fact that it shall
pass a QImage, which the UI knows how to get when the CTRL script just passes a string as argument.
The nice thing here is the EWO still does not need to know anything about the project paths.
It's the UI which searches along the configured paths, reads the image and passes it as QImage to the EWO

ottemot
Posts:13
Joined: Wed Dec 02, 2015 2:30 pm

Re: How to let an EWO access resources in the project folder?

Post by ottemot »

Ah, excellent answer Martin, that cleared up a lot of things :)
I didn't realize methodInterface() was also able to deal with QImage and how the UI handled that, I've only used it for colors.

That does trigger two more questions though.
If I have an .svg file or a config file (text file with plain text content) would there be any way to pass that to the EWO?

For the text file I guess I can simply work around this using something similar to this CTRL script:

Code: Select all

string fileContent;
fileToString("path\\\\to\\\\config.cfg", fileContent,"UTF8");
dyn_string lines = strsplit(fileContent, '\\n');
EWO.setConfig(lines);
That would probably be a decent way of doing it as it limits the path scope to the /scripts/ folder which I guess is the best place for a config file to reside anyway.

However, .svg might be a bit more tricky as it's not convertible to a QImage without rasterizing it.

Is there any way of passing a .svg as a QVariant::ByteArray?
Qt's .svg renderer supports loading the serialized XML data as a QByteArray so this might be a viable option if possible?
I should clarify that embedding the .svg files into the widget (like the DialGauge EWO) is not an option.

Your input on this would be very welcomed, whether or not it's possible.

Thanks in advance!

ottemot
Posts:13
Joined: Wed Dec 02, 2015 2:30 pm

Re: How to let an EWO access resources in the project folder?

Post by ottemot »

I realized I sort of answered my own question in my last post.
I just read the .svg as a regular string using

Code: Select all

fileToString("path\\\\to\\\\file.svg", svgContent,"UTF8");
and passed svgContent to the widget, converted the string to a QByteArray which I could then render. A little hackish but it works. This of course means the .svg must be located in the scripts folder which isn't ideal but as long as it works....
If there are more elegant ways of doing it I would be happy to receive input! :)

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: How to let an EWO access resources in the project folder?

Post by mkoller »

I think the simplest way is: let the UI find the file and just pass the absolute path to the EWO.
The EWO can then do whatever it wants.
E.g. in the script use
string path = getPath(...)
and pass the path as a simple string to the ewo.

P.S.:I don't think the scripts dir is the appropriate dir for a config file. For this we have the config directory (or the data directory)

ottemot
Posts:13
Joined: Wed Dec 02, 2015 2:30 pm

Re: How to let an EWO access resources in the project folder?

Post by ottemot »

I agree, and that is what I initially did, however that failed on the Android app as the getPath() refers to a cache folder in /data/ and I assume the data is only cached there if the UI specifically asks for it.
As far as I know there's no way to ask for stuff to be cached from the widget or CTRL script. I would love to get told otherwise though :)

Gertjan van Schijndel
Posts:634
Joined: Mon Aug 02, 2010 10:37 am

Re: How to let an EWO access resources in the project folder?

Post by Gertjan van Schijndel »

By using the getPath() on a file you are specifically asking for the file (to be cached).

ottemot
Posts:13
Joined: Wed Dec 02, 2015 2:30 pm

Re: How to let an EWO access resources in the project folder?

Post by ottemot »

You're absolutely right, thank you!
I used the getPath() a little backward the first time around, but now everything works like a charm.

8 posts • Page 1 of 1