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