Permalink
Browse files

Custom color support

  • Loading branch information...
feragon committed Aug 10, 2016
1 parent f1da956 commit af6c8364bf62cac2825843e6d8a7cb1c68a00ff7
@@ -15,21 +15,18 @@ AddLayerDialog::AddLayerDialog(lc::Layer_CSPtr oldLayer, lc::Document_SPtr docum
linePatternSelect = new LinePatternSelect(document, this);
lineWidthSelect = new LineWidthSelect(this);
colorSelect = new ColorSelect(this);
auto layout = dynamic_cast<QFormLayout*>(this->layout());
if(layout) {
layout->setWidget(1, QFormLayout::FieldRole, colorSelect);
layout->setWidget(2, QFormLayout::FieldRole, lineWidthSelect);
layout->setWidget(3, QFormLayout::FieldRole, linePatternSelect);
}
if(oldLayer != nullptr) {
ui->name->setText(oldLayer->name().c_str());
ui->r->setValue(oldLayer->color().redI());
ui->g->setValue(oldLayer->color().greenI());
ui->b->setValue(oldLayer->color().blueI());
ui->a->setValue(oldLayer->color().alphaI());
if(oldLayer->linePattern() != nullptr) {
int linePatternIndex = linePatternSelect->findText(oldLayer->linePattern()->name().c_str());
@@ -57,22 +54,14 @@ void AddLayerDialog::accept() {
layer = std::make_shared<const lc::Layer>(
ui->name->text().toStdString(),
*lineWidthSelect->lineWidth(),
lc::Color(ui->r->value(),
ui->g->value(),
ui->b->value(),
ui->a->value()
)
colorSelect->color()
);
}
else {
layer = std::make_shared<const lc::Layer>(
ui->name->text().toStdString(),
*lineWidthSelect->lineWidth(),
lc::Color(ui->r->value(),
ui->g->value(),
ui->b->value(),
ui->a->value()
),
colorSelect->color(),
linePattern,
false
);
@@ -86,24 +75,4 @@ void AddLayerDialog::accept() {
}
this->close();
}
void AddLayerDialog::on_pickColorButton_clicked() {
QColor currentColor(ui->r->value(),
ui->g->value(),
ui->b->value(),
ui->a->value());
auto colorDialog = new QColorDialog();
colorDialog->setCurrentColor(currentColor);
colorDialog->show();
connect(colorDialog, &QColorDialog::colorSelected, this, &AddLayerDialog::on_colorChanged);
}
void AddLayerDialog::on_colorChanged(const QColor& color) {
ui->r->setValue(color.red());
ui->g->setValue(color.green());
ui->b->setValue(color.blue());
ui->a->setValue(color.alpha());
}
@@ -7,6 +7,7 @@
#include "widgets/linepatternselect.h"
#include "widgets/linewidthselect.h"
#include "widgets/colorselect.h"
#include <cad/meta/layer.h>
#include <cad/meta/metalinewidth.h>
@@ -30,13 +31,12 @@ class AddLayerDialog : public QDialog {
private slots:
void accept();
void on_pickColorButton_clicked();
void on_colorChanged(const QColor& color);
private:
Ui::AddLayerDialog* ui;
lc::Document_SPtr _document;
LinePatternSelect* linePatternSelect;
LineWidthSelect* lineWidthSelect;
ColorSelect* colorSelect;
lc::Layer_CSPtr _oldLayer;
};
@@ -55,66 +55,6 @@
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="colorLayout">
<item>
<widget class="QSpinBox" name="r">
<property name="frame">
<bool>true</bool>
</property>
<property name="prefix">
<string>R: </string>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="g">
<property name="prefix">
<string>G: </string>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="b">
<property name="prefix">
<string>B: </string>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="a">
<property name="frame">
<bool>true</bool>
</property>
<property name="prefix">
<string>A: </string>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="value">
<number>255</number>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pickColorButton">
<property name="text">
<string>Pick color</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
View
@@ -298,7 +298,7 @@ void addLCBindings(lua_State *L) {
.addFactory([](QWidget* parent, bool showByLayer, bool showByBlock){
return new ColorSelect(parent, showByLayer, showByBlock);
})
.addFunction("color", &ColorSelect::color)
.addFunction("metaColor", &ColorSelect::metaColor)
.endClass()
.endModule();
@@ -15,19 +15,60 @@ ColorSelect::ColorSelect(QWidget *parent, bool showByLayer, bool showByBlock) :
addItem(BY_BLOCK);
}
addItem(CUSTOM);
insertSeparator(count());
for(auto color : QColor::colorNames()){
QPixmap pixmap(qIconSize);
pixmap.fill(color);
addItem(QIcon(pixmap), color);
}
connect(this, SIGNAL(activated(const QString&)), this, SLOT(onActivated(const QString&)));
}
QColor ColorSelect::getColor() {
if(currentText() == CUSTOM) {
return _customColor;
}
else {
return QColor(currentText());
}
}
lc::MetaColor_CSPtr ColorSelect::color() {
QColor color(currentText());
lc::MetaColor_CSPtr ColorSelect::metaColor() {
QColor color = getColor();
if(!color.isValid()) {
return nullptr;
}
return std::make_shared<lc::MetaColor>(color.redF(), color.greenF(), color.blueF(), color.alphaF());
}
lc::Color ColorSelect::color() {
QColor color = getColor();
if(!color.isValid()) {
return lc::Color();
}
return lc::Color(color.redF(), color.greenF(), color.blueF(), color.alphaF());
}
void ColorSelect::onActivated(const QString& text) {
if(text == CUSTOM) {
auto colorDialog = new QColorDialog(this);
if(_customColor.isValid()) {
colorDialog->setCurrentColor(_customColor);
}
colorDialog->show();
connect(colorDialog, &QColorDialog::colorSelected, this, &ColorSelect::on_customColorChanged);
}
}
void ColorSelect::on_customColorChanged(const QColor &color) {
_customColor = color;
}
View
@@ -3,17 +3,29 @@
#include <QComboBox>
#include <QString>
#include <QColor>
#include <QColorDialog>
#include <cad/meta/metacolor.h>
#define BY_BLOCK "ByBlock"
#define BY_LAYER "ByLayer"
#define CUSTOM "Custom"
class ColorSelect : public QComboBox {
public:
ColorSelect(QWidget* parent = 0, bool showByLayer = false, bool showByBlock = false);
lc::MetaColor_CSPtr color();
Q_OBJECT
private:
QSize qIconSize;
public:
ColorSelect(QWidget* parent = 0, bool showByLayer = false, bool showByBlock = false);
lc::MetaColor_CSPtr metaColor();
lc::Color color();
private slots:
void onActivated(const QString& text);
void on_customColorChanged(const QColor& color);
private:
QColor getColor();
QSize qIconSize;
QColor _customColor;
};
@@ -69,7 +69,7 @@ function active_metaInfo()
metaInfo:add(lineWidth)
end
local color = colorSelect:color()
local color = colorSelect:metaColor()
if(color ~= nil) then
metaInfo:add(color)
end

0 comments on commit af6c836

Please sign in to comment.