~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/kfontinst/viewpart/CharTip.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * KFontInst - KDE Font Installer
 
3
 *
 
4
 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
 
5
 *
 
6
 * ----
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; see the file COPYING.  If not, write to
 
20
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
21
 * Boston, MA 02110-1301, USA.
 
22
 */
 
23
 
 
24
/*
 
25
 * Inspired by konq_filetip.cc
 
26
 *
 
27
 * Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
 
28
 * Copyright (C) 2000, 2001, 2002 David Faure <faure@kde.org>
 
29
 * Copyright (C) 2004 Martin Koller <m.koller@surfeu.at>
 
30
*/
 
31
 
 
32
#include "CharTip.h"
 
33
#include "FontPreview.h"
 
34
#include "UnicodeCategories.h"
 
35
#include <KDE/KLocale>
 
36
#include <KDE/KApplication>
 
37
#include <KDE/KGlobalSettings>
 
38
#include <QtGui/QLabel>
 
39
#include <QtGui/QToolTip>
 
40
#include <QtGui/QPainter>
 
41
#include <QtCore/QTimer>
 
42
#include <QtGui/QPixmap>
 
43
#include <QtGui/QBoxLayout>
 
44
#include <QtCore/QEvent>
 
45
#include <QtGui/QResizeEvent>
 
46
 
 
47
namespace KFI
 
