]> Shamusworld >> Repos - architektonas/blobdiff - src/penwidget.cpp
Add pen toolbar widget.
[architektonas] / src / penwidget.cpp
diff --git a/src/penwidget.cpp b/src/penwidget.cpp
new file mode 100644 (file)
index 0000000..50d90c6
--- /dev/null
@@ -0,0 +1,158 @@
+// penwidget.cpp: Pen tweaking widget
+//
+// Part of the Architektonas Project
+// (C) 2017 Underground Software
+// See the README and GPLv3 files for licensing and warranty information
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// WHO  WHEN        WHAT
+// ---  ----------  -----------------------------------------------------------
+// JLH  05/07/2017  Created this file
+//
+
+#include "penwidget.h"
+
+
+PenWidget::PenWidget(void): QWidget(), r(0), g(0), b(0)
+{
+       width = new QLineEdit("1.0");
+       red = new QLineEdit("00");
+       green = new QLineEdit("00");
+       blue = new QLineEdit("00");
+       style = new QComboBox;
+
+       QLabel * l1 = new QLabel(tr("Width:"));
+       QLabel * l2 = new QLabel(tr("RGB:"));
+       QLabel * l3 = new QLabel(tr("Style:"));
+
+       style->insertItem(1, tr("Solid"));
+       style->insertItem(2, tr("Dash"));
+       style->insertItem(3, tr("Dot"));
+       style->insertItem(4, tr("Dash Dot"));
+       style->insertItem(5, tr("Dash Dot Dot"));
+
+       width->setFixedWidth(72);
+       red->setMaxLength(2);
+       red->setFixedWidth(36);
+       green->setMaxLength(2);
+       green->setFixedWidth(36);
+       blue->setMaxLength(2);
+       blue->setFixedWidth(36);
+
+       red->setInputMask("HH");
+       green->setInputMask("HH");
+       blue->setInputMask("HH");
+
+       red->setToolTip(tr("Sets pen Red component"));
+       green->setToolTip(tr("Sets pen Green component"));
+       blue->setToolTip(tr("Sets pen Blue component"));
+       width->setToolTip(tr("Sets pen width in BU"));
+       style->setToolTip(tr("Sets pen style"));
+
+       QHBoxLayout * hbox1 = new QHBoxLayout;
+       hbox1->addWidget(l1, 0, Qt::AlignLeft);
+       hbox1->addWidget(width, 0, Qt::AlignLeft);
+       hbox1->addWidget(l2, 0, Qt::AlignLeft);
+       hbox1->addWidget(red, 0, Qt::AlignLeft);
+       hbox1->addWidget(green, 0, Qt::AlignLeft);
+       hbox1->addWidget(blue, 0, Qt::AlignLeft);
+       hbox1->addWidget(l3, 0, Qt::AlignLeft);
+       hbox1->addWidget(style, 0, Qt::AlignLeft);
+       hbox1->addStretch(1);
+
+       setLayout(hbox1);
+
+       connect(width, SIGNAL(textEdited(QString)), this, SLOT(HandleWidthSelected(QString)));
+       connect(red, SIGNAL(textEdited(QString)), this, SLOT(HandleRedSelected(QString)));
+       connect(green, SIGNAL(textEdited(QString)), this, SLOT(HandleGreenSelected(QString)));
+       connect(blue, SIGNAL(textEdited(QString)), this, SLOT(HandleBlueSelected(QString)));
+       connect(style, SIGNAL(currentIndexChanged(int)), this, SLOT(HandleStyleSelected(int)));
+}
+
+
+PenWidget::~PenWidget()
+{
+}
+
+
+void PenWidget::SetFields(Object * obj)
+{
+       // Sanity check
+       if (obj == NULL)
+               return;
+
+       int r = (obj->color >> 16) & 0xFF;
+       int g = (obj->color >> 8) & 0xFF;
+       int b = (obj->color >> 0) & 0xFF;
+       width->setText(QString("%1").arg(obj->thickness));
+       red->setText(QString("%1").arg(r, 2, 16, QChar('0')));
+       green->setText(QString("%1").arg(g, 2, 16, QChar('0')));
+       blue->setText(QString("%1").arg(b, 2, 16, QChar('0')));
+       // Styles are 1-indexed while the combobox is 0-indexed
+       style->setCurrentIndex(obj->style - 1);
+}
+
+
+void PenWidget::HandleWidthSelected(QString text)
+{
+       // Parse the text in the control
+       bool ok;
+       double value = text.toDouble(&ok);
+
+       if (!ok)
+               return;
+
+       emit WidthSelected(value);
+}
+
+
+void PenWidget::HandleStyleSelected(int selected)
+{
+       // Styles are 1-based, but the combobox is 0-based, so we compensate for
+       // that here
+       emit StyleSelected(selected + 1);
+}
+
+
+void PenWidget::HandleRedSelected(QString text)
+{
+       // Parse the text in the control
+       bool ok;
+       int value = text.toInt(&ok, 16);
+
+       if (!ok)
+               return;
+
+       r = value;
+       emit ColorSelected((r << 16) | (g << 8) | b);
+}
+
+
+void PenWidget::HandleGreenSelected(QString text)
+{
+       // Parse the text in the control
+       bool ok;
+       int value = text.toInt(&ok, 16);
+
+       if (!ok)
+               return;
+
+       g = value;
+       emit ColorSelected((r << 16) | (g << 8) | b);
+}
+
+
+void PenWidget::HandleBlueSelected(QString text)
+{
+       // Parse the text in the control
+       bool ok;
+       int value = text.toInt(&ok, 16);
+
+       if (!ok)
+               return;
+
+       b = value;
+       emit ColorSelected((r << 16) | (g << 8) | b);
+}
+