Coverage for src/plotly_gtk/widgets/base.py: 12%

78 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-08 21:22 +0000

1import gi 

2 

3gi.require_version("Gdk", "4.0") 

4gi.require_version("Gtk", "4.0") 

5from typing import TYPE_CHECKING 

6 

7from gi.repository import Gdk, Gtk # noqa: E402 

8 

9if TYPE_CHECKING: 

10 from plotly_gtk.chart import PlotlyGtk 

11 

12 

13class Base(Gtk.Box): 

14 def __init__(self): 

15 super().__init__( 

16 vexpand=False, 

17 hexpand=False, 

18 valign=Gtk.Align.CENTER, 

19 halign=Gtk.Align.CENTER, 

20 margin_start=0, 

21 margin_end=0, 

22 margin_top=0, 

23 margin_bottom=0, 

24 ) 

25 

26 def get_position(self, plot: "PlotlyGtk", allocation: Gdk.Rectangle) -> bool: 

27 content_width = plot.get_width() 

28 content_height = plot.get_height() 

29 

30 margin = plot.layout["margin"] 

31 _margin = plot.layout["_margin"] 

32 preferred_size = self.get_preferred_size()[-1] 

33 width = preferred_size.width 

34 height = preferred_size.height 

35 

36 _x = _margin["l"] + self.spec["x"] * ( 

37 content_width - _margin["l"] - _margin["r"] 

38 ) 

39 _y = _margin["t"] + (1 - self.spec["y"]) * ( 

40 content_height - _margin["t"] - _margin["b"] 

41 ) 

42 

43 if "xref" in self.spec: 

44 if self.spec["xref"] == "container": 

45 _x = self.spec["x"] * content_width 

46 elif self.spec["xref"] == "paper": 

47 pass 

48 else: 

49 raise ValueError(f"Unknown xref: {self.spec["xref"]}") 

50 if "yref" in self.spec: 

51 if self.spec["yref"] == "container": 

52 _y = self.spec["y"] * content_width 

53 elif self.spec["yref"] == "paper": 

54 pass 

55 else: 

56 raise ValueError(f"Unknown yref: {self.spec["yref"]}") 

57 

58 if "xoffset" in self.spec: 

59 _x = _x + self.spec["xoffset"] 

60 if "yoffset" in self.spec: 

61 _y = _y + self.spec["yoffset"] 

62 

63 if self.spec["xanchor"] == "center": 

64 _x = _x - width / 2 

65 elif self.spec["xanchor"] == "right": 

66 _x = _x - width 

67 

68 if self.spec["yanchor"] == "middle": 

69 _y = _y - height / 2 

70 elif self.spec["yanchor"] == "bottom": 

71 _y = _y - height 

72 

73 x = margin["l"] + self.spec["x"] * (content_width - margin["l"] - margin["r"]) 

74 y = margin["t"] + (1 - self.spec["y"]) * ( 

75 content_height - margin["t"] - margin["b"] 

76 ) 

77 if "xoffset" in self.spec: 

78 x = x + self.spec["xoffset"] 

79 if "yoffset" in self.spec: 

80 y = y + self.spec["yoffset"] 

81 

82 if self.spec["xanchor"] == "center": 

83 x = x - width / 2 

84 elif self.spec["xanchor"] == "right": 

85 x = x - width 

86 

87 if self.spec["yanchor"] == "middle": 

88 y = y - height / 2 

89 elif self.spec["yanchor"] == "bottom": 

90 y = y - height 

91 

92 allocation.x = _x 

93 allocation.y = _y 

94 

95 allocation.width = width 

96 allocation.height = height 

97 

98 plot.pushmargin[self] = dict( 

99 l=(x - margin["l"]) / (content_width - margin["l"] - margin["r"]), 

100 r=(x + width - margin["l"]) / (content_width - margin["l"] - margin["r"]), 

101 t=(y - margin["t"]) / (content_height - margin["t"] - margin["b"]), 

102 b=(y + height - margin["t"]) / (content_height - margin["t"] - margin["b"]), 

103 ) 

104 if not "xref" in self.spec or self.spec["xref"] == "paper": 

105 plot.pushmargin[self]["x"] = self.spec["x"] 

106 plot.pushmargin[self]["xl"] = ( 

107 0 

108 if self.spec["xanchor"] == "left" 

109 else width if self.spec["xanchor"] == "right" else width / 2 

110 ) 

111 plot.pushmargin[self]["xr"] = ( 

112 0 

113 if self.spec["xanchor"] == "right" 

114 else width if self.spec["xanchor"] == "left" else width / 2 

115 ) 

116 if not "yref" in self.spec or self.spec["yref"] == "paper": 

117 plot.pushmargin[self]["y"] = self.spec["y"] 

118 plot.pushmargin[self]["yt"] = ( 

119 0 

120 if self.spec["yanchor"] == "top" 

121 else height if self.spec["yanchor"] == "bottom" else height / 2 

122 ) 

123 plot.pushmargin[self]["yb"] = ( 

124 0 

125 if self.spec["yanchor"] == "bottom" 

126 else height if self.spec["yanchor"] == "top" else height / 2 

127 ) 

128 if ( 

129 "xoffset" in self.spec 

130 and "xl" in plot.pushmargin[self] 

131 and "xr" in plot.pushmargin[self] 

132 ): 

133 plot.pushmargin[self]["xl"] = ( 

134 plot.pushmargin[self]["xl"] - self.spec["xoffset"] 

135 ) 

136 plot.pushmargin[self]["xr"] = ( 

137 plot.pushmargin[self]["xr"] + self.spec["xoffset"] 

138 ) 

139 if ( 

140 "yoffset" in self.spec 

141 and "yt" in plot.pushmargin[self] 

142 and "yb" in plot.pushmargin[self] 

143 ): 

144 plot.pushmargin[self]["yt"] = ( 

145 plot.pushmargin[self]["yt"] + self.spec["yoffset"] 

146 ) 

147 plot.pushmargin[self]["yb"] = ( 

148 plot.pushmargin[self]["yb"] - self.spec["yoffset"] 

149 ) 

150 return True