Embedding KTextEditor in FreeCAD

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

kkremitzki wrote: Mon Jul 26, 2021 1:43 pm Very cool, I've long thought there is potential in pulling in KDE dependencies to enhance FreeCAD's UI/UX. Even if it isn't feasible to deploy it as a C++-based workbench, it would still be worthwhile IMO to try a "KDE-enhanced" distribution of FreeCAD, à la Realthunder's "fork". In terms of packaging, for Linux at least adding KDE deps wouldn't be any difficulty since KDE is already well-packaged on that platform. For Windows I know there is quite a bit of effort in improving things there, for example they recently put the Kate text editor in the Windows Store and of course Kdenlive as well is quite popular there. I'm not sure what the situation would be for Mac or Conda.

In principle, though, it makes a lot of sense. FreeCAD is already a Qt application, and the KDE Frameworks website describes them as "a set of 83 add-on libraries for programming with Qt."
I also think FreeCAD could get some nice enhancements from KDE. KTextEditor is a good example of how FreeCAD could be improved, it is relatively easy to embed within FreeCAD and provides lots of neat features that the built-in editor doesn't have (auto completion, syntax highlighting, spell checking, search and replace, etc). And there are more widgets from KDE that I think could be useful in FreeCAD; for example, KCommandBar could be a good addition to FreeCAD too.

Also, from the KDE Frameworks page it seems that Windows is supported by many of the frameworks.
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Embedding KTextEditor in FreeCAD

Post by yorik »

This is very, very cool! the possibilities to extend are huge...

My 2 cents:

1) dependency is not really a problem, FreeCAD depends already on pretty heavyweight stuff, this is small and widely and easily available.

2) an external workbench/module coded in C++ would be a hell to maintain (you need to provide builds for different platforms, etc). I would make something nicely organized in a folder somewhere (under Gui for example) then submit for inclusion directlytin FreeCAD.

3) take care with the license. No pure GPL allowed in FreeCAD, only LGPL and similars (MIT, BSD,...)

4) finally, to make everybody happy, i would not remove the current code, but add a cmake switch that allows to enable/disable compiling your editor. So people with dependency issues can simply disable it. In any case, it's good practice. And maybe a way for user to switch between the two at runtime too, while it's still new, might be good...
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

Alright, I will try to include KTextEditor directly in FreeCAD instead of trying to do an external workbench. I think I overestimated how much extra work it would be to add KTextEditor as dependency. I also underestimated how complicated it would be to use external workbenches written in C++. I tried to build the workbench in debian and opensuse leap and realized how much trouble it is to build the workbench. It requires the development packages of pyside2, shiboken2, qt5, ktexteditor and more which is a lot of work. Moreover, the workbench breaks when freecad is compiled with a different version of qt than the one the workbench uses. So it is true that it will be difficult to maintain the external workbench written in c++.

Also I felt like I was reinventing the wheel with the external workbench approach. A lot of the work done in it was trying to find the qt5, pyside2, and shiboken packages which FreeCAD already does.

A cmake switch to enable/disable KTextEditor and a way to switch between the current editor and KTextEditor is a better approach than the external workbench approach.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

What's are some of the challenges?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

Kunda1 wrote: Sun Aug 01, 2021 12:13 pm What's are some of the challenges?
For embedding KTextEditor in FreeCAD directly, I think the major challenge will be dealing with configurations. KTextEditor provides configuration widgets; however, I don't know if it is possible to embed widgets in the FreeCAD preferences dialog (it seems that it only supports ui pages made with QtDesigner). So this means that I will either have to make custom configuration ui pages in QtDesigner and then deal with loading/saving configuration settings for KTextEditor, or I could create a separate configuration dialog only for KTextEditor and leave it to handle loading/saving the configuration settings. In the workbench approach I used a separated configuration dialog only for KTextEditor and it worked okay. But for embedding KTextEditor directly, I don't if using a separated configuration dialog will be good because there is not a good place to put a button to open the configuration dialog for KTextEditor. Maybe it will be better to create the custom configuration ui pages in QtDesigner instead of using the provided widgets from KTextEditor.

The other challenges like implementing a lsp client or a debugger shouldn't be as hard to implement than with the workbench approach. These problems may even be easier to solve since I now will have direct access to FreeCAD
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

alonso_jamm wrote: Sun Aug 01, 2021 5:16 pm
Please document your research into this. It will be helpful with other 3rd party editors as well!
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
kisolre
Veteran
Posts: 4162
Joined: Wed Nov 21, 2018 1:13 pm

Re: Embedding KTextEditor in FreeCAD

Post by kisolre »

Reading this topic I was wandering about using https://qscintilla.com/#home or https://www.scintilla.org/ directly...
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

kisolre wrote: Mon Aug 02, 2021 3:33 pm Reading this topic I was wandering about using https://qscintilla.com/#home or https://www.scintilla.org/ directly...
It seems possible to embed it, though it may be hard since QScintilla seems to be a python package and its license is GPLv3.
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

I did some progress with making the integration of KTextEditor optional in FreeCAD. I added a cmake option to enable the compilation of KTextEditor within FreeCAD. I used a definition to determine which editor to use (the built-in or KTextEditor) depending on the cmake option:

Code: Select all

#if defined BUILD_KTEXTEDITOR
            ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter().
                GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Editor");
            bool use_keditor = hGrp->GetBool("EnableKTextEditor", false);
            if (use_keditor) {
                KEditorView* edit = new KEditorView(getMainWindow());
                edit->openFile(fileName);
                getMainWindow()->addWindow(edit);
            }
            else {
                PythonEditor* editor = new PythonEditor();
                editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
                PythonEditorView* edit = new PythonEditorView(editor, getMainWindow());
                edit->open(fileName);
                edit->resize(400, 300);
                getMainWindow()->addWindow( edit );
            }
#else
            PythonEditor* editor = new PythonEditor();
            editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
            PythonEditorView* edit = new PythonEditorView(editor, getMainWindow());
            edit->open(fileName);
            edit->resize(400, 300);
            getMainWindow()->addWindow( edit );
#endif
I am not sure if doing this is good practice since I would have to add this piece of code everywhere the editor is called which is in multiple files. I am wondering if using a function to open an editor would be a better approach. So the function would contain the code above and we just call that function whenever we want to open and edit a file. This would help to avoid duplicating code and it would be easier to embed another text editor, because in theory we would need to only modify that function. There are some other places where the editor is also required in order to do some other operations (like in the python debugger to add/hide breakpoints). So I think of having all the functions related to the editor in a separated file with the preprocessor directives for choosing the text editor in order to avoid having #if defined BUILD_KTEXTEDITOR all over the place. Is this a good approach?


Also, I made a quick video showing how the user could change between the current built-in python editor and KTextEditor:
phpBB [video]


I am planning on changing the "Enable KTextEditor" checkbox into a separate preferences panel where I will put all the other settings for KTextEditor. I think it is possible to embed widgets in the preferences panel so I may be able to use the widgets provided by KTextEditor.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

Awesome work!! Have no idea how to respond to your technical questions but it does sound advantageous to have directives all in one place.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply