1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
/* ====================================================================
* Copyright (c) 2003-2007, Martin Hauner
* http://subcommander.tigris.org
*
* Subcommander is licensed as described in the file doc/COPYING, which
* you should have received as part of this distribution.
* ====================================================================
*/
// sc
#include "SplitLayout.h"
// qt
#include <QtGui/QCursor>
#include <QtGui/QFrame>
#include <QtCore/QEvent>
#include <QtGui/QMouseEvent>
// sys
#include <assert.h>
///////////////////////////////////////////////////////////////////////////////
class SplitHandleStrategy
{
public:
virtual ~SplitHandleStrategy(){}
virtual void setCursor() = 0;
virtual void setSizePolicy() = 0;
virtual int getPos() = 0;
virtual int getMousePos( QMouseEvent* ) = 0;
virtual void setSize() = 0;
virtual void setGeometry( int ) = 0;
};
///////////////////////////////////////////////////////////////////////////////
class SplitHandle : public QFrame
{
typedef QFrame super;
public:
enum Orientation
{
oHorizontal,
oVertical
};
SplitHandle( QWidget *parent, SplitLayoutHandle* layout, Orientation o );
~SplitHandle();
void reposition( int newPos );
int getPos() const;
protected:
void mouseMoveEvent( QMouseEvent* e );
void mousePressEvent( QMouseEvent* e );
void mouseReleaseEvent( QMouseEvent* e );
//void mouseDoubleClickEvent( QMouseEvent* e );
private:
SplitHandleStrategy* _strategy;
SplitLayoutHandle* _layout;
int _lastPos;
bool _move;
};
///////////////////////////////////////////////////////////////////////////////
class HSplitHandleStrategy : public SplitHandleStrategy
{
public:
HSplitHandleStrategy( SplitHandle* sh ) : _handle(sh)
{
}
void setCursor()
{
_handle->setCursor( QCursor(Qt::SplitHCursor) );
}
void setSizePolicy()
{
_handle->setSizePolicy(
QSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding) );
}
int getMousePos( QMouseEvent* e )
{
return e->globalX();
}
int getPos()
{
return _handle->x();
}
void setSize()
{
_handle->setFixedWidth( 5 );
}
void setGeometry( int newPos )
{
_handle->setGeometry( newPos, _handle->y(), _handle->width(),
_handle->height() );
}
private:
SplitHandle* _handle;
};
class VSplitHandleStrategy : public SplitHandleStrategy
{
public:
VSplitHandleStrategy( SplitHandle* sh ) : _handle(sh)
{
}
void setCursor()
{
_handle->setCursor( QCursor(Qt::SplitVCursor) );
}
void setSizePolicy()
{
_handle->setSizePolicy(
QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed) );
}
int getMousePos( QMouseEvent* e )
{
return e->globalY();
}
int getPos()
{
return _handle->y();
}
void setSize()
{
_handle->setFixedHeight( 5 );
}
void setGeometry( int newPos )
{
_handle->setGeometry( _handle->x(), newPos, _handle->width(),
_handle->height() );
}
private:
SplitHandle* _handle;
};
///////////////////////////////////////////////////////////////////////////////
SplitHandle::SplitHandle( QWidget *parent, SplitLayoutHandle* layout,
Orientation o ) : QFrame(parent), _layout(layout), _move(false)
{
if( o == oHorizontal )
{
_strategy = new HSplitHandleStrategy(this);
}
else
{
_strategy = new VSplitHandleStrategy(this);
}
_strategy->setCursor();
_strategy->setSize();
_strategy->setSizePolicy();
}
SplitHandle::~SplitHandle()
{
delete _strategy;
}
void SplitHandle::mouseMoveEvent( QMouseEvent* e )
{
int myPos = _strategy->getPos();
int mousePos = _strategy->getMousePos(e);
int newPos = _layout->calcMovePos( myPos, mousePos - _lastPos );
_lastPos = mousePos;
_move = true;
reposition(newPos);
}
void SplitHandle::mousePressEvent( QMouseEvent* e )
{
_lastPos = _strategy->getMousePos(e);
}
void SplitHandle::mouseReleaseEvent( QMouseEvent* e )
{
if( _move )
{
_move = ! _move;
return;
}
int newPos = _layout->calcJumpPos( _strategy->getPos() );
reposition(newPos);
}
//void SplitHandle::mouseDoubleClickEvent( QMouseEvent* e )
//{
// int newPos = _layout->calcJumpPos( _strategy->getPos() );
// reposition(newPos);
//}
void SplitHandle::reposition( int newPos )
{
if( newPos == _strategy->getPos() )
{
return;
}
_strategy->setGeometry( newPos );
_layout->recalculate();
}
int SplitHandle::getPos() const
{
return _strategy->getPos();
}
///////////////////////////////////////////////////////////////////////////////
SplitLayout::SplitLayout( QWidget* parent, Pos p )
: QLayout(parent), _stretchOne(0), _stretchTwo(0), _hide(p),
_initialized(false), _handlePos(-1)
{
_items.resize(3);
_items[pOne] = 0;
_items[pTwo] = 0;
}
SplitLayout::~SplitLayout()
{
if( _items[pOne] )
delete _items[pOne];
delete _items[pHandle];
if( _items[pTwo] )
delete _items[pTwo];
}
// SplitLayoutHandle
void SplitLayout::recalculate()
{
//activate();
//parentWidget()->update();
update();
}
int SplitLayout::calcMovePos( int curPos, int diffPos )
{
_items[pOne]->widget()->show();
_items[pTwo]->widget()->show();
int minPos = 0;
int maxPos = getSize(geometry()) - getSize(_items[pHandle]->geometry());
int newPos = curPos + diffPos;
if( newPos < minPos )
{
_items[pOne]->widget()->hide();
newPos = 0;
}
if( newPos > maxPos )
{
_items[pTwo]->widget()->hide();
newPos = maxPos;
}
return newPos;
}
int SplitLayout::calcJumpPos( int curPos )
{
int maxPos = getSize(geometry()) - getSize(_items[pHandle]->sizeHint());
if( curPos == 0 || curPos == maxPos )
{
_items[pOne]->widget()->show();
_items[pTwo]->widget()->show();
if( _handlePos == 0 || _handlePos == maxPos )
{
int w = getSize(_lastRect) / (_stretchOne+_stretchTwo);
return w * _stretchOne;
}
else
{
return _handlePos;
}
}
else
{
_handlePos = getHandlePos();
_items[_hide]->widget()->hide();
if( _hide == pOne )
{
_items[pTwo]->widget()->show();
}
else
{
_items[pOne]->widget()->show();
}
return 0;
}
}
// TODO common code with calcJumpPos
void SplitLayout::jumpPos( bool visible )
{
int maxPos = getSize(geometry()) - getSize(_items[pHandle]->sizeHint());
int w =( getSize(_lastRect) / (_stretchOne+_stretchTwo) ) * _stretchOne;
if( visible )
{
_items[pOne]->widget()->show();
_items[pTwo]->widget()->show();
((SplitHandle*)(_items[pHandle]->widget()))->reposition(w);
}
else
{
if( _hide == pOne )
{
_items[pOne]->widget()->hide();
_items[pTwo]->widget()->show();
((SplitHandle*)(_items[pHandle]->widget()))->reposition(0);
}
else
{
_items[pOne]->widget()->show();
_items[pTwo]->widget()->hide();
((SplitHandle*)(_items[pHandle]->widget()))->reposition(maxPos);
}
}
}
void SplitLayout::addWidgetOne( QWidget* w, bool hide, int stretch )
{
_items[pOne] = new QWidgetItem(w);
_stretchOne = stretch;
if( hide )
{
_hide = pOne;
w->hide();
}
}
void SplitLayout::addWidgetTwo( QWidget* w, bool hide, int stretch )
{
_items[pTwo] = new QWidgetItem(w);
_stretchTwo = stretch;
if( hide )
{
_hide = pTwo;
w->hide();
}
}
void SplitLayout::enableHandle( bool enable )
{
if( enable )
{
(_items[pHandle]->widget())->show();
}
else
{
(_items[pHandle]->widget())->hide();
}
}
void SplitLayout::addItem( QLayoutItem *item )
{
if( !_items[pOne] )
{
_items[pOne] = item;
return;
}
if( !_items[pTwo] )
{
_items[pTwo] = item;
return;
}
assert(false);
}
QLayoutItem* SplitLayout::itemAt( int index ) const
{
if( index >= (int)_items.size() )
{
return NULL;
}
return _items[index];
}
QLayoutItem* SplitLayout::takeAt( int index )
{
if( index >= (int)_items.size() )
{
return NULL;
}
return _items[index];
}
int SplitLayout::count() const
{
return _items.size();
}
QSize SplitLayout::sizeHint() const
{
QSize s(0,0);
for( TLayoutItems::const_iterator it = _items.begin(); it != _items.end(); it++ )
{
adjustSize( s, (*it)->sizeHint() );
}
return s;
}
QSize SplitLayout::minimumSize() const
{
QSize s(0,0);
for( TLayoutItems::const_iterator it = _items.begin(); it != _items.end(); it++ )
{
adjustSize( s, (*it)->minimumSize() );
}
return s;
}
void SplitLayout::setGeometry( const QRect &newRect )
{
QLayout::setGeometry(newRect);
if( _items.empty() )
{
assert(false);
return;
}
QLayoutItem* o = _items[pOne];
QLayoutItem* m = _items[pHandle];
QLayoutItem* t = _items[pTwo];
int os = 0; // one size
int ms = 0; // handle size
int ts = 0; // two size
int op = 0; // one pos
int mp = 0; // handle pos
int tp = 0; // two pos
if( o->widget()->isHidden() )
{
os = 0;
ms = getSize(m->sizeHint());
ts = getSize(newRect) - ms;
op = 0;
mp = 0;
tp = op + ms;
}
else if( t->widget()->isHidden() )
{
ms = getSize(m->sizeHint());
os = getSize(newRect) - ms;
ts = 0;
op = 0;
mp = os;
tp = 0;
}
else
{
int ds = getSize(newRect) - getSize(_lastRect); // delta size
// move handle
if( ds == 0 )
{
os = getPos(m->geometry());
ms = getSize(m->sizeHint());
ts = getSize(newRect) - os - ms;
op = 0;
mp = getPos(m->geometry());
tp = mp + ms;
}
// resize
else
{
os = getPos(m->geometry());
ms = getSize(m->sizeHint());
ts = getSize(_lastRect) - os - ms;
op = 0;
mp = getPos(m->geometry());
tp = mp + ms;
int div = ds / (_stretchOne + _stretchTwo);
int mod = ds % (_stretchOne + _stretchTwo);
int mod_div = mod / 2;
int mod_mod = mod % 1;
int ls = _stretchOne * div + mod_div;
int rs = _stretchTwo * div + mod_div;
if( _stretchOne > _stretchTwo )
{
ls += mod_mod;
}
else
{
rs += mod_mod;
}
os += ls;
ts += rs;
if( os < 0 )
{
os = 0;
o->widget()->hide();
}
if( ts < 0 )
{
ts = 0;
t->widget()->hide();
}
op = 0;
mp = os;
tp = mp + ms;
}
}
QRect orect = calcRect( op, os, newRect );
o->setGeometry(orect);
QRect mrect = calcRect( mp, ms, newRect );
m->setGeometry(mrect);
QRect trect = calcRect( tp, ts, newRect );
t->setGeometry(trect);
_lastRect = newRect;
if( ! _initialized )
{
_initialized = true;
setHandlePos(_handlePos);
}
}
void SplitLayout::setHandlePos( int pos )
{
// the earliest time we can set the handle position is after the
// first layout, so defer the initialization if we haven't done it.
if( _initialized )
{
// we have a handle position?
if( pos != -1 )
{
((SplitHandle*)_items[pHandle]->widget())->reposition(pos);
}
}
else
{
_handlePos = pos;
}
}
int SplitLayout::getHandlePos() const
{
return ((SplitHandle*)_items[pHandle]->widget())->getPos();
}
///////////////////////////////////////////////////////////////////////////////
HSplitLayout::HSplitLayout( QWidget* parent, Pos pos )
: SplitLayout(parent,pos)
{
_items[pHandle] = new QWidgetItem(
new SplitHandle(parent,this,SplitHandle::oHorizontal) );
}
int HSplitLayout::getPos( const QRect& r ) const
{
return r.x();
}
int HSplitLayout::getSize( const QRect& r ) const
{
return r.width();
}
int HSplitLayout::getSize( const QSize& s ) const
{
return s.width();
}
void HSplitLayout::adjustSize( QSize& out, const QSize& item ) const
{
out.setWidth( out.width() + item.width() );
out.setHeight( out.height() < item.height() ? item.height() : out.height() );
}
QRect HSplitLayout::calcRect( int pos, int size, const QRect& r ) const
{
return QRect( pos, r.y(), size, r.height() );
}
///////////////////////////////////////////////////////////////////////////////
VSplitLayout::VSplitLayout( QWidget* parent, Pos pos )
: SplitLayout(parent,pos)
{
_items[pHandle] = new QWidgetItem(
new SplitHandle(parent,this,SplitHandle::oVertical) );
}
int VSplitLayout::getPos( const QRect& r ) const
{
return r.y();
}
int VSplitLayout::getSize( const QRect& r ) const
{
return r.height();
}
int VSplitLayout::getSize( const QSize& s ) const
{
return s.height();
}
void VSplitLayout::adjustSize( QSize& out, const QSize& item ) const
{
out.setHeight( out.height() + item.height() );
out.setWidth( out.width() < item.width() ? item.width() : out.width() );
}
QRect VSplitLayout::calcRect( int pos, int size, const QRect& r ) const
{
return QRect( r.x(), pos, r.width(), size );
}
///////////////////////////////////////////////////////////////////////////////
|