valentina_old/widgets/vtablegraphicsview.cpp

209 lines
6.4 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vtablegraphicsview.cpp
** @author Roman Telezhinsky <dismine@gmail.com>
** @date November 15, 2013
**
** @brief
** @copyright
** This source code is part of the Valentine project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2013 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** 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.
**
** Valentina is distributed in the hope that it will be useful,
** 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-08-29 12:31:50 +02:00
#include "vtablegraphicsview.h"
VTableGraphicsView::VTableGraphicsView(QGraphicsScene* pScene, QWidget *parent)
:QGraphicsView(pScene, parent)
{
2013-08-29 12:31:50 +02:00
QGraphicsView::setResizeAnchor(QGraphicsView::AnchorUnderMouse);
connect(pScene, &QGraphicsScene::selectionChanged, this, &VTableGraphicsView::selectionChanged);
}
void VTableGraphicsView::selectionChanged()
{
2013-08-29 12:31:50 +02:00
QList<QGraphicsItem *> listSelectedItems = scene()->selectedItems();
if ( listSelectedItems.isEmpty() == true )
{
qDebug() << tr("detail don't find");
2013-08-29 12:31:50 +02:00
emit itemChect(true);
}
else
{
qDebug() << tr("detail find");
2013-08-29 12:31:50 +02:00
emit itemChect(false);
}
}
void VTableGraphicsView::MirrorItem()
{
2013-08-29 12:31:50 +02:00
QList<QGraphicsItem *> list = scene()->selectedItems();
if (list.size()>0)
{
for ( qint32 i = 0; i < list.count(); ++i )
{
QGraphicsItem *item = list.at(i);
Q_ASSERT(item != 0);
QRectF itemRectOld = item->sceneBoundingRect();
2013-08-29 12:31:50 +02:00
//Get the current transform
QTransform transform(item->transform());
2013-08-29 12:31:50 +02:00
qreal m11 = transform.m11(); // Horizontal scaling
qreal m12 = transform.m12(); // Vertical shearing
qreal m13 = transform.m13(); // Horizontal Projection
qreal m21 = transform.m21(); // Horizontal shearing
qreal m22 = transform.m22(); // vertical scaling
qreal m23 = transform.m23(); // Vertical Projection
qreal m31 = transform.m31(); // Horizontal Position (DX)
qreal m32 = transform.m32(); // Vertical Position (DY)
qreal m33 = transform.m33(); // Addtional Projection Factor
// Horizontal flip
m11 = -m11;
// Write back to the matrix
transform.setMatrix(m11, m12, m13, m21, m22, m23, m31, m32, m33);
// Set the items transformation
item->setTransform(transform);
QRectF itemRectNew = item->sceneBoundingRect();
2013-08-29 12:31:50 +02:00
qreal dx, dy;
dx = itemRectOld.center().x()-itemRectNew.center().x();
dy = itemRectOld.center().y()-itemRectNew.center().y();
item->moveBy(dx, dy);
2013-08-29 12:31:50 +02:00
}
}
}
void VTableGraphicsView::wheelEvent(QWheelEvent *event)
{
if (QGuiApplication::keyboardModifiers() == Qt::ControlModifier)
{
2013-08-29 12:31:50 +02:00
// Если нажата клавиша CTRL этот код выполнится
if ((event->delta())>0)
{
2013-08-29 12:31:50 +02:00
ZoomIn();
}
else if ((event->delta())<0)
{
2013-08-29 12:31:50 +02:00
ZoomOut();
}
}
else
{
2013-08-29 12:31:50 +02:00
verticalScrollBar()->setValue(verticalScrollBar()->value()-event->delta());
}
}
void VTableGraphicsView::mousePressEvent(QMouseEvent *mousePress)
{
if (mousePress->button() & Qt::LeftButton)
{
switch (QGuiApplication::keyboardModifiers())
{
case Qt::ControlModifier:
QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag);
QGraphicsView::mousePressEvent(mousePress);
break;
default:
QGraphicsView::mousePressEvent(mousePress);
break;
2013-08-29 12:31:50 +02:00
}
}
}
void VTableGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
2013-08-29 12:31:50 +02:00
QGraphicsView::mouseReleaseEvent ( event );
QGraphicsView::setDragMode( QGraphicsView::RubberBandDrag );
}
void VTableGraphicsView::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Space:
rotateIt();
break;
case Qt::Key_Left:
MoveItem(VTableGraphicsView::Left);
break;
case Qt::Key_Right:
MoveItem(VTableGraphicsView::Right);
break;
case Qt::Key_Up:
MoveItem(VTableGraphicsView::Up);
break;
case Qt::Key_Down:
MoveItem(VTableGraphicsView::Down);
break;
default:
break;
2013-08-29 12:31:50 +02:00
}
QGraphicsView::keyPressEvent ( event );
}
void VTableGraphicsView::rotateIt()
{
2013-08-29 12:31:50 +02:00
QList<QGraphicsItem *> list = scene()->selectedItems();
if (list.size()>0)
{
for ( qint32 i = 0; i < list.count(); ++i )
{
QGraphicsItem *item = list.at(i);
Q_ASSERT(item != 0);
item->setTransformOriginPoint(item->boundingRect().center());
item->setRotation(item->rotation() + 180);
2013-08-29 12:31:50 +02:00
}
}
}
void VTableGraphicsView::MoveItem(VTableGraphicsView::typeMove_e move)
{
2013-08-29 12:31:50 +02:00
qreal dx = 0, dy = 0;
switch (move)
{
case VTableGraphicsView::Left:
dx = -3;
dy = 0;
break;
case VTableGraphicsView::Right:
dx = 3;
dy = 0;
break;
case VTableGraphicsView::Up:
dx = 0;
dy = -3;
break;
case VTableGraphicsView::Down:
dx = 0;
dy = 3;
break;
default:
break;
2013-08-29 12:31:50 +02:00
}
QList<QGraphicsItem *> listSelectedItems = scene()->selectedItems();
if (listSelectedItems.size()>0)
{
for ( qint32 i = 0; i < listSelectedItems.count(); ++i )
{
2013-08-29 12:31:50 +02:00
listSelectedItems.at(i)->moveBy(dx, dy);
}
}
}