File size: 6,653 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
 *
 This file is part of the LibreCAD project, a 2D CAD program

 Copyright (C) 2024 LibreCAD.org
 Copyright (C) 2024 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_modifyalignoptions.h"
#include "lc_actionmodifyalign.h"
#include "ui_lc_modifyalignoptions.h"

LC_ModifyAlignOptions::LC_ModifyAlignOptions()
    : LC_ActionOptionsWidget()
    , ui(new Ui::LC_ModifyAlignOptions){
    ui->setupUi(this);

    connect(ui->cbAsGroup, &QCheckBox::toggled, this, &LC_ModifyAlignOptions::onAsGroupChanged);
    connect(ui->tbVAlignTop, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onVAlignChanged);
    connect(ui->tbVAlignMiddle, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onVAlignChanged);
    connect(ui->tbVAlignBottom, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onVAlignChanged);
    connect(ui->tbValignNone, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onVAlignChanged);
    connect(ui->tbHAlignLeft, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onHAlignChanged);
    connect(ui->tbHalignMiddle, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onHAlignChanged);
    connect(ui->tbHalignRight, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onHAlignChanged);
    connect(ui->tbHAlignNone, &QToolButton::toggled, this, &LC_ModifyAlignOptions::onHAlignChanged);
    connect(ui->cbAlignTo, &QComboBox::currentIndexChanged, this, &LC_ModifyAlignOptions::onAlignToIndexChanged);
}

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

bool LC_ModifyAlignOptions::checkActionRttiValid(RS2::ActionType actionType) {
    m_forAlignAction = actionType == RS2::ActionModifyAlign;
    return m_forAlignAction || (actionType == RS2::ActionModifyAlignOne);
}

QString LC_ModifyAlignOptions::getSettingsGroupName() {
    return "Modify";
}

QString LC_ModifyAlignOptions::getSettingsOptionNamePrefix() {
    return (m_forAlignAction ? "Align":"AlignOne");
}

void LC_ModifyAlignOptions::doSaveSettings() {
    int halign = getHAlignFromUI();
    int valign = getVAlignFromUI();
    save("HAlign", halign);
    save("VAlign", valign);
    save("AlignTo", ui->cbAlignTo->currentIndex());
    if (m_forAlignAction) {
        save("AsGroup", ui->cbAsGroup->isChecked());
    }
}

void LC_ModifyAlignOptions::doSetAction(RS_ActionInterface *a, bool update) {
    m_action = dynamic_cast<LC_ActionModifyAlignData *>(a);
    int valign;
    int halign;
    int alignType;
    bool asGroup;
    if (update){
        valign = m_action->getVAlign();
        halign = m_action->getHAlign();
        alignType = m_action->getAlignType();
        asGroup = m_action->isAsGroup();
    }
    else{
        halign = loadInt("HAlign", 0);
        valign = loadInt("VAlign", 0);
        alignType = loadInt("AlignTo", 0);
        asGroup = loadBool("AsGroup", true);
    }
    if (m_forAlignAction) {
        setAsGroupToActionAndView(asGroup);
    }
    ui->cbAsGroup->setVisible(m_forAlignAction);
    setHAlignToActionAndView(halign);
    setVAlignToActionAndView(valign);
    setAlignTypeToActionAndView(alignType);
}

void LC_ModifyAlignOptions::onAsGroupChanged([[maybe_unused]]bool val) {
    setAsGroupToActionAndView(ui->cbAsGroup->isChecked());
}

void LC_ModifyAlignOptions::onVAlignChanged([[maybe_unused]]bool val) {
    int valign = getVAlignFromUI();
    setVAlignToActionAndView(valign);
}

void LC_ModifyAlignOptions::onHAlignChanged([[maybe_unused]]bool val) {
    int halign = getHAlignFromUI();
    setHAlignToActionAndView(halign);
}

void LC_ModifyAlignOptions::onAlignToIndexChanged([[maybe_unused]]int idx) {
    setAlignTypeToActionAndView(ui->cbAlignTo->currentIndex());
}

void LC_ModifyAlignOptions::languageChange() {
    ui->retranslateUi(this);
}

void LC_ModifyAlignOptions::setAlignTypeToActionAndView(int type) {
    m_action->setAlignType(type);
    ui->cbAlignTo->setCurrentIndex(type);
}

void LC_ModifyAlignOptions::setVAlignToActionAndView(int valign) {
    switch (valign){
      case LC_Align::Align::LEFT_TOP:{
            ui->tbVAlignTop->setChecked(true);
            break;
        }
        case LC_Align::Align::MIDDLE:{
            ui->tbVAlignMiddle->setChecked(true);
            break;
        }
        case LC_Align::Align::RIGHT_BOTTOM:{
            ui->tbVAlignBottom->setChecked(true);
            break;
        }
        default:
            ui->tbValignNone->setChecked(true);
    }
    m_action->setVAlign(valign);
}

void LC_ModifyAlignOptions::setHAlignToActionAndView(int halign) {
    switch (halign){
        case LC_Align::Align::LEFT_TOP:{
            ui->tbHAlignLeft->setChecked(true);
            break;
        }
        case LC_Align::Align::MIDDLE:{
            ui->tbHalignMiddle->setChecked(true);
            break;
        }
        case LC_Align::Align::RIGHT_BOTTOM:{
            ui->tbHalignRight->setChecked(true);
            break;
        }
        default:
            ui->tbHAlignNone->setChecked(true);
    }
    m_action->setHAlign(halign);
}

int LC_ModifyAlignOptions::getHAlignFromUI() {
    if (ui->tbHAlignLeft->isChecked()) {
        return LC_Align::Align::LEFT_TOP;
    }
    if (ui->tbHalignMiddle->isChecked()) {
        return LC_Align::Align::MIDDLE;
    }
    if (ui->tbHalignRight->isChecked()) {
        return LC_Align::Align::RIGHT_BOTTOM;
    }
    return LC_Align::Align::NONE;
}

int LC_ModifyAlignOptions::getVAlignFromUI() {
    if (ui->tbVAlignTop->isChecked()) {
        return LC_Align::Align::LEFT_TOP;
    }
    if (ui->tbVAlignMiddle->isChecked()) {
        return LC_Align::Align::MIDDLE;
    }
    if (ui->tbVAlignBottom->isChecked()) {
        return LC_Align::Align::RIGHT_BOTTOM;
    }
    return LC_Align::Align::NONE;
}

void LC_ModifyAlignOptions::setAsGroupToActionAndView(bool group) {
    m_action->setAsGroup(group);
    ui->cbAsGroup->setChecked(group);
}