qwt_dyngrid_layout.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #include "qwt_dyngrid_layout.h"
  10. #include "qwt_math.h"
  11. #include <qwidget.h>
  12. #include <qlist.h>
  13. class QwtDynGridLayout::PrivateData
  14. {
  15. public:
  16. PrivateData():
  17. isDirty( true )
  18. {
  19. }
  20. void updateLayoutCache();
  21. mutable QList<QLayoutItem*> itemList;
  22. uint maxCols;
  23. uint numRows;
  24. uint numCols;
  25. Qt::Orientations expanding;
  26. bool isDirty;
  27. QVector<QSize> itemSizeHints;
  28. };
  29. void QwtDynGridLayout::PrivateData::updateLayoutCache()
  30. {
  31. itemSizeHints.resize( itemList.count() );
  32. int index = 0;
  33. for ( QList<QLayoutItem*>::iterator it = itemList.begin();
  34. it != itemList.end(); ++it, index++ )
  35. {
  36. itemSizeHints[ index ] = ( *it )->sizeHint();
  37. }
  38. isDirty = false;
  39. }
  40. /*!
  41. \param parent Parent widget
  42. \param margin Margin
  43. \param spacing Spacing
  44. */
  45. QwtDynGridLayout::QwtDynGridLayout( QWidget *parent,
  46. int margin, int spacing ):
  47. QLayout( parent )
  48. {
  49. init();
  50. setSpacing( spacing );
  51. setMargin( margin );
  52. }
  53. /*!
  54. \param spacing Spacing
  55. */
  56. QwtDynGridLayout::QwtDynGridLayout( int spacing )
  57. {
  58. init();
  59. setSpacing( spacing );
  60. }
  61. /*!
  62. Initialize the layout with default values.
  63. */
  64. void QwtDynGridLayout::init()
  65. {
  66. d_data = new QwtDynGridLayout::PrivateData;
  67. d_data->maxCols = d_data->numRows = d_data->numCols = 0;
  68. d_data->expanding = 0;
  69. }
  70. //! Destructor
  71. QwtDynGridLayout::~QwtDynGridLayout()
  72. {
  73. for ( int i = 0; i < d_data->itemList.size(); i++ )
  74. delete d_data->itemList[i];
  75. delete d_data;
  76. }
  77. //! Invalidate all internal caches
  78. void QwtDynGridLayout::invalidate()
  79. {
  80. d_data->isDirty = true;
  81. QLayout::invalidate();
  82. }
  83. /*!
  84. Limit the number of columns.
  85. \param maxCols upper limit, 0 means unlimited
  86. \sa maxCols()
  87. */
  88. void QwtDynGridLayout::setMaxCols( uint maxCols )
  89. {
  90. d_data->maxCols = maxCols;
  91. }
  92. /*!
  93. Return the upper limit for the number of columns.
  94. 0 means unlimited, what is the default.
  95. \sa setMaxCols()
  96. */
  97. uint QwtDynGridLayout::maxCols() const
  98. {
  99. return d_data->maxCols;
  100. }
  101. //! Adds item to the next free position.
  102. void QwtDynGridLayout::addItem( QLayoutItem *item )
  103. {
  104. d_data->itemList.append( item );
  105. invalidate();
  106. }
  107. /*!
  108. \return true if this layout is empty.
  109. */
  110. bool QwtDynGridLayout::isEmpty() const
  111. {
  112. return d_data->itemList.isEmpty();
  113. }
  114. /*!
  115. \return number of layout items
  116. */
  117. uint QwtDynGridLayout::itemCount() const
  118. {
  119. return d_data->itemList.count();
  120. }
  121. /*!
  122. Find the item at a spcific index
  123. \param index Index
  124. \sa takeAt()
  125. */
  126. QLayoutItem *QwtDynGridLayout::itemAt( int index ) const
  127. {
  128. if ( index < 0 || index >= d_data->itemList.count() )
  129. return NULL;
  130. return d_data->itemList.at( index );
  131. }
  132. /*!
  133. Find the item at a spcific index and remove it from the layout
  134. \param index Index
  135. \sa itemAt()
  136. */
  137. QLayoutItem *QwtDynGridLayout::takeAt( int index )
  138. {
  139. if ( index < 0 || index >= d_data->itemList.count() )
  140. return NULL;
  141. d_data->isDirty = true;
  142. return d_data->itemList.takeAt( index );
  143. }
  144. //! \return Number of items in the layout
  145. int QwtDynGridLayout::count() const
  146. {
  147. return d_data->itemList.count();
  148. }
  149. /*!
  150. Set whether this layout can make use of more space than sizeHint().
  151. A value of Qt::Vertical or Qt::Horizontal means that it wants to grow in only
  152. one dimension, while Qt::Vertical | Qt::Horizontal means that it wants
  153. to grow in both dimensions. The default value is 0.
  154. \param expanding Or'd orientations
  155. \sa expandingDirections()
  156. */
  157. void QwtDynGridLayout::setExpandingDirections( Qt::Orientations expanding )
  158. {
  159. d_data->expanding = expanding;
  160. }
  161. /*!
  162. Returns whether this layout can make use of more space than sizeHint().
  163. A value of Qt::Vertical or Qt::Horizontal means that it wants to grow in only
  164. one dimension, while Qt::Vertical | Qt::Horizontal means that it wants
  165. to grow in both dimensions.
  166. \sa setExpandingDirections()
  167. */
  168. Qt::Orientations QwtDynGridLayout::expandingDirections() const
  169. {
  170. return d_data->expanding;
  171. }
  172. /*!
  173. Reorganizes columns and rows and resizes managed widgets within
  174. the rectangle rect.
  175. \param rect Layout geometry
  176. */
  177. void QwtDynGridLayout::setGeometry( const QRect &rect )
  178. {
  179. QLayout::setGeometry( rect );
  180. if ( isEmpty() )
  181. return;
  182. d_data->numCols = columnsForWidth( rect.width() );
  183. d_data->numRows = itemCount() / d_data->numCols;
  184. if ( itemCount() % d_data->numCols )
  185. d_data->numRows++;
  186. QList<QRect> itemGeometries = layoutItems( rect, d_data->numCols );
  187. int index = 0;
  188. for ( QList<QLayoutItem*>::iterator it = d_data->itemList.begin();
  189. it != d_data->itemList.end(); ++it )
  190. {
  191. QWidget *w = ( *it )->widget();
  192. if ( w )
  193. {
  194. w->setGeometry( itemGeometries[index] );
  195. index++;
  196. }
  197. }
  198. }
  199. /*!
  200. Calculate the number of columns for a given width. It tries to
  201. use as many columns as possible (limited by maxCols())
  202. \param width Available width for all columns
  203. \sa maxCols(), setMaxCols()
  204. */
  205. uint QwtDynGridLayout::columnsForWidth( int width ) const
  206. {
  207. if ( isEmpty() )
  208. return 0;
  209. const int maxCols = ( d_data->maxCols > 0 ) ? d_data->maxCols : itemCount();
  210. if ( maxRowWidth( maxCols ) <= width )
  211. return maxCols;
  212. for ( int numCols = 2; numCols <= maxCols; numCols++ )
  213. {
  214. const int rowWidth = maxRowWidth( numCols );
  215. if ( rowWidth > width )
  216. return numCols - 1;
  217. }
  218. return 1; // At least 1 column
  219. }
  220. /*!
  221. Calculate the width of a layout for a given number of
  222. columns.
  223. \param numCols Given number of columns
  224. \param itemWidth Array of the width hints for all items
  225. */
  226. int QwtDynGridLayout::maxRowWidth( int numCols ) const
  227. {
  228. int col;
  229. QVector<int> colWidth( numCols );
  230. for ( col = 0; col < ( int )numCols; col++ )
  231. colWidth[col] = 0;
  232. if ( d_data->isDirty )
  233. d_data->updateLayoutCache();
  234. for ( uint index = 0;
  235. index < ( uint )d_data->itemSizeHints.count(); index++ )
  236. {
  237. col = index % numCols;
  238. colWidth[col] = qMax( colWidth[col],
  239. d_data->itemSizeHints[int( index )].width() );
  240. }
  241. int rowWidth = 2 * margin() + ( numCols - 1 ) * spacing();
  242. for ( col = 0; col < ( int )numCols; col++ )
  243. rowWidth += colWidth[col];
  244. return rowWidth;
  245. }
  246. /*!
  247. \return the maximum width of all layout items
  248. */
  249. int QwtDynGridLayout::maxItemWidth() const
  250. {
  251. if ( isEmpty() )
  252. return 0;
  253. if ( d_data->isDirty )
  254. d_data->updateLayoutCache();
  255. int w = 0;
  256. for ( uint i = 0; i < ( uint )d_data->itemSizeHints.count(); i++ )
  257. {
  258. const int itemW = d_data->itemSizeHints[int( i )].width();
  259. if ( itemW > w )
  260. w = itemW;
  261. }
  262. return w;
  263. }
  264. /*!
  265. Calculate the geometries of the layout items for a layout
  266. with numCols columns and a given rect.
  267. \param rect Rect where to place the items
  268. \param numCols Number of columns
  269. \return item geometries
  270. */
  271. QList<QRect> QwtDynGridLayout::layoutItems( const QRect &rect,
  272. uint numCols ) const
  273. {
  274. QList<QRect> itemGeometries;
  275. if ( numCols == 0 || isEmpty() )
  276. return itemGeometries;
  277. uint numRows = itemCount() / numCols;
  278. if ( numRows % itemCount() )
  279. numRows++;
  280. QVector<int> rowHeight( numRows );
  281. QVector<int> colWidth( numCols );
  282. layoutGrid( numCols, rowHeight, colWidth );
  283. bool expandH, expandV;
  284. expandH = expandingDirections() & Qt::Horizontal;
  285. expandV = expandingDirections() & Qt::Vertical;
  286. if ( expandH || expandV )
  287. stretchGrid( rect, numCols, rowHeight, colWidth );
  288. const int maxCols = d_data->maxCols;
  289. d_data->maxCols = numCols;
  290. const QRect alignedRect = alignmentRect( rect );
  291. d_data->maxCols = maxCols;
  292. const int xOffset = expandH ? 0 : alignedRect.x();
  293. const int yOffset = expandV ? 0 : alignedRect.y();
  294. QVector<int> colX( numCols );
  295. QVector<int> rowY( numRows );
  296. const int xySpace = spacing();
  297. rowY[0] = yOffset + margin();
  298. for ( int r = 1; r < ( int )numRows; r++ )
  299. rowY[r] = rowY[r-1] + rowHeight[r-1] + xySpace;
  300. colX[0] = xOffset + margin();
  301. for ( int c = 1; c < ( int )numCols; c++ )
  302. colX[c] = colX[c-1] + colWidth[c-1] + xySpace;
  303. const int itemCount = d_data->itemList.size();
  304. for ( int i = 0; i < itemCount; i++ )
  305. {
  306. const int row = i / numCols;
  307. const int col = i % numCols;
  308. QRect itemGeometry( colX[col], rowY[row],
  309. colWidth[col], rowHeight[row] );
  310. itemGeometries.append( itemGeometry );
  311. }
  312. return itemGeometries;
  313. }
  314. /*!
  315. Calculate the dimensions for the columns and rows for a grid
  316. of numCols columns.
  317. \param numCols Number of columns.
  318. \param rowHeight Array where to fill in the calculated row heights.
  319. \param colWidth Array where to fill in the calculated column widths.
  320. */
  321. void QwtDynGridLayout::layoutGrid( uint numCols,
  322. QVector<int>& rowHeight, QVector<int>& colWidth ) const
  323. {
  324. if ( numCols <= 0 )
  325. return;
  326. if ( d_data->isDirty )
  327. d_data->updateLayoutCache();
  328. for ( uint index = 0;
  329. index < ( uint )d_data->itemSizeHints.count(); index++ )
  330. {
  331. const int row = index / numCols;
  332. const int col = index % numCols;
  333. const QSize &size = d_data->itemSizeHints[int( index )];
  334. rowHeight[row] = ( col == 0 )
  335. ? size.height() : qMax( rowHeight[row], size.height() );
  336. colWidth[col] = ( row == 0 )
  337. ? size.width() : qMax( colWidth[col], size.width() );
  338. }
  339. }
  340. /*!
  341. \return true: QwtDynGridLayout implements heightForWidth.
  342. \sa heightForWidth()
  343. */
  344. bool QwtDynGridLayout::hasHeightForWidth() const
  345. {
  346. return true;
  347. }
  348. /*!
  349. \return The preferred height for this layout, given the width w.
  350. \sa hasHeightForWidth()
  351. */
  352. int QwtDynGridLayout::heightForWidth( int width ) const
  353. {
  354. if ( isEmpty() )
  355. return 0;
  356. const uint numCols = columnsForWidth( width );
  357. uint numRows = itemCount() / numCols;
  358. if ( itemCount() % numCols )
  359. numRows++;
  360. QVector<int> rowHeight( numRows );
  361. QVector<int> colWidth( numCols );
  362. layoutGrid( numCols, rowHeight, colWidth );
  363. int h = 2 * margin() + ( numRows - 1 ) * spacing();
  364. for ( int row = 0; row < ( int )numRows; row++ )
  365. h += rowHeight[row];
  366. return h;
  367. }
  368. /*!
  369. Stretch columns in case of expanding() & QSizePolicy::Horizontal and
  370. rows in case of expanding() & QSizePolicy::Vertical to fill the entire
  371. rect. Rows and columns are stretched with the same factor.
  372. \sa setExpanding(), expanding()
  373. */
  374. void QwtDynGridLayout::stretchGrid( const QRect &rect,
  375. uint numCols, QVector<int>& rowHeight, QVector<int>& colWidth ) const
  376. {
  377. if ( numCols == 0 || isEmpty() )
  378. return;
  379. bool expandH, expandV;
  380. expandH = expandingDirections() & Qt::Horizontal;
  381. expandV = expandingDirections() & Qt::Vertical;
  382. if ( expandH )
  383. {
  384. int xDelta = rect.width() - 2 * margin() - ( numCols - 1 ) * spacing();
  385. for ( int col = 0; col < ( int )numCols; col++ )
  386. xDelta -= colWidth[col];
  387. if ( xDelta > 0 )
  388. {
  389. for ( int col = 0; col < ( int )numCols; col++ )
  390. {
  391. const int space = xDelta / ( numCols - col );
  392. colWidth[col] += space;
  393. xDelta -= space;
  394. }
  395. }
  396. }
  397. if ( expandV )
  398. {
  399. uint numRows = itemCount() / numCols;
  400. if ( itemCount() % numCols )
  401. numRows++;
  402. int yDelta = rect.height() - 2 * margin() - ( numRows - 1 ) * spacing();
  403. for ( int row = 0; row < ( int )numRows; row++ )
  404. yDelta -= rowHeight[row];
  405. if ( yDelta > 0 )
  406. {
  407. for ( int row = 0; row < ( int )numRows; row++ )
  408. {
  409. const int space = yDelta / ( numRows - row );
  410. rowHeight[row] += space;
  411. yDelta -= space;
  412. }
  413. }
  414. }
  415. }
  416. /*!
  417. Return the size hint. If maxCols() > 0 it is the size for
  418. a grid with maxCols() columns, otherwise it is the size for
  419. a grid with only one row.
  420. \sa maxCols(), setMaxCols()
  421. */
  422. QSize QwtDynGridLayout::sizeHint() const
  423. {
  424. if ( isEmpty() )
  425. return QSize();
  426. const uint numCols = ( d_data->maxCols > 0 ) ? d_data->maxCols : itemCount();
  427. uint numRows = itemCount() / numCols;
  428. if ( itemCount() % numCols )
  429. numRows++;
  430. QVector<int> rowHeight( numRows );
  431. QVector<int> colWidth( numCols );
  432. layoutGrid( numCols, rowHeight, colWidth );
  433. int h = 2 * margin() + ( numRows - 1 ) * spacing();
  434. for ( int row = 0; row < ( int )numRows; row++ )
  435. h += rowHeight[row];
  436. int w = 2 * margin() + ( numCols - 1 ) * spacing();
  437. for ( int col = 0; col < ( int )numCols; col++ )
  438. w += colWidth[col];
  439. return QSize( w, h );
  440. }
  441. /*!
  442. \return Number of rows of the current layout.
  443. \sa numCols()
  444. \warning The number of rows might change whenever the geometry changes
  445. */
  446. uint QwtDynGridLayout::numRows() const
  447. {
  448. return d_data->numRows;
  449. }
  450. /*!
  451. \return Number of columns of the current layout.
  452. \sa numRows()
  453. \warning The number of columns might change whenever the geometry changes
  454. */
  455. uint QwtDynGridLayout::numCols() const
  456. {
  457. return d_data->numCols;
  458. }