UBGraphicsItemDelegate.cpp 37.6 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
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
 * (at your option) any later version.
Claudio Valerio's avatar
Claudio Valerio committed
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9 10 11 12 13
 * 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
14 15 16 17 18 19 20
 */

#include <QtGui>
#include <QtSvg>
#include <QDrag>

#include "UBGraphicsItemDelegate.h"
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
21
#include "UBGraphicsMediaItemDelegate.h"
Claudio Valerio's avatar
Claudio Valerio committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "UBGraphicsDelegateFrame.h"
#include "UBGraphicsScene.h"
#include "UBGraphicsItemUndoCommand.h"
#include "UBGraphicsItemTransformUndoCommand.h"

#include "board/UBBoardController.h" // TODO UB 4.x clean that dependency
#include "board/UBBoardView.h" // TODO UB 4.x clean that dependency

#include "core/UBApplication.h"
#include "core/UBApplicationController.h"
#include "core/UBDisplayManager.h"
#include "core/UBSettings.h"
#include "core/UBPersistenceManager.h"

#include "document/UBDocumentProxy.h"

#include "UBGraphicsWidgetItem.h"

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
40
#include "domain/UBGraphicsTextItem.h"
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
41
#include "domain/UBGraphicsMediaItem.h"
Claudio Valerio's avatar
Claudio Valerio committed
42
#include "domain/UBGraphicsGroupContainerItem.h"
Claudio Valerio's avatar
Claudio Valerio committed
43 44 45 46 47 48

#include "web/UBWebController.h"

#include "frameworks/UBFileSystemUtils.h"
#include "board/UBDrawingController.h"

49 50
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
51 52
class UBGraphicsParaschoolEditorWidgetItem;

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
DelegateButton::DelegateButton(const QString & fileName, QGraphicsItem* pDelegated, QGraphicsItem * parent, Qt::WindowFrameSection section)
    : QGraphicsSvgItem(fileName, parent)
    , mDelegated(pDelegated)
    , mIsTransparentToMouseEvent(false)
    , mButtonAlignmentSection(section)
{
    setAcceptedMouseButtons(Qt::LeftButton);
    setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
}

DelegateButton::~DelegateButton()
{
    // NOOP
}

void DelegateButton::setFileName(const QString & fileName)
{
    QGraphicsSvgItem::setSharedRenderer(new QSvgRenderer (fileName, this));
}


74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
void DelegateButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    // make sure delegate is selected, to avoid control being hidden
    mPressedTime = QTime::currentTime();
//    mDelegated->setSelected(true);

    event->setAccepted(!mIsTransparentToMouseEvent);
 }

void DelegateButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    int timeto = qAbs(QTime::currentTime().msecsTo(mPressedTime));

    if (timeto < UBSettings::longClickInterval) {
        emit clicked();
    } else {
        emit longClicked();
    }

    event->setAccepted(!mIsTransparentToMouseEvent);
}

96
UBGraphicsItemDelegate::UBGraphicsItemDelegate(QGraphicsItem* pDelegated, QObject * parent, bool respectRatio, bool canRotate, bool useToolBar)
Claudio Valerio's avatar
Claudio Valerio committed
97 98 99 100 101 102 103 104 105 106 107 108
    : QObject(parent)
    , mDelegated(pDelegated)
    , mDeleteButton(NULL)
    , mDuplicateButton(NULL)
    , mMenuButton(NULL)
    , mMenu(0)
    , mLockAction(0)
    , mShowOnDisplayAction(0)
    , mGotoContentSourceAction(0)
    , mFrame(0)
    , mFrameWidth(UBSettings::settings()->objectFrameWidth)
    , mAntiScaleRatio(1.0)
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
109
    , mToolBarItem(NULL)
Claudio Valerio's avatar
Claudio Valerio committed
110 111 112 113
    , mCanRotate(canRotate)
    , mCanDuplicate(true)
    , mRespectRatio(respectRatio)
    , mMimeData(NULL)
114
    , mFlippable(false)
115
    , mToolBarUsed(useToolBar)
Claudio Valerio's avatar
Claudio Valerio committed
116 117
{
    // NOOP
118
    connect(UBApplication::boardController, SIGNAL(zoomChanged(qreal)), this, SLOT(onZoomChanged()));
Claudio Valerio's avatar
Claudio Valerio committed
119 120 121 122
}

void UBGraphicsItemDelegate::init()
{
123 124
    if (mToolBarUsed)
        mToolBarItem = new UBGraphicsToolBarItem(mDelegated);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
125

Claudio Valerio's avatar
Claudio Valerio committed
126 127 128 129
    mFrame = new UBGraphicsDelegateFrame(this, QRectF(0, 0, 0, 0), mFrameWidth, mRespectRatio);
    mFrame->hide();
    mFrame->setFlag(QGraphicsItem::ItemIsSelectable, true);

130
    mDeleteButton = new DelegateButton(":/images/close.svg", mDelegated, mFrame, Qt::TopLeftSection);
Claudio Valerio's avatar
Claudio Valerio committed
131 132 133
    mButtons << mDeleteButton;
    connect(mDeleteButton, SIGNAL(clicked()), this, SLOT(remove()));
    if (canDuplicate()){
134
        mDuplicateButton = new DelegateButton(":/images/duplicate.svg", mDelegated, mFrame, Qt::TopLeftSection);
Claudio Valerio's avatar
Claudio Valerio committed
135 136 137
        connect(mDuplicateButton, SIGNAL(clicked(bool)), this, SLOT(duplicate()));
        mButtons << mDuplicateButton;
    }
138
    mMenuButton = new DelegateButton(":/images/menu.svg", mDelegated, mFrame, Qt::TopLeftSection);
Claudio Valerio's avatar
Claudio Valerio committed
139 140 141
    connect(mMenuButton, SIGNAL(clicked()), this, SLOT(showMenu()));
    mButtons << mMenuButton;

142
    mZOrderUpButton = new DelegateButton(":/images/z_layer_up.svg", mDelegated, mFrame, Qt::BottomLeftSection);
143 144
    connect(mZOrderUpButton, SIGNAL(clicked()), this, SLOT(increaseZLevelUp()));
    connect(mZOrderUpButton, SIGNAL(longClicked()), this, SLOT(increaseZlevelTop()));
145 146
    mButtons << mZOrderUpButton;

147
    mZOrderDownButton = new DelegateButton(":/images/z_layer_down.svg", mDelegated, mFrame, Qt::BottomLeftSection);
148 149
    connect(mZOrderDownButton, SIGNAL(clicked()), this, SLOT(increaseZLevelDown()));
    connect(mZOrderDownButton, SIGNAL(longClicked()), this, SLOT(increaseZlevelBottom()));
150 151
    mButtons << mZOrderDownButton;

Claudio Valerio's avatar
Claudio Valerio committed
152 153 154 155
    buildButtons();

    foreach(DelegateButton* button, mButtons)
    {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
156 157
        if (button->getSection() != Qt::TitleBarArea)
        {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
158 159 160
            button->hide();
            button->setFlag(QGraphicsItem::ItemIsSelectable, true);
        }
Claudio Valerio's avatar
Claudio Valerio committed
161
    }
162 163 164 165

    //Wrapper function. Use it to set correct data() to QGraphicsItem as well
    setFlippable(false);
    setRotatable(false);
Claudio Valerio's avatar
Claudio Valerio committed
166 167 168 169 170
}


UBGraphicsItemDelegate::~UBGraphicsItemDelegate()
{
171
    qDeleteAll(mButtons);
Claudio Valerio's avatar
Claudio Valerio committed
172 173 174 175 176 177
    // do not release mMimeData.
    // the mMimeData is owned by QDrag since the setMimeData call as specified in the documentation
}

QVariant UBGraphicsItemDelegate::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
178 179 180
    if(change == QGraphicsItem::ItemChildAddedChange){

    }else if (change == QGraphicsItem::ItemSelectedHasChanged) {
Ivan Ilin's avatar
Ivan Ilin committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194
        bool ok;
        bool selected = value.toUInt(&ok);
        if (ok) {
            UBGraphicsScene *ubScene = castUBGraphicsScene();
            if (ubScene) {
                if (selected) {
                    ubScene->setSelectedZLevel(delegated());
                } else {
                    ubScene->setOwnZlevel(delegated());
                }
            }
        }
    }

Claudio Valerio's avatar
Claudio Valerio committed
195 196 197
    if ((change == QGraphicsItem::ItemSelectedHasChanged
         || change == QGraphicsItem::ItemPositionHasChanged
         || change == QGraphicsItem::ItemTransformHasChanged)
198 199 200
            && mDelegated->scene()
            && UBApplication::boardController)
    {
Claudio Valerio's avatar
Claudio Valerio committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
        positionHandles();
    }

    if (change == QGraphicsItem::ItemPositionHasChanged
        || change == QGraphicsItem::ItemTransformHasChanged
        || change == QGraphicsItem::ItemZValueHasChanged)
    {
        UBGraphicsScene* ubScene = qobject_cast<UBGraphicsScene*>(mDelegated->scene());
        if(ubScene)
            ubScene->setModified(true);
    }

    return value;
}

217 218 219 220 221 222
UBGraphicsScene *UBGraphicsItemDelegate::castUBGraphicsScene()
{
    UBGraphicsScene *castScene = dynamic_cast<UBGraphicsScene*>(delegated()->scene());

    return castScene;
}
Claudio Valerio's avatar
Claudio Valerio committed
223 224 225

bool UBGraphicsItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
226
    mDragStartPosition = event->pos();
Claudio Valerio's avatar
Claudio Valerio committed
227 228 229

    startUndoStep();

230
    if (!delegated()->isSelected())
Claudio Valerio's avatar
Claudio Valerio committed
231
    {
232
        delegated()->setSelected(true);
Claudio Valerio's avatar
Claudio Valerio committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        return true;
    }
    else
    {
        return false;
    }
}

void UBGraphicsItemDelegate::setMimeData(QMimeData *mimeData)
{
    mMimeData = mimeData;
}

bool UBGraphicsItemDelegate::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
248 249 250 251 252 253
    if(mMimeData) {
        QDrag* mDrag = new QDrag(event->widget());
        mDrag->setMimeData(mMimeData);
        if (!mDragPixmap.isNull()) {
            mDrag->setPixmap(mDragPixmap);
            mDrag->setHotSpot(mDragPixmap.rect().center());
254
        }
255 256
        mDrag->exec();
        mDragPixmap = QPixmap();
Claudio Valerio's avatar
Claudio Valerio committed
257 258
        return true;
    }
259 260 261 262 263 264
    if(isLocked()) {
        event->accept();
        return true;
    } else {
        return false;
    }
Claudio Valerio's avatar
Claudio Valerio committed
265 266
}

267 268
bool UBGraphicsItemDelegate::weelEvent(QGraphicsSceneWheelEvent *event)
{
269
    Q_UNUSED(event);
270
    if( delegated()->isSelected() )
271
    {
272
//        event->accept();
273 274 275 276
        return true;
    }
    else
    {
277
//        event->ignore();
278 279 280
        return false;
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
281 282 283 284 285

bool UBGraphicsItemDelegate::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);

Ivan Ilin's avatar
Ivan Ilin committed
286 287 288 289 290 291 292 293 294 295 296
    //Deselect all the rest selected items if no ctrl key modifier
    if (delegated()->scene()
            && delegated()->scene()->selectedItems().count()
            && event->modifiers() != Qt::ControlModifier) {
        foreach (QGraphicsItem *item, delegated()->scene()->selectedItems()) {
            if (item != delegated()) {
                item->setSelected(false);
            }
        }
    }

Claudio Valerio's avatar
Claudio Valerio committed
297 298 299 300 301
    commitUndoStep();

    return true;
}

302
void UBGraphicsItemDelegate::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Claudio Valerio's avatar
Claudio Valerio committed
303
{
304 305 306 307 308
    Q_UNUSED(event)
//    if (!mDelegated->isSelected()) {
//        setZOrderButtonsVisible(true);
//    }
}
Claudio Valerio's avatar
Claudio Valerio committed
309

310 311 312 313 314 315 316
void UBGraphicsItemDelegate::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event)
//    if (!mDelegated->isSelected()) {
//        setZOrderButtonsVisible(false);
//    }
}
Claudio Valerio's avatar
Claudio Valerio committed
317

318 319 320 321 322 323 324 325 326 327 328 329
QGraphicsItem *UBGraphicsItemDelegate::delegated()
{
    QGraphicsItem *curDelegate = 0;
    if (mDelegated->parentItem() && mDelegated->parentItem()->type() == UBGraphicsGroupContainerItem::Type) {
        curDelegate = mDelegated->parentItem(); // considering delegated item as an item's group which contains everything
    } else {
        curDelegate = mDelegated;
    }

    return curDelegate;
}

330 331 332
void UBGraphicsItemDelegate::positionHandles()
{
    if (mDelegated->isSelected()) {
Claudio Valerio's avatar
Claudio Valerio committed
333 334
        bool shownOnDisplay = mDelegated->data(UBGraphicsItemData::ItemLayerType).toInt() != UBItemLayerType::Control;
        showHide(shownOnDisplay);
335
        mDelegated->setData(UBGraphicsItemData::ItemLocked, QVariant(isLocked()));
336
        updateFrame();
337 338 339

        if (UBStylusTool::Play != UBDrawingController::drawingController()->stylusTool())
            mFrame->show();
340 341

        updateButtons(true);
Claudio Valerio's avatar
Claudio Valerio committed
342

343
        if (mToolBarItem && mToolBarItem->isVisibleOnBoard())
Ivan Ilyin's avatar
Ivan Ilyin committed
344
        {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
345 346 347
            mToolBarItem->positionHandles();
            mToolBarItem->update();
            mToolBarItem->show();
Ivan Ilyin's avatar
Ivan Ilyin committed
348
        }
349
    } else {
Claudio Valerio's avatar
Claudio Valerio committed
350 351 352 353
        foreach(DelegateButton* button, mButtons)
            button->hide();

        mFrame->hide();
354 355
        if (mToolBarItem)
            mToolBarItem->hide();
Claudio Valerio's avatar
Claudio Valerio committed
356 357
    }
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
358

359 360 361 362 363 364 365 366 367 368 369 370
void UBGraphicsItemDelegate::setZOrderButtonsVisible(bool visible)
{
    if (visible) {
        updateFrame();
        updateButtons();

        QPointF newUpPoint = mFrame->mapToItem(mDelegated, mZOrderUpButton->pos());
        QPointF newDownPoint = mFrame->mapToItem(mDelegated, mZOrderDownButton->pos());


        mZOrderUpButton->setParentItem(mDelegated);
        mZOrderDownButton->setParentItem(mDelegated);
Claudio Valerio's avatar
Claudio Valerio committed
371

372 373 374 375 376 377 378 379 380 381 382
        mZOrderUpButton->setPos(newUpPoint + QPointF(0,0));
        mZOrderDownButton->setPos(newDownPoint + QPointF(0,0));

        mZOrderUpButton->show();
        mZOrderDownButton->show();

    } else {
        mZOrderUpButton->hide();
        mZOrderDownButton->hide();
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
383 384 385

void UBGraphicsItemDelegate::remove(bool canUndo)
{
Yimgo's avatar
Yimgo committed
386
    /*UBGraphicsScene* scene = dynamic_cast<UBGraphicsScene*>(mDelegated->scene());
387
    if (scene && canUndo)
Claudio Valerio's avatar
Claudio Valerio committed
388
    {
389 390
        UBGraphicsItemUndoCommand *uc = new UBGraphicsItemUndoCommand(scene, mDelegated, 0);
        UBApplication::undoStack->push(uc);
Claudio Valerio's avatar
Claudio Valerio committed
391
    }
Yimgo's avatar
Yimgo committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    mDelegated->hide();  */

    UBGraphicsScene* scene = dynamic_cast<UBGraphicsScene*>(mDelegated->scene());
    if (scene)
    {
        foreach(DelegateButton* button, mButtons)
            scene->removeItem(button);

        scene->removeItem(mFrame);

        /* this is performed because when removing delegated from scene while it contains flash content, segfault happens because of QGraphicsScene::removeItem() */ 
        UBGraphicsWebView *mDelegated_casted = dynamic_cast<UBGraphicsWebView*>(mDelegated);
        if (mDelegated_casted)
            mDelegated_casted->setHtml(QString());

        scene->removeItem(mDelegated);

        if (canUndo)
        {
            UBGraphicsItemUndoCommand *uc = new UBGraphicsItemUndoCommand(scene, mDelegated, 0);
            UBApplication::undoStack->push(uc);
        }
    }
Claudio Valerio's avatar
Claudio Valerio committed
415 416 417
}


418
bool UBGraphicsItemDelegate::isLocked() const
Claudio Valerio's avatar
Claudio Valerio committed
419 420 421 422 423 424 425
{
    return mDelegated->data(UBGraphicsItemData::ItemLocked).toBool();
}


void UBGraphicsItemDelegate::duplicate()
{
426
    UBApplication::boardController->duplicateItem(dynamic_cast<UBItem*>(delegated()));
Claudio Valerio's avatar
Claudio Valerio committed
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
void UBGraphicsItemDelegate::increaseZLevelUp()
{
    UBGraphicsScene *curScene = castUBGraphicsScene();
    if (curScene) {
        curScene->changeZLevelTo(delegated(), UBZLayerController::up);
    }
}
void UBGraphicsItemDelegate::increaseZlevelTop()
{
    UBGraphicsScene *curScene = castUBGraphicsScene();
    if (curScene) {
        curScene->changeZLevelTo(delegated(), UBZLayerController::top);
    }
}
void UBGraphicsItemDelegate::increaseZLevelDown()
{
    UBGraphicsScene *curScene = castUBGraphicsScene();
    if (curScene) {
        curScene->changeZLevelTo(delegated(), UBZLayerController::down);
    }
}
void UBGraphicsItemDelegate::increaseZlevelBottom()
{
    UBGraphicsScene *curScene = castUBGraphicsScene();
    if (curScene) {
        curScene->changeZLevelTo(delegated(), UBZLayerController::bottom);
    }
456
}
Claudio Valerio's avatar
Claudio Valerio committed
457 458 459 460 461 462 463 464 465 466 467 468 469

void UBGraphicsItemDelegate::lock(bool locked)
{
    if (locked)
    {
        mDelegated->setData(UBGraphicsItemData::ItemLocked, QVariant(true));
    }
    else
    {
        mDelegated->setData(UBGraphicsItemData::ItemLocked, QVariant(false));
    }

    mDelegated->update();
470
    positionHandles();
Claudio Valerio's avatar
Claudio Valerio committed
471 472 473
    mFrame->positionHandles();
}

474 475 476 477 478 479 480
void UBGraphicsItemDelegate::showHideRecurs(const QVariant &pShow, QGraphicsItem *pItem)
{
    pItem->setData(UBGraphicsItemData::ItemLayerType, pShow);
    foreach (QGraphicsItem *insideItem, pItem->childItems()) {
        showHideRecurs(pShow, insideItem);
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
481 482 483

void UBGraphicsItemDelegate::showHide(bool show)
{
484 485
    QVariant showFlag = QVariant(show ? UBItemLayerType::Object : UBItemLayerType::Control);
    showHideRecurs(showFlag, mDelegated);
Claudio Valerio's avatar
Claudio Valerio committed
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
    mDelegated->update();

    emit showOnDisplayChanged(show);
}


void UBGraphicsItemDelegate::gotoContentSource(bool checked)
{
    Q_UNUSED(checked)

    UBItem* item = dynamic_cast<UBItem*>(mDelegated);

    if(item && !item->sourceUrl().isEmpty())
    {
        UBApplication::applicationController->showInternet();
        UBApplication::webController->loadUrl(item->sourceUrl());
    }
}

void UBGraphicsItemDelegate::startUndoStep()
{
    mPreviousPosition = mDelegated->pos();
    mPreviousTransform = mDelegated->transform();
    mPreviousZValue = mDelegated->zValue();
    UBResizableGraphicsItem* resizableItem = dynamic_cast<UBResizableGraphicsItem*>(mDelegated);

    if (resizableItem)
        mPreviousSize = resizableItem->size();
    else
        mPreviousSize = QSizeF();
}


void UBGraphicsItemDelegate::commitUndoStep()
{
    UBResizableGraphicsItem* resizableItem = dynamic_cast<UBResizableGraphicsItem*>(mDelegated);

    if (mDelegated->pos() != mPreviousPosition
        || mDelegated->transform() != mPreviousTransform
        || mDelegated->zValue() != mPreviousZValue
        || (resizableItem && resizableItem->size() != mPreviousSize))
    {
        UBGraphicsItemTransformUndoCommand *uc =
                new UBGraphicsItemTransformUndoCommand(mDelegated,
                                                       mPreviousPosition,
                                                       mPreviousTransform,
                                                       mPreviousZValue,
                                                       mPreviousSize);

        UBApplication::undoStack->push(uc);
    }
}

539 540 541 542 543 544
void UBGraphicsItemDelegate::onZoomChanged()
{
    mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());

    positionHandles();
}
Claudio Valerio's avatar
Claudio Valerio committed
545

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
546 547 548 549
void UBGraphicsItemDelegate::buildButtons()
{
}

Claudio Valerio's avatar
Claudio Valerio committed
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
void UBGraphicsItemDelegate::decorateMenu(QMenu* menu)
{
    mLockAction = menu->addAction(tr("Locked"), this, SLOT(lock(bool)));
    QIcon lockIcon;
    lockIcon.addPixmap(QPixmap(":/images/locked.svg"), QIcon::Normal, QIcon::On);
    lockIcon.addPixmap(QPixmap(":/images/unlocked.svg"), QIcon::Normal, QIcon::Off);
    mLockAction->setIcon(lockIcon);
    mLockAction->setCheckable(true);

    mShowOnDisplayAction = mMenu->addAction(tr("Visible on Extended Screen"), this, SLOT(showHide(bool)));
    mShowOnDisplayAction->setCheckable(true);

    QIcon showIcon;
    showIcon.addPixmap(QPixmap(":/images/eyeOpened.svg"), QIcon::Normal, QIcon::On);
    showIcon.addPixmap(QPixmap(":/images/eyeClosed.svg"), QIcon::Normal, QIcon::Off);
    mShowOnDisplayAction->setIcon(showIcon);

    mGotoContentSourceAction = menu->addAction(tr("Go to Content Source"), this, SLOT(gotoContentSource(bool)));

    QIcon sourceIcon;
    sourceIcon.addPixmap(QPixmap(":/images/toolbar/internet.png"), QIcon::Normal, QIcon::On);
    mGotoContentSourceAction->setIcon(sourceIcon);

}

void UBGraphicsItemDelegate::updateMenuActionState()
{
    if (mLockAction)
        mLockAction->setChecked(isLocked());

    if (mShowOnDisplayAction)
    {
        bool isControl = mDelegated->data(UBGraphicsItemData::ItemLayerType) == UBItemLayerType::Control;
        mShowOnDisplayAction->setChecked(!isControl);
    }

    if (mGotoContentSourceAction)
    {
        UBItem* item = dynamic_cast<UBItem*>(mDelegated);
        mGotoContentSourceAction->setEnabled(item && !item->sourceUrl().isEmpty());
    }
}

void UBGraphicsItemDelegate::showMenu()
{
    if (!mMenu)
    {
        mMenu = new QMenu(UBApplication::boardController->controlView());
        decorateMenu(mMenu);
    }

    updateMenuActionState();

    UBBoardView* cv = UBApplication::boardController->controlView();
    QRect pinPos = cv->mapFromScene(mMenuButton->sceneBoundingRect()).boundingRect();

    mMenu->exec(cv->mapToGlobal(pinPos.bottomRight()));
}

609 610 611
void UBGraphicsItemDelegate::setFlippable(bool flippable)
{
    mFlippable = flippable;
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

    Q_ASSERT (mDelegated);
    if (mDelegated) {
        mDelegated->setData(UBGraphicsItemData::ItemFlippable, QVariant(flippable));
    }
}

void UBGraphicsItemDelegate::setRotatable(bool pCanRotate)
{
    mCanRotate = pCanRotate;

    Q_ASSERT(mDelegated);

    if (mDelegated) {
        mDelegated->setData(UBGraphicsItemData::ItemRotatable, QVariant(pCanRotate));
    }
628
}
Claudio Valerio's avatar
Claudio Valerio committed
629

630 631 632 633
bool UBGraphicsItemDelegate::isFlippable()
{
    return mFlippable;
}
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

void UBGraphicsItemDelegate::updateFrame()
{
    if (mFrame && !mFrame->scene() && mDelegated->scene())
    {
        mDelegated->scene()->addItem(mFrame);
    }

    mFrame->setAntiScale(mAntiScaleRatio);
    mFrame->positionHandles();
}

void UBGraphicsItemDelegate::updateButtons(bool showUpdated)
{
    QTransform tr;
    tr.scale(mAntiScaleRatio, mAntiScaleRatio);

    mDeleteButton->setParentItem(mFrame);
    mDeleteButton->setTransform(tr);

    qreal topX = mFrame->rect().left() - mDeleteButton->renderer()->viewBox().width() * mAntiScaleRatio / 2;
    qreal topY = mFrame->rect().top() - mDeleteButton->renderer()->viewBox().height() * mAntiScaleRatio / 2;

    qreal bottomX = mFrame->rect().left() - mDeleteButton->renderer()->viewBox().width() * mAntiScaleRatio / 2;
    qreal bottomY = mFrame->rect().bottom() - mDeleteButton->renderer()->viewBox().height() * mAntiScaleRatio / 2;

    mDeleteButton->setPos(topX, topY);

    if (!mDeleteButton->scene())
    {
        if (mDelegated->scene())
            mDelegated->scene()->addItem(mDeleteButton);
    }

668
    if (showUpdated /*&& mFrame->isResizing()*/)
669 670 671 672 673 674 675
        mDeleteButton->show();

    int i = 1, j = 0, k = 0;
    while ((i + j + k) < mButtons.size())  {
        DelegateButton* button = mButtons[i + j];

        if (button->getSection() == Qt::TopLeftSection) {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
676
            button->setParentItem(mFrame);
677
            button->setPos(topX + (i++ * 1.6 * mFrameWidth * mAntiScaleRatio), topY);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
678
            button->setTransform(tr);
679
        } else if (button->getSection() == Qt::BottomLeftSection) {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
680
            button->setParentItem(mFrame);
681
            button->setPos(bottomX + (++j * 1.6 * mFrameWidth * mAntiScaleRatio), bottomY);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
682 683
            button->setTransform(tr);
        } else if (button->getSection() == Qt::TitleBarArea || button->getSection() == Qt::NoSection){
684 685 686 687 688 689 690 691 692 693 694 695 696
            ++k;
        }
        if (!button->scene())
        {
            if (mDelegated->scene())
                mDelegated->scene()->addItem(button);
        }
        if (showUpdated) {
            button->show();
            button->setZValue(delegated()->zValue());
        }
    }
}
697

shibakaneki's avatar
shibakaneki committed
698 699 700 701 702 703
void UBGraphicsItemDelegate::setButtonsVisible(bool visible)
{
    foreach(DelegateButton* pButton, mButtons){
        pButton->setVisible(visible);
    }
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
704

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
705

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
706 707 708 709
UBGraphicsToolBarItem::UBGraphicsToolBarItem(QGraphicsItem * parent) : 
    QGraphicsRectItem(parent),
    mShifting(true),
    mVisible(false),
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
710
    mMinWidth(200),
Aleksei Kanash's avatar
Aleksei Kanash committed
711 712
    mInitialHeight(26),
    mElementsPadding(2)
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
713 714
{
    QRectF rect = this->rect();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
715 716
    rect.setHeight(mInitialHeight);
    rect.setWidth(parent->boundingRect().width());
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
717 718
    this->setRect(rect);

719
  //  setBrush(QColor(UBSettings::paletteColor));             
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
720 721
    setPen(Qt::NoPen);
    hide();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
722 723 724 725 726 727 728 729 730 731 732

    update();
}


void UBGraphicsToolBarItem::positionHandles()
{
    int itemXOffset = 0;
    foreach (QGraphicsItem* item, mItemsOnToolBar)
    {
        item->setPos(itemXOffset, 0);
Aleksei Kanash's avatar
Aleksei Kanash committed
733
        itemXOffset += (item->boundingRect().width()+mElementsPadding);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
734 735 736 737 738 739 740
        item->show();
    }
}

void UBGraphicsToolBarItem::update()
{
    QGraphicsRectItem::update();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
741 742 743 744 745 746 747 748 749 750
}

void UBGraphicsToolBarItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    QPainterPath path;
    path.addRoundedRect(rect(), 10, 10);  

751 752 753 754 755 756 757 758 759 760 761
    if (parentItem() && parentItem()->data(UBGraphicsItemData::ItemLocked).toBool())
    {
        QColor baseColor = UBSettings::paletteColor;
        baseColor.setAlphaF(baseColor.alphaF() / 3);
        setBrush(QBrush(baseColor));
    }
    else
    {
        setBrush(QBrush(UBSettings::paletteColor));
    }

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
762
    painter->fillPath(path, brush());
763
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
764 765

MediaTimer::MediaTimer(QGraphicsItem * parent): QGraphicsRectItem(parent) 
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
766 767 768 769 770
{
    val        = 0;
    smallPoint = false;
    setNumDigits(4);
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
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 851 852

MediaTimer::~MediaTimer()
{}

void MediaTimer::drawString(const QString &s, QPainter &p,
                                   QBitArray *newPoints, bool newString)
{
    QPoint  pos;

    int digitSpace = smallPoint ? 2 : 1;
    int xSegLen    = (rect().width()/1)*5/(ndigits*(5 + digitSpace) + digitSpace);
    int ySegLen    = rect().height()*5/12;
    int segLen     = ySegLen > xSegLen ? xSegLen : ySegLen;
    int xAdvance   = segLen*(5 + digitSpace)/5;
    int xOffset    = rect().x() + (rect().width()/1 - ndigits*xAdvance + segLen/5)/2;
    int yOffset    = (rect().height() - segLen*2)/2;

    for (int i=0;  i<ndigits; i++) {
        pos = QPoint(xOffset + xAdvance*i, yOffset);
        if (newString)
            drawDigit(pos, p, segLen, s[i].toLatin1(), digitStr[i].toLatin1());
        else
            drawDigit(pos, p, segLen, s[i].toLatin1());
        if (newPoints) {
            char newPoint = newPoints->testBit(i) ? '.' : ' ';
            if (newString) {
                char oldPoint = points.testBit(i) ? '.' : ' ';
                drawDigit(pos, p, segLen, newPoint, oldPoint);
            } else {
                drawDigit(pos, p, segLen, newPoint);
            }
        }
    }
    if (newString) {
        digitStr = s;
        digitStr.truncate(ndigits);
        if (newPoints)
            points = *newPoints;
    }
}

void MediaTimer::drawDigit(const QPoint &pos, QPainter &p, int segLen,
                                  char newCh, char oldCh)
{
     char updates[18][2];        // can hold 2 times number of segments, only
                                 // first 9 used if segment table is correct
     int  nErases;
     int  nUpdates;
     const char *segs;
     int  i,j;
 
     const char erase      = 0;
     const char draw       = 1;
     const char leaveAlone = 2;
 
     segs = getSegments(oldCh);
     for (nErases=0; segs[nErases] != 99; nErases++) {
         updates[nErases][0] = erase;            // get segments to erase to
         updates[nErases][1] = segs[nErases];    // remove old char
     }
     nUpdates = nErases;
     segs = getSegments(newCh);
     for(i = 0 ; segs[i] != 99 ; i++) {
         for (j=0;  j<nErases; j++)
             if (segs[i] == updates[j][1]) {   // same segment ?
                 updates[j][0] = leaveAlone;     // yes, already on screen
                 break;
             }
         if (j == nErases) {                   // if not already on screen
             updates[nUpdates][0] = draw;
             updates[nUpdates][1] = segs[i];
             nUpdates++;
         }
     }
     for (i=0; i<nUpdates; i++) {
         if (updates[i][0] == draw)
             drawSegment(pos, updates[i][1], p, segLen);
         if (updates[i][0] == erase)
             drawSegment(pos, updates[i][1], p, segLen, true);
     }
}

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
853
char MediaTimer::segments [][8] = 
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867
        { 
              { 0, 1, 2, 4, 5, 6,99, 0},             // 0    0
              { 2, 5,99, 0, 0, 0, 0, 0},             // 1    1
              { 0, 2, 3, 4, 6,99, 0, 0},             // 2    2
              { 0, 2, 3, 5, 6,99, 0, 0},             // 3    3
              { 1, 2, 3, 5,99, 0, 0, 0},             // 4    4
              { 0, 1, 3, 5, 6,99, 0, 0},             // 5    5
              { 0, 1, 3, 4, 5, 6,99, 0},             // 6    6
              { 0, 2, 5,99, 0, 0, 0, 0},             // 7    7
              { 0, 1, 2, 3, 4, 5, 6,99},             // 8    8
              { 0, 1, 2, 3, 5, 6,99, 0},             // 9    9
              { 8, 9,99, 0, 0, 0, 0, 0},             // 10   :
              {99, 0, 0, 0, 0, 0, 0, 0}              // 11   empty
        };
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
868 869 870

const char* MediaTimer::getSegments(char ch)               // gets list of segments for ch
{
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
871 872 873
     if (ch >= '0' && ch <= '9')
        return segments[ch - '0'];
     if (ch == ':')
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
874
        return segments[10];
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
875
     if (ch == ' ')
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
876
        return segments[11];
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
877
     
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
878
     return NULL;
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
}

void MediaTimer::drawSegment(const QPoint &pos, char segmentNo, QPainter &p,
                                    int segLen, bool erase)
{
    Q_UNUSED(erase);

    QPoint ppt;
    QPoint pt = pos;
    int width = segLen/5;
 
#define LINETO(X,Y) addPoint(a, QPoint(pt.x() + (X),pt.y() + (Y)))
#define LIGHT
#define DARK

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 930 931 932 933 934 935 936 937 938
    QPolygon a(0);
    switch (segmentNo) {
    case 0 :
        ppt = pt;
        LIGHT;
        LINETO(segLen - 1,0);
        DARK;
        LINETO(segLen - width - 1,width);
        LINETO(width,width);
        LINETO(0,0);
        break;
    case 1 :
        pt += QPoint(0 , 1);
        ppt = pt;
        LIGHT;
        LINETO(width,width);
        DARK;
        LINETO(width,segLen - width/2 - 2);
        LINETO(0,segLen - 2);
        LIGHT;
        LINETO(0,0);
        break;
    case 2 :
        pt += QPoint(segLen - 1 , 1);
        ppt = pt;
        DARK;
        LINETO(0,segLen - 2);
        LINETO(-width,segLen - width/2 - 2);
        LIGHT;
        LINETO(-width,width);
        LINETO(0,0);
        break;
    case 3 :
        pt += QPoint(0 , segLen);
        ppt = pt;
        LIGHT;
        LINETO(width,-width/2);
        LINETO(segLen - width - 1,-width/2);
        LINETO(segLen - 1,0);
        DARK;
        if (width & 1) {            // adjust for integer division error
            LINETO(segLen - width - 3,width/2 + 1);
            LINETO(width + 2,width/2 + 1);
        } else {
            LINETO(segLen - width - 1,width/2);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
939 940
            LINETO(width,width/2);
        }
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
        LINETO(0,0);
        break;
    case 4 :
        pt += QPoint(0 , segLen + 1);
        ppt = pt;
        LIGHT;
        LINETO(width,width/2);
        DARK;
        LINETO(width,segLen - width - 2);
        LINETO(0,segLen - 2);
        LIGHT;
        LINETO(0,0);
        break;
    case 5 :
        pt += QPoint(segLen - 1 , segLen + 1);
        ppt = pt;
        DARK;
        LINETO(0,segLen - 2);
        LINETO(-width,segLen - width - 2);
        LIGHT;
        LINETO(-width,width/2);
        LINETO(0,0);
        break;
    case 6 :
        pt += QPoint(0 , segLen*2);
        ppt = pt;
        LIGHT;
        LINETO(width,-width);
        LINETO(segLen - width - 1,-width);
        LINETO(segLen - 1,0);
        DARK;
        LINETO(0,0);
        break;
    case 7 :
        pt += QPoint(segLen/2 , segLen*2);
        ppt = pt;
        DARK;
        LINETO(width,0);
        LINETO(width,-width);
        LIGHT;
        LINETO(0,-width);
        LINETO(0,0);
        break;
    case 8 :
        pt += QPoint(segLen/2 - width/2 + 1 , segLen/2 + width);
        ppt = pt;
        DARK;
        LINETO(width,0);
        LINETO(width,-width);
        LIGHT;
        LINETO(0,-width);
        LINETO(0,0);
        break;
    case 9 :
        pt += QPoint(segLen/2 - width/2 + 1 , 3*segLen/2 + width);
        ppt = pt;
        DARK;
        LINETO(width,0);
        LINETO(width,-width);
        LIGHT;
        LINETO(0,-width);
        LINETO(0,0);
        break;
    default :
        break;
    }
    // End exact copy
    p.setPen(Qt::white);
    p.setBrush(Qt::white);
    p.drawPolygon(a);
    p.setBrush(Qt::NoBrush);

    pt = pos;
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

#undef LINETO
#undef LIGHT
#undef DARK
}

void MediaTimer::addPoint(QPolygon &a, const QPoint &p)
{
    uint n = a.size();
    a.resize(n + 1);
    a.setPoint(n, p);
}

void MediaTimer::paint(QPainter *p,
        const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    QFont f = p->font();
    f.setPointSizeF(f.pointSizeF());
    p->setFont(f);

    if (smallPoint)
        drawString(digitStr, *p, &points, false);
    else
        drawString(digitStr, *p, 0, false);
}

void MediaTimer::internalSetString(const QString& s)
{
    QString buffer;
    int i;
    int len = s.length();
    QBitArray newPoints(ndigits);

    if (!smallPoint) {
        if (len == ndigits)
            buffer = s;
        else
            buffer = s.right(ndigits).rightJustified(ndigits, QLatin1Char(' '));
    } else {
        int  index = -1;
        bool lastWasPoint = true;
        newPoints.clearBit(0);
        for (i=0; i<len; i++) {
            if (s[i] == QLatin1Char('.')) {
                if (lastWasPoint) {           // point already set for digit?
                    if (index == ndigits - 1) // no more digits
                        break;
                    index++;
                    buffer[index] = QLatin1Char(' ');        // 2 points in a row, add space
                }
                newPoints.setBit(index);        // set decimal point
                lastWasPoint = true;
            } else {
                if (index == ndigits - 1)
                    break;
                index++;
                buffer[index] = s[i];
                newPoints.clearBit(index);      // decimal point default off
                lastWasPoint = false;
            }
        }
        if (index < ((int) ndigits) - 1) {
            for(i=index; i>=0; i--) {
                buffer[ndigits - 1 - index + i] = buffer[i];
                newPoints.setBit(ndigits - 1 - index + i,
                                   newPoints.testBit(i));
            }
            for(i=0; i<ndigits-index-1; i++) {
                buffer[i] = QLatin1Char(' ');
                newPoints.clearBit(i);
            }
        }
    }

    if (buffer == digitStr)
        return;

    digitStr = buffer;
    if (smallPoint)
        points = newPoints;
    update();
}

void MediaTimer::display(const QString &s)
{
    val = 0;
    bool ok = false;
    double v = s.toDouble(&ok);
    if (ok)
        val = v;
    internalSetString(s);
}

void MediaTimer::setNumDigits(int numDigits)
{
    if (numDigits > 99) {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1113
        qWarning("QLCDNumber::setNumDigits: Max 99 digits allowed");
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1114 1115 1116
        numDigits = 99;
    }
    if (numDigits < 0) {
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1117
        qWarning("QLCDNumber::setNumDigits: Min 0 digits allowed");
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
        numDigits = 0;
    }
    if (digitStr.isNull()) {                  // from constructor
        ndigits = numDigits;
        digitStr.fill(QLatin1Char(' '), ndigits);
        points.fill(0, ndigits);
        digitStr[ndigits - 1] = QLatin1Char('0');            // "0" is the default number
    } else {
        if (numDigits == ndigits)             // no change
            return;
        register int i;
        int dif;
        if (numDigits > ndigits) {            // expand
            dif = numDigits - ndigits;
            QString buf;
            buf.fill(QLatin1Char(' '), dif);
            digitStr.insert(0, buf);
            points.resize(numDigits);
            for (i=numDigits-1; i>=dif; i--)
                points.setBit(i, points.testBit(i-dif));
            for (i=0; i<dif; i++)
                points.clearBit(i);
        } else {                                        // shrink
            dif = ndigits - numDigits;
            digitStr = digitStr.right(numDigits);
            QBitArray tmpPoints = points;
            points.resize(numDigits);
            for (i=0; i<(int)numDigits; i++)
                points.setBit(i, tmpPoints.testBit(i+dif));
        }
        ndigits = numDigits;
        update();
    }
}

DelegateMediaControl::DelegateMediaControl(UBGraphicsMediaItem* pDelegated, QGraphicsItem * parent)
    : QGraphicsRectItem(parent)
    , mDelegate(pDelegated)
    , mDisplayCurrentTime(false)
    , mCurrentTimeInMs(0)
    , mTotalTimeInMs(0)
    , mStartWidth(200)
1160
    , mSeecAreaBorderHeight(0)
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
{
    setAcceptedMouseButtons(Qt::LeftButton);
    setBrush(QBrush(Qt::white));
    setPen(Qt::NoPen);
    setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));

    lcdTimer = new MediaTimer(this);

    update();
}


void DelegateMediaControl::paint(QPainter *painter,
        const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    
    QPainterPath path;

    mLCDTimerArea.setHeight(rect().height());
    mLCDTimerArea.setWidth(rect().height());

Aleksei Kanash's avatar
Aleksei Kanash committed
1184
    mSeecArea.setWidth(rect().width()-mLCDTimerArea.width()-2);
1185 1186
    mSeecArea.setHeight(rect().height()-2*mSeecAreaBorderHeight);
    mSeecArea.setY(mSeecAreaBorderHeight);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1187 1188 1189 1190

    path.addRoundedRect(mSeecArea, mSeecArea.height()/2, mSeecArea.height()/2);
    painter->fillPath(path, brush());

1191
    qreal frameWidth = mSeecArea.height() / 2;
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1192 1193 1194 1195 1196 1197 1198 1199 1200
    int position = frameWidth;

    if (mTotalTimeInMs > 0)
    {
        position = frameWidth + ((mSeecArea.width() - (2 * frameWidth)) / mTotalTimeInMs) * mCurrentTimeInMs;
    }

    int clearance = 2;
    int radius = frameWidth-clearance;
1201
    QRectF r(position - radius, clearance+mSeecAreaBorderHeight, radius * 2, radius * 2);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

    painter->setBrush(UBSettings::documentViewLightColor);
    painter->drawEllipse(r);
}


QPainterPath DelegateMediaControl::shape() const
{
    QPainterPath path;
    path.addRoundedRect(rect(), rect().height()/ 2, rect().height()/2);
    return path;
}

void DelegateMediaControl::positionHandles()
{
    mLCDTimerArea.setWidth(parentItem()->boundingRect().height());
    mLCDTimerArea.setHeight(parentItem()->boundingRect().height());
    lcdTimer->setRect(mLCDTimerArea);
    lcdTimer->setPos(mSeecArea.width()-mLCDTimerArea.width(),0);

1222 1223 1224 1225
    mSeecAreaBorderHeight = rect().height()/20;
    mSeecArea.setWidth(rect().width()-mLCDTimerArea.width()-2);
    mSeecArea.setHeight(rect().height()-2*mSeecAreaBorderHeight);
    mSeecArea.setY(mSeecAreaBorderHeight);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

    QRectF selfRect = rect();
    selfRect.setHeight(parentItem()->boundingRect().height());
    setRect(selfRect);

    lcdTimer->setPos(rect().width() - mLCDTimerArea.width(), 0); 
}

void DelegateMediaControl::update()
{
    QTime t;
    t = t.addMSecs(mCurrentTimeInMs < 0 ? 0 : mCurrentTimeInMs);
    lcdTimer->display(t.toString("m:ss"));

    QGraphicsRectItem::update();
}

void DelegateMediaControl::updateTicker(qint64 time )
{
    mCurrentTimeInMs = time;
    update();
}


void DelegateMediaControl::totalTimeChanged(qint64 newTotalTime)
{
    mTotalTimeInMs = newTotalTime;
    update();
}


void DelegateMediaControl::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
   qreal frameWidth =  mSeecArea.height()/2;
    if (boundingRect().contains(event->pos() - QPointF(frameWidth,0)) 
        && boundingRect().contains(event->pos() + QPointF(frameWidth,0)))
    {  
        mDisplayCurrentTime = true;
        seekToMousePos(event->pos());
        this->update();
        event->accept();
1267
        emit used();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1268 1269 1270 1271 1272
    }
}

void DelegateMediaControl::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
1273
    qreal frameWidth = mSeecArea.height() / 2;
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1274 1275 1276 1277 1278 1279
    if (boundingRect().contains(event->pos() - QPointF(frameWidth,0)) 
        && boundingRect().contains(event->pos() + QPointF(frameWidth,0)))
    {   
        seekToMousePos(event->pos());
        this->update();
        event->accept();
1280
        emit used();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
    }
}

void DelegateMediaControl::seekToMousePos(QPointF mousePos)
{
    qreal minX, length;
    qreal frameWidth = rect().height() / 2;

    minX = frameWidth;
    length = mSeecArea.width() - lcdTimer->rect().width();

    qreal mouseX = mousePos.x();
    if (mouseX >= (mSeecArea.width() - mSeecArea.height()/2))
        mouseX = mSeecArea.width() - mSeecArea.height()/2;

    if (mTotalTimeInMs > 0 && length > 0 && mDelegate
        && mDelegate->mediaObject() && mDelegate->mediaObject()->isSeekable())
    {
        qint64 tickPos = (mTotalTimeInMs/length)* (mouseX - minX);
        mDelegate->mediaObject()->seek(tickPos);

        //OSX is a bit lazy
        updateTicker(tickPos);
    }
1305
    emit used();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
1306 1307 1308 1309 1310 1311 1312 1313
}

void DelegateMediaControl::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    mDisplayCurrentTime = false;
    this->update();
    event->accept();
}