/****************************************************************************
**
** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** Licensees holding a valid Qt License Agreement may use this file in
** accordance with the rights, responsibilities and obligations
** contained therein. Please consult your licensing agreement or
** contact sales@trolltech.com if any conditions of this licensing
** agreement are not clear to you.
**
** Further information about Qt licensing is available at:
** http://www.trolltech.com/products/qt/licensing.html or by
** contacting info@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "xform.h"
#include "hoverpoints.h"
#include <QLayout>
#include <QPainter>
#include <QPainterPath>
const int alpha = 155;
XFormView::XFormView(QWidget *parent)
: ArthurFrame(parent)
{
setAttribute(Qt::WA_MouseTracking);
type = VectorType;
m_rotation = 0.0;
m_scale = 1.0;
m_shear = 0.0;
pixmap = QPixmap(":/res/bg1.jpg");
pts = new HoverPoints(this, HoverPoints::CircleShape);
pts->setConnectionType(HoverPoints::LineConnection);
pts->setEditable(false);
pts->setPointSize(QSize(15, 15));
pts->setShapeBrush(QBrush(QColor(151, 0, 0, alpha)));
pts->setShapePen(QPen(QColor(255, 100, 50, alpha)));
pts->setConnectionPen(QPen(QColor(151, 0, 0, 50)));
pts->setBoundingRect(QRectF(0, 0, 500, 500));
ctrlPoints << QPointF(250, 250) << QPointF(350, 250);
pts->setPoints(ctrlPoints);
connect(pts, SIGNAL(pointsChanged(const QPolygonF&)),
this, SLOT(updateCtrlPoints(const QPolygonF &)));
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void XFormView::mousePressEvent(QMouseEvent *)
{
setDescriptionEnabled(false);
}
void XFormView::resizeEvent(QResizeEvent *e)
{
pts->setBoundingRect(rect());
ArthurFrame::resizeEvent(e);
}
void XFormView::paint(QPainter *p)
{
p->save();
p->setRenderHint(QPainter::Antialiasing);
p->setRenderHint(QPainter::SmoothPixmapTransform);
switch (type) {
case VectorType:
drawVectorType(p);
break;
case PixmapType:
drawPixmapType(p);
break;
case TextType:
drawTextType(p);
break;
}
p->restore();
}
void XFormView::updateCtrlPoints(const QPolygonF &points)
{
QPointF trans = points.at(0) - ctrlPoints.at(0);
if (qAbs(points.at(0).x() - points.at(1).x()) < 10
&& qAbs(points.at(0).y() - points.at(1).y()) < 10)
pts->setPoints(ctrlPoints);
if (!trans.isNull()) {
ctrlPoints[0] = points.at(0);
ctrlPoints[1] += trans;
pts->setPoints(ctrlPoints);
}
ctrlPoints = points;
QLineF line(ctrlPoints.at(0), ctrlPoints.at(1));
m_rotation = line.angle(QLineF(0, 0, 1, 0));
if (line.dy() < 0)
m_rotation = 360 - m_rotation;
if (trans.isNull())
emit rotationChanged(int(m_rotation*10));
}
void XFormView::setVectorType()
{
type = VectorType;
update();
}
void XFormView::setPixmapType()
{
type = PixmapType;
update();
}
void XFormView::setTextType()
{
type = TextType;
update();
}
void XFormView::setAnimation(bool animate)
{
timer.stop();
if (animate)
timer.start(25, this);
}
void XFormView::changeRotation(int r)
{
setRotation(double(r)/10.0);
}
void XFormView::changeScale(int s)
{
setScale(double(s)/1000.0);
}
void XFormView::changeShear(int s)
{
setShear(double(s)/1000.0);
}
void XFormView::setShear(double s)
{
m_shear = s;
update();
}
void XFormView::setScale(double s)
{
m_scale = s;
update();
}
void XFormView::setRotation(double r)
{
double old_rot = m_rotation;
m_rotation = r;
QPointF center(pts->points().at(0));
QMatrix m;
m.translate(center.x(), center.y());
m.rotate(m_rotation - old_rot);
m.translate(-center.x(), -center.y());
pts->setPoints(pts->points() * m);
update();
}
void XFormView::timerEvent(QTimerEvent *e)
{
if (e->timerId() == timer.timerId()) {
QPointF center(pts->points().at(0));
QMatrix m;
m.translate(center.x(), center.y());
m.rotate(0.2);
m.translate(-center.x(), -center.y());
pts->setPoints(pts->points() * m);
setUpdatesEnabled(false);
static double scale_inc = 0.003;
static double shear_inc = -0.001;
emit scaleChanged(int((m_scale + scale_inc) * 1000));
emit shearChanged(int((m_shear + shear_inc) * 1000));
if (m_scale >= 4.0 || m_scale <= 0.1)
scale_inc = -scale_inc;
if (m_shear >= 1.0 || m_shear <= -1.0)
shear_inc = -shear_inc;
setUpdatesEnabled(true);
pts->firePointChange();
}
}
void XFormView::wheelEvent(QWheelEvent *e)
{
m_scale += e->delta()/600.0;
m_scale = qMax(.1, qMin(4.0, m_scale));
emit scaleChanged(int(m_scale*1000));
}
void XFormView::reset()
{
emit rotationChanged(0);
emit scaleChanged(1000);
emit shearChanged(0);
ctrlPoints = QPolygonF();
ctrlPoints << QPointF(250, 250) << QPointF(350, 250);
pts->setPoints(ctrlPoints);
pts->firePointChange();
}
void XFormView::drawPixmapType(QPainter *painter)
{
QPointF center(pixmap.width()/2.0, pixmap.height()/2.0);
painter->translate(ctrlPoints.at(0) - center);
painter->translate(center);
painter->rotate(m_rotation);
painter->scale(m_scale, m_scale);
painter->shear(0, m_shear);
painter->translate(-center);
painter->drawPixmap(QPointF(0, 0), pixmap);
painter->setPen(QPen(QColor(255, 0, 0, alpha), 0.25, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));
painter->setBrush(Qt::NoBrush);
painter->drawRect(QRectF(0, 0, pixmap.width(), pixmap.height()).adjusted(-2, -2, 2, 2));
}
void XFormView::drawTextType(QPainter *painter)
{
QPainterPath path;
QFont f("times new roman,utopia");
f.setStyleStrategy(QFont::ForceOutline);
f.setPointSize(72);
f.setStyleHint(QFont::Times);
path.addText(0, 0, f, textEditor->text());
QFontMetrics fm(f);
QRectF br(fm.boundingRect(textEditor->text()));
QPointF center(br.center());
painter->translate(ctrlPoints.at(0) - center);
painter->translate(center);
painter->rotate(m_rotation);
painter->scale(m_scale, m_scale);
painter->shear(0, m_shear);
painter->translate(-center);
painter->fillPath(path, Qt::black);
painter->setPen(QPen(QColor(255, 0, 0, alpha), 0.25, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));
painter->setBrush(Qt::NoBrush);
painter->drawRect(br.adjusted(-1, -1, 1, 1));
}
void XFormView::drawVectorType(QPainter *painter)
{
QPainterPath path;
painter->translate(ctrlPoints.at(0) - QPointF(250,250));
painter->scale(0.77, 0.77);
painter->translate(98.9154 + 30 , -217.691 - 20);
QRect br(-55, 275, 500, 590);
QPoint center = br.center();
painter->translate(center.x(), center.y());
painter->rotate(m_rotation);
painter->scale(m_scale, m_scale);
painter->shear(0, m_shear);
painter->translate(-center.x(), -center.y());
painter->setPen(Qt::NoPen);
path.moveTo(120, 470);
path.lineTo(60+245, 470);
path.lineTo(60+245, 470+350);
path.lineTo(60, 470+350);
path.lineTo(60, 470+80);
painter->setBrush(Qt::white);
painter->drawPath(path);
path = QPainterPath();
painter->setBrush(QColor( 193, 19
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
affine.zip (12个子文件)
main.cpp 1KB
affine.qrc 164B
bg1.jpg 23KB
affine.pro 424B
xform.cpp 39KB
affine.dsp 9KB
Makefile.Debug 71KB
release
affine.exe 196KB
Makefile.Release 71KB
xform.html 827B
xform.h 3KB
tmp
obj
debug_shared
release_shared
moc
debug_shared
release_shared
rcc
debug_shared
release_shared
Makefile 5KB
debug
共 12 条
- 1
资源评论
- Mister_郑2013-05-03晕死,怎么只给工程文件,而且编译后也运行不了。。。
Ksirer
- 粉丝: 4
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功