2014-02-23 18:55:26 +00:00
|
|
|
#include "qetshapeitem.h"
|
2014-03-07 08:05:25 +00:00
|
|
|
#include "diagramcommands.h"
|
2014-02-23 18:55:26 +00:00
|
|
|
|
2014-02-28 14:30:59 +00:00
|
|
|
|
2014-02-23 18:55:26 +00:00
|
|
|
QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, bool lineAngle,QGraphicsItem *parent) :
|
|
|
|
QetGraphicsItem(parent),
|
|
|
|
_shapeStyle(Qt::DashLine),
|
2014-02-27 09:55:54 +00:00
|
|
|
_lineAngle(lineAngle),
|
2014-02-28 14:30:59 +00:00
|
|
|
_isFullyBuilt(false),
|
|
|
|
_writingXml(false)
|
2014-02-23 18:55:26 +00:00
|
|
|
{
|
|
|
|
_shapeType = type;
|
|
|
|
_boundingRect = QRectF(p1, p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
QetShapeItem::~QetShapeItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QetShapeItem::setStyle(Qt::PenStyle newStyle)
|
|
|
|
{
|
|
|
|
_shapeStyle = newStyle;
|
2014-03-07 08:05:25 +00:00
|
|
|
update();
|
2014-02-23 18:55:26 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 18:33:06 +00:00
|
|
|
void QetShapeItem::scale(double factor)
|
|
|
|
{
|
|
|
|
QRectF bounding_rect = boundingRect();
|
|
|
|
bounding_rect.setWidth(bounding_rect.width() * factor);
|
|
|
|
bounding_rect.setHeight(bounding_rect.height() * factor);
|
|
|
|
setBoundingRect(bounding_rect);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-02-27 09:55:54 +00:00
|
|
|
void QetShapeItem::setFullyBuilt(bool isBuilt)
|
|
|
|
{
|
|
|
|
_isFullyBuilt = isBuilt;
|
2014-03-06 15:40:18 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
2014-02-27 09:55:54 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 08:20:00 +00:00
|
|
|
QLineF *QetShapeItem::getLine()
|
|
|
|
{
|
|
|
|
QRectF rect = boundingRect();
|
|
|
|
QLineF *line = 0;
|
|
|
|
if (_shapeType == Line) {
|
|
|
|
if (_lineAngle)
|
|
|
|
line = new QLineF(rect.topRight(), rect.bottomLeft());
|
|
|
|
else
|
|
|
|
line = new QLineF(rect.topLeft(), rect.bottomRight());
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF *QetShapeItem::getRectangle()
|
|
|
|
{
|
|
|
|
QRectF rect = boundingRect();
|
|
|
|
QRectF *rec = 0;
|
|
|
|
if (_shapeType == Rectangle)
|
|
|
|
rec = new QRectF(rect);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF *QetShapeItem::getEllipse()
|
|
|
|
{
|
|
|
|
QRectF rect = boundingRect();
|
|
|
|
QRectF *rec = 0;
|
|
|
|
if (_shapeType == Ellipse)
|
|
|
|
rec = new QRectF(rect);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
2014-02-23 18:55:26 +00:00
|
|
|
QRectF QetShapeItem::boundingRect() const
|
|
|
|
{
|
|
|
|
return _boundingRect;
|
|
|
|
}
|
|
|
|
|
2014-02-27 09:55:54 +00:00
|
|
|
QPainterPath QetShapeItem::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
QPainterPathStroker pps;
|
|
|
|
QRectF rect = boundingRect();
|
|
|
|
|
|
|
|
switch (_shapeType) {
|
|
|
|
case Line:
|
|
|
|
if (_lineAngle) {
|
|
|
|
path.moveTo(rect.topRight());
|
|
|
|
path.lineTo(rect.bottomLeft());
|
|
|
|
} else {
|
|
|
|
path.moveTo(rect.topLeft());
|
|
|
|
path.lineTo(rect.bottomRight());
|
|
|
|
}
|
|
|
|
//use @pps for grab line with bigger outerline
|
|
|
|
//more usefull
|
|
|
|
pps.setWidth(10);
|
|
|
|
path= pps.createStroke(path);
|
|
|
|
break;
|
|
|
|
case Rectangle:
|
|
|
|
path = QetGraphicsItem::shape();
|
|
|
|
break;
|
|
|
|
case Ellipse:
|
|
|
|
path.addEllipse(rect);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
path = QetGraphicsItem::shape();
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2014-02-23 18:55:26 +00:00
|
|
|
void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2014-02-28 14:30:59 +00:00
|
|
|
if (!_writingXml) {
|
|
|
|
painter -> setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
QRectF rec = boundingRect();
|
|
|
|
QPen pen(Qt::black);
|
2014-03-21 02:28:47 +00:00
|
|
|
|
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
pen.setWidthF(1);
|
|
|
|
#endif
|
2014-03-19 14:56:06 +00:00
|
|
|
|
2014-02-28 14:30:59 +00:00
|
|
|
if (isSelected())
|
|
|
|
pen.setColor(Qt::red);
|
|
|
|
pen.setStyle(_shapeStyle);
|
|
|
|
painter->setPen(pen);
|
|
|
|
switch(_shapeType) {
|
|
|
|
case Line:
|
|
|
|
if (_lineAngle)
|
|
|
|
painter -> drawLine(rec.topRight(), rec.bottomLeft());
|
|
|
|
else
|
|
|
|
painter -> drawLine(rec.topLeft(), rec.bottomRight());
|
|
|
|
break;
|
|
|
|
case Rectangle:
|
|
|
|
painter -> drawRect(rec);
|
|
|
|
break;
|
|
|
|
default: //(case Ellipse:)
|
|
|
|
painter ->drawEllipse(rec);
|
|
|
|
}
|
2014-02-23 18:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 14:30:59 +00:00
|
|
|
|
2014-03-07 12:34:47 +00:00
|
|
|
void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
|
|
|
|
{
|
|
|
|
_origMousePress = mapToScene(e -> pos());
|
|
|
|
first_move_ = true;
|
|
|
|
if (e -> modifiers() & Qt::ControlModifier) {
|
|
|
|
setSelected(!isSelected());
|
|
|
|
}
|
|
|
|
QGraphicsItem::mousePressEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
|
|
|
{
|
|
|
|
if (diagram()) diagram() -> endMoveElements();
|
|
|
|
QPointF newCoord = mapToScene(e -> pos());
|
|
|
|
if (newCoord != _origMousePress) {
|
|
|
|
//translate bounding rectangle
|
|
|
|
QRectF rec = boundingRect();
|
|
|
|
rec.translate(newCoord - _origMousePress);
|
|
|
|
setBoundingRect(rec);
|
|
|
|
setPos(pos() - newCoord + _origMousePress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(e -> modifiers() & Qt::ControlModifier)) QGraphicsItem::mouseReleaseEvent(e);
|
|
|
|
}
|
|
|
|
|
2014-02-28 14:30:59 +00:00
|
|
|
|
|
|
|
bool QetShapeItem::fromXml(const QDomElement &e)
|
|
|
|
{
|
|
|
|
if (!_writingXml) {
|
|
|
|
if (e.tagName() != "shape") return (false);
|
|
|
|
|
|
|
|
_shapeType = QetShapeItem::ShapeType(e.attribute("type","0").toInt());
|
|
|
|
_shapeStyle = Qt::PenStyle(e.attribute("style","0").toInt());
|
|
|
|
_lineAngle = e.attribute("lineAngle","0").toInt();
|
|
|
|
qreal x = e.attribute("x","0").toDouble();
|
|
|
|
qreal y = e.attribute("y","0").toDouble();
|
|
|
|
qreal w = e.attribute("w","0").toDouble();
|
|
|
|
qreal h = e.attribute("h","0").toDouble();
|
|
|
|
setBoundingRect(QRectF(x, y, w, h));
|
|
|
|
setFullyBuilt(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QDomElement QetShapeItem::toXml(QDomDocument &document) const {
|
|
|
|
QDomElement result = document.createElement("shape");
|
|
|
|
QRectF rec = boundingRect();
|
|
|
|
|
|
|
|
//write some attribute
|
|
|
|
result.setAttribute("type", QString::number(_shapeType));
|
|
|
|
result.setAttribute("x", QString::number(rec.topLeft().x()));
|
|
|
|
result.setAttribute("y", QString::number(rec.topLeft().y()));
|
|
|
|
result.setAttribute("w", QString::number(rec.width()));
|
|
|
|
result.setAttribute("h", QString::number(rec.height()));
|
|
|
|
result.setAttribute("lineAngle", QString::number(_lineAngle));
|
|
|
|
result.setAttribute("style", QString::number(_shapeStyle));
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
2014-03-07 08:05:25 +00:00
|
|
|
|
|
|
|
void QetShapeItem::editProperty()
|
|
|
|
{
|
|
|
|
if (diagram() -> isReadOnly()) return;
|
|
|
|
|
|
|
|
//the dialog
|
|
|
|
QDialog property_dialog(diagram()->views().at(0));
|
2014-03-08 13:45:36 +00:00
|
|
|
property_dialog.setWindowTitle(tr("\311diter les propri\351t\351s d'une liaison, Zone ", "window title"));
|
2014-03-07 08:05:25 +00:00
|
|
|
//the main layout
|
|
|
|
QVBoxLayout dialog_layout(&property_dialog);
|
|
|
|
|
|
|
|
//GroupBox for resizer image
|
2014-05-03 00:37:57 +00:00
|
|
|
QGroupBox restyle_groupe(QObject::tr("Type de ligne", "shape style"));
|
2014-03-07 08:05:25 +00:00
|
|
|
dialog_layout.addWidget(&restyle_groupe);
|
|
|
|
QHBoxLayout restyle_layout(&restyle_groupe);
|
|
|
|
|
|
|
|
QComboBox style_combo(&property_dialog);
|
2014-05-03 00:37:57 +00:00
|
|
|
style_combo.addItem(QObject::tr("Normal"));
|
|
|
|
style_combo.addItem(QObject::tr("Tiret"));
|
|
|
|
style_combo.addItem(QObject::tr("Pointill\351"));
|
|
|
|
style_combo.addItem(QObject::tr("Traits et points"));
|
|
|
|
style_combo.addItem(QObject::tr("Traits points points"));
|
2014-03-07 08:05:25 +00:00
|
|
|
|
|
|
|
// The items have been added in order accordance with Qt::PenStyle.
|
|
|
|
style_combo.setCurrentIndex(int(_shapeStyle) - 1);
|
|
|
|
|
|
|
|
restyle_layout.addWidget(&style_combo);
|
|
|
|
|
|
|
|
//check box for disable move
|
|
|
|
QCheckBox cb(tr("Verrouiller la position"), &property_dialog);
|
|
|
|
cb.setChecked(!is_movable_);
|
|
|
|
dialog_layout.addWidget(&cb);
|
2014-04-29 03:47:37 +00:00
|
|
|
cb.setVisible(false);
|
2014-03-07 08:05:25 +00:00
|
|
|
|
2014-05-25 18:33:06 +00:00
|
|
|
//GroupBox for Scaling
|
|
|
|
QGroupBox scale_groupe(QObject::tr("Scale", "shape scale"));
|
|
|
|
dialog_layout.addWidget(&scale_groupe);
|
|
|
|
QHBoxLayout scale_layout(&scale_groupe);
|
|
|
|
|
|
|
|
QLabel scale_label(&property_dialog);
|
|
|
|
scale_label.setText(tr("Scale Factor"));
|
|
|
|
|
|
|
|
QLineEdit scale_lineedit(&property_dialog);
|
|
|
|
QDoubleValidator scale_val(0.0,1000,3, &property_dialog);
|
|
|
|
scale_lineedit.setValidator(&scale_val);
|
|
|
|
scale_lineedit.setText("1.0");
|
|
|
|
|
|
|
|
scale_layout.addWidget(&scale_label);
|
|
|
|
scale_layout.addWidget(&scale_lineedit);
|
|
|
|
|
2014-03-07 08:05:25 +00:00
|
|
|
//dialog button, box
|
|
|
|
QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
|
|
|
dialog_layout.addWidget(&dbb);
|
|
|
|
connect(&dbb, SIGNAL(accepted()), &property_dialog, SLOT(accept()));
|
|
|
|
connect(&dbb, SIGNAL(rejected()), &property_dialog, SLOT(reject()));
|
|
|
|
|
|
|
|
//dialog is accepted...
|
|
|
|
if (property_dialog.exec() == QDialog::Accepted) {
|
|
|
|
cb.isChecked() ? is_movable_=false : is_movable_=true;
|
|
|
|
|
|
|
|
Qt::PenStyle new_style = Qt::PenStyle(style_combo.currentIndex() + 1);
|
|
|
|
if (new_style != _shapeStyle)
|
|
|
|
diagram()->undoStack().push(new ChangeShapeStyleCommand(this, _shapeStyle, new_style));
|
2014-05-25 18:33:06 +00:00
|
|
|
double scale_factor = scale_lineedit.text().toDouble();
|
|
|
|
if (scale_factor != 1 && scale_factor > 0 && scale_factor < 1000 )
|
|
|
|
diagram()->undoStack().push(new ChangeShapeScaleCommand(this, scale_factor));
|
2014-03-07 08:05:25 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|