2015-06-21 20:16:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2006-2015 The QElectroTech Team
|
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "shapegraphicsitempropertieswidget.h"
|
|
|
|
#include "ui_shapegraphicsitempropertieswidget.h"
|
|
|
|
#include "qetshapeitem.h"
|
|
|
|
#include "diagram.h"
|
2015-08-06 16:34:38 +00:00
|
|
|
#include "QPropertyUndoCommand/qpropertyundocommand.h"
|
2015-06-21 20:16:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget
|
|
|
|
* Constructor
|
|
|
|
* @param item : shape to edit
|
|
|
|
* @param parent : parent widget
|
|
|
|
*/
|
|
|
|
ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget(QetShapeItem *item, QWidget *parent) :
|
|
|
|
PropertiesEditorWidget(parent),
|
|
|
|
ui(new Ui::ShapeGraphicsItemPropertiesWidget),
|
|
|
|
m_shape(nullptr)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
setItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::~ShapeGraphicsItemPropertiesWidget
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
ShapeGraphicsItemPropertiesWidget::~ShapeGraphicsItemPropertiesWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::setItem
|
|
|
|
* Set @shape as the current edited item
|
|
|
|
* @param shape
|
|
|
|
*/
|
|
|
|
void ShapeGraphicsItemPropertiesWidget::setItem(QetShapeItem *shape)
|
|
|
|
{
|
2016-01-12 14:02:34 +00:00
|
|
|
if (!shape || shape == m_shape) return;
|
2015-06-21 20:16:41 +00:00
|
|
|
|
2016-01-03 13:45:30 +00:00
|
|
|
if (m_shape && m_live_edit)
|
2016-01-12 14:02:34 +00:00
|
|
|
{
|
2015-08-06 16:34:38 +00:00
|
|
|
disconnect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
2016-01-12 14:02:34 +00:00
|
|
|
disconnect(m_shape, &QetShapeItem::brushChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
|
|
|
disconnect(m_shape, &QetShapeItem::closeChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
|
|
|
}
|
2015-06-21 20:16:41 +00:00
|
|
|
|
|
|
|
m_shape = shape;
|
2016-01-12 14:02:34 +00:00
|
|
|
ui->m_close_polygon->setVisible(m_shape->shapeType() == QetShapeItem::Polygon);
|
2015-12-30 13:47:21 +00:00
|
|
|
|
2016-01-03 13:45:30 +00:00
|
|
|
if (m_live_edit)
|
2016-01-12 14:02:34 +00:00
|
|
|
{
|
2016-01-03 13:45:30 +00:00
|
|
|
connect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
2016-01-12 14:02:34 +00:00
|
|
|
connect(m_shape, &QetShapeItem::brushChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
|
|
|
connect(m_shape, &QetShapeItem::closeChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
|
|
|
}
|
2015-06-21 20:16:41 +00:00
|
|
|
|
|
|
|
updateUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::apply
|
|
|
|
* Apply the current change, by pushing an undo command to the
|
|
|
|
* undo stack of the shape diagram.
|
|
|
|
*/
|
|
|
|
void ShapeGraphicsItemPropertiesWidget::apply()
|
2016-01-03 13:45:30 +00:00
|
|
|
{
|
2015-06-21 20:16:41 +00:00
|
|
|
if (m_shape->diagram())
|
|
|
|
if (QUndoCommand *undo = associatedUndo())
|
|
|
|
m_shape->diagram()->undoStack().push(undo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::reset
|
|
|
|
* Reset the change
|
|
|
|
*/
|
2015-08-06 16:34:38 +00:00
|
|
|
void ShapeGraphicsItemPropertiesWidget::reset() {
|
2015-06-21 20:16:41 +00:00
|
|
|
updateUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::associatedUndo
|
|
|
|
* @return an undo command that represent the change edited by this widget.
|
2016-01-03 13:45:30 +00:00
|
|
|
* The returned undo command is a QPropertyUndoCommand with the properties "pen".
|
2015-06-21 20:16:41 +00:00
|
|
|
* If there isn't change, return nullptr
|
|
|
|
*/
|
|
|
|
QUndoCommand* ShapeGraphicsItemPropertiesWidget::associatedUndo() const
|
|
|
|
{
|
2016-01-12 14:02:34 +00:00
|
|
|
QPropertyUndoCommand *undo = nullptr;
|
2015-08-06 16:34:38 +00:00
|
|
|
QPen old_pen = m_shape->pen();
|
|
|
|
QPen new_pen = old_pen;
|
2016-01-03 13:45:30 +00:00
|
|
|
|
2015-08-06 16:34:38 +00:00
|
|
|
new_pen.setStyle(Qt::PenStyle(ui->m_style_cb->currentIndex() + 1));
|
2016-01-03 13:45:30 +00:00
|
|
|
new_pen.setWidthF(ui->m_size_dsb->value());
|
|
|
|
new_pen.setColor(ui->m_color_pb->palette().color(QPalette::Button));
|
|
|
|
|
2016-01-12 14:02:34 +00:00
|
|
|
if (new_pen != old_pen)
|
|
|
|
{
|
|
|
|
undo = new QPropertyUndoCommand(m_shape, "pen", old_pen, new_pen);
|
|
|
|
undo->setText(tr("Modifier le trait d'une forme"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QBrush old_brush = m_shape->brush();
|
|
|
|
QBrush new_brush = old_brush;
|
|
|
|
new_brush.setStyle(Qt::BrushStyle(ui->m_brush_style_cb->currentIndex()));
|
|
|
|
new_brush.setColor(ui->m_brush_color_pb->palette().color(QPalette::Button));
|
|
|
|
|
|
|
|
if (new_brush != old_brush)
|
|
|
|
{
|
|
|
|
if (undo)
|
|
|
|
new QPropertyUndoCommand(m_shape, "brush", old_brush, new_brush, undo);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
undo = new QPropertyUndoCommand(m_shape, "brush", old_brush, new_brush);
|
|
|
|
undo->setText(tr("Modifier le remplissage d'une forme"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ui->m_close_polygon->isChecked() != m_shape->isClosed())
|
|
|
|
{
|
|
|
|
if (undo)
|
|
|
|
new QPropertyUndoCommand(m_shape, "close", m_shape->isClosed(), ui->m_close_polygon->isChecked(), undo);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
undo = new QPropertyUndoCommand(m_shape, "close", m_shape->isClosed(), ui->m_close_polygon->isChecked(), undo);
|
|
|
|
undo->setText(tr("Fermer le polygone"));
|
|
|
|
}
|
|
|
|
}
|
2015-08-06 16:34:38 +00:00
|
|
|
|
|
|
|
return undo;
|
2015-06-21 20:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::updateUi
|
|
|
|
*/
|
|
|
|
void ShapeGraphicsItemPropertiesWidget::updateUi()
|
|
|
|
{
|
2016-01-03 13:45:30 +00:00
|
|
|
bool le = m_live_edit;
|
|
|
|
setLiveEdit(false); //Disable temporally live edit mode to avoid weird behavior
|
2016-01-12 14:02:34 +00:00
|
|
|
//Pen
|
2015-08-06 16:34:38 +00:00
|
|
|
ui->m_style_cb->setCurrentIndex(static_cast<int>(m_shape->pen().style()) - 1);
|
2016-01-03 13:45:30 +00:00
|
|
|
ui->m_size_dsb ->setValue(m_shape->pen().widthF());
|
2016-01-12 14:02:34 +00:00
|
|
|
setPenColorButton(m_shape->pen().color());
|
|
|
|
//Brush
|
|
|
|
ui->m_brush_style_cb->setCurrentIndex(static_cast<int>(m_shape->brush().style()));
|
|
|
|
setBrushColorButton(m_shape->brush().color());
|
|
|
|
|
2015-06-21 20:16:41 +00:00
|
|
|
ui->m_lock_pos_cb->setChecked(!m_shape->isMovable());
|
2016-01-12 14:02:34 +00:00
|
|
|
ui->m_close_polygon->setChecked(m_shape->isClosed());
|
2016-01-03 13:45:30 +00:00
|
|
|
setLiveEdit(le);
|
2015-06-21 20:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::setLiveEdit
|
|
|
|
* @param live_edit
|
|
|
|
* @return always true
|
|
|
|
*/
|
|
|
|
bool ShapeGraphicsItemPropertiesWidget::setLiveEdit(bool live_edit)
|
|
|
|
{
|
|
|
|
if (live_edit == m_live_edit) return true;
|
|
|
|
m_live_edit = live_edit;
|
|
|
|
|
2016-01-03 13:45:30 +00:00
|
|
|
if (m_live_edit)
|
|
|
|
{
|
2015-06-21 20:16:41 +00:00
|
|
|
connect (ui->m_style_cb, SIGNAL(activated(int)), this, SLOT(apply()));
|
2016-01-03 13:45:30 +00:00
|
|
|
connect (ui->m_size_dsb, SIGNAL(valueChanged(double)), this, SLOT(apply()));
|
2016-01-12 14:02:34 +00:00
|
|
|
connect (ui->m_brush_style_cb, SIGNAL(activated(int)), this, SLOT(apply()));
|
|
|
|
connect (ui->m_close_polygon, &QCheckBox::clicked, this, &ShapeGraphicsItemPropertiesWidget::apply);
|
2016-01-03 13:45:30 +00:00
|
|
|
connect (m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
2016-01-12 14:02:34 +00:00
|
|
|
connect (m_shape, &QetShapeItem::closeChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
2016-01-03 13:45:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-21 20:16:41 +00:00
|
|
|
disconnect (ui->m_style_cb, SIGNAL(activated(int)), this, SLOT(apply()));
|
2016-01-03 13:45:30 +00:00
|
|
|
disconnect (ui->m_size_dsb, SIGNAL(valueChanged(double)), this, SLOT(apply()));
|
2016-01-12 14:02:34 +00:00
|
|
|
disconnect (ui->m_brush_style_cb, SIGNAL(activated(int)), this, SLOT(apply()));
|
|
|
|
disconnect (ui->m_close_polygon, &QCheckBox::clicked, this, &ShapeGraphicsItemPropertiesWidget::apply);
|
2016-01-03 13:45:30 +00:00
|
|
|
disconnect (m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
2016-01-12 14:02:34 +00:00
|
|
|
disconnect (m_shape, &QetShapeItem::closeChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
|
2016-01-03 13:45:30 +00:00
|
|
|
}
|
2015-06-21 20:16:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-03 13:45:30 +00:00
|
|
|
/**
|
2016-01-12 14:02:34 +00:00
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::setPenColorButton
|
|
|
|
* Set the color of pen push button to the current color of the shape pen
|
2016-01-03 13:45:30 +00:00
|
|
|
* @param color
|
|
|
|
*/
|
2016-01-12 14:02:34 +00:00
|
|
|
void ShapeGraphicsItemPropertiesWidget::setPenColorButton(const QColor &color)
|
2016-01-03 13:45:30 +00:00
|
|
|
{
|
|
|
|
QPalette palette;
|
|
|
|
palette.setColor(QPalette::Button, color);
|
|
|
|
ui -> m_color_pb -> setStyleSheet(QString("background-color: %1; min-height: 1.5em; border-style: outset; border-width: 2px; border-color: gray; border-radius: 4px;").arg(color.name()));
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:02:34 +00:00
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::setBrushColorButton
|
|
|
|
* Set the color of brush push button to the current color of shape brush
|
|
|
|
* @param color
|
|
|
|
*/
|
|
|
|
void ShapeGraphicsItemPropertiesWidget::setBrushColorButton(const QColor &color)
|
|
|
|
{
|
|
|
|
QPalette palette;
|
|
|
|
palette.setColor(QPalette::Button, color);
|
|
|
|
ui->m_brush_color_pb->setStyleSheet(QString("background-color: %1; min-height: 1.5em; border-style: outset; border-width: 2px; border-color: gray; border-radius: 4px;").arg(color.name()));
|
|
|
|
}
|
|
|
|
|
2015-06-21 20:16:41 +00:00
|
|
|
void ShapeGraphicsItemPropertiesWidget::on_m_lock_pos_cb_clicked() {
|
|
|
|
m_shape->setMovable(!ui->m_lock_pos_cb->isChecked());
|
|
|
|
}
|
2016-01-03 13:45:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::on_m_color_pb_clicked
|
2016-01-12 14:02:34 +00:00
|
|
|
* Pen color button was clicked, we open a QColorDialog for select the color to apply to the shape pen.
|
2016-01-03 13:45:30 +00:00
|
|
|
*/
|
|
|
|
void ShapeGraphicsItemPropertiesWidget::on_m_color_pb_clicked()
|
|
|
|
{
|
|
|
|
QColor color = QColorDialog::getColor(m_shape->pen().color(), this);
|
|
|
|
if (color.isValid())
|
2016-01-12 14:02:34 +00:00
|
|
|
setPenColorButton(color);
|
|
|
|
if (m_live_edit)
|
|
|
|
apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ShapeGraphicsItemPropertiesWidget::on_m_brush_color_pb_clicked
|
|
|
|
* Brush color button was clicked, we open a QColorDialog for select the color to apply to the shape brush.
|
|
|
|
*/
|
|
|
|
void ShapeGraphicsItemPropertiesWidget::on_m_brush_color_pb_clicked()
|
|
|
|
{
|
|
|
|
QColor color = QColorDialog::getColor(m_shape->brush().color(), this);
|
|
|
|
if (color.isValid())
|
|
|
|
setBrushColorButton(color);
|
|
|
|
if (m_live_edit)
|
2016-01-03 13:45:30 +00:00
|
|
|
apply();
|
|
|
|
}
|