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
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

Great!!!
At some point in the future it would be awesome to make a short guide on how to add other editors
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: Mon Sep 13, 2021 2:07 pm Great!!!
At some point in the future it would be awesome to make a short guide on how to add other editors
Hopefully the interface I made to add custom editors works on other editors than KTextEditor. The interface is in src/Gui/CustomEditor.h. There are all the functions used through FreeCAD to open text files, to debug macros, and to get the undo/redo history of the editor. So most of all the changes required to embed new editors should be done on src/Gui/CustomEditor.cpp
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

Hello, I just pushed a new commit that adds support to execute macro scripts when they are being edited with KTextEditor.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Embedding KTextEditor in FreeCAD

Post by wmayer »

At some point in the future it would be awesome to make a short guide on how to add other editors
A very general way is to get the window ID of any GUI application and then embed it e.g. as an mdi view into FreeCAD. So, this will also work for text editors of other DE or OS but it may feel like a foreign body as it doesn't use any of the functions (Open, Save, Undo, Redo, ...) of its host application.

The steps are:
  • Start the external editor with QProcess. The path to it can be saved in the preferences.
  • Using the process id retrieve the window id. For Windows there exist some API functions but I don't know how exactly to do this on other platforms. At least on Linux you can get the window id manually with the tool xwininfo
    See also: https://stackoverflow.com/questions/450 ... k-properly
  • Once you know the window id you can embed the application with

    Code: Select all

    from PySide2 import QtWidgets
    from PySide2 import QtGui
    
    winid = 0x4800003 # for example
    win = QtGui.QWindow.fromWinId(winid)
    win.show()
    widget = QtWidgets.QWidget()
    cont = QtWidgets.QWidget.createWindowContainer(win, widget)
    
    mw = Gui.getMainWindow()
    mdi = mw.findChild(QtWidgets.QMdiArea)
    mdi.addSubWindow(cont)
    cont.show()
    
Further interesting links:
https://www.linux-magazin.de/ausgaben/2 ... nd-und-qt/
bleber
Posts: 259
Joined: Thu Jun 30, 2016 5:12 pm

Re: Embedding KTextEditor in FreeCAD

Post by bleber »

I can use ktexeditor in free ad main oficial branch?
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

bleber wrote: Sun Jan 09, 2022 5:24 pm I can use ktexeditor in free ad main oficial branch?
No, you only can use ktexteditor from my fork (in the branch "KTextEditor"). Yesterday I did a rebase so the code in my branch is pretty close to the oficial master branch of FreeCAD.
User avatar
alonso_jamm
Posts: 77
Joined: Mon Nov 11, 2019 11:32 pm

Re: Embedding KTextEditor in FreeCAD

Post by alonso_jamm »

The last couple weeks I have been using KTextEditor as my default text editor in FreeCAD to try to caught as many problems as possible. And I have found several problems that make it annoying to use KTextEditor. The most annoying problems are related to shortcuts being ambiguous. The ambiguous shortcuts happen because KTextEditor comes with several actions with shortcuts that FreeCAD uses for its commands.

I found a way to remove the shortcuts of some of the actions of KTextEditor and use FreeCAD's commands instead. This only works for the standard actions that are the same in FreeCAD and in KTextEditor (like Copy, Paste, Save, etc). I am not sure how to solve this problem for actions that are different in FreeCAD and KTextEditor that have the same shortcut. For example, in KTextEditor Ctrl+D by default comments the line or the selected text. However, if I have an object selected in the Tree view while I am editing a macro file and I hit Ctrl+D I will get an ambiguous shortcut warning because KTextEditor has the comment action activated and FreeCAD has the display properties of the selected object activated.

Some other problems that are less annoying are:
1. So far there is no a preferences page to configure.
2. There is no auto-completion of FreeCAD's functions.
3. There is no LSP integration.
4. There is no a context menu

I will work on fixing the shortcuts of actions that are the same for FreeCAD and KTextEditor to remove most of the ambiguous shortcut warnings since they are the most annoying.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Embedding KTextEditor in FreeCAD

Post by Kunda1 »

Thanks for the update! This is important work you're doing @alonso_jamm
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
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding KTextEditor in FreeCAD

Post by adrianinsaval »

time to make this happen? :lol:
yorik wrote: Sat Oct 15, 2011 8:50 pm Oh.. I was going to propose to rename FreeCad to FreeKad... ;)
Post Reply