]> Shamusworld >> Repos - architektonas/blob - src/penwidget.cpp
50d90c6d52484ffecc7f7b532bce0834dfb69246
[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
14 #include "penwidget.h"
15
16
17 PenWidget::PenWidget(void): QWidget(), r(0), g(0), b(0)
18 {
19         width = new QLineEdit("1.0");
20         red = new QLineEdit("00");
21         green = new QLineEdit("00");
22         blue = new QLineEdit("00");
23         style = new QComboBox;
24
25         QLabel * l1 = new QLabel(tr("Width:"));
26         QLabel * l2 = new QLabel(tr("RGB:"));
27         QLabel * l3 = new QLabel(tr("Style:"));
28
29         style->insertItem(1, tr("Solid"));
30         style->insertItem(2, tr("Dash"));
31         style->insertItem(3, tr("Dot"));
32         style->insertItem(4, tr("Dash Dot"));
33         style->insertItem(5, tr("Dash Dot Dot"));
34
35         width->setFixedWidth(72);
36         red->setMaxLength(2);
37         red->setFixedWidth(36);
38         green->setMaxLength(2);
39         green->setFixedWidth(36);
40         blue->setMaxLength(2);
41         blue->setFixedWidth(36);
42
43         red->setInputMask("HH");
44         green->setInputMask("HH");
45         blue->setInputMask("HH");
46
47         red->setToolTip(tr("Sets pen Red component"));
48         green->setToolTip(tr("Sets pen Green component"));
49         blue->setToolTip(tr("Sets pen Blue component"));
50         width->setToolTip(tr("Sets pen width in BU"));
51         style->setToolTip(tr("Sets pen style"));
52
53         QHBoxLayout * hbox1 = new QHBoxLayout;
54         hbox1->addWidget(l1, 0, Qt::AlignLeft);
55         hbox1->addWidget(width, 0, Qt::AlignLeft);
56         hbox1->addWidget(l2, 0, Qt::AlignLeft);
57         hbox1->addWidget(red, 0, Qt::AlignLeft);
58         hbox1->addWidget(green, 0, Qt::AlignLeft);
59         hbox1->addWidget(blue, 0, Qt::AlignLeft);
60         hbox1->addWidget(l3, 0, Qt::AlignLeft);
61         hbox1->addWidget(style, 0, Qt::AlignLeft);
62         hbox1->addStretch(1);
63
64         setLayout(hbox1);
65
66         connect(width, SIGNAL(textEdited(QString)), this, SLOT(HandleWidthSelected(QString)));
67         connect(red, SIGNAL(textEdited(QString)), this, SLOT(HandleRedSelected(QString)));
68         connect(green, SIGNAL(textEdited(QString)), this, SLOT(HandleGreenSelected(QString)));
69         connect(blue, SIGNAL(textEdited(QString)), this, SLOT(HandleBlueSelected(QString)));
70         connect(style, SIGNAL(currentIndexChanged(int)), this, SLOT(HandleStyleSelected(int)));
71 }
72
73
74 PenWidget::~PenWidget()
75 {
76 }
77
78
79 void PenWidget::SetFields(Object * obj)
80 {
81         // Sanity check
82         if (obj == NULL)
83                 return;
84
85         int r = (obj->color >> 16) & 0xFF;
86         int g = (obj->color >> 8) & 0xFF;
87         int b = (obj->color >> 0) & 0xFF;
88         width->setText(QString("%1").arg(obj->thickness));
89         red->setText(QString("%1").arg(r, 2, 16, QChar('0')));
90         green->setText(QString("%1").arg(g, 2, 16, QChar('0')));
91         blue->setText(QString("%1").arg(b, 2, 16, QChar('0')));
92         // Styles are 1-indexed while the combobox is 0-indexed
93         style->setCurrentIndex(obj->style - 1);
94 }
95
96
97 void PenWidget::HandleWidthSelected(QString text)
98 {
99         // Parse the text in the control
100         bool ok;
101         double value = text.toDouble(&ok);
102
103         if (!ok)
104                 return;
105
106         emit WidthSelected(value);
107 }
108
109
110 void PenWidget::HandleStyleSelected(int selected)
111 {
112         // Styles are 1-based, but the combobox is 0-based, so we compensate for
113         // that here
114         emit StyleSelected(selected + 1);
115 }
116
117
118 void PenWidget::HandleRedSelected(QString text)
119 {
120         // Parse the text in the control
121         bool ok;
122         int value = text.toInt(&ok, 16);
123
124         if (!ok)
125                 return;
126
127         r = value;
128         emit ColorSelected((r << 16) | (g << 8) | b);
129 }
130
131
132 void PenWidget::HandleGreenSelected(QString text)
133 {
134         // Parse the text in the control
135         bool ok;
136         int value = text.toInt(&ok, 16);
137
138         if (!ok)
139                 return;
140
141         g = value;
142         emit ColorSelected((r << 16) | (g << 8) | b);
143 }
144
145
146 void PenWidget::HandleBlueSelected(QString text)
147 {
148         // Parse the text in the control
149         bool ok;
150         int value = text.toInt(&ok, 16);
151
152         if (!ok)
153                 return;
154
155         b = value;
156         emit ColorSelected((r << 16) | (g << 8) | b);
157 }
158