GtkPaned widget for arbitrary geometries? - gtk

I would like my application window to be divided into rectangles with sides perpendicular to the window borders. The number of rectangles would normally be quite large, and the user should be able to resize the rectangles.
Is there a Gtk widget which would allow for that? GTkPaned comes close - by embedding several GtkPaned widgets one can get such rectangle disivisons, but not all of them are possible - one obvious constraint is that there must be an edge which spans the whole window either horizontally or vertically. The simplest arrangement I know of which doesn't have this property, and so can't be built with Gtkpaned, is: one square in the middle and four rectangles of the same size each, around the square.
Is there a widget which allows for such arbitrary resizable rectangle arrangements in Gtk?

If you don't have to have the dividing lines draggable, then use a GtkGrid:
grid = Gtk.Grid()
grid.attach(widget1, 0, 0, 3, 1)
grid.attach(widget2, 3, 0, 1, 3)
grid.attach(widget3, 0, 1, 1, 3)
grid.attach(widget4, 1, 3, 3, 1)
grid.attach(widget5, 1, 1, 2, 2)
# 1112
# 3552
# 3552
# 3444

Related

Altair: How to make scatter plot aligned with image background created by mark_image?

I'm looking for a working example to have a .png picture as the background of a scatter chart.
Currently, I use mark_image to draw the background image:
source = pd.DataFrame.from_records([
{"x": 0, "y": 0,
"img": "http://localhost:8888/files/BARTStreamlit/assets/BARTtracksmap.png?_xsrf=2%7Ce13c35be%7Ce013c83479b892363239e5b6a77d97dc%7C1652400559"}
])
tracksmap = alt.Chart(source).mark_image(
width=500,
height=500
).encode(
x='x',
y='y',
url='img'
)
tracksmap
Here is the resulted image drown:
and draw the scater chart,
chart = alt.Chart(maptable).mark_circle(size=60).encode(
x= 'x',
y= 'y',
tooltip=['short_name', 'ENTRY']
).interactive()
chart
I have scaled the x, y channel values for the scatter chart to be in the range of [0, 500]. 500 is the width and height of the background image that I guessed.
Here is the resulted scatter plot:
then I combined the two chart with layer mechanism:
geovizvega = alt.layer(tracksmap, chart)
geovizvega
resulting the following:
The two charts do not align. I'd like to have the scatter dots aligning with the tracks on the background image. How can I achieve that?
To have them aligned, I might need to have the background image's top left corner at the coordinates (0, 0), how can I achieve that? (It seems that the x, y channel values for mark_image is the coordinates of the center of the image? With accurate definition of the x, y channel values, it might be possible to calculate the proper value of x, and y for the top left coroner to be at (0, 0)).
I might need to to have precise dimension of the background image. How?
My above approach may not be the right one. Please show me a working example.
Yes, if you change the values of x and y in your image plot to something like y=-200 and x=200, the image should be more centered in the scatter plot.
You can also change the anchor point of the image using align and baseline:
import altair as alt
import pandas as pd
source = pd.DataFrame.from_records([
{"x": 2, "y": 2, "img": "https://vega.github.io/vega-datasets/data/7zip.png"}
])
imgs = alt.Chart(source).mark_image(
width=100,
height=100
).encode(
x='x',
y='y',
url='img'
)
imgs + imgs.mark_circle(size=200, color='red', opacity=1)
imgs = alt.Chart(source).mark_image(
width=100,
height=100,
align='right',
baseline='top'
).encode(
x='x',
y='y',
url='img'
)
imgs + imgs.mark_circle(size=200, color='red', opacity=1)
After this, you would still need to change the dimensions of the chart so that it has the same size as the image. The default is width=400 and height=300. You can get the dimensions of your image in most image editing software or using the file <imagename> command (at least on linux). But even after getting these dimensions, you would have to do some manual adjustments due to axes taking up some of that space in the chart.

how to determine cairo drawing with line width even?

