UBDockPalette.cpp 17.9 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Claudio Valerio's avatar
Claudio Valerio committed
15 16 17 18 19
#include <QPoint>
#include <QPointF>
#include <QPainterPath>

#include "UBDockPalette.h"
Claudio Valerio's avatar
Claudio Valerio committed
20

Claudio Valerio's avatar
Claudio Valerio committed
21
#include "frameworks/UBPlatformUtils.h"
Claudio Valerio's avatar
Claudio Valerio committed
22 23

#include "core/UBSettings.h"
24 25
#include "core/UBApplication.h"
#include "core/UBPreferencesController.h"
26
#include "core/UBDownloadManager.h"
Claudio Valerio's avatar
Claudio Valerio committed
27

Claudio Valerio's avatar
Claudio Valerio committed
28 29
#include "board/UBBoardController.h"

30 31
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
32 33 34
/**
 * \brief The constructor
 */
35
UBDockPalette::UBDockPalette(eUBDockPaletteType paletteType, QWidget *parent, const char *name)
Claudio Valerio's avatar
Claudio Valerio committed
36
:QWidget(parent, Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint)
Claudio Valerio's avatar
Claudio Valerio committed
37
, mCurrentMode(eUBDockPaletteWidget_BOARD)
Claudio Valerio's avatar
Claudio Valerio committed
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
, mOrientation(eUBDockOrientation_Left)
, mPreferredWidth(100)
, mPreferredHeight(100)
, mCanResize(false)
, mResized(false)
, mCollapseWidth(150)
, mLastWidth(-1)
, mHTab(0)
, mpStackWidget(NULL)
, mpLayout(NULL)
, mCurrentTab(0)
, mPaletteType(paletteType)
, mTabPalette(new UBTabDockPalette(this, parent))
{
	setObjectName(name);

	mpLayout = new QVBoxLayout();
	setLayout(mpLayout);

	mpStackWidget = new QStackedWidget(this);
	mpLayout->addWidget(mpStackWidget);

	// clear the tab widgets
	mTabWidgets.clear();

	// We let 2 pixels in order to keep a small border for the resizing
	setMinimumWidth(0);

	if (parent)
	{
		setAttribute(Qt::WA_NoMousePropagation);
		setAttribute(Qt::WA_TranslucentBackground);
	}
	else
	{
		// standalone window
		setAttribute(Qt::WA_TranslucentBackground);
	}

	mBackgroundBrush = QBrush(UBSettings::paletteColor);

	// This is the only way to set the background as transparent!
	setStyleSheet("QWidget {background-color: transparent}");

	// Set the position of the tab
	onToolbarPosUpdated();
	connect(UBSettings::settings()->appToolBarPositionedAtTop, SIGNAL(changed(QVariant)), this, SLOT(onToolbarPosUpdated()));
	connect(UBDownloadManager::downloadManager(), SIGNAL(allDownloadsFinished()), this, SLOT(onAllDownloadsFinished()));
Claudio Valerio's avatar
Claudio Valerio committed
86 87

	connect(UBApplication::boardController,SIGNAL(documentSet(UBDocumentProxy*)),this,SLOT(onDocumentSet(UBDocumentProxy*)));
Claudio Valerio's avatar
Claudio Valerio committed
88 89 90 91 92 93 94
}

/**
 * \brief The destructor
 */
UBDockPalette::~UBDockPalette()
{
Claudio Valerio's avatar
Claudio Valerio committed
95 96 97 98 99 100 101 102 103 104
	if(NULL != mpStackWidget)
	{
		delete mpStackWidget;
		mpStackWidget = NULL;
	}
	if(NULL != mpLayout)
	{
		delete mpLayout;
		mpLayout = NULL;
	}
Claudio Valerio's avatar
Claudio Valerio committed
105 106
}

Claudio Valerio's avatar
Claudio Valerio committed
107 108 109 110 111
void UBDockPalette::onDocumentSet(UBDocumentProxy* documentProxy)
{
	Q_UNUSED(documentProxy);
}

Claudio Valerio's avatar
Claudio Valerio committed
112 113 114 115 116 117
/**
 * \brief Get the current orientation
 * @return the current orientation
 */
eUBDockOrientation UBDockPalette::orientation()
{
Claudio Valerio's avatar
Claudio Valerio committed
118
	return mOrientation;
Claudio Valerio's avatar
Claudio Valerio committed
119 120 121 122 123 124 125 126
}

/**
 * \brief Set the current orientation
 * @param orientation as the given orientation
 */
void UBDockPalette::setOrientation(eUBDockOrientation orientation)
{
Claudio Valerio's avatar
Claudio Valerio committed
127 128
	// Set the size
	mOrientation = orientation;
Claudio Valerio's avatar
Claudio Valerio committed
129

Claudio Valerio's avatar
Claudio Valerio committed
130 131 132 133 134 135 136 137 138 139
	if(orientation == eUBDockOrientation_Left || orientation == eUBDockOrientation_Right)
	{
		setMaximumHeight(parentWidget()->height());
		setMinimumHeight(maximumHeight());
	}
	else if(orientation == eUBDockOrientation_Top || orientation == eUBDockOrientation_Bottom)
	{
		setMaximumWidth(parentWidget()->width());
		setMinimumWidth(maximumWidth());
	}
Claudio Valerio's avatar
Claudio Valerio committed
140 141
}

142 143 144 145
/**
 * \brief Handle the resize event
 * @param event as the resize event
 */
Claudio Valerio's avatar
Claudio Valerio committed
146 147
void UBDockPalette::resizeEvent(QResizeEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
148 149 150 151 152 153
	Q_UNUSED(event);
	updateMaxWidth();
	if(parentWidget())
	{
		setMinimumHeight(parentWidget()->height());
	}
Claudio Valerio's avatar
Claudio Valerio committed
154
	// Set the position
Claudio Valerio's avatar
Claudio Valerio committed
155 156 157 158
	QPoint origin;
	switch(mOrientation)
	{
	case eUBDockOrientation_Right:
Claudio Valerio's avatar
Claudio Valerio committed
159 160
		origin.setX(parentWidget()->width() - this->width());
		origin.setY(0);
Claudio Valerio's avatar
Claudio Valerio committed
161 162 163 164 165 166 167 168 169 170 171 172 173
		break;
	case eUBDockOrientation_Bottom:
		// Not supported yet
	case eUBDockOrientation_Top:
		// Not supported yet
	case eUBDockOrientation_Left:
	default:
		origin.setX(0);
		origin.setY(0);
		break;
	}
	move(origin.x(), origin.y());
	moveTabs();
Claudio Valerio's avatar
Claudio Valerio committed
174 175 176 177 178 179 180 181
}

/**
 * \brief Handle the mouse enter event
 * @param event as the mouse event
 */
void UBDockPalette::enterEvent(QEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
182 183 184
	Q_UNUSED(event);
	// We want to set the cursor as an arrow everytime it enters the palette
	setCursor(Qt::ArrowCursor);
Claudio Valerio's avatar
Claudio Valerio committed
185 186 187 188 189 190 191 192
}

/**
 * \brief Handle the mouse leave event
 * @param event as the mouse event
 */
void UBDockPalette::leaveEvent(QEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
193 194 195
	Q_UNUSED(event);
	// Restore the cursor to its previous shape
	unsetCursor();
Claudio Valerio's avatar
Claudio Valerio committed
196 197 198 199 200 201 202 203
}

/**
 * \brief Draw the palette
 * @param event as the paint event
 */
void UBDockPalette::paintEvent(QPaintEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
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
	Q_UNUSED(event);
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setPen(Qt::NoPen);
	painter.setBrush(mBackgroundBrush);
	QPainterPath path;
	path.setFillRule(Qt::WindingFill);

	int nbTabs = mTabWidgets.size();
	if(0 < nbTabs)
	{
		// First draw the BIG RECTANGLE (I write it big because the rectangle is big...)
		if(mOrientation == eUBDockOrientation_Left)
		{
			path.addRect(0.0, 0.0, width(), height());
		}
		else if(mOrientation == eUBDockOrientation_Right)
		{
			path.addRect(0.0, 0.0, width(), height());
		}

		// THEN DRAW THE small tabs (yes, the tabs are small...)
		if(eUBDockTabOrientation_Up == mTabsOrientation)
		{
			mHTab = border();
		}
		else
		{
			mHTab = height() - border() - nbTabs*TABSIZE - (nbTabs-1)*tabSpacing();
		}
		painter.drawPath(path);
	}
Claudio Valerio's avatar
Claudio Valerio committed
236 237 238 239 240 241 242 243
}

/**
 * \brief Set the background brush
 * @param brush as the given brush
 */
void UBDockPalette::setBackgroundBrush(const QBrush &brush)
{
Claudio Valerio's avatar
Claudio Valerio committed
244 245 246 247 248
	if (mBackgroundBrush != brush)
	{
		mBackgroundBrush = brush;
		update();
	}
Claudio Valerio's avatar
Claudio Valerio committed
249 250 251 252 253 254 255 256
}

/**
 * Get the border size
 * @return the border size
 */
int UBDockPalette::border()
{
Claudio Valerio's avatar
Claudio Valerio committed
257
	return 15;
Claudio Valerio's avatar
Claudio Valerio committed
258 259 260 261 262 263 264 265
}

/**
 * \brief Get the radius size
 * @return the radius size
 */
int UBDockPalette::radius()
{
Claudio Valerio's avatar
Claudio Valerio committed
266
	return 5;
Claudio Valerio's avatar
Claudio Valerio committed
267 268 269 270 271 272 273
}

/**
 * \brief Set the maximum width
 */
void UBDockPalette::updateMaxWidth()
{
Claudio Valerio's avatar
Claudio Valerio committed
274
	// Only the inherited class will overload this method
Claudio Valerio's avatar
Claudio Valerio committed
275 276 277 278 279 280 281 282
}

/**
 * \brief Get the collapse width value
 * @return the collapse widht value
 */
int UBDockPalette::collapseWidth()
{
Claudio Valerio's avatar
Claudio Valerio committed
283
	return mCollapseWidth;
Claudio Valerio's avatar
Claudio Valerio committed
284 285 286 287 288
}

/**
 * \brief collapse/expand automatically the palette after a click on its tab
 */
289 290
void UBDockPalette::tabClicked(int tabIndex)
{
Claudio Valerio's avatar
Claudio Valerio committed
291 292 293 294 295 296 297 298 299 300 301
	// If the current tab is not the clicked one, show its content
	if(mCurrentTab != tabIndex)
	{
		showTabWidget(tabIndex);
	}
	// else collapse the palette
	else
	{
		toggleCollapseExpand();
	}
	mTabPalette->update();
302 303
}

304 305 306 307
/**
 * \brief Show the tab widget related to the given index
 * @param tabIndex as the given index
 */
308 309
void UBDockPalette::showTabWidget(int tabIndex)
{
Claudio Valerio's avatar
Claudio Valerio committed
310 311
	mpStackWidget->setCurrentIndex(tabIndex);
	mCurrentTab = tabIndex;
312

Claudio Valerio's avatar
Claudio Valerio committed
313 314 315 316
	// Update the current tab index
	if(NULL != (dynamic_cast<UBDockPaletteWidget*>(mpStackWidget->widget(tabIndex)))){
		mCrntTabWidget = dynamic_cast<UBDockPaletteWidget*>(mpStackWidget->widget(tabIndex))->name();
	}
317

318 319
}

320 321 322
/**
 * \brief Toggle the collapse / expand state
 */
323
void UBDockPalette::toggleCollapseExpand()
Claudio Valerio's avatar
Claudio Valerio committed
324
{
Claudio Valerio's avatar
Claudio Valerio committed
325 326 327 328
	if(width() < mCollapseWidth)
		resize(mLastWidth,height());
	else{
		mLastWidth = width();
Aleksei Kanash's avatar
Aleksei Kanash committed
329
        update();
Claudio Valerio's avatar
Claudio Valerio committed
330 331
		resize(0,height());
	}
Claudio Valerio's avatar
Claudio Valerio committed
332
}
333

334 335 336 337
/**
 * \brief Set the tabs orientation
 * @param orientation as the given tabs orientation
 */
338 339
void UBDockPalette::setTabsOrientation(eUBDockTabOrientation orientation)
{
Claudio Valerio's avatar
Claudio Valerio committed
340
	mTabsOrientation = orientation;
341 342
}

343 344 345
/**
 * \brief Update the tab position regarding the toolbar position (up or down)
 */
346 347
void UBDockPalette::onToolbarPosUpdated()
{
Claudio Valerio's avatar
Claudio Valerio committed
348 349 350 351 352 353 354 355 356 357 358
	// Get the position of the tab
	if(UBSettings::settings()->appToolBarPositionedAtTop->get().toBool())
	{
		setTabsOrientation(eUBDockTabOrientation_Up);
	}
	else
	{
		setTabsOrientation(eUBDockTabOrientation_Down);
	}
	moveTabs();
	update();
359 360
}

361 362 363 364
/**
 * \brief Get the custom margin
 * @return the custom margin value
 */
365 366
int UBDockPalette::customMargin()
{
Claudio Valerio's avatar
Claudio Valerio committed
367
	return 5;
368
}
shibakaneki's avatar
shibakaneki committed
369

370 371 372 373
/**
 * \brief Add the given tab widget
 * @param widget as the given widget
 */
374 375
void UBDockPalette::addTab(UBDockPaletteWidget *widget)
{
Claudio Valerio's avatar
Claudio Valerio committed
376 377 378 379 380 381 382 383 384
	if(!mTabWidgets.contains(widget) && widget->visibleState())
	{
		widget->setVisible(true);
		mTabWidgets.append(widget);
		mpStackWidget->addWidget(widget);
		mpStackWidget->setCurrentWidget(widget);
		resizeTabs();
		update();
	}
shibakaneki's avatar
shibakaneki committed
385
}
386 387 388 389
/**
 * \brief Remove the given tab
 * @param widgetName as the tab widget name
 */
390
void UBDockPalette::removeTab(UBDockPaletteWidget* widget)
391
{
Claudio Valerio's avatar
Claudio Valerio committed
392 393 394 395 396 397 398 399 400 401
	int nWidget = mTabWidgets.indexOf(widget);
	if( nWidget >= 0 )
	{
		mpStackWidget->removeWidget(widget);
		mTabWidgets.remove(nWidget);
		widget->hide();
		update();
	}
	resizeTabs();
	mCurrentTab = qMax(mCurrentTab - 1, 0);
402 403
}

404 405 406 407
/**
 * \brief Handle the resize request
 * @param event as the given resize request
 */
408 409
void UBDockPalette::onResizeRequest(QResizeEvent *event)
{
Claudio Valerio's avatar
Claudio Valerio committed
410
	resizeEvent(event);
411 412
}

413 414 415 416
/**
 * \brief Get the tab spacing
 * @return the tab spacing
 */
417 418
int UBDockPalette::tabSpacing()
{
Claudio Valerio's avatar
Claudio Valerio committed
419
	return 2;
shibakaneki's avatar
shibakaneki committed
420
}
421

422 423 424 425
/**
 * \brief Show the given widget
 * @param widgetName as the given widget name
 */
426
void UBDockPalette::onShowTabWidget(UBDockPaletteWidget* widget)
427
{
Claudio Valerio's avatar
Claudio Valerio committed
428 429 430 431 432
	if (mRegisteredWidgets.contains(widget))
	{
		widget->setVisibleState(true);
		addTab(widget);
	}
433 434
}

435 436 437 438
/**
 * \brief Hide the given widget
 * @param widgetName as the given widget name
 */
439
void UBDockPalette::onHideTabWidget(UBDockPaletteWidget* widget)
440
{
Claudio Valerio's avatar
Claudio Valerio committed
441 442 443 444 445
	if (mRegisteredWidgets.contains(widget))
	{
		widget->setVisibleState(false);
		removeTab(widget);
	}
446 447
}

448 449 450
/**
 * \brief Connect the show / hide signals of the widget related to this dock palette
 */
451 452
void UBDockPalette::connectSignals()
{
Claudio Valerio's avatar
Claudio Valerio committed
453 454 455 456 457
	for(int i=0; i < mRegisteredWidgets.size(); i++)
	{
		connect(mRegisteredWidgets.at(i), SIGNAL(showTab(UBDockPaletteWidget*)), this, SLOT(onShowTabWidget(UBDockPaletteWidget*)));
		connect(mRegisteredWidgets.at(i), SIGNAL(hideTab(UBDockPaletteWidget*)), this, SLOT(onHideTabWidget(UBDockPaletteWidget*)));
	}
458 459
}

460 461 462 463
/**
 * \brief Register the given widget
 * @param widget as the given widget
 */
464 465
void UBDockPalette::registerWidget(UBDockPaletteWidget *widget)
{
Claudio Valerio's avatar
Claudio Valerio committed
466 467 468 469 470
	if(!mRegisteredWidgets.contains(widget))
	{
		// Update the parent of this widget
		widget->setParent(this);
		mRegisteredWidgets.append(widget);
471

Claudio Valerio's avatar
Claudio Valerio committed
472 473 474
		// By default, the widget is hidden
		widget->hide();
	}
475
}
476 477

