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

#include "qg_colorbox.h"
#include "qg_widthbox.h"
#include "qg_linetypebox.h"
#include "rs_graphic.h"
#include "rs_graphicview.h"
#include "rs_layer.h"
#include "rs_pen.h"

/**
 * Constructor.
 */
QG_PenToolBar::QG_PenToolBar( const QString & title, QWidget * parent )
		: QToolBar(title, parent)
		, m_currentPen(new RS_Pen{})
		, m_colorBox(new QG_ColorBox{true, false, this, "colorbox"})
		, m_widthBox(new QG_WidthBox{true, false, this, "widthbox"})
		, m_lineTypeBox(new QG_LineTypeBox{true, false, this, "lineTypebox"})
{
    m_colorBox->setToolTip(tr("Line color"));
    connect(m_colorBox.get(), &QG_ColorBox::colorChanged, this, &QG_PenToolBar::slotColorChanged);

    m_widthBox->setToolTip(tr("Line width"));
    connect(m_widthBox.get(), &QG_WidthBox::widthChanged,this, &QG_PenToolBar::slotWidthChanged);

    m_lineTypeBox->setToolTip(tr("Line type"));
    connect(m_lineTypeBox.get(), &QG_LineTypeBox::lineTypeChanged,this,&QG_PenToolBar::slotLineTypeChanged);

    m_currentPen->setColor(m_colorBox->getColor());
    m_currentPen->setWidth(m_widthBox->getWidth());
    m_currentPen->setLineType(m_lineTypeBox->getLineType());

    addWidget(m_colorBox.get());
    addWidget(m_widthBox.get());
    addWidget(m_lineTypeBox.get());
}

/**
 * Destructor
 */
QG_PenToolBar::~QG_PenToolBar() = default;

void QG_PenToolBar::updateByLayer(RS_Layer* l) {
    if (l==nullptr) return;
    m_colorBox->setLayerColor(l->getPen().getColor());
    m_widthBox->setLayerWidth(l->getPen().getWidth());
    m_lineTypeBox->setLayerLineType(l->getPen().getLineType());
}

/**
 * Called by the layer list if this object was added as a listener
 * to a layer list.
 */
void QG_PenToolBar::layerActivated(RS_Layer* l) {
    updateByLayer(l);
}

void QG_PenToolBar::setLayerColor(RS_Color color, bool updateSelection){
    m_colorBox->setLayerColor(color);
    if (updateSelection){
        m_colorBox->setCurrentIndex(0);
    }
    m_currentPen->setColor(color);
    emitPenChanged();
}

void QG_PenToolBar::setColor(RS_Color color){
    m_colorBox->setColor(color);
}

void QG_PenToolBar::setLayerLineType(RS2::LineType lineType, bool updateSelection){
    m_lineTypeBox->setLayerLineType(lineType);
    if (updateSelection){
        m_lineTypeBox->setCurrentIndex(0);
    }
    m_currentPen->setLineType(lineType);
    emitPenChanged();
}

void QG_PenToolBar::setLineType(RS2::LineType lineType){
    m_lineTypeBox->setLineType(lineType);
}

void QG_PenToolBar::setLayerWidth(RS2::LineWidth width, bool updateSelection){
    m_widthBox->setLayerWidth(width);
    if (updateSelection){
        m_widthBox->setCurrentIndex(0);
    }
    m_currentPen->setWidth(width);
    emitPenChanged();
}

void QG_PenToolBar::setWidth(RS2::LineWidth width){
    m_widthBox->setWidth(width);
}

void QG_PenToolBar::emitPenChanged(){
    emit penChanged(*m_currentPen);
}

void QG_PenToolBar::setLayerList(RS_LayerList* ll) {
    if (m_layerList != nullptr) {
        m_layerList->removeListener(this);
    }
    m_layerList = ll;
    if (ll != nullptr) {
        m_layerList->addListener(this);
        auto activeLayer = m_layerList->getActive();
        if (activeLayer != nullptr) {
            updateByLayer(activeLayer);
        }
    }
}

void QG_PenToolBar::setGraphicView(RS_GraphicView* gv) {
    if (gv == nullptr) {
        setLayerList(nullptr);
    }
    else {
        auto graphic = gv->getGraphic();
        if (graphic == nullptr) {
            setLayerList(nullptr);
        }
        else {
            auto layerList = graphic->getLayerList();
            setLayerList(layerList);
        }
    }
}

RS_Pen QG_PenToolBar::getPen() const {
    return *m_currentPen;
}

/**
 * Called by the layer list (if this object was previously
 * added as a listener to a layer list).
 */
void QG_PenToolBar::layerEdited(RS_Layer* l) { updateByLayer(l); }

/**
 * Called when the color was changed by the user.
 */
void QG_PenToolBar::slotColorChanged(const RS_Color& color) {
    m_currentPen->setColor(color);
    //printf("  color changed\n");

    emit penChanged(*m_currentPen);
}

/**
 * Called when the width was changed by the user.
 */
void QG_PenToolBar::slotWidthChanged(RS2::LineWidth w) {
    m_currentPen->setWidth(w);
    //printf("  width changed\n");

    emit penChanged(*m_currentPen);
}

/**
 * Called when the linetype was changed by the user.
 */
void QG_PenToolBar::slotLineTypeChanged(RS2::LineType w) {
    m_currentPen->setLineType(w);
    //printf("  line type changed\n");

    emit penChanged(*m_currentPen);
}