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
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding KTextEditor in FreeCAD

Post by adrianinsaval »

drmacro wrote: Tue Jul 13, 2021 5:33 pm Ok, thanks.

I use VSCode and it can do the full IDE thing. I was just curious if this was more to that end.
Can you debug freecad macros/python workbenches in VSCode? If so, can you give me any hints on how to do it?
drmacro
Veteran
Posts: 8866
Joined: Sun Mar 02, 2014 4:35 pm

Re: Embedding KTextEditor in FreeCAD

Post by drmacro »

I started collecting all I discovered about scripting in hopes to make a sort of handbook.

I have a bunch of collected notes the includes everything I've figured out about configuring vscode in the appendices of this document: https://github.com/macdroid53/FreeCADScripting .

It will allow debugging (single stepping, etc.).
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 »

I went on a rabbit hole when I tried to make an external workbench in c++. Apparently it is discouraged to do that and it would be better to try to use python. So then I tried to use Shiboken to generate python bindings from c++. I couldn't find that many tutorials/examples of how to use Shiboken so it was hard to make it work. But from looking at the Shiboken documentation, a KDAB tutorial, and the FreeCAD source code I was able to get something working. So it is possible to embed KTextEditor in FreeCAD without having to make changes to the source code or adding more dependencies. I organized the code as a python package so the c++ code containing KTextEditor can be build and installed as a normal python package:

Code: Select all

python setup.py install --user
Then it can be used by FreeCAD by importing the python module and running this script:

Code: Select all

from PySide import QtGui
from Editor.KEditor import KEditorView


def getMainWindow():
    toplevel = QtGui.QApplication.topLevelWidgets()
    for i in toplevel:
        if i.metaObject().className() == "Gui::MainWindow":
            return i
    raise Exception("No main window found")


mw = getMainWindow()

mdi=mw.findChild(QtGui.QMdiArea)
sub=mdi.addSubWindow(KEditorView())
sub.show()
mw.update()
And this should show the text editor:
Embedded_KTextEditor.png
Embedded_KTextEditor.png (68.75 KiB) Viewed 3174 times
However, it still can't open python/macro files. So I think the next step is in working on this.

Also, does someone have more resources on how to use Shiboken?
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

Lol, just came across this on the wiki Developing_FreeCAD_with_KDevelop I got excited but very anticlimactic :oops: :lol:
alonso_jamm wrote: Sat Jul 17, 2021 8:05 pm Also, does someone have more resources on how to use Shiboken?
Can't find much of anything on the wiki about it
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 »

I made a workbench using the python bindings for KTextEditor so now it can be used to edit python/macro files (or any other text file):
Embedded_KTextEditor.png
Embedded_KTextEditor.png (106.71 KiB) Viewed 3010 times
So far, the workbench have 3 commands:
  1. Open file: Opens a QFileDialog which can be used to select a text file to edit.
  2. Configure: Opens a configuration dialog for KTextEditor. The editor already provides the configuration dialog so it was easy to add it. Also, it seems that KTextEditor handles the saving/loading of configuration settings so I didn't have to do much to include the configuration dialog.
  3. Open macro: Opens a QFileDialog on the macro folder in order to open and edit a macro file.
With these changes, KTextEditor now can be used to edit files similar to the built-in editor :)
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: Embedding KTextEditor in FreeCAD

Post by HakanSeven12 »

Much better than standard editor ;) good job
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

Awesome news. Is it ready for the Addon Manager yet?
What are the prerequisites (https://github.com/Alonso-JAMM/FreeCAD_ ... r/issues/4) ?
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 Jul 25, 2021 10:47 pm Awesome news. Is it ready for the Addon Manager yet?
What are the prerequisites (https://github.com/Alonso-JAMM/FreeCAD_ ... r/issues/4) ?
Thanks!

I am not sure if it is ready for the Addon Manager because I don't know what are the requisites to be added. The prerequisites for building the python bindings are the same than for building FreeCAD from source plus the KTextEditor package which is provided by many Linux distributions. So, getting the required libraries to build the python bindings should be as hard as getting the libraries required to build FreeCAD from source.

The cmake files need a little bit more work in order to be sure they will work on other systems (right now I have hard-coded the include directory for PySide2).

Also, windows users won't be able to use the workbench since the cmake files only work with Linux and I don't know how to make them work in windows.
User avatar
kkremitzki
Veteran
Posts: 2511
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Embedding KTextEditor in FreeCAD

Post by kkremitzki »

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."
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

+1
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