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

Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

Hello, a couple months ago I tried to embed KTextEditor in FreeCAD as an alternative script editor. However, I found a problem where some keyboard inputs were not registered in KTextEditor. It seems like the keyboard inputs are seen as shortcuts and the events are consumed somewhere else in FreeCAD. Has anyone had the same problem?

I just came back to try to solve this problem and found some hack to make it work. The hack consisted of putting an event filter in the parent Widget of the editor and accept all key presses except modifiers and other special keys. I am not exactly sure why this works but it solves the problem of some keys not being recognized by the editor. Here is the event filter I made:

Code: Select all

bool KEditorView::event(QEvent* e)
{
    // HACK: when keys are pressed QEvent::ShortcutOverride is emitted. If we
    // accept all the key events except for the modifiers and other special keys,
    // then text will be added to the text field and shortcuts (like Ctrl+A or Ctrl+S)
    // will also be enabled.
    if (e->type() == QEvent::ShortcutOverride)
    {
        QKeyEvent *ke = static_cast<QKeyEvent *>(e);
        int key = ke->key();
        // no modifiers and other special keys
        if (!ke->modifiers() & (key != Qt::Key_Backspace) & (key != Qt::Key_Insert)
            & (key != Qt::Key_Delete) & (key != Qt::Key_Home) & (key != Qt::Key_End)
            & (key != Qt::Key_Left) & (key != Qt::Key_Up) & (key != Qt::Key_Right)
            & (key != Qt::Key_Down) & (key != Qt::Key_PageUp) & (key != Qt::Key_PageDown))
        {
            e->accept();
        }
    }
    return QWidget::event(e);
}
I put a minimal example of embedding KTextEditor in FreeCAD in a fork to show it (the editor is in src/Gui/KEditor.cpp). Also I would like to know if embedding KTextEditor in FreeCAD would be a good thing for FreeCAD. Some of the pros I see with KTextEditor is that it is more advanced than the current editor. It is relatively easy to embed in FreeCAD (other than the key presses bug) and it includes some neat features by default. For example, KTextEditor comes with a word find functionality (including find and replace), it contains python syntax highlighting, and spell checking. By default KTextEditor also comes with a simple word completion functionality with the possibility of adding a lsp client plugging and more advanced word completion. The license of KTextEditor is also LGPL2+ so I am pretty sure it is compatible with FreeCAD's license. The only drawback I see is that including KTextEditor will require add an extra dependency to FreeCAD. Any thoughts on this?

I also added a couple of screenshots comparing KTextEditor and the current editor in FreeCAD.
Attachments
Current python editor
Current python editor
Screenshot_20210711_221836.png (321.08 KiB) Viewed 4287 times
KTextEditor embedded in FreeCAD
KTextEditor embedded in FreeCAD
Screenshot_20210711_221706.png (284.73 KiB) Viewed 4287 times
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding KTextEditor in FreeCAD

Post by adrianinsaval »

Seems interesting, I would certainly like it.
What would be the additional dependencies? Do you have an estimate of how much code can be removed from FreeCAD if this is used? In my opinion the extra dependency could be worth it if the need to maintain a big portion of code is also removed in the process.
But there is a question about the stability of this dependency, can we trust that it won't break out of the blue in the future? If KDE moves on to the next version of qt but FreeCAD is still not ready for that, what happens?
Can this be implemented as an addon?
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

adrianinsaval wrote: Tue Jul 13, 2021 4:01 am What would be the additional dependencies?
The extra dependency would be KTextEditor, I looked at several Linux distributions (Debian, Ubuntu, Archlinux) and they offer this dependency. I don't know much about packaging so I am not sure how much harder would it be to build and distribute FreeCAD when including KTextEditor

adrianinsaval wrote: Tue Jul 13, 2021 4:01 am Do you have an estimate of how much code can be removed from FreeCAD if this is used? In my opinion the extra dependency could be worth it if the need to maintain a big portion of code is also removed in the process.
If the current python editor was completely removed, then a couple of files in src/Gui/ would be removed but in change we would need to add a couple more files in order to interface KTextEditor with FreeCAD. So the overall change in amount of code may not be that much.

adrianinsaval wrote: Tue Jul 13, 2021 4:01 am But there is a question about the stability of this dependency, can we trust that it won't break out of the blue in the future? If KDE moves on to the next version of qt but FreeCAD is still not ready for that, what happens?
This could be a problem, as far as I know KDE will remain using Qt5 for a while, but I don't know for how long. I assume that the Qt5 version of KTextEditor will remain available long enough to no cause incompatibilities.

