📄 pathdeform.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.
**
** Trolltech ASA (c) 2007
**
** 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 "pathdeform.h"
#include <QApplication>
#include <QtDebug>
#include <QMouseEvent>
#include <QTimerEvent>
#include <QLayout>
#include <QLineEdit>
#include <QPainter>
#include <QSlider>
#include <math.h>
PathDeformWidget::PathDeformWidget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("Vector Deformation");
m_renderer = new PathDeformRenderer(this);
m_renderer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QGroupBox *mainGroup = new QGroupBox(this);
mainGroup->setTitle("Vector Deformation");
QGroupBox *radiusGroup = new QGroupBox(mainGroup);
radiusGroup->setAttribute(Qt::WA_ContentsPropagated);
radiusGroup->setTitle("Lens Radius");
QSlider *radiusSlider = new QSlider(Qt::Horizontal, radiusGroup);
radiusSlider->setRange(50, 150);
radiusSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
QGroupBox *deformGroup = new QGroupBox(mainGroup);
deformGroup->setAttribute(Qt::WA_ContentsPropagated);
deformGroup->setTitle("Deformation");
QSlider *deformSlider = new QSlider(Qt::Horizontal, deformGroup);
deformSlider->setRange(-100, 100);
deformSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
QGroupBox *fontSizeGroup = new QGroupBox(mainGroup);
fontSizeGroup->setAttribute(Qt::WA_ContentsPropagated);
fontSizeGroup->setTitle("Font Size");
QSlider *fontSizeSlider = new QSlider(Qt::Horizontal, fontSizeGroup);
fontSizeSlider->setRange(16, 200);
fontSizeSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
QGroupBox *textGroup = new QGroupBox(mainGroup);
textGroup->setAttribute(Qt::WA_ContentsPropagated);
textGroup->setTitle("Text");
QLineEdit *textInput = new QLineEdit(textGroup);
QPushButton *animateButton = new QPushButton(mainGroup);
animateButton->setText("Animated");
animateButton->setCheckable(true);
QPushButton *showSourceButton = new QPushButton(mainGroup);
showSourceButton->setText("Show Source");
// showSourceButton->setCheckable(true);
#ifdef QT_OPENGL_SUPPORT
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText("Use OpenGL");
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
if (!QGLFormat::hasOpenGL())
enableOpenGLButton->hide();
#endif
QPushButton *whatsThisButton = new QPushButton(mainGroup);
whatsThisButton->setText("What's This?");
whatsThisButton->setCheckable(true);
// Layouts
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(m_renderer);
mainLayout->addWidget(mainGroup);
mainGroup->setFixedWidth(180);
QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
mainGroupLayout->addWidget(radiusGroup);
mainGroupLayout->addWidget(deformGroup);
mainGroupLayout->addWidget(fontSizeGroup);
mainGroupLayout->addWidget(textGroup);
mainGroupLayout->addWidget(animateButton);
mainGroupLayout->addStretch(1);
mainGroupLayout->addWidget(showSourceButton);
#ifdef QT_OPENGL_SUPPORT
mainGroupLayout->addWidget(enableOpenGLButton);
#endif
mainGroupLayout->addWidget(whatsThisButton);
QVBoxLayout *radiusGroupLayout = new QVBoxLayout(radiusGroup);
radiusGroupLayout->addWidget(radiusSlider);
QVBoxLayout *deformGroupLayout = new QVBoxLayout(deformGroup);
deformGroupLayout->addWidget(deformSlider);
QVBoxLayout *fontSizeGroupLayout = new QVBoxLayout(fontSizeGroup);
fontSizeGroupLayout->addWidget(fontSizeSlider);
QVBoxLayout *textGroupLayout = new QVBoxLayout(textGroup);
textGroupLayout->addWidget(textInput);
connect(textInput, SIGNAL(textChanged(QString)), m_renderer, SLOT(setText(QString)));
connect(radiusSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setRadius(int)));
connect(deformSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setIntensity(int)));
connect(fontSizeSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setFontSize(int)));
connect(animateButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setAnimated(bool)));
connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool)));
connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource()));
#ifdef QT_OPENGL_SUPPORT
connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool)));
#endif
connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
whatsThisButton, SLOT(setChecked(bool)));
animateButton->animateClick();
deformSlider->setValue(80);
radiusSlider->setValue(100);
fontSizeSlider->setValue(120);
textInput->setText("Qt");
m_renderer->loadSourceFile(":res/pathdeform.cpp");
m_renderer->loadDescription(":res/pathdeform.html");
m_renderer->setDescriptionEnabled(false);
}
static inline QRect circle_bounds(const QPointF ¢er, double radius, double compensation)
{
return QRect(qRound(center.x() - radius - compensation),
qRound(center.y() - radius - compensation),
qRound((radius + compensation) * 2),
qRound((radius + compensation) * 2));
}
const int LENS_EXTENT = 10;
PathDeformRenderer::PathDeformRenderer(QWidget *widget)
: ArthurFrame(widget)
{
m_radius = 100;
m_pos = QPointF(m_radius, m_radius);
m_direction = QPointF(1, 1);
m_fontSize = 24;
m_animated = true;
m_repaintTimer.start(25, this);
m_repaintTracker.start();
m_intensity = 100;
// m_fpsTimer.start(1000, this);
// m_fpsCounter = 0;
generateLensPixmap();
}
void PathDeformRenderer::setText(const QString &text)
{
m_text = text;
QFont f("times new roman,utopia");
f.setStyleStrategy(QFont::ForceOutline);
f.setPointSize(m_fontSize);
f.setStyleHint(QFont::Times);
QFontMetrics fm(f);
m_paths.clear();
m_pathBounds = QRect();
QPointF advance(0, 0);
bool do_quick = true;
for (int i=0; i<text.size(); ++i) {
if (text.at(i).unicode() >= 0x4ff && text.at(i).unicode() <= 0x1e00) {
do_quick = false;
break;
}
}
if (do_quick) {
for (int i=0; i<text.size(); ++i) {
QPainterPath path;
path.addText(advance, f, text.mid(i, 1));
m_pathBounds |= path.boundingRect();
m_paths << path;
advance += QPointF(fm.width(text.mid(i, 1)), 0);
}
} else {
QPainterPath path;
path.addText(advance, f, text);
m_pathBounds |= path.boundingRect();
m_paths << path;
}
for (int i=0; i<m_paths.size(); ++i)
m_paths[i] = m_paths[i] * QMatrix(1, 0, 0, 1, -m_pathBounds.x(), -m_pathBounds.y());
update();
}
void PathDeformRenderer::generateLensPixmap()
{
double rad = m_radius + LENS_EXTENT;
QRect bounds = circle_bounds(QPointF(), rad, 0);
QPainter painter;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -