Refactor: Abstract dimension input and finalize creation logic to SketchTool
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -16,23 +16,20 @@ RectangleTool::RectangleTool(ViewportWidget* viewport)
|
||||
|
||||
void RectangleTool::activate()
|
||||
{
|
||||
m_isDefiningRectangle = false;
|
||||
SketchTool::activate();
|
||||
m_dimensionModes << "height" << "width";
|
||||
m_dimensionPropertyNames["height"] = "heightInput";
|
||||
m_dimensionPropertyNames["width"] = "widthInput";
|
||||
|
||||
m_viewport->setProperty("widthInput", "");
|
||||
m_viewport->setProperty("heightInput", "");
|
||||
m_viewport->setProperty("dimensionEditMode", "height");
|
||||
}
|
||||
|
||||
void RectangleTool::deactivate()
|
||||
{
|
||||
m_isDefiningRectangle = false;
|
||||
m_viewport->setProperty("widthInput", "");
|
||||
m_viewport->setProperty("heightInput", "");
|
||||
}
|
||||
|
||||
void RectangleTool::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
gp_Pnt p;
|
||||
if (!m_isDefiningRectangle) {
|
||||
if (!m_isDefining) {
|
||||
if (m_viewport->isSnappingOrigin()) {
|
||||
p.SetCoord(0, 0, 0);
|
||||
} else if (m_viewport->isSnappingVertex()) {
|
||||
@@ -42,7 +39,7 @@ void RectangleTool::mousePressEvent(QMouseEvent *event)
|
||||
p.SetCoord(worldPos.x(), worldPos.y(), worldPos.z());
|
||||
}
|
||||
m_firstRectanglePoint = p;
|
||||
m_isDefiningRectangle = true;
|
||||
m_isDefining = true;
|
||||
m_viewport->setProperty("widthInput", "");
|
||||
m_viewport->setProperty("heightInput", "");
|
||||
m_viewport->setProperty("dimensionEditMode", "height");
|
||||
@@ -116,116 +113,71 @@ void RectangleTool::mouseMoveEvent(QMouseEvent *event)
|
||||
// To be implemented
|
||||
}
|
||||
|
||||
void RectangleTool::keyPressEvent(QKeyEvent *event)
|
||||
void RectangleTool::finalizeCreation()
|
||||
{
|
||||
if (m_isDefiningRectangle) {
|
||||
if (event->key() == Qt::Key_Tab) {
|
||||
QString currentMode = m_viewport->property("dimensionEditMode").toString();
|
||||
if (currentMode == "width") {
|
||||
m_viewport->setProperty("dimensionEditMode", "height");
|
||||
} else {
|
||||
m_viewport->setProperty("dimensionEditMode", "width");
|
||||
}
|
||||
m_viewport->update();
|
||||
return;
|
||||
}
|
||||
QVector3D worldPos;
|
||||
QVector3D startPos(m_firstRectanglePoint.X(), m_firstRectanglePoint.Y(), m_firstRectanglePoint.Z());
|
||||
|
||||
QString editMode = m_viewport->property("dimensionEditMode").toString();
|
||||
const char* propertyName = (editMode == "width") ? "widthInput" : "heightInput";
|
||||
QString currentInput = m_viewport->property(propertyName).toString();
|
||||
QString widthInput = m_viewport->property("widthInput").toString();
|
||||
QString heightInput = m_viewport->property("heightInput").toString();
|
||||
bool widthFromInput = false, heightFromInput = false;
|
||||
double inputWidth = 0, inputHeight = 0;
|
||||
|
||||
if (event->key() >= Qt::Key_0 && event->key() <= Qt::Key_9) {
|
||||
currentInput += event->text();
|
||||
m_viewport->setProperty(propertyName, currentInput);
|
||||
m_viewport->update();
|
||||
return;
|
||||
} else if (event->key() == Qt::Key_Period) {
|
||||
if (!currentInput.contains('.')) {
|
||||
currentInput += '.';
|
||||
m_viewport->setProperty(propertyName, currentInput);
|
||||
m_viewport->update();
|
||||
}
|
||||
return;
|
||||
} else if (event->key() == Qt::Key_Backspace) {
|
||||
if (!currentInput.isEmpty()) {
|
||||
currentInput.chop(1);
|
||||
m_viewport->setProperty(propertyName, currentInput);
|
||||
m_viewport->update();
|
||||
}
|
||||
return;
|
||||
} else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
|
||||
QVector3D worldPos;
|
||||
QVector3D startPos(m_firstRectanglePoint.X(), m_firstRectanglePoint.Y(), m_firstRectanglePoint.Z());
|
||||
|
||||
QString widthInput = m_viewport->property("widthInput").toString();
|
||||
QString heightInput = m_viewport->property("heightInput").toString();
|
||||
bool widthFromInput = false, heightFromInput = false;
|
||||
double inputWidth = 0, inputHeight = 0;
|
||||
|
||||
if (!widthInput.isEmpty()) {
|
||||
bool ok;
|
||||
inputWidth = widthInput.toDouble(&ok);
|
||||
if (ok) widthFromInput = true;
|
||||
}
|
||||
if (!heightInput.isEmpty()) {
|
||||
bool ok;
|
||||
inputHeight = heightInput.toDouble(&ok);
|
||||
if (ok) heightFromInput = true;
|
||||
}
|
||||
|
||||
if (widthFromInput || heightFromInput) {
|
||||
QVector3D mousePos = m_viewport->unproject(m_viewport->currentMousePos(), m_viewport->currentPlane());
|
||||
QVector3D mouseDir = mousePos - startPos;
|
||||
double current_w, current_h;
|
||||
if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XY) {
|
||||
current_w = qAbs(mouseDir.x());
|
||||
current_h = qAbs(mouseDir.z());
|
||||
} else if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XZ) {
|
||||
current_w = qAbs(mouseDir.x());
|
||||
current_h = qAbs(mouseDir.y());
|
||||
} else { // YZ
|
||||
current_w = qAbs(mouseDir.y());
|
||||
current_h = qAbs(mouseDir.z());
|
||||
}
|
||||
double rect_w = widthFromInput ? inputWidth : current_w;
|
||||
double rect_h = heightFromInput ? inputHeight : current_h;
|
||||
int signX = (mouseDir.x() >= 0) ? 1 : -1;
|
||||
int signY = (mouseDir.y() >= 0) ? 1 : -1;
|
||||
int signZ = (mouseDir.z() >= 0) ? 1 : -1;
|
||||
worldPos = startPos;
|
||||
if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XY) {
|
||||
worldPos.setX(startPos.x() + signX * rect_w);
|
||||
worldPos.setZ(startPos.z() + signZ * rect_h);
|
||||
} else if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XZ) {
|
||||
worldPos.setX(startPos.x() + signX * rect_w);
|
||||
worldPos.setY(startPos.y() + signY * rect_h);
|
||||
} else { // YZ
|
||||
worldPos.setY(startPos.y() + signY * rect_w);
|
||||
worldPos.setZ(startPos.z() + signZ * rect_h);
|
||||
}
|
||||
} else {
|
||||
worldPos = m_viewport->unproject(m_viewport->currentMousePos(), m_viewport->currentPlane());
|
||||
}
|
||||
|
||||
gp_Pnt p;
|
||||
p.SetCoord(worldPos.x(), worldPos.y(), worldPos.z());
|
||||
|
||||
emit m_viewport->rectangleAdded(m_firstRectanglePoint, p);
|
||||
deactivate();
|
||||
m_viewport->update();
|
||||
return;
|
||||
} else if (event->key() == Qt::Key_Escape) {
|
||||
deactivate();
|
||||
m_viewport->deactivateActiveTool();
|
||||
m_viewport->update();
|
||||
return;
|
||||
}
|
||||
if (!widthInput.isEmpty()) {
|
||||
bool ok;
|
||||
inputWidth = widthInput.toDouble(&ok);
|
||||
if (ok) widthFromInput = true;
|
||||
}
|
||||
if (!heightInput.isEmpty()) {
|
||||
bool ok;
|
||||
inputHeight = heightInput.toDouble(&ok);
|
||||
if (ok) heightFromInput = true;
|
||||
}
|
||||
|
||||
if (widthFromInput || heightFromInput) {
|
||||
QVector3D mousePos = m_viewport->unproject(m_viewport->currentMousePos(), m_viewport->currentPlane());
|
||||
QVector3D mouseDir = mousePos - startPos;
|
||||
double current_w, current_h;
|
||||
if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XY) {
|
||||
current_w = qAbs(mouseDir.x());
|
||||
current_h = qAbs(mouseDir.z());
|
||||
} else if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XZ) {
|
||||
current_w = qAbs(mouseDir.x());
|
||||
current_h = qAbs(mouseDir.y());
|
||||
} else { // YZ
|
||||
current_w = qAbs(mouseDir.y());
|
||||
current_h = qAbs(mouseDir.z());
|
||||
}
|
||||
double rect_w = widthFromInput ? inputWidth : current_w;
|
||||
double rect_h = heightFromInput ? inputHeight : current_h;
|
||||
int signX = (mouseDir.x() >= 0) ? 1 : -1;
|
||||
int signY = (mouseDir.y() >= 0) ? 1 : -1;
|
||||
int signZ = (mouseDir.z() >= 0) ? 1 : -1;
|
||||
worldPos = startPos;
|
||||
if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XY) {
|
||||
worldPos.setX(startPos.x() + signX * rect_w);
|
||||
worldPos.setZ(startPos.z() + signZ * rect_h);
|
||||
} else if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XZ) {
|
||||
worldPos.setX(startPos.x() + signX * rect_w);
|
||||
worldPos.setY(startPos.y() + signY * rect_h);
|
||||
} else { // YZ
|
||||
worldPos.setY(startPos.y() + signY * rect_w);
|
||||
worldPos.setZ(startPos.z() + signZ * rect_h);
|
||||
}
|
||||
} else {
|
||||
worldPos = m_viewport->unproject(m_viewport->currentMousePos(), m_viewport->currentPlane());
|
||||
}
|
||||
|
||||
gp_Pnt p;
|
||||
p.SetCoord(worldPos.x(), worldPos.y(), worldPos.z());
|
||||
|
||||
emit m_viewport->rectangleAdded(m_firstRectanglePoint, p);
|
||||
deactivate();
|
||||
}
|
||||
|
||||
void RectangleTool::paintGL()
|
||||
{
|
||||
if (m_isDefiningRectangle) {
|
||||
if (m_isDefining) {
|
||||
QVector<GLfloat> vertices;
|
||||
QVector3D worldPos;
|
||||
QVector3D startPos(m_firstRectanglePoint.X(), m_firstRectanglePoint.Y(), m_firstRectanglePoint.Z());
|
||||
@@ -320,7 +272,7 @@ void RectangleTool::paintGL()
|
||||
|
||||
void RectangleTool::paint2D(QPainter& painter, const QMatrix4x4& modelView, const QMatrix4x4& projection)
|
||||
{
|
||||
if (m_isDefiningRectangle) {
|
||||
if (m_isDefining) {
|
||||
QVector3D worldPos;
|
||||
QVector3D p1_3d(m_firstRectanglePoint.X(), m_firstRectanglePoint.Y(), m_firstRectanglePoint.Z());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user