diff --git a/src/annotations/core/AnnotationItemFactory.cpp b/src/annotations/core/AnnotationItemFactory.cpp index 4388681..5e84b25 100644 --- a/src/annotations/core/AnnotationItemFactory.cpp +++ b/src/annotations/core/AnnotationItemFactory.cpp @@ -1,167 +1,168 @@ /* * Copyright (C) 2018 Damir Porobic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "AnnotationItemFactory.h" namespace kImageAnnotator { AnnotationItemFactory::AnnotationItemFactory(Config *config) { mPropertiesFactory = new AnnotationPropertiesFactory(config); mNumberManager = new NumberManager(); connect(config, &Config::firstBadgeNumberChanged, mNumberManager, &NumberManager::firstNumberChanged); reset(); } AnnotationItemFactory::~AnnotationItemFactory() { delete mPropertiesFactory; delete mNumberManager; } void AnnotationItemFactory::reset() { mNextZValue = 1; mNumberManager->reset(); } AbstractAnnotationItem *AnnotationItemFactory::create(const QPointF &initPosition, ToolTypes toolType) { auto properties = mPropertiesFactory->create(toolType); auto *newItem = createItem(initPosition, toolType, properties); setZValue(newItem); return newItem; } AbstractAnnotationItem *AnnotationItemFactory::clone(const AbstractAnnotationItem *item) { Q_ASSERT(item != nullptr); auto newItem = cloneItem(item); setZValue(newItem); return newItem; } AbstractAnnotationItem *AnnotationItemFactory::createItem(const QPointF &initPosition, const ToolTypes &toolType, AnnotationProperties *properties) { AbstractAnnotationItem *newItem = nullptr; switch (toolType) { case ToolTypes::Pen: newItem = new AnnotationPen(initPosition, dynamic_cast(properties)); break; case ToolTypes::MarkerPen: newItem = new AnnotationPen(initPosition, dynamic_cast(properties)); break; case ToolTypes::MarkerRect: newItem = new AnnotationRect(initPosition, properties); break; case ToolTypes::MarkerEllipse: newItem = new AnnotationEllipse(initPosition, properties); break; case ToolTypes::Line: newItem = new AnnotationLine(initPosition, properties); break; case ToolTypes::Ellipse: newItem = new AnnotationEllipse(initPosition, properties); break; case ToolTypes::Rect: newItem = new AnnotationRect(initPosition, properties); break; case ToolTypes::Arrow: newItem = new AnnotationArrow(initPosition, properties); break; case ToolTypes::DoubleArrow: newItem = new AnnotationDoubleArrow(initPosition, properties); break; case ToolTypes::Number: newItem = new AnnotationNumber(initPosition, dynamic_cast(properties)); mNumberManager->addItem(dynamic_cast(newItem)); break; case ToolTypes::Text: newItem = new AnnotationText(initPosition, dynamic_cast(properties)); break; case ToolTypes::Blur: newItem = new AnnotationBlur(initPosition, properties); break; default: qCritical("Cannot create item for provided tool type."); } return newItem; } AbstractAnnotationItem *AnnotationItemFactory::cloneItem(const AbstractAnnotationItem *item) { Q_ASSERT(item != nullptr); AbstractAnnotationItem *newItem = nullptr; switch (item->toolType()) { case ToolTypes::Pen: newItem = new AnnotationPen(*(static_cast(item))); break; case ToolTypes::MarkerPen: newItem = new AnnotationPen(*(static_cast(item))); break; case ToolTypes::MarkerRect: newItem = new AnnotationRect(*(static_cast(item))); break; case ToolTypes::MarkerEllipse: newItem = new AnnotationEllipse(*(static_cast(item))); break; case ToolTypes::Line: newItem = new AnnotationLine(*(static_cast(item))); break; case ToolTypes::Ellipse: newItem = new AnnotationEllipse(*(static_cast(item))); break; case ToolTypes::Rect: newItem = new AnnotationRect(*(static_cast(item))); break; case ToolTypes::Arrow: newItem = new AnnotationArrow(*(static_cast(item))); break; case ToolTypes::DoubleArrow: newItem = new AnnotationDoubleArrow(*(static_cast(item))); break; case ToolTypes::Number: newItem = new AnnotationNumber(*(static_cast(item))); + mNumberManager->addItem(dynamic_cast(newItem)); break; case ToolTypes::Text: newItem = new AnnotationText(*(static_cast(item))); break; case ToolTypes::Blur: newItem = new AnnotationBlur(*(static_cast(item))); break; default: qCritical("Cannot create item for provided tool type."); } return newItem; } void AnnotationItemFactory::setZValue(AbstractAnnotationItem *item) { if (item != nullptr) { item->setZValue(mNextZValue++); } } } // namespace kImageAnnotator diff --git a/src/annotations/items/AnnotationNumber.cpp b/src/annotations/items/AnnotationNumber.cpp index 7303966..b2257c2 100644 --- a/src/annotations/items/AnnotationNumber.cpp +++ b/src/annotations/items/AnnotationNumber.cpp @@ -1,99 +1,98 @@ /* * Copyright (C) 2018 Damir Porobic * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "AnnotationNumber.h" namespace kImageAnnotator { AnnotationNumber::AnnotationNumber(const QPointF ¢erPosition, AnnotationTextProperties *properties) : AbstractAnnotationRect(centerPosition, properties) { mRect->moveCenter(centerPosition); } AnnotationNumber::AnnotationNumber(const AnnotationNumber &other) : AbstractAnnotationRect(other) { - mNumberString = other.mNumberString; updateShape(); } void AnnotationNumber::addPoint(const QPointF &position, bool modified) { Q_UNUSED(position); Q_UNUSED(modified); // Nothing to do here } const AnnotationTextProperties *AnnotationNumber::properties() const { return dynamic_cast(AbstractAnnotationItem::properties()); } ToolTypes AnnotationNumber::toolType() const { return ToolTypes::Number; } void AnnotationNumber::setNumber(int number) { mNumberString = QString::number(number); updateRect(); } int AnnotationNumber::number() const { return mNumberString.toInt(); } void AnnotationNumber::updateRect() { prepareGeometryChange(); auto center = mRect->center(); auto size = getTextRectSize(); mRect->setSize(size); mRect->moveCenter(center); updateShape(); } void AnnotationNumber::updateShape() { QPainterPath path; path.addEllipse(*mRect); setShape(path); } void AnnotationNumber::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { AbstractAnnotationRect::paint(painter, option, widget); painter->setFont(properties()->font()); painter->setPen(properties()->textColor()); painter->drawText(boundingRect(), Qt::AlignCenter, mNumberString); } QSizeF AnnotationNumber::getTextRectSize() const { QFontMetricsF metrics(properties()->font()); auto boundingRect = metrics.boundingRect(mNumberString).adjusted(-5, -5, 5, 5); auto largestSite = boundingRect.width() > boundingRect.height() ? boundingRect.width() : boundingRect.height(); return { largestSite, largestSite }; } } // namespace kImageAnnotator