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

@@ -33,7 +33,6 @@
#include "qetapp.h"
#include "qetgraphicsitem/diagramimageitem.h"
#include "qetgraphicsitem/qetshapeitem.h"
#include "qetgraphicsitem/crossrefitem.h"
const int Diagram::xGrid = 10;
const int Diagram::yGrid = 10;
@@ -101,8 +100,6 @@ Diagram::~Diagram() {
foreach(QGraphicsItem *qgi, items()) {
if (qgi -> parentItem()) continue;
if (qgraphicsitem_cast<Conductor *>(qgi)) continue;
else if (qgraphicsitem_cast<CrossRefItem *>(qgi)) continue;
deletable_items << qgi;
}

View File

@@ -22,8 +22,7 @@
* Default Constructor
*/
XRefProperties::XRefProperties()
{
}
{}
/**
* @brief XRefProperties::toSettings
@@ -35,6 +34,8 @@ void XRefProperties::toSettings(QSettings &settings, const QString prefix) const
settings.setValue(prefix + "showpowerctc", m_show_power_ctc);
QString display = m_display == Cross? "cross" : "contacts";
settings.setValue(prefix + "displayhas", display);
QString snap = m_snap_to == Bottom? "bottom" : "label";
settings.setValue(prefix + "snapto", snap);
settings.setValue(prefix + "powerprefix", m_prefix.value("power"));
settings.setValue(prefix + "delayprefix", m_prefix.value("delay"));
}
@@ -49,6 +50,8 @@ void XRefProperties::fromSettings(const QSettings &settings, const QString prefi
m_show_power_ctc = settings.value(prefix + "showpowerctc", false).toBool();
QString display = settings.value(prefix + "displayhas", "cross").toString();
display == "cross"? m_display = Cross : m_display = Contacts;
QString snap = settings.value(prefix + "snapto", "label").toString();
snap == "bottom"? m_snap_to = Bottom : m_snap_to = Label;
m_prefix.insert("power", settings.value(prefix + "powerprefix").toString());
m_prefix.insert("delay", settings.value(prefix + "delayprefix").toString());
}
@@ -62,6 +65,8 @@ void XRefProperties::toXml(QDomElement &xml_element) const {
xml_element.setAttribute("showpowerctc", m_show_power_ctc? "true" : "fasle");
QString display = m_display == Cross? "cross" : "contacts";
xml_element.setAttribute("displayhas", display);
QString snap = m_snap_to == Bottom? "bottom" : "label";
xml_element.setAttribute("snapto", snap);
xml_element.setAttribute("powerprefix", m_prefix.value("power"));
xml_element.setAttribute("delayprefix", m_prefix.value("delay"));
}
@@ -75,6 +80,8 @@ void XRefProperties::fromXml(const QDomElement &xml_element) {
m_show_power_ctc = xml_element.attribute("showpowerctc") == "true";
QString display = xml_element.attribute("displayhas", "cross");
display == "cross"? m_display = Cross : m_display = Contacts;
QString snap = xml_element.attribute("snapto", "label");
snap == "bottom"? m_snap_to = Bottom : m_snap_to = Label;
m_prefix.insert("power", xml_element.attribute("powerprefix"));
m_prefix.insert("delay", xml_element.attribute("delayprefix"));
}
@@ -82,6 +89,7 @@ void XRefProperties::fromXml(const QDomElement &xml_element) {
bool XRefProperties::operator ==(const XRefProperties &xrp) const{
return (m_show_power_ctc == xrp.m_show_power_ctc &&
m_display == xrp.m_display &&
m_snap_to == xrp.m_snap_to &&
m_prefix == xrp.m_prefix);
}

View File

@@ -34,6 +34,11 @@ class XRefProperties : public PropertiesInterface
Contacts
};
enum SnapTo {
Bottom,
Label
};
virtual void toSettings (QSettings &settings, const QString = QString()) const;
virtual void fromSettings (const QSettings &settings, const QString = QString());
virtual void toXml (QDomElement &xml_element) const;
@@ -45,8 +50,11 @@ class XRefProperties : public PropertiesInterface
void setShowPowerContac (const bool a) {m_show_power_ctc = a;}
bool showPowerContact () const {return m_show_power_ctc;}
void setDisplayHas (const DisplayHas dh) {m_display = dh;}
DisplayHas displayHas () const {return m_display;}
void setDisplayHas (const DisplayHas dh) {m_display = dh;}
DisplayHas displayHas () const {return m_display;}
void setSnapTo (const SnapTo st) {m_snap_to = st;}
SnapTo snapTo () const {return m_snap_to;}
void setPrefix (const QString &key, const QString &value) {m_prefix.insert(key, value);}
QString prefix (const QString &key) const {return m_prefix.value(key);}
@@ -54,6 +62,7 @@ class XRefProperties : public PropertiesInterface
private:
bool m_show_power_ctc;
DisplayHas m_display;
SnapTo m_snap_to;
QHash <QString, QString> m_prefix;
};

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);
}
/**

View File

@@ -31,6 +31,9 @@ XRefPropertiesWidget::XRefPropertiesWidget(XRefProperties properties, QWidget *p
m_properties(properties)
{
ui->setupUi(this);
ui->m_snap_to_cb->addItem(tr("En bas de page"), "bottom");
ui->m_snap_to_cb->addItem(tr("Sous le label de l'\351l\351ment"), "label");
connect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_cross_properties_gb, SLOT(setEnabled(bool)));
updateDisplay();
}
@@ -62,6 +65,9 @@ void XRefPropertiesWidget::setProperties(const XRefProperties &properties) {
XRefProperties XRefPropertiesWidget::properties() {
if (ui->m_display_has_cross_rb->isChecked()) m_properties.setDisplayHas(XRefProperties::Cross);
else if (ui->m_display_has_contacts_rb->isChecked()) m_properties.setDisplayHas(XRefProperties::Contacts);
if (ui->m_snap_to_cb->itemData(ui->m_snap_to_cb->currentIndex()).toString() == "bottom")
m_properties.setSnapTo(XRefProperties::Bottom);
else m_properties.setSnapTo(XRefProperties::Label);
m_properties.setShowPowerContac(ui->m_show_power_cb->isChecked());
m_properties.setPrefix("power", ui->m_power_prefix_le->text());
m_properties.setPrefix("delay", ui->m_delay_prefix_le->text());
@@ -97,6 +103,9 @@ void XRefPropertiesWidget::updateDisplay() {
ui->m_display_has_contacts_rb->setChecked(true);
}
if (m_properties.snapTo() == XRefProperties::Bottom)
ui->m_snap_to_cb->setCurrentIndex(ui->m_snap_to_cb->findData("bottom"));
else ui->m_snap_to_cb->setCurrentIndex(ui->m_snap_to_cb->findData("label"));
ui->m_show_power_cb->setChecked(m_properties.showPowerContact());
ui->m_power_prefix_le->setText(m_properties.prefix("power"));
ui->m_delay_prefix_le->setText(m_properties.prefix("delay"));

View File

@@ -20,6 +20,20 @@
<string>Représentation:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Positionner :</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="m_snap_to_cb"/>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>