add cpp tool to draw a coordinate system

preferencesAboutTextFull
letsfindaway 4 years ago
parent 36a9cdaccd
commit b30b3fd522
  1. 2
      resources/OpenBoard.qrc
  2. 63
      resources/images/numbersTool.svg
  3. BIN
      resources/images/toolPalette/axesTool.png
  4. 91
      src/adaptors/UBSvgSubsetAdaptor.cpp
  5. 4
      src/adaptors/UBSvgSubsetAdaptor.h
  6. 5
      src/board/UBBoardController.cpp
  7. 3
      src/board/UBBoardView.cpp
  8. 1
      src/core/UB.h
  9. 16
      src/domain/UBGraphicsScene.cpp
  10. 1
      src/domain/UBGraphicsScene.h
  11. 580
      src/tools/UBGraphicsAxes.cpp
  12. 142
      src/tools/UBGraphicsAxes.h
  13. 8
      src/tools/UBToolsManager.cpp
  14. 1
      src/tools/UBToolsManager.h
  15. 2
      src/tools/tools.pri

@ -364,5 +364,7 @@
<file>images/expand-all.png</file>
<file>images/asc.png</file>
<file>images/desc.png</file>
<file>images/toolPalette/axesTool.png</file>
<file>images/numbersTool.svg</file>
</qresource>
</RCC>

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
version="1.1"
id="Layer_1"
x="0px"
y="0px"
width="26.703px"
height="22.297px"
viewBox="0 0 26.703 22.297"
enable-background="new 0 0 26.703 22.297"
xml:space="preserve"
sodipodi:docname="numbersTool.svg"
inkscape:version="0.92.2 5c3e80d, 2017-08-06"><metadata
id="metadata13"><rdf:RDF><cc:Work
rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" /><dc:title></dc:title></cc:Work></rdf:RDF></metadata><defs
id="defs11" /><sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1920"
inkscape:window-height="1015"
id="namedview9"
showgrid="false"
inkscape:zoom="11.313708"
inkscape:cx="13.836098"
inkscape:cy="1.5919993"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="g6" /><g
id="g6"><path
stroke-miterlimit="3.8637"
d="m 17.978,0.507 c 4.547,0 8.231,3.686 8.231,8.232 v 4.838 c 0,4.547 -3.685,8.232 -8.231,8.232 H 8.732 c -4.546,0 -8.231,-3.686 -8.231,-8.232 V 8.739 c 0,-4.547 3.686,-8.232 8.231,-8.232 z"
id="path2"
inkscape:connector-curvature="0"
style="clip-rule:evenodd;fill:#333333;fill-rule:evenodd;stroke:#cdcccc;stroke-miterlimit:3.86369991" /><text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:600;font-stretch:normal;font-size:17.90173149px;line-height:1.25;font-family:'Noto Sans';-inkscape-font-specification:'Noto Sans, Semi-Bold';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:start;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#cccccc;fill-opacity:1;stroke:none;stroke-width:0.44756019"
x="8.2361794"
y="17.963972"
id="text854"
transform="scale(1.0088516,0.9912261)"><tspan
sodipodi:role="line"
id="tspan852"
x="8.2361794"
y="17.963972"
style="font-style:normal;font-variant:normal;font-weight:600;font-stretch:normal;font-size:17.90173149px;font-family:'Noto Sans';-inkscape-font-specification:'Noto Sans, Semi-Bold';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:start;writing-mode:lr-tb;text-anchor:start;fill:#cccccc;stroke-width:0.44756019">1</tspan></text>
</g></svg>

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

@ -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<UBGraphicsAxes*> (item);
if (axes && axes->isVisible())
{
axesToSvg(axes);
continue;
}
// Is the item a cache?
UBGraphicsCache* cache = qgraphicsitem_cast<UBGraphicsCache*>(item);
if(cache && cache->isVisible())
@ -2859,6 +2879,40 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::rulerToSvg(UBGraphicsRuler* item)
mXmlWriter.writeEndElement();
}
void UBSvgSubsetAdaptor::UBSvgSubsetWriter::axesToSvg(UBGraphicsAxes *item)
{
/**
*
* sample
*
<ub:axes x="250" y="150" left="65" top="28" width="122" height="67" numbers="1"...>
</ub:ruler>
*/
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<UBItem*>(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)
{

@ -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);

@ -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);

@ -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:

@ -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.
};
};

@ -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

@ -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);

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QPixmap>
#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<UBGraphicsAxes*>(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<UBGraphicsScene*>(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;
}

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef UBGRAPHICSAXES_H
#define UBGRAPHICSAXES_H
#include <QtGui>
#include <QtSvg>
#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

@ -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");

@ -84,6 +84,7 @@ class UBToolsManager : public QObject
}
UBToolDescriptor ruler;
UBToolDescriptor axes;
UBToolDescriptor protractor;
UBToolDescriptor compass;
UBToolDescriptor mask;

@ -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 \

Loading…
Cancel
Save