From da61ebad0cfc84f56667915e29d7f7b8eca6807f Mon Sep 17 00:00:00 2001 From: Ivan Ilin Date: Mon, 9 Apr 2012 19:20:34 +0300 Subject: [PATCH] Permanent Z-layer order changes, click bugs with pictures --- src/adaptors/UBSvgSubsetAdaptor.cpp | 4 - src/board/UBBoardView.cpp | 6 +- src/core/UBSettings.cpp | 1 + src/core/UBSettings.h | 1 + src/domain/UBGraphicsItemDelegate.cpp | 255 ++++++++++++++++++-------- src/domain/UBGraphicsItemDelegate.h | 39 ++-- src/domain/UBGraphicsPixmapItem.cpp | 7 +- src/domain/UBGraphicsPixmapItem.h | 4 - src/domain/UBGraphicsScene.cpp | 211 +++++++++++++++++---- src/domain/UBGraphicsScene.h | 62 ++++--- src/domain/UBGraphicsVideoItem.cpp | 8 +- src/domain/UBGraphicsWidgetItem.cpp | 2 + src/domain/UBItem.cpp | 2 +- src/domain/UBItem.h | 2 +- src/tools/UBGraphicsRuler.h | 2 +- 15 files changed, 427 insertions(+), 179 deletions(-) diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index b39b7d25..33a5a091 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -2559,7 +2559,6 @@ UBGraphicsRuler* UBSvgSubsetAdaptor::UBSvgSubsetReader::rulerFromSvg() graphicsItemFromSvg(ruler); - UBGraphicsItem::assignZValue(ruler, UBGraphicsScene::toolLayerStart + UBGraphicsScene::toolOffsetRuler); ruler->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); QStringRef svgWidth = mXmlReader.attributes().value("width"); @@ -2617,7 +2616,6 @@ UBGraphicsCompass* UBSvgSubsetAdaptor::UBSvgSubsetReader::compassFromSvg() graphicsItemFromSvg(compass); - UBGraphicsItem::assignZValue(compass, UBGraphicsScene::toolLayerStart + UBGraphicsScene::toolOffsetCompass); compass->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); QStringRef svgX = mXmlReader.attributes().value("x"); @@ -2681,7 +2679,6 @@ UBGraphicsProtractor* UBSvgSubsetAdaptor::UBSvgSubsetReader::protractorFromSvg() { UBGraphicsProtractor* protractor = new UBGraphicsProtractor(); - UBGraphicsItem::assignZValue(protractor, UBGraphicsScene::toolLayerStart + UBGraphicsScene::toolOffsetProtractor); protractor->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); graphicsItemFromSvg(protractor); @@ -2751,7 +2748,6 @@ UBGraphicsTriangle* UBSvgSubsetAdaptor::UBSvgSubsetReader::triangleFromSvg() { UBGraphicsTriangle* triangle = new UBGraphicsTriangle(); - UBGraphicsItem::assignZValue(triangle, UBGraphicsScene::toolLayerStart + UBGraphicsScene::toolOffsetTriangle); triangle->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); graphicsItemFromSvg(triangle); diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index e9ffde4d..e4d9045c 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -418,10 +418,10 @@ UBBoardView::mousePressEvent (QMouseEvent *event) movingItem = scene()->itemAt(this->mapToScene(event->posF().toPoint())); - if (!movingItem + if (!movingItem || movingItem->isSelected() || movingItem->type() == UBGraphicsDelegateFrame::Type - || movingItem->type() == DelegateButton::Type + || movingItem->type() == DelegateButton::Type || movingItem->type() == UBGraphicsCompass::Type || movingItem->type() == UBGraphicsPDFItem::Type || movingItem->type() == UBGraphicsPolygonItem::Type @@ -431,7 +431,7 @@ UBBoardView::mousePressEvent (QMouseEvent *event) QGraphicsView::mousePressEvent (event); } - else + else { mLastPressedMousePos = mapToScene(event->pos()); if (suspendedMousePressEvent) diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index afd08aa9..3aa758d3 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -86,6 +86,7 @@ const int UBSettings::defaultWidgetIconWidth = 110; const int UBSettings::defaultVideoWidth = 80; const int UBSettings::thumbnailSpacing = 20; +const int UBSettings::longClickInterval = 2000; const qreal UBSettings::minScreenRatio = 1.33; // 800/600 or 1024/768 diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index 4e8c9666..e32e6210 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -179,6 +179,7 @@ class UBSettings : public QObject static const int defaultSoundWidth; static const int thumbnailSpacing; + static const int longClickInterval; static const qreal minScreenRatio; diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp index 3d233923..c5d7cdbf 100644 --- a/src/domain/UBGraphicsItemDelegate.cpp +++ b/src/domain/UBGraphicsItemDelegate.cpp @@ -47,6 +47,28 @@ class UBGraphicsParaschoolEditorWidgetItem; +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); +} + UBGraphicsItemDelegate::UBGraphicsItemDelegate(QGraphicsItem* pDelegated, QObject * parent, bool respectRatio, bool canRotate) : QObject(parent) , mDelegated(pDelegated) @@ -88,11 +110,13 @@ void UBGraphicsItemDelegate::init() mButtons << mMenuButton; mZOrderUpButton = new DelegateButton(":/images/plus.svg", mDelegated, mFrame, Qt::BottomLeftSection); - connect(mZOrderUpButton, SIGNAL(clicked()), this, SLOT(increaseZLevel())); + connect(mZOrderUpButton, SIGNAL(clicked()), this, SLOT(increaseZLevelUp())); + connect(mZOrderUpButton, SIGNAL(longClicked()), this, SLOT(increaseZlevelTop())); mButtons << mZOrderUpButton; mZOrderDownButton = new DelegateButton(":/images/minus.svg", mDelegated, mFrame, Qt::BottomLeftSection); - connect(mZOrderDownButton, SIGNAL(clicked()), this, SLOT(decreaseZLevel())); + connect(mZOrderDownButton, SIGNAL(clicked()), this, SLOT(increaseZLevelDown())); + connect(mZOrderDownButton, SIGNAL(longClicked()), this, SLOT(increaseZlevelBottom())); mButtons << mZOrderDownButton; buildButtons(); @@ -149,23 +173,22 @@ QVariant UBGraphicsItemDelegate::itemChange(QGraphicsItem::GraphicsItemChange ch return value; } +UBGraphicsScene *UBGraphicsItemDelegate::castUBGraphicsScene() +{ + UBGraphicsScene *castScene = dynamic_cast(delegated()->scene()); + + return castScene; +} bool UBGraphicsItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if(NULL != mMimeData) - { - QDrag* mDrag = new QDrag(event->widget()); - mDrag->setMimeData(mMimeData); - mDrag->start(); - } + mDragStartPosition = event->pos(); startUndoStep(); if (!mDelegated->isSelected()) { mDelegated->setSelected(true); - qDebug() << mDelegated->zValue(); - positionHandles(); return true; } @@ -182,13 +205,27 @@ void UBGraphicsItemDelegate::setMimeData(QMimeData *mimeData) bool UBGraphicsItemDelegate::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + if((NULL != mMimeData) && ((event->pos() - mDragStartPosition).manhattanLength() < QApplication::startDragDistance())) + { + QDrag* mDrag = new QDrag(event->widget()); + mDrag->setMimeData(mMimeData); + if (!mDragPixmap.isNull()) { + mDrag->setPixmap(mDragPixmap); + mDrag->setHotSpot(mDragPixmap.rect().center()); + } + mDrag->exec(); + mDragPixmap = QPixmap(); + + return true; + } + if(isLocked()) { event->accept(); return true; } else - return false; + return true; } bool UBGraphicsItemDelegate::weelEvent(QGraphicsSceneWheelEvent *event) @@ -215,68 +252,34 @@ bool UBGraphicsItemDelegate::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) return true; } - -void UBGraphicsItemDelegate::positionHandles() +void UBGraphicsItemDelegate::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { - if (mDelegated->isSelected()) - { - if (mFrame && !mFrame->scene() && mDelegated->scene()) - { - mDelegated->scene()->addItem(mFrame); - } - - mFrame->setAntiScale(mAntiScaleRatio); - mFrame->positionHandles(); - mFrame->show(); - - QTransform tr; - tr.scale(mAntiScaleRatio, mAntiScaleRatio); - - 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); + Q_UNUSED(event) +// if (!mDelegated->isSelected()) { +// setZOrderButtonsVisible(true); +// } +} - if (!mDeleteButton->scene()) - { - mDeleteButton->setParentItem(mFrame);//update parent for the case the item has been previously removed from scene - if (mDelegated->scene()) - mDelegated->scene()->addItem(mDeleteButton); - } +void UBGraphicsItemDelegate::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) +{ + Q_UNUSED(event) +// if (!mDelegated->isSelected()) { +// setZOrderButtonsVisible(false); +// } +} - mDeleteButton->show(); +void UBGraphicsItemDelegate::positionHandles() +{ + if (mDelegated->isSelected()) { bool shownOnDisplay = mDelegated->data(UBGraphicsItemData::ItemLayerType).toInt() != UBItemLayerType::Control; showHide(shownOnDisplay); - lock(isLocked()); + updateFrame(); + mFrame->show(); + + updateButtons(true); - int i = 1, j = 0, k = 0; - while ((i + j) < mButtons.size()) { - DelegateButton* button = mButtons[i + j]; - - button->setTransform(tr); - if (button->getSection() == Qt::TopLeftSection) { - button->setPos(topX + (i++ * 1.6 * mFrameWidth * mAntiScaleRatio), topY); - } else if (button->getSection() == Qt::BottomLeftSection) { - button->setPos(bottomX + (++j * 1.6 * mFrameWidth * mAntiScaleRatio), bottomY); - } else if (button->getSection() == Qt::NoSection) { - ++k; - } - if (!button->scene()) - { - button->setParentItem(mFrame);//update parent for the case the item has been previously removed from scene - if (mDelegated->scene()) - mDelegated->scene()->addItem(button); - } - button->show(); - button->setZValue(delegated()->zValue()); - } } else { foreach(DelegateButton* button, mButtons) button->hide(); @@ -284,7 +287,30 @@ void UBGraphicsItemDelegate::positionHandles() mFrame->hide(); } } +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); + mZOrderUpButton->setPos(newUpPoint + QPointF(0,0)); + mZOrderDownButton->setPos(newDownPoint + QPointF(0,0)); + + mZOrderUpButton->show(); + mZOrderDownButton->show(); + + } else { + mZOrderUpButton->hide(); + mZOrderDownButton->hide(); + } +} void UBGraphicsItemDelegate::remove(bool canUndo) { @@ -320,18 +346,34 @@ void UBGraphicsItemDelegate::duplicate() UBApplication::boardController->copy(); UBApplication::boardController->paste(); } -void UBGraphicsItemDelegate::increaseZLevel(int delta) -{ - qDebug() << delegated()->scene()->items().count(); - -// UBGraphicsItem::assignZValue(delegated(), ) -// int valueCandidate = delegated()->data(UBGraphicsItemData::ItemOwnZValue).toInt(); -// if (delta < 0) { - -// } else if (delta > 0) { - -// } +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); + } } void UBGraphicsItemDelegate::lock(bool locked) @@ -484,3 +526,64 @@ bool UBGraphicsItemDelegate::isFlippable() { return mFlippable; } + +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); + } + + if (showUpdated) + mDeleteButton->show(); + + int i = 1, j = 0, k = 0; + while ((i + j + k) < mButtons.size()) { + DelegateButton* button = mButtons[i + j]; + button->setParentItem(mFrame); + + button->setTransform(tr); + if (button->getSection() == Qt::TopLeftSection) { + button->setPos(topX + (i++ * 1.6 * mFrameWidth * mAntiScaleRatio), topY); + } else if (button->getSection() == Qt::BottomLeftSection) { + button->setPos(bottomX + (++j * 1.6 * mFrameWidth * mAntiScaleRatio), bottomY); + } else if (button->getSection() == Qt::NoSection) { + ++k; + } + if (!button->scene()) + { + if (mDelegated->scene()) + mDelegated->scene()->addItem(button); + } + if (showUpdated) { + button->show(); + button->setZValue(delegated()->zValue()); + } + } +} diff --git a/src/domain/UBGraphicsItemDelegate.h b/src/domain/UBGraphicsItemDelegate.h index b7388665..8bf0b3ad 100644 --- a/src/domain/UBGraphicsItemDelegate.h +++ b/src/domain/UBGraphicsItemDelegate.h @@ -21,9 +21,11 @@ #include #include "core/UB.h" +#include "core/UBSettings.h" class QGraphicsSceneMouseEvent; class QGraphicsItem; +class UBGraphicsScene; class UBGraphicsProxyWidget; class UBGraphicsDelegateFrame; class UBGraphicsWidgetItem; @@ -63,20 +65,8 @@ class DelegateButton: public QGraphicsSvgItem protected: - virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) - { - // make sure delegate is selected, to avoid control being hidden - mDelegated->setSelected(true); - - event->setAccepted(!mIsTransparentToMouseEvent); - } - - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) - { - emit clicked(); - - event->setAccepted(!mIsTransparentToMouseEvent); - } + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void modified(); @@ -84,12 +74,13 @@ class DelegateButton: public QGraphicsSvgItem QGraphicsItem* mDelegated; - + QTime mPressedTime; bool mIsTransparentToMouseEvent; Qt::WindowFrameSection mButtonAlignmentSection; signals: void clicked (bool checked = false); + void longClicked(); }; @@ -110,8 +101,12 @@ class UBGraphicsItemDelegate : public QObject virtual bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual bool weelEvent(QGraphicsSceneWheelEvent *event); + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); + virtual UBGraphicsScene *castUBGraphicsScene(); void printMessage(const QString &mess) {qDebug() << mess;} @@ -123,6 +118,7 @@ class UBGraphicsItemDelegate : public QObject void setCanDuplicate(bool allow){ mCanDuplicate = allow; } virtual void positionHandles(); + void setZOrderButtonsVisible(bool visible); void startUndoStep(); void commitUndoStep(); @@ -135,6 +131,7 @@ class UBGraphicsItemDelegate : public QObject QMimeData* mimeData(){ return mMimeData; } void setMimeData(QMimeData* mimeData); + void setDragPixmap(const QPixmap &pix) {mDragPixmap = pix;} void setFlippable(bool flippable); bool isFlippable(); @@ -151,8 +148,10 @@ class UBGraphicsItemDelegate : public QObject virtual void lock(bool lock); virtual void duplicate(); - virtual void increaseZLevel() {increaseZLevel(1);} - virtual void decreaseZLevel() {increaseZLevel(-1);} + void increaseZLevelUp(); + void increaseZLevelDown(); + void increaseZlevelTop(); + void increaseZlevelBottom(); protected: virtual void buildButtons() {;} @@ -187,18 +186,22 @@ protected slots: virtual void gotoContentSource(bool checked); private: + void updateFrame(); + void updateButtons(bool showUpdated = false); + - virtual void increaseZLevel(int delta); QPointF mOffset; QTransform mPreviousTransform; QPointF mPreviousPosition; + QPointF mDragStartPosition; qreal mPreviousZValue; QSizeF mPreviousSize; bool mCanRotate; bool mCanDuplicate; bool mRespectRatio; QMimeData* mMimeData; + QPixmap mDragPixmap; /** A boolean saying that this object can be flippable (mirror effect) */ bool mFlippable; diff --git a/src/domain/UBGraphicsPixmapItem.cpp b/src/domain/UBGraphicsPixmapItem.cpp index 07dc4679..9298d775 100644 --- a/src/domain/UBGraphicsPixmapItem.cpp +++ b/src/domain/UBGraphicsPixmapItem.cpp @@ -58,13 +58,18 @@ void UBGraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) QMimeData* pMime = new QMimeData(); pMime->setImageData(pixmap().toImage()); mDelegate->setMimeData(pMime); + int k = pixmap().width() / 100; + QSize newSize(pixmap().width() / k, pixmap().height() / k); + + mDelegate->setDragPixmap(pixmap().scaled(newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); + if (mDelegate->mousePressEvent(event)) { //NOOP } else { - QGraphicsPixmapItem::mousePressEvent(event); +// QGraphicsPixmapItem::mousePressEvent(event); } } diff --git a/src/domain/UBGraphicsPixmapItem.h b/src/domain/UBGraphicsPixmapItem.h index 56608ba1..5cb7d599 100644 --- a/src/domain/UBGraphicsPixmapItem.h +++ b/src/domain/UBGraphicsPixmapItem.h @@ -61,10 +61,6 @@ protected: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); - - -// UBGraphicsItemDelegate* mDelegate; - }; #endif /* UBGRAPHICSPIXMAPITEM_H_ */ diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index bbd4af62..d3a77e0e 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -61,24 +61,11 @@ #include "core/memcheck.h" -qreal UBGraphicsScene::drawingLayerStart = 0.0; -qreal UBGraphicsScene::toolLayerStart = 10000000.0; - -qreal UBGraphicsScene::toolOffsetRuler = 100; -qreal UBGraphicsScene::toolOffsetProtractor = 100; -qreal UBGraphicsScene::toolOffsetTriangle = 100; -qreal UBGraphicsScene::toolOffsetCompass = 100; -qreal UBGraphicsScene::toolOffsetEraser = 200; - -qreal UBGraphicsScene::toolOffsetCurtain = 1000; -qreal UBGraphicsScene::toolOffsetPointer = 1100; - -qreal UBGraphicsScene::toolOffsetCache = 1000;//Didier please define offset you want - qreal UBZLayerController::errorNumber = -20000001.0; +UBZLayerController::UBZLayerController(QGraphicsScene *scene) : + mScene(scene) -UBZLayerController::UBZLayerController() { scopeMap.insert(itemLayerType::NoLayer, ItemLayerTypeData( errorNumber, errorNumber)); scopeMap.insert(itemLayerType::BackgroundItem, ItemLayerTypeData(-10000000.0, -10000000.0 )); @@ -104,17 +91,162 @@ qreal UBZLayerController::generateZLevel(itemLayerType::Enum key) qreal result = scopeMap.value(key).curValue; qreal top = scopeMap.value(key).topLimit; + qreal incrementalStep = scopeMap.value(key).incStep; - result++; + result += incrementalStep; if (result >= top) { - qDebug() << "new values are over for the scope" << key; - result = top - 1; + // If not only one variable presents in the scope, notify that values for scope are over + if (scopeMap.value(key).topLimit != scopeMap.value(key).bottomLimit) { + qDebug() << "new values are over for the scope" << key; + } + result = top - incrementalStep; } scopeMap[key].curValue = result; return result; } +qreal UBZLayerController::generateZLevel(QGraphicsItem *item) +{ + qreal result = errorNumber; + itemLayerType::Enum type = static_cast(item->data(UBGraphicsItemData::itemLayerType).toInt()); + + if (validLayerType(type)) { + result = generateZLevel(type); + } + + return result; +} + +qreal UBZLayerController::changeZLevelTo(QGraphicsItem *item, moveDestination dest) +{ + itemLayerType::Enum curItemLayerType = typeForData(item); + if (curItemLayerType == itemLayerType::NoLayer) { + qDebug() << "item's layer is out of the scope. Can't implement z-layer changing operation"; + return errorNum(); + } + + //select only items wiht the same z-level as item's one and push it to sortedItems QMultiMap + QMultiMap sortedItems; + if (mScene->items().count()) { + foreach (QGraphicsItem *tmpItem, mScene->items()) { + if (typeForData(tmpItem) == curItemLayerType) { + + sortedItems.insert(tmpItem->data(UBGraphicsItemData::ItemOwnZValue).toReal(), tmpItem); + } + } + } + + //If only one item itself - do nothing, return it's z-value + if (sortedItems.count() == 1 && sortedItems.values().first() == item) { + qDebug() << "only one item exists in layer. Have nothing to change"; + return item->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + } + + QMapIteratoriCurElement(sortedItems); + + if (dest == up) { + if (iCurElement.findNext(item)) { + if (iCurElement.hasNext()) { + qreal nextZ = iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + UBGraphicsItem::assignZValue(iCurElement.peekNext().value(), item->data(UBGraphicsItemData::ItemOwnZValue).toReal()); + UBGraphicsItem::assignZValue(item, nextZ); + + iCurElement.next(); + + while (iCurElement.hasNext() && iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal() == nextZ) { + UBGraphicsItem::assignZValue(iCurElement.next().value(), nextZ); + } + + item->scene()->clearSelection(); + item->setSelected(true); + } + } + + } else if (dest == top) { + if (iCurElement.findNext(item)) { + if (iCurElement.hasNext()) { + UBGraphicsItem::assignZValue(item, generateZLevel(item)); + } + } + + } else if (dest == down) { + iCurElement.toBack(); + if (iCurElement.findPrevious(item)) { + if (iCurElement.hasPrevious()) { + qreal nextZ = iCurElement.peekPrevious().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + UBGraphicsItem::assignZValue(iCurElement.peekPrevious().value(), item->data(UBGraphicsItemData::ItemOwnZValue).toReal()); + UBGraphicsItem::assignZValue(item, nextZ); + + while (iCurElement.hasNext() && iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal() == nextZ) { + UBGraphicsItem::assignZValue(iCurElement.next().value(), nextZ); + } + } + } + + } else if (dest == bottom) { + iCurElement.toBack(); + if (iCurElement.findPrevious(item)) { + if (iCurElement.hasPrevious()) { +// qreal oldz = iCurElement.peekPrevious().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + qreal oldz = item->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + iCurElement.toFront(); + qreal nextZ = iCurElement.next().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + + ItemLayerTypeData curItemLayerTypeData = scopeMap.value(curItemLayerType); +// + //if we have some free space between lowest graphics item and layer's bottom bound, + //insert element close to first element in layer + if (nextZ >= curItemLayerTypeData.bottomLimit + curItemLayerTypeData.incStep) { + qreal result = nextZ - curItemLayerTypeData.incStep; + UBGraphicsItem::assignZValue(item, result); + } else { + UBGraphicsItem::assignZValue(item, nextZ); + bool doubleGap = false; //to detect if we can finish rundown since we can insert item to the free space + + while (iCurElement.peekNext().value() != item) { + qreal curZ = iCurElement.value()->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + qreal curNextZ = iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal(); + if (curNextZ - curZ >= 2 * curItemLayerTypeData.incStep) { + UBGraphicsItem::assignZValue(iCurElement.value(), curZ + curItemLayerTypeData.incStep); + doubleGap = true; + break; + } else { + UBGraphicsItem::assignZValue(iCurElement.value(), iCurElement.next().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal()); + } + } + if (!doubleGap) { + + UBGraphicsItem::assignZValue(iCurElement.value(), oldz); + + while (iCurElement.hasNext() && (iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal() == oldz)) { + UBGraphicsItem::assignZValue(iCurElement.next().value(), oldz); + } + } + } + } + } + } + + + //clear selection of the item and then select it again to activate selectionChangeProcessing() + item->scene()->clearSelection(); + item->setSelected(true); + + //Return new z value assigned to item + return item->data(UBGraphicsItemData::ItemOwnZValue).toReal(); +} + +itemLayerType::Enum UBZLayerController::typeForData(QGraphicsItem *item) const +{ + itemLayerType::Enum result = static_cast(item->data(UBGraphicsItemData::itemLayerType).toInt()); + + if (!scopeMap.contains(result)) { + result = itemLayerType::NoLayer; + } + + return result; +} UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent) : UBCoreGraphicsScene(parent) @@ -135,7 +267,7 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent) , enableUndoRedoStack(true) , magniferControlViewWidget(0) , magniferDisplayViewWidget(0) - + , mZLayerController(new UBZLayerController(this)) { #ifdef __ppc__ @@ -165,6 +297,9 @@ UBGraphicsScene::~UBGraphicsScene() if (mCurrentStroke) if (mCurrentStroke->polygons().empty()) delete mCurrentStroke; + + if (mZLayerController) + delete mZLayerController; } void UBGraphicsScene::selectionChangedProcessing() @@ -178,7 +313,7 @@ void UBGraphicsScene::selectionChangedProcessing() QGraphicsItem *nextItem = allItemsList.at(i); if (nextItem->isSelected()) { - nextItem->setZValue(mZLayerController.generateZLevel(itemLayerType::SelectedItem)); + nextItem->setZValue(mZLayerController->generateZLevel(itemLayerType::SelectedItem)); } else { nextItem->setZValue(nextItem->data(UBGraphicsItemData::ItemOwnZValue).toReal()); } @@ -823,7 +958,6 @@ void UBGraphicsScene::leaveEvent(QEvent * event) hideEraser(); } - UBGraphicsScene* UBGraphicsScene::sceneDeepCopy() const { UBGraphicsScene* copy = new UBGraphicsScene(this->document()); @@ -1330,7 +1464,7 @@ void UBGraphicsScene::addItem(QGraphicsItem* item) setModified(true); UBCoreGraphicsScene::addItem(item); - UBGraphicsItem::assignZValue(item, generateZLevel(item)); + UBGraphicsItem::assignZValue(item, mZLayerController->generateZLevel(item)); if (!mTools.contains(item)) ++mItemCount; @@ -1344,7 +1478,7 @@ void UBGraphicsScene::addItems(const QSet& items) foreach(QGraphicsItem* item, items) { UBCoreGraphicsScene::addItem(item); - UBGraphicsItem::assignZValue(item, generateZLevel(item)); + UBGraphicsItem::assignZValue(item, mZLayerController->generateZLevel(item)); } mItemCount += items.size(); @@ -1406,7 +1540,7 @@ QGraphicsItem* UBGraphicsScene::setAsBackgroundObject(QGraphicsItem* item, bool item->setAcceptedMouseButtons(Qt::NoButton); item->setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::FixedBackground); - UBGraphicsItem::assignZValue(item, mZLayerController.generateZLevel(itemLayerType::BackgroundItem)); + UBGraphicsItem::assignZValue(item, mZLayerController->generateZLevel(itemLayerType::BackgroundItem)); if (pAdaptTransformation) { @@ -1779,6 +1913,23 @@ void UBGraphicsScene::setNominalSize(int pWidth, int pHeight) setNominalSize(QSize(pWidth, pHeight)); } +void UBGraphicsScene::setSelectedZLevel(QList itemList) +{ + foreach (QGraphicsItem *item, itemList) { + item->setZValue(mZLayerController->generateZLevel(itemLayerType::SelectedItem)); + } +} +void UBGraphicsScene::setOwnZlevel(QList itemList) +{ + foreach (QGraphicsItem *item, itemList) { + item->setZValue(item->data(UBGraphicsItemData::ItemOwnZValue).toReal()); + } +} + +qreal UBGraphicsScene::changeZLevelTo(QGraphicsItem *item, UBZLayerController::moveDestination dest) +{ + return mZLayerController->changeZLevelTo(item, dest); +} QGraphicsItem* UBGraphicsScene::rootItem(QGraphicsItem* item) const { @@ -1951,18 +2102,6 @@ void UBGraphicsScene::createPointer() addItem(mPointer); } -qreal UBGraphicsScene::generateZLevel(QGraphicsItem *item) -{ - qreal result = UBZLayerController::errorNum(); - itemLayerType::Enum type = static_cast(item->data(UBGraphicsItemData::itemLayerType).toInt()); - - if (mZLayerController.validLayerType(type)) { - result = mZLayerController.generateZLevel(type); - } - - return result; -} - void UBGraphicsScene::setToolCursor(int tool) { if (tool != (int)UBStylusTool::Selector diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 8fa84053..b869063f 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -49,33 +49,47 @@ class UBGraphicsCache; const double PI = 4.0 * atan(1.0); -class UBZLayerController +class UBZLayerController : public QObject { -public: + Q_OBJECT +public: struct ItemLayerTypeData { - ItemLayerTypeData() : bottomLimit(0), topLimit(0), curValue(0) {;} - ItemLayerTypeData(qreal bot, qreal top) : bottomLimit(bot), topLimit(top), curValue(bot) {;} - qreal bottomLimit; - qreal topLimit; - qreal curValue; + ItemLayerTypeData() : bottomLimit(0), topLimit(0), curValue(0), incStep(1) {;} + ItemLayerTypeData(qreal bot, qreal top, qreal increment = 1) : bottomLimit(bot), topLimit(top), curValue(bot), incStep(increment) {;} + qreal bottomLimit; //bottom bound of the layer + qreal topLimit;//top bound of the layer + qreal curValue;//current value of variable + qreal incStep;//incremental step + }; + + enum moveDestination { + up + , down + , top + , bottom }; typedef QMap ScopeMap; - UBZLayerController(); + UBZLayerController(QGraphicsScene *scene); qreal getBottomLimit(itemLayerType::Enum key) const {return scopeMap.value(key).bottomLimit;} qreal getTopLimit(itemLayerType::Enum key) const {return scopeMap.value(key).topLimit;} bool validLayerType(itemLayerType::Enum key) const {return scopeMap.contains(key);} + static qreal errorNum() {return errorNumber;} qreal generateZLevel(itemLayerType::Enum key); + qreal generateZLevel(QGraphicsItem *item); - private: - ScopeMap scopeMap; - static qreal errorNumber; + qreal changeZLevelTo(QGraphicsItem *item, moveDestination dest); + itemLayerType::Enum typeForData(QGraphicsItem *item) const; +private: + ScopeMap scopeMap; + static qreal errorNumber; + QGraphicsScene *mScene; }; class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem @@ -87,7 +101,7 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem // tmp stub for divide addings scene objects from undo mechanism implementation void setURStackEnable(bool set = true) {enableUndoRedoStack = set;} - UBGraphicsScene(UBDocumentProxy *parent); + UBGraphicsScene(UBDocumentProxy *parent); virtual ~UBGraphicsScene(); virtual UBItem* deepCopy() const; @@ -242,6 +256,8 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem void setNominalSize(int pWidth, int pHeight); + qreal changeZLevelTo(QGraphicsItem *item, UBZLayerController::moveDestination dest); + enum RenderingContext { Screen = 0, NonScreen, PdfExport, Podcast @@ -257,20 +273,6 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem return mRenderingContext; } - static qreal backgroundLayerStart; - static qreal objectLayerStart; - static qreal drawingLayerStart; - static qreal toolLayerStart; - - static qreal toolOffsetEraser; - static qreal toolOffsetPointer; - static qreal toolOffsetRuler; - static qreal toolOffsetProtractor; - static qreal toolOffsetCompass; - static qreal toolOffsetCurtain; - static qreal toolOffsetTriangle; - static qreal toolOffsetCache; - QSet tools(){ return mTools;} void registerTool(QGraphicsItem* item) @@ -283,7 +285,9 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem return mPreviousPoint; } - public slots: + void setSelectedZLevel(QList itemList); + void setOwnZlevel(QList itemList); +public slots: void hideEraser(); @@ -336,8 +340,6 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem void createEraiser(); void createPointer(); - qreal generateZLevel(QGraphicsItem *item); - QGraphicsEllipseItem* mEraser; QGraphicsEllipseItem* mPointer; @@ -387,7 +389,7 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem UBMagnifier *magniferControlViewWidget; UBMagnifier *magniferDisplayViewWidget; - UBZLayerController mZLayerController; + UBZLayerController *mZLayerController; }; diff --git a/src/domain/UBGraphicsVideoItem.cpp b/src/domain/UBGraphicsVideoItem.cpp index 887fdb7a..9fa9d345 100644 --- a/src/domain/UBGraphicsVideoItem.cpp +++ b/src/domain/UBGraphicsVideoItem.cpp @@ -109,10 +109,10 @@ void UBGraphicsVideoItem::showOnDisplayChanged(bool shown) void UBGraphicsVideoItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - QDrag* mDrag = new QDrag(event->widget()); - QMimeData* pMime = new QMimeData(); - mDrag->setMimeData(pMime); - mDrag->start(); +// QDrag* mDrag = new QDrag(event->widget()); +// QMimeData* pMime = new QMimeData(); +// mDrag->setMimeData(pMime); +// mDrag->start(); mShouldMove = (event->buttons() & Qt::LeftButton); mMousePressPos = event->scenePos(); diff --git a/src/domain/UBGraphicsWidgetItem.cpp b/src/domain/UBGraphicsWidgetItem.cpp index 899a7714..13487641 100644 --- a/src/domain/UBGraphicsWidgetItem.cpp +++ b/src/domain/UBGraphicsWidgetItem.cpp @@ -82,11 +82,13 @@ void UBGraphicsWidgetItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsWidgetItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { sendJSEnterEvent(); + mDelegate->hoverEnterEvent(event); UBGraphicsProxyWidget::hoverEnterEvent(event); } void UBGraphicsWidgetItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { sendJSLeaveEvent(); + mDelegate->hoverLeaveEvent(event); UBGraphicsProxyWidget::hoverLeaveEvent(event); } diff --git a/src/domain/UBItem.cpp b/src/domain/UBItem.cpp index 3e6f68cf..54c16e73 100644 --- a/src/domain/UBItem.cpp +++ b/src/domain/UBItem.cpp @@ -29,7 +29,7 @@ UBItem::~UBItem() // NOOP } -void UBGraphicsItem::assignZValue(QGraphicsItem *item, int value) +void UBGraphicsItem::assignZValue(QGraphicsItem *item, qreal value) { item->setZValue(value); item->setData(UBGraphicsItemData::ItemOwnZValue, value); diff --git a/src/domain/UBItem.h b/src/domain/UBItem.h index e10cd57a..301395fa 100644 --- a/src/domain/UBItem.h +++ b/src/domain/UBItem.h @@ -101,7 +101,7 @@ protected: public: - static void assignZValue(QGraphicsItem*, int value); + static void assignZValue(QGraphicsItem*, qreal value); virtual UBGraphicsItemDelegate *Delegate() const = 0; virtual void remove() = 0; diff --git a/src/tools/UBGraphicsRuler.h b/src/tools/UBGraphicsRuler.h index fb74dd9d..1c799bb1 100644 --- a/src/tools/UBGraphicsRuler.h +++ b/src/tools/UBGraphicsRuler.h @@ -44,7 +44,7 @@ class UBGraphicsRuler : public UBAbstractDrawRuler, public QGraphicsRectItem, pu virtual void StartLine(const QPointF& position, qreal width); virtual void DrawLine(const QPointF& position, qreal width); - virtual void EndLine(); + virtual void EndLine(); protected: