How to use a model-view architecture with pyqtgraph plotting - pyqt

Is it possible to implement a model-view architecture with pyqtgraph? It seems like pyqtgraph forces you along a widget-centric approach (https://doc.qt.io/qt-5/modelview.html) in which each plotitem has its own scene?
It would be great if the ViewBox (see image below from docs -http://www.pyqtgraph.org/documentation/plotting.html) would be an actual "view" of a scene, and the axis items around it would be dependent on that particular view. Therefore, if a number of PlotWidgets/PlotItems were displaying the same PlotDataItems at different zoom-levels, if you changed the zorder or scaled an item this would be shown on all the PlotItems without resorting to sending signals etc between PlotItems.
To do this does the ViewBox have to be an embedded GraphicsView? Or is it possible to have a PlotItem on multiple GraphicsViews?
Thanks!
Some other reference links:
https://doc.qt.io/qt-5/graphicsview.html
https://doc.qt.io/qt-5/model-view-programming.html

Related

Is there any code for an interactive plotting application for a two dimensional curves

Plotting packages offer a variety of methods for displaying data. Write an interactive plotting application for two dimensionsional curves. Your application should be able to allow the user to choose the mode (line strip or polyline display of the data, bar chart or pie charts), colours, and line styles.
You should start with the GUI editation like this:
Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?
and change it to your primitives (more points per primitive instead of one ... handle each point as (sub)object so you can change its position later).
Then just add tools like add object,del object,... For hand drawing tool use piece wise interpolation cubics
The grid can be done like this:
How to draw dynamic 2D grid that adjusts according to camera zoom: OpenGL
Mouse zooming/panning is also important
Zooming graphics based on current mouse position
Putting all above together into simple editor looks like this:
Using GPU for curve rendering might give you some nice speed and functionality boost:
Is it possible to express "t" variable from Cubic Bezier Curve equation?
Mouse selection of objects might be a speed problem if your scene contains too many objects so in such case its best to use index buffers where you can mouse select with pixel perfect precision for almost free in O(1):
OpenGL 3D-raypicking with high poly meshes
The example is for 3D , in 2D is much simpler ...
Also do not forget to implement save/load functionality to some vector file format. I recommend using SVG it might be complicated to start with it but you can quickly check it contents in any SVG viewer or browser also in notepad as its just a text file. If you use just basic path elements and ignore the rest of SVG features you will see the parsing and creating SVG is not that hard for example See these:
Get Vertices/Edges From BMP or SVG (C#)
Discrete probability distribution plot with given values
For really big datasets you might want to use spatial subdivision techniques (Bounding (Volume)Area Hierarchy, or Quad tree) to ease up the operations...
More in depth implementation details about 2D vector gfx editors depends on language, OS, gfx api and GUI api you using and task you are aiming for ...

Overlapping opaque shapes drawn to a graphics object within a translucent container don't show the correct alpha in the overlapping region

I have a PIXI.Graphics inside a PIXI.Container (along with some other stuff, including a mask, and a border). The graphics object is being used to draw various polygons. The alpha property of the Container is set to 0.5. Here is the result:
The bright square is the overlap between two polygons. It seems that even though both polygons were drawn to the same opaque graphics object, it's as though they are separate objects with their own alpha channels.
Is there any way to merge all of the polygons together so that the resulting graphics will have uniform alpha despite some overlapping polygons?
Pixi version is 4.7.3.
You can easily use AlphaFilter to achieve this. See this thread: https://github.com/pixijs/pixi.js/issues/4334
const colorMatrix = new filters.AlphaFilter();
colorMatrix.alpha = 0.5;
container.filters = [colorMatrix];
One solution to this problem in general is to draw all the necessary geometry, then set cacheAsBitmap to true on the Graphics object.
cacheAsBitmap is great for graphics that don't change often, and another benefit to using it is that it speeds up rendering.
Unfortunately, it appears that there could possibly be a bug using cacheAsBitmap with objects that use parent layers or masks which cause all the graphics to disappear if either is set.
In my particular situation, this does not help me because I need masking. Hopefully it helps someone else though.
Edit
The above solution works if you put the graphics inside a container, and apply the mask to the container. I found this out by complete accident while messing around.

Lib GDX Font rendering issues

I use an OrthographicCamera set to 720 by 1280 and then set it's combined matrix as the projection matrix in my SpriteBatch. I then generate a BitmapFont using the FreeTypeFontGenerator and use it to render text.
OrthographicCamera camera = new OrthographicCamera();
camera.setToOrtho(false, 720, 1280);
SpriteBatch batch = new SpriteBatch();
batch.setProjectionMatrix(camera.combined);
This is the output:
As you can see the fonts look very distorted and the only way I found to fix it is by removing the line where I set the projection matrix in my SpriteBatch.
batch.setProjectionMatrix(camera.combined);
I put this as an issue on the lib GDX github page, but I was told this is not because of Lib GDX. I need to use this projection matrix so that I can develop my application in one resolution and have it scale to fit any screen. Is there a way to render text without encountering this problems?
Use a pixel perfect projection, by using ScreenViewport. Then use Table to layout your GUI, including its labels. This way you can also support multiple aspect ratio or even use a different layout depending on the aspect ratio.
If you want to support a wide range of resolution then you will need to provide different assets depending on the resolution. Depending on the file size you might want to use different build flavors or use ResolutionFileHandleResolver. Alternatively you can use the freetype extension to generate the correct font for the device, but be aware that that might result in additional render calls which can affect performance.
Note that all of this only applies for your GUI. For you game logic you obviously use the coordinate system that makes most sense for that (e.g. meters), with a separate camera.

Pygame sprites always on top example?

I was wondering if someone could give me simple example of how to always draw sprites in pygame on --the top layer-- whilst an animation is being blitted on screen?
The senario I'm trying to solve is having an animated sprite (eg a man walking on the spot) and various other objects passing him, whilst he is still animated.
My solution so far has the animation layering on top of the "passing objects" which is not what I want.
Thanks in advance.
I think you've partially answered your own question here.
It's a matter of organizing what gets drawn first. Some side-scrolling 2D games use a "layer" solution, in which there is a background layer, a middleground layer, and a foreground layer, and the drawing system renders one layer after another.
I've also seen Pokémon top-down style games simply sort the sprites by their vertical position, so sprites "nearest" to the camera are drawn last, and thus on top of the other sprites.
See how the current implementation of Scene2D in libGDX gives each Actor a z-index property which can later be used to organize Actors into layers.
Just draw your sprites with a LayeredUpdates group:
class pygame.sprite.LayeredUpdates
LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredUpdates(*spites, **kwargs) -> LayeredUpdates
You can set the default layer through kwargs using ‘default_layer’ and an integer for the layer. The default layer is 0.
If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains ‘layer’ then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.
and give your sprites the correct layer value.

Geogebra resizing shapes without copying

How do I resize a shape without making a copy and using dilate from point?
How my shape with vectors looks like
How do I limit the axes range for exporting?
How do I define a size, say 3 cm for cropping a portion of the graphics view for export?
Also, is there any software for making simulated graphs without any real data? I'm doing image processing and need to make mock-ups such as temporal segments and histograms.
I guess this question is out of scope of stackoverflow and should be asked in https://help.geogebra.org , but I'll try anyway.
1) You can right-click the graphics view and set the dimensions in there. The export will take the current graphics view. Or you can select part of Graphics using the mouse before export. Or use points Export_1, Export_2 as described here
2) You define cropping of graphics view in one of the ways described above to eg. 6 units, then set the export scale to 0.5cm per unit
3) You can do random functions in GeoGebra directly, eg.
Function[Join[{0,5},Sequence[3*sin(k)+RandomUniform[0, 1],k,0,5,0.1]]]

Resources