55# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
66# (see LICENSE file for more details)
77
8+ """
9+ qwt.dyngrid_layout
10+ ------------------
11+
12+ The `dyngrid_layout` module provides the `QwtDynGridLayout` class.
13+
14+ .. autoclass:: QwtDynGridLayout
15+ :members:
16+ """
17+
818from qwt .qt .QtGui import QLayout
919from qwt .qt .QtCore import Qt , QRect , QSize
1020
@@ -24,6 +34,32 @@ def updateLayoutCache(self):
2434 self .isDirty = False
2535
2636class QwtDynGridLayout (QLayout ):
37+ """
38+ The `QwtDynGridLayout` class lays out widgets in a grid,
39+ adjusting the number of columns and rows to the current size.
40+
41+ `QwtDynGridLayout` takes the space it gets, divides it up into rows and
42+ columns, and puts each of the widgets it manages into the correct cell(s).
43+ It lays out as many number of columns as possible (limited by
44+ :py:meth:`maxColumns()`).
45+
46+ .. py:class:: QwtDynGridLayout(parent, margin, [spacing=-1])
47+
48+ :param QWidget parent: parent widget
49+ :param int margin: margin
50+ :param int spacing: spacing
51+
52+ .. py:class:: QwtDynGridLayout(spacing)
53+
54+ :param int spacing: spacing
55+
56+ .. py:class:: QwtDynGridLayout()
57+
58+ Initialize the layout with default values.
59+
60+ :param int spacing: spacing
61+ """
62+
2763 def __init__ (self , * args ):
2864 self .__data = None
2965 parent = None
@@ -47,46 +83,71 @@ def __init__(self, *args):
4783 self .setContentsMargins (margin , margin , margin , margin )
4884
4985 def invalidate (self ):
86+ """Invalidate all internal caches"""
5087 self .__data .isDirty = True
5188 QLayout .invalidate (self )
5289
5390 def setMaxColumns (self , maxColumns ):
91+ """Limit the number of columns"""
5492 self .__data .maxColumns = maxColumns
5593
5694 def maxColumns (self ):
95+ """Return the upper limit for the number of columns"""
5796 return self .__data .maxColumns
5897
5998 def addItem (self , item ):
99+ """Add an item to the next free position"""
60100 self .__data .itemList .append (item )
61101 self .invalidate ()
62102
63103 def isEmpty (self ):
104+ """Return true if this layout is empty"""
64105 return self .count () == 0
65106
66107 def itemCount (self ):
108+ """Return number of layout items"""
67109 return self .count ()
68110
69111 def itemAt (self , index ):
112+ """Find the item at a specific index"""
70113 if index < 0 or index >= len (self .__data .itemList ):
71114 return
72115 return self .__data .itemList [index ]
73116
74117 def takeAt (self , index ):
118+ """Find the item at a specific index and remove it from the layout"""
75119 if index < 0 or index >= len (self .__data .itemList ):
76120 return
77121 self .__data .isDirty = True
78122 return self .__data .itemList .pop (index )
79123
80124 def count (self ):
125+ """Return Number of items in the layout"""
81126 return len (self .__data .itemList )
82127
83128 def setExpandingDirections (self , expanding ):
129+ """
130+ Set whether this layout can make use of more space than sizeHint().
131+ A value of Qt.Vertical or Qt.Horizontal means that it wants to grow in
132+ only one dimension, while Qt.Vertical | Qt.Horizontal means that it
133+ wants to grow in both dimensions. The default value is 0.
134+ """
84135 self .__data .expanding = expanding
85136
86137 def expandingDirections (self ):
138+ """
139+ Returns whether this layout can make use of more space than sizeHint().
140+ A value of Qt.Vertical or Qt.Horizontal means that it wants to grow in
141+ only one dimension, while Qt.Vertical | Qt.Horizontal means that it
142+ wants to grow in both dimensions.
143+ """
87144 return self .__data .expanding
88145
89146 def setGeometry (self , rect ):
147+ """
148+ Reorganizes columns and rows and resizes managed items within a
149+ rectangle.
150+ """
90151 QLayout .setGeometry (self , rect )
91152 if self .isEmpty ():
92153 return
@@ -99,6 +160,12 @@ def setGeometry(self, rect):
99160 it .setGeometry (geo )
100161
101162 def columnsForWidth (self , width ):
163+ """
164+ Calculate the number of columns for a given width.
165+
166+ The calculation tries to use as many columns as possible
167+ ( limited by maxColumns() )
168+ """
102169 if self .isEmpty ():
103170 return 0
104171 maxColumns = self .itemCount ()
@@ -113,6 +180,7 @@ def columnsForWidth(self, width):
113180 return 1
114181
115182 def maxRowWidth (self , numColumns ):
183+ """Calculate the width of a layout for a given number of columns."""
116184 colWidth = [0 ] * numColumns
117185 if self .__data .isDirty :
118186 self .__data .updateLayoutCache ()
@@ -124,13 +192,18 @@ def maxRowWidth(self, numColumns):
124192 return margin_w + (numColumns - 1 )* self .spacing ()+ sum (colWidth )
125193
126194 def maxItemWidth (self ):
195+ """Return the maximum width of all layout items"""
127196 if self .isEmpty ():
128197 return 0
129198 if self .__data .isDirty :
130199 self .__data .updateLayoutCache ()
131200 return max ([hint .width () for hint in self .__data .itemSizeHints ])
132201
133202 def layoutItems (self , rect , numColumns ):
203+ """
204+ Calculate the geometries of the layout items for a layout
205+ with numColumns columns and a given rectangle.
206+ """
134207 itemGeometries = []
135208 if numColumns == 0 or self .isEmpty ():
136209 return itemGeometries
@@ -172,6 +245,10 @@ def layoutItems(self, rect, numColumns):
172245 return itemGeometries
173246
174247 def layoutGrid (self , numColumns , rowHeight , colWidth ):
248+ """
249+ Calculate the dimensions for the columns and rows for a grid
250+ of numColumns columns.
251+ """
175252 if numColumns <= 0 :
176253 return
177254 if self .__data .isDirty :
@@ -190,9 +267,11 @@ def layoutGrid(self, numColumns, rowHeight, colWidth):
190267 colWidth [col ] = max ([colWidth [col ], size .width ()])
191268
192269 def hasHeightForWidth (self ):
270+ """Return true: QwtDynGridLayout implements heightForWidth()."""
193271 return True
194272
195273 def heightForWidth (self , width ):
274+ """Return The preferred height for this layout, given a width."""
196275 if self .isEmpty ():
197276 return 0
198277 numColumns = self .columnsForWidth (width )
@@ -207,6 +286,11 @@ def heightForWidth(self, width):
207286 return margin_h + (numRows - 1 )* self .spacing ()+ sum (rowHeight )
208287
209288 def stretchGrid (self , rect , numColumns , rowHeight , colWidth ):
289+ """
290+ Stretch columns in case of expanding() & QSizePolicy::Horizontal and
291+ rows in case of expanding() & QSizePolicy::Vertical to fill the entire
292+ rect. Rows and columns are stretched with the same factor.
293+ """
210294 if numColumns == 0 or self .isEmpty ():
211295 return
212296 expandH = self .expandingDirections () & Qt .Horizontal
@@ -234,6 +318,11 @@ def stretchGrid(self, rect, numColumns, rowHeight, colWidth):
234318 yDelta -= space
235319
236320 def sizeHint (self ):
321+ """
322+ Return the size hint. If maxColumns() > 0 it is the size for
323+ a grid with maxColumns() columns, otherwise it is the size for
324+ a grid with only one row.
325+ """
237326 if self .isEmpty ():
238327 return QSize ()
239328 numColumns = self .itemCount ()
@@ -253,7 +342,9 @@ def sizeHint(self):
253342 return QSize (w , h )
254343
255344 def numRows (self ):
345+ """Return Number of rows of the current layout."""
256346 return self .__data .numRows
257347
258348 def numColumns (self ):
349+ """Return Number of columns of the current layout."""
259350 return self .__data .numColumns
0 commit comments