Skip to content

Commit d037f28

Browse files
committed
Round the numbers to grid size
1 parent 997610d commit d037f28

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8519,43 +8519,48 @@ void ModelWidgetContainer::fitToDiagram()
85198519
QRect diagramRect = pGraphicsView->itemsBoundingRect().toAlignedRect();
85208520
diagramRect = pGraphicsView->mapToScene(diagramRect).boundingRect().toRect();
85218521
// invert the rectangle as the drawing area has scale(1.0, -1.0);
8522-
int top = diagramRect.top();
8522+
const int top = diagramRect.top();
85238523
diagramRect.setTop(diagramRect.bottom());
85248524
diagramRect.setBottom(top);
8525-
// Make the extent values interval of 10
8526-
int interval = 10;
8527-
diagramRect.setLeft(((diagramRect.left() / interval) * interval) - interval);
8528-
diagramRect.setBottom(((diagramRect.bottom() / interval) * interval) - interval);
8529-
diagramRect.setRight(((diagramRect.right() / interval) * interval) + interval);
8530-
diagramRect.setTop(((diagramRect.top() / interval) * interval) + interval);
8525+
// Make the extent values interval of 10 based on grid size
8526+
const int xInterval = qRound(pGraphicsView->mMergedCoOrdinateSystem.getHorizontalGridStep()) * 10;
8527+
const int yInterval = qRound(pGraphicsView->mMergedCoOrdinateSystem.getVerticalGridStep()) * 10;
8528+
const int left = qRound((double)diagramRect.left() / xInterval) * xInterval;
8529+
const int bottom = qRound((double)diagramRect.bottom() / yInterval) * yInterval;
8530+
const int right = qRound((double)diagramRect.right() / xInterval) * xInterval;
8531+
const int top_ = qRound((double)diagramRect.top() / yInterval) * yInterval;
8532+
QRectF adaptedRect(left, bottom, qAbs(left - right), qAbs(bottom - top_));
85318533
// For read-only system libraries we just set the zoom and for writeable models we modify the extent.
85328534
if (pModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
85338535
pGraphicsView->setIsCustomScale(true);
85348536
pGraphicsView->fitInView(diagramRect, Qt::KeepAspectRatio);
85358537
} else {
8536-
// CoOrdinateSystem
8537-
CoOrdinateSystem oldCoOrdinateSystem = pGraphicsView->getCoOrdinateSystem();
8538-
// version
8539-
QString oldVersion = pModelWidget->getLibraryTreeItem()->mClassInformation.version;
8540-
// uses annotation
8541-
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
8542-
QList<QList<QString> > usesAnnotation = pOMCProxy->getUses(pModelWidget->getLibraryTreeItem()->getNameStructure());
8543-
QStringList oldUsesAnnotation;
8544-
for (int i = 0 ; i < usesAnnotation.size() ; i++) {
8545-
oldUsesAnnotation.append(QString("%1(version=\"%2\")").arg(usesAnnotation.at(i).at(0)).arg(usesAnnotation.at(i).at(1)));
8546-
}
8547-
QString oldUsesAnnotationString = QString("annotate=$annotation(uses(%1))").arg(oldUsesAnnotation.join(","));
8548-
// construct a new CoOrdinateSystem
8549-
CoOrdinateSystem newCoOrdinateSystem = oldCoOrdinateSystem;
8550-
newCoOrdinateSystem.setLeft(diagramRect.left());
8551-
newCoOrdinateSystem.setBottom(diagramRect.bottom());
8552-
newCoOrdinateSystem.setRight(diagramRect.right());
8553-
newCoOrdinateSystem.setTop(diagramRect.top());
8554-
// push the CoOrdinateSystem change to undo stack
8555-
UpdateCoOrdinateSystemCommand *pUpdateCoOrdinateSystemCommand = new UpdateCoOrdinateSystemCommand(pGraphicsView, oldCoOrdinateSystem, newCoOrdinateSystem, false,
8556-
oldVersion, oldVersion, oldUsesAnnotationString, oldUsesAnnotationString);
8557-
pModelWidget->getUndoStack()->push(pUpdateCoOrdinateSystemCommand);
8558-
pModelWidget->updateModelText();
8538+
// avoid putting unnecessary commands on the stack
8539+
if (adaptedRect.width() != 0 && adaptedRect.height() != 0 && adaptedRect != pGraphicsView->mMergedCoOrdinateSystem.getExtentRectangle()) {
8540+
// CoOrdinateSystem
8541+
CoOrdinateSystem oldCoOrdinateSystem = pGraphicsView->getCoOrdinateSystem();
8542+
// version
8543+
QString oldVersion = pModelWidget->getLibraryTreeItem()->mClassInformation.version;
8544+
// uses annotation
8545+
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
8546+
QList<QList<QString> > usesAnnotation = pOMCProxy->getUses(pModelWidget->getLibraryTreeItem()->getNameStructure());
8547+
QStringList oldUsesAnnotation;
8548+
for (int i = 0 ; i < usesAnnotation.size() ; i++) {
8549+
oldUsesAnnotation.append(QString("%1(version=\"%2\")").arg(usesAnnotation.at(i).at(0)).arg(usesAnnotation.at(i).at(1)));
8550+
}
8551+
QString oldUsesAnnotationString = QString("annotate=$annotation(uses(%1))").arg(oldUsesAnnotation.join(","));
8552+
// construct a new CoOrdinateSystem
8553+
CoOrdinateSystem newCoOrdinateSystem = oldCoOrdinateSystem;
8554+
newCoOrdinateSystem.setLeft(adaptedRect.left());
8555+
newCoOrdinateSystem.setBottom(adaptedRect.bottom());
8556+
newCoOrdinateSystem.setRight(adaptedRect.right());
8557+
newCoOrdinateSystem.setTop(adaptedRect.top());
8558+
// push the CoOrdinateSystem change to undo stack
8559+
UpdateCoOrdinateSystemCommand *pUpdateCoOrdinateSystemCommand = new UpdateCoOrdinateSystemCommand(pGraphicsView, oldCoOrdinateSystem, newCoOrdinateSystem, false,
8560+
oldVersion, oldVersion, oldUsesAnnotationString, oldUsesAnnotationString);
8561+
pModelWidget->getUndoStack()->push(pUpdateCoOrdinateSystemCommand);
8562+
pModelWidget->updateModelText();
8563+
}
85598564
}
85608565
// hide the progressbar and clear the message in status bar
85618566
MainWindow::instance()->getStatusBar()->clearMessage();

0 commit comments

Comments
 (0)