mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-20 08:10:52 +01:00
Element editor : Element primitive decorator doesn't manage anymore the text field when a single text is selected
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5000 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -26,20 +26,17 @@
|
||||
Constructeur
|
||||
@param editor L'editeur d'element concerne
|
||||
@param parent Le QGraphicsItem parent de ce texte statique
|
||||
@param scene La scene sur laquelle figure ce texte statique
|
||||
*/
|
||||
PartText::PartText(QETElementEditor *editor, QGraphicsItem *parent, ElementScene *scene) :
|
||||
PartText::PartText(QETElementEditor *editor, QGraphicsItem *parent) :
|
||||
QGraphicsTextItem(parent),
|
||||
CustomElementPart(editor),
|
||||
previous_text(),
|
||||
decorator_(0)
|
||||
previous_text()
|
||||
{
|
||||
Q_UNUSED(scene)
|
||||
document() -> setDocumentMargin(1.0);
|
||||
setDefaultTextColor(Qt::black);
|
||||
setFont(QETApp::diagramTextsFont());
|
||||
real_font_size_ = font().pointSize();
|
||||
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
|
||||
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsMovable);
|
||||
setAcceptHoverEvents(true);
|
||||
setDefaultTextColor(Qt::black);
|
||||
setPlainText(QObject::tr("T", "default text when adding a text in the element editor"));
|
||||
@@ -231,150 +228,37 @@ void PartText::handleUserTransformation(const QRectF &initial_selection_rect, co
|
||||
setProperty("real_size", qMax(1, qRound(new_font_size)));
|
||||
}
|
||||
|
||||
/**
|
||||
Dessine le texte statique.
|
||||
@param painter QPainter a utiliser pour effectuer le rendu
|
||||
@param qsogi Pptions de dessin
|
||||
@param widget Widget sur lequel on dessine (facultatif)
|
||||
*/
|
||||
void PartText::paint(QPainter *painter, const QStyleOptionGraphicsItem *qsogi, QWidget *widget) {
|
||||
// According to the source code of QGraphicsTextItem::paint(), this should
|
||||
// avoid the drawing of the dashed rectangle around the text.
|
||||
QStyleOptionGraphicsItem our_qsogi(*qsogi);
|
||||
our_qsogi.state = QStyle::State_None;
|
||||
|
||||
QGraphicsTextItem::paint(painter, &our_qsogi, widget);
|
||||
|
||||
#ifdef QET_DEBUG_EDITOR_TEXTS
|
||||
painter -> setPen(Qt::blue);
|
||||
painter -> drawRect(boundingRect());
|
||||
|
||||
painter -> setPen(Qt::red);
|
||||
drawPoint(painter, QPointF(0, 0));
|
||||
|
||||
painter -> setPen(Qt::green);
|
||||
drawPoint(painter, mapFromScene(pos()));
|
||||
#endif
|
||||
|
||||
void PartText::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable))
|
||||
{
|
||||
QPointF pos = event->scenePos() + (m_origine_pos - event->buttonDownScenePos(Qt::LeftButton));
|
||||
event->modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene()->snapToGrid(pos));
|
||||
}
|
||||
else
|
||||
QGraphicsObject::mouseMoveEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
Handle context menu events.
|
||||
@param event Object describing the context menu event to handle.
|
||||
*/
|
||||
void PartText::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
|
||||
Q_UNUSED(event);
|
||||
void PartText::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if(event->button() == Qt::LeftButton)
|
||||
m_origine_pos = this->pos();
|
||||
|
||||
QGraphicsObject::mousePressEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
Handle events generated when the mouse hovers over the decorator.
|
||||
@param event Object describing the hover event.
|
||||
*/
|
||||
void PartText::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
|
||||
// force the cursor when the text is being edited
|
||||
if (hasFocus() && decorator_) {
|
||||
decorator_ -> setCursor(Qt::IBeamCursor);
|
||||
}
|
||||
QGraphicsTextItem::hoverMoveEvent(event);
|
||||
}
|
||||
void PartText::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if((event->button() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable) && m_origine_pos != pos())
|
||||
{
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(this, "pos", QVariant(m_origine_pos), QVariant(pos()));
|
||||
undo->setText(tr("Déplacer un texte"));
|
||||
undo->enableAnimation();
|
||||
elementScene()->undoStack().push(undo);
|
||||
}
|
||||
|
||||
/**
|
||||
@reimp CustomElementPart::setDecorator(ElementPrimitiveDecorator *)
|
||||
Install or remove a sceneEventFilter on the decorator and ensure it will
|
||||
adjust itself while the text is being edited.
|
||||
*/
|
||||
void PartText::setDecorator(ElementPrimitiveDecorator *decorator) {
|
||||
if (decorator) {
|
||||
decorator -> installSceneEventFilter(this);
|
||||
// ensure the decorator will adjust itself when the text area expands or shrinks
|
||||
connect(document(), SIGNAL(contentsChanged()), decorator, SLOT(adjust()));
|
||||
}
|
||||
else {
|
||||
decorator_ -> removeSceneEventFilter(this);
|
||||
endEdition();
|
||||
}
|
||||
decorator_ = decorator;
|
||||
}
|
||||
|
||||
/**
|
||||
@reimp QGraphicsItem::sceneEventFilter(QGraphicsItem *, QEvent *).
|
||||
Intercepts events before they reach the watched target, i.e. typically the
|
||||
primitives decorator.
|
||||
This method mainly works with key strokes (F2, escape) and double clicks to
|
||||
begin or end text edition.
|
||||
*/
|
||||
bool PartText::sceneEventFilter(QGraphicsItem *watched, QEvent *event) {
|
||||
if (watched != decorator_) return(false);
|
||||
|
||||
QPointF event_scene_pos = QET::graphicsSceneEventPos(event);
|
||||
if (!event_scene_pos.isNull()) {
|
||||
if (contains(mapFromScene(event_scene_pos))) {
|
||||
if (hasFocus()) {
|
||||
return sceneEvent(event); // manually deliver the event to this item
|
||||
return(true); // prevent this event from being delivered to any item
|
||||
} else {
|
||||
if (event -> type() == QEvent::GraphicsSceneMouseDoubleClick) {
|
||||
mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (event -> type() == QEvent::KeyRelease || event -> type() == QEvent::KeyPress) {
|
||||
// Intercept F2 and escape keystrokes to focus in and out
|
||||
QKeyEvent *key_event = static_cast<QKeyEvent *>(event);
|
||||
if (!hasFocus() && key_event -> key() == Qt::Key_F2) {
|
||||
setEditable(true);
|
||||
QTextCursor qtc = textCursor();
|
||||
qtc.setPosition(qMax(0, document()->characterCount() - 1));
|
||||
setTextCursor(qtc);
|
||||
} else if (hasFocus() && key_event -> key() == Qt::Key_Escape) {
|
||||
endEdition();
|
||||
}
|
||||
if (hasFocus()) {
|
||||
sceneEvent(event); // manually deliver the event to this item
|
||||
return(true); // prevent this event from being delivered to any item
|
||||
}
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
/**
|
||||
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
||||
*/
|
||||
bool PartText::singleItemPressEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED(decorator)
|
||||
Q_UNUSED(event)
|
||||
return(hasFocus());
|
||||
}
|
||||
|
||||
/**
|
||||
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
||||
*/
|
||||
bool PartText::singleItemMoveEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED(decorator)
|
||||
Q_UNUSED(event)
|
||||
return(hasFocus());
|
||||
}
|
||||
|
||||
/**
|
||||
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
||||
*/
|
||||
bool PartText::singleItemReleaseEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED(decorator)
|
||||
Q_UNUSED(event)
|
||||
return(hasFocus());
|
||||
}
|
||||
|
||||
/**
|
||||
Accept the mouse \a event relayed by \a decorator if this text item has focus.
|
||||
*/
|
||||
bool PartText::singleItemDoubleClickEvent(ElementPrimitiveDecorator *decorator, QGraphicsSceneMouseEvent *event) {
|
||||
Q_UNUSED(decorator)
|
||||
// calling mouseDoubleClickEvent() will set this text item editable and grab keyboard focus
|
||||
if (event -> button() == Qt::LeftButton) {
|
||||
mouseDoubleClickEvent(event);
|
||||
return(true);
|
||||
}
|
||||
return(false);
|
||||
QGraphicsObject::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,27 +326,4 @@ void PartText::endEdition()
|
||||
setTextCursor(qtc);
|
||||
|
||||
setEditable(false);
|
||||
if (decorator_) {
|
||||
decorator_ -> setFocus();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef QET_DEBUG_EDITOR_TEXTS
|
||||
/**
|
||||
Dessine deux petites fleches pour mettre un point en valeur
|
||||
@param painter QPainter a utiliser pour effectuer le rendu
|
||||
@param point Point a dessiner
|
||||
*/
|
||||
void PartText::drawPoint(QPainter *painter, const QPointF &point) {
|
||||
qreal px = point.x();
|
||||
qreal py = point.y();
|
||||
qreal size_1 = 5.0;
|
||||
qreal size_2 = 1.0;
|
||||
painter -> drawLine(QLineF(px, py, px + size_1, py));
|
||||
painter -> drawLine(QLineF(px + size_1 - size_2, py - size_2, px + size_1, py));
|
||||
painter -> drawLine(QLineF(px + size_1 - size_2, py + size_2, px + size_1, py));
|
||||
painter -> drawLine(QLineF(px, py, px, py + size_1));
|
||||
painter -> drawLine(QLineF(px, py + size_1, px - size_2, py + size_1 - size_2));
|
||||
painter -> drawLine(QLineF(px, py + size_1, px + size_2, py + size_1 - size_2));
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user