UBDockPalette.cpp 25.9 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (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 20 21
#include <QPoint>
#include <QPointF>
#include <QPainterPath>

#include "UBDockPalette.h"
#include "core/UBSettings.h"
#include "frameworks/UBPlatformUtils.h"
22 23
#include "core/UBApplication.h"
#include "core/UBPreferencesController.h"
24
#include "core/UBDownloadManager.h"
Claudio Valerio's avatar
Claudio Valerio committed
25

26 27
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
28 29 30 31 32 33 34 35 36 37 38 39
/**
 * \brief The constructor
 */
UBDockPalette::UBDockPalette(QWidget *parent, const char *name)
    :QWidget(parent, Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint)
    , mOrientation(eUBDockOrientation_Left)
    , mPreferredWidth(100)
    , mPreferredHeight(100)
    , mCanResize(false)
    , mResized(false)
    , mCollapseWidth(150)
    , mLastWidth(-1)
40
    , mHTab(0)
shibakaneki's avatar
shibakaneki committed
41
    , mpStackWidget(NULL)
42 43
    , mpLayout(NULL)
    , mCurrentTab(0)
Ivan Ilin's avatar
Ivan Ilin committed
44
    , mTabPalette(new UBTabDockPalete(this, parent))
Claudio Valerio's avatar
Claudio Valerio committed
45 46 47
{
    setObjectName(name);

48 49 50 51 52 53
    mpLayout = new QVBoxLayout();
    setLayout(mpLayout);

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

shibakaneki's avatar
shibakaneki committed
54 55 56
    // clear the tab widgets
    mTabWidgets.clear();

Claudio Valerio's avatar
Claudio Valerio committed
57
    // We let 2 pixels in order to keep a small border for the resizing
Ivan Ilin's avatar
Ivan Ilin committed
58
    setMinimumWidth(/*2*border() + 2*/0);
Claudio Valerio's avatar
Claudio Valerio committed
59 60 61

    if (parent)
    {
62 63
        setAttribute(Qt::WA_NoMousePropagation);
        setAttribute(Qt::WA_TranslucentBackground);
Claudio Valerio's avatar
Claudio Valerio committed
64 65 66
    }
    else
    {
67 68
        // standalone window
        setAttribute(Qt::WA_TranslucentBackground);
Claudio Valerio's avatar
Claudio Valerio committed
69 70 71 72 73 74
    }

    mBackgroundBrush = QBrush(UBSettings::paletteColor);

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

    // Set the position of the tab
    onToolbarPosUpdated();
    connect(UBSettings::settings()->appToolBarPositionedAtTop, SIGNAL(changed(QVariant)), this, SLOT(onToolbarPosUpdated()));
79
    connect(UBDownloadManager::downloadManager(), SIGNAL(allDownloadsFinished()), this, SLOT(onAllDownloadsFinished()));
Claudio Valerio's avatar
Claudio Valerio committed
80 81 82 83 84 85 86
}

/**
 * \brief The destructor
 */
UBDockPalette::~UBDockPalette()
{
shibakaneki's avatar
shibakaneki committed
87 88 89 90 91
    if(NULL != mpStackWidget)
    {
        delete mpStackWidget;
        mpStackWidget = NULL;
    }
92 93 94 95 96
    if(NULL != mpLayout)
    {
        delete mpLayout;
        mpLayout = NULL;
    }
Ivan Ilin's avatar
Ivan Ilin committed
97 98 99 100
    if (NULL != mTabPalette) {
        delete mTabPalette;
        mTabPalette = NULL;
    }
Claudio Valerio's avatar
Claudio Valerio committed
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
}

/**
 * \brief Get the current orientation
 * @return the current orientation
 */
eUBDockOrientation UBDockPalette::orientation()
{
    return mOrientation;
}

/**
 * \brief Set the current orientation
 * @param orientation as the given orientation
 */
void UBDockPalette::setOrientation(eUBDockOrientation orientation)
{
    // Set the size
    mOrientation = orientation;

    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());
    }
}

/**
 * \brief Handle the mouse move event
 * @param as the mouse event
 */
Ivan Ilin's avatar
Ivan Ilin committed
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
//void UBDockPalette::mouseMoveEvent(QMouseEvent *event)
//{

//    QPoint p = event->pos();

//    if(mCanResize && ((mMousePressPos - p).manhattanLength() > QApplication::startDragDistance()))
//    {
//	switch(mOrientation)
//	{
//	case eUBDockOrientation_Left:
//            if(p.x() < collapseWidth() && p.x() >= minimumWidth())
//            {
//                resize(border(), height());
//                mLastWidth = collapseWidth() + 1;
//                mResized = true;
//            }
//            else if(p.x() <= maximumWidth() && p.x() >= minimumWidth())
//	    {
//		resize(p.x(), height());
//                mResized = true;
//	    }
//	    break;
//	case eUBDockOrientation_Right:
//            if((this->x() + p.x() > parentWidget()->width() - collapseWidth()) && (this->x() + p.x() < parentWidget()->width()))
//            {
//                resize(border(), height());
//                mLastWidth = collapseWidth() + 1;
//                mResized = true;
//            }
//            else if((this->x() + p.x() >= parentWidget()->width() - maximumWidth()) && (this->x() + p.x() <= parentWidget()->width() - this->minimumWidth()))
//	    {
//		resize(parentWidget()->width() - (this->x() + p.x()), height());
//                mResized = true;
//	    }

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

//	default:
//	    break;
//	}
//    }
//}
Claudio Valerio's avatar
Claudio Valerio committed
187 188 189 190 191

/**
 * \brief Handle the mouse press event
 * @param as the mouse event
 */
Ivan Ilin's avatar
Ivan Ilin committed
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
//void UBDockPalette::mousePressEvent(QMouseEvent *event)
//{
//    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();
//    mMousePressPos = p;
//    mResized = false;

//    switch(mOrientation)
//    {
//    case eUBDockOrientation_Left:
//        if((p.x() >= width() - 2 * border()) && (p.x() <= width()))
//	{
//	    mCanResize = true;
//	}
//	break;
//    case eUBDockOrientation_Right:
//        if((p.x() >= 0) && (p.x() <= 2 * border()))
//	{
//	    mCanResize = true;
//	}
//	break;
//    case eUBDockOrientation_Top:
//	// Not supported yet
//	break;
//    case eUBDockOrientation_Bottom:
//	// Not supported yet
//	break;
//    default:
//	break;
//    }
//}
Claudio Valerio's avatar
Claudio Valerio committed
225 226 227 228 229

/**
 * \brief Handle the mouse release event
 * @param event as the mouse event
 */
Ivan Ilin's avatar
Ivan Ilin committed
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
//void UBDockPalette::mouseReleaseEvent(QMouseEvent *event)
//{
//    Q_UNUSED(event);
//    if(!mResized && mClickTime.elapsed() < CLICKTIME)
//    {
//        int nbTabs = mTabWidgets.size();
//        int clickedTab = 0;
//        // If the clicked position is in the tab, perform the related action
//        if(eUBDockOrientation_Left == mOrientation)
//        {
//            if(mMousePressPos.x() >= width() - 2*border() &&
//               mMousePressPos.y() >= mHTab &&
//               mMousePressPos.x() <= width() &&
//               mMousePressPos.y() <= mHTab + nbTabs*TABSIZE + (nbTabs -1)*tabSpacing())
//            {
//                clickedTab = (mMousePressPos.y() - mHTab)/(TABSIZE+tabSpacing());
//                tabClicked(clickedTab);
//            }
//        }
//        else if(eUBDockOrientation_Right == mOrientation)
//        {
//            if(mMousePressPos.x() >= 0 &&
//               mMousePressPos.x() <= 2*border() &&
//               mMousePressPos.y() >= mHTab &&
//               mMousePressPos.y() <= mHTab + nbTabs*TABSIZE + (nbTabs -1)*tabSpacing())
//            {
//                clickedTab = (mMousePressPos.y() - mHTab)/(TABSIZE+tabSpacing());
//                tabClicked(clickedTab);
//            }
//        }
//    }

//    mCanResize = false;
//}
Claudio Valerio's avatar
Claudio Valerio committed
264

265 266 267 268
/**
 * \brief Handle the resize event
 * @param event as the resize event
 */
Claudio Valerio's avatar
Claudio Valerio committed
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
void UBDockPalette::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    updateMaxWidth();
    if(parentWidget())
    {
        setMinimumHeight(parentWidget()->height());
    }
	// Set the position
    QPoint origin;
    switch(mOrientation)
    {
    case eUBDockOrientation_Right:
		origin.setX(parentWidget()->width() - this->width());
		origin.setY(0);
	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());
Ivan Ilin's avatar
Ivan Ilin committed
296
    moveTabs();
Claudio Valerio's avatar
Claudio Valerio committed
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
}

/**
 * \brief Handle the mouse enter event
 * @param event as the mouse event
 */
void UBDockPalette::enterEvent(QEvent *event)
{
    Q_UNUSED(event);
    // We want to set the cursor as an arrow everytime it enters the palette
    setCursor(Qt::ArrowCursor);
}

/**
 * \brief Handle the mouse leave event
 * @param event as the mouse event
 */
void UBDockPalette::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    // Restore the cursor to its previous shape
    unsetCursor();
}

/**
 * \brief Draw the palette
 * @param event as the paint event
 */
void UBDockPalette::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(mBackgroundBrush);
332 333
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
Claudio Valerio's avatar
Claudio Valerio committed
334

335 336
    int nbTabs = mTabWidgets.size();
    if(0 < nbTabs)
337
    {
338
        // First draw the BIG RECTANGLE (I write it big because the rectangle is big...)
shibakaneki's avatar
shibakaneki committed
339 340
        if(mOrientation == eUBDockOrientation_Left)
        {
Ivan Ilin's avatar
Ivan Ilin committed
341
            path.addRect(0.0, 0.0, width(), height());
shibakaneki's avatar
shibakaneki committed
342 343 344
        }
        else if(mOrientation == eUBDockOrientation_Right)
        {
Ivan Ilin's avatar
Ivan Ilin committed
345
            path.addRect(0.0, 0.0, width(), height());
346 347 348 349 350 351
        }

        // THEN DRAW THE small tabs (yes, the tabs are small...)
        if(eUBDockTabOrientation_Up == mTabsOrientation)
        {
            mHTab = border();
shibakaneki's avatar
shibakaneki committed
352 353 354
        }
        else
        {
355 356
            mHTab = height() - border() - nbTabs*TABSIZE - (nbTabs-1)*tabSpacing();
        }
Ivan Ilin's avatar
Ivan Ilin committed
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
        painter.drawPath(path);

//        for(int i = 0; i < mTabWidgets.size(); i++)
//        {
//            UBDockPaletteWidget* pCrntWidget = mTabWidgets.at(i);
//            if(NULL != pCrntWidget)
//            {
//                if(mOrientation == eUBDockOrientation_Left)
//                {
//                    path.addRoundedRect(width()-4*border(), mHTab + i*TABSIZE + i*tabSpacing(), 4*border(), TABSIZE, radius(), radius());
//                    painter.drawPath(path);
//                    QPixmap iconPixmap;
//                    if(mCollapseWidth >= width())
//                    {
//                        // Get the collapsed icon
//                        iconPixmap = pCrntWidget->iconToRight();
//                    }
//                    else
//                    {
//                        // Get the expanded icon
//                        iconPixmap = pCrntWidget->iconToLeft();
//                    }
//                    painter.drawPixmap(width() - 2*border() + 1, mHTab + i*TABSIZE + i*tabSpacing() + 1 , 2*border() - 4, TABSIZE - 2, iconPixmap);
//                }
//                else if(mOrientation == eUBDockOrientation_Right)
//                {
//                    path.addRoundedRect(0.0, mHTab + i*TABSIZE + i*tabSpacing(), 4*border(), TABSIZE, radius(), radius());
//                    painter.drawPath(path);
//                    QPixmap iconPixmap;
//                    if(mCollapseWidth >= width())
//                    {
//                        // Get the collapsed icon
//                        iconPixmap = pCrntWidget->iconToLeft();
//                    }
//                    else
//                    {
//                        // Get the expanded icon
//                        iconPixmap = pCrntWidget->iconToRight();
//                    }
//                    painter.drawPixmap(2, mHTab + i*TABSIZE + i*tabSpacing() + 1, 2*border() - 4, TABSIZE - 2, iconPixmap);
//                }
//                else
//                {
//                    painter.drawRoundedRect(border(), border(), width() - 2 * border(), height() - 2 * border(), radius(), radius());
//                }
//            }
//        }
Claudio Valerio's avatar
Claudio Valerio committed
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
    }
}

/**
 * \brief Set the background brush
 * @param brush as the given brush
 */
void UBDockPalette::setBackgroundBrush(const QBrush &brush)
{
    if (mBackgroundBrush != brush)
    {
	mBackgroundBrush = brush;
	update();
    }
}

/**
 * Get the border size
 * @return the border size
 */
int UBDockPalette::border()
{
    return 15;
}

/**
 * \brief Get the radius size
 * @return the radius size
 */
int UBDockPalette::radius()
{
    return 5;
}

/**
 * \brief Set the maximum width
 */
void UBDockPalette::updateMaxWidth()
{
    // Only the inherited class will overload this method
}

/**
 * \brief Get the collapse width value
 * @return the collapse widht value
 */
int UBDockPalette::collapseWidth()
{
    return mCollapseWidth;
}

/**
 * \brief collapse/expand automatically the palette after a click on its tab
 */
458 459 460 461 462 463 464 465 466 467 468 469
void UBDockPalette::tabClicked(int tabIndex)
{
    // If the current tab is not the clicked one, show its content
    if(mCurrentTab != tabIndex)
    {
        showTabWidget(tabIndex);
    }
    // else collapse the palette
    else
    {
        toggleCollapseExpand();
    }
Ivan Ilin's avatar
Ivan Ilin committed
470
    mTabPalette->update();
471 472
}

473 474 475 476
/**
 * \brief Show the tab widget related to the given index
 * @param tabIndex as the given index
 */
477 478 479 480 481 482 483 484 485 486
void UBDockPalette::showTabWidget(int tabIndex)
{
    mpStackWidget->setCurrentIndex(tabIndex);
    mCurrentTab = tabIndex;

    // Expand it if collapsed
    if(mLastWidth != -1)
    {
        toggleCollapseExpand();
    }
487 488 489 490 491 492

    // Update the current tab index
    if(NULL != (dynamic_cast<UBDockPaletteWidget*>(mpStackWidget->widget(tabIndex)))){
        mCrntTabWidget = dynamic_cast<UBDockPaletteWidget*>(mpStackWidget->widget(tabIndex))->name();
    }

493 494
}

495 496 497
/**
 * \brief Toggle the collapse / expand state
 */
498
void UBDockPalette::toggleCollapseExpand()
Claudio Valerio's avatar
Claudio Valerio committed
499 500 501 502 503
{
    if(mLastWidth == -1)
    {
        // The palette must be collapsed
        mLastWidth = width();
Ivan Ilin's avatar
Ivan Ilin committed
504
        resize(0, height());
Claudio Valerio's avatar
Claudio Valerio committed
505 506 507 508 509 510 511 512
    }
    else
    {
        // The palette will be expanded
        resize(mLastWidth, height());
        mLastWidth = -1;
    }
}
513

514 515 516 517
/**
 * \brief Set the tabs orientation
 * @param orientation as the given tabs orientation
 */
518 519 520 521 522
void UBDockPalette::setTabsOrientation(eUBDockTabOrientation orientation)
{
    mTabsOrientation = orientation;
}

523 524 525
/**
 * \brief Update the tab position regarding the toolbar position (up or down)
 */
526 527 528 529 530 531 532 533 534 535 536 537 538 539
void UBDockPalette::onToolbarPosUpdated()
{
    // Get the position of the tab
    if(UBSettings::settings()->appToolBarPositionedAtTop->get().toBool())
    {
        setTabsOrientation(eUBDockTabOrientation_Up);
    }
    else
    {
        setTabsOrientation(eUBDockTabOrientation_Down);
    }
    update();
}

540 541 542 543
/**
 * \brief Get the custom margin
 * @return the custom margin value
 */
544 545 546 547
int UBDockPalette::customMargin()
{
    return 5;
}
shibakaneki's avatar
shibakaneki committed
548

549 550 551 552
/**
 * \brief Add the given tab widget
 * @param widget as the given widget
 */
553 554 555 556 557 558 559 560
void UBDockPalette::addTab(UBDockPaletteWidget *widget)
{
    if(!mTabWidgets.contains(widget) && widget->visibleState())
    {
        widget->setVisible(true);
        mTabWidgets.append(widget);
        mpStackWidget->addWidget(widget);
        mpStackWidget->setCurrentWidget(widget);
Ivan Ilin's avatar
Ivan Ilin committed
561
        resizeTabs();
562 563
        update();
    }
shibakaneki's avatar
shibakaneki committed
564
}
565 566 567 568
/**
 * \brief Remove the given tab
 * @param widgetName as the tab widget name
 */
569 570 571 572 573 574 575 576 577 578 579 580 581 582
void UBDockPalette::removeTab(const QString &widgetName)
{
    for(int i = 0; i < mTabWidgets.size(); i++)
    {
        UBDockPaletteWidget* pCrntWidget = mTabWidgets.at(i);
        if(NULL != pCrntWidget && (pCrntWidget->name() == widgetName))
        {
            mpStackWidget->removeWidget(pCrntWidget);
            mTabWidgets.remove(i);
            pCrntWidget->hide();
            update();
            break;
        }
    }
Ivan Ilin's avatar
Ivan Ilin committed
583 584
    resizeTabs();
    mCurrentTab = qMax(mCurrentTab - 1, 0);
585 586
}

587 588 589 590
/**
 * \brief Handle the resize request
 * @param event as the given resize request
 */
591 592 593 594 595
void UBDockPalette::onResizeRequest(QResizeEvent *event)
{
    resizeEvent(event);
}

596 597 598 599
/**
 * \brief Get the tab spacing
 * @return the tab spacing
 */
600 601 602
int UBDockPalette::tabSpacing()
{
    return 2;
shibakaneki's avatar
shibakaneki committed
603
}
604

605 606 607 608
/**
 * \brief Show the given widget
 * @param widgetName as the given widget name
 */
609 610 611 612 613 614 615
void UBDockPalette::onShowTabWidget(const QString &widgetName)
{
    for(int i = 0; i < mRegisteredWidgets.size(); i++)
    {
        UBDockPaletteWidget* pCrntWidget = mRegisteredWidgets.at(i);
        if(NULL != pCrntWidget && (pCrntWidget->name() == widgetName))
        {
616
            pCrntWidget->setVisibleState(true);
617
            addTab(pCrntWidget);
618 619 620 621 622
            break;
        }
    }
}

623 624 625 626
/**
 * \brief Hide the given widget
 * @param widgetName as the given widget name
 */
627 628
void UBDockPalette::onHideTabWidget(const QString &widgetName)
{
629 630 631 632 633 634 635
    for(int i = 0; i < mRegisteredWidgets.size(); i++){
        UBDockPaletteWidget* pCrntWidget = mRegisteredWidgets.at(i);
        if(NULL != pCrntWidget && (pCrntWidget->name() == widgetName)){
            pCrntWidget->setVisibleState(false);
            break;
        }
    }
636 637 638
    removeTab(widgetName);
}

639 640 641
/**
 * \brief Connect the show / hide signals of the widget related to this dock palette
 */
642 643 644 645 646 647 648 649 650
void UBDockPalette::connectSignals()
{
    for(int i=0; i < mRegisteredWidgets.size(); i++)
    {
        connect(mRegisteredWidgets.at(i), SIGNAL(showTab(QString)), this, SLOT(onShowTabWidget(QString)));
        connect(mRegisteredWidgets.at(i), SIGNAL(hideTab(QString)), this, SLOT(onHideTabWidget(QString)));
    }
}

651 652 653 654
/**
 * \brief Register the given widget
 * @param widget as the given widget
 */
655 656 657 658
void UBDockPalette::registerWidget(UBDockPaletteWidget *widget)
{
    if(!mRegisteredWidgets.contains(widget))
    {
659 660
        // Update the parent of this widget
        widget->setParent(this);
661 662 663 664 665 666
        mRegisteredWidgets.append(widget);

        // By default, the widget is hidden
        widget->hide();
    }
}
667 668 669 670 671 672 673 674 675 676 677 678 679

/**
  * \brief Handles the 'all download finished' notification
  */
void UBDockPalette::onAllDownloadsFinished()
{
    for(int i=0; i<mTabWidgets.size(); i++){
        UBDockPaletteWidget* pW = mTabWidgets.at(i);
        if(NULL != pW && mCrntTabWidget == pW->name()){
            mpStackWidget->setCurrentWidget(pW);
        }
    }
}
Ivan Ilin's avatar
Ivan Ilin committed
680 681 682 683 684 685 686 687 688 689 690

void UBDockPalette::moveTabs()
{
    if (!mHTab) {
        if(eUBDockTabOrientation_Up == mTabsOrientation) {
            mHTab = border();
        } else {
            mHTab = height() - border() - mTabWidgets.size() * TABSIZE - (mTabWidgets.size() - 1) * tabSpacing();
        }
    }

Ivan Ilin's avatar
Ivan Ilin committed
691
    QPoint origin(width(), mHTab + mTabPalette->mVerticalOffset);
Ivan Ilin's avatar
Ivan Ilin committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

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

    mTabPalette->move(origin.x(), origin.y());
}
void UBDockPalette::resizeTabs()
{
    int numTabs = mTabWidgets.size();
    mTabPalette->resize(2 * border(), (numTabs * TABSIZE) + qMax(numTabs - 1, 0) * tabSpacing());
}
QRect UBDockPalette::getTabPaletteRect()
{
    QRect tabsRect = mTabPalette->rect();
    QPoint topLeft = mTabPalette->mapToParent(tabsRect.topLeft());
    QPoint bottomRight = mTabPalette->mapToParent(tabsRect.bottomRight());

    return QRect(topLeft, bottomRight);
}
void UBDockPalette::assignParent(QWidget *widget)
{
    setParent(widget);
    mTabPalette->setParent(widget);
}
void UBDockPalette::setVisible(bool visible)
{
    QWidget::setVisible(visible);
    mTabPalette->setVisible(visible);
}

UBTabDockPalete::UBTabDockPalete(UBDockPalette *dockPalette, QWidget *parent) :
    QWidget(parent)
  , dock(dockPalette)
Ivan Ilin's avatar
Ivan Ilin committed
735 736
  , mVerticalOffset(0)
  , mFlotable(false)
Ivan Ilin's avatar
Ivan Ilin committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
{
    int numTabs = dock->mTabWidgets.size();
    resize(2 * dock->border(), (numTabs * TABSIZE) + qMax(numTabs - 1, 0) * dock->tabSpacing());
//    move(dock->width(), 20);

    setAttribute(Qt::WA_TranslucentBackground);
}

void UBTabDockPalete::paintEvent(QPaintEvent */*event*/)
{
    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();
    }
}
UBTabDockPalete::~UBTabDockPalete()
{
}

void UBTabDockPalete::mousePressEvent(QMouseEvent *event)
{
    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;
    }
}
void UBTabDockPalete::mouseMoveEvent(QMouseEvent *event)
{
    QPoint p = event->pos();

    if(dock->mCanResize && ((dock->mMousePressPos - p).manhattanLength() > QApplication::startDragDistance()))
    {
Ivan Ilin's avatar
Ivan Ilin committed
851 852 853 854 855 856
        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());
        }

Ivan Ilin's avatar
Ivan Ilin committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
        switch(dock->mOrientation) {

        case eUBDockOrientation_Left:
            p.setX(p.x() + dock->width());
            if(p.x() < dock->collapseWidth() && p.x() >= dock->minimumWidth()) {
                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())) {
                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;
        }
    }
}
void UBTabDockPalete::mouseReleaseEvent(QMouseEvent *event)
{
    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(eUBDockOrientation_Left == dock->mOrientation) {
            if(dock->mMousePressPos.y() >= dock->mHTab &&
                    dock->mMousePressPos.x() <= width() &&
                    dock->mMousePressPos.y() <= dock->mHTab + nbTabs * TABSIZE + (nbTabs -1)*dock->tabSpacing()) {

                clickedTab = (dock->mMousePressPos.y() - dock->mHTab) / (TABSIZE + dock->tabSpacing());
                dock->tabClicked(clickedTab);
            }

        } else if (eUBDockOrientation_Right == dock->mOrientation) {
            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;
}