File: scimstringrender.cpp

package info (click to toggle)
skim 1.4.4-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,876 kB
  • ctags: 1,247
  • sloc: cpp: 9,421; python: 1,608; sh: 260; makefile: 68
file content (230 lines) | stat: -rw-r--r-- 8,193 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
 *   Copyright (C) 2003-2005 by liuspider                                  *
 *   liuspider@users.sourceforge.net                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "scimstringrender.h"

#include <qwidget.h>
#include <kapplication.h>

class ScimStringRenderPrivate
{
public:
    QString text;
    scim::AttributeList attrs;
    int valid_widthhint;
    QSize storedSizeHint;
    QWidget * parent;
    int cursorPos;
    QPixmap contentBuffer;
    bool validBuffer;
    bool displayCursor;
};

ScimStringRender::ScimStringRender(QWidget * w) 
    : d(new ScimStringRenderPrivate)//, fm(w->fontMetrics())
{
    d->parent = w;
    d->cursorPos = -1;
    d->validBuffer = false;
    d->displayCursor = false;
}

void ScimStringRender::setDisplayCursor(bool b)
{
    d->displayCursor = b;
}

void ScimStringRender::drawString ( QPainter * globalp, const QRect & cr) const
{
    if(!cr.isValid())
        return;

    const QColorGroup& cg = KApplication::palette().active();

    QBrush bg = QBrush( d->parent->paletteBackgroundColor() );
    if ( d->parent->paletteBackgroundPixmap() )
        bg = QBrush( cg.background(), *(d->parent->paletteBackgroundPixmap()) );

    globalp->fillRect( cr, bg );

    if(!d->validBuffer)
    {
        d->validBuffer = true;
        if(d->storedSizeHint.isEmpty())
            return;

        d->contentBuffer.resize(d->storedSizeHint);

        QPainter painter(&d->contentBuffer);
        QPainter * p = &painter;

        p->fillRect( d->contentBuffer.rect(), bg );

        QFontMetrics fm( d->parent->fontMetrics() );

        p->setFont(d->parent->font());

        p->setPen( cg.text() );

        if(d->attrs.size())
        {
        uint start_index, length, wlen = d->text.length();
        QRect curRect;
        std::vector<QRect> drawForegroundRect;
        std::vector<QString> drawForegroundString;
        std::vector<scim::Attribute *> drawForegroundAttribute;
    
        //first draw all background and save the qrect for those which
        //need to draw customized foreground
        for(uint i=0; i<d->attrs.size (); ++i)
        {
            curRect = cr;
            start_index = d->attrs[i].get_start();
            length = d->attrs[i].get_length();
            const QString leftPart = d->text.mid(0, start_index);
            const QString curString = d->text.mid(start_index, length);
            int newLineCount = leftPart.contains('\n');

            if(d->text.contains('\n') > 0) {
            int newLineBegin = d->text.findRev('\n', start_index) + 1;
            curRect.setLeft(cr.x() +
                fm.width(d->text.mid(newLineBegin , start_index - newLineBegin)));
            curRect.setTop(cr.y() + fm.height() * newLineCount + fm.leading() * newLineCount);
            curRect.setBottom(curRect.y() + fm.height());
            } else {
            curRect.setLeft(cr.x() + fm.width(leftPart));
            curRect.setRight(curRect.x() + fm.width(curString));
            }

            uint dtype = d->attrs[i].get_type(), dvalue = d->attrs[i].get_value();
            bool needDrawForeground = false;
            if (start_index + length <= wlen)
            {
                if(dtype == scim::SCIM_ATTR_DECORATE)
                {
                    if( dvalue == scim::SCIM_ATTR_DECORATE_UNDERLINE) {
                        needDrawForeground = true;
                    } else if( dvalue == scim::SCIM_ATTR_DECORATE_REVERSE){
                        p->fillRect(curRect, cg.text());
                        needDrawForeground = true;
                    } else if( dvalue == scim::SCIM_ATTR_DECORATE_HIGHLIGHT){
                        p->fillRect(curRect, cg.highlight());
                        needDrawForeground = true;
                    }
                } else if(dtype == scim::SCIM_ATTR_FOREGROUND){
                    needDrawForeground = true;
                } else if(dtype == scim::SCIM_ATTR_BACKGROUND){
                    p->fillRect(curRect, QColor(SCIM_RGB_COLOR_RED(dvalue),
                        SCIM_RGB_COLOR_GREEN(dvalue), SCIM_RGB_COLOR_BLUE(dvalue)));
                }
                if(needDrawForeground)
                {
                    drawForegroundRect.push_back(curRect);
                    drawForegroundString.push_back(curString);
                    drawForegroundAttribute.push_back(&d->attrs[i]);
                }
            }
        }
        
        //now draw all the string in default painter
        p->drawText(cr, Qt::AlignVCenter, d->text);
    
        //draw customized foreground text
        for(uint rectIndex = 0; rectIndex < drawForegroundRect.size(); ++rectIndex)
        {
    //       std::cout << d->attrs[i].get_start() << "ok \n";
            uint dtype = drawForegroundAttribute[rectIndex]->get_type(),
            dvalue = drawForegroundAttribute[rectIndex]->get_value();

            p->save();
            if(dtype == scim::SCIM_ATTR_DECORATE)
            {
                if( dvalue == scim::SCIM_ATTR_DECORATE_UNDERLINE) {
                QFont f = d->parent->font();
                f.setUnderline(true);
                p->setFont(f);
                } else if( dvalue == scim::SCIM_ATTR_DECORATE_REVERSE){
                p->setPen( cg.background() );
                } else if( dvalue == scim::SCIM_ATTR_DECORATE_HIGHLIGHT){
                p->setPen( cg.highlightedText() );
                }
            } else if(dtype == scim::SCIM_ATTR_FOREGROUND){
                p->setPen( QColor(SCIM_RGB_COLOR_RED(dvalue),
                    SCIM_RGB_COLOR_GREEN(dvalue), SCIM_RGB_COLOR_BLUE(dvalue)) );
            }
            p->drawText(drawForegroundRect[rectIndex],
                Qt::AlignVCenter, drawForegroundString[rectIndex]);
            p->restore();
        }
        } else
        p->drawText(cr, Qt::AlignVCenter, d->text);

        //draw cursor if it is visible
        //this will not modify the painter, so we do not need to save/restore its state
        if(d->displayCursor && d->cursorPos >= 0 && (uint)d->cursorPos <= d->text.length())
        {
            int cix = fm.width(d->text, cursorPosition());
            QPoint from( cr.x() + cix, cr.top() + (cr.height() - fm.height()) / 2);
            QPoint to = from + QPoint( 0, fm.height() );
            p->drawLine( from, to );
        }
        p->end();
    }

    if(!d->contentBuffer.isNull() && !d->storedSizeHint.isEmpty())
        bitBlt(d->parent, cr.x(), cr.y(),
            &d->contentBuffer, 0, 0, d->storedSizeHint.width(), d->storedSizeHint.height());
}