/**
Claudio Valerio's avatar
Claudio Valerio committed
478 479
 * \brief Handles the 'all download finished' notification
 */
480 481
void UBDockPalette::onAllDownloadsFinished()
{
Claudio Valerio's avatar
Claudio Valerio committed
482 483 484 485 486 487
	for(int i=0; i<mTabWidgets.size(); i++){
		UBDockPaletteWidget* pW = mTabWidgets.at(i);
		if(NULL != pW && mCrntTabWidget == pW->name()){
			mpStackWidget->setCurrentWidget(pW);
		}
	}
488
}
Ivan Ilin's avatar
Ivan Ilin committed
489 490 491

void UBDockPalette::moveTabs()
{
Claudio Valerio's avatar
Claudio Valerio committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	//   if (!mHTab) {
	if(eUBDockTabOrientation_Up == mTabsOrientation) {
		mHTab = border();
	} else {
		mHTab = height() - border() - mTabWidgets.size() * TABSIZE - (mTabWidgets.size() - 1) * tabSpacing();
	}
	//    }

	QPoint origin(width(), mHTab + mTabPalette->mVerticalOffset);

	switch (mOrientation) {
	case eUBDockOrientation_Left:
		origin.setX(width());
		break;
	case eUBDockOrientation_Right:
		if (parentWidget()) {
			origin.setX(parentWidget()->width() - width() - border() * 2);
		}
		break;
	case eUBDockOrientation_Top: ;
	case eUBDockOrientation_Bottom: ;
	}

	mTabPalette->move(origin.x(), origin.y());
Ivan Ilin's avatar
Ivan Ilin committed
516 517 518
}
void UBDockPalette::resizeTabs()
{
Claudio Valerio's avatar
Claudio Valerio committed
519 520
	int numTabs = mTabWidgets.size();
	mTabPalette->setFixedSize(2 * border(), (numTabs * TABSIZE) + qMax(numTabs - 1, 0) * tabSpacing());
Ivan Ilin's avatar
Ivan Ilin committed
521 522 523
}
QRect UBDockPalette::getTabPaletteRect()
{
Claudio Valerio's avatar
Claudio Valerio committed
524 525 526
	QRect tabsRect = mTabPalette->rect();
	QPoint topLeft = mTabPalette->mapToParent(tabsRect.topLeft());
	QPoint bottomRight = mTabPalette->mapToParent(tabsRect.bottomRight());
Ivan Ilin's avatar
Ivan Ilin committed
527

Claudio Valerio's avatar
Claudio Valerio committed
528
	return QRect(topLeft, bottomRight);
Ivan Ilin's avatar
Ivan Ilin committed
529 530 531
}
void UBDockPalette::assignParent(QWidget *widget)
{
Claudio Valerio's avatar
Claudio Valerio committed
532 533
	setParent(widget);
	mTabPalette->setParent(widget);
Ivan Ilin's avatar
Ivan Ilin committed
534 535 536
}
void UBDockPalette::setVisible(bool visible)
{
Claudio Valerio's avatar
Claudio Valerio committed
537 538
	QWidget::setVisible(visible);
	mTabPalette->setVisible(visible);
Ivan Ilin's avatar
Ivan Ilin committed
539 540
}

541 542
bool UBDockPalette::switchMode(eUBDockPaletteWidgetMode mode)
{
Claudio Valerio's avatar
Claudio Valerio committed
543
	mLastOpenedTabForMode.insert(mCurrentMode, mpStackWidget->currentIndex());
Claudio Valerio's avatar
Claudio Valerio committed
544
	mCurrentMode = mode;
Claudio Valerio's avatar
Claudio Valerio committed
545 546
	bool hasVisibleElements = false;
	//-------------------------------//
547
	// get full palette widgets list, parse it, show all widgets for BOARD mode, and hide all other
Claudio Valerio's avatar
Claudio Valerio committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	for(int i = 0; i < mRegisteredWidgets.size(); i++)
	{
		UBDockPaletteWidget* pNextWidget = mRegisteredWidgets.at(i);
		if( pNextWidget != NULL )
		{
			if( pNextWidget->visibleInMode(mode) )
			{
				addTab(pNextWidget);
				hasVisibleElements = true;
			}
			else
			{
				removeTab(pNextWidget);
			}
		}
	}
	//-------------------------------//

	if(mRegisteredWidgets.size() > 0)
Claudio Valerio's avatar
Claudio Valerio committed
567
		showTabWidget(mLastOpenedTabForMode.value(mCurrentMode));
Claudio Valerio's avatar
Claudio Valerio committed
568 569 570 571

	update();

	return hasVisibleElements;
572 573 574
}


