File size: 5,493 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * ********************************************************************************
 * This file is part of the LibreCAD project, a 2D CAD program
 *
 * Copyright (C) 2025 LibreCAD.org
 * Copyright (C) 2025 sand1024
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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 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 <limits>
#include <QDoubleValidator>

#include "lc_circlepropertieseditingwidget.h"

#include "rs_circle.h"
#include "ui_lc_circlepropertieseditingwidget.h"

LC_CirclePropertiesEditingWidget::LC_CirclePropertiesEditingWidget(QWidget* parent)
    : LC_EntityPropertiesEditorWidget(parent)
      , ui(new Ui::LC_CirclePropertiesEditingWidget) {
    ui->setupUi(this);

    // Set up validators for input fields
    auto coordValidator = new QDoubleValidator(this);
    ui->leCenterX->setValidator(coordValidator);
    ui->leCenterY->setValidator(coordValidator);

    auto positiveValidator = new QDoubleValidator(0.0, std::numeric_limits<double>::max(), 10, this);
    ui->leRadius->setValidator(positiveValidator);
    ui->leDiameter->setValidator(positiveValidator);

    connect(ui->leCenterX, &QLineEdit::editingFinished, this, &LC_CirclePropertiesEditingWidget::onCenterEditingFinished);
    connect(ui->leCenterY, &QLineEdit::editingFinished, this, &LC_CirclePropertiesEditingWidget::onCenterEditingFinished);
    connect(ui->leRadius, &QLineEdit::editingFinished, this, &LC_CirclePropertiesEditingWidget::onRadiusEditingFinished);
    connect(ui->leDiameter, &QLineEdit::editingFinished, this, &LC_CirclePropertiesEditingWidget::onDiameterEditingFinished);
    connect(ui->leRadius, &QLineEdit::textChanged, this, &LC_CirclePropertiesEditingWidget::onRadiusTextChanged);
    connect(ui->leDiameter, &QLineEdit::textChanged, this, &LC_CirclePropertiesEditingWidget::onDiameterTextChanged);
}

LC_CirclePropertiesEditingWidget::~LC_CirclePropertiesEditingWidget(){
    delete ui;
}

void LC_CirclePropertiesEditingWidget::setEntity(RS_Entity* entity) {
    m_entity = static_cast<RS_Circle*>(entity);

    ui->leRadius->blockSignals(true);
    ui->leDiameter->blockSignals(true);
    toUI(m_entity->getCenter(), ui->leCenterX, ui->leCenterY);
    toUIValue(m_entity->getRadius(), ui->leRadius);
    toUIValue(m_entity->getRadius() * 2, ui->leDiameter);
    ui->leRadius->blockSignals(false);
    ui->leDiameter->blockSignals(false);
}

void LC_CirclePropertiesEditingWidget::onCenterEditingFinished() {
    m_entity->setCenter(toWCS(ui->leCenterX, ui->leCenterY, m_entity->getCenter()));
}

void LC_CirclePropertiesEditingWidget::onRadiusEditingFinished() {
    double newRadius = toWCSValue(ui->leRadius, m_entity->getRadius());
    if (newRadius > 0.0) {  // Ensure positive radius
        m_entity->setRadius(newRadius);
        ui->leDiameter->blockSignals(true);
        toUIValue(newRadius * 2, ui->leDiameter);
        ui->leDiameter->blockSignals(false);
    } else {
        // Revert to current value if invalid
        toUIValue(m_entity->getRadius(), ui->leRadius);
    }
}

void LC_CirclePropertiesEditingWidget::onDiameterEditingFinished() {
    double newDiameter = toWCSValue(ui->leDiameter, m_entity->getRadius() * 2);
    double newRadius = newDiameter / 2.0;
    if (newRadius > 0.0) {  // Ensure positive radius
        m_entity->setRadius(newRadius);
        ui->leRadius->blockSignals(true);
        toUIValue(newRadius, ui->leRadius);
        ui->leRadius->blockSignals(false);
    } else {
        // Revert to current value if invalid
        toUIValue(m_entity->getRadius() * 2, ui->leDiameter);
    }
}

void LC_CirclePropertiesEditingWidget::onRadiusTextChanged(const QString &text) {
    int pos = 0;
    if (ui->leRadius->validator()->validate(const_cast<QString&>(text), pos) == QValidator::Acceptable) {
        bool ok;
        double value = text.toDouble(&ok);
        if (ok && value > 0.0) {
            ui->leDiameter->blockSignals(true);
            toUIValue(value * 2, ui->leDiameter);
            ui->leDiameter->blockSignals(false);
        }
    }
}

void LC_CirclePropertiesEditingWidget::onDiameterTextChanged(const QString &text) {
    int pos = 0;
    if (ui->leDiameter->validator()->validate(const_cast<QString&>(text), pos) == QValidator::Acceptable) {
        bool ok;
        double value = text.toDouble(&ok);
        if (ok && value > 0.0) {
            ui->leRadius->blockSignals(true);
            toUIValue(value / 2.0, ui->leRadius);
            ui->leRadius->blockSignals(false);
        }
    }
}

void LC_CirclePropertiesEditingWidget::setupInteractiveInputWidgets() {
    pickPointSetup(ui->wPickPointCenter, "center", ui->leCenterX, ui->leCenterY);
    pickDistanceSetup(ui->tbPickRadius, "radius", ui->leRadius);
    pickDistanceSetup(ui->tbPickDiameter, "diameter", ui->leDiameter);
}