UBSelectionFrame.cpp 14.2 KB
Newer Older
Ilia Ryabokon's avatar
Ilia Ryabokon committed
1 2 3 4 5 6 7 8
#include "UBSelectionFrame.h"

#include <QtGui>

#include "domain/UBItem.h"
#include "board/UBBoardController.h"
#include "core/UBSettings.h"
#include "core/UBApplication.h"
9 10 11
#include "gui/UBResources.h"
#include "core/UBApplication.h"
#include "domain/UBGraphicsScene.h"
Ilia Ryabokon's avatar
Ilia Ryabokon committed
12
#include "board/UBBoardView.h"
Ilia Ryabokon's avatar
Ilia Ryabokon committed
13 14 15 16

UBSelectionFrame::UBSelectionFrame()
    : mThickness(UBSettings::settings()->objectFrameWidth)
    , mAntiscaleRatio(1.0)
17
    , mRotationAngle(0)
18 19 20 21
    , mDeleteButton(0)
    , mDuplicateButton(0)
    , mZOrderUpButton(0)
    , mZOrderDownButton(0)
22
    , mRotateButton(0)
Ilia Ryabokon's avatar
Ilia Ryabokon committed
23 24 25 26
{
    setLocalBrush(QBrush(UBSettings::paletteColor));
    setPen(Qt::NoPen);
    setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
27
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::SelectionFrame)); //Necessary to set if we want z value to be assigned correctly
28 29
    setFlags(QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsSelectable | ItemIsMovable);

Ilia Ryabokon's avatar
Ilia Ryabokon committed
30
    connect(UBApplication::boardController, SIGNAL(zoomChanged(qreal)), this, SLOT(onZoomChanged(qreal)));
Ilia Ryabokon's avatar
Ilia Ryabokon committed
31 32

    onZoomChanged(UBApplication::boardController->currentZoom());
Ilia Ryabokon's avatar
Ilia Ryabokon committed
33 34 35 36 37 38 39 40 41
}

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

    QPainterPath path;
    QRectF shRect = option->rect;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
42
    path.addRoundedRect(shRect, adjThickness() / 2, adjThickness() / 2);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
43 44 45

    if (rect().width() > 1 && rect().height() > 1) {
        QPainterPath extruded;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
46
        extruded.addRect(shRect.adjusted(adjThickness(), adjThickness(), (adjThickness() * -1), (adjThickness() * -1)));
Ilia Ryabokon's avatar
Ilia Ryabokon committed
47 48 49 50 51 52 53 54
        path = path.subtracted(extruded);
    }

    painter->fillPath(path, mLocalBrush);
}

QRectF UBSelectionFrame::boundingRect() const
{
Ilia Ryabokon's avatar
Ilia Ryabokon committed
55
    return rect().adjusted(-adjThickness(), -adjThickness(), adjThickness(), adjThickness());
Ilia Ryabokon's avatar
Ilia Ryabokon committed
56 57 58 59 60 61
}

QPainterPath UBSelectionFrame::shape() const
{
    QPainterPath resShape;
    QRectF ownRect = rect();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
62 63
    QRectF shRect = ownRect.adjusted(-adjThickness(), -adjThickness(), adjThickness(), adjThickness());
    resShape.addRoundedRect(shRect, adjThickness() / 2, adjThickness() / 2);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
64 65 66 67 68 69 70 71 72 73 74 75

    if (rect().width() > 1 && rect().height() > 1) {
        QPainterPath extruded;
        extruded.addRect(ownRect);
        resShape = resShape.subtracted(extruded);
    }

    return resShape;
}

void UBSelectionFrame::setEnclosedItems(const QList<QGraphicsItem*> pGraphicsItems)
{
76 77
    mButtons.clear();
    mButtons.append(mDeleteButton);
78
    mRotationAngle = 0;
79

80
    QRegion resultRegion;
81
    UBGraphicsFlags resultFlags;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
82 83 84 85 86
    mEnclosedtems.clear();
    foreach (QGraphicsItem *nextItem, pGraphicsItems) {
        UBGraphicsItemDelegate *nextDelegate = UBGraphicsItem::Delegate(nextItem);
        if (nextDelegate) {
            mEnclosedtems.append(nextDelegate);
87
            resultRegion |= nextItem->boundingRegion(nextItem->sceneTransform());
88
            resultFlags |= nextDelegate->ubflags();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
89 90 91
        }
    }

92
    QRectF resultRect = resultRegion.boundingRect();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
93 94
    setRect(resultRect);

95
    mButtons = buttonsForFlags(resultFlags);
96 97
    placeButtons();

Ilia Ryabokon's avatar
Ilia Ryabokon committed
98 99 100 101 102 103 104
    if (resultRect.isEmpty()) {
        hide();
    }
}

void UBSelectionFrame::updateRect()
{
105
    QRegion resultRegion;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
106
    foreach (UBGraphicsItemDelegate *curDelegateItem, mEnclosedtems) {
107
        resultRegion |= curDelegateItem->delegated()->boundingRegion(curDelegateItem->delegated()->sceneTransform());
Ilia Ryabokon's avatar
Ilia Ryabokon committed
108 109
    }

110
    QRectF result = resultRegion.boundingRect();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
111 112
    setRect(result);

113 114
    placeButtons();

Ilia Ryabokon's avatar
Ilia Ryabokon committed
115 116 117 118 119 120 121 122 123 124
    if (result.isEmpty()) {
        setVisible(false);
    }
}

void UBSelectionFrame::updateScale()
{
    setScale(-UBApplication::boardController->currentZoom());
}

125 126 127 128
void UBSelectionFrame::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    mPressedPos = mLastMovedPos = event->pos();
    mLastTranslateOffset = QPointF();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
129
    mRotationAngle = 0;
130

131 132 133 134 135
    if (scene()->itemAt(event->scenePos()) == mRotateButton) {
        mOperationMode = om_rotating;
    } else {
        mOperationMode = om_moving;
    }
136 137 138 139 140
}

void UBSelectionFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF dp = event->pos() - mPressedPos;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
141
    QPointF rotCenter = mapToScene(rect().center());
142 143 144

    foreach (UBGraphicsItemDelegate *curDelegate, mEnclosedtems) {

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
        switch (static_cast<int>(mOperationMode)) {
        case om_moving : {
            QGraphicsItem *item = curDelegate->delegated();
            QTransform ownTransform = item->transform();
            QTransform dTransform(
                        ownTransform.m11()
                        , ownTransform.m12()
                        , ownTransform.m13()

                        , ownTransform.m21()
                        , ownTransform.m22()
                        , ownTransform.m23()

                        , ownTransform.m31() + (dp - mLastTranslateOffset).x()
                        , ownTransform.m32() + (dp - mLastTranslateOffset).y()
                        , ownTransform.m33()
                        );

            item->setTransform(dTransform);
        } break;

        case om_rotating : {
            QLineF startLine(sceneBoundingRect().center(), event->lastScenePos());
            QLineF currentLine(sceneBoundingRect().center(), event->scenePos());
            qreal dAngle = startLine.angleTo(currentLine);

            QGraphicsItem *item = curDelegate->delegated();
            QTransform ownTransform = item->transform();

Ilia Ryabokon's avatar
Ilia Ryabokon committed
174 175 176 177 178

            QPointF nextRotCenter = item->mapFromScene(rotCenter);

            qreal cntrX = nextRotCenter.x();
            qreal cntrY = nextRotCenter.y();
179

180 181
            ownTransform.translate(cntrX, cntrY);
            mRotationAngle -= dAngle;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
182
            ownTransform.rotate(-dAngle);
183
            ownTransform.translate(-cntrX, -cntrY);
184

Ilia Ryabokon's avatar
Ilia Ryabokon committed
185 186 187 188
            item->update();
            item->setTransform(ownTransform, false);

            qDebug() << "curAngle" << mRotationAngle;
189 190 191
        } break;

        }
192 193 194 195 196 197 198 199 200 201

    }

    updateRect();
    mLastMovedPos = event->pos();
    mLastTranslateOffset = dp;
}

void UBSelectionFrame::mouseReleaseEvent(QGraphicsSceneMouseEvent */*event*/)
{
202
    mPressedPos = mLastMovedPos = mLastTranslateOffset = QPointF();
203 204

    if (mOperationMode == om_moving || mOperationMode == om_rotating) {
205
        UBApplication::undoStack->beginMacro(UBSettings::undoCommandTransactionName);
206 207 208 209 210 211 212
        foreach (UBGraphicsItemDelegate *d, mEnclosedtems) {
            d->commitUndoStep();
        }
        UBApplication::undoStack->endMacro();
    }
    mOperationMode = om_idle;

213 214
}

Ilia Ryabokon's avatar
Ilia Ryabokon committed
215 216
void UBSelectionFrame::onZoomChanged(qreal pZoom)
{
Ilia Ryabokon's avatar
Ilia Ryabokon committed
217 218 219 220 221
    qDebug() << "Pzoom" << pZoom;
    qDebug() << "Board current zoom" << UBApplication::boardController->currentZoom();
    qDebug() << "UBApplication::boardController->systemScaleFactor()" << UBApplication::boardController->systemScaleFactor();
    mAntiscaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * pZoom);

222 223 224 225
}

void UBSelectionFrame::remove()
{
226
    UBApplication::undoStack->beginMacro(UBSettings::undoCommandTransactionName);
227 228 229
    foreach (UBGraphicsItemDelegate *d, mEnclosedtems) {
        d->remove(true);
    }
230 231 232 233 234 235 236
    UBApplication::undoStack->endMacro();

    updateRect();
}

void UBSelectionFrame::duplicate()
{
237
    UBApplication::undoStack->beginMacro(UBSettings::undoCommandTransactionName);
238 239 240 241
    foreach (UBGraphicsItemDelegate *d, mEnclosedtems) {
        d->duplicate();
    }
    UBApplication::undoStack->endMacro();
242 243

    updateRect();
244
}
Ilia Ryabokon's avatar
Ilia Ryabokon committed
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
void UBSelectionFrame::increaseZlevelUp()
{
    foreach (QGraphicsItem *item, sortedByZ(scene()->selectedItems())) {
        ubscene()->changeZLevelTo(item, UBZLayerController::up);
    }
}

void UBSelectionFrame::increaseZlevelTop()
{
    foreach (QGraphicsItem *item, sortedByZ(scene()->selectedItems())) {
        ubscene()->changeZLevelTo(item, UBZLayerController::top);
    }
}

void UBSelectionFrame::increaseZlevelDown()
{
    foreach (QGraphicsItem *item, sortedByZ(scene()->selectedItems())) {
        ubscene()->changeZLevelTo(item, UBZLayerController::down);
    }
}

void UBSelectionFrame::increaseZlevelBottom()
{
269 270 271 272
    QListIterator<QGraphicsItem*> iter(sortedByZ(scene()->selectedItems()));
    iter.toBack();
    while (iter.hasPrevious()) {
        ubscene()->changeZLevelTo(iter.previous(), UBZLayerController::bottom);
273
    }
274 275 276
//    foreach (QGraphicsItem *item, sortedByZ(scene()->selectedItems())) {
//        ubscene()->changeZLevelTo(item, UBZLayerController::bottom);
//    }
277 278
}

279 280
void UBSelectionFrame::translateItem(QGraphicsItem */*item*/, const QPointF &/*translatePoint*/)
{
Ilia Ryabokon's avatar
Ilia Ryabokon committed
281
}
282

283 284
void UBSelectionFrame::placeButtons()
{
285 286 287 288
    if (!mButtons.count()) {
        return;
    }

289
    QTransform tr;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
290
    tr.scale(mAntiscaleRatio, mAntiscaleRatio);
291
    mDeleteButton->setParentItem(this);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
292
    mDeleteButton->setTransform(tr);
293

Ilia Ryabokon's avatar
Ilia Ryabokon committed
294
    QRectF frRect = boundingRect();
295

Ilia Ryabokon's avatar
Ilia Ryabokon committed
296 297
    qreal topX = frRect.left() - mDeleteButton->renderer()->viewBox().width() * mAntiscaleRatio / 2;
    qreal topY = frRect.top() - mDeleteButton->renderer()->viewBox().height() * mAntiscaleRatio / 2;
298

Ilia Ryabokon's avatar
Ilia Ryabokon committed
299 300
    qreal bottomX = topX;
    qreal bottomY = frRect.bottom() - mDeleteButton->renderer()->viewBox().height() * mAntiscaleRatio / 2;
301

Ilia Ryabokon's avatar
Ilia Ryabokon committed
302
    mDeleteButton->setPos(topX, topY);
303 304
    mDeleteButton->show();

Ilia Ryabokon's avatar
Ilia Ryabokon committed
305 306 307 308 309 310 311 312 313 314 315 316
    int i = 1, j = 0, k = 0;
    while ((i + j + k) < mButtons.size())  {
        DelegateButton* button = mButtons[i + j];

        if (button->getSection() == Qt::TopLeftSection) {
            button->setParentItem(this);
            button->setPos(topX + (i++ * 1.6 * adjThickness()), topY);
            button->setTransform(tr);
        } else if (button->getSection() == Qt::BottomLeftSection) {
            button->setParentItem(this);
            button->setPos(bottomX + (++j * 1.6 * adjThickness()), bottomY);
            button->setTransform(tr);
317 318 319 320
        } else if (button->getSection() == Qt::NoSection) {
            button->setParentItem(this);
            placeExceptionButton(button, tr);
            k++;
Ilia Ryabokon's avatar
Ilia Ryabokon committed
321 322 323 324 325
        } else {
            ++k;
        }
            button->show();
    }
326 327
}

328 329 330 331 332 333 334 335 336 337 338 339
void UBSelectionFrame::placeExceptionButton(DelegateButton *pButton, QTransform pTransform)
{
    QRectF frRect = boundingRect();

    if (pButton == mRotateButton) {
        qreal topX = frRect.right() - (mRotateButton->renderer()->viewBox().width()) * mAntiscaleRatio - 5;
        qreal topY = frRect.top() + 5;
        mRotateButton->setPos(topX, topY);
        mRotateButton->setTransform(pTransform);
    }
}

340 341 342 343 344 345 346 347 348 349 350
void UBSelectionFrame::clearButtons()
{
    foreach (DelegateButton *b, mButtons)
    {
        b->setParentItem(0);
        b->hide();
    }

    mButtons.clear();
}

351 352 353 354 355
inline UBGraphicsScene *UBSelectionFrame::ubscene()
{
    return qobject_cast<UBGraphicsScene*>(scene());
}

Ilia Ryabokon's avatar
Ilia Ryabokon committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
void UBSelectionFrame::setCursorFromAngle(QString angle)
{
    QWidget *controlViewport = UBApplication::boardController->controlView()->viewport();

    QSize cursorSize(45,30);


    QImage mask_img(cursorSize, QImage::Format_Mono);
    mask_img.fill(0xff);
    QPainter mask_ptr(&mask_img);
    mask_ptr.setBrush( QBrush( QColor(0, 0, 0) ) );
    mask_ptr.drawRoundedRect(0,0, cursorSize.width()-1, cursorSize.height()-1, 6, 6);
    QBitmap bmpMask = QBitmap::fromImage(mask_img);


    QPixmap pixCursor(cursorSize);
    pixCursor.fill(QColor(Qt::white));

    QPainter painter(&pixCursor);

    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setBrush(QBrush(Qt::white));
    painter.setPen(QPen(QColor(Qt::black)));
    painter.drawRoundedRect(1,1,cursorSize.width()-2,cursorSize.height()-2,6,6);
    painter.setFont(QFont("Arial", 10));
    painter.drawText(1,1,cursorSize.width(),cursorSize.height(), Qt::AlignCenter, angle.append(QChar(176)));
    painter.end();

    pixCursor.setMask(bmpMask);
    controlViewport->setCursor(pixCursor);
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401
QList<QGraphicsItem*> UBSelectionFrame::sortedByZ(const QList<QGraphicsItem *> &pItems)
{
    //select only items wiht the same z-level as item's one and push it to sortedItems QMultiMap
    QMultiMap<qreal, QGraphicsItem*> sortedItems;
    foreach (QGraphicsItem *tmpItem, pItems) {
        if (tmpItem->type() == Type) {
            continue;
        }
        sortedItems.insert(tmpItem->data(UBGraphicsItemData::ItemOwnZValue).toReal(), tmpItem);
    }

    return sortedItems.values();
}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
QList<DelegateButton*> UBSelectionFrame::buttonsForFlags(UBGraphicsFlags fls) {

    qDebug() << "buttons for flags" << QString::number((int)fls, 2);
    QList<DelegateButton*> result;

    if (!mDeleteButton) {
        mDeleteButton = new DelegateButton(":/images/close.svg", this, 0, Qt::TopLeftSection);
        mButtons << mDeleteButton;
        connect(mDeleteButton, SIGNAL(clicked()), this, SLOT(remove()));
    }
    result << mDeleteButton;

    if (fls | GF_DUPLICATION_ENABLED) {
        if (!mDuplicateButton) {
            mDuplicateButton = new DelegateButton(":/images/duplicate.svg", this, 0, Qt::TopLeftSection);
            connect(mDuplicateButton, SIGNAL(clicked(bool)), this, SLOT(duplicate()));
        }
        result <<  mDuplicateButton;
    }

    if (fls | GF_ZORDER_MANIPULATIONS_ALLOWED) {
        if (!mZOrderUpButton) {
            mZOrderUpButton = new DelegateButton(":/images/z_layer_up.svg", this, 0, Qt::BottomLeftSection);
            mZOrderUpButton->setShowProgressIndicator(true);
Ilia Ryabokon's avatar
Ilia Ryabokon committed
426
            connect(mZOrderUpButton, SIGNAL(clicked()), this, SLOT(increaseZlevelUp()));
427
            connect(mZOrderUpButton, SIGNAL(longClicked()), this, SLOT(increaseZlevelTop()));
428 429 430 431 432
        }

        if (!mZOrderDownButton) {
            mZOrderDownButton = new DelegateButton(":/images/z_layer_down.svg", this, 0, Qt::BottomLeftSection);
            mZOrderDownButton->setShowProgressIndicator(true);
433 434
            connect(mZOrderDownButton, SIGNAL(clicked()), this, SLOT(increaseZlevelDown()));
            connect(mZOrderDownButton, SIGNAL(longClicked()), this, SLOT(increaseZlevelBottom()));
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        }

        result << mZOrderUpButton;
        result << mZOrderDownButton;
    }

    if (fls | GF_REVOLVABLE) {
        if (!mRotateButton) {
            mRotateButton = new DelegateButton(":/images/rotate.svg", this, 0, Qt::NoSection);
            mRotateButton->setCursor(UBResources::resources()->rotateCursor);
            mRotateButton->setShowProgressIndicator(false);
            mRotateButton->setTransparentToMouseEvent(true);
        }
        result << mRotateButton;
    }

    return result;
}

454