void ScimStringRender::setCursorPosition ( int p )
{
    d->cursorPos = p;
    d->validBuffer = false;
}

int ScimStringRender::cursorPosition() const  { return d->cursorPos; }

void ScimStringRender::setText(const QString & text, const scim::AttributeList & attrlist ) 
{
    d->text = text;
    d->attrs = attrlist;
    d->valid_widthhint = -1;
    d->validBuffer = false;
}

QSize ScimStringRender::minimumSizeHint () const
{
    if(d->valid_widthhint == 1)
        return d->storedSizeHint;

    d->valid_widthhint = 1;
    if(d->text.length())
    {
        QFontMetrics fm( d->parent->fontMetrics() );
        QRect br = fm.boundingRect( 0, 0, 2000 ,2000, 
            Qt::AlignAuto | Qt::AlignVCenter | Qt::ExpandTabs, d->text );
        if(d->displayCursor)
            d->storedSizeHint.setWidth(br.width() + 2); //more space to display the cursor
        else
            d->storedSizeHint.setWidth(br.width());

        d->storedSizeHint.setHeight(fm.lineSpacing() + 4);
    }
    else
        d->storedSizeHint = QSize(0, 0);

    return d->storedSizeHint;
}

ScimStringRender::~ScimStringRender()
{
    delete d;
}