File size: 5,519 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
/*
 * ********************************************************************************
 * 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 "lc_imagepropertieseditingwidget.h"

#include <QFileInfo>

#include "rs_dialogfactory.h"
#include "rs_dialogfactoryinterface.h"
#include "rs_image.h"
#include "rs_units.h"
#include "ui_lc_imagepropertieseditingwidget.h"

#define ALLOW_PICK_SCALE false

LC_ImagePropertiesEditingWidget::LC_ImagePropertiesEditingWidget(QWidget *parent)
    : LC_EntityPropertiesEditorWidget(parent)
    , ui(new Ui::LC_ImagePropertiesEditingWidget){
    ui->setupUi(this);
    connect(ui->leInsertX, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onInsertionPointEditingFinished);
    connect(ui->leInsertY, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onInsertionPointEditingFinished);
    connect(ui->leAngle, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onAngleEditingFinished);
    connect(ui->leWidth, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onWidthChanged);
    connect(ui->leHeight, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onHeightChanged);
    connect(ui->leScale, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onScaleChanged);
    connect(ui->leDPI, &QLineEdit::editingFinished, this, &LC_ImagePropertiesEditingWidget::onDPIChanged);
    connect(ui->pbFile, &QPushButton::toggled, this,&LC_ImagePropertiesEditingWidget::onImageFileClick);
    connect(ui->lePath, &QLineEdit::textChanged, this, &LC_ImagePropertiesEditingWidget::onPathChanged);
    if (!ALLOW_PICK_SCALE) {
        ui->tbPickScale->setVisible(false);
    }
}

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

void LC_ImagePropertiesEditingWidget::setEntity(RS_Entity* entity) {
    m_entity = static_cast<RS_Image*>(entity);
}


void LC_ImagePropertiesEditingWidget::onWidthChanged() {
    double width = toWCSValue(ui->leWidth, m_entity->getWidth());
    m_scale = width / m_entity->getWidth();

    toUIValue(m_entity->getHeight()*m_scale, ui->leHeight);
    toUIValue(m_scale, ui->leScale);
}

void LC_ImagePropertiesEditingWidget::onHeightChanged() {
    double height = toWCSValue(ui->leHeight, m_entity->getHeight());
    m_scale = height / m_entity->getHeight();

    toUIValue(m_entity->getWidth()*m_scale, ui->leWidth);
    toUIValue(m_scale, ui->leScale);
}

void LC_ImagePropertiesEditingWidget::onScaleChanged() {
    m_scale = toWCSValue(ui->leScale, m_scale);
    toUIValue(m_entity->getWidth()*m_scale, ui->leWidth);
    toUIValue(m_entity->getHeight()*m_scale, ui->leHeight);
    toUIValue(RS_Units::scaleToDpi(m_scale, m_entity->getGraphicUnit()), ui->leDPI);
}

void LC_ImagePropertiesEditingWidget::onDPIChanged(){
    double oldDpi = RS_Units::scaleToDpi(m_scale, m_entity->getGraphicUnit()); // todo - what if scale was changed? Save dpi in dlg?
    double dpi = toWCSValue(ui->leDPI, oldDpi);
    m_scale = RS_Units::dpiToScale(dpi, m_entity->getGraphicUnit());
    toUIValue(m_scale, ui->leScale);
    toUIValue(m_entity->getWidth()*m_scale, ui->leWidth);
    toUIValue(m_entity->getHeight()*m_scale, ui->leHeight);
}

void LC_ImagePropertiesEditingWidget::onInsertionPointEditingFinished() {
    m_entity->setInsertionPoint(toWCS(ui->leInsertX, ui->leInsertY, m_entity->getInsertionPoint()));
}

void LC_ImagePropertiesEditingWidget::onAngleEditingFinished() {
    double orgScale = m_entity->getUVector().magnitude();
    m_scale /= orgScale;
    double orgAngle = m_entity->getUVector().angle();
    double angle = toWCSAngle(ui->leAngle, orgAngle);
    m_entity->scale(m_entity->getInsertionPoint(), RS_Vector(m_scale, m_scale));
    m_entity->rotate(m_entity->getInsertionPoint(), angle - orgAngle);
}

void LC_ImagePropertiesEditingWidget::onImageFileClick() {
    ui->lePath->setText(RS_DIALOGFACTORY->requestImageOpenDialog()); // fixme - is it bad dependency?
}

void LC_ImagePropertiesEditingWidget::onPathChanged(const QString&) {
    auto text = ui->lePath->text().trimmed();
    if (QFileInfo(text).isFile()) {
        m_entity->setFile(text);
    }
}

void LC_ImagePropertiesEditingWidget::setupInteractiveInputWidgets() {
    pickPointSetup(ui->wPickInsertionPoint, "insert", ui->leInsertX, ui->leInsertY);
    pickDistanceSetup(ui->tbPickWidth, "width", ui->leWidth);
    pickDistanceSetup(ui->tbPickHeight, "height", ui->leHeight);
    pickAngleSetup(ui->tbPickAngle, "angle", ui->leAngle);
    if (!ALLOW_PICK_SCALE) {
        pickDistanceSetup(ui->tbPickScale, "scale", ui->leScale);
    }
}