575
UBTabDockPalette::UBTabDockPalette(UBDockPalette *dockPalette, QWidget *parent) :
Claudio Valerio's avatar
Claudio Valerio committed
576 577 578 579
    		QWidget(parent)
, dock(dockPalette)
, mVerticalOffset(0)
, mFlotable(false)
Ivan Ilin's avatar
Ivan Ilin committed
580
{
Claudio Valerio's avatar
Claudio Valerio committed
581 582
	int numTabs = dock->mTabWidgets.size();
	resize(2 * dock->border(), (numTabs * TABSIZE) + qMax(numTabs - 1, 0) * dock->tabSpacing());
Ivan Ilin's avatar
Ivan Ilin committed
583

Claudio Valerio's avatar
Claudio Valerio committed
584
	setAttribute(Qt::WA_TranslucentBackground);
Ivan Ilin's avatar
Ivan Ilin committed
585 586
}

587
void UBTabDockPalette::paintEvent(QPaintEvent *)
Ivan Ilin's avatar
Ivan Ilin committed
588
{
Claudio Valerio's avatar
Claudio Valerio committed
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
	int nTabs = dock->mTabWidgets.size();
	if (nTabs <= 0) {
		qDebug() << "not enough tabs";
		return;
	}

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setPen(Qt::NoPen);
	painter.setBrush(dock->mBackgroundBrush);

	int yFrom = 0;
	for (int i = 0; i < nTabs; i++) {
		UBDockPaletteWidget* pCrntWidget = dock->mTabWidgets.at(i);
		QPainterPath path;
		path.setFillRule(Qt::WindingFill);
		QPixmap iconPixmap;

		switch (dock->mOrientation) {
		case eUBDockOrientation_Left:
			path.addRect(0, yFrom, width() / 2, TABSIZE);
			path.addRoundedRect(0, yFrom, width(), TABSIZE, dock->radius(), dock->radius());
			if (pCrntWidget) {
				if(dock->mCollapseWidth >= dock->width()) {
					// Get the collapsed icon
					iconPixmap = pCrntWidget->iconToRight();
				} else {
					// Get the expanded icon
					iconPixmap = pCrntWidget->iconToLeft();
				}

			}
			break;

		case eUBDockOrientation_Right:
			path.addRect(width() /2, yFrom, width() / 2, TABSIZE);
			path.addRoundedRect(0, yFrom, width(), TABSIZE, dock->radius(), dock->radius());
			if (pCrntWidget) {
				if(dock->mCollapseWidth >= dock->width()) {
					// Get the collapsed icon
					iconPixmap = pCrntWidget->iconToLeft();
				} else {
					// Get the expanded icon
					iconPixmap = pCrntWidget->iconToRight();
				}
			}
			break;

		case eUBDockOrientation_Top: ;
		case eUBDockOrientation_Bottom: ;
		default:
			break;
		}

		painter.save();
		QPixmap transparencyPix(":/images/tab_mask.png");
		if (dock->mCurrentTab != i) {
			iconPixmap.setAlphaChannel(transparencyPix);
			QColor color(0x7F, 0x7F, 0x7F, 0x3F);
			painter.setBrush(QBrush(color));
		}

		painter.drawPath(path);
		painter.drawPixmap(2, yFrom + 2, width() - 4, TABSIZE - 4, iconPixmap);
		yFrom += (TABSIZE + dock->tabSpacing());
		painter.restore();
	}
Ivan Ilin's avatar
Ivan Ilin committed
656
}
657
UBTabDockPalette::~UBTabDockPalette()
Ivan Ilin's avatar
Ivan Ilin committed
658 659 660
{
}

661
void UBTabDockPalette::mousePressEvent(QMouseEvent *event)
Ivan Ilin's avatar
Ivan Ilin committed
662
{
Claudio Valerio's avatar
Claudio Valerio committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	dock->mClickTime = QTime::currentTime();
	// The goal here is to verify if the user can resize the widget.
	// It is only possible to resize it if the border is selected
	QPoint p = event->pos();
	dock->mMousePressPos = p;
	dock->mResized = false;

	switch(dock->mOrientation) {
	case eUBDockOrientation_Left:
		dock->mCanResize = true;
		break;
	case eUBDockOrientation_Right:
		dock->mCanResize = true;
		break;
	case eUBDockOrientation_Top:
		// Not supported yet
		break;
	case eUBDockOrientation_Bottom:
		// Not supported yet
		break;
	default:
		break;
	}
Ivan Ilin's avatar
Ivan Ilin committed
686
}
687
void UBTabDockPalette::mouseMoveEvent(QMouseEvent *event)
Ivan Ilin's avatar
Ivan Ilin committed
688
{
Claudio Valerio's avatar
Claudio Valerio committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	QPoint p = event->pos();

	if(dock->mCanResize && ((dock->mMousePressPos - p).manhattanLength() > QApplication::startDragDistance()))
	{
		if (qAbs(dock->mMousePressPos.y() - p.y()) > 10 && mFlotable) {
			qDebug() << "y differences" << dock->mMousePressPos.y() << p.y();
			mVerticalOffset += p.y() - dock->mMousePressPos.y();
			move(this->pos().x(),  p.y());
		}

		switch(dock->mOrientation) {

		case eUBDockOrientation_Left:
			p.setX(p.x() + dock->width());
			if(p.x() < dock->collapseWidth() && p.x() >= dock->minimumWidth()) {
Aleksei Kanash's avatar
Aleksei Kanash committed
704
                dock->update();
Claudio Valerio's avatar
Claudio Valerio committed
705 706 707 708 709 710 711 712 713 714 715 716
				dock->resize(0, dock->height());
				//dock->mLastWidth = dock->collapseWidth() + 1;
				dock->mResized = true;
			} else if (p.x() <= dock->maximumWidth() && p.x() >= dock->minimumWidth()) {
				dock->resize(p.x(), dock->height());
				dock->mResized = true;
			}
			break;

		case eUBDockOrientation_Right:
			p.setX(p.x() - 2 * dock->border());
			if((dock->x() + p.x() > dock->parentWidget()->width() - dock->collapseWidth()) && (dock->x() + p.x() < dock->parentWidget()->width())) {
Aleksei Kanash's avatar
Aleksei Kanash committed
717
                dock->update();
Claudio Valerio's avatar
Claudio Valerio committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
				dock->resize(0, dock->height());
				//dock->mLastWidth = dock->collapseWidth() + 1;
				dock->mResized = true;
			} else if((dock->x() + p.x() >= dock->parentWidget()->width() - dock->maximumWidth()) && (dock->x() + p.x() <= dock->parentWidget()->width() - dock->minimumWidth())) {
				dock->resize(dock->parentWidget()->width() - (dock->x() + p.x()), dock->height());
				dock->mResized = true;
			}
			break;

		case eUBDockOrientation_Top:
		case eUBDockOrientation_Bottom:
			if(p.y() <= dock->maximumHeight()) {
				dock->resize(width(), p.y());
				dock->mResized = true;
			}
			break;

		default:
			break;
		}
	}
Ivan Ilin's avatar
Ivan Ilin committed
739
}
740

741
void UBTabDockPalette::mouseReleaseEvent(QMouseEvent *event)
Ivan Ilin's avatar
Ivan Ilin committed
742
{
Claudio Valerio's avatar
Claudio Valerio committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	Q_UNUSED(event);
	if(!dock->mResized && dock->mClickTime.elapsed() < CLICKTIME) {
		int nbTabs = dock->mTabWidgets.size();
		int clickedTab = 0;
		// If the clicked position is in the tab, perform the related action

		if(dock->mMousePressPos.x() >= 0 &&
				dock->mMousePressPos.x() <= width() &&
				dock->mMousePressPos.y() >= 0 &&
				dock->mMousePressPos.y() <= nbTabs * TABSIZE + (nbTabs -1)*dock->tabSpacing()) {

			clickedTab = (dock->mMousePressPos.y()) / (TABSIZE + dock->tabSpacing());
			dock->tabClicked(clickedTab);
		}
	}
	dock->mCanResize = false;
Ivan Ilin's avatar
Ivan Ilin committed
759
}