• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kcharselect.cpp
1/* This file is part of the KDE libraries
2
3 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kcharselect.h"
22#include "kcharselect.moc"
23
24#include <tqbrush.h>
25#include <tqcolor.h>
26#include <tqevent.h>
27#include <tqfont.h>
28#include <tqfontdatabase.h>
29#include <tqhbox.h>
30#include <tqkeycode.h>
31#include <tqlabel.h>
32#include <tqpainter.h>
33#include <tqpen.h>
34#include <tqregexp.h>
35#include <tqstyle.h>
36#include <tqstylesheet.h>
37#include <tqtooltip.h>
38#include <tqvalidator.h>
39
40#include <tdeapplication.h>
41#include <kdebug.h>
42#include <kdialog.h>
43#include <klineedit.h>
44#include <tdelocale.h>
45
46class KCharSelectTableToolTip;
47class KCharSelectTable::KCharSelectTablePrivate
48{
49public:
50 KCharSelectTableToolTip *t = nullptr;
51};
52
53class KCharSelect::KCharSelectPrivate
54{
55public:
56 TQLineEdit *unicodeLine = nullptr;
57 TQScrollBar *scrollBar = nullptr;
58};
59
60TQFontDatabase * KCharSelect::fontDataBase = 0;
61
62void KCharSelect::cleanupFontDatabase()
63{
64 delete fontDataBase;
65 fontDataBase = 0;
66}
67
68/******************************************************************/
69/* Class: KCharSelectTableToolTip */
70/******************************************************************/
71
75class KCharSelectTableToolTip : public TQToolTip
76{
77public:
78 // Note: we pass TQToolTip becuse we want to display tooltips in vieport's coordinates.
79 KCharSelectTableToolTip(KCharSelectTable * parent)
80 : TQToolTip(parent->viewport()), cst(parent) {}
81
82protected:
83 void maybeTip( const TQPoint &pnt) {
84 // Point in content coodrinates; since we don't move viewport in KCharSelectTableToolTip
85 // it should be exactly the same, but to be on the safe side we assume it's not
86 TQPoint cPnt = cst->viewportToContents(pnt);
87
88 int col = cst->columnAt( cPnt.x() );
89 int row = cst->rowAt( cPnt.y() );
90 if ( col < 0 || row < 0 || col > cst->numCols()-1 || row > cst->numRows()-1 ) {
91 return;
92 }
93 TQChar ch = cst->charAt( row, col );
94 if (ch.isNull()) {
95 return;
96 }
97
98 TQRect r = cst->cellGeometry( row, col );
99 r = TQRect( cst->contentsToViewport(r.topLeft()), r.size());
100
101 TQString hex = TQString().sprintf( "%04X", ch.unicode() );
102 TQString character = TQStyleSheet::escape(ch); // Character sometimes need to be escaped
103
104 tip(r, i18n( "Character",
105 "<qt><font size=\"+4\" face=\"%1\">%2</font>"
106 "<br>Unicode code point: U+%3"
107 "<br>(In decimal: %4)"
108 "<br>(Character: %5)"
109 "</qt>" ).arg( cst->font() )
110 .arg( character )
111 .arg( hex )
112 .arg( ch.unicode() )
113 .arg( character )
114 );
115 }
116private:
117 KCharSelectTable *cst;
118};
119
120
121/******************************************************************/
122/* Class: KCharSelectTable */
123/******************************************************************/
124
125//==================================================================
126KCharSelectTable::KCharSelectTable( TQWidget *parent, const char *name, const TQString &_font,
127 const TQChar &_chr, int _tableNum )
128 : TQGridView( parent, name, TQt::WNoAutoErase ), vFont( _font ), vChr( _chr ),
129 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ),
130 d(new KCharSelectTable::KCharSelectTablePrivate)
131{
132 // Note: when updating we are completely overpainting the widget to
133 // avoid flickering, hence we use TQt::WNoAutoErase.
134 setCellWidth( 20 );
135 setCellHeight( 25 );
136
137 setNumCols( 32 );
138 setNumRows( 8 );
139
140 updateContents();
141
142 d->t = new KCharSelectTableToolTip(this);
143
144 setFocusPolicy( TQWidget::StrongFocus );
145}
146
147KCharSelectTable::~KCharSelectTable () {
148 delete d->t;
149 delete d;
150}
151
152//==================================================================
153void KCharSelectTable::setFont( const TQString &_font )
154{
155 if(_font == vFont)
156 return;
157 vFont = _font;
158 updateContents();
159}
160
161//==================================================================
162void KCharSelectTable::setChar( const TQChar &_chr )
163{
164 if(_chr == vChr)
165 return;
166 vChr = _chr;
167 updateContents();
168}
169
170//==================================================================
171void KCharSelectTable::setTableNum( int _tableNum )
172{
173 if(_tableNum == vTableNum)
174 return;
175 focusItem = TQChar( _tableNum * 256 );
176
177 vTableNum = _tableNum;
178 updateContents();
179
180 emit tableNumChanged(vTableNum);
181}
182
183//==================================================================
184TQSize KCharSelectTable::sizeHint() const
185{
186 int w = cellWidth();
187 int h = cellHeight();
188
189 w *= numCols();
190 h *= numRows();
191
192 return TQSize( w, h );
193}
194
195//==================================================================
196// BCI: remove me
197void KCharSelectTable::resizeEvent( TQResizeEvent *e )
198{
199 TQGridView::resizeEvent(e);
200}
201
202//==================================================================
203void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
204{
205 const int new_w = e->size().width() / numCols();
206 const int new_h = e->size().height() / numRows();
207
208 if( new_w != cellWidth())
209 setCellWidth( new_w );
210 if( new_h != cellHeight())
211 setCellHeight( new_h );
212
213 TQGridView::viewportResizeEvent(e);
214}
215
216//==================================================================
217void KCharSelectTable::paintCell( class TQPainter* p, int row, int col )
218{
219 const int w = cellWidth();
220 const int h = cellHeight();
221 const int x2 = w - 1;
222 const int y2 = h - 1;
223
224 //if( row == 0 && col == 0 ) {
225 // printf("Repaint %d\n", temp++);
226 // fflush( stdout );
227 // }
228
229 TQFont font = TQFont( vFont );
230 font.setPixelSize( int(.7 * h) );
231
232 unsigned short c = vTableNum * 256;
233 c += row * numCols();
234 c += col;
235
236 if ( c == vChr.unicode() ) {
237 p->setBrush( TQBrush( colorGroup().highlight() ) );
238 p->setPen( NoPen );
239 p->drawRect( 0, 0, w, h );
240 p->setPen( colorGroup().highlightedText() );
241 vPos = TQPoint( col, row );
242 } else {
243 TQFontMetrics fm = TQFontMetrics( font );
244 if( fm.inFont( c ) )
245 p->setBrush( TQBrush( colorGroup().base() ) );
246 else
247 p->setBrush( TQBrush( colorGroup().button() ) );
248 p->setPen( NoPen );
249 p->drawRect( 0, 0, w, h );
250 p->setPen( colorGroup().text() );
251 }
252
253 if ( c == focusItem.unicode() && hasFocus() ) {
254 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
255 colorGroup() );
256 focusPos = TQPoint( col, row );
257 }
258
259 p->setFont( font );
260
261 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
262
263 p->setPen( colorGroup().text() );
264 p->drawLine( x2, 0, x2, y2 );
265 p->drawLine( 0, y2, x2, y2 );
266
267 if ( row == 0 )
268 p->drawLine( 0, 0, x2, 0 );
269 if ( col == 0 )
270 p->drawLine( 0, 0, 0, y2 );
271}
272
273//==================================================================
274// BCI: remove me
275void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
276{
277 TQGridView::mouseMoveEvent(e);
278}
279
280//==================================================================
281void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
282{
283 contentsMouseMoveEvent(e);
284}
285
286//==================================================================
287void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
288{
289 contentsMouseMoveEvent(e);
290 emit doubleClicked();
291}
292
293//==================================================================
294void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
295{
296 contentsMouseMoveEvent( e );
297
298 if( e->isAccepted() ) {
299 emit activated( chr() );
300 emit activated();
301 }
302}
303
304//==================================================================
305void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
306{
307 const int row = rowAt( e->y() );
308 const int col = columnAt( e->x() );
309 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
310 const TQPoint oldPos = vPos;
311
312 vPos.setX( col );
313 vPos.setY( row );
314
315 vChr = charAt( vPos.y(), vPos.x() );
316
317 const TQPoint oldFocus = focusPos;
318
319 focusPos = vPos;
320 focusItem = vChr;
321
322 updateCell( oldFocus.y(), oldFocus.x() );
323 updateCell( oldPos.y(), oldPos.x() );
324 updateCell( vPos.y(), vPos.x() );
325
326 emit highlighted( vChr );
327 emit highlighted();
328
329 emit focusItemChanged( focusItem );
330 emit focusItemChanged();
331
332 e->accept();
333 } else {
334 e->ignore();
335 }
336}
337
338//==================================================================
339void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
340{
341 switch ( e->key() ) {
342 case Key_Left:
343 gotoLeft();
344 break;
345 case Key_Right:
346 gotoRight();
347 break;
348 case Key_Up:
349 gotoUp();
350 break;
351 case Key_Down:
352 gotoDown();
353 break;
354 case Key_Next:
355 if ( tableNum() < 255 ) {
356 setTableNum( tableNum() + 1 );
357 }
358 break;
359 case Key_Prior:
360 if ( tableNum() > 0 ) {
361 setTableNum( tableNum() - 1 );
362 }
363 break;
364 case Key_Space:
365 emit activated( ' ' );
366 emit activated();
367 emit highlighted( ' ' );
368 emit highlighted();
369 break;
370 case Key_Enter: case Key_Return: {
371 const TQPoint oldPos = vPos;
372
373 vPos = focusPos;
374 vChr = focusItem;
375
376 updateCell( oldPos.y(), oldPos.x() );
377 updateCell( vPos.y(), vPos.x() );
378
379 emit activated( vChr );
380 emit activated();
381 emit highlighted( vChr );
382 emit highlighted();
383 } break;
384 }
385}
386
387//==================================================================
388void KCharSelectTable::gotoLeft()
389{
390 if ( focusPos.x() > 0 ) {
391 doGoto(-1, 0);
392 }
393}
394
395//==================================================================
396void KCharSelectTable::gotoRight()
397{
398 if ( focusPos.x() < numCols()-1 ) {
399 doGoto(1, 0);
400 }
401}
402
403//==================================================================
404void KCharSelectTable::gotoUp()
405{
406 if ( focusPos.y() > 0 ) {
407 doGoto(0, -1);
408 } else if ( tableNum() > 0 ) {
409 emit tableNumChanged(--vTableNum);
410 doGoto(0, numRows()-1);
411 repaintContents( false );
412 }
413}
414
415//==================================================================
416void KCharSelectTable::gotoDown()
417{
418 if ( focusPos.y() < numRows()-1 ) {
419 doGoto(0, +1);
420 } else if ( tableNum() < 255 ) {
421 emit tableNumChanged(++vTableNum);
422 doGoto(0, -(numRows()-1));
423 repaintContents( false );
424 }
425}
426
427//==================================================================
428void KCharSelectTable::doGoto(int dx, int dy)
429{
430 TQPoint oldPos = focusPos;
431 focusPos += TQPoint(dx, dy);
432 focusItem = charAt( focusPos.y(), focusPos.x() );
433
434 updateCell( oldPos.y(), oldPos.x() );
435 updateCell( focusPos.y(), focusPos.x() );
436
437 emit focusItemChanged( vChr );
438 emit focusItemChanged();
439}
440
441//==================================================================
442TQChar KCharSelectTable::charAt(int row, int col) const
443{
444 return TQChar( vTableNum * 256 + numCols() * row + col );
445}
446
447/******************************************************************/
448/* Class: KCharSelect */
449/******************************************************************/
450
451//==================================================================
452KCharSelect::KCharSelect( TQWidget *parent, const char *name, const TQString &_font, const TQChar &_chr, int _tableNum )
453 : TQVBox( parent, name ), d(new KCharSelectPrivate)
454{
455 setSpacing( KDialog::spacingHint() );
456 TQHBox* const bar = new TQHBox( this );
457 bar->setSpacing( KDialog::spacingHint() );
458
459 TQLabel* const lFont = new TQLabel( i18n( "Font:" ), bar );
460 lFont->resize( lFont->sizeHint() );
461 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
462 lFont->setMaximumWidth( lFont->sizeHint().width() );
463
464 fontCombo = new TQComboBox( true, bar );
465 fillFontCombo();
466 fontCombo->resize( fontCombo->sizeHint() );
467
468 connect( fontCombo, TQ_SIGNAL( activated( const TQString & ) ), this, TQ_SLOT( fontSelected( const TQString & ) ) );
469
470 TQLabel* const lTable = new TQLabel( i18n( "Table:" ), bar );
471 lTable->resize( lTable->sizeHint() );
472 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
473 lTable->setMaximumWidth( lTable->sizeHint().width() );
474
475 tableSpinBox = new TQSpinBox( 0, 255, 1, bar );
476 tableSpinBox->resize( tableSpinBox->sizeHint() );
477
478 TQLabel* const lUnicode = new TQLabel( i18n( "&Unicode code point:" ), bar );
479 lUnicode->resize( lUnicode->sizeHint() );
480 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
481 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
482
483 const TQRegExp rx( "[a-fA-F0-9]{1,4}" );
484 TQValidator* const validator = new TQRegExpValidator( rx, this );
485
486 d->unicodeLine = new KLineEdit( bar );
487 d->unicodeLine->setValidator(validator);
488 lUnicode->setBuddy(d->unicodeLine);
489 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
490 slotUpdateUnicode(_chr);
491
492 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ), this, TQ_SLOT( slotUnicodeEntered() ) );
493
494 TQHBox* const box = new TQHBox(this);
495 box->setSpacing(0);
496
497 charTable = new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
498 const TQSize sz( charTable->contentsWidth() + 4 ,
499 charTable->contentsHeight() + 4 );
500 charTable->resize( sz );
501 //charTable->setMaximumSize( sz );
502 charTable->setMinimumSize( sz );
503 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
504 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
505 charTable->installEventFilter( this );
506
507 d->scrollBar = new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
508
509 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
510 setTableNum( _tableNum );
511
512 connect( tableSpinBox, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
513 connect( d->scrollBar, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
514 connect( charTable, TQ_SIGNAL( tableNumChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
515
516 connect( charTable, TQ_SIGNAL( highlighted( const TQChar & ) ), this, TQ_SLOT( slotUpdateUnicode( const TQChar & ) ) );
517 connect( charTable, TQ_SIGNAL( highlighted( const TQChar & ) ), this, TQ_SLOT( charHighlighted( const TQChar & ) ) );
518 connect( charTable, TQ_SIGNAL( highlighted() ), this, TQ_SLOT( charHighlighted() ) );
519 connect( charTable, TQ_SIGNAL( activated( const TQChar & ) ), this, TQ_SLOT( charActivated( const TQChar & ) ) );
520 connect( charTable, TQ_SIGNAL( activated() ), this, TQ_SLOT( charActivated() ) );
521 connect( charTable, TQ_SIGNAL( focusItemChanged( const TQChar & ) ),
522 this, TQ_SLOT( charFocusItemChanged( const TQChar & ) ) );
523 connect( charTable, TQ_SIGNAL( focusItemChanged() ), this, TQ_SLOT( charFocusItemChanged() ) );
524
525 connect( charTable, TQ_SIGNAL(doubleClicked()),this,TQ_SLOT(slotDoubleClicked()));
526
527 setFocusPolicy( TQWidget::StrongFocus );
528 setFocusProxy( charTable );
529}
530
531KCharSelect::~KCharSelect()
532{
533 delete d;
534}
535
536//==================================================================
537TQSize KCharSelect::sizeHint() const
538{
539 return TQVBox::sizeHint();
540}
541
542//==================================================================
543void KCharSelect::setFont( const TQString &_font )
544{
545 const TQValueList<TQString>::Iterator it = fontList.find( _font );
546 if ( it != fontList.end() ) {
547 TQValueList<TQString>::Iterator it2 = fontList.begin();
548 int pos = 0;
549 for ( ; it != it2; ++it2, ++pos);
550 fontCombo->setCurrentItem( pos );
551 charTable->setFont( _font );
552 }
553 else
554 kdWarning() << "Can't find Font: " << _font << endl;
555}
556
557//==================================================================
558void KCharSelect::setChar( const TQChar &_chr )
559{
560 charTable->setChar( _chr );
561 slotUpdateUnicode( _chr );
562}
563
564//==================================================================
565void KCharSelect::setTableNum( int _tableNum )
566{
567 tableSpinBox->setValue( _tableNum );
568 d->scrollBar->setValue( _tableNum );
569 charTable->setTableNum( _tableNum );
570}
571
572//==================================================================
573void KCharSelect::fillFontCombo()
574{
575 if ( !fontDataBase ) {
576 fontDataBase = new TQFontDatabase();
577 tqAddPostRoutine( cleanupFontDatabase );
578 }
579 fontList=fontDataBase->families();
580 fontCombo->insertStringList( fontList );
581}
582
583//==================================================================
584void KCharSelect::fontSelected( const TQString &_font )
585{
586 charTable->setFont( _font );
587 emit fontChanged( _font );
588}
589
590//==================================================================
591void KCharSelect::tableChanged( int _value )
592{
593 setTableNum( _value );
594}
595
596//==================================================================
597void KCharSelect::slotUnicodeEntered( )
598{
599 const TQString s = d->unicodeLine->text();
600 if (s.isEmpty())
601 return;
602
603 bool ok;
604 const int uc = s.toInt(&ok, 16);
605 if (!ok)
606 return;
607
608 const int table = uc / 256;
609 charTable->setTableNum( table );
610 tableSpinBox->setValue(table);
611 d->scrollBar->setValue(table);
612 const TQChar ch(uc);
613 charTable->setChar( ch );
614 charActivated( ch );
615}
616
617//==================================================================
618void KCharSelect::slotUpdateUnicode( const TQChar &c )
619{
620 const int uc = c.unicode();
621 TQString s;
622 s.sprintf("%04X", uc);
623 d->unicodeLine->setText(s);
624}
625
626//==================================================================
627bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
628{
629 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
630 // just pass charTable's mouse wheel events to the scrollBar
631 return TQApplication::sendEvent( d->scrollBar, e );
632 }
633 return false;
634}
635
636//==================================================================
637void KCharSelectTable::virtual_hook( int, void*)
638{ /*BASE::virtual_hook( id, data );*/ }
639
640void KCharSelect::virtual_hook( int, void* )
641{ /*BASE::virtual_hook( id, data );*/ }
642
KCharSelectTable
Character selection table.
Definition kcharselect.h:50
KCharSelect::KCharSelect
KCharSelect(TQWidget *parent, const char *name, const TQString &font=TQString::null, const TQChar &chr=' ', int tableNum=0)
Constructor.
Definition kcharselect.cpp:452
KCharSelect::setChar
virtual void setChar(const TQChar &chr)
Sets the currently selected character to chr.
Definition kcharselect.cpp:558
KCharSelect::setFont
virtual void setFont(const TQString &font)
Sets the font which is displayed to font.
Definition kcharselect.cpp:543
KCharSelect::sizeHint
virtual TQSize sizeHint() const
Reimplemented.
Definition kcharselect.cpp:537
KCharSelect::setTableNum
virtual void setTableNum(int tableNum)
Sets the currently displayed table to tableNum.
Definition kcharselect.cpp:565
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition kdialog.cpp:110
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition klineedit.h:146
kdWarning
kdbgstream kdWarning(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
TDEStdAccel::name
TQString name(StdAccel id)
tdelocale.h

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.15.0
This website is maintained by Timothy Pearson.