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
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2013-11-15 13:41:26 +01:00
|
|
|
** 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"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QFlags>
|
2014-06-17 11:50:11 +02:00
|
|
|
#include <QFont>
|
2015-06-15 15:59:19 +02:00
|
|
|
#include <QGraphicsScene>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2015-11-25 18:26:37 +01:00
|
|
|
#include <QGraphicsView>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QPoint>
|
|
|
|
#include <QPolygonF>
|
|
|
|
#include <QRectF>
|
|
|
|
#include <Qt>
|
2013-07-13 12:51:31 +02:00
|
|
|
|
2016-01-15 13:57:47 +01:00
|
|
|
#include "vmaingraphicsscene.h"
|
|
|
|
#include "vmaingraphicsview.h"
|
2017-06-19 14:26:29 +02:00
|
|
|
#include "global.h"
|
2016-01-15 13:57:47 +01: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)
|
2017-06-19 16:44:18 +02:00
|
|
|
:QGraphicsSimpleTextItem(parent), m_fontSize(0), selectionType(SelectionType::ByMouseRelease)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-09-10 14:29:06 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
2016-05-08 17:04:18 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
|
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);
|
2017-06-19 16:44:18 +02:00
|
|
|
m_fontSize = font.pointSize();
|
2013-10-16 11:17:34 +02:00
|
|
|
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 )
|
2017-06-19 16:44:18 +02:00
|
|
|
:QGraphicsSimpleTextItem(text, parent), m_fontSize(0), selectionType(SelectionType::ByMouseRelease)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-06-19 14:26:29 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
const qreal scale = SceneScale(scene());
|
|
|
|
if (scale > 1)
|
|
|
|
{
|
2017-09-30 20:13:22 +02:00
|
|
|
setScale(1/scale);
|
2017-06-19 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsSimpleTextItem::paint(painter, option, 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:10:20 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGraphicsSimpleTextItem::LabelSelectionType(const SelectionType &type)
|
|
|
|
{
|
|
|
|
selectionType = type;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-10-02 15:25:18 +02:00
|
|
|
if (VMainGraphicsView *view = qobject_cast<VMainGraphicsView *>(viewList.at(0)))
|
2015-12-05 17:27:38 +01:00
|
|
|
{
|
2017-10-02 15:25:18 +02:00
|
|
|
int xmargin = 50;
|
|
|
|
int ymargin = 50;
|
|
|
|
|
|
|
|
const int scale = qRound(SceneScale(scene()));
|
|
|
|
if (scale > 1)
|
|
|
|
{
|
|
|
|
xmargin /= scale;
|
|
|
|
ymargin /= scale;
|
|
|
|
}
|
2016-01-15 13:57:47 +01:00
|
|
|
|
|
|
|
const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view);
|
|
|
|
const QRectF itemRect = mapToScene(boundingRect()).boundingRect();
|
|
|
|
|
|
|
|
// If item's rect is bigger than view's rect ensureVisible works very unstable.
|
|
|
|
if (itemRect.height() + 2*ymargin < viewRect.height() &&
|
|
|
|
itemRect.width() + 2*xmargin < viewRect.width())
|
|
|
|
{
|
2017-10-02 15:25:18 +02:00
|
|
|
view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, ymargin);
|
2016-01-15 13:57:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure visible only small rect around a cursor
|
|
|
|
VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(scene());
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(currentScene)
|
2016-01-15 13:57:47 +01:00
|
|
|
const QPointF cursorPosition = currentScene->getScenePos();
|
2017-10-02 15:25:18 +02:00
|
|
|
|
|
|
|
view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10),
|
|
|
|
VMainGraphicsView::scrollDelay);
|
2016-01-15 13:57:47 +01:00
|
|
|
}
|
2015-12-05 17:27:38 +01:00
|
|
|
}
|
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
|
|
|
}
|
2016-04-05 19:14:12 +02:00
|
|
|
if (change == QGraphicsItem::ItemSelectedChange)
|
|
|
|
{
|
|
|
|
emit PointSelected(value.toBool());
|
|
|
|
}
|
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)
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowOpenHand, 1, 1);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2016-03-31 16:01:41 +02:00
|
|
|
this->setBrush(Qt::green);
|
2017-08-23 13:50:48 +02:00
|
|
|
|
|
|
|
if(QGraphicsItem *parent = parentItem())
|
|
|
|
{
|
|
|
|
setToolTip(parent->toolTip());
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-03-19 12:13:13 +01:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
setCursor(QCursor());
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2016-03-31 16:01:41 +02:00
|
|
|
this->setBrush(Qt::black);
|
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)
|
|
|
|
{
|
2016-03-31 16:01:41 +02:00
|
|
|
// Special for not selectable item first need to call standard mousePressEvent then accept event
|
|
|
|
QGraphicsSimpleTextItem::mousePressEvent(event);
|
2016-05-24 11:43:30 +02:00
|
|
|
|
|
|
|
// Somehow clicking on notselectable object do not clean previous selections.
|
|
|
|
if (not (flags() & ItemIsSelectable) && scene())
|
|
|
|
{
|
|
|
|
scene()->clearSelection();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
2016-03-31 19:10:20 +02:00
|
|
|
if (selectionType == SelectionType::ByMouseRelease)
|
|
|
|
{
|
|
|
|
event->accept(); // This help for not selectable items still receive mouseReleaseEvent events
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit PointChoosed();
|
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
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)
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowOpenHand, 1, 1);
|
2015-03-19 12:13:13 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
2016-02-11 12:05:08 +01:00
|
|
|
|
2016-03-31 19:10:20 +02:00
|
|
|
if (selectionType == SelectionType::ByMouseRelease)
|
|
|
|
{
|
|
|
|
emit PointChoosed();
|
|
|
|
}
|
2016-03-31 16:01:41 +02:00
|
|
|
|
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 );
|
|
|
|
}
|