Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class MetricCoordinateSystem: public graphicsUtils::AbstractCoordinateSystem
{
Q_OBJECT
public:
explicit MetricCoordinateSystem(twoDModel::model::SizeUnit *metricSystem,
explicit MetricCoordinateSystem(const QSharedPointer<twoDModel::model::SizeUnit> &metricSystem,
QObject* parent = nullptr);

~MetricCoordinateSystem();
~MetricCoordinateSystem() override;

/// Conversе of units of measurement into pixels
qreal toPx(const qreal size) const override;
Expand All @@ -46,9 +46,7 @@ class MetricCoordinateSystem: public graphicsUtils::AbstractCoordinateSystem
/// Converting pixels to QPointF
QPointF toUnit(const QPointF &size) const override;
private:
// Doesn't take ownership, ownership is twoDModel::model::Settings.
// The lifetime of this object is greater than MetricCoordinateSystem (see Model.h)
QPointer<twoDModel::model::SizeUnit> mMetricSystem;
QSharedPointer<twoDModel::model::SizeUnit> mMetricSystem;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ namespace twoDModel {
namespace model {

/// Incapsulates size unit settings used by 2D model.
class TWO_D_MODEL_EXPORT SizeUnit : public QObject
class TWO_D_MODEL_EXPORT SizeUnit
{
Q_OBJECT

public:
/// Possible units of measurement
enum class Unit {
Expand All @@ -42,14 +40,16 @@ class TWO_D_MODEL_EXPORT SizeUnit : public QObject
/// The current pixel value in centimeters
qreal pixelsInCm() const;

twoDModel::model::SizeUnit::Unit unit() const;

/// Serialize the sizeUnit in the WorldModel
void serialize(QDomElement &parent) const;

/// Derialize the sizeUnit in the WorldModel
void deserialize(const QDomElement &parent);

/// Set the current unit of measurement
void setUnit(twoDModel::model::SizeUnit::Unit unit);
void setSizeUnit(twoDModel::model::SizeUnit::Unit unit);

/// Multiplier for conversion to pixels
qreal countFactor() const;
Expand All @@ -60,14 +60,9 @@ class TWO_D_MODEL_EXPORT SizeUnit : public QObject
/// Get current unit string representation
QString toStr() const;

std::map<QString, Unit> currentValues() const;

Unit defaultUnit() const;

Q_SIGNALS:
/// Emit when the sizeUnit is serialized
void sizeUnitChanged(const twoDModel::model::SizeUnit::Unit &unit);
static std::map<QString, Unit> currentValues();

static Unit defaultUnit();
private:
Unit mSizeUnit { Unit::Pixels };
qreal mPixelsInCm { twoDModel::pixelsInCm };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
#pragma once

#include <QtCore/QObject>
#include "metricSystem.h"
#include <QSharedPointer>
#include "twoDModel/twoDModelDeclSpec.h"

class QDomElement;

namespace twoDModel {
namespace model {

class SizeUnit;

/// Incapsulates settings used by 2D model.
class TWO_D_MODEL_EXPORT Settings : public QObject
{
Expand All @@ -37,10 +39,6 @@ class TWO_D_MODEL_EXPORT Settings : public QObject
/// Returns true is user wants to add some noise to sensors values.
bool realisticSensors() const;

/// To simplify the already overloaded WorldModel xml,
/// it was decided to use a tag in the existing <settings> tag
SizeUnit *sizeUnit();

qreal pixelsInCm() const;

/// Returns true is user wants to add some noise to motors work.
Expand All @@ -56,17 +54,22 @@ class TWO_D_MODEL_EXPORT Settings : public QObject

void setRealisticMotors(bool set);

SizeUnit *sizeUnit() const;
QSharedPointer<SizeUnit> sizeUnit() const;

Q_SIGNALS:
/// Emitted each time when user modifies physical preferences.
void physicsChanged(bool isRealistic);

/// Emit when the sizeUnit is serialized
void sizeUnitChanged(const QSharedPointer<twoDModel::model::SizeUnit> &unit);

void gridSizeChanged(qreal size);

private:
bool mRealisticPhysics { false };
bool mRealisticSensors { false };
bool mRealisticMotors { false };
QScopedPointer<SizeUnit> mSizeUnitSystem;
QSharedPointer<SizeUnit> mSizeUnitSystem;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace twoDModel {
namespace model {
class Model;
class RobotModel;
class SizeUnit;
}

namespace view {
Expand Down Expand Up @@ -176,8 +177,7 @@ private Q_SLOTS:
void trainingModeChanged(bool enabled);

void updateUIPhysicsSettings();
void updateRobotInfoWidget(const qreal factor, const QString& unitString);

void updateRobotInfoWidget(const QSharedPointer<twoDModel::model::SizeUnit> &sizeUnit);
private:
enum CursorType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

namespace twoDModel {

namespace model {
class SizeUnit;
}

namespace items {

class ColorFieldItem: public view::TwoDSceneItem
Expand Down
14 changes: 7 additions & 7 deletions plugins/robots/common/twoDModel/src/engine/items/imageItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using namespace qReal;
using namespace graphicsUtils;

ImageItem::ImageItem(graphicsUtils::AbstractCoordinateSystem *metricSystem,
const QSharedPointer<model::Image> &image, QRect geometry)
const QSharedPointer<model::Image> &image, const QRectF &geometry)
: mImage(image)
{
setCoordinateSystem(metricSystem);
Expand All @@ -44,7 +44,7 @@ ImageItem::ImageItem(graphicsUtils::AbstractCoordinateSystem *metricSystem,

AbstractItem *ImageItem::clone() const
{
const auto cloned = new ImageItem(coordinateSystem(), mImage, QRect(x1(), y1(), x2() - x1(), y2() - y1()));
const auto cloned = new ImageItem(coordinateSystem(), mImage, QRectF(x1(), y1(), x2() - x1(), y2() - y1()));
AbstractItem::copyTo(cloned);
return cloned;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ QRectF ImageItem::boundingRect() const

QRectF ImageItem::calcNecessaryBoundingRect() const
{
return QRectF(qMin(x1(), x2()), qMin(y1(), y2()), qAbs(x2() - x1()), qAbs(y2() - y1()));
return {qMin(x1(), x2()), qMin(y1(), y2()), qAbs(x2() - x1()), qAbs(y2() - y1())};
}

void ImageItem::drawItem(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Expand Down Expand Up @@ -232,10 +232,10 @@ QRectF ImageItem::deserializeRect(const QString &string) const
const auto y = splittedStr[1].toDouble();
const auto w = splittedStr[2].toDouble();
const auto h = splittedStr[3].toDouble();
return QRectF(x, y, w, h);
return {x, y, w, h};
}

return QRectF();
return {};
}

void ImageItem::resizeItem(QGraphicsSceneMouseEvent *event)
Expand All @@ -250,7 +250,7 @@ void ImageItem::resizeItem(QGraphicsSceneMouseEvent *event)
}
} else if (dragState() != None) {
setFlag(QGraphicsItem::ItemIsMovable, false);
const auto gridSize = SettingsManager::value("2dGridCellSize").toInt();
const auto gridSize = SettingsManager::value("2dDoubleGridCellSize").toReal();
const auto x = alignedCoordinate(event->scenePos().x(), gridSize);
const auto y = alignedCoordinate(event->scenePos().y(), gridSize);
setXYWithDragState(mapFromScene(x, y));
Expand All @@ -261,7 +261,7 @@ void ImageItem::resizeItem(QGraphicsSceneMouseEvent *event)
// and align top left corner to grid
QRectF itemBoundingRect = calcNecessaryBoundingRect();
const auto topLeft = mapToScene(QPointF(itemBoundingRect.left(), itemBoundingRect.top()));
const auto gridSize = SettingsManager::value("2dGridCellSize").toInt();
const auto gridSize = SettingsManager::value("2dDoubleGridCellSize").toReal();
const auto x = alignedCoordinate(topLeft.x(), gridSize);
const auto y = alignedCoordinate(topLeft.y(), gridSize);
auto delta = QPointF(x, y) - topLeft;
Expand Down
4 changes: 2 additions & 2 deletions plugins/robots/common/twoDModel/src/engine/items/imageItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ImageItem : public graphicsUtils::AbstractItem

public:
ImageItem(graphicsUtils::AbstractCoordinateSystem *metricSystem,
const QSharedPointer<model::Image> &image, QRect geometry);
const QSharedPointer<model::Image> &image, const QRectF &geometry);

AbstractItem *clone() const;

Expand All @@ -45,7 +45,7 @@ class ImageItem : public graphicsUtils::AbstractItem

QRectF boundingRect() const override;
QRectF calcNecessaryBoundingRect() const override;
void drawItem(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
void drawItem(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
void drawExtractionForItem(QPainter* painter) override;
QPainterPath resizeArea() const override;
void resizeItem(QGraphicsSceneMouseEvent *event) override;
Expand Down
40 changes: 21 additions & 19 deletions plugins/robots/common/twoDModel/src/engine/items/wallItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ WallItem::WallItem(graphicsUtils::AbstractCoordinateSystem *metricSystem,

WallItem *WallItem::clone() const
{
WallItem * const cloned = new WallItem(coordinateSystem(), {x1(), y1()}, {x2(), y2()});
auto * const cloned = new WallItem(coordinateSystem(), {x1(), y1()}, {x2(), y2()});
AbstractItem::copyTo(cloned);
connect(this, &AbstractItem::positionChanged, cloned, &WallItem::recalculateBorders);
connect(this, &AbstractItem::x1Changed, cloned, &WallItem::recalculateBorders);
Expand All @@ -65,7 +65,7 @@ WallItem *WallItem::clone() const

QAction *WallItem::wallTool()
{
QAction * const result = new QAction(QIcon(":/icons/2d_wall.png"), tr("Wall (W)"), nullptr);
auto * const result = new QAction(QIcon(":/icons/2d_wall.png"), tr("Wall (W)"), nullptr);
result->setShortcuts({QKeySequence(Qt::Key_W), QKeySequence(Qt::Key_2)});
result->setCheckable(true);
return result;
Expand Down Expand Up @@ -175,8 +175,8 @@ void WallItem::deserialize(const QDomElement &element)
setY2(end.y());

readPenBrush(element);
if (pen().widthF()) {
mWallWidth = pen().widthF();
if (pen().width()) {
mWallWidth = pen().width();
}

SolidItem::deserialize(element);
Expand All @@ -190,7 +190,8 @@ QPainterPath WallItem::path() const

void WallItem::recalculateBorders()
{
mPath = mLineImpl.shape(mWallWidth, begin().x(), begin().y(), end().x(), end().y());
/// TODO: Fix narrowing conversion
mPath = mLineImpl.shape(static_cast<int>(mWallWidth), begin().x(), begin().y(), end().x(), end().y());
}

void WallItem::resizeItem(QGraphicsSceneMouseEvent *event)
Expand All @@ -202,7 +203,7 @@ void WallItem::resizeItem(QGraphicsSceneMouseEvent *event)
reshapeRectWithShift();
} else {
if (SettingsManager::value("2dShowGrid").toBool() && event->modifiers() != Qt::ControlModifier) {
resizeWithGrid(event, SettingsManager::value("2dGridCellSize").toInt());
resizeWithGrid(event, SettingsManager::value("2dDoubleGridCellSize").toReal());
} else {
if (dragState() == TopLeft || dragState() == BottomRight) {
calcResizeItem(event);
Expand All @@ -220,7 +221,8 @@ void WallItem::reshapeRectWithShift()
const qreal differenceY = qAbs(y2() - y1());
const qreal differenceXY = qAbs(differenceX - differenceY);
const qreal size = qMax(differenceX, differenceY);
const int delta = size / 2;
/// TODO: Fix narrowing conversion
const int delta = static_cast<int>(size / 2);
if (differenceXY > delta) {
const qreal corner1X = dragState() == TopLeft ? x2() : x1();
const qreal corner1Y = dragState() == TopLeft ? y2() : y1();
Expand All @@ -247,7 +249,7 @@ void WallItem::reshapeRectWithShift()
}
}

void WallItem::resizeWithGrid(QGraphicsSceneMouseEvent *event, int indexGrid)
void WallItem::resizeWithGrid(QGraphicsSceneMouseEvent *event, qreal gridSize)
{
const qreal x = mapFromScene(event->scenePos()).x();
const qreal y = mapFromScene(event->scenePos()).y();
Expand All @@ -257,31 +259,31 @@ void WallItem::resizeWithGrid(QGraphicsSceneMouseEvent *event, int indexGrid)
if (dragState() == TopLeft) {
setX1(x);
setY1(y);
reshapeBeginWithGrid(indexGrid);
reshapeBeginWithGrid(gridSize);
} else if (dragState() == BottomRight) {
setX2(x);
setY2(y);
reshapeEndWithGrid(indexGrid);
reshapeEndWithGrid(gridSize);
} else {
setPos(mEstimatedPos);
moveBy(alignedCoordinate(begin().x(), indexGrid) - begin().x()
, alignedCoordinate(begin().y(), indexGrid) - begin().y());
moveBy(alignedCoordinate(begin().x(), gridSize) - begin().x()
, alignedCoordinate(begin().y(), gridSize) - begin().y());
}
}

void WallItem::reshapeEndWithGrid(int indexGrid)
void WallItem::reshapeEndWithGrid(qreal gridSize)
{
setX2(alignedCoordinate(end().x(), indexGrid) - pos().x());
setY2(alignedCoordinate(end().y(), indexGrid) - pos().y());
setX2(alignedCoordinate(end().x(), gridSize) - pos().x());
setY2(alignedCoordinate(end().y(), gridSize) - pos().y());
}

void WallItem::reshapeBeginWithGrid(int indexGrid)
void WallItem::reshapeBeginWithGrid(qreal gridSize)
{
setX1(alignedCoordinate(begin().x(), indexGrid) - pos().x());
setY1(alignedCoordinate(begin().y(), indexGrid) - pos().y());
setX1(alignedCoordinate(begin().x(), gridSize) - pos().x());
setY1(alignedCoordinate(begin().y(), gridSize) - pos().y());
}

void WallItem::alignTheWall(int indexGrid)
void WallItem::alignTheWall(qreal indexGrid)
{
reshapeBeginWithGrid(indexGrid);
reshapeEndWithGrid(indexGrid);
Expand Down
10 changes: 5 additions & 5 deletions plugins/robots/common/twoDModel/src/engine/items/wallItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class WallItem : public graphicsUtils::AbstractItem, public SolidItem

QPainterPath path() const;

void resizeWithGrid(QGraphicsSceneMouseEvent *event, int indexGrid);
void resizeWithGrid(QGraphicsSceneMouseEvent *event, qreal gridSize);

void reshapeEndWithGrid(int indexGrid);
void reshapeBeginWithGrid(int indexGrid);
void reshapeEndWithGrid(qreal indexGrid);
void reshapeBeginWithGrid(qreal indexGrid);
void setDraggedEnd(qreal x, qreal y);
void alignTheWall(int indexGrid);
void alignTheWall(qreal indexGrid);

QPolygonF collidingPolygon() const override;
BodyType bodyType() const override;
Expand All @@ -79,7 +79,7 @@ class WallItem : public graphicsUtils::AbstractItem, public SolidItem
const QImage mImage;

QPainterPath mPath;
qreal mWallWidth {10};
int mWallWidth {10};
QPointF mEstimatedPos;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
using namespace twoDModel::model;

MetricCoordinateSystem::MetricCoordinateSystem(
twoDModel::model::SizeUnit *metricSystem,
const QSharedPointer<twoDModel::model::SizeUnit> &metricSystem,
QObject* parent)
: graphicsUtils::AbstractCoordinateSystem(parent)
, mMetricSystem(metricSystem)
{
}

MetricCoordinateSystem::~MetricCoordinateSystem()
{
}
MetricCoordinateSystem::~MetricCoordinateSystem() = default;

qreal MetricCoordinateSystem::toPx(const qreal size) const
{
if (mMetricSystem.isNull()) {
if (!mMetricSystem) {
return size;
}

Expand All @@ -41,7 +39,7 @@ qreal MetricCoordinateSystem::toPx(const qreal size) const

qreal MetricCoordinateSystem::toUnit(const qreal size) const
{
if (mMetricSystem.isNull()) {
if (!mMetricSystem) {
return size;
}

Expand Down
Loading
Loading