Xref item: item position can be set under the texte field "label" of master element.

Double clic in xref open the properties dialog of master.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3183 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2014-06-27 18:34:18 +00:00
parent 76371b8740
commit 8379163061
12 changed files with 139 additions and 77 deletions

View File

@@ -19,6 +19,7 @@
#include "element.h"
#include "qetapp.h"
#include "diagramposition.h"
#include "elementtextitem.h"
//define the height of the header.
#define header 5
@@ -28,19 +29,24 @@
/**
* @brief CrossRefItem::CrossRefItem
* Default constructor
* @param elmt element to dispaly the cross ref
* @param parent parent QetGraphicsItem
* @param elmt element to display the cross ref and also parent item.
*/
CrossRefItem::CrossRefItem(Element *elmt, QGraphicsItem *parent) :
QGraphicsObject(parent),
CrossRefItem::CrossRefItem(Element *elmt) :
QGraphicsObject(elmt),
m_element (elmt)
{
m_properties = elmt->diagram()->defaultXRefProperties();
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
connect(elmt, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
connect(elmt->diagram()->project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
connect(elmt->diagram(), SIGNAL(XRefPropertiesChanged(XRefProperties)), this, SLOT(setProperties(XRefProperties)));
//set specific behavior related to the parent item.
if(m_properties.snapTo() == XRefProperties::Bottom) {
connect(elmt, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
connect(elmt, SIGNAL(rotationChanged()), this, SLOT(autoPos()));
} else {
setTextParent();
}
updateLabel();
}
@@ -49,7 +55,10 @@ CrossRefItem::CrossRefItem(Element *elmt, QGraphicsItem *parent) :
* Default destructor
*/
CrossRefItem::~CrossRefItem() {
disconnect(m_element, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
if(m_properties.snapTo() == XRefProperties::Bottom) {
disconnect(m_element, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
disconnect(m_element, SIGNAL(rotationChanged()), this, SLOT(autoPos()));
}
disconnect(m_element, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
disconnect(m_element->diagram()->project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
disconnect(m_element->diagram(), SIGNAL(XRefPropertiesChanged(XRefProperties)), this, SLOT(setProperties(XRefProperties)));
@@ -112,6 +121,17 @@ void CrossRefItem::allElementsPositionText(QString &no_str, QString &nc_str, con
void CrossRefItem::setProperties(const XRefProperties &xrp) {
if (m_properties != xrp) {
if (m_properties.snapTo() != xrp.snapTo()) {
if (xrp.snapTo() == XRefProperties::Bottom) {
setParentItem(m_element);
connect(m_element, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
connect(m_element, SIGNAL(rotationChanged()), this, SLOT(autoPos()));
} else {
setTextParent();
disconnect(m_element, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
disconnect(m_element, SIGNAL(rotationChanged()), this, SLOT(autoPos()));
}
}
m_properties = xrp;
updateLabel();
}
@@ -153,24 +173,28 @@ void CrossRefItem::updateLabel() {
* Calculate and set position automaticaly.
*/
void CrossRefItem::autoPos() {
if (isSelected() && m_element->isSelected()) return;
QRectF border= m_element->diagram()->border();
QPointF point;
//We calcul the position according to the @snapTo of the xrefproperties
if (m_properties.snapTo() == XRefProperties::Bottom) {
QRectF border = m_element->diagram()->border();
QPointF point = m_element->sceneBoundingRect().center();
//if this item have parent calcule the position by using mapped point.
if(parentItem()) {
point = m_element->boundingRect().center();
QPointF ypoint_ = mapToParent(mapFromScene(0, border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height()));
point.setY(ypoint_.y());
point.setY(border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height());
point.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative
setPos(0,0); //Due to a weird behavior or bug, before set the new position and rotation,
setRotation(0); //we must to set the position and rotation at 0.
setPos(mapFromScene(point));
if (rotation() != - m_element->rotation()) {
setRotation(0);
setRotation(- m_element->rotation());
}
}
else {
point = m_element->sceneBoundingRect().center();
point.setY(border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height());
QPointF p = parentItem()->boundingRect().center();
p.ry() += parentItem()->boundingRect().height()/2;
p.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative
setPos(p);
}
qreal offset = m_bounding_rect.topLeft().x() < 0 ? m_bounding_rect.topLeft().x() : 0;
point.setX(point.x() - m_bounding_rect.width()/2 - offset);
setPos(point);
}
/**
@@ -183,39 +207,16 @@ void CrossRefItem::autoPos() {
void CrossRefItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
Q_UNUSED(option);
Q_UNUSED(widget);
//draw the selection rect
if (isSelected()) {
painter->save();
QPen t(Qt::black);
t.setStyle(Qt::DashLine);
t.setCosmetic(true);
painter -> setPen(t);
painter -> setRenderHint(QPainter::Antialiasing, false);
painter -> drawPath(m_shape_path);
painter -> restore();
}
m_drawing.play(painter);
}
/**
* @brief CrossRefItem::mouseMoveEvent
* handle mouse move event
* @param e event
* @brief CrossRefItem::mouseDoubleClickEvent
* @param event
*/
void CrossRefItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
m_element->setHighlighted(true);
QGraphicsObject::mouseMoveEvent(e);
}
/**
* @brief CrossRefItem::mouseReleaseEvent
* handle mouse release event
* @param e event
*/
void CrossRefItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
m_element->setHighlighted(false);
QGraphicsObject::mouseReleaseEvent(e);
void CrossRefItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
event->accept();
m_element->editProperty();
}
/**
@@ -526,3 +527,14 @@ void CrossRefItem::checkMustShow() {
}
}
/**
* @brief CrossRefItem::setTextParent
* Set the text field tagged "label" of m_element
* parent of this item
*/
void CrossRefItem::setTextParent() {
ElementTextItem *eti = m_element->taggedText("label");
if (eti) setParentItem(eti);
else qDebug() << "CrossRefItem,no texte tagged 'label' found to set has parent";
}

View File

@@ -27,8 +27,11 @@ class element;
* This clas provide an item, for show the cross reference, like the contacts linked to a coil.
* The item setpos automaticaly when parent move.
* All slave displayed in cross ref will be updated when folio position change in the project.
* It's the responsability of the parent to informe displayed slave are moved,
* It's the responsability of the master element to informe displayed slave are moved,
* by calling the slot @updateLabel
* By default master element is the parent graphics item of this Xref,
* but if the Xref must be snap to the label of master, the label become the parent of this Xref.
* This behavior can be changed at anytime by calling setProperties.
*/
class CrossRefItem : public QGraphicsObject
{
@@ -36,7 +39,7 @@ class CrossRefItem : public QGraphicsObject
//Methods
public:
explicit CrossRefItem(Element *elmt, QGraphicsItem *parent = 0);
explicit CrossRefItem(Element *elmt);
~CrossRefItem();
enum { Type = UserType + 1009 };
@@ -63,9 +66,8 @@ class CrossRefItem : public QGraphicsObject
void autoPos ();
protected:
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *e);
virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *e);
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event );
private:
void buildHeaderContact ();
@@ -76,6 +78,7 @@ class CrossRefItem : public QGraphicsObject
void fillCrossRef (QPainter &painter);
void AddExtraInfo (QPainter &painter);
void checkMustShow ();
void setTextParent ();
//Attributes
private:

View File

@@ -910,18 +910,28 @@ void CustomElement::setPainterStyle(QDomElement &e, QPainter &qp) {
/**
* @brief CustomElement::setTaggedText
* Set text @newstr to the text tagged with @tagg.
* If tagg is found return the text item, else return 0.
* If tagg is found return the text item, else return NULL.
* @param tagg required tagg
* @param newstr new label
* @param noeditable set editable or not (by default, set editable)
*/
ElementTextItem* CustomElement::setTaggedText(const QString &tagg, const QString &newstr, const bool noeditable) {
foreach (ElementTextItem *eti, list_texts_) {
if (eti -> tagg() == tagg) {
eti -> setPlainText(newstr);
eti -> setNoEditable(noeditable);
return eti;
}
ElementTextItem *eti = taggedText(tagg);
if (eti) {
eti -> setPlainText(newstr);
eti -> setNoEditable(noeditable);
}
return 0;
return eti;
}
/**
* @brief CustomElement::taggedText
* return the text field tagged with @tagg or NULL if text field isn't found
* @param tagg
*/
ElementTextItem* CustomElement::taggedText(const QString &tagg) const {
foreach (ElementTextItem *eti, list_texts_) {
if (eti -> tagg() == tagg) return eti;
}
return NULL;
}

View File

@@ -74,6 +74,7 @@ class CustomElement : public FixedElement {
bool isNull() const;
int state() const;
QString name() const;
ElementTextItem* taggedText(const QString &tagg) const;
protected:
virtual bool buildFromXml(const QDomElement &, int * = 0);

View File

@@ -74,6 +74,8 @@ class Element : public QetGraphicsItem {
virtual QList<Conductor *> conductors() const = 0;
/// @return the list of text items attached to this element
virtual QList<ElementTextItem *> texts() const = 0;
/// @return the text field tagged with @tagg or NULL if text field isn't found
virtual ElementTextItem* taggedText(const QString &tagg) const = 0;
/// @return the list of lines items in this element
virtual QList<QLineF *> lines() const = 0;
/// @return the list of rectangles items in this element

View File

@@ -54,10 +54,7 @@ void MasterElement::linkToElement(Element *elmt) {
connected_elements << elmt;
elmt->linkToElement(this);
if (!cri_) {
cri_ = new CrossRefItem(this); //create cross ref item if not yet
diagram()->addItem(cri_);
}
if (!cri_) cri_ = new CrossRefItem(this); //create cross ref item if not yet
connect(elmt, SIGNAL(positionChange(QPointF)), cri_, SLOT(updateLabel()));
cri_->updateLabel();
}
@@ -91,7 +88,6 @@ void MasterElement::unlinkElement(Element *elmt) {
disconnect(elmt, SIGNAL(positionChange(QPointF)), cri_, SLOT(updateLabel()));
if (linkedElements().isEmpty()) {
diagram()->removeItem(cri_);
delete cri_;
cri_ = nullptr;
}

View File

@@ -48,11 +48,12 @@ Diagram* QetGraphicsItem::diagram() const{
* @param p the new position of item
*/
void QetGraphicsItem::setPos(const QPointF &p) {
if (p == pos() || !is_movable_) return;
QPointF pp = Diagram::snapToGrid(p);
if (pp == pos() || !is_movable_) return;
if (scene() && snap_to_grid_) {
QGraphicsItem::setPos(Diagram::snapToGrid(p));
QGraphicsItem::setPos(pp);
emit positionChange(pos());
} else QGraphicsItem::setPos(p);
} else QGraphicsItem::setPos(pp);
}
/**