Qt 4.8
Public Functions | Public Variables | List of all members
QGridLayoutRowData Class Reference

#include <qgridlayoutengine_p.h>

Public Functions

void calculateGeometries (int start, int end, qreal targetSize, qreal *positions, qreal *sizes, qreal *descents, const QGridLayoutBox &totalBox, const QGridLayoutRowInfo &rowInfo)
 
void distributeMultiCells (const QGridLayoutRowInfo &rowInfo)
 
void dump (int indent=0) const
 
void reset (int count)
 
void stealBox (int start, int end, int which, qreal *positions, qreal *sizes)
 
QGridLayoutBox totalBox (int start, int end) const
 

Public Variables

QVector< QGridLayoutBoxboxes
 
bool hasIgnoreFlag
 
QBitArray ignore
 
MultiCellMap multiCellMap
 
QVector< qrealspacings
 
QVector< int > stretches
 

Detailed Description

Definition at line 229 of file qgridlayoutengine_p.h.

Functions

◆ calculateGeometries()

void QGridLayoutRowData::calculateGeometries ( int  start,
int  end,
qreal  targetSize,
qreal positions,
qreal sizes,
qreal descents,
const QGridLayoutBox totalBox,
const QGridLayoutRowInfo rowInfo 
)

Definition at line 204 of file qgridlayoutengine.cpp.

208 {
209  Q_ASSERT(end > start);
210 
211  targetSize = qMax(totalBox.q_minimumSize, targetSize);
212 
213  int n = end - start;
214  QVarLengthArray<qreal> newSizes(n);
215  QVarLengthArray<qreal> factors(n);
216  qreal sumFactors = 0.0;
217  int sumStretches = 0;
218  qreal sumAvailable;
219 
220  for (int i = 0; i < n; ++i) {
221  if (stretches[start + i] > 0)
222  sumStretches += stretches[start + i];
223  }
224 
225  if (targetSize < totalBox.q_preferredSize) {
226  stealBox(start, end, MinimumSize, positions, sizes);
227 
228  sumAvailable = targetSize - totalBox.q_minimumSize;
229  if (sumAvailable > 0.0) {
230  qreal sumDesired = totalBox.q_preferredSize - totalBox.q_minimumSize;
231 
232  for (int i = 0; i < n; ++i) {
233  if (ignore.testBit(start + i)) {
234  factors[i] = 0.0;
235  continue;
236  }
237 
238  const QGridLayoutBox &box = boxes.at(start + i);
239  qreal desired = box.q_preferredSize - box.q_minimumSize;
240  factors[i] = growthFactorBelowPreferredSize(desired, sumAvailable, sumDesired);
241  sumFactors += factors[i];
242  }
243 
244  for (int i = 0; i < n; ++i) {
245  Q_ASSERT(sumFactors > 0.0);
246  qreal delta = sumAvailable * factors[i] / sumFactors;
247  newSizes[i] = sizes[i] + delta;
248  }
249  }
250  } else {
251  bool isLargerThanMaximum = (targetSize > totalBox.q_maximumSize);
252  if (isLargerThanMaximum) {
253  stealBox(start, end, MaximumSize, positions, sizes);
254  sumAvailable = targetSize - totalBox.q_maximumSize;
255  } else {
256  stealBox(start, end, PreferredSize, positions, sizes);
257  sumAvailable = targetSize - totalBox.q_preferredSize;
258  }
259 
260  if (sumAvailable > 0.0) {
261  qreal sumCurrentAvailable = sumAvailable;
262  bool somethingHasAMaximumSize = false;
263 
264  qreal sumSizes = 0.0;
265  for (int i = 0; i < n; ++i)
266  sumSizes += sizes[i];
267 
268  for (int i = 0; i < n; ++i) {
269  if (ignore.testBit(start + i)) {
270  newSizes[i] = 0.0;
271  factors[i] = 0.0;
272  continue;
273  }
274 
275  const QGridLayoutBox &box = boxes.at(start + i);
276  qreal boxSize;
277 
278  qreal desired;
279  if (isLargerThanMaximum) {
280  boxSize = box.q_maximumSize;
281  desired = rowInfo.boxes.value(start + i).q_maximumSize - boxSize;
282  } else {
283  boxSize = box.q_preferredSize;
284  desired = box.q_maximumSize - boxSize;
285  }
286  if (desired == 0.0) {
287  newSizes[i] = sizes[i];
288  factors[i] = 0.0;
289  } else {
290  Q_ASSERT(desired > 0.0);
291 
292  int stretch = stretches[start + i];
293  if (sumStretches == 0) {
294  if (hasIgnoreFlag) {
295  factors[i] = (stretch < 0) ? qreal(1.0) : qreal(0.0);
296  } else {
297  factors[i] = (stretch < 0) ? sizes[i] : qreal(0.0);
298  }
299  } else if (stretch == sumStretches) {
300  factors[i] = 1.0;
301  } else if (stretch <= 0) {
302  factors[i] = 0.0;
303  } else {
304  qreal ultimateSize;
305  qreal ultimateSumSizes;
306  qreal x = ((stretch * sumSizes)
307  - (sumStretches * boxSize))
308  / (sumStretches - stretch);
309  if (x >= 0.0) {
310  ultimateSize = boxSize + x;
311  ultimateSumSizes = sumSizes + x;
312  } else {
313  ultimateSize = boxSize;
314  ultimateSumSizes = (sumStretches * boxSize)
315  / stretch;
316  }
317 
318  /*
319  We multiply these by 1.5 to give some space for a smooth transition
320  (at the expense of the stretch factors, which are not fully respected
321  during the transition).
322  */
323  ultimateSize = ultimateSize * 3 / 2;
324  ultimateSumSizes = ultimateSumSizes * 3 / 2;
325 
326  qreal beta = ultimateSumSizes - sumSizes;
327  if (!beta) {
328  factors[i] = 1;
329  } else {
330  qreal alpha = qMin(sumCurrentAvailable, beta);
331  qreal ultimateFactor = (stretch * ultimateSumSizes / sumStretches)
332  - (boxSize);
333  qreal transitionalFactor = sumCurrentAvailable * (ultimateSize - boxSize) / beta;
334 
335  factors[i] = ((alpha * ultimateFactor)
336  + ((beta - alpha) * transitionalFactor)) / beta;
337  }
338 
339  }
340  sumFactors += factors[i];
341  if (desired < sumCurrentAvailable)
342  somethingHasAMaximumSize = true;
343 
344  newSizes[i] = -1.0;
345  }
346  }
347 
348  bool keepGoing = somethingHasAMaximumSize;
349  while (keepGoing) {
350  keepGoing = false;
351 
352  for (int i = 0; i < n; ++i) {
353  if (newSizes[i] >= 0.0)
354  continue;
355 
356  qreal maxBoxSize;
357  if (isLargerThanMaximum)
358  maxBoxSize = rowInfo.boxes.value(start + i).q_maximumSize;
359  else
360  maxBoxSize = boxes.at(start + i).q_maximumSize;
361 
362  qreal avail = sumCurrentAvailable * factors[i] / sumFactors;
363  if (sizes[i] + avail >= maxBoxSize) {
364  newSizes[i] = maxBoxSize;
365  sumCurrentAvailable -= maxBoxSize - sizes[i];
366  sumFactors -= factors[i];
367  keepGoing = (sumCurrentAvailable > 0.0);
368  if (!keepGoing)
369  break;
370  }
371  }
372  }
373 
374  for (int i = 0; i < n; ++i) {
375  if (newSizes[i] < 0.0) {
376  qreal delta = (sumFactors == 0.0) ? qreal(0.0)
377  : sumCurrentAvailable * factors[i] / sumFactors;
378  newSizes[i] = sizes[i] + delta;
379  }
380  }
381  }
382  }
383 
384  if (sumAvailable > 0) {
385  qreal offset = 0;
386  for (int i = 0; i < n; ++i) {
387  qreal delta = newSizes[i] - sizes[i];
388  positions[i] += offset;
389  sizes[i] += delta;
390  offset += delta;
391  }
392 
393 #if 0 // some "pixel allocation"
394  int surplus = targetSize - (positions[n - 1] + sizes[n - 1]);
395  Q_ASSERT(surplus >= 0 && surplus <= n);
396 
397  int prevSurplus = -1;
398  while (surplus > 0 && surplus != prevSurplus) {
399  prevSurplus = surplus;
400 
401  int offset = 0;
402  for (int i = 0; i < n; ++i) {
403  const QGridLayoutBox &box = boxes.at(start + i);
404  int delta = (!ignore.testBit(start + i) && surplus > 0
405  && factors[i] > 0 && sizes[i] < box.q_maximumSize)
406  ? 1 : 0;
407 
408  positions[i] += offset;
409  sizes[i] += delta;
410  offset += delta;
411  surplus -= delta;
412  }
413  }
414  Q_ASSERT(surplus == 0);
415 #endif
416  }
417 
418  if (descents) {
419  for (int i = 0; i < n; ++i) {
420  if (ignore.testBit(start + i))
421  continue;
422  const QGridLayoutBox &box = boxes.at(start + i);
423  descents[i] = fixedDescent(box.q_minimumDescent, box.q_minimumAscent, sizes[i]);
424  }
425  }
426 }
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
static qreal growthFactorBelowPreferredSize(qreal desired, qreal sumAvailable, qreal sumDesired)
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QVector< QGridLayoutBox > boxes
static const QCssKnownValue positions[NumKnownPositionModes - 1]
Definition: qcssparser.cpp:329
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
QVector< int > stretches
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
static qreal fixedDescent(qreal descent, qreal ascent, qreal targetSize)
void stealBox(int start, int end, int which, qreal *positions, qreal *sizes)
QVector< QGridLayoutBox > boxes
static const KeyPair *const end

◆ distributeMultiCells()

void QGridLayoutRowData::distributeMultiCells ( const QGridLayoutRowInfo rowInfo)

Definition at line 169 of file qgridlayoutengine.cpp.

Referenced by QGridLayoutEngine::ensureColumnAndRowData().

170 {
172  for (; i != multiCellMap.constEnd(); ++i) {
173  int start = i.key().first;
174  int span = i.key().second;
175  int end = start + span;
176  const QGridLayoutBox &box = i.value().q_box;
177  int stretch = i.value().q_stretch;
178 
179  QGridLayoutBox totalBox = this->totalBox(start, end);
180  QVarLengthArray<QGridLayoutBox> extras(span);
181  QVarLengthArray<qreal> dummy(span);
182  QVarLengthArray<qreal> newSizes(span);
183 
184  for (int j = 0; j < NSizes; ++j) {
185  qreal extra = compare(box, totalBox, j);
186  if (extra > 0.0) {
187  calculateGeometries(start, end, box.q_sizes(j), dummy.data(), newSizes.data(),
188  0, totalBox, rowInfo);
189 
190  for (int k = 0; k < span; ++k)
191  extras[k].q_sizes(j) = newSizes[k];
192  }
193  }
194 
195  for (int k = 0; k < span; ++k) {
196  boxes[start + k].combine(extras[k]);
197  if (stretch != 0)
198  stretches[start + k] = qMax(stretches[start + k], stretch);
199  }
200  }
202 }
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QVector< QGridLayoutBox > boxes
QVector< int > stretches
QGridLayoutBox totalBox(int start, int end) const
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
qreal & q_sizes(int which)
void calculateGeometries(int start, int end, qreal targetSize, qreal *positions, qreal *sizes, qreal *descents, const QGridLayoutBox &totalBox, const QGridLayoutRowInfo &rowInfo)
static const KeyPair *const end
static qreal compare(const QGridLayoutBox &box1, const QGridLayoutBox &box2, int which)
void clear()
Removes all items from the map.
Definition: qmap.h:444

◆ dump()

void QGridLayoutRowData::dump ( int  indent = 0) const

Definition at line 466 of file qgridlayoutengine.cpp.

467 {
468  qDebug("%*sData", indent, "");
469 
470  for (int i = 0; i < ignore.count(); ++i) {
471  qDebug("%*s Row %d (stretch %d, spacing %g)", indent, "", i, stretches.at(i),
472  spacings.at(i));
473  if (ignore.testBit(i))
474  qDebug("%*s Ignored", indent, "");
475  boxes.at(i).dump(indent + 2);
476  }
477 
479  while (it != multiCellMap.constEnd()) {
480  qDebug("%*s Multi-cell entry <%d, %d> (stretch %d)", indent, "", it.key().first,
481  it.key().second, it.value().q_stretch);
482  it.value().q_box.dump(indent + 2);
483  }
484 }
#define it(className, varName)
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
Q_CORE_EXPORT void qDebug(const char *,...)
QVector< QGridLayoutBox > boxes
QVector< int > stretches
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< qreal > spacings
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
void dump(int indent=0) const
int count() const
Same as size().
Definition: qbitarray.h:74

◆ reset()

void QGridLayoutRowData::reset ( int  count)

Definition at line 159 of file qgridlayoutengine.cpp.

Referenced by QGridLayoutEngine::ensureColumnAndRowData().

160 {
161  ignore.fill(false, count);
162  boxes.fill(QGridLayoutBox(), count);
164  stretches.fill(0, count);
165  spacings.fill(0.0, count);
166  hasIgnoreFlag = false;
167 }
QVector< T > & fill(const T &t, int size=-1)
Assigns value to all items in the vector.
Definition: qvector.h:665
bool fill(bool val, int size=-1)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false...
Definition: qbitarray.h:117
QVector< QGridLayoutBox > boxes
QVector< int > stretches
QVector< qreal > spacings
void clear()
Removes all items from the map.
Definition: qmap.h:444

◆ stealBox()

void QGridLayoutRowData::stealBox ( int  start,
int  end,
int  which,
qreal positions,
qreal sizes 
)

Definition at line 444 of file qgridlayoutengine.cpp.

445 {
446  qreal offset = 0.0;
447  qreal nextSpacing = 0.0;
448 
449  for (int i = start; i < end; ++i) {
450  qreal avail = 0.0;
451 
452  if (!ignore.testBit(i)) {
453  const QGridLayoutBox &box = boxes.at(i);
454  avail = box.q_sizes(which);
455  offset += nextSpacing;
456  nextSpacing = spacings.at(i);
457  }
458 
459  *positions++ = offset;
460  *sizes++ = avail;
461  offset += avail;
462  }
463 }
double qreal
Definition: qglobal.h:1193
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
QVector< QGridLayoutBox > boxes
static const QCssKnownValue positions[NumKnownPositionModes - 1]
Definition: qcssparser.cpp:329
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< qreal > spacings
qreal & q_sizes(int which)
static const KeyPair *const end

◆ totalBox()

QGridLayoutBox QGridLayoutRowData::totalBox ( int  start,
int  end 
) const

Definition at line 428 of file qgridlayoutengine.cpp.

Referenced by QGridLayoutEngine::ensureColumnAndRowData().

429 {
430  QGridLayoutBox result;
431  if (start < end) {
432  result.q_maximumSize = 0.0;
433  qreal nextSpacing = 0.0;
434  for (int i = start; i < end; ++i) {
435  if (ignore.testBit(i))
436  continue;
437  result.add(boxes.at(i), stretches.at(i), nextSpacing);
438  nextSpacing = spacings.at(i);
439  }
440  }
441  return result;
442 }
double qreal
Definition: qglobal.h:1193
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
QVector< QGridLayoutBox > boxes
QVector< int > stretches
void add(const QGridLayoutBox &other, int stretch, qreal spacing)
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< qreal > spacings
static const KeyPair *const end

Properties

◆ boxes

QVector<QGridLayoutBox> QGridLayoutRowData::boxes

Definition at line 245 of file qgridlayoutengine_p.h.

Referenced by QGridLayoutEngine::fillRowData().

◆ hasIgnoreFlag

bool QGridLayoutRowData::hasIgnoreFlag

Definition at line 249 of file qgridlayoutengine_p.h.

Referenced by QGridLayoutEngine::fillRowData().

◆ ignore

QBitArray QGridLayoutRowData::ignore

Definition at line 244 of file qgridlayoutengine_p.h.

Referenced by QGridLayoutEngine::fillRowData().

◆ multiCellMap

MultiCellMap QGridLayoutRowData::multiCellMap

Definition at line 246 of file qgridlayoutengine_p.h.

Referenced by QGridLayoutEngine::fillRowData().

◆ spacings

QVector<qreal> QGridLayoutRowData::spacings

Definition at line 248 of file qgridlayoutengine_p.h.

Referenced by QGridLayoutEngine::fillRowData().

◆ stretches

QVector<int> QGridLayoutRowData::stretches

Definition at line 247 of file qgridlayoutengine_p.h.

Referenced by QGridLayoutEngine::fillRowData().


The documentation for this class was generated from the following files: