Triangle graphics tool

preferencesAboutTextFull
Anatoly Mihalchenko 14 years ago
parent 166194ca71
commit 575970e4e2
  1. 50
      src/adaptors/UBSvgSubsetAdaptor.cpp
  2. 3
      src/adaptors/UBSvgSubsetAdaptor.h
  3. 5
      src/core/main.cpp
  4. 10
      src/domain/UBGraphicsScene.cpp
  5. 1
      src/domain/UBGraphicsScene.h
  6. 1
      src/frameworks/UBCoreGraphicsScene.cpp
  7. 126
      src/tools/UBAbstractDrawRuler.cpp
  8. 62
      src/tools/UBAbstractDrawRuler.h
  9. 120
      src/tools/UBGraphicsProtractor.cpp
  10. 30
      src/tools/UBGraphicsProtractor.h
  11. 392
      src/tools/UBGraphicsRuler.cpp
  12. 67
      src/tools/UBGraphicsRuler.h
  13. 224
      src/tools/UBGraphicsTriangle.cpp
  14. 60
      src/tools/UBGraphicsTriangle.h

@ -26,6 +26,7 @@
#include "tools/UBGraphicsCompass.h" #include "tools/UBGraphicsCompass.h"
#include "tools/UBGraphicsProtractor.h" #include "tools/UBGraphicsProtractor.h"
#include "tools/UBGraphicsCurtainItem.h" #include "tools/UBGraphicsCurtainItem.h"
#include "tools/UBGraphicsTriangle.h"
#include "document/UBDocumentProxy.h" #include "document/UBDocumentProxy.h"
@ -644,6 +645,16 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene()
scene->addItem(protractor); scene->addItem(protractor);
scene->registerTool(protractor); scene->registerTool(protractor);
} }
}
else if (mXmlReader.name() == "protractor")
{
UBGraphicsTriangle *triangle = triangleFromSvg();
if (triangle)
{
scene->addItem(triangle);
scene->registerTool(triangle);
}
} }
else if (mXmlReader.name() == "foreignObject") else if (mXmlReader.name() == "foreignObject")
{ {
@ -2556,7 +2567,46 @@ UBGraphicsProtractor* UBSvgSubsetAdaptor::UBSvgSubsetReader::protractorFromSvg()
return protractor; return protractor;
} }
UBGraphicsTriangle* UBSvgSubsetAdaptor::UBSvgSubsetReader::triangleFromSvg()
{
UBGraphicsTriangle* triangle = new UBGraphicsTriangle();
triangle->setZValue(UBGraphicsScene::toolLayerStart + UBGraphicsScene::toolOffsetTriangle);
triangle->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool));
graphicsItemFromSvg(triangle);
//QStringRef angle = mXmlReader.attributes().value(mNamespaceUri, "angle");
//if (!angle.isNull())
//{
// protractor->setAngle(angle.toString().toFloat());
//}
//QStringRef markerAngle = mXmlReader.attributes().value(mNamespaceUri, "marker-angle");
//if (!markerAngle.isNull())
//{
// protractor->setMarkerAngle(markerAngle.toString().toFloat());
//}
QStringRef svgX = mXmlReader.attributes().value("x");
QStringRef svgY = mXmlReader.attributes().value("y");
QStringRef svgWidth = mXmlReader.attributes().value("width");
QStringRef svgHeight = mXmlReader.attributes().value("height");
UBGraphicsTriangle::UBGraphicsTriangleOrientation orientation =
UBGraphicsTriangle::orientationFromStr((mXmlReader.attributes().value("orientation")));
if (!svgX.isNull() && !svgY.isNull() && !svgWidth.isNull() && !svgHeight.isNull())
{
triangle->setRect(svgX.toString().toFloat(), svgY.toString().toFloat()
, svgWidth.toString().toFloat(), svgHeight.toString().toFloat(),
orientation);
}
triangle->setVisible(true);
return triangle;
}
void UBSvgSubsetAdaptor::convertPDFObjectsToImages(UBDocumentProxy* proxy) void UBSvgSubsetAdaptor::convertPDFObjectsToImages(UBDocumentProxy* proxy)
{ {

@ -31,6 +31,7 @@ class UBGraphicsScene;
class UBDocumentProxy; class UBDocumentProxy;
class UBGraphicsStroke; class UBGraphicsStroke;
class UBPersistenceManager; class UBPersistenceManager;
class UBGraphicsTriangle;
class UBSvgSubsetAdaptor class UBSvgSubsetAdaptor
{ {
@ -118,6 +119,8 @@ class UBSvgSubsetAdaptor
UBGraphicsProtractor* protractorFromSvg(); UBGraphicsProtractor* protractorFromSvg();
UBGraphicsTriangle* triangleFromSvg();
void graphicsItemFromSvg(QGraphicsItem* gItem); void graphicsItemFromSvg(QGraphicsItem* gItem);
QXmlStreamReader mXmlReader; QXmlStreamReader mXmlReader;

@ -54,9 +54,14 @@ void ub_message_output(QtMsgType type, const char *msg) {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// Uncomment next section to have memory leaks information
// tracing in VC++ debug mode under Windows
/*
#if defined(_MSC_VER) && defined(_DEBUG) #if defined(_MSC_VER) && defined(_DEBUG)
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif #endif
*/
Q_INIT_RESOURCE(sankore); Q_INIT_RESOURCE(sankore);

@ -56,6 +56,7 @@ qreal UBGraphicsScene::toolLayerStart = 10000000.0;
qreal UBGraphicsScene::toolOffsetRuler = 100; qreal UBGraphicsScene::toolOffsetRuler = 100;
qreal UBGraphicsScene::toolOffsetProtractor = 100; qreal UBGraphicsScene::toolOffsetProtractor = 100;
qreal UBGraphicsScene::toolOffsetTriangle = 100;
qreal UBGraphicsScene::toolOffsetCompass = 100; qreal UBGraphicsScene::toolOffsetCompass = 100;
qreal UBGraphicsScene::toolOffsetEraser = 200; qreal UBGraphicsScene::toolOffsetEraser = 200;
@ -125,7 +126,6 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent)
UBGraphicsScene::~UBGraphicsScene() UBGraphicsScene::~UBGraphicsScene()
{ {
int a = 13;
// NOOP // NOOP
} }
@ -1411,9 +1411,9 @@ void UBGraphicsScene::addProtractor(QPointF center)
void UBGraphicsScene::addTriangle(QPointF center) void UBGraphicsScene::addTriangle(QPointF center)
{ {
// Protractor // Triangle
/*
UBGraphicsTriangle* protractor = new UBGraphicsTriangle(); // mem : owned and destroyed by the scene UBGraphicsTriangle* triangle = new UBGraphicsTriangle(); // mem : owned and destroyed by the scene
mTools << triangle; mTools << triangle;
triangle->setZValue(toolLayerStart + toolOffsetProtractor); triangle->setZValue(toolLayerStart + toolOffsetProtractor);
@ -1425,7 +1425,7 @@ void UBGraphicsScene::addTriangle(QPointF center)
triangle->moveBy(center.x() - itemSceneCenter.x(), center.y() - itemSceneCenter.y()); triangle->moveBy(center.x() - itemSceneCenter.x(), center.y() - itemSceneCenter.y());
triangle->setVisible(true); triangle->setVisible(true);
setModified(true);*/ setModified(true);
} }

@ -235,6 +235,7 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem
static qreal toolOffsetProtractor; static qreal toolOffsetProtractor;
static qreal toolOffsetCompass; static qreal toolOffsetCompass;
static qreal toolOffsetCurtain; static qreal toolOffsetCurtain;
static qreal toolOffsetTriangle;
QSet<QGraphicsItem*> tools(){ return mTools;} QSet<QGraphicsItem*> tools(){ return mTools;}

@ -12,7 +12,6 @@
UBCoreGraphicsScene::UBCoreGraphicsScene(QObject * parent) UBCoreGraphicsScene::UBCoreGraphicsScene(QObject * parent)
: QGraphicsScene ( parent ) : QGraphicsScene ( parent )
{ {
int a = 13;
//NOOP //NOOP
} }

@ -1,11 +1,137 @@
#include "UBAbstractDrawRuler.h" #include "UBAbstractDrawRuler.h"
#include <QtSvg>
#include "core/UB.h"
#include "gui/UBResources.h"
#include "domain/UBGraphicsScene.h"
#include "board/UBDrawingController.h"
#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "core/memcheck.h" #include "core/memcheck.h"
const QColor UBAbstractDrawRuler::sLightBackgroundMiddleFillColor = QColor(0x72, 0x72, 0x72, sFillTransparency);
const QColor UBAbstractDrawRuler::sLightBackgroundEdgeFillColor = QColor(0xc3, 0xc3, 0xc3, sFillTransparency);
const QColor UBAbstractDrawRuler::sLightBackgroundDrawColor = QColor(0x33, 0x33, 0x33, sDrawTransparency);
const QColor UBAbstractDrawRuler::sDarkBackgroundMiddleFillColor = QColor(0xb5, 0xb5, 0xb5, sFillTransparency);
const QColor UBAbstractDrawRuler::sDarkBackgroundEdgeFillColor = QColor(0xdd, 0xdd, 0xdd, sFillTransparency);
const QColor UBAbstractDrawRuler::sDarkBackgroundDrawColor = QColor(0xff, 0xff, 0xff, sDrawTransparency);
UBAbstractDrawRuler::UBAbstractDrawRuler() UBAbstractDrawRuler::UBAbstractDrawRuler()
: mResizing(false)
, mRotating(false)
, mShowButtons(false)
, mAntiScaleRatio(1.0)
{} {}
void UBAbstractDrawRuler::create(QGraphicsItem& item)
{
item.setFlag(QGraphicsItem::ItemIsMovable, true);
item.setFlag(QGraphicsItem::ItemIsSelectable, true);
item.setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
item.setAcceptsHoverEvents(true);
mCloseSvgItem = new QGraphicsSvgItem(":/images/closeTool.svg", &item);
mCloseSvgItem->setVisible(false);
mCloseSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", &item);
mRotateSvgItem->setVisible(false);
mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
updateResizeCursor(item);
}
UBAbstractDrawRuler::~UBAbstractDrawRuler() UBAbstractDrawRuler::~UBAbstractDrawRuler()
{
}
void UBAbstractDrawRuler::updateResizeCursor(QGraphicsItem &item)
{
QPixmap pix(":/images/cursors/resize.png");
QTransform itemTransform = item.sceneTransform();
QRectF itemRect = item.boundingRect();
QPointF topLeft = itemTransform.map(itemRect.topLeft());
QPointF topRight = itemTransform.map(itemRect.topRight());
QLineF topLine(topLeft, topRight);
qreal angle = topLine.angle();
QTransform tr;
tr.rotate(- angle);
QCursor resizeCursor = QCursor(pix.transformed(tr, Qt::SmoothTransformation), pix.width() / 2, pix.height() / 2);
mResizeCursor = resizeCursor;
}
QCursor UBAbstractDrawRuler::moveCursor() const
{
return Qt::SizeAllCursor;
}
QCursor UBAbstractDrawRuler::resizeCursor() const
{
return mResizeCursor;
}
QCursor UBAbstractDrawRuler::rotateCursor() const
{
return UBResources::resources()->rotateCursor;
}
QCursor UBAbstractDrawRuler::closeCursor() const
{
return Qt::ArrowCursor;
}
QCursor UBAbstractDrawRuler::drawRulerLineCursor() const
{
return UBResources::resources()->drawLineRulerCursor;
}
QColor UBAbstractDrawRuler::drawColor() const
{
return scene()->isDarkBackground() ? sDarkBackgroundDrawColor : sLightBackgroundDrawColor;
}
QColor UBAbstractDrawRuler::middleFillColor() const
{
return scene()->isDarkBackground() ? sDarkBackgroundMiddleFillColor : sLightBackgroundMiddleFillColor;
}
QColor UBAbstractDrawRuler::edgeFillColor() const
{
return scene()->isDarkBackground() ? sDarkBackgroundEdgeFillColor : sLightBackgroundEdgeFillColor;
}
QFont UBAbstractDrawRuler::font() const
{
QFont font("Arial");
font.setPixelSize(16);
return font;
}
void UBAbstractDrawRuler::StartLine(const QPointF& position, qreal width)
{}
void UBAbstractDrawRuler::DrawLine(const QPointF& position, qreal width)
{}
void UBAbstractDrawRuler::EndLine()
{} {}
void UBAbstractDrawRuler::paint()
{
mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
QTransform antiScaleTransform;
antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio);
mCloseSvgItem->setTransform(antiScaleTransform);
mCloseSvgItem->setPos(closeButtonRect().topLeft());
mRotateSvgItem->setTransform(antiScaleTransform);
mRotateSvgItem->setPos(rotateButtonRect().topLeft());
}

@ -3,18 +3,72 @@
#include <QtGui> #include <QtGui>
class UBGraphicsScene; class UBGraphicsScene;
class QGraphicsSvgItem;
class UBAbstractDrawRuler : public QObject class UBAbstractDrawRuler : public QObject
{ {
Q_OBJECT; Q_OBJECT
public: public:
UBAbstractDrawRuler(); UBAbstractDrawRuler();
~UBAbstractDrawRuler(); ~UBAbstractDrawRuler();
virtual void StartLine(const QPointF& position, qreal width) = 0; void create(QGraphicsItem& item);
virtual void DrawLine(const QPointF& position, qreal width) = 0;
virtual void EndLine() = 0; virtual void StartLine(const QPointF& position, qreal width);
virtual void DrawLine(const QPointF& position, qreal width);
virtual void EndLine();
protected:
void paint();
virtual UBGraphicsScene* scene() const = 0;
virtual void rotateAroundTopLeftOrigin(qreal angle) = 0;
virtual QPointF topLeftOrigin() const = 0;
virtual QRectF resizeButtonRect() const = 0;
virtual QRectF closeButtonRect() const = 0;
virtual QRectF rotateButtonRect() const = 0;
void updateResizeCursor(QGraphicsItem &item);
bool mResizing;
bool mRotating;
bool mShowButtons;
QGraphicsSvgItem* mCloseSvgItem;
QGraphicsSvgItem* mRotateSvgItem;
QCursor mResizeCursor;
qreal mAntiScaleRatio;
QPointF startDrawPosition;
QCursor moveCursor() const;
QCursor resizeCursor() const;
QCursor rotateCursor() const;
QCursor closeCursor() const;
QCursor drawRulerLineCursor() const;
QColor drawColor() const;
QColor middleFillColor() const;
QColor edgeFillColor() const;
QFont font() const;
static const QColor sLightBackgroundEdgeFillColor;
static const QColor sLightBackgroundMiddleFillColor;
static const QColor sLightBackgroundDrawColor;
static const QColor sDarkBackgroundEdgeFillColor;
static const QColor sDarkBackgroundMiddleFillColor;
static const QColor sDarkBackgroundDrawColor;
static const int sLeftEdgeMargin = 10;
static const int sMinLength = 150;
static const int sDegreeToQtAngleUnit = 16;
static const int sRotationRadius = 15;
static const int sPixelsPerMillimeter = 5;
static const int sFillTransparency = 127;
static const int sDrawTransparency = 192;
static const int sRoundingRadius = sLeftEdgeMargin / 2;
}; };

@ -16,15 +16,7 @@
#include "core/memcheck.h" #include "core/memcheck.h"
const int UBGraphicsProtractor::sFillTransparency = 127;
const int UBGraphicsProtractor::sDrawTransparency = 192;
const QRectF UBGraphicsProtractor::sDefaultRect = QRectF(-175, -175, 350, 350); const QRectF UBGraphicsProtractor::sDefaultRect = QRectF(-175, -175, 350, 350);
const QColor UBGraphicsProtractor::sFillColor = QColor(0x72, 0x72, 0x72, sFillTransparency);
const QColor UBGraphicsProtractor::sFillColorCenter = QColor(0xbe, 0xbe, 0xbe, sFillTransparency);
const QColor UBGraphicsProtractor::sDrawColor = QColor(32, 32, 32, sDrawTransparency);
const QColor UBGraphicsProtractor::sDarkBackgroundFillColor = QColor(0xb5, 0xb5, 0xb5, sFillTransparency);
const QColor UBGraphicsProtractor::sDarkBackgroundFillColorCenter = QColor(0xde, 0xde, 0xde, sFillTransparency);
const QColor UBGraphicsProtractor::sDarkBackgroundDrawColor = QColor(255, 255, 255, sDrawTransparency);
UBGraphicsProtractor::UBGraphicsProtractor() UBGraphicsProtractor::UBGraphicsProtractor()
: QGraphicsEllipseItem(sDefaultRect) : QGraphicsEllipseItem(sDefaultRect)
@ -34,46 +26,32 @@ UBGraphicsProtractor::UBGraphicsProtractor()
, mSpan(180) , mSpan(180)
, mStartAngle(0) , mStartAngle(0)
, mScaleFactor(1) , mScaleFactor(1)
, mCloseSvgItem(0)
, mResetSvgItem(0) , mResetSvgItem(0)
, mResizeSvgItem(0) , mResizeSvgItem(0)
, mRotateSvgItem(0)
, mMarkerSvgItem(0) , mMarkerSvgItem(0)
{ {
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setAcceptsHoverEvents(true); create(*this);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
setStartAngle(0); setStartAngle(0);
setSpanAngle(180 * 16); setSpanAngle(180 * 16);
mCloseSvgItem = new QGraphicsSvgItem(":/images/closeTool.svg", this);
mCloseSvgItem->setVisible(false);
mCloseSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mCloseSvgItem->setPos(closeButtonBounds().topLeft());
mResetSvgItem = new QGraphicsSvgItem(":/images/resetTool.svg", this); mResetSvgItem = new QGraphicsSvgItem(":/images/resetTool.svg", this);
mResetSvgItem->setVisible(false); mResetSvgItem->setVisible(false);
mResetSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mResetSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mResetSvgItem->setPos(resetButtonBounds().topLeft()); mResetSvgItem->setPos(resetButtonRect().topLeft());
mResizeSvgItem = new QGraphicsSvgItem(":/images/resizeTool.svg", this); mResizeSvgItem = new QGraphicsSvgItem(":/images/resizeTool.svg", this);
mResizeSvgItem->setVisible(false); mResizeSvgItem->setVisible(false);
mResizeSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mResizeSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mResizeSvgItem->setPos(resizeButtonBounds().topLeft()); mResizeSvgItem->setPos(resizeButtonRect().topLeft());
mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateProtractor.svg", this);
mRotateSvgItem->setVisible(false);
mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mRotateSvgItem->setPos(rotateButtonBounds().topLeft());
mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this); mMarkerSvgItem = new QGraphicsSvgItem(":/images/angleMarker.svg", this);
mMarkerSvgItem->setVisible(false); mMarkerSvgItem->setVisible(false);
mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool)); mMarkerSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Tool));
mMarkerSvgItem->setPos(markerButtonBounds().topLeft()); mMarkerSvgItem->setPos(markerButtonRect().topLeft());
scale(1.5, 1.5); scale(1.5, 1.5);
} }
@ -120,9 +98,9 @@ QRectF UBGraphicsProtractor::boundingRect() const
qreal centerX = center.x(); qreal centerX = center.x();
qreal centerY = center.y(); qreal centerY = center.y();
QRectF bounds = resizeButtonBounds().adjusted(centerX, centerY, centerX, centerY); QRectF bounds = resizeButtonRect().adjusted(centerX, centerY, centerX, centerY);
bounds = bounds.united(closeButtonBounds().adjusted(centerX, centerY, centerX, centerY)); bounds = bounds.united(closeButtonRect().adjusted(centerX, centerY, centerX, centerY));
bounds = bounds.united(resetButtonBounds().adjusted(centerX, centerY, centerX, centerY)); bounds = bounds.united(resetButtonRect().adjusted(centerX, centerY, centerX, centerY));
QTransform t; QTransform t;
t.translate(centerX, centerY); t.translate(centerX, centerY);
@ -140,21 +118,21 @@ QPainterPath UBGraphicsProtractor::shape() const
{ {
QPainterPath path = QGraphicsEllipseItem::shape(); QPainterPath path = QGraphicsEllipseItem::shape();
QPainterPath buttonPath; QPainterPath buttonPath;
QRectF markerBounds = markerButtonBounds(); QRectF markerRect = markerButtonRect();
QPointF center = rect().center(); QPointF center = rect().center();
qreal centerX = center.x(); qreal centerX = center.x();
qreal centerY = center.y(); qreal centerY = center.y();
buttonPath.addRect(resizeButtonBounds().adjusted(centerX, centerY, centerX, centerY)); buttonPath.addRect(resizeButtonRect().adjusted(centerX, centerY, centerX, centerY));
if(!resizeButtonBounds().contains(markerBounds)) if(!resizeButtonRect().contains(markerRect))
{ {
buttonPath.addRect(markerBounds.adjusted(centerX - markerBounds.left() * 2 - markerBounds.width(), centerY buttonPath.addRect(markerRect.adjusted(centerX - markerRect.left() * 2 - markerRect.width(), centerY
, centerX - markerBounds.left() * 2 - markerBounds.width(), centerY)); , centerX - markerRect.left() * 2 - markerRect.width(), centerY));
buttonPath.addRect(markerBounds.adjusted(centerX, centerY, centerX, centerY)); buttonPath.addRect(markerRect.adjusted(centerX, centerY, centerX, centerY));
} }
buttonPath.addRect(closeButtonBounds().adjusted(centerX, centerY, centerX, centerY)); buttonPath.addRect(closeButtonRect().adjusted(centerX, centerY, centerX, centerY));
buttonPath.addRect(resetButtonBounds().adjusted(centerX, centerY, centerX, centerY)); buttonPath.addRect(resetButtonRect().adjusted(centerX, centerY, centerX, centerY));
QTransform t; QTransform t;
t.translate(centerX, centerY); t.translate(centerX, centerY);
t.rotate(-mStartAngle); t.rotate(-mStartAngle);
@ -323,7 +301,7 @@ qreal UBGraphicsProtractor::antiScale() const
} }
QRectF UBGraphicsProtractor::resetButtonBounds () const QRectF UBGraphicsProtractor::resetButtonRect () const
{ {
qreal antiSc = antiScale(); qreal antiSc = antiScale();
@ -336,7 +314,7 @@ QRectF UBGraphicsProtractor::resetButtonBounds () const
} }
QRectF UBGraphicsProtractor::closeButtonBounds () const QRectF UBGraphicsProtractor::closeButtonRect () const
{ {
qreal antiSc = antiScale(); qreal antiSc = antiScale();
@ -352,7 +330,7 @@ QRectF UBGraphicsProtractor::closeButtonBounds () const
} }
QRectF UBGraphicsProtractor::resizeButtonBounds () const QRectF UBGraphicsProtractor::resizeButtonRect () const
{ {
qreal antiSc = antiScale(); qreal antiSc = antiScale();
@ -442,41 +420,41 @@ void UBGraphicsProtractor::paintButtons(QPainter *painter)
qreal antiSc = antiScale(); qreal antiSc = antiScale();
qreal scale = buttonSizeReference().width() / mCloseSvgItem->boundingRect().width(); qreal scale = buttonSizeReference().width() / mCloseSvgItem->boundingRect().width();
mCloseSvgItem->setPos(closeButtonBounds().topLeft() + rect().center()); mCloseSvgItem->setPos(closeButtonRect().topLeft() + rect().center());
mCloseSvgItem->resetTransform(); mCloseSvgItem->resetTransform();
mCloseSvgItem->translate(-closeButtonBounds().left(),-closeButtonBounds().top()); mCloseSvgItem->translate(-closeButtonRect().left(),-closeButtonRect().top());
mCloseSvgItem->rotate(-mStartAngle); mCloseSvgItem->rotate(-mStartAngle);
mCloseSvgItem->translate(closeButtonBounds().left(), closeButtonBounds().top()); mCloseSvgItem->translate(closeButtonRect().left(), closeButtonRect().top());
mCloseSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item... mCloseSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item...
mResetSvgItem->setPos(resetButtonBounds().topLeft() + rect().center()); mResetSvgItem->setPos(resetButtonRect().topLeft() + rect().center());
mResetSvgItem->resetTransform(); mResetSvgItem->resetTransform();
mResetSvgItem->translate(-resetButtonBounds().left(), -resetButtonBounds().top()); mResetSvgItem->translate(-resetButtonRect().left(), -resetButtonRect().top());
mResetSvgItem->rotate(-mStartAngle); mResetSvgItem->rotate(-mStartAngle);
mResetSvgItem->translate(resetButtonBounds().left(), resetButtonBounds().top()); mResetSvgItem->translate(resetButtonRect().left(), resetButtonRect().top());
mResetSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item... mResetSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item...
mResizeSvgItem->setPos(resizeButtonBounds().topLeft() + rect().center()); mResizeSvgItem->setPos(resizeButtonRect().topLeft() + rect().center());
mResizeSvgItem->resetTransform(); mResizeSvgItem->resetTransform();
mResizeSvgItem->translate(-resizeButtonBounds().left(), -resizeButtonBounds().top()); mResizeSvgItem->translate(-resizeButtonRect().left(), -resizeButtonRect().top());
mResizeSvgItem->rotate(-mStartAngle); mResizeSvgItem->rotate(-mStartAngle);
mResizeSvgItem->translate(resizeButtonBounds().left(), resizeButtonBounds().top()); mResizeSvgItem->translate(resizeButtonRect().left(), resizeButtonRect().top());
mResizeSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item... mResizeSvgItem->scale(scale * antiSc, scale * antiSc);//this do not impact the bounding box of thr svg item...
mRotateSvgItem->setPos(rotateButtonBounds().topLeft() + rect().center()); mRotateSvgItem->setPos(rotateButtonRect().topLeft() + rect().center());
mRotateSvgItem->resetTransform(); mRotateSvgItem->resetTransform();
mRotateSvgItem->translate(-rotateButtonBounds().left(), -rotateButtonBounds().top()); mRotateSvgItem->translate(-rotateButtonRect().left(), -rotateButtonRect().top());
mRotateSvgItem->rotate(-mStartAngle); mRotateSvgItem->rotate(-mStartAngle);
mRotateSvgItem->translate(rotateButtonBounds().left(), rotateButtonBounds().top()); mRotateSvgItem->translate(rotateButtonRect().left(), rotateButtonRect().top());
mRotateSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item... mRotateSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item...
} }
qreal scale = markerSizeReference().width()/mMarkerSvgItem->boundingRect().width(); qreal scale = markerSizeReference().width()/mMarkerSvgItem->boundingRect().width();
mMarkerSvgItem->setPos(markerButtonBounds().topLeft() + rect().center()); mMarkerSvgItem->setPos(markerButtonRect().topLeft() + rect().center());
mMarkerSvgItem->resetTransform(); mMarkerSvgItem->resetTransform();
mMarkerSvgItem->translate(-markerButtonBounds().left(), -markerButtonBounds().top()); mMarkerSvgItem->translate(-markerButtonRect().left(), -markerButtonRect().top());
mMarkerSvgItem->rotate(- mStartAngle - mCurrentAngle); mMarkerSvgItem->rotate(- mStartAngle - mCurrentAngle);
mMarkerSvgItem->translate(markerButtonBounds().left(), markerButtonBounds().top()); mMarkerSvgItem->translate(markerButtonRect().left(), markerButtonRect().top());
mMarkerSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item... mMarkerSvgItem->scale(scale, scale);//this do not impact the bounding box of thr svg item...
mCloseSvgItem->setVisible(mShowButtons); mCloseSvgItem->setVisible(mShowButtons);
@ -550,15 +528,15 @@ UBGraphicsProtractor::Tool UBGraphicsProtractor::toolFromPos(QPointF pos)
t.rotate(mCurrentAngle); t.rotate(mCurrentAngle);
QPointF p2 = t.map(pos); QPointF p2 = t.map(pos);
if(resizeButtonBounds().contains(p1)) if(resizeButtonRect().contains(p1))
return Resize; return Resize;
else if(closeButtonBounds().contains(p1)) else if(closeButtonRect().contains(p1))
return Close; return Close;
else if(resetButtonBounds().contains(p1)) else if(resetButtonRect().contains(p1))
return Reset; return Reset;
else if(rotateButtonBounds().contains(p1)) else if(rotateButtonRect().contains(p1))
return Rotate; return Rotate;
else if(markerButtonBounds().contains(p2)) else if(markerButtonRect().contains(p2))
return MoveMarker; return MoveMarker;
else if(line.length() <= radius()) else if(line.length() <= radius())
return Move; return Move;
@ -572,15 +550,10 @@ UBGraphicsScene* UBGraphicsProtractor::scene() const
return static_cast<UBGraphicsScene*>(QGraphicsEllipseItem::scene()); return static_cast<UBGraphicsScene*>(QGraphicsEllipseItem::scene());
} }
QColor UBGraphicsProtractor::drawColor() const
{
return scene()->isDarkBackground() ? sDarkBackgroundDrawColor : sDrawColor;
}
QBrush UBGraphicsProtractor::fillBrush() const QBrush UBGraphicsProtractor::fillBrush() const
{ {
QColor fillColor = scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor; QColor fillColor = edgeFillColor();// scene()->isDarkBackground() ? sDarkBackgroundFillColor : sFillColor;
QColor fillColorCenter = scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter; QColor fillColorCenter = middleFillColor();//scene()->isDarkBackground() ? sDarkBackgroundFillColorCenter : sFillColorCenter;
QColor transparentWhite = Qt::white; QColor transparentWhite = Qt::white;
transparentWhite.setAlpha(scene()->isDarkBackground() ? sDrawTransparency : sFillTransparency); transparentWhite.setAlpha(scene()->isDarkBackground() ? sDrawTransparency : sFillTransparency);
QRadialGradient radialGradient(rect().center(), radius(), rect().center()); QRadialGradient radialGradient(rect().center(), radius(), rect().center());
@ -608,3 +581,12 @@ UBItem* UBGraphicsProtractor::deepCopy() const
return copy; return copy;
} }
void UBGraphicsProtractor::rotateAroundTopLeftOrigin(qreal angle)
{}
QPointF UBGraphicsProtractor::topLeftOrigin() const
{
return QPointF(rect().x(), rect().y());
}

@ -13,12 +13,12 @@
#include <QtSvg> #include <QtSvg>
#include "core/UB.h" #include "core/UB.h"
#include "tools/UBAbstractDrawRuler.h"
#include "domain/UBItem.h" #include "domain/UBItem.h"
class UBGraphicsScene; class UBGraphicsScene;
class UBGraphicsProtractor : public QObject, public QGraphicsEllipseItem, public UBItem class UBGraphicsProtractor : public UBAbstractDrawRuler, public QGraphicsEllipseItem, public UBItem
{ {
Q_OBJECT; Q_OBJECT;
@ -66,16 +66,15 @@ class UBGraphicsProtractor : public QObject, public QGraphicsEllipseItem, public
Tool toolFromPos (QPointF pos); Tool toolFromPos (QPointF pos);
qreal antiScale () const; qreal antiScale () const;
UBGraphicsScene* scene() const; UBGraphicsScene* scene() const;
QColor drawColor() const;
QBrush fillBrush() const; QBrush fillBrush() const;
QSizeF buttonSizeReference () const{return QSizeF(radius() / 10, mCloseSvgItem->boundingRect().height() * radius()/(10 * mCloseSvgItem->boundingRect().width()));} QSizeF buttonSizeReference () const{return QSizeF(radius() / 10, mCloseSvgItem->boundingRect().height() * radius()/(10 * mCloseSvgItem->boundingRect().width()));}
QSizeF markerSizeReference () const{return QSizeF(radius() / 10, mMarkerSvgItem->boundingRect().height() * radius()/(10 * mMarkerSvgItem->boundingRect().width()));} QSizeF markerSizeReference () const{return QSizeF(radius() / 10, mMarkerSvgItem->boundingRect().height() * radius()/(10 * mMarkerSvgItem->boundingRect().width()));}
QRectF resetButtonBounds () const; QRectF resetButtonRect () const;
QRectF closeButtonBounds () const; QRectF closeButtonRect () const;
QRectF resizeButtonBounds () const; QRectF resizeButtonRect () const;
QRectF rotateButtonBounds () const{return QRectF(buttonSizeReference().width() * 5.5, -buttonSizeReference().width() * 5, buttonSizeReference().width(), buttonSizeReference().width());} QRectF rotateButtonRect () const{return QRectF(buttonSizeReference().width() * 5.5, -buttonSizeReference().width() * 5, buttonSizeReference().width(), buttonSizeReference().width());}
QRectF markerButtonBounds () const{return QRectF(radius() + 3, -markerSizeReference().height() / 2 , markerSizeReference().width(), markerSizeReference().height());} QRectF markerButtonRect () const{return QRectF(radius() + 3, -markerSizeReference().height() / 2 , markerSizeReference().width(), markerSizeReference().height());}
inline qreal radius () const{return rect().height() / 2 - 20;} inline qreal radius () const{return rect().height() / 2 - 20;}
// Members // Members
@ -87,21 +86,14 @@ class UBGraphicsProtractor : public QObject, public QGraphicsEllipseItem, public
qreal mStartAngle; qreal mStartAngle;
qreal mScaleFactor; qreal mScaleFactor;
QGraphicsSvgItem* mCloseSvgItem;
QGraphicsSvgItem* mResetSvgItem; QGraphicsSvgItem* mResetSvgItem;
QGraphicsSvgItem* mResizeSvgItem; QGraphicsSvgItem* mResizeSvgItem;
QGraphicsSvgItem* mRotateSvgItem;
QGraphicsSvgItem* mMarkerSvgItem; QGraphicsSvgItem* mMarkerSvgItem;
static const int sFillTransparency; static const QRectF sDefaultRect;
static const int sDrawTransparency;
static const QRectF sDefaultRect; virtual void rotateAroundTopLeftOrigin(qreal angle);
static const QColor sFillColor; virtual QPointF topLeftOrigin() const;
static const QColor sFillColorCenter;
static const QColor sDrawColor;
static const QColor sDarkBackgroundFillColor;
static const QColor sDarkBackgroundFillColorCenter;
static const QColor sDarkBackgroundDrawColor;
}; };
#endif /* UBGRAPHICSPROTRACTOR_H_ */ #endif /* UBGRAPHICSPROTRACTOR_H_ */

@ -18,45 +18,19 @@
#include "core/memcheck.h" #include "core/memcheck.h"
const QRect UBGraphicsRuler::sDefaultRect = QRect(0, 0, 800, 96); const QRect UBGraphicsRuler::sDefaultRect = QRect(0, 0, 800, 96);
const QColor UBGraphicsRuler::sLightBackgroundMiddleFillColor = QColor(0x72, 0x72, 0x72, sFillTransparency);
const QColor UBGraphicsRuler::sLightBackgroundEdgeFillColor = QColor(0xc3, 0xc3, 0xc3, sFillTransparency);
const QColor UBGraphicsRuler::sLightBackgroundDrawColor = QColor(0x33, 0x33, 0x33, sDrawTransparency);
const QColor UBGraphicsRuler::sDarkBackgroundMiddleFillColor = QColor(0xb5, 0xb5, 0xb5, sFillTransparency);
const QColor UBGraphicsRuler::sDarkBackgroundEdgeFillColor = QColor(0xdd, 0xdd, 0xdd, sFillTransparency);
const QColor UBGraphicsRuler::sDarkBackgroundDrawColor = QColor(0xff, 0xff, 0xff, sDrawTransparency);
UBGraphicsRuler::UBGraphicsRuler() UBGraphicsRuler::UBGraphicsRuler()
: QGraphicsRectItem() : QGraphicsRectItem()
, mResizing(false)
, mRotating(false)
, mShowButtons(false)
, mCloseSvgItem(0)
, mRotateSvgItem(0)
, mResizeSvgItem(0)
, mAntiScaleRatio(1.0)
{ {
setRect(sDefaultRect); setRect(sDefaultRect);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setAcceptsHoverEvents(true);
mCloseSvgItem = new QGraphicsSvgItem(":/images/closeTool.svg", this);
mCloseSvgItem->setVisible(false);
mCloseSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mRotateSvgItem = new QGraphicsSvgItem(":/images/rotateTool.svg", this);
mRotateSvgItem->setVisible(false);
mRotateSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
mResizeSvgItem = new QGraphicsSvgItem(":/images/resizeRuler.svg", this); mResizeSvgItem = new QGraphicsSvgItem(":/images/resizeRuler.svg", this);
mResizeSvgItem->setVisible(false); mResizeSvgItem->setVisible(false);
mResizeSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control)); mResizeSvgItem->setData(UBGraphicsItemData::ItemLayerType, QVariant(UBItemLayerType::Control));
updateResizeCursor();
create(*this);
} }
UBGraphicsRuler::~UBGraphicsRuler() UBGraphicsRuler::~UBGraphicsRuler()
@ -83,22 +57,15 @@ void UBGraphicsRuler::paint(QPainter *painter, const QStyleOptionGraphicsItem *s
Q_UNUSED(styleOption); Q_UNUSED(styleOption);
Q_UNUSED(widget); Q_UNUSED(widget);
mAntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom()); UBAbstractDrawRuler::paint();
QTransform antiScaleTransform;
antiScaleTransform.scale(mAntiScaleRatio, mAntiScaleRatio);
mCloseSvgItem->setTransform(antiScaleTransform);
mCloseSvgItem->setPos(closeButtonRect().topLeft());
mRotateSvgItem->setTransform(antiScaleTransform);
mRotateSvgItem->setPos(rotateButtonRect().topLeft());
QTransform antiScaleTransform2; QTransform antiScaleTransform2;
qreal ratio = mAntiScaleRatio > 1.0 ? mAntiScaleRatio : 1.0; qreal ratio = mAntiScaleRatio > 1.0 ? mAntiScaleRatio : 1.0;
antiScaleTransform2.scale(ratio, 1.0); antiScaleTransform2.scale(ratio, 1.0);
mResizeSvgItem->setTransform(antiScaleTransform2); mResizeSvgItem->setTransform(antiScaleTransform2);
mResizeSvgItem->setPos(resizeButtonRect().topLeft()); mResizeSvgItem->setPos(resizeButtonRect().topLeft());
painter->setPen(drawColor()); painter->setPen(drawColor());
painter->drawRoundedRect(rect(), sRoundingRadius, sRoundingRadius); painter->drawRoundedRect(rect(), sRoundingRadius, sRoundingRadius);
fillBackground(painter); fillBackground(painter);
@ -120,168 +87,6 @@ QVariant UBGraphicsRuler::itemChange(GraphicsItemChange change, const QVariant &
return QGraphicsRectItem::itemChange(change, value); return QGraphicsRectItem::itemChange(change, value);
} }
void UBGraphicsRuler::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->pos().x() > resizeButtonRect().left())
{
mResizing = true;
event->accept();
}
else if (rotateButtonRect().contains(event->pos()))
{
mRotating = true;
event->accept();
}
else
{
mResizeSvgItem->setVisible(false);
mRotateSvgItem->setVisible(false);
QGraphicsRectItem::mousePressEvent(event);
}
mResizeSvgItem->setVisible(mShowButtons && mResizing);
mRotateSvgItem->setVisible(mShowButtons && mRotating);
mCloseSvgItem->setVisible(false);
}
void UBGraphicsRuler::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (!mResizing && !mRotating)
{
QGraphicsRectItem::mouseMoveEvent(event);
}
else
{
if (mResizing)
{
QPointF delta = event->pos() - event->lastPos();
if (rect().width() + delta.x() < sMinLength)
delta.setX(sMinLength - rect().width());
setRect(QRectF(rect().topLeft(), QSizeF(rect().width() + delta.x(), rect().height())));
}
else
{
QLineF currentLine(topLeftOrigin(), event->pos());
QLineF lastLine(topLeftOrigin(), event->lastPos());
rotateAroundTopLeftOrigin(currentLine.angleTo(lastLine));
}
event->accept();
}
}
void UBGraphicsRuler::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (mResizing)
{
mResizing = false;
event->accept();
}
else if (mRotating)
{
mRotating = false;
updateResizeCursor();
update(QRectF(topLeftOrigin(), QSizeF(sRotationRadius, sRotationRadius)));
event->accept();
}
else if (closeButtonRect().contains(event->pos()))
{
hide();
emit hidden();
event->accept();
}
else
{
QGraphicsRectItem::mouseReleaseEvent(event);
}
if (scene())
scene()->setModified(true);
}
void UBGraphicsRuler::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
QObject* obj = new QObject();
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
if (currentTool == UBStylusTool::Selector)
{
mCloseSvgItem->setParentItem(this);
mResizeSvgItem->setParentItem(this);
mRotateSvgItem->setParentItem(this);
mShowButtons = true;
mCloseSvgItem->setVisible(mShowButtons);
mResizeSvgItem->setVisible(mShowButtons);
mRotateSvgItem->setVisible(mShowButtons);
if (event->pos().x() >= resizeButtonRect().left())
{
setCursor(resizeCursor());
}
else
{
if (closeButtonRect().contains(event->pos()))
setCursor(closeCursor());
else if (rotateButtonRect().contains(event->pos()))
setCursor(rotateCursor());
else
setCursor(moveCursor());
}
event->accept();
update();
}
else if (UBDrawingController::drawingController()->isDrawingTool())
{
setCursor(drawRulerLineCursor());
UBDrawingController::drawingController()->mActiveRuler = this;
event->accept();
}
}
void UBGraphicsRuler::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
mShowButtons = false;
setCursor(Qt::ArrowCursor);
mCloseSvgItem->setVisible(mShowButtons);
mResizeSvgItem->setVisible(mShowButtons);
mRotateSvgItem->setVisible(mShowButtons);
UBDrawingController::drawingController()->mActiveRuler = NULL;
event->accept();
update();
}
void UBGraphicsRuler::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
if (currentTool == UBStylusTool::Selector)
{
mCloseSvgItem->setVisible(mShowButtons);
mResizeSvgItem->setVisible(mShowButtons);
mRotateSvgItem->setVisible(mShowButtons);
if (event->pos().x() >= resizeButtonRect().left())
{
setCursor(resizeCursor());
}
else
{
if (closeButtonRect().contains(event->pos()))
setCursor(closeCursor());
else if (rotateButtonRect().contains(event->pos()))
setCursor(rotateCursor());
else
setCursor(moveCursor());
}
event->accept();
}
else if (currentTool == UBStylusTool::Pen || currentTool == UBStylusTool::Marker)
{
event->accept();
}
}
void UBGraphicsRuler::fillBackground(QPainter *painter) void UBGraphicsRuler::fillBackground(QPainter *painter)
{ {
QRectF rect1(rect().topLeft(), QSizeF(rect().width(), rect().height() / 4)); QRectF rect1(rect().topLeft(), QSizeF(rect().width(), rect().height() / 4));
@ -365,25 +170,6 @@ QPointF UBGraphicsRuler::topLeftOrigin() const
return QPointF(rect().x() + sLeftEdgeMargin, rect().y()); return QPointF(rect().x() + sLeftEdgeMargin, rect().y());
} }
QCursor UBGraphicsRuler::moveCursor() const
{
return Qt::SizeAllCursor;
}
QCursor UBGraphicsRuler::resizeCursor() const
{
return mResizeCursor;
}
QCursor UBGraphicsRuler::rotateCursor() const
{
return UBResources::resources()->rotateCursor;
}
QCursor UBGraphicsRuler::closeCursor() const
{
return Qt::ArrowCursor;
}
QRectF UBGraphicsRuler::resizeButtonRect() const QRectF UBGraphicsRuler::resizeButtonRect() const
{ {
@ -440,51 +226,165 @@ QRectF UBGraphicsRuler::rotateButtonRect() const
return QRectF(rotateRectTopLeft, rotateRectSize); return QRectF(rotateRectTopLeft, rotateRectSize);
} }
UBGraphicsScene* UBGraphicsRuler::scene() const void UBGraphicsRuler::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{ {
return static_cast<UBGraphicsScene*>(QGraphicsRectItem::scene()); UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
if (currentTool == UBStylusTool::Selector)
{
mCloseSvgItem->setVisible(mShowButtons);
mResizeSvgItem->setVisible(mShowButtons);
mRotateSvgItem->setVisible(mShowButtons);
if (resizeButtonRect().contains(event->pos()))
setCursor(resizeCursor());
else if (closeButtonRect().contains(event->pos()))
setCursor(closeCursor());
else if (rotateButtonRect().contains(event->pos()))
setCursor(rotateCursor());
else
setCursor(moveCursor());
event->accept();
}
else if (currentTool == UBStylusTool::Pen || currentTool == UBStylusTool::Marker)
{
event->accept();
}
} }
QColor UBGraphicsRuler::drawColor() const void UBGraphicsRuler::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
return scene()->isDarkBackground() ? sDarkBackgroundDrawColor : sLightBackgroundDrawColor; if (event->pos().x() > resizeButtonRect().left())
{
mResizing = true;
event->accept();
}
else if (rotateButtonRect().contains(event->pos()))
{
mRotating = true;
event->accept();
}
else
{
mResizeSvgItem->setVisible(false);
mRotateSvgItem->setVisible(false);
QGraphicsItem::mousePressEvent(event);
}
mResizeSvgItem->setVisible(mShowButtons && mResizing);
mRotateSvgItem->setVisible(mShowButtons && mRotating);
mCloseSvgItem->setVisible(false);
} }
QColor UBGraphicsRuler::middleFillColor() const void UBGraphicsRuler::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
return scene()->isDarkBackground() ? sDarkBackgroundMiddleFillColor : sLightBackgroundMiddleFillColor; if (!mResizing && !mRotating)
{
QGraphicsItem::mouseMoveEvent(event);
}
else
{
if (mResizing)
{
QPointF delta = event->pos() - event->lastPos();
if (rect().width() + delta.x() < sMinLength)
delta.setX(sMinLength - rect().width());
setRect(QRectF(rect().topLeft(), QSizeF(rect().width() + delta.x(), rect().height())));
}
else
{
QLineF currentLine(topLeftOrigin(), event->pos());
QLineF lastLine(topLeftOrigin(), event->lastPos());
rotateAroundTopLeftOrigin(currentLine.angleTo(lastLine));
}
event->accept();
}
} }
QColor UBGraphicsRuler::edgeFillColor() const void UBGraphicsRuler::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
return scene()->isDarkBackground() ? sDarkBackgroundEdgeFillColor : sLightBackgroundEdgeFillColor; if (mResizing)
{
mResizing = false;
event->accept();
}
else if (mRotating)
{
mRotating = false;
updateResizeCursor(*this);
update(QRectF(topLeftOrigin(), QSizeF(sRotationRadius, sRotationRadius)));
event->accept();
}
else if (closeButtonRect().contains(event->pos()))
{
hide();
emit hidden();
event->accept();
}
else
{
QGraphicsItem::mouseReleaseEvent(event);
}
if (scene())
scene()->setModified(true);
} }
QFont UBGraphicsRuler::font() const void UBGraphicsRuler::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
QFont font("Arial"); UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController ()->stylusTool ();
font.setPixelSize(16);
return font; if (currentTool == UBStylusTool::Selector)
{
mCloseSvgItem->setParentItem(this);
mResizeSvgItem->setParentItem(this);
mRotateSvgItem->setParentItem(this);
mShowButtons = true;
mCloseSvgItem->setVisible(mShowButtons);
mResizeSvgItem->setVisible(mShowButtons);
mRotateSvgItem->setVisible(mShowButtons);
if (event->pos().x() >= resizeButtonRect().left())
{
setCursor(resizeCursor());
}
else
{
if (closeButtonRect().contains(event->pos()))
setCursor(closeCursor());
else if (rotateButtonRect().contains(event->pos()))
setCursor(rotateCursor());
else
setCursor(moveCursor());
}
event->accept();
update();
}
else if (UBDrawingController::drawingController()->isDrawingTool())
{
setCursor(drawRulerLineCursor());
UBDrawingController::drawingController()->mActiveRuler = this;
event->accept();
}
} }
void UBGraphicsRuler::updateResizeCursor() void UBGraphicsRuler::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
QPixmap pix(":/images/cursors/resize.png"); mShowButtons = false;
QTransform itemTransform = sceneTransform(); setCursor(Qt::ArrowCursor);
QRectF itemRect = boundingRect(); mCloseSvgItem->setVisible(mShowButtons);
QPointF topLeft = itemTransform.map(itemRect.topLeft()); mResizeSvgItem->setVisible(mShowButtons);
QPointF topRight = itemTransform.map(itemRect.topRight()); mRotateSvgItem->setVisible(mShowButtons);
QLineF topLine(topLeft, topRight); UBDrawingController::drawingController()->mActiveRuler = NULL;
qreal angle = topLine.angle(); event->accept();
QTransform tr; update();
tr.rotate(- angle);
QCursor resizeCursor = QCursor(pix.transformed(tr, Qt::SmoothTransformation), pix.width() / 2, pix.height() / 2);
mResizeCursor = resizeCursor;
} }
QCursor UBGraphicsRuler::drawRulerLineCursor() const
UBGraphicsScene* UBGraphicsRuler::scene() const
{ {
return UBResources::resources()->drawLineRulerCursor; return static_cast<UBGraphicsScene*>(QGraphicsRectItem::scene());
} }
void UBGraphicsRuler::StartLine(const QPointF& scenePos, qreal width) void UBGraphicsRuler::StartLine(const QPointF& scenePos, qreal width)

@ -44,70 +44,37 @@ class UBGraphicsRuler : public UBAbstractDrawRuler, public QGraphicsRectItem, pu
void hidden(); void hidden();
protected: protected:
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget); virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget);
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
// Events // Events
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
private: private:
// Helpers // Helpers
void fillBackground(QPainter *painter); void fillBackground(QPainter *painter);
void paintGraduations(QPainter *painter); void paintGraduations(QPainter *painter);
void paintRotationCenter(QPainter *painter); void paintRotationCenter(QPainter *painter);
void rotateAroundTopLeftOrigin(qreal angle); virtual void rotateAroundTopLeftOrigin(qreal angle);
void updateResizeCursor();
QPointF topLeftOrigin() const;
QCursor moveCursor() const;
QCursor resizeCursor() const;
QCursor rotateCursor() const;
QCursor closeCursor() const;
QCursor drawRulerLineCursor() const;
QRectF resizeButtonRect() const;
QRectF closeButtonRect() const;
QRectF rotateButtonRect() const;
UBGraphicsScene* scene() const;
QColor drawColor() const;
QColor middleFillColor() const;
QColor edgeFillColor() const;
QFont font() const;
int drawLineDirection; QGraphicsSvgItem* mResizeSvgItem;
// Members virtual QPointF topLeftOrigin() const;
bool mResizing; virtual QRectF resizeButtonRect() const;
bool mRotating; virtual QRectF closeButtonRect() const;
bool mShowButtons; virtual QRectF rotateButtonRect() const;
QGraphicsSvgItem* mCloseSvgItem; virtual UBGraphicsScene* scene() const;
QGraphicsSvgItem* mRotateSvgItem;
QGraphicsSvgItem* mResizeSvgItem;
QCursor mResizeCursor;
qreal mAntiScaleRatio;
QPointF startDrawPosition; int drawLineDirection;
// Constants // Constants
static const QRect sDefaultRect; static const QRect sDefaultRect;
static const int sLeftEdgeMargin = 10;
static const int sMinLength = 150;
static const int sDegreeToQtAngleUnit = 16;
static const int sRotationRadius = 15;
static const int sPixelsPerMillimeter = 5;
static const int sFillTransparency = 127;
static const int sDrawTransparency = 192;
static const int sRoundingRadius = sLeftEdgeMargin / 2;
static const QColor sLightBackgroundEdgeFillColor;
static const QColor sLightBackgroundMiddleFillColor;
static const QColor sLightBackgroundDrawColor;
static const QColor sDarkBackgroundEdgeFillColor;
static const QColor sDarkBackgroundMiddleFillColor;
static const QColor sDarkBackgroundDrawColor;
}; };
#endif /* UBGRAPHICSRULER_H_ */ #endif /* UBGRAPHICSRULER_H_ */

@ -1,10 +1,232 @@
#include <QGraphicsPolygonItem>
#include <QPolygonF>
#include "tools/UBGraphicsTriangle.h" #include "tools/UBGraphicsTriangle.h"
#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "board/UBDrawingController.h"
#include "domain/UBGraphicsScene.h"
#include "core/memcheck.h" #include "core/memcheck.h"
const QRect UBGraphicsTriangle::sDefaultRect = QRect(0, 0, 800, 400);
const UBGraphicsTriangle::UBGraphicsTriangleOrientation UBGraphicsTriangle::sDefaultOrientation =
UBGraphicsTriangle::BottomLeft;
UBGraphicsTriangle::UBGraphicsTriangle() UBGraphicsTriangle::UBGraphicsTriangle()
:QGraphicsPolygonItem()
{ {
setRect(sDefaultRect, sDefaultOrientation);
create(*this);
//updateResizeCursor();
} }
UBGraphicsTriangle::~UBGraphicsTriangle() UBGraphicsTriangle::~UBGraphicsTriangle()
{} {
}
UBItem* UBGraphicsTriangle::deepCopy(void) const
{
UBGraphicsTriangle* copy = new UBGraphicsTriangle();
copy->setPos(this->pos());
copy->setPolygon(this->polygon());
copy->setZValue(this->zValue());
copy->setTransform(this->transform());
// TODO UB 4.7 ... complete all members ?
return copy;
}
void UBGraphicsTriangle::setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation)
{
mRect.setCoords(x, y, x+w, y+h);
mOrientation = orientation;
QPolygonF polygon;
polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h) << QPoint(x, y);
QTransform t;
switch(orientation)
{
case BottomLeft:
t.setMatrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
break;
case BottomRight:
t.setMatrix(-1, 0, 0, 0, 1, 0, x, 0, 1);
break;
case TopLeft:
t.setMatrix(1, 0, 0, 0, -1, 0, 0, y, 1);
break;
case TopRight:
t.setMatrix(-1, 0, 0, 0, -1, 0, x, y, 1);
break;
}
/*
switch(orientation)
{
case BottomLeft:
polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x+w, y + h) << QPoint(x, y);
break;
case BottomRight:
polygon << QPointF(x, y + h) << QPoint(x + w, y + y) << QPoint(x + w, y) << QPoint(x, y + h);
break;
case TopLeft:
polygon << QPointF(x, y) << QPoint(x, y + h) << QPoint(x + w, y) << QPoint(x, y);
break;
case TopRight:
polygon << QPointF(x, y) << QPoint(x + w, y + h) << QPoint(x+w, y) << QPoint(x, y );
break;
}
*/
setPolygon(polygon);
setTransform(t);
}
UBGraphicsScene* UBGraphicsTriangle::scene() const
{
return static_cast<UBGraphicsScene*>(QGraphicsPolygonItem::scene());
}
void UBGraphicsTriangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget)
{
QPointF A1(mRect.x(), mRect.y());
QPointF B1(mRect.x(), mRect.y() + mRect.height());
QPointF C1(mRect.x() + mRect.width(), mRect.y() + mRect.height());
qreal d = 70;
qreal C = sqrt(mRect.width() * mRect.width() + mRect.height() * mRect.height());
qreal L = (C * d + mRect.width() * d)/ mRect.height();
qreal K = (C * d + mRect.height() * d)/ mRect.width();
qreal W1 = mRect.height() * d / C;
qreal H1 = mRect.width() * d / C;
QPointF A2(mRect.x() + d, mRect.y() + K);
QPointF B2(mRect.x() + d, mRect.y() + mRect.height() - d);
QPointF C2(mRect.x() + mRect.width() - L, mRect.y() + mRect.height() - d);
QPoint CC(mRect.x() + mRect.width() - L + W1,
mRect.y() + mRect.height() - d - H1);
painter->setPen(Qt::NoPen);
QPolygonF polygon;
QLinearGradient gradient1(QPointF(A1.x(), 0), QPointF(A2.x(), 0));
gradient1.setColorAt(0, edgeFillColor());
gradient1.setColorAt(1, middleFillColor());
painter->setBrush(gradient1);
polygon << A1 << A2 << B2 << B1;
painter->drawPolygon(polygon);
polygon.clear();
QLinearGradient gradient2(QPointF(0, B1.y()), QPointF(0, B2.y()));
gradient2.setColorAt(0, edgeFillColor());
gradient2.setColorAt(1, middleFillColor());
painter->setBrush(gradient2);
polygon << B1 << B2 << C2 << C1;
painter->drawPolygon(polygon);
polygon.clear();
QLinearGradient gradient3(CC, C2);
gradient3.setColorAt(0, edgeFillColor());
gradient3.setColorAt(1, middleFillColor());
painter->setBrush(gradient3);
polygon << C1 << C2 << A2 << A1;
painter->drawPolygon(polygon);
polygon.clear();
painter->setBrush(Qt::NoBrush);
painter->setPen(drawColor());
polygon << A1 << B1 << C1;
painter->drawPolygon(polygon);
polygon.clear();
polygon << A2 << B2 << C2;
painter->drawPolygon(polygon);
paintGraduations(painter);
}
void UBGraphicsTriangle::paintGraduations(QPainter *painter)
{
const int centimeterGraduationHeight = 15;
const int halfCentimeterGraduationHeight = 10;
const int millimeterGraduationHeight = 5;
const int millimetersPerCentimeter = 10;
const int millimetersPerHalfCentimeter = 5;
painter->save();
painter->setFont(font());
QFontMetricsF fontMetrics(painter->font());
for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++)
{
int graduationX = topLeftOrigin().x() + sPixelsPerMillimeter * millimeters;
int graduationHeight = (0 == millimeters % millimetersPerCentimeter) ?
centimeterGraduationHeight :
((0 == millimeters % millimetersPerHalfCentimeter) ?
halfCentimeterGraduationHeight : millimeterGraduationHeight);
// Check that grad. line inside triangle
qreal lineY = rect().bottom() - rect().height()/rect().width()*(rect().width() - graduationX);
if (lineY >= topLeftOrigin().y() + rect().height() - graduationHeight)
break;
painter->drawLine(QLine(graduationX, topLeftOrigin().y() + rect().height(), graduationX, topLeftOrigin().y() + rect().height() - graduationHeight));
if (0 == millimeters % millimetersPerCentimeter)
{
QString text = QString("%1").arg((int)(millimeters / millimetersPerCentimeter));
int textXRight = graduationX + fontMetrics.width(text) / 2;
qreal textWidth = fontMetrics.width(text);
qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5;
int textY = rect().bottom() - 5 - centimeterGraduationHeight - textHeight;
lineY = rect().bottom() - rect().height()/rect().width()*(rect().width() - textXRight);
if (textXRight < rect().right()
&& lineY < textY)
{
painter->drawText(
QRectF(graduationX - textWidth / 2,
textY, textWidth, textHeight),
Qt::AlignVCenter, text);
}
}
}
painter->restore();
}
void UBGraphicsTriangle::rotateAroundTopLeftOrigin(qreal angle)
{}
QPointF UBGraphicsTriangle::topLeftOrigin() const
{
return QPointF(mRect.x() + sLeftEdgeMargin , mRect.y());
}
QRectF UBGraphicsTriangle::resizeButtonRect() const
{
return QRectF(0,0,0,0);
}
QRectF UBGraphicsTriangle::closeButtonRect() const
{
return QRectF(0,0,0,0);
}
QRectF UBGraphicsTriangle::rotateButtonRect() const
{
return QRectF(0,0,0,0);
}

@ -15,17 +15,75 @@
#include "core/UB.h" #include "core/UB.h"
#include "domain/UBItem.h" #include "domain/UBItem.h"
#include "tools/UBAbstractDrawRuler.h"
class UBGraphicsScene; class UBGraphicsScene;
class UBItem;
class UBGraphicsTriangle : public QObject, public QGraphicsPolygonItem, public UBItem class UBGraphicsTriangle : public UBAbstractDrawRuler, public QGraphicsPolygonItem, public UBItem
{ {
Q_OBJECT; Q_OBJECT;
public: public:
UBGraphicsTriangle(); UBGraphicsTriangle();
virtual ~UBGraphicsTriangle(); virtual ~UBGraphicsTriangle();
enum { Type = UBGraphicsItemType::TriangleItemType };
virtual int type() const
{
return Type;
}
virtual UBItem* deepCopy(void) const;
enum UBGraphicsTriangleOrientation
{
BottomLeft = 0,
BottomRight,
TopLeft,
TopRight
};
static UBGraphicsTriangleOrientation orientationFromStr(QStringRef& str)
{
if (str == "BottomLeft") return BottomLeft;
if (str == "BottomRight") return BottomRight;
if (str == "TopLeft") return TopLeft;
if (str == "TopRight") return TopRight;
return sDefaultOrientation;
}
void setRect(const QRectF &rect, UBGraphicsTriangleOrientation orientation)
{
setRect(rect.x(), rect.y(), rect.width(), rect.height(), orientation);
}
void setRect(qreal x, qreal y, qreal w, qreal h, UBGraphicsTriangleOrientation orientation);
QRectF rect() const {return mRect;}
UBGraphicsScene* scene() const;
protected:
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *styleOption, QWidget *widget);
virtual void rotateAroundTopLeftOrigin(qreal angle);
virtual QPointF topLeftOrigin() const;
virtual QRectF resizeButtonRect() const;
virtual QRectF closeButtonRect() const;
virtual QRectF rotateButtonRect() const;
private:
static const QRect sDefaultRect;
static const UBGraphicsTriangleOrientation sDefaultOrientation;
void paintGraduations(QPainter *painter);
QRectF mRect;
UBGraphicsTriangleOrientation mOrientation;
}; };
#endif /* UBGRAPHICSTRIANGLE_H_ */ #endif /* UBGRAPHICSTRIANGLE_H_ */

Loading…
Cancel
Save