Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve manual code during rework #99

Closed
matecsaj opened this issue Mar 20, 2022 · 9 comments
Closed

preserve manual code during rework #99

matecsaj opened this issue Mar 20, 2022 · 9 comments

Comments

@matecsaj
Copy link

matecsaj commented Mar 20, 2022

The handwritten code I put in the generated .py file gets overwritten when making subsequent versions of my application. Reapplying my changes consumes labour and, more importantly, is an opportunity for error.

Could the generator insert tagged comments to delineate where manual code can go? Then when the code generator is re-run, could it preserve manually added code? If something is added, deleted or modified in the UI, the code generator will somehow need to cope.

Step 4 on this page is wiki page is relevant
https://github.com/alejandroautalan/pygubu-designer/wiki/Design-Code-Iteration

A UML diagramming tool that I used 25-years ago generated C++ code with stubs where my handwritten code belonged. It has a feature they called round trip engineering.

A competing tool has this already; search for 'rework problem' here http://page.sourceforge.net

@matecsaj
Copy link
Author

An alternate solution would be adding a tab in the Property pane to edit relevant Python code snippets; it would be handy if the objects in scope were available. Snippets would be inserted in the generated code and be indented appropriately. Both https://anvil.works and http://www.web2py.com take this approach. Rather than make a code editor and debugger from scratch, perhaps parts of another open-source project, such as IDLE could be reused.

@matecsaj
Copy link
Author

Another alternate would be to transform pygubu into a plugin for Visual Studio, PyCharm, Eclipse, or other IDE with good Python support.

@alejandroautalan
Copy link
Owner

alejandroautalan commented Mar 27, 2022

Hello.

These are all good ideas that could be implemented in the future.

Another option that can be implemented (and that would take less time) is to use inheritance.
Consider the following development iteration:

  • Tab Design. The user makes a first sketch of its interface
  • Tab Code.
    Enter 'myapp' as the class name.
    Select option "Use inheritance to generate modules" (or something similar)
  • User clicks Generate. Then, the designer will generate two files:
    myappbase.py: Will contain a class named MyappBase. The user should not edit this file.
    myapp.py: Will contain a class Myapp than inherits from MyappBase. It will be generate only the first time (or if possible updated with the new callback methods that don't exist yet.)

Example of generated files:

myappbase.py:

#!/usr/bin/python3
import pathlib
import pygubu

PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "myapp.ui"


class MyappBase:
    def __init__(self, master=None):
        self.builder = builder = pygubu.Builder()
        builder.add_resource_path(PROJECT_PATH)
        builder.add_from_file(PROJECT_UI)
        self.mainwindow = builder.get_object("mainwindow", master)
        builder.connect_callbacks(self)

    def run(self):
        self.mainwindow.mainloop()

    def on_action1_clicked(self):
        pass


if __name__ == "__main__":
    app = MyappBase()
    app.run()

myapp.py:

#!/usr/bin/python3
from myappbase import MyappBase

class Myapp(MyappBase):

    def on_action1_clicked(self):
        # Write your code here.
        print('Button action 1 clicked.')


if __name__ == "__main__":
    app = Myapp()
    app.run()

After that, user can modify the interface and regenerate the base class and update the other file accordingly.

I think I saw this approach in another design app, but not remember which one. I'm using this approach for some projects.
Let me know.

Regards
Alejandro A.

@matecsaj
Copy link
Author

I gave the inheritance approach a go, and it is working well so far; thanks for suggesting. Should the issue be closed after incorporating your advice into this wiki page?

@alejandroautalan
Copy link
Owner

Yes, please.

Regards
Aejandro A.

@matecsaj
Copy link
Author

While editing the wiki, I could not get the python code to look right. Would you please try pasting in the sample code that you provided earlier?

@alejandroautalan
Copy link
Owner

Added code to the wiki page.

Regards
Alejandro A.

@rdbende
Copy link
Contributor

rdbende commented Mar 31, 2022

Possibly the base class could be an ABC, and the methods to be overridden are abstractmethods, so the user has to implement them, that helps to avoid bugs and confusion.

@alejandroautalan
Copy link
Owner

Inheritance approach implemented in v0.39.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants