• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kcharselect.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002 
00003    Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kcharselect.h"
00022 
00023 #include "kcharselect_p.h"
00024 
00025 #include <QtGui/QActionEvent>
00026 #include <QtGui/QDoubleSpinBox>
00027 #include <QtGui/QHeaderView>
00028 #include <QtGui/QBoxLayout>
00029 #include <QtGui/QSplitter>
00030 #include <QtGui/QPushButton>
00031 
00032 #include <kcombobox.h>
00033 #include <kdebug.h>
00034 #include <kdialog.h>
00035 #include <klocale.h>
00036 #include <klineedit.h>
00037 #include <ktextbrowser.h>
00038 #include <kfontcombobox.h>
00039 
00040 K_GLOBAL_STATIC(KCharSelectData, s_data)
00041 
00042 class KCharSelectTablePrivate
00043 {
00044 public:
00045     KCharSelectTablePrivate(KCharSelectTable *q): q(q), model(0) {}
00046     KCharSelectTable *q;
00047 
00048     QFont font;
00049     KCharSelectItemModel *model;
00050     QList<QChar> chars;
00051     QChar chr;
00052 
00053     void _k_resizeCells();
00054     void _k_doubleClicked(const QModelIndex & index);
00055     void _k_slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
00056 };
00057 
00058 class KCharSelect::KCharSelectPrivate
00059 {
00060 public:
00061     KLineEdit* searchLine;
00062     KFontComboBox *fontCombo;
00063     QSpinBox *fontSizeSpinBox;
00064     QComboBox *sectionCombo;
00065     QComboBox *blockCombo;
00066     KCharSelectTable *charTable;
00067     KTextBrowser *detailBrowser;
00068 
00069     KCharSelect *q;
00070 
00071     QString createLinks(QString s);
00072     void _k_fontSelected();
00073     void _k_updateCurrentChar(const QChar &c);
00074     void _k_slotUpdateUnicode(const QChar &c);
00075     void _k_sectionSelected(int index);
00076     void _k_blockSelected(int index);
00077     void _k_searchEditChanged();
00078     void _k_search();
00079     void _k_linkClicked(QUrl url);
00080 };
00081 
00082 /******************************************************************/
00083 /* Class: KCharSelectTable                                        */
00084 /******************************************************************/
00085 
00086 KCharSelectTable::KCharSelectTable(QWidget *parent, const QFont &_font)
00087         : QTableView(parent), d(new KCharSelectTablePrivate(this))
00088 {
00089     d->font = _font;
00090 
00091     setTabKeyNavigation(false);
00092     setSelectionMode(QAbstractItemView::SingleSelection);
00093     QPalette _palette;
00094     _palette.setColor(backgroundRole(), palette().color(QPalette::Base));
00095     setPalette(_palette);
00096     verticalHeader()->setVisible(false);
00097     verticalHeader()->setResizeMode(QHeaderView::Custom);
00098     horizontalHeader()->setVisible(false);
00099     horizontalHeader()->setResizeMode(QHeaderView::Custom);
00100 
00101     setFocusPolicy(Qt::StrongFocus);
00102     setDragEnabled(true);
00103     setAcceptDrops(true);
00104     setDropIndicatorShown(false);
00105     setDragDropMode(QAbstractItemView::DragDrop);
00106 
00107     connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(_k_doubleClicked(QModelIndex)));
00108 
00109     d->_k_resizeCells();
00110 }
00111 
00112 KCharSelectTable::~KCharSelectTable()
00113 {
00114     delete d;
00115 }
00116 
00117 void KCharSelectTable::setFont(const QFont &_font)
00118 {
00119     QTableView::setFont(_font);
00120     d->font = _font;
00121     if (d->model) d->model->setFont(_font);
00122     d->_k_resizeCells();
00123 }
00124 
00125 QChar KCharSelectTable::chr()
00126 {
00127     return d->chr;
00128 }
00129 
00130 QFont KCharSelectTable::font() const
00131 {
00132     return d->font;
00133 }
00134 
00135 QList<QChar> KCharSelectTable::displayedChars() const
00136 {
00137     return d->chars;
00138 }
00139 
00140 void KCharSelectTable::setChar(const QChar &c)
00141 {
00142     int pos = d->chars.indexOf(c);
00143     if (pos != -1) {
00144         setCurrentIndex(model()->index(pos / model()->columnCount(), pos % model()->columnCount()));
00145     }
00146 }
00147 
00148 void KCharSelectTable::setContents(QList<QChar> chars)
00149 {
00150     d->chars = chars;
00151 
00152     KCharSelectItemModel *m = d->model;
00153     d->model = new KCharSelectItemModel(chars, d->font, this);
00154     setModel(d->model);
00155     d->_k_resizeCells();
00156     QItemSelectionModel *selectionModel = new QItemSelectionModel(d->model);
00157     setSelectionModel(selectionModel);
00158     setSelectionBehavior(QAbstractItemView::SelectItems);
00159     setSelectionMode(QAbstractItemView::SingleSelection);
00160     connect(selectionModel, SIGNAL(currentChanged(const QModelIndex & , const QModelIndex &)), this, SLOT(_k_slotCurrentChanged(const QModelIndex &, const QModelIndex &)));
00161     connect(d->model, SIGNAL(showCharRequested(QChar)), this, SIGNAL(showCharRequested(QChar)));
00162     delete m; // this should hopefully delete aold selection models too, since it is the parent of them (didn't track, if there are setParent calls somewhere. Check that (jowenn)
00163 }
00164 
00165 void KCharSelectTable::scrollTo(const QModelIndex & index, ScrollHint hint)
00166 {
00167     // this prevents horizontal scrolling when selecting a character in the last column
00168     if (index.isValid() && index.column() != 0) {
00169         QTableView::scrollTo(d->model->index(index.row(), 0), hint);
00170     } else {
00171         QTableView::scrollTo(index, hint);
00172     }
00173 }
00174 
00175 void KCharSelectTablePrivate::_k_slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous)
00176 {
00177     Q_UNUSED(previous);
00178     if (!model) return;
00179     QVariant temp = model->data(current, KCharSelectItemModel::CharacterRole);
00180     if (temp.type() != QVariant::Char)
00181         return;
00182     QChar c = temp.toChar();
00183     chr = c;
00184     emit q->focusItemChanged(c);
00185 }
00186 
00187 void KCharSelectTable::resizeEvent(QResizeEvent * e)
00188 {
00189     QTableView::resizeEvent(e);
00190     if (e->size().width() != e->oldSize().width()) {
00191         d->_k_resizeCells();
00192     }
00193 }
00194 
00195 void KCharSelectTablePrivate::_k_resizeCells()
00196 {
00197     if (!q->model()) return;
00198     static_cast<KCharSelectItemModel*>(q->model())->updateColumnCount(q->viewport()->size().width());
00199 
00200     QChar oldChar = q->chr();
00201 
00202     const int new_w   = q->viewport()->size().width() / q->model()->columnCount(QModelIndex());
00203     const int columns = q->model()->columnCount(QModelIndex());
00204     const int rows = q->model()->rowCount(QModelIndex());
00205     q->setUpdatesEnabled(false);
00206     QHeaderView* hv = q->horizontalHeader();
00207     int spaceLeft = q->viewport()->size().width() % new_w + 1;
00208     for (int i = 0;i <= columns;i++) {
00209         if (i < spaceLeft) {
00210             hv->resizeSection(i, new_w + 1);
00211         } else {
00212             hv->resizeSection(i, new_w);
00213         }
00214     }
00215 
00216     hv = q->verticalHeader();
00217     const int new_h = QFontMetrics(font).xHeight() * 3;
00218     for (int i = 0;i < rows;i++) {
00219         hv->resizeSection(i, new_h);
00220     }
00221 
00222     q->setUpdatesEnabled(true);
00223     q->setChar(oldChar);
00224 }
00225 
00226 void KCharSelectTablePrivate::_k_doubleClicked(const QModelIndex & index)
00227 {
00228     QChar c = model->data(index, KCharSelectItemModel::CharacterRole).toChar();
00229     if (c.isPrint()) {
00230         emit q->activated(c);
00231     }
00232 }
00233 
00234 void KCharSelectTable::keyPressEvent(QKeyEvent *e)
00235 {
00236     if (d->model)
00237         switch (e->key()) {
00238         case Qt::Key_Space:
00239             emit activated(' ');
00240             return;
00241             break;
00242     case Qt::Key_Enter: case Qt::Key_Return: {
00243             if (!currentIndex().isValid()) return;
00244             QChar c = d->model->data(currentIndex(), KCharSelectItemModel::CharacterRole).toChar();
00245             if (c.isPrint()) {
00246                 emit activated(c);
00247             }
00248         }
00249         return;
00250         break;
00251         }
00252     QTableView::keyPressEvent(e);
00253 }
00254 
00255 
00256 /******************************************************************/
00257 /* Class: KCharSelect                                             */
00258 /******************************************************************/
00259 
00260 KCharSelect::KCharSelect(QWidget *parent, const Controls controls)
00261         : QWidget(parent), d(new KCharSelectPrivate)
00262 {
00263     d->q = this;
00264 
00265     QVBoxLayout *mainLayout = new QVBoxLayout(this);
00266     mainLayout->setMargin(0);
00267     if (SearchLine & controls) {
00268         QHBoxLayout *searchLayout = new QHBoxLayout();
00269         mainLayout->addLayout(searchLayout);
00270         d->searchLine = new KLineEdit(this);
00271         searchLayout->addWidget(d->searchLine);
00272         d->searchLine->setClickMessage(i18n("Enter a search term or character here"));
00273         d->searchLine->setClearButtonShown(true);
00274         d->searchLine->setToolTip(i18n("Enter a search term or character here"));
00275         connect(d->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_k_searchEditChanged()));
00276 
00277         QPushButton* searchButton = new QPushButton(i18n("Search"), this);
00278         searchLayout->addWidget(searchButton);
00279         connect(d->searchLine, SIGNAL(returnPressed(QString)), searchButton, SLOT(animateClick()));
00280         connect(searchButton, SIGNAL(pressed()), this, SLOT(_k_search()));
00281     }
00282 
00283     if ((SearchLine & controls) && ((FontCombo & controls) || (FontSize & controls) || (BlockCombos & controls))) {
00284         QFrame* line = new QFrame(this);
00285         line->setFrameShape(QFrame::HLine);
00286         line->setFrameShadow(QFrame::Sunken);
00287         mainLayout->addWidget(line);
00288     }
00289 
00290     QHBoxLayout *comboLayout = new QHBoxLayout();
00291 
00292 
00293     d->fontCombo = new KFontComboBox(this);
00294     comboLayout->addWidget(d->fontCombo);
00295     d->fontCombo->setEditable(true);
00296     d->fontCombo->resize(d->fontCombo->sizeHint());
00297     d->fontCombo->setToolTip(i18n("Set font"));
00298 
00299     d->fontSizeSpinBox = new QSpinBox(this);
00300     comboLayout->addWidget(d->fontSizeSpinBox);
00301     d->fontSizeSpinBox->setValue(QWidget::font().pointSize());
00302     d->fontSizeSpinBox->setRange(1, 400);
00303     d->fontSizeSpinBox->setSingleStep(1);
00304     d->fontSizeSpinBox->setToolTip(i18n("Set font size"));
00305 
00306     connect(d->fontCombo, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(_k_fontSelected()));
00307     connect(d->fontSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(_k_fontSelected()));
00308 
00309 
00310     d->sectionCombo = new KComboBox(this);
00311     d->sectionCombo->setToolTip(i18n("Select a category"));
00312     comboLayout->addWidget(d->sectionCombo);
00313     d->blockCombo = new KComboBox(this);
00314     d->blockCombo->setToolTip(i18n("Select a block to be displayed"));
00315     d->blockCombo->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
00316     comboLayout->addWidget(d->blockCombo, 1);
00317     d->sectionCombo->addItems(s_data->sectionList());
00318     d->blockCombo->setMinimumWidth(QFontMetrics(QWidget::font()).averageCharWidth() * 25);
00319 
00320     connect(d->sectionCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(_k_sectionSelected(int)));
00321     connect(d->blockCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(_k_blockSelected(int)));
00322     if ((FontCombo & controls) || (FontSize & controls) || (BlockCombos & controls)) {
00323         mainLayout->addLayout(comboLayout);
00324     }
00325     if (!(FontCombo & controls)) {
00326         d->fontCombo->hide();
00327     }
00328     if (!(FontSize & controls)) {
00329         d->fontSizeSpinBox->hide();
00330     }
00331     if (!(BlockCombos & controls)) {
00332         d->sectionCombo->hide();
00333         d->blockCombo->hide();
00334     }
00335 
00336     QSplitter *splitter = new QSplitter(this);
00337     if ((CharacterTable & controls) || (DetailBrowser & controls)) {
00338         mainLayout->addWidget(splitter);
00339     } else {
00340         splitter->hide();
00341     }
00342     d->charTable = new KCharSelectTable(this, QFont());
00343     if (CharacterTable & controls) {
00344         splitter->addWidget(d->charTable);
00345         d->charTable->setFocus(Qt::OtherFocusReason);
00346     } else {
00347         d->charTable->hide();
00348     }
00349 
00350     const QSize sz(200, 200);
00351     d->charTable->resize(sz);
00352     d->charTable->setMinimumSize(sz);
00353 
00354     d->charTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00355 
00356     setCurrentFont(QFont());
00357 
00358     connect(d->charTable, SIGNAL(focusItemChanged(const QChar &)), this, SLOT(_k_updateCurrentChar(const QChar &)));
00359     connect(d->charTable, SIGNAL(activated(const QChar &)), this, SIGNAL(charSelected(const QChar &)));
00360     connect(d->charTable, SIGNAL(focusItemChanged(const QChar &)),
00361             this, SIGNAL(currentCharChanged(const QChar &)));
00362 
00363     connect(d->charTable, SIGNAL(showCharRequested(QChar)), this, SLOT(setCurrentChar(QChar)));
00364 
00365     d->detailBrowser = new KTextBrowser(this);
00366     if (DetailBrowser & controls) {
00367         splitter->addWidget(d->detailBrowser);
00368     } else {
00369         d->detailBrowser->hide();
00370     }
00371     d->detailBrowser->setOpenLinks(false);
00372     connect(d->detailBrowser, SIGNAL(anchorClicked(QUrl)), this, SLOT(_k_linkClicked(QUrl)));
00373 
00374     setFocusPolicy(Qt::StrongFocus);
00375     setFocusProxy(d->charTable);
00376     d->_k_sectionSelected(0);
00377     d->_k_blockSelected(0);
00378     setCurrentChar(0x0);
00379 }
00380 
00381 KCharSelect::~KCharSelect()
00382 {
00383     delete d;
00384 }
00385 
00386 QSize KCharSelect::sizeHint() const
00387 {
00388     return QWidget::sizeHint();
00389 }
00390 
00391 void KCharSelect::setCurrentFont(const QFont &_font)
00392 {
00393     d->fontCombo->setCurrentFont(_font);
00394     d->fontSizeSpinBox->setValue(_font.pointSize());
00395     d->_k_fontSelected();
00396 }
00397 
00398 QChar KCharSelect::currentChar() const
00399 {
00400     return d->charTable->chr();
00401 }
00402 
00403 QFont KCharSelect::currentFont() const
00404 {
00405     return d->charTable->font();
00406 }
00407 
00408 QList<QChar> KCharSelect::displayedChars() const
00409 {
00410     return d->charTable->displayedChars();
00411 }
00412 
00413 void KCharSelect::setCurrentChar(const QChar &c)
00414 {
00415     int block = s_data->blockIndex(c);
00416     int section = s_data->sectionIndex(block);
00417     d->sectionCombo->setCurrentIndex(section);
00418     int index = d->blockCombo->findData(block);
00419     if (index != -1) {
00420         d->blockCombo->setCurrentIndex(index);
00421     }
00422     d->charTable->setChar(c);
00423 }
00424 
00425 void KCharSelect::KCharSelectPrivate::_k_fontSelected()
00426 {
00427     QFont font = fontCombo->currentFont();
00428     font.setPointSize(fontSizeSpinBox->value());
00429     charTable->setFont(font);
00430     emit q->currentFontChanged(font);
00431 }
00432 
00433 void KCharSelect::KCharSelectPrivate::_k_updateCurrentChar(const QChar &c)
00434 {
00435     if(blockCombo->isEnabled() == false) {
00436         //we are in search mode. make the two comboboxes show the section & block for this character.
00437         //(when we are not in search mode the current character always belongs to the current section & block.)
00438         int block = s_data->blockIndex(c);
00439         int section = s_data->sectionIndex(block);
00440         sectionCombo->setCurrentIndex(section);
00441         int index = blockCombo->findData(block);
00442         if (index != -1) {
00443             blockCombo->setCurrentIndex(index);
00444         }
00445     }
00446 
00447     _k_slotUpdateUnicode(c);
00448 }
00449 
00450 void KCharSelect::KCharSelectPrivate::_k_slotUpdateUnicode(const QChar &c)
00451 {
00452     QString html;
00453     // Qt internally uses U+FDD0 and U+FDD1 to mark the beginning and the end of frames.
00454     // They should be seen as non-printable characters, as trying to display them leads
00455     //  to a crash caused by a Qt "noBlockInString" assertion.
00456     if (c.isPrint() && c.unicode() != 0xFDD0 && c.unicode() != 0xFDD1) {
00457     // Wrap Combining Diacritical Marks in spaces to prevent them from being combined with the text around them
00458     // It still doesn't look perfect, but at least better than without the spaces
00459     QString combiningSpace;
00460     if(s_data->block(c) == i18nc("KCharselect unicode block name", "Combining Diacritical Marks")) {
00461         combiningSpace = "&nbsp;";
00462     }
00463         html = QString("<p>" + i18n("Character:") + " <font size=\"+4\" face=\"") + charTable->font().family() + "\">" + combiningSpace + "&#" + QString::number(c.unicode()) + ";" + combiningSpace + "</font> " + s_data->formatCode(c.unicode())  + "<br>";
00464     } else {
00465         html = QString("<p>" + i18n("Character:") + " <b>" + i18n("Non-printable") + "</b> ") + s_data->formatCode(c.unicode())  + "<br>";
00466     }
00467     QString name = s_data->name(c);
00468     if (!name.isEmpty()) {
00469         html += i18n("Name: ") + Qt::escape(name) + "</p>";
00470     }
00471     QStringList aliases = s_data->aliases(c);
00472     QStringList notes = s_data->notes(c);
00473     QList<QChar> seeAlso = s_data->seeAlso(c);
00474     QStringList equivalents = s_data->equivalents(c);
00475     QStringList approxEquivalents = s_data->approximateEquivalents(c);
00476     if (!(aliases.isEmpty() && notes.isEmpty() && seeAlso.isEmpty() && equivalents.isEmpty() && approxEquivalents.isEmpty())) {
00477         html += "<p><b>" + i18n("Annotations and Cross References") + "</b></p>";
00478     }
00479 
00480     if (!aliases.isEmpty()) {
00481         html += "<p style=\"margin-bottom: 0px;\">" + i18n("Alias names:") + "</p><ul style=\"margin-top: 0px;\">";
00482         foreach(const QString &alias, aliases) {
00483             html += "<li>" + Qt::escape(alias) + "</li>";
00484         }
00485         html += "</ul>";
00486     }
00487 
00488     if (!notes.isEmpty()) {
00489         html += "<p style=\"margin-bottom: 0px;\">" + i18n("Notes:") + "</p><ul style=\"margin-top: 0px;\">";
00490         foreach(const QString &note, notes) {
00491             html += "<li>" + createLinks(Qt::escape(note)) + "</li>";
00492         }
00493         html += "</ul>";
00494     }
00495 
00496     if (!seeAlso.isEmpty()) {
00497         html += "<p style=\"margin-bottom: 0px;\">" + i18n("See also:") + "</p><ul style=\"margin-top: 0px;\">";
00498         foreach(const QChar &c2, seeAlso) {
00499             html += "<li><a href=\"" + QString::number(c2.unicode(), 16) + "\">";
00500             if (c2.isPrint()) {
00501                 html += "&#" + QString::number(c2.unicode()) + "; ";
00502             }
00503             html += s_data->formatCode(c2.unicode()) + ' ' + Qt::escape(s_data->name(c2)) + "</a></li>";
00504         }
00505         html += "</ul>";
00506     }
00507 
00508     if (!equivalents.isEmpty()) {
00509         html += "<p style=\"margin-bottom: 0px;\">" + i18n("Equivalents:") + "</p><ul style=\"margin-top: 0px;\">";
00510         foreach(const QString &equivalent, equivalents) {
00511             html += "<li>" + createLinks(Qt::escape(equivalent)) + "</li>";
00512         }
00513         html += "</ul>";
00514     }
00515 
00516     if (!approxEquivalents.isEmpty()) {
00517         html += "<p style=\"margin-bottom: 0px;\">" + i18n("Approximate equivalents:") + "</p><ul style=\"margin-top: 0px;\">";
00518         foreach(const QString &approxEquivalent, approxEquivalents) {
00519             html += "<li>" + createLinks(Qt::escape(approxEquivalent)) + "</li>";
00520         }
00521         html += "</ul>";
00522     }
00523 
00524     QStringList unihan = s_data->unihanInfo(c);
00525     if (unihan.count() == 7) {
00526         html += "<p><b>" + i18n("CJK Ideograph Information") + "</b></p><p>";
00527         bool newline = true;
00528         if (!unihan[0].isEmpty()) {
00529             html += i18n("Definition in English: ") + unihan[0];
00530             newline = false;
00531         }
00532         if (!unihan[2].isEmpty()) {
00533             if (!newline) html += "<br>";
00534             html += i18n("Mandarin Pronunciation: ") + unihan[2];
00535             newline = false;
00536         }
00537         if (!unihan[1].isEmpty()) {
00538             if (!newline) html += "<br>";
00539             html += i18n("Cantonese Pronunciation: ") + unihan[1];
00540             newline = false;
00541         }
00542         if (!unihan[6].isEmpty()) {
00543             if (!newline) html += "<br>";
00544             html += i18n("Japanese On Pronunciation: ") + unihan[6];
00545             newline = false;
00546         }
00547         if (!unihan[5].isEmpty()) {
00548             if (!newline) html += "<br>";
00549             html += i18n("Japanese Kun Pronunciation: ") + unihan[5];
00550             newline = false;
00551         }
00552         if (!unihan[3].isEmpty()) {
00553             if (!newline) html += "<br>";
00554             html += i18n("Tang Pronunciation: ") + unihan[3];
00555             newline = false;
00556         }
00557         if (!unihan[4].isEmpty()) {
00558             if (!newline) html += "<br>";
00559             html += i18n("Korean Pronunciation: ") + unihan[4];
00560             newline = false;
00561         }
00562         html += "</p>";
00563     }
00564 
00565     html += "<p><b>" + i18n("General Character Properties") + "</b><br>";
00566     html += i18n("Block: ") + s_data->block(c) + "<br>";
00567     html += i18n("Unicode category: ") + s_data->categoryText(c.category()) + "</p>";
00568 
00569     QByteArray utf8 = QString(c).toUtf8();
00570 
00571     html += "<p><b>" + i18n("Various Useful Representations") + "</b><br>";
00572     html += i18n("UTF-8:");
00573     foreach(unsigned char c, utf8)
00574     html += ' ' + s_data->formatCode(c, 2, "0x");
00575     html += "<br>" + i18n("UTF-16: ") + s_data->formatCode(c.unicode(), 4, "0x") + "<br>";
00576     html += i18n("C octal escaped UTF-8: ");
00577     foreach(unsigned char c, utf8)
00578     html += s_data->formatCode(c, 3, "\\", 8);
00579     html += "<br>" + i18n("XML decimal entity:") + " &amp;#" + QString::number(c.unicode()) + ";</p>";
00580 
00581     detailBrowser->setHtml(html);
00582 }
00583 
00584 QString KCharSelect::KCharSelectPrivate::createLinks(QString s)
00585 {
00586     QRegExp rx("\\b([\\dABCDEF]{4})\\b");
00587 
00588     QStringList chars;
00589     int pos = 0;
00590 
00591     while ((pos = rx.indexIn(s, pos)) != -1) {
00592         chars << rx.cap(1);
00593         pos += rx.matchedLength();
00594     }
00595 
00596     QSet<QString> chars2 = QSet<QString>::fromList(chars);
00597     foreach(const QString &c, chars2) {
00598         int unicode = c.toInt(0, 16);
00599         QString link = "<a href=\"" + c + "\">";
00600         if (QChar(unicode).isPrint()) {
00601             link += "&#" + QString::number(unicode) + ";&nbsp;";
00602         }
00603         link += "U+" + c + ' ';
00604         link += Qt::escape(s_data->name(QChar(unicode))) + "</a>";
00605         s.replace(c, link);
00606     }
00607     return s;
00608 }
00609 
00610 void KCharSelect::KCharSelectPrivate::_k_sectionSelected(int index)
00611 {
00612     blockCombo->clear();
00613     QList<int> blocks = s_data->sectionContents(index);
00614     foreach(int block, blocks) {
00615         blockCombo->addItem(s_data->blockName(block), QVariant(block));
00616     }
00617     blockCombo->setCurrentIndex(0);
00618 }
00619 
00620 void KCharSelect::KCharSelectPrivate::_k_blockSelected(int index)
00621 {
00622     if(blockCombo->isEnabled() == false) {
00623         //we are in search mode, so don't fill the table with this block.
00624         return;
00625     }
00626 
00627     int block = blockCombo->itemData(index).toInt();
00628     const QList<QChar> contents = s_data->blockContents(block);
00629     if(contents.count() <= index) {
00630         return;
00631     }
00632     charTable->setContents(contents);
00633     emit q->displayedCharsChanged();
00634     charTable->setChar(contents[0]);
00635 }
00636 
00637 void KCharSelect::KCharSelectPrivate::_k_searchEditChanged()
00638 {
00639     if (searchLine->text().isEmpty()) {
00640         sectionCombo->setEnabled(true);
00641         blockCombo->setEnabled(true);
00642 
00643         //upon leaving search mode, keep the same character selected
00644         QChar c = charTable->chr();
00645         _k_blockSelected(blockCombo->currentIndex());
00646         q->setCurrentChar(c);
00647     } else {
00648         sectionCombo->setEnabled(false);
00649         blockCombo->setEnabled(false);
00650     }
00651 }
00652 
00653 void KCharSelect::KCharSelectPrivate::_k_search()
00654 {
00655     if (searchLine->text().isEmpty()) {
00656         return;
00657     }
00658     charTable->setContents(s_data->find(searchLine->text()));
00659     emit q->displayedCharsChanged();
00660 }
00661 
00662 void  KCharSelect::KCharSelectPrivate::_k_linkClicked(QUrl url)
00663 {
00664     QString hex = url.toString();
00665     if (hex.size() > 4) {
00666         return;
00667     }
00668     int unicode = hex.toInt(0, 16);
00669     searchLine->clear();
00670     q->setCurrentChar(QChar(unicode));
00671 }
00672 
00674 
00675 QVariant KCharSelectItemModel::data(const QModelIndex &index, int role) const
00676 {
00677     int pos = m_columns * (index.row()) + index.column();
00678     if (pos >= m_chars.size() || index.row() < 0 || index.column() < 0) {
00679         if (role == Qt::BackgroundColorRole) {
00680             return QVariant(qApp->palette().color(QPalette::Button));
00681         }
00682         return QVariant();
00683     }
00684 
00685     QChar c = m_chars[pos];
00686     if (!index.isValid())
00687         return QVariant();
00688     else if (role == Qt::ToolTipRole) {
00689         QString s;
00690         if (c.isPrint()) {
00691             s = "&#" + QString::number(c.unicode()) + ';';
00692             // Wrap Combining Diacritical Marks in spaces
00693             // It still doesn't look perfect, but at least better than without the spaces
00694             if(s_data->block(c) == i18nc("KCharselect unicode block name", "Combining Diacritical Marks")) {
00695                 s = "&nbsp;" + s + "&nbsp;";
00696             }
00697         } else {
00698             s = i18n("Non-printable");
00699         }
00700         QString result = i18nc("Character", "<qt><font size=\"+4\" face=\"%1\">%2</font><br />%3<br />Unicode code point: %4<br />(In decimal: %5)</qt>" ,  m_font.family() ,  s , Qt::escape(s_data->name(c)), s_data->formatCode(c.unicode()) ,  c.unicode());
00701         return QVariant(result);
00702     } else if (role == Qt::TextAlignmentRole)
00703         return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
00704     else if (role == Qt::DisplayRole) {
00705         if (c.isPrint())
00706             return QVariant(c);
00707         return QVariant();
00708     } else if (role == Qt::BackgroundColorRole) {
00709         QFontMetrics fm = QFontMetrics(m_font);
00710         if (fm.inFont(c) && c.isPrint())
00711             return QVariant(qApp->palette().color(QPalette::Base));
00712         else
00713             return QVariant(qApp->palette().color(QPalette::Button));
00714     } else if (role == Qt::FontRole)
00715         return QVariant(m_font);
00716     else if (role == CharacterRole) {
00717         return QVariant(c);
00718     }
00719     return QVariant();
00720 }
00721 
00722 #include "kcharselect.moc"
00723 #include "kcharselect_p.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal