📄 xform.cpp
字号:
/******************************************************************************** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.**** This file is part of the demonstration applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -