2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vgraphicssimpletextitem.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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 Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-07-13 12:51:31 +02:00
|
|
|
#include "vgraphicssimpletextitem.h"
|
2014-06-17 11:50:11 +02:00
|
|
|
#include <QFont>
|
|
|
|
#include <QBrush>
|
2014-07-25 15:37:27 +02:00
|
|
|
#include <QStyle>
|
2014-07-30 21:45:27 +02:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2014-08-29 16:10:47 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2014-10-25 12:56:27 +02:00
|
|
|
#include <QKeyEvent>
|
2015-06-15 15:59:19 +02:00
|
|
|
#include <QGraphicsScene>
|
2015-11-25 18:26:37 +01:00
|
|
|
#include <QGraphicsView>
|
2013-07-13 12:51:31 +02:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief VGraphicsSimpleTextItem default constructor.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
VGraphicsSimpleTextItem::VGraphicsSimpleTextItem(QGraphicsItem * parent)
|
|
|
|
:QGraphicsSimpleTextItem(parent), fontSize(0)
|
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
2014-10-24 20:17:23 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsFocusable, true);
|
2013-09-10 14:29:06 +02:00
|
|
|
this->setAcceptHoverEvents(true);
|
2013-10-16 11:17:34 +02:00
|
|
|
QFont font = this->font();
|
2014-07-11 15:10:54 +02:00
|
|
|
font.setPointSize(font.pointSize()+20);
|
2013-10-16 11:17:34 +02:00
|
|
|
fontSize = font.pointSize();
|
|
|
|
this->setFont(font);
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief VGraphicsSimpleTextItem constructor.
|
|
|
|
* @param text text.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
2013-09-10 14:29:06 +02:00
|
|
|
VGraphicsSimpleTextItem::VGraphicsSimpleTextItem( const QString & text, QGraphicsItem * parent )
|
2013-11-04 21:35:15 +01:00
|
|
|
:QGraphicsSimpleTextItem(text, parent), fontSize(0)
|
|
|
|
{
|
2013-07-13 12:51:31 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
2013-07-28 00:18:06 +02:00
|
|
|
this->setAcceptHoverEvents(true);
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 19:02:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VGraphicsSimpleTextItem::~VGraphicsSimpleTextItem()
|
|
|
|
{
|
|
|
|
//Disable cursor-arrow-openhand
|
2015-06-15 15:47:44 +02:00
|
|
|
RestoreOverrideCursor(cursorArrowOpenHand);
|
2015-04-29 19:02:03 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:37:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
/* From question on StackOverflow
|
|
|
|
* https://stackoverflow.com/questions/10985028/how-to-remove-border-around-qgraphicsitem-when-selected
|
|
|
|
*
|
|
|
|
* There's no interface to disable the drawing of the selection border for the build-in QGraphicsItems. The only way
|
|
|
|
* I can think of is derive your own items from the build-in ones and override the paint() function:*/
|
|
|
|
QStyleOptionGraphicsItem myOption(*option);
|
|
|
|
myOption.state &= ~QStyle::State_Selected;
|
|
|
|
QGraphicsSimpleTextItem::paint(painter, &myOption, widget);
|
|
|
|
}
|
|
|
|
|
2015-02-04 12:47:53 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
QGraphicsSimpleTextItem::setEnabled(enabled);
|
|
|
|
const QPalette palet = this->scene()->palette();
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
setBrush(palet.brush(QPalette::Active, QPalette::Text));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setBrush(palet.brush(QPalette::Disabled, QPalette::Text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief itemChange handle item change.
|
|
|
|
* @param change change.
|
|
|
|
* @param value value.
|
|
|
|
* @return value.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
QVariant VGraphicsSimpleTextItem::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (change == ItemPositionChange && scene())
|
|
|
|
{
|
2015-11-25 14:45:38 +01:00
|
|
|
// Each time we move something we call recalculation scene rect. In some cases this can cause moving
|
|
|
|
// objects positions. And this cause infinite redrawing. That's why we wait the finish of saving the last move.
|
|
|
|
static bool changeFinished = true;
|
|
|
|
if (changeFinished)
|
|
|
|
{
|
|
|
|
changeFinished = false;
|
|
|
|
QPointF newPos = value.toPointF() + this->parentItem()->pos();
|
|
|
|
emit NameChangePosition(newPos);
|
2015-11-25 18:26:37 +01:00
|
|
|
if (scene())
|
|
|
|
{
|
2015-12-05 17:27:38 +01:00
|
|
|
const QList<QGraphicsView *> viewList = scene()->views();
|
|
|
|
if (not viewList.isEmpty())
|
2015-11-25 18:26:37 +01:00
|
|
|
{
|
2015-12-05 17:27:38 +01:00
|
|
|
if (QGraphicsView *view = viewList.at(0))
|
|
|
|
{
|
|
|
|
view->ensureVisible(this);
|
|
|
|
}
|
2015-11-25 18:26:37 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-25 14:45:38 +01:00
|
|
|
changeFinished = true;
|
|
|
|
}
|
2013-07-13 12:51:31 +02:00
|
|
|
}
|
|
|
|
return QGraphicsItem::itemChange(change, value);
|
2013-07-28 00:18:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
2014-08-19 13:16:00 +02:00
|
|
|
* @brief hoverEnterEvent handle hover enter events.
|
|
|
|
* @param event hover enter event.
|
2014-06-13 19:02:41 +02:00
|
|
|
*/
|
2014-08-19 13:16:00 +02:00
|
|
|
void VGraphicsSimpleTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2015-03-19 12:13:13 +01:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
this->setBrush(Qt::green);
|
2014-08-19 13:16:00 +02:00
|
|
|
|
2015-06-15 15:47:44 +02:00
|
|
|
SetOverrideCursor(cursorArrowOpenHand, 1, 1);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
QGraphicsSimpleTextItem::hoverEnterEvent(event);
|
2013-07-28 00:18:06 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief hoverLeaveEvent handle hover leave events.
|
|
|
|
* @param event hover leave event.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VGraphicsSimpleTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2013-07-28 00:18:06 +02:00
|
|
|
Q_UNUSED(event);
|
2015-03-19 12:13:13 +01:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
this->setBrush(Qt::black);
|
2014-08-19 13:16:00 +02:00
|
|
|
|
2015-03-19 12:13:13 +01:00
|
|
|
//Disable cursor-arrow-openhand
|
2015-06-15 15:47:44 +02:00
|
|
|
RestoreOverrideCursor(cursorArrowOpenHand);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
QGraphicsSimpleTextItem::hoverLeaveEvent(event);
|
2013-07-28 00:18:06 +02:00
|
|
|
}
|
2014-01-09 13:51:09 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief contextMenuEvent handle context menu events.
|
|
|
|
* @param event context menu event.
|
|
|
|
*/
|
2014-01-09 13:51:09 +01:00
|
|
|
void VGraphicsSimpleTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
emit ShowContextMenu(event);
|
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2015-03-19 12:13:13 +01:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
2014-08-19 13:16:00 +02:00
|
|
|
{
|
2015-03-19 12:13:13 +01:00
|
|
|
if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
|
|
|
{
|
2015-06-15 15:47:44 +02:00
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
|
|
|
QGraphicsSimpleTextItem::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2015-03-19 12:13:13 +01:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
2014-08-19 13:16:00 +02:00
|
|
|
{
|
2015-03-19 12:13:13 +01:00
|
|
|
if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
|
|
|
{
|
|
|
|
//Disable cursor-arrow-closehand
|
2015-06-15 15:47:44 +02:00
|
|
|
RestoreOverrideCursor(cursorArrowCloseHand);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
2015-03-19 14:02:39 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
emit PointChoosed();
|
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
QGraphicsSimpleTextItem::mouseReleaseEvent(event);
|
|
|
|
}
|
2014-10-24 20:17:23 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
emit DeleteTool();
|
|
|
|
return; //Leave this method immediately after call!!!
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QGraphicsSimpleTextItem::keyReleaseEvent ( event );
|
|
|
|
}
|