Skip to content

Commit 5674220

Browse files
authored
Merge pull request #1166 from IKhonakhbeeva/fixes
Fixes and features for images [AUTOZIP]
2 parents 04eb54b + fba8146 commit 5674220

File tree

11 files changed

+72
-33
lines changed

11 files changed

+72
-33
lines changed

plugins/robots/common/twoDModel/src/engine/items/imageItem.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ ImageItem::ImageItem(const QSharedPointer<model::Image> &image, const QRect &geo
3535
setY2(geometry.bottom());
3636
setBackgroundRole(false);
3737
unsetCursor();
38+
connect(this, &AbstractItem::mouseInteractionStarted, this, [this](){
39+
mEstimatedPos = pos();
40+
});
3841
}
3942

4043
AbstractItem *ImageItem::clone() const
@@ -255,3 +258,34 @@ QRectF ImageItem::deserializeRect(const QString &string) const
255258

256259
return QRectF();
257260
}
261+
262+
void ImageItem::resizeItem(QGraphicsSceneMouseEvent *event)
263+
{
264+
mEstimatedPos += event->scenePos() - event->lastScenePos();
265+
const auto showGrid = SettingsManager::value("2dShowGrid").toBool();
266+
if (!showGrid || event->modifiers() != Qt::ShiftModifier) {
267+
if (dragState() != None) {
268+
calcResizeItem(event);
269+
} else {
270+
setPos(mEstimatedPos);
271+
}
272+
} else if (dragState() != None) {
273+
setFlag(QGraphicsItem::ItemIsMovable, false);
274+
const auto gridSize = SettingsManager::value("2dGridCellSize").toInt();
275+
const auto x = alignedCoordinate(event->scenePos().x(), gridSize);
276+
const auto y = alignedCoordinate(event->scenePos().y(), gridSize);
277+
setXYWithDragState(mapFromScene(x, y));
278+
} else {
279+
setFlag(QGraphicsItem::ItemIsMovable, false);
280+
// move
281+
setPos(mEstimatedPos);
282+
// and align top left corner to grid
283+
QRectF itemBoundingRect = calcNecessaryBoundingRect();
284+
const auto topLeft = mapToScene(QPointF(itemBoundingRect.left(), itemBoundingRect.top()));
285+
const auto gridSize = SettingsManager::value("2dGridCellSize").toInt();
286+
const auto x = alignedCoordinate(topLeft.x(), gridSize);
287+
const auto y = alignedCoordinate(topLeft.y(), gridSize);
288+
auto delta = QPointF(x, y) - topLeft;
289+
moveBy(delta.x(), delta.y());
290+
}
291+
}

plugins/robots/common/twoDModel/src/engine/items/imageItem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ImageItem : public graphicsUtils::AbstractItem
4747
void drawItem(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
4848
void drawExtractionForItem(QPainter* painter) override;
4949
QPainterPath resizeArea() const override;
50+
void resizeItem(QGraphicsSceneMouseEvent *event) override;
5051

5152
QDomElement serialize(QDomElement &parent) const override;
5253
void deserialize(const QDomElement &element) override;
@@ -94,6 +95,7 @@ class ImageItem : public graphicsUtils::AbstractItem
9495
QSharedPointer<model::Image> mImage;
9596

9697
bool mBackgroundRole {};
98+
QPointF mEstimatedPos;
9799
};
98100

99101
}

plugins/robots/common/twoDModel/src/engine/items/wallItem.cpp

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -256,33 +256,26 @@ void WallItem::resizeWithGrid(QGraphicsSceneMouseEvent *event, int indexGrid)
256256
setY2(y);
257257
reshapeEndWithGrid(indexGrid);
258258
} else {
259-
const int coefX = static_cast<int>(pos().x()) / indexGrid;
260-
const int coefY = static_cast<int>(pos().y()) / indexGrid;
261-
auto newPos = QPointF(alignedCoordinate(pos().x(), coefX, indexGrid),
262-
alignedCoordinate(pos().y(), coefY, indexGrid));
259+
auto newPos = QPointF(alignedCoordinate(pos().x(), indexGrid),
260+
alignedCoordinate(pos().y(), indexGrid));
263261
setPos(newPos);
264262
update();
265263
}
266264
}
267265

268266
void WallItem::reshapeEndWithGrid(int indexGrid)
269267
{
270-
const int coefX = static_cast<int>(x2()) / indexGrid;
271-
const int coefY = static_cast<int>(y2()) / indexGrid;
272-
273-
setX2(alignedCoordinate(x2(), coefX, indexGrid));
274-
setY2(alignedCoordinate(y2(), coefY, indexGrid));
268+
setX2(alignedCoordinate(x2(), indexGrid));
269+
setY2(alignedCoordinate(y2(), indexGrid));
275270

276271
mCellNumbX2 = x2() / indexGrid;
277272
mCellNumbY2 = y2() / indexGrid;
278273
}
279274

280275
void WallItem::reshapeBeginWithGrid(int indexGrid)
281276
{
282-
const int coefX = static_cast<int> (x1()) / indexGrid;
283-
const int coefY = static_cast<int> (y1()) / indexGrid;
284-
setX1(alignedCoordinate(x1(), coefX, indexGrid));
285-
setY1(alignedCoordinate(y1(), coefY, indexGrid));
277+
setX1(alignedCoordinate(x1(), indexGrid));
278+
setY1(alignedCoordinate(y1(), indexGrid));
286279
mCellNumbX1 = x1() / indexGrid;
287280
mCellNumbY1 = y1() / indexGrid;
288281
}
@@ -378,16 +371,3 @@ void WallItem::setDraggedEnd(qreal x, qreal y)
378371
setX2(x1() - x);
379372
setY2(y1() - y);
380373
}
381-
382-
qreal WallItem::alignedCoordinate(qreal coord, int coef, const int indexGrid) const
383-
{
384-
const int coefSign = coef ? coef / qAbs(coef) : 0;
385-
386-
if (qAbs(qAbs(coord) - qAbs(coef) * indexGrid) <= indexGrid / 2.0) {
387-
return coef * indexGrid;
388-
} else if (qAbs(qAbs(coord) - (qAbs(coef) + 1) * indexGrid) <= indexGrid / 2.0) {
389-
return (coef + coefSign) * indexGrid;
390-
}
391-
392-
return coord;
393-
}

plugins/robots/common/twoDModel/src/engine/items/wallItem.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class WallItem : public graphicsUtils::AbstractItem, public SolidItem
7878
private:
7979
void recalculateBorders();
8080

81-
qreal alignedCoordinate(qreal coord, int coef, const int indexGrid) const;
8281
void setBeginCoordinatesWithGrid(int indexGrid);
8382
void setEndCoordinatesWithGrid(int indexGrid);
8483
void countCellNumbCoordinates(int indexGrid);

plugins/robots/common/twoDModel/src/engine/view/scene/twoDModelScene.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,10 @@ void TwoDModelScene::clearScene(bool removeRobot, Reason reason)
784784
worldItemsToDelete << region->id();
785785
}
786786

787+
for (auto &&image : mModel.worldModel().imageItems()) {
788+
worldItemsToDelete << image->id();
789+
}
790+
787791
QList<QPair<model::RobotModel *, kitBase::robotModel::PortInfo>> sensorsToDelete;
788792
QList<qReal::commands::AbstractCommand *> additionalCommands;
789793
for (model::RobotModel *robotModel : mRobots.keys()) {

qrtranslations/fr/plugins/robots/twoDModel_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313
<context>
314314
<name>twoDModel::items::ImageItem</name>
315315
<message>
316-
<location filename="../../../../plugins/robots/common/twoDModel/src/engine/items/imageItem.cpp" line="+75"/>
316+
<location filename="../../../../plugins/robots/common/twoDModel/src/engine/items/imageItem.cpp" line="+78"/>
317317
<source>Image (I)</source>
318318
<translation type="unfinished"></translation>
319319
</message>

qrtranslations/fr/qrutils_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<translation>Division entière par zéro</translation>
8989
</message>
9090
<message>
91-
<location filename="../../qrutils/graphicsUtils/abstractItem.cpp" line="+570"/>
91+
<location filename="../../qrutils/graphicsUtils/abstractItem.cpp" line="+574"/>
9292
<source>Remove</source>
9393
<translation>Supprimer</translation>
9494
</message>

qrtranslations/ru/plugins/robots/twoDModel_ru.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@
589589
<context>
590590
<name>twoDModel::items::ImageItem</name>
591591
<message>
592-
<location filename="../../../../plugins/robots/common/twoDModel/src/engine/items/imageItem.cpp" line="+75"/>
592+
<location filename="../../../../plugins/robots/common/twoDModel/src/engine/items/imageItem.cpp" line="+78"/>
593593
<source>Image (I)</source>
594594
<translation>Картинка (I)</translation>
595595
</message>

qrtranslations/ru/qrutils_ru.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<translation>Целичисленное деление на ноль</translation>
9393
</message>
9494
<message>
95-
<location filename="../../qrutils/graphicsUtils/abstractItem.cpp" line="+570"/>
95+
<location filename="../../qrutils/graphicsUtils/abstractItem.cpp" line="+574"/>
9696
<source>Remove</source>
9797
<translation>Удалить</translation>
9898
</message>

qrutils/graphicsUtils/abstractItem.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,16 @@ void AbstractItem::setDragState(AbstractItem::DragState dragState)
180180

181181
void AbstractItem::calcResizeItem(QGraphicsSceneMouseEvent *event)
182182
{
183-
const qreal x = mapFromScene(event->scenePos()).x();
184-
const qreal y = mapFromScene(event->scenePos()).y();
185183
if (mDragState != None) {
186184
setFlag(QGraphicsItem::ItemIsMovable, false);
187185
}
186+
setXYWithDragState(mapFromScene(event->scenePos()));
187+
}
188188

189+
void AbstractItem::setXYWithDragState(const QPointF pos)
190+
{
191+
const auto x = pos.x();
192+
const auto y = pos.y();
189193
if (mDragState == TopLeft) {
190194
setX1(x);
191195
setY1(y);
@@ -628,3 +632,17 @@ void AbstractItem::copyTo(AbstractItem * const other) const
628632
connect(this, &AbstractItem::brushChanged, other
629633
, QOverload<const QBrush &>::of(&AbstractItem::setBrush));
630634
}
635+
636+
qreal AbstractItem::alignedCoordinate(qreal coord, const int indexGrid) const
637+
{
638+
const int coef = static_cast<int>(coord) / indexGrid;
639+
const int coefSign = coef ? coef / qAbs(coef) : 0;
640+
641+
if (qAbs(qAbs(coord) - qAbs(coef) * indexGrid) <= indexGrid / 2.0) {
642+
return coef * indexGrid;
643+
} else if (qAbs(qAbs(coord) - (qAbs(coef) + 1) * indexGrid) <= indexGrid / 2.0) {
644+
return (coef + coefSign) * indexGrid;
645+
}
646+
647+
return coord;
648+
}

0 commit comments

Comments
 (0)