adrianinsaval wrote: Tue Jul 13, 2021 4:01 am Can this be implemented as an addon?
Actually, I like the idea of implementing KTextEditor as an addon for FreeCAD. Doing so would avoid having KTextEditor as a hard dependency of
FreeCAD and avoid many possible problems with incompatibilities. Also, older versions of FreeCAD could use KTextEditor instead of only the git version.

The first problem would be embedding KTextEditor to FreeCAD from an external Module/Workbench. I found this thread that talks about how to embed Qt widgets into the main window so it may be possible of at least opening a new tab with KTextEditor from an external Module/Workbench.

The second problem would be to open Python/Macro files using the external Module/Workbench so that we can use KTextEditor to edit the files instead of the built-in editor. This forum post talks about opening custom file formats from a Module/Workbench so it may be possible to create a custom command that opens a Python/Macro file using the embedded KTextEditor instead of the built in editor.

It seems possible to embed KTextEditor as an addon instead of having to change the source code of FreeCAD. I will try to do this and see how it works.
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding KTextEditor in FreeCAD

Post by adrianinsaval »

Looking at the ktexteditor package in manjaro it seems to bring too many kde dependencies, this is undesirable for anyone not using kde so I think it's not viable for use in main FreeCAD but I think those of us who are already using KDE would love to have this as an addon.
drmacro
Veteran
Posts: 8865
Joined: Sun Mar 02, 2014 4:35 pm

Re: Embedding KTextEditor in FreeCAD

Post by drmacro »

Can it set breakpoints to stop the executing code and display the current state of properties and other variables?
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

adrianinsaval wrote: Tue Jul 13, 2021 4:15 pm Looking at the ktexteditor package in manjaro it seems to bring too many kde dependencies, this is undesirable for anyone not using kde so I think it's not viable for use in main FreeCAD but I think those of us who are already using KDE would love to have this as an addon.
Yeah, the additional dependencies may be a bigger problem than what I first thought. I hope it is not too hard to add KTextEditor as an addon 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: Tue Jul 13, 2021 5:10 pm
adrianinsaval wrote: Tue Jul 13, 2021 4:15 pm Looking at the ktexteditor package in manjaro it seems to bring too many kde dependencies, this is undesirable for anyone not using kde so I think it's not viable for use in main FreeCAD but I think those of us who are already using KDE would love to have this as an addon.
Yeah, the additional dependencies may be a bigger problem than what I first thought. I hope it is not too hard to add KTextEditor as an addon to FreeCAD.
I'm all for adding a template for users who have preferred IDEs and Text editors to be able to use them within FC. Aside from the dependency issues which is a nightmare in itself, making said template code would be quite a UI/UX boost for users. And I'm for using the Addon Manager as the way to implement this so it's optional.
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 »

drmacro wrote: Tue Jul 13, 2021 4:29 pm Can it set breakpoints to stop the executing code and display the current state of properties and other variables?
No, it is just a text editor and doesn't know anything about executing the files. However, Kate has a gdb plugging which is a gui interface of gdb with breakpoints and the ability to see the stack so it may be possible to add a debugger to and embedded KTextEditor. But, I am not sure how hard it would be to do that.
drmacro
Veteran
Posts: 8865
Joined: Sun Mar 02, 2014 4:35 pm

Re: Embedding KTextEditor in FreeCAD

Post by drmacro »

alonso_jamm wrote: Tue Jul 13, 2021 5:16 pm
drmacro wrote: Tue Jul 13, 2021 4:29 pm Can it set breakpoints to stop the executing code and display the current state of properties and other variables?
No, it is just a text editor and doesn't know anything about executing the files. However, Kate has a gdb plugging which is a gui interface of gdb with breakpoints and the ability to see the stack so it may be possible to add a debugger to and embedded KTextEditor. But, I am not sure how hard it would be to do that.
Ok, thanks.

I use VSCode and it can do the full IDE thing. I was just curious if this was more to that end.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
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: Tue Jul 13, 2021 5:13 pm I'm all for adding a template for users who have preferred IDEs and Text editors to be able to use them within FC. Aside from the dependency issues which is a nightmare in itself, making said template code would be quite a UI/UX boost for users. And I'm for using the Addon Manager as the way to implement this so it's optional.
With a "template" do you mean a way of making FreeCAD able to change the text editor used to modify Python/Macros? So users can select their preferred text editor in the "Preferences" menu and then when they click the edit macro button FreeCAD opens the preferred text editor. Because I like that idea.

But for now I am trying to find a way of making an external workbench with C++. Are there any examples of external C++ workbenches?
Post Reply