48
{
 
49
 
 
50
EUnicodeCategory getCategory(quint32 ucs2)
 
51
{
 
52
    for(int i=0; UNICODE_INVALID!=constUnicodeCategoryList[i].category; ++i)
 
53
        if(constUnicodeCategoryList[i].start<=ucs2 &&
 
54
           constUnicodeCategoryList[i].end>=ucs2)
 
55
            return constUnicodeCategoryList[i].category;
 
56
 
 
57
    return UNICODE_UNASSIGNED;
 
58
}
 
59
 
 
60
static QString toStr(EUnicodeCategory cat)
 
61
{
 
62
    switch (cat)
 
63
    {
 
64
        case UNICODE_CONTROL:
 
65
            return i18n("Other, Control");
 
66
        case UNICODE_FORMAT:
 
67
            return i18n("Other, Format");
 
68
        case UNICODE_UNASSIGNED:
 
69
            return i18n("Other, Not Assigned");
 
70
        case UNICODE_PRIVATE_USE:
 
71
            return i18n("Other, Private Use");
 
72
        case UNICODE_SURROGATE:
 
73
            return i18n("Other, Surrogate");
 
74
        case UNICODE_LOWERCASE_LETTER:
 
75
            return i18n("Letter, Lowercase");
 
76
        case UNICODE_MODIFIER_LETTER:
 
77
            return i18n("Letter, Modifier");
 
78
        case UNICODE_OTHER_LETTER:
 
79
            return i18n("Letter, Other");
 
80
        case UNICODE_TITLECASE_LETTER:
 
81
            return i18n("Letter, Titlecase");
 
82
        case UNICODE_UPPERCASE_LETTER:
 
83
            return i18n("Letter, Uppercase");
 
84
        case UNICODE_COMBINING_MARK:
 
85
            return i18n("Mark, Spacing Combining");
 
86
        case UNICODE_ENCLOSING_MARK:
 
87
            return i18n("Mark, Enclosing");
 
88
        case UNICODE_NON_SPACING_MARK:
 
89
            return i18n("Mark, Non-Spacing");
 
90
        case UNICODE_DECIMAL_NUMBER:
 
91
            return i18n("Number, Decimal Digit");
 
92
        case UNICODE_LETTER_NUMBER:
 
93
            return i18n("Number, Letter");
 
94
        case UNICODE_OTHER_NUMBER:
 
95
            return i18n("Number, Other");
 
96
        case UNICODE_CONNECT_PUNCTUATION:
 
97
            return i18n("Punctuation, Connector");
 
98
        case UNICODE_DASH_PUNCTUATION:
 
99
            return i18n("Punctuation, Dash");
 
100
        case UNICODE_CLOSE_PUNCTUATION:
 
101
            return i18n("Punctuation, Close");
 
102
        case UNICODE_FINAL_PUNCTUATION:
 
103
            return i18n("Punctuation, Final Quote");
 
104
        case UNICODE_INITIAL_PUNCTUATION:
 
105
            return i18n("Punctuation, Initial Quote");
 
106
        case UNICODE_OTHER_PUNCTUATION:
 
107
            return i18n("Punctuation, Other");
 
108
        case UNICODE_OPEN_PUNCTUATION:
 
109
            return i18n("Punctuation, Open");
 
110
        case UNICODE_CURRENCY_SYMBOL:
 
111
            return i18n("Symbol, Currency");
 
112
        case UNICODE_MODIFIER_SYMBOL:
 
113
            return i18n("Symbol, Modifier");
 
114
        case UNICODE_MATH_SYMBOL:
 
115
            return i18n("Symbol, Math");
 
116
        case UNICODE_OTHER_SYMBOL:
 
117
            return i18n("Symbol, Other");
 
118
        case UNICODE_LINE_SEPARATOR:
 
119
            return i18n("Separator, Line");
 
120
        case UNICODE_PARAGRAPH_SEPARATOR:
 
121
            return i18n("Separator, Paragraph");
 
122
        case UNICODE_SPACE_SEPARATOR:
 
123
            return i18n("Separator, Space");
 
124
        default:
 
125
            return "";
 
126
    }
 
127
}
 
128
 
 
129
CCharTip::CCharTip(CFontPreview *parent)
 
130
        : QFrame(0, Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint),
 
131
          itsParent(parent)
 
132
{
 
133
    itsPixmapLabel = new QLabel(this);
 
134
    itsLabel = new QLabel(this);
 
135
    itsTimer = new QTimer(this);
 
136
 
 
137
    QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
 
138
    layout->setMargin(8);
 
139
    layout->setSpacing(0);
 
140
    layout->addWidget(itsPixmapLabel);
 
141
    layout->addWidget(itsLabel);
 
142
 
 
143
    setPalette(QToolTip::palette());
 
144
    setFrameShape(QFrame::Box);
 
145
    setFrameShadow(QFrame::Plain);
 
146
    hide();
 
147
}
 
148
 
 
149
CCharTip::~CCharTip()
 
150
{
 
151
}
 
152
 
 
153
void CCharTip::setItem(const CFcEngine::TChar &ch)
 
154
{
 
155
    hideTip();
 
156
 
 
157
    itsItem=ch;
 
158
    itsTimer->disconnect(this);
 
159
    connect(itsTimer, SIGNAL(timeout()), this, SLOT(showTip()));
 
160
    itsTimer->setSingleShot(true);
 
161
    itsTimer->start(300);
 
162
}
 
163
 
 
164
void CCharTip::showTip()
 
165
{
 
166
    if(!itsParent->underMouse())
 
167
        return;
 
168
 
 
169
    EUnicodeCategory cat(getCategory(itsItem.ucs4));
 
170
    QString          details("<table>");
 
171
 
 
172
    details+="<tr><td><b>"+i18n("Category")+"</b></td><td>"+
 
173
             toStr(cat)+"</td></tr>";
 
174
    details+="<tr><td><b>"+i18n("UCS-4")+"</b></td><td>"+
 
175
             QString().sprintf("U+%4.4X", itsItem.ucs4)+"</td></tr>";
 
176
 
 
177
    QString str(QString::fromUcs4(&(itsItem.ucs4), 1));
 
178
    details+="<tr><td><b>"+i18n("UTF-16")+"</b></td><td>";
 
179
 
 
180
    const ushort *utf16(str.utf16());
 
181
 
 
182
    for(int i=0; utf16[i]; ++i)
 
183
    {
 
184
        if(i)
 
185
            details+=' ';
 
186
        details+=QString().sprintf("0x%4.4X",  utf16[i]);
 
187
    }
 
188
    details+="</td></tr>";
 
189
    details+="<tr><td><b>"+i18n("UTF-8")+"</b></td><td>";
 
190
 
 
191
    QByteArray utf8(str.toUtf8());
 
192
 
 
193
    for(int i=0; i<utf8.size(); ++i)
 
194
    {
 
195
        if(i)
 
196
            details+=' ';
 
197
        details+=QString().sprintf("0x%2.2X", (unsigned char)(utf8.constData()[i]));
 
198
    }
 
199
    details+="</td></tr>";
 
200
 
 
201
    // Note: the "<b></b> below is just to stop Qt converting the xml entry into
 
202
    // a character!
 
203
    if ((0x0001 <= itsItem.ucs4 && itsItem.ucs4 <= 0xD7FF) ||
 
204
        (0xE000 <= itsItem.ucs4 && itsItem.ucs4 <= 0xFFFD) ||
 
205
        (0x10000 <= itsItem.ucs4 && itsItem.ucs4 <= 0x10FFFF))
 
206
        details+="<tr><td><b>"+i18n("XML Decimal Entity")+"</b></td><td>"+
 
207
                 QString().sprintf("&#<b></b>%d;", itsItem.ucs4)+"</td></tr>";
 
208
 
 
209
    details+="</table>";
 
210
    itsLabel->setText(details);
 
211
 
 
212
    QList<CFcEngine::TRange> range;
 
213
    range.append(CFcEngine::TRange(itsItem.ucs4, 0));
 
214
 
 
215
    QColor bgnd(Qt::white);
 
216
    bgnd.setAlpha(0);
 
217
 
 
218
    QImage img=itsParent->engine()->draw(itsParent->itsFontName, itsParent->itsStyleInfo,
 
219
                                         itsParent->itsCurrentFace-1, palette().text().color(), bgnd,
 
220
                                         (int)(itsItem.width()*2.5), (int)(itsItem.height()*2.5), false, range, NULL);
 
221
 
 
222
    if(!img.isNull())
 
223
        itsPixmapLabel->setPixmap(QPixmap::fromImage(img));
 
224
    else
 
225
        itsPixmapLabel->setPixmap(QPixmap());
 
226
 
 
227
    itsTimer->disconnect(this);
 
228
    connect(itsTimer, SIGNAL(timeout()), this, SLOT(hideTip()));
 
229
    itsTimer->setSingleShot(true);
 
230
    itsTimer->start(15000);
 
231
 
 
232
    kapp->installEventFilter(this);
 
233
    reposition();
 
234
    show();
 
235
}
 
236
 
 
237
void CCharTip::hideTip()
 
238
{
 
239
    itsTimer->stop();
 
240
    kapp->removeEventFilter(this);
 
241
    hide();
 
242
}
 
243
 
 
244
void CCharTip::reposition()
 
245
{
 
246
    QRect rect(itsItem);
 
247
 
 
248
    rect.moveTopRight(itsParent->mapToGlobal(rect.topRight()));
 
249
 
 
250
    QPoint pos(rect.center());
 
251
    QRect  desk(KGlobalSettings::desktopGeometry(rect.center()));
 
252
 
 
253
    if ((rect.center().x() + width()) > desk.right())
 
254
    {
 
255
        if (pos.x() - width() < 0)
 
256
            pos.setX(0);
 
257
        else
 
258
            pos.setX( pos.x() - width() );
 
259
    }
 
260
    // should the tooltip be shown above or below the ivi ?
 
261
    if (rect.bottom() + height() > desk.bottom())
 
262
        pos.setY( rect.top() - height() );
 
263
    else
 
264
        pos.setY(rect.bottom() + 1);
 
265
 
 
266
    move(pos);
 
267
    update();
 
268
}
 
269
 
 
270
void CCharTip::resizeEvent(QResizeEvent *event)
 
271
{
 
272
    QFrame::resizeEvent(event);
 
273
    reposition();
 
274
}
 
275
 
 
276
bool CCharTip::eventFilter(QObject *, QEvent *e)
 
277
{
 
278
    switch (e->type())
 
279
    {
 
280
        case QEvent::Leave:
 
281
        case QEvent::MouseButtonPress:
 
282
        case QEvent::MouseButtonRelease:
 
283
        case QEvent::KeyPress:
 
284
        case QEvent::KeyRelease:
 
285
        case QEvent::FocusIn:
 
286
        case QEvent::FocusOut:
 
287
        case QEvent::Wheel:
 
288
            hideTip();
 
289
        default: break;
 
290
    }
 
291
 
 
292
    return false;
 
293
}
 
294
 
 
295
}
 
296
 
 
297
#include "CharTip.moc"