KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1555 lines
41KB

  1. /* -*- Mode: C++ -*-
  2. $Id$
  3. */
  4. /****************************************************************************
  5. ** Copyright (C) 2002-2004 Klarälvdalens Datakonsult AB. All rights reserved.
  6. **
  7. ** This file is part of the KDGantt library.
  8. **
  9. ** This file may be distributed and/or modified under the terms of the
  10. ** GNU General Public License version 2 as published by the Free Software
  11. ** Foundation and appearing in the file LICENSE.GPL included in the
  12. ** packaging of this file.
  13. **
  14. ** Licensees holding valid commercial KDGantt licenses may use this file in
  15. ** accordance with the KDGantt Commercial License Agreement provided with
  16. ** the Software.
  17. **
  18. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  19. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. **
  21. ** See http://www.klaralvdalens-datakonsult.se/Public/products/ for
  22. ** information about KDGantt Commercial License Agreements.
  23. **
  24. ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
  25. ** licensing are not clear to you.
  26. **
  27. ** As a special exception, permission is given to link this program
  28. ** with any edition of TQt, and distribute the resulting executable,
  29. ** without including the source code for TQt in the source distribution.
  30. **
  31. **********************************************************************/
  32. #include "KDGanttMinimizeSplitter.h"
  33. #ifndef TQT_NO_SPLITTER
  34. #include "tqpainter.h"
  35. #include "tqdrawutil.h"
  36. #include "tqbitmap.h"
  37. #include "tqptrlist.h"
  38. #include "tqmemarray.h"
  39. #include "tqlayout.h"
  40. #include "tqlayoutengine_p.h"
  41. #include "tqobjectlist.h"
  42. #include "tqstyle.h"
  43. #include "tqapplication.h" //sendPostedEvents
  44. #include <tqvaluelist.h>
  45. #include <tqcursor.h>
  46. #ifndef KDGANTT_MASTER_CVS
  47. #include "KDGanttMinimizeSplitter.moc"
  48. #endif
  49. #ifndef DOXYGEN_SKIP_INTERNAL
  50. static int mouseOffset;
  51. static int opaqueOldPos = -1; //### there's only one mouse, but this is a bit risky
  52. KDGanttSplitterHandle::KDGanttSplitterHandle( Qt::Orientation o,
  53. KDGanttMinimizeSplitter *parent, const char * name )
  54. : TQWidget( parent, name ), _activeButton( 0 ), _collapsed( false )
  55. {
  56. s = parent;
  57. setOrientation(o);
  58. setMouseTracking( true );
  59. }
  60. TQSize KDGanttSplitterHandle::sizeHint() const
  61. {
  62. return TQSize(8,8);
  63. }
  64. void KDGanttSplitterHandle::setOrientation( Qt::Orientation o )
  65. {
  66. orient = o;
  67. #ifndef TQT_NO_CURSOR
  68. if ( o == Qt::Horizontal )
  69. setCursor( splitHCursor );
  70. else
  71. setCursor( splitVCursor );
  72. #endif
  73. }
  74. void KDGanttSplitterHandle::mouseMoveEvent( TQMouseEvent *e )
  75. {
  76. updateCursor( e->pos() );
  77. if ( !(e->state()&Qt::LeftButton) )
  78. return;
  79. if ( _activeButton != 0)
  80. return;
  81. TQCOORD pos = s->pick(parentWidget()->mapFromGlobal(e->globalPos()))
  82. - mouseOffset;
  83. if ( opaque() ) {
  84. s->moveSplitter( pos, id() );
  85. } else {
  86. int min = pos; int max = pos;
  87. s->getRange( id(), &min, &max );
  88. s->setRubberband( TQMAX( min, TQMIN(max, pos )));
  89. }
  90. _collapsed = false;
  91. }
  92. void KDGanttSplitterHandle::mousePressEvent( TQMouseEvent *e )
  93. {
  94. if ( e->button() == Qt::LeftButton ) {
  95. _activeButton = onButton( e->pos() );
  96. mouseOffset = s->pick(e->pos());
  97. if ( _activeButton != 0)
  98. repaint();
  99. updateCursor( e->pos() );
  100. }
  101. }
  102. void KDGanttSplitterHandle::updateCursor( const TQPoint& p)
  103. {
  104. if ( onButton( p ) != 0 ) {
  105. setCursor( arrowCursor );
  106. }
  107. else {
  108. if ( orient == Qt::Horizontal )
  109. setCursor( splitHCursor );
  110. else
  111. setCursor( splitVCursor );
  112. }
  113. }
  114. void KDGanttSplitterHandle::mouseReleaseEvent( TQMouseEvent *e )
  115. {
  116. if ( _activeButton != 0 ) {
  117. if ( onButton( e->pos() ) == _activeButton )
  118. {
  119. int pos;
  120. int min, max;
  121. if ( !_collapsed ) {
  122. s->expandPos( id(), &min, &max );
  123. if ( s->minimizeDirection() == KDGanttMinimizeSplitter::Left
  124. || s->minimizeDirection() == KDGanttMinimizeSplitter::Up ) {
  125. pos = min;
  126. }
  127. else {
  128. pos = max;
  129. }
  130. _origPos = s->pick(mapToParent( TQPoint( 0,0 ) ));
  131. s->moveSplitter( pos, id() );
  132. _collapsed = true;
  133. }
  134. else {
  135. s->moveSplitter( _origPos, id() );
  136. _collapsed = false;
  137. }
  138. }
  139. _activeButton = 0;
  140. updateCursor( e->pos() );
  141. }
  142. else {
  143. if ( !opaque() && e->button() == Qt::LeftButton ) {
  144. TQCOORD pos = s->pick(parentWidget()->mapFromGlobal(e->globalPos()))
  145. - mouseOffset;
  146. s->setRubberband( -1 );
  147. s->moveSplitter( pos, id() );
  148. }
  149. }
  150. repaint();
  151. }
  152. int KDGanttSplitterHandle::onButton( const TQPoint& p )
  153. {
  154. TQValueList<TQPointArray> list = buttonRegions();
  155. int index = 1;
  156. for( TQValueList<TQPointArray>::Iterator it = list.begin(); it != list.end(); ++it ) {
  157. TQRect rect = (*it).boundingRect();
  158. rect.setLeft( rect.left()- 4 );
  159. rect.setRight( rect.right() + 4);
  160. rect.setTop( rect.top()- 4 );
  161. rect.setBottom( rect.bottom() + 4);
  162. if ( rect.contains( p ) ) {
  163. return index;
  164. }
  165. index++;
  166. }
  167. return 0;
  168. }
  169. TQValueList<TQPointArray> KDGanttSplitterHandle::buttonRegions()
  170. {
  171. TQValueList<TQPointArray> list;
  172. int sw = 8;
  173. int voffset[] = { (int) -sw*3, (int) sw*3 };
  174. for ( int i = 0; i < 2; i++ ) {
  175. TQPointArray arr;
  176. if ( !_collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Right ||
  177. _collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Left) {
  178. int mid = height()/2 + voffset[i];
  179. arr.setPoints( 3,
  180. 1, mid - sw + 4,
  181. sw-3, mid,
  182. 1, mid + sw -4);
  183. }
  184. else if ( !_collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Left ||
  185. _collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Right ) {
  186. int mid = height()/2 + voffset[i];
  187. arr.setPoints( 3,
  188. sw-4, mid - sw + 4,
  189. 0, mid,
  190. sw-4, mid + sw - 4);
  191. }
  192. else if ( !_collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Up ||
  193. _collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Down) {
  194. int mid = width()/2 + voffset[i];
  195. arr.setPoints( 3,
  196. mid - sw + 4, sw-4,
  197. mid, 0,
  198. mid + sw - 4, sw-4 );
  199. }
  200. else if ( !_collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Down ||
  201. _collapsed && s->minimizeDirection() == KDGanttMinimizeSplitter::Up ) {
  202. int mid = width()/2 + voffset[i];
  203. arr.setPoints( 3,
  204. mid - sw + 4, 1,
  205. mid, sw-3,
  206. mid + sw -4, 1);
  207. }
  208. list.append( arr );
  209. }
  210. return list;
  211. }
  212. void KDGanttSplitterHandle::paintEvent( TQPaintEvent * )
  213. {
  214. TQPixmap buffer( size() );
  215. TQPainter p( &buffer );
  216. // Draw the splitter rectangle
  217. p.setBrush( colorGroup().background() );
  218. p.setPen( colorGroup().foreground() );
  219. p.drawRect( rect() );
  220. parentWidget()->style().tqdrawPrimitive( TQStyle::PE_Panel, &p, rect(),
  221. parentWidget()->colorGroup());
  222. int sw = 8; // Hardcoded, given I didn't use styles anymore, I didn't like to use their size
  223. // arrow color
  224. TQColor col = colorGroup().background().dark( 200 );
  225. p.setBrush( col );
  226. p.setPen( col );
  227. TQValueList<TQPointArray> list = buttonRegions();
  228. int index = 1;
  229. for ( TQValueList<TQPointArray>::Iterator it = list.begin(); it != list.end(); ++it ) {
  230. if ( index == _activeButton ) {
  231. p.save();
  232. p.translate( parentWidget()->style().pixelMetric( TQStyle::PM_ButtonShiftHorizontal ),
  233. parentWidget()->style().pixelMetric( TQStyle::PM_ButtonShiftVertical ) );
  234. p.drawPolygon( *it, true );
  235. p.restore();
  236. }
  237. else {
  238. p.drawPolygon( *it, true );
  239. }
  240. index++;
  241. }
  242. // Draw the lines between the arrows
  243. if ( s->minimizeDirection() == KDGanttMinimizeSplitter::Left ||
  244. s->minimizeDirection() == KDGanttMinimizeSplitter::Right ) {
  245. int mid = height()/2;
  246. p.drawLine ( 2, mid - sw, 2, mid + sw );
  247. p.drawLine ( 4, mid - sw, 4, mid + sw );
  248. }
  249. else if ( s->minimizeDirection() == KDGanttMinimizeSplitter::Up ||
  250. s->minimizeDirection() == KDGanttMinimizeSplitter::Down ) {
  251. int mid = width()/2;
  252. p.drawLine( mid -sw, 2, mid +sw, 2 );
  253. p.drawLine( mid -sw, 4, mid +sw, 4 );
  254. }
  255. bitBlt( this, 0, 0, &buffer );
  256. }
  257. class KDGanttSplitterLayoutStruct
  258. {
  259. public:
  260. KDGanttMinimizeSplitter::ResizeMode mode;
  261. TQCOORD sizer;
  262. bool isSplitter;
  263. TQWidget *wid;
  264. };
  265. class TQSplitterData
  266. {
  267. public:
  268. TQSplitterData() : opaque( FALSE ), firstShow( TRUE ) {}
  269. TQPtrList<KDGanttSplitterLayoutStruct> list;
  270. bool opaque;
  271. bool firstShow;
  272. };
  273. void kdganttGeomCalc( TQMemArray<TQLayoutStruct> &chain, int start, int count, int pos,
  274. int space, int spacer );
  275. #endif // DOXYGEN_SKIP_INTERNAL
  276. /*!
  277. \class KDGanttMinimizeSplitter KDGanttMinimizeSplitter.h
  278. \brief The KDGanttMinimizeSplitter class implements a splitter
  279. widget with minimize buttons.
  280. This class (and its documentation) is largely a copy of TQt's
  281. TQSplitter; the copying was necessary because TQSplitter is not
  282. extensible at all. TQSplitter and its documentation are licensed
  283. according to the GPL and the TQt Professional License (if you hold
  284. such a license) and are (C) Trolltech AS.
  285. A splitter lets the user control the size of child widgets by
  286. dragging the boundary between the children. Any number of widgets
  287. may be controlled.
  288. To show a TQListBox, a TQListView and a TQTextEdit side by side:
  289. \code
  290. KDGanttMinimizeSplitter *split = new KDGanttMinimizeSplitter( parent );
  291. TQListBox *lb = new TQListBox( split );
  292. TQListView *lv = new TQListView( split );
  293. TQTextEdit *ed = new TQTextEdit( split );
  294. \endcode
  295. In KDGanttMinimizeSplitter, the boundary can be either horizontal or
  296. vertical. The default is horizontal (the children are side by side)
  297. but you can use setOrientation( TQSplitter::Vertical ) to set it to
  298. vertical.
  299. Use setResizeMode() to specify
  300. that a widget should keep its size when the splitter is resized.
  301. Although KDGanttMinimizeSplitter normally resizes the children only
  302. at the end of a resize operation, if you call setOpaqueResize( TRUE
  303. ) the widgets are resized as often as possible.
  304. The initial distribution of size between the widgets is determined
  305. by the initial size of each widget. You can also use setSizes() to
  306. set the sizes of all the widgets. The function sizes() returns the
  307. sizes set by the user.
  308. If you hide() a child, its space will be distributed among the other
  309. children. It will be reinstated when you show() it again. It is also
  310. possible to reorder the widgets within the splitter using
  311. moveToFirst() and moveToLast().
  312. */
  313. static TQSize minSize( const TQWidget* /*w*/ )
  314. {
  315. return TQSize(0,0);
  316. }
  317. // This is the original version of minSize
  318. static TQSize minSizeHint( const TQWidget* w )
  319. {
  320. TQSize min = w->minimumSize();
  321. TQSize s;
  322. if ( min.height() <= 0 || min.width() <= 0 )
  323. s = w->minimumSizeHint();
  324. if ( min.height() > 0 )
  325. s.setHeight( min.height() );
  326. if ( min.width() > 0 )
  327. s.setWidth( min.width() );
  328. return s.expandedTo(TQSize(0,0));
  329. }
  330. /*!
  331. Constructs a horizontal splitter with the \a parent and \a
  332. name arguments being passed on to the TQFrame constructor.
  333. */
  334. KDGanttMinimizeSplitter::KDGanttMinimizeSplitter( TQWidget *parent, const char *name )
  335. :TQFrame(parent,name,WPaintUnclipped)
  336. {
  337. orient =Qt::Horizontal;
  338. init();
  339. }
  340. /*!
  341. Constructs a splitter with orientation \a o with the \a parent
  342. and \a name arguments being passed on to the TQFrame constructor.
  343. */
  344. KDGanttMinimizeSplitter::KDGanttMinimizeSplitter( Qt::Orientation o, TQWidget *parent, const char *name )
  345. :TQFrame(parent,name,WPaintUnclipped)
  346. {
  347. orient = o;
  348. init();
  349. }
  350. /*!
  351. Destroys the splitter and any children.
  352. */
  353. KDGanttMinimizeSplitter::~KDGanttMinimizeSplitter()
  354. {
  355. data->list.setAutoDelete( TRUE );
  356. delete data;
  357. }
  358. void KDGanttMinimizeSplitter::init()
  359. {
  360. data = new TQSplitterData;
  361. if ( orient ==Qt::Horizontal )
  362. setSizePolicy( TQSizePolicy(TQSizePolicy::Expanding,TQSizePolicy::Minimum) );
  363. else
  364. setSizePolicy( TQSizePolicy(TQSizePolicy::Minimum,TQSizePolicy::Expanding) );
  365. }
  366. /*!
  367. \brief the orientation of the splitter
  368. By default the orientation is horizontal (the widgets are side by side).
  369. The possible orientations are TQt:Vertical and Qt::Horizontal (the default).
  370. */
  371. void KDGanttMinimizeSplitter::setOrientation( Qt::Orientation o )
  372. {
  373. if ( orient == o )
  374. return;
  375. orient = o;
  376. if ( orient ==Qt::Horizontal )
  377. setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Minimum ) );
  378. else
  379. setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
  380. KDGanttSplitterLayoutStruct *s = data->list.first();
  381. while ( s ) {
  382. if ( s->isSplitter )
  383. ((KDGanttSplitterHandle*)s->wid)->setOrientation( o );
  384. s = data->list.next(); // ### next at end of loop, no iterator
  385. }
  386. recalc( isVisible() );
  387. }
  388. /*!
  389. Reimplemented from superclass.
  390. */
  391. void KDGanttMinimizeSplitter::resizeEvent( TQResizeEvent * )
  392. {
  393. doResize();
  394. }
  395. /*
  396. Inserts the widget \a w at the end (or at the beginning if \a first
  397. is TRUE) of the splitter's list of widgets.
  398. It is the responsibility of the caller of this function to make sure
  399. that \a w is not already in the splitter and to call recalcId if
  400. needed. (If \a first is TRUE, then recalcId is very probably
  401. needed.)
  402. */
  403. KDGanttSplitterLayoutStruct *KDGanttMinimizeSplitter::addWidget( TQWidget *w, bool first )
  404. {
  405. KDGanttSplitterLayoutStruct *s;
  406. KDGanttSplitterHandle *newHandle = 0;
  407. if ( data->list.count() > 0 ) {
  408. s = new KDGanttSplitterLayoutStruct;
  409. s->mode = KeepSize;
  410. TQString tmp = "qt_splithandle_";
  411. tmp += w->name();
  412. newHandle = new KDGanttSplitterHandle( orientation(), this, tmp.latin1() );
  413. s->wid = newHandle;
  414. newHandle->setId(data->list.count());
  415. s->isSplitter = TRUE;
  416. s->sizer = pick( newHandle->sizeHint() );
  417. if ( first )
  418. data->list.insert( 0, s );
  419. else
  420. data->list.append( s );
  421. }
  422. s = new KDGanttSplitterLayoutStruct;
  423. s->mode = Stretch;
  424. s->wid = w;
  425. if ( !testWState( WState_Resized ) && w->sizeHint().isValid() )
  426. s->sizer = pick( w->sizeHint() );
  427. else
  428. s->sizer = pick( w->size() );
  429. s->isSplitter = FALSE;
  430. if ( first )
  431. data->list.insert( 0, s );
  432. else
  433. data->list.append( s );
  434. if ( newHandle && isVisible() )
  435. newHandle->show(); //will trigger sending of post events
  436. return s;
  437. }
  438. /*!
  439. Tells the splitter that a child widget has been inserted or removed.
  440. The event is passed in \a c.
  441. */
  442. void KDGanttMinimizeSplitter::childEvent( TQChildEvent *c )
  443. {
  444. if ( c->type() == TQEvent::ChildInserted ) {
  445. if ( !c->child()->isWidgetType() )
  446. return;
  447. if ( ((TQWidget*)c->child())->testWFlags( WType_TopLevel ) )
  448. return;
  449. KDGanttSplitterLayoutStruct *s = data->list.first();
  450. while ( s ) {
  451. if ( s->wid == c->child() )
  452. return;
  453. s = data->list.next();
  454. }
  455. addWidget( (TQWidget*)c->child() );
  456. recalc( isVisible() );
  457. } else if ( c->type() == TQEvent::ChildRemoved ) {
  458. KDGanttSplitterLayoutStruct *p = 0;
  459. if ( data->list.count() > 1 )
  460. p = data->list.at(1); //remove handle _after_ first widget.
  461. KDGanttSplitterLayoutStruct *s = data->list.first();
  462. while ( s ) {
  463. if ( s->wid == c->child() ) {
  464. data->list.removeRef( s );
  465. delete s;
  466. if ( p && p->isSplitter ) {
  467. data->list.removeRef( p );
  468. delete p->wid; //will call childEvent
  469. delete p;
  470. }
  471. recalcId();
  472. doResize();
  473. return;
  474. }
  475. p = s;
  476. s = data->list.next();
  477. }
  478. }
  479. }
  480. /*!
  481. Shows a rubber band at position \a p. If \a p is negative, the
  482. rubber band is removed.
  483. */
  484. void KDGanttMinimizeSplitter::setRubberband( int p )
  485. {
  486. TQPainter paint( this );
  487. paint.setPen( gray );
  488. paint.setBrush( gray );
  489. paint.setRasterOp( XorROP );
  490. TQRect r = contentsRect();
  491. const int rBord = 3; //Themable????
  492. int sw = style().pixelMetric(TQStyle::PM_SplitterWidth, this);
  493. if ( orient ==Qt::Horizontal ) {
  494. if ( opaqueOldPos >= 0 )
  495. paint.drawRect( opaqueOldPos + sw/2 - rBord , r.y(),
  496. 2*rBord, r.height() );
  497. if ( p >= 0 )
  498. paint.drawRect( p + sw/2 - rBord, r.y(), 2*rBord, r.height() );
  499. } else {
  500. if ( opaqueOldPos >= 0 )
  501. paint.drawRect( r.x(), opaqueOldPos + sw/2 - rBord,
  502. r.width(), 2*rBord );
  503. if ( p >= 0 )
  504. paint.drawRect( r.x(), p + sw/2 - rBord, r.width(), 2*rBord );
  505. }
  506. opaqueOldPos = p;
  507. }
  508. /*! Reimplemented from superclass. */
  509. bool KDGanttMinimizeSplitter::event( TQEvent *e )
  510. {
  511. if ( e->type() == TQEvent::LayoutHint || ( e->type() == TQEvent::Show && data->firstShow ) ) {
  512. recalc( isVisible() );
  513. if ( e->type() == TQEvent::Show )
  514. data->firstShow = FALSE;
  515. }
  516. return TQWidget::event( e );
  517. }
  518. /*!
  519. \obsolete
  520. Draws the splitter handle in the rectangle described by \a x, \a y,
  521. \a w, \a h using painter \a p.
  522. \sa TQStyle::drawPrimitive()
  523. */
  524. void KDGanttMinimizeSplitter::drawSplitter( TQPainter *p,
  525. TQCOORD x, TQCOORD y, TQCOORD w, TQCOORD h )
  526. {
  527. style().tqdrawPrimitive(TQStyle::PE_Splitter, p, TQRect(x, y, w, h), colorGroup(),
  528. (orientation() == Qt::Horizontal ?
  529. TQStyle::Style_Horizontal : 0));
  530. }
  531. /*!
  532. Returns the id of the splitter to the right of or below the widget \a w,
  533. or 0 if there is no such splitter
  534. (i.e. it is either not in this KDGanttMinimizeSplitter or it is at the end).
  535. */
  536. int KDGanttMinimizeSplitter::idAfter( TQWidget* w ) const
  537. {
  538. KDGanttSplitterLayoutStruct *s = data->list.first();
  539. bool seen_w = FALSE;
  540. while ( s ) {
  541. if ( s->isSplitter && seen_w )
  542. return data->list.at();
  543. if ( !s->isSplitter && s->wid == w )
  544. seen_w = TRUE;
  545. s = data->list.next();
  546. }
  547. return 0;
  548. }
  549. /*!
  550. Moves the left/top edge of the splitter handle with id \a id as
  551. close as possible to position \a p, which is the distance from the
  552. left (or top) edge of the widget.
  553. For Arabic and Hebrew the layout is reversed, and using this
  554. function to set the position of the splitter might lead to
  555. unexpected results, since in Arabic and Hebrew the position of
  556. splitter one is to the left of the position of splitter zero.
  557. \sa idAfter()
  558. */
  559. void KDGanttMinimizeSplitter::moveSplitter( TQCOORD p, int id )
  560. {
  561. p = adjustPos( p, id );
  562. KDGanttSplitterLayoutStruct *s = data->list.at(id);
  563. int oldP = orient ==Qt::Horizontal ? s->wid->x() : s->wid->y();
  564. bool upLeft;
  565. if ( TQApplication::reverseLayout() && orient ==Qt::Horizontal ) {
  566. p += s->wid->width();
  567. upLeft = p > oldP;
  568. } else
  569. upLeft = p < oldP;
  570. moveAfter( p, id, upLeft );
  571. moveBefore( p-1, id-1, upLeft );
  572. storeSizes();
  573. }
  574. void KDGanttMinimizeSplitter::setG( TQWidget *w, int p, int s, bool isSplitter )
  575. {
  576. if ( orient ==Qt::Horizontal ) {
  577. if ( TQApplication::reverseLayout() && orient ==Qt::Horizontal && !isSplitter )
  578. p = contentsRect().width() - p - s;
  579. w->setGeometry( p, contentsRect().y(), s, contentsRect().height() );
  580. } else
  581. w->setGeometry( contentsRect().x(), p, contentsRect().width(), s );
  582. }
  583. /*
  584. Places the right/bottom edge of the widget at \a id at position \a pos.
  585. \sa idAfter()
  586. */
  587. void KDGanttMinimizeSplitter::moveBefore( int pos, int id, bool upLeft )
  588. {
  589. if( id < 0 )
  590. return;
  591. KDGanttSplitterLayoutStruct *s = data->list.at(id);
  592. if ( !s )
  593. return;
  594. TQWidget *w = s->wid;
  595. if ( w->isHidden() ) {
  596. moveBefore( pos, id-1, upLeft );
  597. } else if ( s->isSplitter ) {
  598. int pos1, pos2;
  599. int dd = s->sizer;
  600. if( TQApplication::reverseLayout() && orient ==Qt::Horizontal ) {
  601. pos1 = pos;
  602. pos2 = pos + dd;
  603. } else {
  604. pos2 = pos - dd;
  605. pos1 = pos2 + 1;
  606. }
  607. if ( upLeft ) {
  608. setG( w, pos1, dd, TRUE );
  609. moveBefore( pos2, id-1, upLeft );
  610. } else {
  611. moveBefore( pos2, id-1, upLeft );
  612. setG( w, pos1, dd, TRUE );
  613. }
  614. } else {
  615. int dd, newLeft, nextPos;
  616. if( TQApplication::reverseLayout() && orient ==Qt::Horizontal ) {
  617. dd = w->geometry().right() - pos;
  618. dd = TQMAX( pick(minSize(w)), TQMIN(dd, pick(w->maximumSize())));
  619. newLeft = pos+1;
  620. nextPos = newLeft + dd;
  621. } else {
  622. dd = pos - pick( w->pos() ) + 1;
  623. dd = TQMAX( pick(minSize(w)), TQMIN(dd, pick(w->maximumSize())));
  624. newLeft = pos-dd+1;
  625. nextPos = newLeft - 1;
  626. }
  627. setG( w, newLeft, dd, TRUE );
  628. moveBefore( nextPos, id-1, upLeft );
  629. }
  630. }
  631. /*
  632. Places the left/top edge of the widget at \a id at position \a pos.
  633. \sa idAfter()
  634. */
  635. void KDGanttMinimizeSplitter::moveAfter( int pos, int id, bool upLeft )
  636. {
  637. KDGanttSplitterLayoutStruct *s = id < int(data->list.count()) ?
  638. data->list.at(id) : 0;
  639. if ( !s )
  640. return;
  641. TQWidget *w = s->wid;
  642. if ( w->isHidden() ) {
  643. moveAfter( pos, id+1, upLeft );
  644. } else if ( pick( w->pos() ) == pos ) {
  645. //No need to do anything if it's already there.
  646. return;
  647. } else if ( s->isSplitter ) {
  648. int dd = s->sizer;
  649. int pos1, pos2;
  650. if( TQApplication::reverseLayout() && orient ==Qt::Horizontal ) {
  651. pos2 = pos - dd;
  652. pos1 = pos2 + 1;
  653. } else {
  654. pos1 = pos;
  655. pos2 = pos + dd;
  656. }
  657. if ( upLeft ) {
  658. setG( w, pos1, dd, TRUE );
  659. moveAfter( pos2, id+1, upLeft );
  660. } else {
  661. moveAfter( pos2, id+1, upLeft );
  662. setG( w, pos1, dd, TRUE );
  663. }
  664. } else {
  665. int left = pick( w->pos() );
  666. int right, dd,/* newRight,*/ newLeft, nextPos;
  667. if ( TQApplication::reverseLayout() && orient ==Qt::Horizontal ) {
  668. dd = pos - left + 1;
  669. dd = TQMAX( pick(minSize(w)), TQMIN(dd, pick(w->maximumSize())));
  670. newLeft = pos-dd+1;
  671. nextPos = newLeft - 1;
  672. } else {
  673. right = pick( w->geometry().bottomRight() );
  674. dd = right - pos + 1;
  675. dd = TQMAX( pick(minSize(w)), TQMIN(dd, pick(w->maximumSize())));
  676. /*newRight = pos+dd-1;*/
  677. newLeft = pos;
  678. nextPos = newLeft + dd;
  679. }
  680. setG( w, newLeft, dd, TRUE );
  681. /*if( right != newRight )*/
  682. moveAfter( nextPos, id+1, upLeft );
  683. }
  684. }
  685. void KDGanttMinimizeSplitter::expandPos( int id, int* min, int* max )
  686. {
  687. KDGanttSplitterLayoutStruct *s = data->list.at(id-1);
  688. TQWidget* w = s->wid;
  689. *min = pick( w->mapToParent( TQPoint(0,0) ) );
  690. if ( (uint) id == data->list.count() ) {
  691. pick( size() );
  692. }
  693. else {
  694. KDGanttSplitterLayoutStruct *s = data->list.at(id+1);
  695. TQWidget* w = s->wid;
  696. *max = pick( w->mapToParent( TQPoint( w->width(), w->height() ) ) ) -8;
  697. }
  698. }
  699. /*!
  700. Returns the valid range of the splitter with id \a id in \a *min and \a *max.
  701. \sa idAfter()
  702. */
  703. void KDGanttMinimizeSplitter::getRange( int id, int *min, int *max )
  704. {
  705. int minB = 0; //before
  706. int maxB = 0;
  707. int minA = 0;
  708. int maxA = 0; //after
  709. int n = data->list.count();
  710. if ( id < 0 || id >= n )
  711. return;
  712. int i;
  713. for ( i = 0; i < id; i++ ) {
  714. KDGanttSplitterLayoutStruct *s = data->list.at(i);
  715. if ( s->wid->isHidden() ) {
  716. //ignore
  717. } else if ( s->isSplitter ) {
  718. minB += s->sizer;
  719. maxB += s->sizer;
  720. } else {
  721. minB += pick( minSize(s->wid) );
  722. maxB += pick( s->wid->maximumSize() );
  723. }
  724. }
  725. for ( i = id; i < n; i++ ) {
  726. KDGanttSplitterLayoutStruct *s = data->list.at(i);
  727. if ( s->wid->isHidden() ) {
  728. //ignore
  729. } else if ( s->isSplitter ) {
  730. minA += s->sizer;
  731. maxA += s->sizer;
  732. } else {
  733. minA += pick( minSize(s->wid) );
  734. maxA += pick( s->wid->maximumSize() );
  735. }
  736. }
  737. TQRect r = contentsRect();
  738. if ( orient ==Qt::Horizontal && TQApplication::reverseLayout() ) {
  739. int splitterWidth = style().pixelMetric(TQStyle::PM_SplitterWidth, this);
  740. if ( min )
  741. *min = pick(r.topRight()) - TQMIN( maxB, pick(r.size())-minA ) - splitterWidth;
  742. if ( max )
  743. *max = pick(r.topRight()) - TQMAX( minB, pick(r.size())-maxA ) - splitterWidth;
  744. } else {
  745. if ( min )
  746. *min = pick(r.topLeft()) + TQMAX( minB, pick(r.size())-maxA );
  747. if ( max )
  748. *max = pick(r.topLeft()) + TQMIN( maxB, pick(r.size())-minA );
  749. }
  750. }
  751. /*!
  752. Returns the closest legal position to \a p of the splitter with id \a id.
  753. \sa idAfter()
  754. */
  755. int KDGanttMinimizeSplitter::adjustPos( int p, int id )
  756. {
  757. int min = 0;
  758. int max = 0;
  759. getRange( id, &min, &max );
  760. p = TQMAX( min, TQMIN( p, max ) );
  761. return p;
  762. }
  763. void KDGanttMinimizeSplitter::doResize()
  764. {
  765. TQRect r = contentsRect();
  766. int i;
  767. int n = data->list.count();
  768. TQMemArray<TQLayoutStruct> a( n );
  769. for ( i = 0; i< n; i++ ) {
  770. a[i].init();
  771. KDGanttSplitterLayoutStruct *s = data->list.at(i);
  772. if ( s->wid->isHidden() ) {
  773. a[i].stretch = 0;
  774. a[i].sizeHint = a[i].minimumSize = 0;
  775. a[i].maximumSize = 0;
  776. } else if ( s->isSplitter ) {
  777. a[i].stretch = 0;
  778. a[i].sizeHint = a[i].minimumSize = a[i].maximumSize = s->sizer;
  779. a[i].empty = FALSE;
  780. } else if ( s->mode == KeepSize ) {
  781. a[i].stretch = 0;
  782. a[i].minimumSize = pick( minSize(s->wid) );
  783. a[i].sizeHint = s->sizer;
  784. a[i].maximumSize = pick( s->wid->maximumSize() );
  785. a[i].empty = FALSE;
  786. } else if ( s->mode == FollowSizeHint ) {
  787. a[i].stretch = 0;
  788. a[i].minimumSize = a[i].sizeHint = pick( s->wid->sizeHint() );
  789. a[i].maximumSize = pick( s->wid->maximumSize() );
  790. a[i].empty = FALSE;
  791. } else { //proportional
  792. a[i].stretch = s->sizer;
  793. a[i].maximumSize = pick( s->wid->maximumSize() );
  794. a[i].sizeHint = a[i].minimumSize = pick( minSize(s->wid) );
  795. a[i].empty = FALSE;
  796. }
  797. }
  798. kdganttGeomCalc( a, 0, n, pick( r.topLeft() ), pick( r.size() ), 0 );
  799. for ( i = 0; i< n; i++ ) {
  800. KDGanttSplitterLayoutStruct *s = data->list.at(i);
  801. setG( s->wid, a[i].pos, a[i].size );
  802. }
  803. }
  804. void KDGanttMinimizeSplitter::recalc( bool update )
  805. {
  806. int fi = 2*frameWidth();
  807. int maxl = fi;
  808. int minl = fi;
  809. int maxt = TQWIDGETSIZE_MAX;
  810. int mint = fi;
  811. int n = data->list.count();
  812. bool first = TRUE;
  813. /*
  814. The splitter before a hidden widget is always hidden.
  815. The splitter before the first visible widget is hidden.
  816. The splitter before any other visible widget is visible.
  817. */
  818. for ( int i = 0; i< n; i++ ) {
  819. KDGanttSplitterLayoutStruct *s = data->list.at(i);
  820. if ( !s->isSplitter ) {
  821. KDGanttSplitterLayoutStruct *p = (i > 0) ? data->list.at( i-1 ) : 0;
  822. if ( p && p->isSplitter )
  823. if ( first || s->wid->isHidden() )
  824. p->wid->hide(); //may trigger new recalc
  825. else
  826. p->wid->show(); //may trigger new recalc
  827. if ( !s->wid->isHidden() )
  828. first = FALSE;
  829. }
  830. }
  831. bool empty=TRUE;
  832. for ( int j = 0; j< n; j++ ) {
  833. KDGanttSplitterLayoutStruct *s = data->list.at(j);
  834. if ( !s->wid->isHidden() ) {
  835. empty = FALSE;
  836. if ( s->isSplitter ) {
  837. minl += s->sizer;
  838. maxl += s->sizer;
  839. } else {
  840. TQSize minS = minSize(s->wid);
  841. minl += pick( minS );
  842. maxl += pick( s->wid->maximumSize() );
  843. mint = TQMAX( mint, trans( minS ));
  844. int tm = trans( s->wid->maximumSize() );
  845. if ( tm > 0 )
  846. maxt = TQMIN( maxt, tm );
  847. }
  848. }
  849. }
  850. if ( empty ) {
  851. if ( parentWidget() != 0 && parentWidget()->inherits("KDGanttMinimizeSplitter") ) {
  852. // nested splitters; be nice
  853. maxl = maxt = 0;
  854. } else {
  855. // KDGanttMinimizeSplitter with no children yet
  856. maxl = TQWIDGETSIZE_MAX;
  857. }
  858. } else {
  859. maxl = TQMIN( maxl, TQWIDGETSIZE_MAX );
  860. }
  861. if ( maxt < mint )
  862. maxt = mint;
  863. if ( orient ==Qt::Horizontal ) {
  864. setMaximumSize( maxl, maxt );
  865. setMinimumSize( minl, mint );
  866. } else {
  867. setMaximumSize( maxt, maxl );
  868. setMinimumSize( mint, minl );
  869. }
  870. if ( update )
  871. doResize();
  872. }
  873. /*!
  874. Sets resize mode of \a w to \a mode.
  875. \sa ResizeMode
  876. */
  877. void KDGanttMinimizeSplitter::setResizeMode( TQWidget *w, ResizeMode mode )
  878. {
  879. processChildEvents();
  880. KDGanttSplitterLayoutStruct *s = data->list.first();
  881. while ( s ) {
  882. if ( s->wid == w ) {
  883. s->mode = mode;
  884. return;
  885. }
  886. s = data->list.next();
  887. }
  888. s = addWidget( w, TRUE );
  889. s->mode = mode;
  890. }
  891. /*!
  892. Returns TRUE if opaque resize is on; otherwise returns FALSE.
  893. \sa setOpaqueResize()
  894. */
  895. bool KDGanttMinimizeSplitter::opaqueResize() const
  896. {
  897. return data->opaque;
  898. }
  899. /*!
  900. If \a on is TRUE then opaque resizing is turned on; otherwise
  901. opaque resizing is turned off.
  902. Opaque resizing is initially turned off.
  903. \sa opaqueResize()
  904. */
  905. void KDGanttMinimizeSplitter::setOpaqueResize( bool on )
  906. {
  907. data->opaque = on;
  908. }
  909. /*!
  910. Moves widget \a w to the leftmost/top position.
  911. */
  912. void KDGanttMinimizeSplitter::moveToFirst( TQWidget *w )
  913. {
  914. processChildEvents();
  915. bool found = FALSE;
  916. KDGanttSplitterLayoutStruct *s = data->list.first();
  917. while ( s ) {
  918. if ( s->wid == w ) {
  919. found = TRUE;
  920. KDGanttSplitterLayoutStruct *p = data->list.prev();
  921. if ( p ) { // not already at first place
  922. data->list.take(); //take p
  923. data->list.take(); // take s
  924. data->list.insert( 0, p );
  925. data->list.insert( 0, s );
  926. }
  927. break;
  928. }
  929. s = data->list.next();
  930. }
  931. if ( !found )
  932. addWidget( w, TRUE );
  933. recalcId();
  934. }
  935. /*!
  936. Moves widget \a w to the rightmost/bottom position.
  937. */
  938. void KDGanttMinimizeSplitter::moveToLast( TQWidget *w )
  939. {
  940. processChildEvents();
  941. bool found = FALSE;
  942. KDGanttSplitterLayoutStruct *s = data->list.first();
  943. while ( s ) {
  944. if ( s->wid == w ) {
  945. found = TRUE;
  946. data->list.take(); // take s
  947. KDGanttSplitterLayoutStruct *p = data->list.current();
  948. if ( p ) { // the splitter handle after s
  949. data->list.take(); //take p
  950. data->list.append( p );
  951. }
  952. data->list.append( s );
  953. break;
  954. }
  955. s = data->list.next();
  956. }
  957. if ( !found )
  958. addWidget( w);
  959. recalcId();
  960. }
  961. void KDGanttMinimizeSplitter::recalcId()
  962. {
  963. int n = data->list.count();
  964. for ( int i = 0; i < n; i++ ) {
  965. KDGanttSplitterLayoutStruct *s = data->list.at(i);
  966. if ( s->isSplitter )
  967. ((KDGanttSplitterHandle*)s->wid)->setId(i);
  968. }
  969. }
  970. /*! Reimplemented from superclass.
  971. */
  972. TQSize KDGanttMinimizeSplitter::sizeHint() const
  973. {
  974. constPolish();
  975. int l = 0;
  976. int t = 0;
  977. TQObjectList clo = childrenListObject();
  978. if ( !clo.isEmpty() ) {
  979. TQObjectListIt it( clo );
  980. TQObject * o;
  981. while( (o=it.current()) != 0 ) {
  982. ++it;
  983. if ( o->isWidgetType() &&
  984. !((TQWidget*)o)->isHidden() ) {
  985. TQSize s = ((TQWidget*)o)->sizeHint();
  986. if ( s.isValid() ) {
  987. l += pick( s );
  988. t = TQMAX( t, trans( s ) );
  989. }
  990. }
  991. }
  992. }
  993. return orientation() ==Qt::Horizontal ? TQSize( l, t ) : TQSize( t, l );
  994. }
  995. /*!
  996. \reimp
  997. */
  998. TQSize KDGanttMinimizeSplitter::minimumSizeHint() const
  999. {
  1000. constPolish();
  1001. int l = 0;
  1002. int t = 0;
  1003. TQObjectList clo = childrenListObject();
  1004. if ( !clo.isEmpty() ) {
  1005. TQObjectListIt it( clo );
  1006. TQObject * o;
  1007. while( (o=it.current()) != 0 ) {
  1008. ++it;
  1009. if ( o->isWidgetType() &&
  1010. !((TQWidget*)o)->isHidden() ) {
  1011. TQSize s = minSizeHint((TQWidget*)o);
  1012. if ( s.isValid() ) {
  1013. l += pick( s );
  1014. t = TQMAX( t, trans( s ) );
  1015. }
  1016. }
  1017. }
  1018. }
  1019. return orientation() ==Qt::Horizontal ? TQSize( l, t ) : TQSize( t, l );
  1020. }
  1021. /*
  1022. Calculates stretch parameters from current sizes
  1023. */
  1024. void KDGanttMinimizeSplitter::storeSizes()
  1025. {
  1026. KDGanttSplitterLayoutStruct *s = data->list.first();
  1027. while ( s ) {
  1028. if ( !s->isSplitter )
  1029. s->sizer = pick( s->wid->size() );
  1030. s = data->list.next();
  1031. }
  1032. }
  1033. #if 0 // ### remove this code ASAP
  1034. /*!
  1035. Hides \a w if \a hide is TRUE and updates the splitter.
  1036. \warning Due to a limitation in the current implementation,
  1037. calling TQWidget::hide() will not work.
  1038. */
  1039. void KDGanttMinimizeSplitter::setHidden( TQWidget *w, bool hide )
  1040. {
  1041. if ( w == w1 ) {
  1042. w1show = !hide;
  1043. } else if ( w == w2 ) {
  1044. w2show = !hide;
  1045. } else {
  1046. #ifdef TQT_CHECK_RANGE
  1047. tqWarning( "KDGanttMinimizeSplitter::setHidden(), unknown widget" );
  1048. #endif
  1049. return;
  1050. }
  1051. if ( hide )
  1052. w->hide();
  1053. else
  1054. w->show();
  1055. recalc( TRUE );
  1056. }
  1057. /*!
  1058. Returns the hidden status of \a w
  1059. */
  1060. bool KDGanttMinimizeSplitter::isHidden( TQWidget *w ) const
  1061. {
  1062. if ( w == w1 )
  1063. return !w1show;
  1064. else if ( w == w2 )
  1065. return !w2show;
  1066. #ifdef TQT_CHECK_RANGE
  1067. else
  1068. tqWarning( "KDGanttMinimizeSplitter::isHidden(), unknown widget" );
  1069. #endif
  1070. return FALSE;
  1071. }
  1072. #endif
  1073. /*!
  1074. Returns a list of the size parameters of all the widgets in this
  1075. splitter.
  1076. Giving the values to another splitter's setSizes() function will
  1077. produce a splitter with the same layout as this one.
  1078. Note that if you want to iterate over the list, you should
  1079. iterate over a copy, e.g.
  1080. \code
  1081. TQValueList<int> list = mySplitter.sizes();
  1082. TQValueList<int>::Iterator it = list.begin();
  1083. while( it != list.end() ) {
  1084. myProcessing( *it );
  1085. ++it;
  1086. }
  1087. \endcode
  1088. \sa setSizes()
  1089. */
  1090. TQValueList<int> KDGanttMinimizeSplitter::sizes() const
  1091. {
  1092. if ( !testWState(WState_Polished) ) {
  1093. TQWidget* that = (TQWidget*) this;
  1094. that->polish();
  1095. }
  1096. TQValueList<int> list;
  1097. KDGanttSplitterLayoutStruct *s = data->list.first();
  1098. while ( s ) {
  1099. if ( !s->isSplitter )
  1100. list.append( s->sizer );
  1101. s = data->list.next();
  1102. }
  1103. return list;
  1104. }
  1105. /*!
  1106. Sets the size parameters to the values given in \a list.
  1107. If the splitter is horizontal, the values set the sizes from
  1108. left to right. If it is vertical, the sizes are applied from
  1109. top to bottom.
  1110. Extra values in \a list are ignored.
  1111. If \a list contains too few values, the result is undefined
  1112. but the program will still be well-behaved.
  1113. \sa sizes()
  1114. */
  1115. void KDGanttMinimizeSplitter::setSizes( TQValueList<int> list )
  1116. {
  1117. processChildEvents();
  1118. TQValueList<int>::Iterator it = list.begin();
  1119. KDGanttSplitterLayoutStruct *s = data->list.first();
  1120. while ( s && it != list.end() ) {
  1121. if ( !s->isSplitter ) {
  1122. s->sizer = *it;
  1123. ++it;
  1124. }
  1125. s = data->list.next();
  1126. }
  1127. doResize();
  1128. }
  1129. /*!
  1130. Gets all posted child events, ensuring that the internal state of
  1131. the splitter is consistent.
  1132. */
  1133. void KDGanttMinimizeSplitter::processChildEvents()
  1134. {
  1135. TQApplication::sendPostedEvents( this, TQEvent::ChildInserted );
  1136. }
  1137. /*!
  1138. Reimplemented from superclass.
  1139. */
  1140. void KDGanttMinimizeSplitter::styleChange( TQStyle& old )
  1141. {
  1142. int sw = style().pixelMetric(TQStyle::PM_SplitterWidth, this);
  1143. KDGanttSplitterLayoutStruct *s = data->list.first();
  1144. while ( s ) {
  1145. if ( s->isSplitter )
  1146. s->sizer = sw;
  1147. s = data->list.next();
  1148. }
  1149. doResize();
  1150. TQFrame::styleChange( old );
  1151. }
  1152. /*!
  1153. Specifies the direction of the minimize buttons.
  1154. If the orientation of the splitter is horizontal then with
  1155. KDGanttMinimizeSplitter::Left or KDGanttMinimizeSplitter::Right should be used,
  1156. otherwise either KDGanttMinimizeSplitter::Up or KDGanttMinimizeSplitter::Down
  1157. should be used.
  1158. */
  1159. void KDGanttMinimizeSplitter::setMinimizeDirection( Direction direction )
  1160. {
  1161. _direction = direction;
  1162. }
  1163. /*!
  1164. Returns the direction of the minimize buttons.
  1165. */
  1166. KDGanttMinimizeSplitter::Direction KDGanttMinimizeSplitter::minimizeDirection() const
  1167. {
  1168. return _direction;
  1169. }
  1170. /*
  1171. This is a copy of qGeomCalc() in qlayoutengine.cpp which
  1172. unfortunately isn't exported.
  1173. */
  1174. static inline int toFixed( int i ) { return i * 256; }
  1175. static inline int fRound( int i ) {
  1176. return ( i % 256 < 128 ) ? i / 256 : 1 + i / 256;
  1177. }
  1178. void kdganttGeomCalc( TQMemArray<TQLayoutStruct> &chain, int start, int count, int pos,
  1179. int space, int spacer )
  1180. {
  1181. typedef int fixed;
  1182. int cHint = 0;
  1183. int cMin = 0;
  1184. int cMax = 0;
  1185. int sumStretch = 0;
  1186. int spacerCount = 0;
  1187. bool wannaGrow = FALSE; // anyone who really wants to grow?
  1188. // bool canShrink = FALSE; // anyone who could be persuaded to shrink?
  1189. int i;
  1190. for ( i = start; i < start + count; i++ ) {
  1191. chain[i].done = FALSE;
  1192. cHint += chain[i].sizeHint;
  1193. cMin += chain[i].minimumSize;
  1194. cMax += chain[i].maximumSize;
  1195. sumStretch += chain[i].stretch;
  1196. if ( !chain[i].empty )
  1197. spacerCount++;
  1198. wannaGrow = wannaGrow || chain[i].expansive;
  1199. }
  1200. int extraspace = 0;
  1201. if ( spacerCount )
  1202. spacerCount--; // only spacers between things
  1203. if ( space < cMin + spacerCount * spacer ) {
  1204. // tqDebug("not enough space");
  1205. for ( i = start; i < start+count; i++ ) {
  1206. chain[i].size = chain[i].minimumSize;
  1207. chain[i].done = TRUE;
  1208. }
  1209. } else if ( space < cHint + spacerCount*spacer ) {
  1210. // Less space than sizeHint, but more than minimum.
  1211. // Currently take space equally from each, like in TQt 2.x.
  1212. // Commented-out lines will give more space to stretchier items.
  1213. int n = count;
  1214. int space_left = space - spacerCount*spacer;
  1215. int overdraft = cHint - space_left;
  1216. //first give to the fixed ones:
  1217. for ( i = start; i < start+count; i++ ) {
  1218. if ( !chain[i].done && chain[i].minimumSize >= chain[i].sizeHint) {
  1219. chain[i].size = chain[i].sizeHint;
  1220. chain[i].done = TRUE;
  1221. space_left -= chain[i].sizeHint;
  1222. // sumStretch -= chain[i].stretch;
  1223. n--;
  1224. }
  1225. }
  1226. bool finished = n == 0;
  1227. while ( !finished ) {
  1228. finished = TRUE;
  1229. fixed fp_over = toFixed( overdraft );
  1230. fixed fp_w = 0;
  1231. for ( i = start; i < start+count; i++ ) {
  1232. if ( chain[i].done )
  1233. continue;
  1234. // if ( sumStretch <= 0 )
  1235. fp_w += fp_over / n;
  1236. // else
  1237. // fp_w += (fp_over * chain[i].stretch) / sumStretch;
  1238. int w = fRound( fp_w );
  1239. chain[i].size = chain[i].sizeHint - w;
  1240. fp_w -= toFixed( w ); //give the difference to the next
  1241. if ( chain[i].size < chain[i].minimumSize ) {
  1242. chain[i].done = TRUE;
  1243. chain[i].size = chain[i].minimumSize;
  1244. finished = FALSE;
  1245. overdraft -= chain[i].sizeHint - chain[i].minimumSize;
  1246. // sumStretch -= chain[i].stretch;
  1247. n--;
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. } else { //extra space
  1253. int n = count;
  1254. int space_left = space - spacerCount*spacer;
  1255. // first give to the fixed ones, and handle non-expansiveness
  1256. for ( i = start; i < start + count; i++ ) {
  1257. if ( !chain[i].done && (chain[i].maximumSize <= chain[i].sizeHint
  1258. || wannaGrow && !chain[i].expansive) ) {
  1259. chain[i].size = chain[i].sizeHint;
  1260. chain[i].done = TRUE;
  1261. space_left -= chain[i].sizeHint;
  1262. sumStretch -= chain[i].stretch;
  1263. n--;
  1264. }
  1265. }
  1266. extraspace = space_left;
  1267. /*
  1268. Do a trial distribution and calculate how much it is off.
  1269. If there are more deficit pixels than surplus pixels, give
  1270. the minimum size items what they need, and repeat.
  1271. Otherwise give to the maximum size items, and repeat.
  1272. I have a wonderful mathematical proof for the correctness
  1273. of this principle, but unfortunately this comment is too
  1274. small to contain it.
  1275. */
  1276. int surplus, deficit;
  1277. do {
  1278. surplus = deficit = 0;
  1279. fixed fp_space = toFixed( space_left );
  1280. fixed fp_w = 0;
  1281. for ( i = start; i < start+count; i++ ) {
  1282. if ( chain[i].done )
  1283. continue;
  1284. extraspace = 0;
  1285. if ( sumStretch <= 0 )
  1286. fp_w += fp_space / n;
  1287. else
  1288. fp_w += (fp_space * chain[i].stretch) / sumStretch;
  1289. int w = fRound( fp_w );
  1290. chain[i].size = w;
  1291. fp_w -= toFixed( w ); // give the difference to the next
  1292. if ( w < chain[i].sizeHint ) {
  1293. deficit += chain[i].sizeHint - w;
  1294. } else if ( w > chain[i].maximumSize ) {
  1295. surplus += w - chain[i].maximumSize;
  1296. }
  1297. }
  1298. if ( deficit > 0 && surplus <= deficit ) {
  1299. // give to the ones that have too little
  1300. for ( i = start; i < start+count; i++ ) {
  1301. if ( !chain[i].done &&
  1302. chain[i].size < chain[i].sizeHint ) {
  1303. chain[i].size = chain[i].sizeHint;
  1304. chain[i].done = TRUE;
  1305. space_left -= chain[i].sizeHint;
  1306. sumStretch -= chain[i].stretch;
  1307. n--;
  1308. }
  1309. }
  1310. }
  1311. if ( surplus > 0 && surplus >= deficit ) {
  1312. // take from the ones that have too much
  1313. for ( i = start; i < start+count; i++ ) {
  1314. if ( !chain[i].done &&
  1315. chain[i].size > chain[i].maximumSize ) {
  1316. chain[i].size = chain[i].maximumSize;
  1317. chain[i].done = TRUE;
  1318. space_left -= chain[i].maximumSize;
  1319. sumStretch -= chain[i].stretch;
  1320. n--;
  1321. }
  1322. }
  1323. }
  1324. } while ( n > 0 && surplus != deficit );
  1325. if ( n == 0 )
  1326. extraspace = space_left;
  1327. }
  1328. // as a last resort, we distribute the unwanted space equally
  1329. // among the spacers (counting the start and end of the chain).
  1330. //### should do a sub-pixel allocation of extra space
  1331. int extra = extraspace / ( spacerCount + 2 );
  1332. int p = pos + extra;
  1333. for ( i = start; i < start+count; i++ ) {
  1334. chain[i].pos = p;
  1335. p = p + chain[i].size;
  1336. if ( !chain[i].empty )
  1337. p += spacer+extra;
  1338. }
  1339. }
  1340. #endif
  1341. /*!
  1342. \enum KDGanttMinimizeSplitter::Direction
  1343. The values of this enumeration describe into which direction the
  1344. splitter will collapse its child widgets. By extension, it also
  1345. specifies the orientation of the splitter; collapsing to the left or
  1346. to the right results in a horizontal splitter, collapsing to the top
  1347. or bottom in a vertical splitter.
  1348. */
  1349. /*!
  1350. \fn Qt::Orientation KDGanttMinimizeSplitter::orientation() const
  1351. Returns the orientation of the splitter.
  1352. */
  1353. /*! \enum KDGanttMinimizeSplitter::ResizeMode
  1354. This enum type describes how KDGanttMinimizeSplitter will resize each of its child widgets. The currently defined values are:
  1355. Stretch: the widget will be resized when the splitter
  1356. itself is resized.
  1357. KeepSize: KDGanttMinimizeSplitter will try to keep this widget's size
  1358. unchanged.
  1359. FollowSizeHint: KDGanttMinimizeSplitter will resize the widget when the
  1360. widget's size hint changes.
  1361. */