How to do Q_INTERFACES with PyQt? (Goal: Implement QTextObjectInterface) - pyqt

I'm trying to implement something similar to Cocoa's NSTokenField with Qt, by placing objects with custom rendering in a QTextEdit.
Qt's text system allows for this by implementing a subclass of QTextObjectInterface to encapsulate the custom drawing code. However, in addition to subclassing, the class also has to make the interface it implements known to the meta-object system. In C++, this is done via the Q_INTERFACES macro in the header:
Q_INTERFACES(QTextObjectInterface)
I've looked at moc's output for a C++ prototype, and what it ends up doing is to generate a qt_metacast() implementation that does something useful for the interface name.
I can't find a way to emulate this using PyQt. Any ideas?

Use QtGui.QPyTextObject as the base class of your custom text object. See the richtext/textobject example in the PyQt archive for an example of how to do this.

Related

Access to GTK4 Widgets in C Using Gnome Builder

I'm fairly new to coding for Linux, and haven't done a lot of GUI programming. I've run into problems using Gnome Builder and programmatic access to UI widgets.
How the heck to you access a widget (built in a UI file and instantiated by widget templates in the *-window.c source file) to do anything with the widget? I could use gtk_builder calls in GTK3 to access widgets, but the GTK4 model is completely different, and I haven't found useful information that describes or demonstrates how to do it.
There are 2 ways of doing this in GTK (both 3 and 4) with UI files:
Parse the UI definition using something like gtk_builder_new_from_resource(). You can then use API like gtk_builder_get_object() to fetch the widgets you want by their id attribute
Use composite templates like gtk_widget_class_bind_template_child(). You can find an example of how to do this in the second chapter of the GNOME beginners tutorial
Usually, the second option is preferred, as it tends to lead to less boilerplate code

extend class in Java FX?

In many occasions JavaFX needs to be customized with classes that extend existing ones. I tried this approach, for example to add a method to the NumberAxis class that would enable the label of the axis to be rotated.
But I got a "NumberAxis is declared final, can't be extended" compiler error. I wonder how people who extend classes do? Do they have access to the source code of javafx, modify it to make some classes not final, and recompile it? (sounds tricky! )
Making lots of classes final in the JavaFX framework was an intentional decision by the framework developers. To get a flavor of why it's done, see the Making Color Final proposal. That's just an example, there are other reasons. I think experience with subclassing in the Swing framework was that it caused errors and maintenance issues that the JavaFX designers wanted to avoid, so many things are made final.
There are other way to extend functionality than to directly subclass. Some alternatives for your rotation example:
aggregation: include the NumberAxis as a member of new class (e.g. NumberAxisWithRotatableText) which adds an accessor to get the underlying NumberAxis node and a method to perform the rotation (e.g. via a lookup as explained below).
composition: for example extend Pane, add a NumberAxis, disable the standard text drawing on the axis and add rotated labels yourself as needed.
css stylesheet: for example use a selector to lookup the text in the NumberAxis and the -fx-rotate attribute to rotate it.
node lookup: Use a node.lookup to get at the underlying text node, and apply the rotation via an API.
skin: All controls have a skin class attached them, replace the default skin class with a custom one.
subclass an alternate class: Subclass the abstract ValueAxis class rather than the final NumberAxis class.
Source code for JavaFX is available with build instructions. However, I don't recommend hacking a personal copy of the source code to remove final constructs unless you also submit it as an accepted patch to the JavaFX system so that you can be sure that your app won't break on a standard JavaFX install.
If you really think it is a good idea for a given class to be subclassable, then log a change request. Sometimes the JavaFX developers are overzealous and make stuff final which would be better not being final. NumberAxis perhaps falls into that category.

Insert a Monogame view inside MvvmCross monodroid Activity

I'm trying to build a Monogame view inside a RelativeLayout from my MvvmCross monodroid Activity view.
An android Activity inherits from Microsoft.Xna.Framework.AndroidGameActivity to be able to run a Monogame inside a RelativeLayout (working).
My MvvmCross Activity inherits from MvxBindingActivityView(working).
So, I need a way to run the game and bind some datas within the same activity.
Thanks in advance for your help.
Loosely speaking, you can translate any Activity to an MvxActivity by inheriting some interfaces and by then cutting and pasting a small amount of code which does the basic loading and assignation of the ViewModel.
e.g. see the #Region and IMvxAndroidView<TViewModel> added to make MvxActivityView.cs from a normal Activity.
e.g. it's the same region and interface used for adapting a specialised Activity like Google's MapActivity into MvxMapActivityView.cs
At this level, the Activity/View has a ViewModel which can be used in C# code, but has no clever xml inflation - it has no clever Binding support.
Code can be written at this level - I've shipped apps without binding - but many users prefer to add DataBinding too...
To add this DataBinding support, you need to add a bit more code which provides BindingInflate, storage of bindings, disposal of bindings, etc.
e.g. a raw MvxActivityView is extended using the IMvxBindingActivity interface and a #region like: MvxBindingActivityView.cs
e.g. MvxMapActivityView is extended using the same region and interface: MvxBindingMapActivityView.cs
So to extend your the custom AndroidGameActivity:
Inherit from AndroidGameActivity to get ViewModelOwningGameActivity<T> and cut and paste the IMvxAndroidView<TViewModel> interface and #region from MvxActivityView<T> to provide the ViewModel methods, fields and properties.
Then assuming you want binding:
Inherit from ViewModelOwningGameActivity<T> to get BindingGameActivity<T> and cut and paste the IMvxBindingActivity and #region from MvxBindingActivityView<T> to get the binding methods
For specialist Activities you may want to add more - e.g. you may could add some custom helper methods for the MapActivity to plot points and lines, or for GameActivity to do whatever games do... but this is up to individual implementations.
Sorry about the cut and paste of code required in adapting Activities - I have tried to keep this to a minimum. However, writing Mvx is the one time so far that I've really wanted Multiple Inheritance or Mixins in C#

Pharo: #subclass:instanceVariables... message personalized

I am fighting against the Object message #subclass:instanceVariableNames:classVariableNames:poolDictionaries:category, in order to manipulate subclasses of an specific class.
I have RareClass with a class method #subclass:coposes...etc and want to do:
RareClass
subclass: #RareSubclass
composes: #SomeMagic
instanceVariableNames: ''
classVariableNames: ''
in the browser.
whether this method should return a class, (and i think it does) it is not working at all, im getting parsing errors when i try to 'save'...
I guess you are using OmniBrowser with the Refactoring Engine loaded?
The Refactoring Engine enforces the standard class templates to enable undo on any action performed in the browser. Obviously that cannot work with custom class definitions.
As a workaround you can do one of the following:
Use the traditional browser that has no undo functionality (evaluate Browser open),
Get rid of the refactoring functionality in OmniBrowser by unloading the package "OB-Reactory", or
Update the code in RBAddClassChange to support your use case.

Why ATL calls subclassing "superclassing"

Getting interested in learning ATL, I started reading this tutorial and I got confused at reading this (also related):
If you want to extend the capabilities of a predefined window class, such as the button or list box controls, you can superclass it.
Why is it called super-classing instead of sub-classing?
Super-classing is writing a new class, that would behave as it it was a new control altogether. A CColoredButton inheriting from CButton would be superclass, when you create instance of CColoredButton
When you already have a control (probably on resource), you may subclass it. Here the "control" in picture is being handled differently and hence the term subclass.
Any class may behave as super class or subclass. You would call some of SubclassXX function/method to subclass already existing control. You would create instance of a control at runtime by allocating the C++ object (CColoredButton) by calling its Create method - here the term super-class.

Resources