I use a cr (from Gtk's widget draw event) and I want to create a rectangle (or a line) using an even number of pixels (2,4,6 etc.) without any «context transformations». According to this the line will be "around the path". And according to this "the diameter of a pen that is circular".
But in a rectangle will it be less outside and more inside or the opposite? And in a line will be up,down, left or right?
I understand that in an odd line width, "around the path" means 1 in the center and the rest are equally around.
But in an even line width, as when the line width is 2, will be 1 pixel inside the path or outside?
Is there a stable way to determine the pixels affected or it is random?
The walk around of creating two times every stroke'ing, first with line width 1 and then by using the remainder (an odd) number is pain killing and time consuming.
But in a rectangle will it be less outside and more inside or the opposite? And in a line will be up,down, left or right?
The line will have the same width on either side of the path. If this does not align with the pixel grid, you get some anti-aliased result.
I understand that in an odd line width, "around the path" means 1 in the center and the rest are equally around. But in an even line width, as when the line width is 2, will be 1 pixel inside the path or outside?
With a line width of 3, 1.5 pixels will be drawn on either side of the path.
With a line width of 4, 2 pixels are drawn on either side of the path.
Perhaps the following example makes this clearer. This is written in Lua and uses LGI as cairo bindings for Lua, but this maps directly to the C API:
local cairo = require("lgi").cairo
s = cairo.ImageSurface(cairo.Format.RGB24, 100, 30)
cr = cairo.Context(s)
cr:set_source_rgb(1, 1, 1)
cr:paint()
cr:set_source_rgb(0, 0, 0)
cr:set_line_width(2)
cr:rectangle(5, 10, 5, 5)
cr:stroke()
cr:set_line_width(6)
cr:rectangle(15, 10, 14, 14)
cr:stroke()
cr:set_line_width(7)
cr:rectangle(40.5, 10.5, 14, 14)
cr:stroke()
cr:set_line_width(7)
cr:rectangle(70, 10, 14, 14)
cr:stroke()
s:write_to_png("out.png")
The resulting image is:
The first rectangle has a line width of 2. It is drawn with integer coordinates, so that there is e.g. a line from (5, 10) to (10, 10) (the top line). Half the line width is drawn on either side of the line, so this line corresponds to a "filled rectangle" from (4, 9) to (6, 11).
The last rectangle has a line width of 7 and is also drawn with integer coordinates. Its top line goes from (70, 10) to (70, 24). Since half the line width is on either side of the line, the "filled rectangle" goes from (66.5, 6.5) to (73.5, 27.5). These numbers are not integers and you can see in the result that some anti-aliasing was applied.
In contrast, the second to last rectangle has its position shifted by 0.5. This causes the "filled rectangle" for its "top line" to end up on the pixel grid again.
See also this FAQ entry: https://www.cairographics.org/FAQ/#sharp_lines

How do I compute the convolution input shape/size?

If I have the output shape, filter shape, strides and padding,
filter shape: [kernel_height, kernel_width, output_depth, input_depth]
output shape: [batch, height, width, depth]
strides=[1,1,1,1]
padding='VALID'
Can I get the input shape?
For example ,
filter shape: [3, 3, 1, 1]
output shape: [1, 1, 1, 1]
Can I compute the fixed input shape [1,3,3,1] and How ?
Do you have the code to compute the shape? Because I think I need not to write it myself..
It's batch, height + kernel_height - 1, width + kernel_width - 1, input_depth
batch at the beginning is somewhat obvious, so is input_depth at the end. To understand height + kernel_height - 1, consider how kernel is applied. If you input image was say 10 by 10 and you applied a 3 by 3 kernel, horizontally you would apply it at positions 0, 1, ..., 7, a total of 8 different positions, similar thinking applies to how kernel moves vertically, which would result in output map of size 8x8. If you generalize this thinking, you will see that the size of the output map is width + kernel - 1, height + kernel - 1, which means that if you have the size of the output map, to get the size of the input you need to invert the operation, which will result in width - kernel + 1, height - kernel + 1.
This is all only valid for padding type "VALID". If the type was "SAME", the output would be padded to match the dimensions of the input, and as such the input shape would be batch, height, width, input_depht

Microsoft charting stacked area still visible even when y-values are all 0

Here is my problem. Let's say I have 2 stacked area series and their data looks like this :
Series A (Color blue) :
X values {1, 2, 3, 4}
Y values {4, 6, 7, 6}
Series B (Color red) :
X values {1, 2, 3, 4}
Y values {0, 0, 0, 0}
If I had these 2 series to a chart (A first and B second), there is still a red line that appears on top of the blue area even if all the y-values of the B series are 0. Is there a way to make sure that the red line doesn't appear in the graph without removing the B-series from the legend?
Format the data series to have a fill only and no border. If the areas have a border, it will show even if the values are 0.

CGAffineTransform help, flipping a label

I basically have a pie chart where I have lines coming out of each segment of the pie chart. So in the case where the line comes out of the circle to the left, when I draw my text, it is reversed. "100%" would look like => "%001" (Note, the 1 and % sign are actually drawn in reverse to, like if a mirror. So the little overhang on top of the 1 points to the right, rather than the left.)
I tried reading through Apple's docs for the AffineTransform, but it doesn't make complete sense to me. I tried making this transformation matrix to start:
CGAffineTransform transform1 = CGAffineTransformMake(-1, 0, 0, 1, 0, 0);
This does flip the text around its x-axis so the text now looks correct on the left side of the circle. However, the text is now on the line, rather than at the end of the line like it originally was. So I thought I could translate it by moving the text in the x-axis direction by changing the tx value in the matrix. So instead of using the above matrix, I used this:
CGAffineTransform transform1 = CGAffineTransformMake(-1, 0, 0, 1, -strlen(t1AsChar), 0);
However, the text just stays where it's at. What am I doing wrong? Thanks.
strlen() doesn't give you the size of the rendered text box, it just gives you the length of the string itself (how many characters that string has). If you're using a UITextField you can use textField.frame.size.width instead.