]> Shamusworld >> Repos - architektonas/blob - src/penwidget.cpp
4bd966af36e2af3cf3ac4e1f8c1d7a893f768f33
[architektonas] / src / penwidget.cpp
1 // penwidget.cpp: Pen tweaking widget
2 //
3 // Part of the Architektonas Project
4 // (C) 2017 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  -----------------------------------------------------------
11 // JLH  05/07/2017  Created this file
12 //
13 //maybe add a button to have it stamp attributes on all objects clicked on? [added Global::penStamp to facilitate this.]
14 //need to add to drawingview the ability to use attributes on new objects [DONE]
15 //need to override the selection so you can see the attributes effects immediately when they are changed instead of having to deselect them to see
16
17
18 #include "penwidget.h"
19
20
21 PenWidget::PenWidget(void): QWidget(), r(0), g(0), b(0), programChange(false)
22 {
23         width = new QLineEdit("1.0");
24         red = new QLineEdit("00");
25         green = new QLineEdit("00");
26         blue = new QLineEdit("00");
27         style = new QComboBox;
28
29         QLabel * l1 = new QLabel(tr("Width:"));
30         QLabel * l2 = new QLabel(tr("RGB:"));
31         QLabel * l3 = new QLabel(tr("Style:"));
32         QToolButton * qtb = new QToolButton(this);
33
34         QAction * action = new QAction(QIcon(":/res/pen-stamp.png"), tr(""), this);
35         action->setToolTip(tr("Stamp Selected"));
36         action->setStatusTip(tr("Stamp selected objects with pen attributes."));
37         action->setShortcut(QKeySequence(tr("p,p")));
38 //      action->setCheckable(checkable);
39         qtb->setDefaultAction(action);
40
41         style->insertItem(1, tr("Solid"));
42         style->insertItem(2, tr("Dash"));
43         style->insertItem(3, tr("Dot"));
44         style->insertItem(4, tr("Dash Dot"));
45         style->insertItem(5, tr("Dash Dot Dot"));
46
47         width->setFixedWidth(72);
48         red->setMaxLength(2);
49         red->setFixedWidth(36);
50         green->setMaxLength(2);
51         green->setFixedWidth(36);
52         blue->setMaxLength(2);
53         blue->setFixedWidth(36);
54
55         red->setInputMask("HH");
56         green->setInputMask("HH");
57         blue->setInputMask("HH");
58
59         red->setToolTip(tr("Sets pen Red component"));
60         green->setToolTip(tr("Sets pen Green component"));
61         blue->setToolTip(tr("Sets pen Blue component"));
62         width->setToolTip(tr("Sets pen width in BU"));
63         style->setToolTip(tr("Sets pen style"));
64
65         QHBoxLayout * hbox1 = new QHBoxLayout;
66         hbox1->addWidget(l1, 0, Qt::AlignLeft);
67         hbox1->addWidget(width, 0, Qt::AlignLeft);
68         hbox1->addWidget(l2, 0, Qt::AlignLeft);
69         hbox1->addWidget(red, 0, Qt::AlignLeft);
70         hbox1->addWidget(green, 0, Qt::AlignLeft);
71         hbox1->addWidget(blue, 0, Qt::AlignLeft);
72         hbox1->addWidget(l3, 0, Qt::AlignLeft);
73         hbox1->addWidget(style, 0, Qt::AlignLeft);
74         hbox1->addWidget(qtb, 0, Qt::AlignLeft);
75         hbox1->addStretch(1);
76
77         setLayout(hbox1);
78
79         connect(width, SIGNAL(textEdited(QString)), this, SLOT(HandleWidthSelected(QString)));
80         connect(red, SIGNAL(textEdited(QString)), this, SLOT(HandleRedSelected(QString)));
81         connect(green, SIGNAL(textEdited(QString)), this, SLOT(HandleGreenSelected(QString)));
82         connect(blue, SIGNAL(textEdited(QString)), this, SLOT(HandleBlueSelected(QString)));
83         connect(style, SIGNAL(currentIndexChanged(int)), this, SLOT(HandleStyleSelected(int)));
84         connect(qtb, SIGNAL(triggered(QAction *)), this, SLOT(HandleStamp(QAction *)));
85 }
86
87
88 PenWidget::~PenWidget()
89 {
90 }
91
92
93 void PenWidget::SetFields(Object * obj)
94 {
95         // Sanity check
96         if (obj == NULL)
97                 return;
98
99         // We don't handle groups properly--yet--so punt:
100         if (obj->type == OTContainer)
101                 return;
102
103         r = (obj->color >> 16) & 0xFF;
104         g = (obj->color >> 8) & 0xFF;
105         b = (obj->color >> 0) & 0xFF;
106         width->setText(QString("%1").arg(obj->thickness));
107         red->setText(QString("%1").arg(r, 2, 16, QChar('0')));
108         green->setText(QString("%1").arg(g, 2, 16, QChar('0')));
109         blue->setText(QString("%1").arg(b, 2, 16, QChar('0')));
110         // Styles are 1-indexed while the combobox is 0-indexed
111         programChange = true;
112         style->setCurrentIndex(obj->style - 1);
113         programChange = false;
114 }
115
116
117 void PenWidget::HandleWidthSelected(QString text)
118 {
119         // Parse the text in the control
120         bool ok;
121         double value = text.toDouble(&ok);
122
123         if (!ok)
124                 return;
125
126         Global::penWidth = value;
127         emit WidthSelected(Global::penWidth);
128 }
129
130
131 void PenWidget::HandleStyleSelected(int selected)
132 {
133         // Change was programmatic, don't do anything
134         if (programChange)
135                 return;
136
137         // Styles are 1-based, but the combobox is 0-based, so we compensate for
138         // that here
139         Global::penStyle = selected + 1;
140         emit StyleSelected(Global::penStyle);
141 }
142
143
144 void PenWidget::HandleRedSelected(QString text)
145 {
146         // Parse the text in the control
147         bool ok;
148         int value = text.toInt(&ok, 16);
149
150         if (!ok)
151                 return;
152
153         r = value;
154         Global::penColor = (r << 16) | (g << 8) | b;
155         emit ColorSelected(Global::penColor);
156 }
157
158
159 void PenWidget::HandleGreenSelected(QString text)
160 {
161         // Parse the text in the control
162         bool ok;
163         int value = text.toInt(&ok, 16);
164
165         if (!ok)
166                 return;
167
168         g = value;
169         Global::penColor = (r << 16) | (g << 8) | b;
170         emit ColorSelected(Global::penColor);
171 }
172
173
174 void PenWidget::HandleBlueSelected(QString text)
175 {
176         // Parse the text in the control
177         bool ok;
178         int value = text.toInt(&ok, 16);
179
180         if (!ok)
181                 return;
182
183         b = value;
184         Global::penColor = (r << 16) | (g << 8) | b;
185         emit ColorSelected(Global::penColor);
186 }
187
188
189 void PenWidget::HandleStamp(QAction * /* action */)
190 {
191         emit StampSelected();
192 }
193