• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • view
katetextanimation.cpp
Go to the documentation of this file.
1 /* This file is part of the Kate project.
2  *
3  * Copyright (C) 2013 Dominik Haumann <dhaumann kde org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #include "katetextanimation.h"
21 
22 #include "kateviewinternal.h"
23 #include "katerenderer.h"
24 
25 #include <ktexteditor/document.h>
26 
27 #include <QTimeLine>
28 #include <QPainter>
29 #include <QRect>
30 #include <QSizeF>
31 #include <QPointF>
32 
33 #include <QDebug>
34 
35 KateTextAnimation::KateTextAnimation(const KTextEditor::Range & range, KTextEditor::Attribute::Ptr attribute, KateViewInternal * view)
36  : QObject(view)
37  , m_range(range)
38  , m_attribute(attribute)
39  , m_doc(view->view()->doc())
40  , m_view(view)
41  , m_timeLine(new QTimeLine(250, this))
42  , m_value(0.0)
43 {
44  m_text = view->view()->doc()->text(range);
45 
46  connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(nextFrame(qreal)));
47  connect(m_timeLine, SIGNAL(finished()), this, SLOT(deleteLater()));
48 
49  m_timeLine->setCurveShape(QTimeLine::SineCurve);
50  m_timeLine->start();
51 }
52 
53 KateTextAnimation::~KateTextAnimation()
54 {
55  // if still running, we need to update the view a last time to avoid artifacts
56  if (m_timeLine->state() == QTimeLine::Running) {
57  m_timeLine->stop();
58  nextFrame(0.0);
59  }
60 }
61 
62 QRectF KateTextAnimation::rectForText()
63 {
64  const QFontMetricsF fm = m_view->view()->renderer()->currentFontMetrics();
65  const int lineHeight = m_view->view()->renderer()->lineHeight();
66  QPoint pixelPos = m_view->cursorToCoordinate(m_range.start(), /*bool realCursor*/ true, /*bool includeBorder*/ false);
67 
68  if (pixelPos.x() == -1 || pixelPos.y() == -1) {
69  return QRectF();
70  } else {
71  QRectF rect(pixelPos.x(), pixelPos.y(),
72  fm.width(m_view->view()->doc()->text(m_range)), lineHeight);
73  const QPointF center = rect.center();
74  const qreal factor = 1.0 + 0.5 * m_value;
75  rect.setWidth(rect.width() * factor);
76  rect.setHeight(rect.height() * factor);
77  rect.moveCenter(center);
78  return rect;
79  }
80 }
81 
82 void KateTextAnimation::draw(QPainter & painter)
83 {
84  // could happen in corner cases: timeLine emitted finished(), but this object
85  // is not yet deleted. Therefore, draw() might be called in paintEvent().
86  if (m_timeLine->state() == QTimeLine::NotRunning) {
87  return;
88  }
89 
90  // get current rect and fill background
91  QRectF rect = rectForText();
92  painter.fillRect(rect, m_attribute->background());
93 
94  // scale font with animation
95  QFont f = m_view->view()->renderer()->currentFont();
96  f.setBold(m_attribute->fontBold());
97  f.setPointSizeF(f.pointSizeF() * (1.0 + 0.5 * m_value));
98  painter.setFont(f);
99 
100  painter.setPen(m_attribute->foreground().color());
101  // finally draw contents on the view
102  painter.drawText(rect, m_text);
103 }
104 
105 void KateTextAnimation::nextFrame(qreal value)
106 {
107  // cache previous rect for update
108  const QRectF prevRect = rectForText();
109 
110  m_value = value;
111 
112  // next rect is used to draw the text
113  const QRectF nextRect = rectForText();
114 
115  // due to rounding errors, increase the rect 1px to avoid artifacts
116  const QRect updateRect = nextRect.united(prevRect).adjusted(-1, -1, 1, 1).toRect();
117 
118  // request repaint
119  m_view->update(updateRect);
120 }
121 
122 // kate: space-indent on; indent-width 2; replace-tabs on;
KateRenderer::currentFont
const QFont & currentFont() const
Definition: katerenderer.cpp:805
QRectF::toRect
QRect toRect() const
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
KateView::renderer
KateRenderer * renderer()
Definition: kateview.cpp:1664
katerenderer.h
KateTextAnimation::nextFrame
void nextFrame(qreal value)
Definition: katetextanimation.cpp:105
QFont::pointSizeF
qreal pointSizeF() const
QFont
KateRenderer::currentFontMetrics
const QFontMetricsF & currentFontMetrics() const
Definition: katerenderer.cpp:810
KateViewInternal::cursorToCoordinate
QPoint cursorToCoordinate(const KTextEditor::Cursor &cursor, bool realCursor=true, bool includeBorder=true) const
Definition: kateviewinternal.cpp:739
QPoint
QFontMetricsF::width
qreal width(const QString &text) const
QWidget::update
void update()
QPoint::x
int x() const
QPoint::y
int y() const
QTimeLine::setCurveShape
void setCurveShape(CurveShape shape)
QPointF
QFont::setBold
void setBold(bool enable)
QRect
QPainter::setFont
void setFont(const QFont &font)
QObject
QPainter::setPen
void setPen(const QColor &color)
QPainter
KateViewInternal
Definition: kateviewinternal.h:58
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
kateviewinternal.h
QObject::deleteLater
void deleteLater()
KateTextAnimation::draw
void draw(QPainter &painter)
Definition: katetextanimation.cpp:82
QRectF::united
QRectF united(const QRectF &rectangle) const
KateTextAnimation::KateTextAnimation
KateTextAnimation(const KTextEditor::Range &range, KTextEditor::Attribute::Ptr attribute, KateViewInternal *view)
Definition: katetextanimation.cpp:35
QFont::setPointSizeF
void setPointSizeF(qreal pointSize)
katetextanimation.h
QRectF
QTimeLine::stop
void stop()
KateTextAnimation::~KateTextAnimation
virtual ~KateTextAnimation()
Definition: katetextanimation.cpp:53
QTimeLine
KateRenderer::lineHeight
int lineHeight() const
Definition: katerenderer.cpp:825
KateDocument::text
virtual QString text(const KTextEditor::Range &range, bool blockwise=false) const
Definition: katedocument.cpp:337
QRectF::adjusted
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const
KateView::doc
KateDocument * doc()
accessor to katedocument pointer
Definition: kateview.h:553
KateViewInternal::view
KateView * view() const
Definition: kateviewinternal.h:81
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QTimeLine::start
void start()
QTimeLine::state
State state() const
QFontMetricsF
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal