add more spacing
[personal-kdebase.git] / workspace / kcontrol / kfontinst / viewpart / CharTip.cpp
blobc49d1e2bcf2857256c04f0cee068a324c7d29430
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
6 * ----
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.
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.
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.
25 * Inspired by konq_filetip.cc
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>
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>
47 namespace KFI
50 EUnicodeCategory getCategory(quint32 ucs2)
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;
57 return UNICODE_UNASSIGNED;
60 static QString toStr(EUnicodeCategory cat)
62 switch (cat)
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 "";
129 CCharTip::CCharTip(CFontPreview *parent)
130 : QFrame(0, Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint),
131 itsParent(parent)
133 itsPixmapLabel = new QLabel(this);
134 itsLabel = new QLabel(this);
135 itsTimer = new QTimer(this);
137 QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
138 layout->setMargin(8);
139 layout->setSpacing(0);
140 layout->addWidget(itsPixmapLabel);
141 layout->addWidget(itsLabel);
143 setPalette(QToolTip::palette());
144 setFrameShape(QFrame::Box);
145 setFrameShadow(QFrame::Plain);
146 hide();
149 CCharTip::~CCharTip()
153 void CCharTip::setItem(const CFcEngine::TChar &ch)
155 hideTip();
157 itsItem=ch;
158 itsTimer->disconnect(this);
159 connect(itsTimer, SIGNAL(timeout()), this, SLOT(showTip()));
160 itsTimer->setSingleShot(true);
161 itsTimer->start(300);
164 void CCharTip::showTip()
166 if(!itsParent->underMouse())
167 return;
169 EUnicodeCategory cat(getCategory(itsItem.ucs4));
170 QString details("<table>");
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>";
177 QString str(QString::fromUcs4(&(itsItem.ucs4), 1));
178 details+="<tr><td><b>"+i18n("UTF-16")+"</b></td><td>";
180 const ushort *utf16(str.utf16());
182 for(int i=0; utf16[i]; ++i)
184 if(i)
185 details+=' ';
186 details+=QString().sprintf("0x%4.4X", utf16[i]);
188 details+="</td></tr>";
189 details+="<tr><td><b>"+i18n("UTF-8")+"</b></td><td>";
191 QByteArray utf8(str.toUtf8());
193 for(int i=0; i<utf8.size(); ++i)
195 if(i)
196 details+=' ';
197 details+=QString().sprintf("0x%2.2X", (unsigned char)(utf8.constData()[i]));
199 details+="</td></tr>";
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>";
209 details+="</table>";
210 itsLabel->setText(details);
212 QPixmap pix((int)(itsItem.width()*2.5), (int)(itsItem.height()*2.5));
213 QList<CFcEngine::TRange> range;
214 range.append(CFcEngine::TRange(itsItem.ucs4, 0));
216 if(itsParent->engine()->draw(itsParent->itsCurrentUrl, pix.width(), pix.height(), pix,
217 itsParent->itsCurrentFace-1, false, range,
218 NULL, itsParent->itsFontName, itsParent->itsStyleInfo))
219 itsPixmapLabel->setPixmap(pix);
220 else
221 itsPixmapLabel->setPixmap(QPixmap());
222 itsTimer->disconnect(this);
223 connect(itsTimer, SIGNAL(timeout()), this, SLOT(hideTip()));
224 itsTimer->setSingleShot(true);
225 itsTimer->start(15000);
227 kapp->installEventFilter(this);
228 reposition();
229 show();
232 void CCharTip::hideTip()
234 itsTimer->stop();
235 kapp->removeEventFilter(this);
236 hide();
239 void CCharTip::reposition()
241 QRect rect(itsItem);
243 rect.moveTopRight(itsParent->mapToGlobal(rect.topRight()));
245 QPoint pos(rect.center());
246 QRect desk(KGlobalSettings::desktopGeometry(rect.center()));
248 if ((rect.center().x() + width()) > desk.right())
250 if (pos.x() - width() < 0)
251 pos.setX(0);
252 else
253 pos.setX( pos.x() - width() );
255 // should the tooltip be shown above or below the ivi ?
256 if (rect.bottom() + height() > desk.bottom())
257 pos.setY( rect.top() - height() );
258 else
259 pos.setY(rect.bottom() + 1);
261 move(pos);
262 update();
265 void CCharTip::resizeEvent(QResizeEvent *event)
267 QFrame::resizeEvent(event);
268 reposition();
271 bool CCharTip::eventFilter(QObject *, QEvent *e)
273 switch (e->type())
275 case QEvent::Leave:
276 case QEvent::MouseButtonPress:
277 case QEvent::MouseButtonRelease:
278 case QEvent::KeyPress:
279 case QEvent::KeyRelease:
280 case QEvent::FocusIn:
281 case QEvent::FocusOut:
282 case QEvent::Wheel:
283 hideTip();
284 default: break;
287 return false;
292 #include "CharTip.moc"