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.

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