diff --git a/resources/OpenBoard.qrc b/resources/OpenBoard.qrc index 3e41418a..480269c1 100644 --- a/resources/OpenBoard.qrc +++ b/resources/OpenBoard.qrc @@ -364,5 +364,7 @@ images/expand-all.png images/asc.png images/desc.png + images/toolPalette/axesTool.png + images/numbersTool.svg diff --git a/resources/images/numbersTool.svg b/resources/images/numbersTool.svg new file mode 100644 index 00000000..862270de --- /dev/null +++ b/resources/images/numbersTool.svg @@ -0,0 +1,63 @@ + + + +image/svg+xml1 + \ No newline at end of file diff --git a/resources/images/toolPalette/axesTool.png b/resources/images/toolPalette/axesTool.png new file mode 100644 index 00000000..05008c8c Binary files /dev/null and b/resources/images/toolPalette/axesTool.png differ diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index 052ea10b..ad209b1b 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -51,6 +51,7 @@ #include "domain/UBItem.h" #include "tools/UBGraphicsRuler.h" +#include "tools/UBGraphicsAxes.h" #include "tools/UBGraphicsCompass.h" #include "tools/UBGraphicsProtractor.h" #include "tools/UBGraphicsCurtainItem.h" @@ -754,6 +755,17 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene(UBDocumentProx mScene->registerTool(ruler); } + } + else if (mXmlReader.name() == "axes") + { + + UBGraphicsAxes *axes = axesFromSvg(); + if (axes) + { + mScene->addItem(axes); + mScene->registerTool(axes); + } + } else if (mXmlReader.name() == "compass") { @@ -1353,6 +1365,14 @@ bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistScene(UBDocumentProxy* proxy, continue; } + // Is the item a axes? + UBGraphicsAxes *axes = qgraphicsitem_cast (item); + if (axes && axes->isVisible()) + { + axesToSvg(axes); + continue; + } + // Is the item a cache? UBGraphicsCache* cache = qgraphicsitem_cast(item); if(cache && cache->isVisible()) @@ -2859,6 +2879,40 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::rulerToSvg(UBGraphicsRuler* item) mXmlWriter.writeEndElement(); } +void UBSvgSubsetAdaptor::UBSvgSubsetWriter::axesToSvg(UBGraphicsAxes *item) +{ + + /** + * + * sample + * + + + */ + + mXmlWriter.writeStartElement(UBSettings::uniboardDocumentNamespaceUri, "axes"); + mXmlWriter.writeAttribute("x", QString("%1").arg(item->pos().x())); + mXmlWriter.writeAttribute("y", QString("%1").arg(item->pos().y())); + mXmlWriter.writeAttribute("left", QString("%1").arg(item->bounds().left())); + mXmlWriter.writeAttribute("top", QString("%1").arg(item->bounds().top())); + mXmlWriter.writeAttribute("width", QString("%1").arg(item->bounds().width())); + mXmlWriter.writeAttribute("height", QString("%1").arg(item->bounds().height())); + mXmlWriter.writeAttribute("numbers", QString("%1").arg(item->showNumbes())); + + QString zs; + zs.setNum(item->zValue(), 'f'); // 'f' keeps precision + mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "z-value", zs); + + UBItem* ubItem = dynamic_cast(item); + + if (ubItem) + { + mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "uuid", UBStringUtils::toCanonicalUuid(ubItem->uuid())); + } + + mXmlWriter.writeEndElement(); +} + UBGraphicsRuler* UBSvgSubsetAdaptor::UBSvgSubsetReader::rulerFromSvg() { @@ -2883,6 +2937,43 @@ UBGraphicsRuler* UBSvgSubsetAdaptor::UBSvgSubsetReader::rulerFromSvg() return ruler; } +UBGraphicsAxes *UBSvgSubsetAdaptor::UBSvgSubsetReader::axesFromSvg() +{ + UBGraphicsAxes* axes = new UBGraphicsAxes(); + + graphicsItemFromSvg(axes); + + axes->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); + + QStringRef svgX = mXmlReader.attributes().value("x"); + QStringRef svgY = mXmlReader.attributes().value("y"); + QStringRef svgLeft = mXmlReader.attributes().value("left"); + QStringRef svgTop = mXmlReader.attributes().value("top"); + QStringRef svgWidth = mXmlReader.attributes().value("width"); + QStringRef svgHeight = mXmlReader.attributes().value("height"); + QStringRef svgNumbers = mXmlReader.attributes().value("numbers"); + + if (!svgX.isNull() && !svgY.isNull()) + { + axes->setPos(svgX.toString().toFloat(), svgY.toString().toFloat()); + } + + if (!svgWidth.isNull() && !svgHeight.isNull() && !svgLeft.isNull() && !svgTop.isNull()) + { + axes->setRect(svgLeft.toString().toFloat(), svgTop.toString().toFloat(), + svgWidth.toString().toFloat(), svgHeight.toString().toFloat()); + } + + if (!svgNumbers.isNull()) + { + axes->setShowNumbers(svgNumbers.toInt()); + } + + axes->setVisible(true); + + return axes; +} + void UBSvgSubsetAdaptor::UBSvgSubsetWriter::compassToSvg(UBGraphicsCompass* item) { diff --git a/src/adaptors/UBSvgSubsetAdaptor.h b/src/adaptors/UBSvgSubsetAdaptor.h index c66c8198..ea263969 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.h +++ b/src/adaptors/UBSvgSubsetAdaptor.h @@ -49,6 +49,7 @@ class UBGraphicsW3CWidgetItem; class UBGraphicsTextItem; class UBGraphicsCurtainItem; class UBGraphicsRuler; +class UBGraphicsAxes; class UBGraphicsCompass; class UBGraphicsProtractor; class UBGraphicsScene; @@ -144,6 +145,8 @@ class UBSvgSubsetAdaptor UBGraphicsRuler* rulerFromSvg(); + UBGraphicsAxes* axesFromSvg(); + UBGraphicsCompass* compassFromSvg(); UBGraphicsProtractor* protractorFromSvg(); @@ -247,6 +250,7 @@ class UBSvgSubsetAdaptor void textItemToSvg(UBGraphicsTextItem *item); void curtainItemToSvg(UBGraphicsCurtainItem *item); void rulerToSvg(UBGraphicsRuler *item); + void axesToSvg(UBGraphicsAxes *item); void compassToSvg(UBGraphicsCompass *item); void protractorToSvg(UBGraphicsProtractor *item); void cacheToSvg(UBGraphicsCache* item); diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 749b73e4..0ee35ae5 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1441,6 +1441,11 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QUrl mActiveScene->addRuler(pPos); UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Selector); } + else if (sourceUrl.toString() == UBToolsManager::manager()->axes.id) + { + mActiveScene->addAxes(pPos); + UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Selector); + } else if (sourceUrl.toString() == UBToolsManager::manager()->protractor.id) { mActiveScene->addProtractor(pPos); diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 78ba7ba2..4accb0f6 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -77,6 +77,7 @@ #include "document/UBDocumentProxy.h" #include "tools/UBGraphicsRuler.h" +#include "tools/UBGraphicsAxes.h" #include "tools/UBGraphicsCurtainItem.h" #include "tools/UBGraphicsCompass.h" #include "tools/UBGraphicsCache.h" @@ -467,6 +468,7 @@ bool UBBoardView::isCppTool(QGraphicsItem *item) { return (item->type() == UBGraphicsItemType::CompassItemType || item->type() == UBGraphicsItemType::RulerItemType + || item->type() == UBGraphicsItemType::AxesItemType || item->type() == UBGraphicsItemType::ProtractorItemType || item->type() == UBGraphicsItemType::TriangleItemType || item->type() == UBGraphicsItemType::CurtainItemType); @@ -537,6 +539,7 @@ Here we determines cases when items should to get mouse press event at pressing { case UBGraphicsProtractor::Type: case UBGraphicsRuler::Type: + case UBGraphicsAxes::Type: case UBGraphicsTriangle::Type: case UBGraphicsCompass::Type: case UBGraphicsCache::Type: diff --git a/src/core/UB.h b/src/core/UB.h index 86f9691a..0811b035 100644 --- a/src/core/UB.h +++ b/src/core/UB.h @@ -172,6 +172,7 @@ struct UBGraphicsItemType ToolWidgetItemType, //65555 GraphicsWidgetItemType, //65556 UserTypesCount, //65557 + AxesItemType, //65558 SelectionFrameType // this line must be the last line in this enum because it is types counter. }; }; diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 44b14418..4e91018e 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -50,6 +50,7 @@ #include "gui/UBResources.h" #include "tools/UBGraphicsRuler.h" +#include "tools/UBGraphicsAxes.h" #include "tools/UBGraphicsProtractor.h" #include "tools/UBGraphicsCompass.h" #include "tools/UBGraphicsTriangle.h" @@ -2191,6 +2192,21 @@ void UBGraphicsScene::addRuler(QPointF center) ruler->setVisible(true); } +void UBGraphicsScene::addAxes(QPointF center) +{ + UBGraphicsAxes* axes = new UBGraphicsAxes(); // mem : owned and destroyed by the scene + mTools << axes; + + axes->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); + + addItem(axes); + + QPointF itemSceneCenter = axes->sceneBoundingRect().center(); + axes->moveBy(center.x() - itemSceneCenter.x(), center.y() - itemSceneCenter.y()); + + axes->setVisible(true); +} + void UBGraphicsScene::addProtractor(QPointF center) { // Protractor diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 4040af7e..3b898685 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -236,6 +236,7 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem } void addRuler(QPointF center); + void addAxes(QPointF center); void addProtractor(QPointF center); void addCompass(QPointF center); void addTriangle(QPointF center); diff --git a/src/tools/UBGraphicsAxes.cpp b/src/tools/UBGraphicsAxes.cpp new file mode 100644 index 00000000..566e4b15 --- /dev/null +++ b/src/tools/UBGraphicsAxes.cpp @@ -0,0 +1,580 @@ +/* + * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM) + * + * Copyright (C) 2013 Open Education Foundation + * + * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour + * l'Education Numérique en Afrique (GIP ENA) + * + * This file is part of OpenBoard. + * + * OpenBoard is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License, + * with a specific linking exception for the OpenSSL project's + * "OpenSSL" library (or with modified versions of it that use the + * same license as the "OpenSSL" library). + * + * OpenBoard 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 OpenBoard. If not, see . + */ + + + + +#include + +#include "tools/UBGraphicsAxes.h" +#include "domain/UBGraphicsScene.h" +#include "frameworks/UBGeometryUtils.h" +#include "core/UBApplication.h" +#include "gui/UBResources.h" +#include "board/UBBoardController.h" // TODO UB 4.x clean that dependency +#include "board/UBDrawingController.h" + +#include "core/memcheck.h" + +const QRect UBGraphicsAxes::sDefaultRect = QRect(-200, -200, 400, 400); + +const QColor UBGraphicsAxes::sLightBackgroundDrawColor = QColor(0x33, 0x33, 0x33, sDrawTransparency); +const QColor UBGraphicsAxes::sDarkBackgroundDrawColor = QColor(0xff, 0xff, 0xff, sDrawTransparency); + + +UBGraphicsAxes::UBGraphicsAxes() + : QGraphicsPolygonItem() + , mResizing(false) +{ + setRect(sDefaultRect); + + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); + + setAcceptHoverEvents(true); + + mCloseSvgItem = new QGraphicsSvgItem(":/images/closeTool.svg", this); + mCloseSvgItem->setVisible(false); + mCloseSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); + + mNumbersSvgItem = new QGraphicsSvgItem(":/images/numbersTool.svg", this); + mNumbersSvgItem->setVisible(false); + mNumbersSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); + + mShowNumbers = true; + + setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::CppTool)); //Necessary to set if we want z value to be assigned correctly + + setFlag(QGraphicsItem::ItemIsSelectable, false); + updateResizeCursor(); + + connect(UBApplication::boardController, &UBBoardController::zoomChanged, [this](qreal){ + // recalculate shape when zoom factor changes + setRect(mBounds); + }); +} + +UBGraphicsAxes::~UBGraphicsAxes() +{ + // NOOP +} + +UBItem* UBGraphicsAxes::deepCopy() const +{ + UBGraphicsAxes* copy = new UBGraphicsAxes(); + + copyItemParameters(copy); + + // TODO UB 4.7 ... complete all members ? + + return copy; +} + +void UBGraphicsAxes::copyItemParameters(UBItem *copy) const +{ + UBGraphicsAxes *cp = dynamic_cast(copy); + if (cp) + { + cp->setPos(this->pos()); + cp->setRect(this->mBounds); + cp->setTransform(this->transform()); + } +} + +void UBGraphicsAxes::setRect(qreal x, qreal y, qreal w, qreal h) +{ + // Save the bounds rect + mBounds.setX(x); mBounds.setY(y); mBounds.setWidth(w); mBounds.setHeight(h); + + mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom()); + qreal range = sItemWidth * mAntiScaleRatio; + QPolygonF polygon; + polygon << QPointF(-range, -range) + << QPointF(-range, mBounds.top()) + << QPointF(range, mBounds.top()) + << QPointF(range, -range) + << QPointF(mBounds.right(), -range) + << QPointF(mBounds.right(), range) + << QPointF(range, range) + << QPointF(range, mBounds.bottom()) + << QPointF(-range, mBounds.bottom()) + << QPointF(-range, range) + << QPointF(mBounds.left(), range) + << QPointF(mBounds.left(), -range); + setPolygon(polygon); +} + +QRectF UBGraphicsAxes::bounds() const +{ + return mBounds; +} + +void UBGraphicsAxes::setShowNumbers(bool showNumbers) +{ + mShowNumbers = showNumbers; +} + +bool UBGraphicsAxes::showNumbes() const +{ + return mShowNumbers; +} + +void UBGraphicsAxes::paint(QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget) +{ + Q_UNUSED(styleOption); + Q_UNUSED(widget); + + mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom()); + QTransform antiScaleTransform; + antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio); + + mCloseSvgItem->setTransform(antiScaleTransform); + mCloseSvgItem->setPos(closeButtonRect().topLeft()); + + mNumbersSvgItem->setTransform(antiScaleTransform); + mNumbersSvgItem->setPos(numbersButtonRect().topLeft()); + + QTransform antiScaleTransform2; + qreal ratio = mAntiScaleRatio > 1.0 ? mAntiScaleRatio : 1.0; + antiScaleTransform2.scale(ratio, 1.0); + + QPen pen(drawColor()); + pen.setWidthF(2); + painter->setPen(pen); + painter->setRenderHint(QPainter::Antialiasing, true); + painter->drawLine(xAxis()); + painter->drawLine(yAxis()); + + // draw arrows at end + QPointF tip = xAxis().p1(); + painter->drawLine(tip.x(), tip.y(), tip.x() + sArrowLength, tip.y() + sArrowWidth); + painter->drawLine(tip.x(), tip.y(), tip.x() + sArrowLength, tip.y() - sArrowWidth); + + tip = xAxis().p2(); + painter->drawLine(tip.x(), tip.y(), tip.x() - sArrowLength, tip.y() + sArrowWidth); + painter->drawLine(tip.x(), tip.y(), tip.x() - sArrowLength, tip.y() - sArrowWidth); + + tip = yAxis().p1(); + painter->drawLine(tip.x(), tip.y(), tip.x() + sArrowWidth, tip.y() - sArrowLength); + painter->drawLine(tip.x(), tip.y(), tip.x() - sArrowWidth, tip.y() - sArrowLength); + + tip = yAxis().p2(); + painter->drawLine(tip.x(), tip.y(), tip.x() + sArrowWidth, tip.y() + sArrowLength); + painter->drawLine(tip.x(), tip.y(), tip.x() - sArrowWidth, tip.y() + sArrowLength); + + pen.setWidthF(1); + painter->setPen(pen); + paintGraduations(painter); +} + + +QVariant UBGraphicsAxes::itemChange(GraphicsItemChange change, const QVariant &value) +{ + if (change == QGraphicsItem::ItemVisibleHasChanged) + { + mCloseSvgItem->setParentItem(this); + mNumbersSvgItem->setParentItem(this); + } + + return QGraphicsPolygonItem::itemChange(change, value); +} + +void UBGraphicsAxes::paintGraduations(QPainter *painter) +{ + painter->save(); + painter->setFont(font()); + QFontMetricsF fontMetrics(painter->font()); + + // Update the width of one "centimeter" to correspond to the width of the background grid (whether it is displayed or not) + sPixelsPerCentimeter = UBApplication::boardController->activeScene()->backgroundGridSize(); + + // When a "centimeter" is too narrow, we only display every 5th number + double numbersWidth = fontMetrics.boundingRect("-00").width(); + bool shouldDisplayAllNumbers = (numbersWidth <= (sPixelsPerCentimeter - 5)); + + // draw numbers on x axis + int fromX = (xAxis().x1() + sMargin) / sPixelsPerCentimeter; + int toX = (xAxis().x2() - sMargin) / sPixelsPerCentimeter; + + for (int centimeters(fromX); centimeters <= toX; centimeters++) + { + bool isImportant = abs(centimeters) == 1 || abs(centimeters) % 5 == 0; + double graduationX = sPixelsPerCentimeter * centimeters; + double graduationHeight = UBGeometryUtils::millimeterGraduationHeight; + + painter->drawLine(QLineF(graduationX, graduationHeight, graduationX, -graduationHeight)); + + if (mShowNumbers && (shouldDisplayAllNumbers || isImportant) && centimeters != 0) + { + QString text = QString("%1").arg(centimeters); + + if (graduationX + fontMetrics.width(text) / 2 < xAxis().x2()) + { + qreal textWidth = fontMetrics.width(text); + qreal textHeight = fontMetrics.tightBoundingRect(text).height(); + painter->drawText( + QRectF(graduationX - textWidth / 2, textHeight - 5, textWidth, textHeight), + Qt::AlignVCenter, text); + } + } + } + + // draw numbers on y axis + int fromY = (-yAxis().y1() + sMargin) / sPixelsPerCentimeter; + int toY = (-yAxis().y2() - sMargin) / sPixelsPerCentimeter; + + for (int centimeters(fromY); centimeters <= toY; centimeters++) + { + bool isImportant = abs(centimeters) == 1 || abs(centimeters) % 5 == 0; + double graduationY = - sPixelsPerCentimeter * centimeters; + double graduationHeight = UBGeometryUtils::millimeterGraduationHeight; + + painter->drawLine(QLineF(graduationHeight, graduationY, - graduationHeight, graduationY)); + + if (mShowNumbers && (shouldDisplayAllNumbers || isImportant) && centimeters != 0) + { + QString text = QString("%1").arg(centimeters); + + qreal textWidth = fontMetrics.width(text); + qreal textHeight = fontMetrics.tightBoundingRect(text).height(); + painter->drawText( + QRectF(- textWidth - 10, graduationY - textHeight / 2, textWidth, textHeight), + Qt::AlignVCenter, text); + } + } + + painter->restore(); +} + +void UBGraphicsAxes::setRect(const QRectF &rect) +{ + setRect(rect.x(), rect.y(), rect.width(), rect.height()); +} + +void UBGraphicsAxes::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (resizeLeftRect().contains(event->pos())) + { + mResizeDirection = Left; + mResizing = true; + event->accept(); + } + else if (resizeRightRect().contains(event->pos())) + { + mResizeDirection = Right; + mResizing = true; + event->accept(); + } + else if (resizeTopRect().contains(event->pos())) + { + mResizeDirection = Top; + mResizing = true; + event->accept(); + } + else if (resizeBottomRect().contains(event->pos())) + { + mResizeDirection = Bottom; + mResizing = true; + event->accept(); + } + else + { + mResizing = false; + QGraphicsItem::mousePressEvent(event); + } + mCloseSvgItem->setVisible(false); + mNumbersSvgItem->setVisible(false); +} + +void UBGraphicsAxes::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if (!mResizing) + { + QGraphicsItem::mouseMoveEvent(event); + } + else + { + QPointF delta = event->pos() - event->lastPos(); + prepareGeometryChange(); + + switch (mResizeDirection) + { + case Left: + delta.setY(0); + if (delta.x() > -mBounds.left() - sMinLength) + delta.setX(-mBounds.left() - sMinLength); + setRect(mBounds.x() + delta.x(), mBounds.y(), mBounds.width() - delta.x(), mBounds.height()); + break; + + case Right: + if (-delta.x() > mBounds.right() - sMinLength) + delta.setX(sMinLength - mBounds.right()); + setRect(mBounds.x(), mBounds.y(), mBounds.width() + delta.x(), mBounds.height()); + break; + + case Top: + delta.setX(0); + if (delta.y() > -mBounds.top() - sMinLength) + delta.setY(-mBounds.top() - sMinLength); + setRect(mBounds.x(), mBounds.y() + delta.y(), mBounds.width(), mBounds.height() - delta.y()); + break; + + case Bottom: + if (-delta.y() > mBounds.bottom() - sMinLength) + delta.setY(sMinLength - mBounds.bottom()); + setRect(mBounds.x(), mBounds.y(), mBounds.width(), mBounds.height() + delta.y()); + break; + } + + event->accept(); + } +} + +void UBGraphicsAxes::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + if (mResizing) + { + mResizing = false; + event->accept(); + } + else if (closeButtonRect().contains(event->pos())) + { + hide(); + event->accept(); + } + else if (numbersButtonRect().contains(event->pos())) + { + mShowNumbers = !mShowNumbers; + update(boundingRect()); + event->accept(); + } + else + { + // snap to grid + if (true) { + QPointF delta = pos(); + qreal gridSize = scene()->backgroundGridSize(); + qreal deltaX = delta.x() - round(delta.x() / gridSize) * gridSize; + qreal deltaY = delta.y() - round(delta.y() / gridSize) * gridSize; + setPos(pos() - QPointF(deltaX, deltaY)); + } + + QGraphicsItem::mouseReleaseEvent(event); + } + + if (scene()) + scene()->setModified(true); +} + +void UBGraphicsAxes::hoverEnterEvent(QGraphicsSceneHoverEvent *event) +{ + UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool (); + + if (currentTool == UBStylusTool::Selector || + currentTool == UBStylusTool::Play) + { + mCloseSvgItem->setParentItem(this); + mNumbersSvgItem->setParentItem(this); + + mShowButtons = true; + mCloseSvgItem->setVisible(mShowButtons); + mNumbersSvgItem->setVisible(mShowButtons); + selectCursor(event); + event->accept(); + update(); + } +} + +void UBGraphicsAxes::hoverMoveEvent(QGraphicsSceneHoverEvent *event) +{ + UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool (); + + if (currentTool == UBStylusTool::Selector || currentTool == UBStylusTool::Play) + { + mCloseSvgItem->setVisible(mShowButtons); + mNumbersSvgItem->setVisible(mShowButtons); + selectCursor(event); + event->accept(); + } +} + +void UBGraphicsAxes::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) +{ + mShowButtons = false; + setCursor(Qt::ArrowCursor); + mCloseSvgItem->setVisible(mShowButtons); + mNumbersSvgItem->setVisible(mShowButtons); + UBDrawingController::drawingController()->mActiveRuler = NULL; + event->accept(); + update(); +} + +void UBGraphicsAxes::updateResizeCursor() +{ + QPixmap pix(":/images/cursors/resize.png"); + QTransform tr; + tr.rotate(-90); + mResizeCursorH = QCursor(pix, pix.width() / 2, pix.height() / 2); + mResizeCursorV = QCursor(pix.transformed(tr, Qt::SmoothTransformation), pix.width() / 2, pix.height() / 2); +} + +QCursor UBGraphicsAxes::resizeCursor() const +{ + switch (mResizeDirection) + { + case Left: + case Right: + return mResizeCursorH; + + default: + break; + } + + return mResizeCursorV; +} + +QCursor UBGraphicsAxes::closeCursor() const +{ + return Qt::ArrowCursor; +} + +QCursor UBGraphicsAxes::moveCursor() const +{ + return Qt::SizeAllCursor; +} + +void UBGraphicsAxes::selectCursor(QGraphicsSceneHoverEvent *event) +{ + if (resizeLeftRect().contains(event->pos())) + { + mResizeDirection = Left; + setCursor(resizeCursor()); + } + else if (resizeRightRect().contains(event->pos())) + { + mResizeDirection = Right; + setCursor(resizeCursor()); + } + else if (resizeTopRect().contains(event->pos())) + { + mResizeDirection = Top; + setCursor(resizeCursor()); + } + else if (resizeBottomRect().contains(event->pos())) + { + mResizeDirection = Bottom; + setCursor(resizeCursor()); + } else if (closeButtonRect().contains(event->pos())) + setCursor(closeCursor()); + else if (numbersButtonRect().contains(event->pos())) { + setCursor(closeCursor()); + } else { + setCursor(moveCursor()); + } +} + +QRectF UBGraphicsAxes::resizeLeftRect() const +{ + qreal range = sItemWidth * mAntiScaleRatio; + return QRectF(xAxis().x1(), xAxis().y1() - range, sArrowLength, 2 * range); +} + +QRectF UBGraphicsAxes::resizeRightRect() const +{ + qreal range = sItemWidth * mAntiScaleRatio; + return QRectF(xAxis().x2() - sArrowLength, xAxis().y2() - range, sArrowLength, 2 * range); +} + +QRectF UBGraphicsAxes::resizeBottomRect() const +{ + qreal range = sItemWidth * mAntiScaleRatio; + return QRectF(yAxis().x1() - range, yAxis().y1() - sArrowLength, 2 * range, sArrowLength); +} + +QRectF UBGraphicsAxes::resizeTopRect() const +{ + qreal range = sItemWidth * mAntiScaleRatio; + return QRectF(yAxis().x2() - range, yAxis().y2(), 2 * range, sArrowLength); +} + +QRectF UBGraphicsAxes::closeButtonRect() const +{ + QPixmap closePixmap(":/images/closeTool.svg"); + + QSizeF closeRectSize( + closePixmap.width() * mAntiScaleRatio, + closePixmap.height() * mAntiScaleRatio); + + QPointF closeRectTopLeft( + -sItemWidth * mAntiScaleRatio, + -sItemWidth * mAntiScaleRatio); + + return QRectF(closeRectTopLeft, closeRectSize); +} + +QRectF UBGraphicsAxes::numbersButtonRect() const +{ + QPixmap numbersPixmap(":/images/numbersTool.svg"); + + QSizeF numbersRectSize( + numbersPixmap.width() * mAntiScaleRatio, + numbersPixmap.height() * mAntiScaleRatio); + + QPointF numbersRectTopLeft( + sItemWidth * mAntiScaleRatio - numbersRectSize.width(), + sItemWidth * mAntiScaleRatio - numbersRectSize.height()); + + return QRectF(numbersRectTopLeft, numbersRectSize); +} + +QLineF UBGraphicsAxes::xAxis() const +{ + return QLineF(mBounds.left(), 0, mBounds.right(), 0); +} + +QLineF UBGraphicsAxes::yAxis() const +{ + return QLineF(0, mBounds.bottom(), 0, mBounds.top()); +} + +UBGraphicsScene* UBGraphicsAxes::scene() const +{ + return static_cast(QGraphicsPolygonItem::scene()); +} + +QColor UBGraphicsAxes::drawColor() const +{ + return scene()->isDarkBackground() ? sDarkBackgroundDrawColor : sLightBackgroundDrawColor; +} + +QFont UBGraphicsAxes::font() const +{ + QFont font("Arial"); + font.setPixelSize(16); + font.setBold(true); + return font; +} diff --git a/src/tools/UBGraphicsAxes.h b/src/tools/UBGraphicsAxes.h new file mode 100644 index 00000000..c478800f --- /dev/null +++ b/src/tools/UBGraphicsAxes.h @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM) + * + * Copyright (C) 2013 Open Education Foundation + * + * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour + * l'Education Numérique en Afrique (GIP ENA) + * + * This file is part of OpenBoard. + * + * OpenBoard is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License, + * with a specific linking exception for the OpenSSL project's + * "OpenSSL" library (or with modified versions of it that use the + * same license as the "OpenSSL" library). + * + * OpenBoard 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 OpenBoard. If not, see . + */ + + + + +#ifndef UBGRAPHICSAXES_H +#define UBGRAPHICSAXES_H + + +#include +#include + +#include "core/UB.h" +#include "domain/UBItem.h" + +class UBGraphicsScene; + +class UBGraphicsAxes : public QObject, public QGraphicsPolygonItem, public UBItem +{ + Q_OBJECT + + public: + UBGraphicsAxes(); + virtual ~UBGraphicsAxes(); + + enum { Type = UBGraphicsItemType::AxesItemType }; + + virtual int type() const + { + return Type; + } + + virtual UBItem* deepCopy() const; + virtual void copyItemParameters(UBItem *copy) const; + + void setRect(qreal x, qreal y, qreal w, qreal h); + QRectF bounds() const; + void setShowNumbers(bool showNumbers); + bool showNumbes() const; + + enum UBGraphicsAxesDirection + { + Left = 0, + Right, + Bottom, + Top + }; + + protected: + + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget); + virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); + void paintGraduations(QPainter *painter); + void setRect(const QRectF &rect); + + // Events + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + + private: + // Helpers + void updateResizeCursor(); + QCursor resizeCursor() const; + QCursor closeCursor() const; + QCursor moveCursor() const; + void selectCursor(QGraphicsSceneHoverEvent *event); + + virtual QRectF resizeLeftRect() const; + virtual QRectF resizeRightRect() const; + virtual QRectF resizeBottomRect() const; + virtual QRectF resizeTopRect() const; + virtual QRectF closeButtonRect() const; + virtual QRectF numbersButtonRect() const; + virtual QLineF xAxis() const; + virtual QLineF yAxis() const; + virtual UBGraphicsScene* scene() const; + + QColor drawColor() const; + QFont font() const; + + QGraphicsSvgItem* mCloseSvgItem; + QGraphicsSvgItem* mNumbersSvgItem; + + bool mResizing; + UBGraphicsAxesDirection mResizeDirection; + bool mShowButtons; + bool mShowNumbers; + + + QCursor mResizeCursorH; + QCursor mResizeCursorV; + + int drawLineDirection; + + // Constants + static const QRect sDefaultRect; + + static const int sMinLength = 50; // 1sm + static const int sMaxLength = 35000; // 700sm + static const int sArrowLength = 12; + static const int sArrowWidth = 5; + static const int sMargin = 5; + static const int sItemWidth = 30; + static const int sDrawTransparency = 255; + static const QColor sLightBackgroundDrawColor; + static const QColor sDarkBackgroundDrawColor; + + qreal mStrokeWidth; + qreal mAntiScaleRatio; + qreal sPixelsPerCentimeter; + QRectF mBounds; +}; + +#endif // UBGRAPHICSAXES_H diff --git a/src/tools/UBToolsManager.cpp b/src/tools/UBToolsManager.cpp index 210f74f0..d041a0b4 100644 --- a/src/tools/UBToolsManager.cpp +++ b/src/tools/UBToolsManager.cpp @@ -68,6 +68,14 @@ UBToolsManager::UBToolsManager(QObject *parent) mDescriptors << ruler; + axes.id = "openboardtool://axes"; + axes.icon = QPixmap(":/images/toolPalette/axesTool.png"); + axes.label = tr("Axes"); + axes.version = "1.0"; + mToolsIcon.insert(axes.id, ":/images/toolPalette/axesTool.png"); + mDescriptors << axes; + + compass.id = "openboardtool://compass"; compass.icon = QPixmap(":/images/toolPalette/compassTool.png"); compass.label = tr("Compass"); diff --git a/src/tools/UBToolsManager.h b/src/tools/UBToolsManager.h index beec45cf..5783080b 100644 --- a/src/tools/UBToolsManager.h +++ b/src/tools/UBToolsManager.h @@ -84,6 +84,7 @@ class UBToolsManager : public QObject } UBToolDescriptor ruler; + UBToolDescriptor axes; UBToolDescriptor protractor; UBToolDescriptor compass; UBToolDescriptor mask; diff --git a/src/tools/tools.pri b/src/tools/tools.pri index 1f2d00af..e43955af 100644 --- a/src/tools/tools.pri +++ b/src/tools/tools.pri @@ -1,4 +1,5 @@ HEADERS += src/tools/UBGraphicsRuler.h \ + src/tools/UBGraphicsAxes.h \ src/tools/UBGraphicsTriangle.h \ src/tools/UBGraphicsProtractor.h \ src/tools/UBGraphicsCompass.h \ @@ -9,6 +10,7 @@ HEADERS += src/tools/UBGraphicsRuler.h \ src/tools/UBGraphicsCache.h SOURCES += src/tools/UBGraphicsRuler.cpp \ + src/tools/UBGraphicsAxes.cpp \ src/tools/UBGraphicsTriangle.cpp \ src/tools/UBGraphicsProtractor.cpp \ src/tools/UBGraphicsCompass.cpp \