~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.1/textengines/mathml/qwt_mathml_text_engine.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2003   Uwe Rathmann
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
// vim: expandtab
 
11
 
 
12
#include <qglobal.h>
 
13
#if QT_VERSION >= 0x040000
 
14
 
 
15
#include <qstring.h>
 
16
#include <qpainter.h>
 
17
#include "qwt_mathml_text_engine.h"
 
18
 
 
19
#include <qtmmlwidget.h>
 
20
 
 
21
//! Constructor
 
22
QwtMathMLTextEngine::QwtMathMLTextEngine()
 
23
{
 
24
}
 
25
 
 
26
//! Destructor
 
27
QwtMathMLTextEngine::~QwtMathMLTextEngine()
 
28
{
 
29
}
 
30
 
 
31
/*!
 
32
   Find the height for a given width
 
33
 
 
34
   \param font Font of the text
 
35
   \param flags Bitwise OR of the flags used like in QPainter::drawText
 
36
   \param text Text to be rendered
 
37
   \param width Width
 
38
 
 
39
   \return Calculated height
 
40
*/
 
41
int QwtMathMLTextEngine::heightForWidth(const QFont& font, int flags,
 
42
        const QString& text, int) const
 
43
{
 
44
    return textSize(font, flags, text).height();
 
45
}
 
46
 
 
47
/*!
 
48
  Returns the size, that is needed to render text
 
49
 
 
50
  \param font Font of the text
 
51
  \param flags Bitwise OR of the flags used like in QPainter::drawText
 
52
  \param text Text to be rendered
 
53
 
 
54
  \return Caluclated size
 
55
*/
 
56
QSize QwtMathMLTextEngine::textSize(const QFont &font,
 
57
    int, const QString& text) const
 
58
{
 
59
    static QString t;
 
60
    static QSize sz;
 
61
 
 
62
    if ( text != t )
 
63
    {
 
64
        QtMmlDocument doc;
 
65
        doc.setContent(text);
 
66
        doc.setBaseFontPointSize(font.pointSize());
 
67
 
 
68
        sz = doc.size();
 
69
        t = text;
 
70
    }
 
71
 
 
72
    return sz;
 
73
}
 
74
 
 
75
/*!
 
76
  Return margins around the texts
 
77
 
 
78
  \param left Return 0
 
79
  \param right Return 0
 
80
  \param top Return 0
 
81
  \param bottom Return 0
 
82
*/
 
83
void QwtMathMLTextEngine::textMargins(const QFont &, const QString &,
 
84
    int &left, int &right, int &top, int &bottom) const
 
85
{
 
86
    left = right = top = bottom = 0;
 
87
}
 
88
 
 
89
/*!
 
90
   Draw the text in a clipping rectangle
 
91
 
 
92
   \param painter Painter
 
93
   \param rect Clipping rectangle 
 
94
   \param flags Bitwise OR of the flags like in for QPainter::drawText
 
95
   \param text Text to be rendered
 
96
*/ 
 
97
void QwtMathMLTextEngine::draw(QPainter *painter, const QRect &rect,
 
98
    int flags, const QString& text) const
 
99
{
 
100
    QtMmlDocument doc;
 
101
    doc.setContent(text);
 
102
    doc.setBaseFontPointSize(painter->font().pointSize());
 
103
 
 
104
    const QSize docSize = doc.size();
 
105
 
 
106
    QPoint pos = rect.topLeft();
 
107
    if ( rect.width() > docSize.width() )
 
108
    {
 
109
        if ( flags & Qt::AlignRight )
 
110
            pos.setX(rect.right() - docSize.width());
 
111
        if ( flags & Qt::AlignHCenter )
 
112
            pos.setX(rect.center().x() - docSize.width() / 2);
 
113
    }
 
114
    if ( rect.height() > docSize.height() )
 
115
    {
 
116
        if ( flags & Qt::AlignBottom )
 
117
            pos.setY(rect.bottom() - docSize.height());
 
118
        if ( flags & Qt::AlignVCenter )
 
119
            pos.setY(rect.center().y() - docSize.height() / 2);
 
120
    }
 
121
 
 
122
    doc.paint(painter, pos);
 
123
}
 
124
 
 
125
/*!
 
126
  Test if a string can be rendered by QwtMathMLTextEngine
 
127
 
 
128
  \param text Text to be tested
 
129
  \return true, if text begins with "<math>".
 
130
*/
 
131
bool QwtMathMLTextEngine::mightRender(const QString &text) const
 
132
{
 
133
    return text.trimmed().startsWith("<math");
 
134
}
 
135
 
 
136
#endif // QT_VERSION < 0x040000