📄 pathdeform.cpp
字号:
if (preferImage()) {
m_lens_image = QImage(bounds.size(), QImage::Format_ARGB32_Premultiplied);
m_lens_image.fill(0);
painter.begin(&m_lens_image);
} else {
m_lens_pixmap = QPixmap(bounds.size());
m_lens_pixmap.fill(QColor(0, 0, 0, 0));
painter.begin(&m_lens_pixmap);
}
QRadialGradient gr(rad, rad, rad, 3 * rad / 5, 3 * rad / 5);
gr.setColorAt(0.0, QColor(255, 255, 255, 191));
gr.setColorAt(0.2, QColor(255, 255, 127, 191));
gr.setColorAt(0.9, QColor(150, 150, 200, 63));
gr.setColorAt(0.95, QColor(0, 0, 0, 127));
gr.setColorAt(1, QColor(0, 0, 0, 0));
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(gr);
painter.setPen(Qt::NoPen);
painter.drawEllipse(0, 0, bounds.width(), bounds.height());
}
void PathDeformRenderer::setAnimated(bool animated)
{
m_animated = animated;
if (m_animated) {
// m_fpsTimer.start(1000, this);
// m_fpsCounter = 0;
m_repaintTimer.start(25, this);
m_repaintTracker.start();
} else {
// m_fpsTimer.stop();
m_repaintTimer.stop();
}
}
void PathDeformRenderer::timerEvent(QTimerEvent *e)
{
if (e->timerId() == m_repaintTimer.timerId()) {
if (QLineF(QPointF(0,0), m_direction).length() > 1)
m_direction *= 0.995;
double time = m_repaintTracker.restart();
QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
double dx = m_direction.x();
double dy = m_direction.y();
if (time > 0) {
dx = dx * time * .1;
dy = dy * time * .1;
}
m_pos += QPointF(dx, dy);
if (m_pos.x() - m_radius < 0) {
m_direction.setX(-m_direction.x());
m_pos.setX(m_radius);
} else if (m_pos.x() + m_radius > width()) {
m_direction.setX(-m_direction.x());
m_pos.setX(width() - m_radius);
}
if (m_pos.y() - m_radius < 0) {
m_direction.setY(-m_direction.y());
m_pos.setY(m_radius);
} else if (m_pos.y() + m_radius > height()) {
m_direction.setY(-m_direction.y());
m_pos.setY(height() - m_radius);
}
#ifdef QT_OPENGL_SUPPORT
if (usesOpenGL()) {
update();
} else
#endif
{
QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
update(rectAfter | rectBefore);
QApplication::syncX();
}
}
// else if (e->timerId() == m_fpsTimer.timerId()) {
// printf("fps: %d\n", m_fpsCounter);
// emit frameRate(m_fpsCounter);
// m_fpsCounter = 0;
// }
}
void PathDeformRenderer::mousePressEvent(QMouseEvent *e)
{
setDescriptionEnabled(false);
m_repaintTimer.stop();
m_offset = QPointF();
if (QLineF(m_pos, e->pos()).length() <= m_radius)
m_offset = m_pos - e->pos();
mouseMoveEvent(e);
}
void PathDeformRenderer::mouseReleaseEvent(QMouseEvent *e)
{
if (e->buttons() == Qt::NoButton && m_animated) {
m_repaintTimer.start(10, this);
m_repaintTracker.start();
}
}
void PathDeformRenderer::mouseMoveEvent(QMouseEvent *e)
{
QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
if (e->type() == QEvent::MouseMove) {
QLineF line(m_pos, e->pos() + m_offset);
line.setLength(line.length() * .1);
QPointF dir(line.dx(), line.dy());
m_direction = (m_direction + dir) / 2;
}
m_pos = e->pos() + m_offset;
#ifdef QT_OPENGL_SUPPORT
if (usesOpenGL()) {
update();
} else
#endif
{
QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
update(rectBefore | rectAfter);
}
}
QPainterPath PathDeformRenderer::lensDeform(const QPainterPath &source, const QPointF &offset)
{
QPainterPath path;
path.addPath(source);
double flip = m_intensity / 100.0;
for (int i=0; i<path.elementCount(); ++i) {
const QPainterPath::Element &e = path.elementAt(i);
double x = e.x + offset.x();
double y = e.y + offset.y();
double dx = x - m_pos.x();
double dy = y - m_pos.y();
double len = m_radius - sqrt(dx * dx + dy * dy);
if (len > 0) {
path.setElementPositionAt(i,
x + flip * dx * len / m_radius,
y + flip * dy * len / m_radius);
} else {
path.setElementPositionAt(i, x, y);
}
}
return path;
}
void PathDeformRenderer::paint(QPainter *painter)
{
int pad_x = 5;
int pad_y = 5;
int skip_x = qRound(m_pathBounds.width() + pad_x + m_fontSize/2);
int skip_y = qRound(m_pathBounds.height() + pad_y);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::black);
QRectF clip(painter->clipPath().boundingRect());
int overlap = pad_x / 2;
for (int start_y=0; start_y < height(); start_y += skip_y) {
if (start_y > clip.bottom())
break;
int start_x = -overlap;
for (; start_x < width(); start_x += skip_x) {
if (start_y + skip_y >= clip.top() &&
start_x + skip_x >= clip.left() &&
start_x <= clip.right()) {
for (int i=0; i<m_paths.size(); ++i) {
QPainterPath path = lensDeform(m_paths[i], QPointF(start_x, start_y));
painter->drawPath(path);
}
}
}
overlap = skip_x - (start_x - width());
}
if (preferImage()) {
painter->drawImage(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
m_lens_image);
} else {
painter->drawPixmap(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
m_lens_pixmap);
}
}
void PathDeformRenderer::setRadius(int radius)
{
double max = qMax(m_radius, (double)radius);
m_radius = radius;
generateLensPixmap();
if (!m_animated || m_radius < max) {
#ifdef QT_OPENGL_SUPPORT
if (usesOpenGL()) {
update();
} else
#endif
{
update(circle_bounds(m_pos, max, m_fontSize));
}
}
}
void PathDeformRenderer::setIntensity(int intensity)
{
m_intensity = intensity;
if (!m_animated) {
#ifdef QT_OPENGL_SUPPORT
if (usesOpenGL()) {
update();
} else
#endif
{
update(circle_bounds(m_pos, m_radius, m_fontSize));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -