File size: 5,326 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
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software 
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** 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
**
** This copyright notice MUST APPEAR in all copies of the script!  
**
**********************************************************************/

#include "qg_widgetpen.h"

#include <qevent.h>

#include "rs_debug.h"
#include "rs_entity.h"
#include "rs_layer.h"

/*
 *  Constructs a QG_WidgetPen as a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'.
 */
QG_WidgetPen::QG_WidgetPen(QWidget* parent, Qt::WindowFlags fl)
    : QWidget(parent, fl)
{
    setupUi(this);
    connect(cbColor, &QG_ColorBox::colorChanged, this, &QG_WidgetPen::onColorChanged);
    connect(cbWidth, &QG_WidthBox::widthChanged, this, &QG_WidgetPen::onWidthChanged);
    connect(cbLineType, &QG_LineTypeBox::lineTypeChanged, this, &QG_WidgetPen::onLineTypeChanged);
//    cbColor->setFocusPolicy(Qt::StrongFocus);
//    cbWidth->setFocusPolicy(Qt::StrongFocus);
//    cbLineType->setFocusPolicy(Qt::StrongFocus);
//    setTabOrder(cbColor, cbWidth); // cbColor to cbWidth
//    setTabOrder(cbWidth, cbLineType); // cbColor to cbWidth to cbLineType
}

void QG_WidgetPen::disableVerticalSpacer() {
    vsSpacerVertical->changeSize(0,0);
}

void QG_WidgetPen::onColorChanged([[maybe_unused]]const RS_Color& color) {
    notifyPenChanged();
}

void QG_WidgetPen::onWidthChanged(RS2::LineWidth) {
    notifyPenChanged();
}

void QG_WidgetPen::onLineTypeChanged(RS2::LineType) {
    notifyPenChanged();
}

void QG_WidgetPen::notifyPenChanged() {
    emit penChanged();
}

void QG_WidgetPen::setPen(RS_Pen pen, bool showByLayer, 
                          bool showUnchanged, const QString& title) {
    if (!m_initialized) {
        cbColor->init(showByLayer, showUnchanged);
        cbWidth->init(showByLayer, showUnchanged);
        cbLineType->init(showByLayer, showUnchanged);
        m_initialized = true;
    }
    if (!showUnchanged) {
       cbColor->setColor(pen.getColor());
       cbWidth->setWidth(pen.getWidth());
       cbLineType->setLineType(pen.getLineType());
    }

    if (!title.isEmpty()) {
        bgPen->setTitle(title);
    }
}

void QG_WidgetPen::setPen(RS_Entity* entity, RS_Layer* layer, const QString &title){
    RS_Pen entityPen = entity->getPen(false);
    RS_Pen entityResolvedPen = entity->getPen(true);

    RS_Color originalColor = entityPen.getColor();
    RS_Color resolvedColor = entityResolvedPen.getColor();
    resolvedColor.applyFlags(originalColor);
    entityResolvedPen.setColor(resolvedColor);

    entityResolvedPen.setLineType(entityPen.getLineType());
    entityResolvedPen.setWidth(entityPen.getWidth());

    setPen(entityResolvedPen, layer, title);
}

void QG_WidgetPen::setPen(RS_Pen pen, RS_Layer* layer, bool showUnchanged, const QString &title){
    setPen(pen, true, showUnchanged, title);
    if (layer != nullptr){
        RS_Pen layerPen = layer->getPen();
        RS_Color layerColor = layerPen.getColor();
        cbColor->setLayerColor(layerColor);
    }
}
void QG_WidgetPen::setPen(RS_Pen pen, RS_Layer* layer, const QString &title){
    setPen(pen, layer, false, title);
}

RS_Pen QG_WidgetPen::getPen() {
    RS_Pen pen;

    pen.setColor(cbColor->getColor());
    pen.setWidth(cbWidth->getWidth());
    pen.setLineType(cbLineType->getLineType());

    return pen;
}

bool QG_WidgetPen::isColorUnchanged() {
    return cbColor->isUnchanged();
}

bool QG_WidgetPen::isLineTypeUnchanged() {
    return cbLineType->isUnchanged();
}

bool QG_WidgetPen::isWidthUnchanged() {
    return cbWidth->isUnchanged();
}

/*
 *  Destroys the object and frees any allocated resources
 */
QG_WidgetPen::~QG_WidgetPen() {
    // no need to delete child widgets, Qt does it all for us
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void QG_WidgetPen::languageChange(){
    retranslateUi(this);
}

/*
 *  When tabbing in to the widget, passes the focus to the 
 *  cbColor subwidget.
 */
void QG_WidgetPen::focusInEvent(QFocusEvent* event) {
    int reason = event->reason();
    RS_DEBUG->print(RS_Debug::D_ERROR, "QG_WidgetPen::focusInEvent, reason '%d'", reason);
    //	if ( reason == Qt::BacktabFocusReason )
    //	    cbLineType->setFocus();
    //	else
    //	    cbColor->setFocus();
}

void QG_WidgetPen::focusOutEvent(QFocusEvent *event){
    int reason = event->reason();
    RS_DEBUG->print(RS_Debug::D_ERROR,"QG_WidgetPen::focusOutEvent, reason '%d'",reason);
}