Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Limiting number of actions to be created

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Limiting number of actions to be created

Post by krambriw »

Hi,
I have a little nut I'm trying to crack...

As example I have a plugin with just one action. I want to limit how many the user shall be able to define & configure

Let's look at this simple sample:

Code: Select all

class PrintString(eg.ActionBase):
    def __call__(self, myString):
        print myString

    def Configure(self, myString=""):
        panel = eg.ConfigPanel()
        textControl = wx.TextCtrl(panel, -1, myString)
        panel.sizer.Add(textControl, 1, wx.EXPAND)
        while panel.Affirmed():
            panel.SetResult(textControl.GetValue())

I want to allow the user to define only one of this action. How would you do to limit that?

Kind regards, Walter
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Limiting number of actions to be created

Post by kgschlosser »

depending on how you want to limit it. i am thinking the easiest solution would be to limit it with a counter and then have a dialog show letting the user know they have to many and you can use the tree item to then delete the thing since the tree item is what is going to init the action.

using the way i found to call the XmlId from code to open config dialogs should work to delete the tree item as well tho that would be alot of extra code. using a counter to just display the dialog and then the user would have to delete the tree item them selves would be super simple.


example:

Code: Select all


def SomeAction(eg.ActionBase):
    
    actionCounter = 0

    def __init__(self):
        self.aCounter= self.__class__.actionCounter
        if self.aCounter is >= 10:
            OPENSOMEDIALOG()
        self.__class__.actionCounter += 1

    def Configure(self, param1='something', param2='something', param3='something'):
        panel = eg.ConfigPanel()
        if aCounter < 10:
            ctrl1 = wx.TextCtrl(panel, -1, param1)
            ctrl2 = wx.TextCtrl(panel, -1, param2)
            ctrl3 = wx.TextCtrl(panel, -1, param3)
            .......
            while panel.Affirmed():
                ......
        else:
            event = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, panel.dialog.buttonRow.cancelButton.GetId())
            wx.PostEvent(panel.dialog.buttonRow.cancelButton, event)
            while panel.Affirmed():
                panel.SetResult(param1, param2, param3)

pseudo code above but it give you the idea to be able to count.

that would be the easiest.

give me until the end of the day if you want to make it automatically delete the tree item. i have to ponder the most efficient way to do that and to test some functionality of how EG works with the creation of the tree item. and if it's selected or not and things of that nature. it just depends on the order of how EG creates the item where it passes what to who. but i believe the tree item is passed to the config dialog and i am just not sure if it gets stored into a self to be able to access it. because if it does then you should be able to grab that tree item from panel.dialog.treeItem and pass it to the eg.document or maybe the undo handler for deletion.
If you like the work I have been doing then feel free to Image
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Limiting number of actions to be created

Post by krambriw »

Hello KG,
I have now a solution, thank you for the hints!

I was a bit lucky since in my plugin implementation I had already implemented two useful functions:

- addActionName: keeps an updated list of names for defined actions
- getAllActionNames: returns the list

So with this and your hints it works pretty well. I have set the limit to allow only one defined action of the specific kind so the code looks like this (a bit pseudo) and is part of the Configure section of the action:

Code: Select all

    def Configure(
        self,
        actionName = "Give this action a name",
        etc,
        etc
    ):
        plugin = self.plugin
        panel = eg.ConfigPanel(self)

        p = plugin.getAllActionNames()
        if len(p) > 0:
            if actionName != p[0]:
                eg.PrintNotice('You have already defined an action of this kind')
                event = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, panel.dialog.buttonRow.cancelButton.GetId())
                wx.PostEvent(panel.dialog.buttonRow.cancelButton, event)            

        mySizer_1 = wx.GridBagSizer(10, 10)
        mySizer_2 = wx.GridBagSizer(5, 5)
        mySizer_3 = wx.GridBagSizer(10, 10)

        etc, etc

In my case I wanted to restrict it to allow only one defined action of this particular kind but it is of course possible to allow another number by changing the code a bit. The good part is that no action is created in the tree if you select to add action. If you select add macro, the left over is just an empty harmless macro that can be easily deleted manually.

Besides, it allows you to create one action the first time and you can edit it as many times you like. If I delete it, the list will be empty and I can create a new one again.

Thanks,
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Limiting number of actions to be created

Post by kgschlosser »

that's right because the config dialog appears on creation of the action and if you cancel it' EG will remove the added action because it was canceled on creation.


So that works out awesome!!

Glad to toss out some helpful hints to get the job done. i know how irritating it can be. I have a really complex problem i am working on right now. and i have been trying for a couple of days to get it right and i am sooo close. it's driving me nutz.

maybe you might know the solution.

I have created a new Script editor. and added threading support to it. i posted an early release of it to see if there are any bugs mainly in the config pieces of it. and actually your code in the suntracker plugin taught me how to use the config file instead of making a seperate file for the data. So i thank you in respect to that.


but what i have done in my script editor is added what would be multi module support. meaning that you can code a script that has core functionality and for code organization the ability to have multiple "files" files being dynamic compiled objects but act just like files in a folder and using import to bring them over to the core script but i have also added the script plugin to the __builtins__ so it can be accessed anywhere and everywhere inside of EG. it's not limited to just one place. so if you code a script it will add it to the "script" module and instead of having jumps in the tree or have duplicate scripts with the same code in it. or if you just want to use one of the "files" from another script you are able to by just importing it. now all works fine with this when importing the actual script. but not the files i can't seem to figure out how to get it to compile the code and run it and add the items in the file to a module.__dict__ and then look in the __dict__ to see if the keyword is there and then return it.

don't know if i am explaining that in a way that you can understand. but i might have to have you try it and look at the code.

but it's very frustrating

*** EDIT***

I got it working was actually something stupid i did that was making it not work right.
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Limiting number of actions to be created

Post by kgschlosser »

You may consider using this in replacement for the eg.PrintNotice('You have already defined an action of this kind')

just a suggestion :-D

K

Code: Select all

dlg = wx.MessageDialog(panel,
                                   message='You have already defined an action of this kind',
                                   caption=self.__name__ + ', Error!'
                                   style=wx.OK |wx.ICON_ERROR | wx.STAY_ON_TOP
                                   )
if dlg.ShowModal() == wx.OK:
    dlg.Destroy
    event = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, panel.dialog.buttonRow.cancelButton.GetId())
    wx.PostEvent(panel.dialog.buttonRow.cancelButton, event)
If you like the work I have been doing then feel free to Image
Post Reply