• 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 ), vFont( _font ), vChr( _chr ),
129 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ),
130 d(new KCharSelectTable::KCharSelectTablePrivate)
131{
132 setBackgroundColor( colorGroup().base() );
133
134 setCellWidth( 20 );
135 setCellHeight( 25 );
136
137 setNumCols( 32 );
138 setNumRows( 8 );
139
140 repaintContents( false );
141
142 d->t = new KCharSelectTableToolTip(this);
143
144 setFocusPolicy( TQWidget::StrongFocus );
145 setBackgroundMode( TQWidget::NoBackground );
146}
147
148KCharSelectTable::~KCharSelectTable () {
149 delete d->t;
150 delete d;
151}
152
153//==================================================================
154void KCharSelectTable::setFont( const TQString &_font )
155{
156 if(_font == vFont)
157 return;
158 vFont = _font;
159 repaintContents( false );
160}
161
162//==================================================================
163void KCharSelectTable::setChar( const TQChar &_chr )
164{
165 if(_chr == vChr)
166 return;
167 vChr = _chr;
168 repaintContents( false );
169}
170
171//==================================================================
172void KCharSelectTable::setTableNum( int _tableNum )
173{
174 if(_tableNum == vTableNum)
175 return;
176 focusItem = TQChar( _tableNum * 256 );
177
178 vTableNum = _tableNum;
179 repaintContents( false );
180
181 emit tableNumChanged(vTableNum);
182}
183
184//==================================================================
185TQSize KCharSelectTable::sizeHint() const
186{
187 int w = cellWidth();
188 int h = cellHeight();
189
190 w *= numCols();
191 h *= numRows();
192
193 return TQSize( w, h );
194}
195
196//==================================================================
197// BCI: remove me
198void KCharSelectTable::resizeEvent( TQResizeEvent *e )
199{
200 TQGridView::resizeEvent(e);
201}
202
203//==================================================================
204void KCharSelectTable::viewportResizeEvent( TQResizeEvent * e )
205{
206 const int new_w = e->size().width() / numCols();
207 const int new_h = e->size().height() / numRows();
208
209 if( new_w != cellWidth())
210 setCellWidth( new_w );
211 if( new_h != cellHeight())
212 setCellHeight( new_h );
213
214 TQGridView::viewportResizeEvent(e);
215}
216
217//==================================================================
218void KCharSelectTable::paintCell( class TQPainter* p, int row, int col )
219{
220 const int w = cellWidth();
221 const int h = cellHeight();
222 const int x2 = w - 1;
223 const int y2 = h - 1;
224
225 //if( row == 0 && col == 0 ) {
226 // printf("Repaint %d\n", temp++);
227 // fflush( stdout );
228 // }
229
230 TQFont font = TQFont( vFont );
231 font.setPixelSize( int(.7 * h) );
232
233 unsigned short c = vTableNum * 256;
234 c += row * numCols();
235 c += col;
236
237 if ( c == vChr.unicode() ) {
238 p->setBrush( TQBrush( colorGroup().highlight() ) );
239 p->setPen( NoPen );
240 p->drawRect( 0, 0, w, h );
241 p->setPen( colorGroup().highlightedText() );
242 vPos = TQPoint( col, row );
243 } else {
244 TQFontMetrics fm = TQFontMetrics( font );
245 if( fm.inFont( c ) )
246 p->setBrush( TQBrush( colorGroup().base() ) );
247 else
248 p->setBrush( TQBrush( colorGroup().button() ) );
249 p->setPen( NoPen );
250 p->drawRect( 0, 0, w, h );
251 p->setPen( colorGroup().text() );
252 }
253
254 if ( c == focusItem.unicode() && hasFocus() ) {
255 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
256 colorGroup() );
257 focusPos = TQPoint( col, row );
258 }
259
260 p->setFont( font );
261
262 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
263
264 p->setPen( colorGroup().text() );
265 p->drawLine( x2, 0, x2, y2 );
266 p->drawLine( 0, y2, x2, y2 );
267
268 if ( row == 0 )
269 p->drawLine( 0, 0, x2, 0 );
270 if ( col == 0 )
271 p->drawLine( 0, 0, 0, y2 );
272}
273
274//==================================================================
275// BCI: remove me
276void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
277{
278 TQGridView::mouseMoveEvent(e);
279}
280
281//==================================================================
282void KCharSelectTable::contentsMousePressEvent( TQMouseEvent *e )
283{
284 contentsMouseMoveEvent(e);
285}
286
287//==================================================================
288void KCharSelectTable::contentsMouseDoubleClickEvent ( TQMouseEvent *e )
289{
290 contentsMouseMoveEvent(e);
291 emit doubleClicked();
292}
293
294//==================================================================
295void KCharSelectTable::contentsMouseReleaseEvent( TQMouseEvent *e )
296{
297 contentsMouseMoveEvent( e );
298
299 if( e->isAccepted() ) {
300 emit activated( chr() );
301 emit activated();
302 }
303}
304
305//==================================================================
306void KCharSelectTable::contentsMouseMoveEvent( TQMouseEvent *e )
307{
308 const int row = rowAt( e->y() );
309 const int col = columnAt( e->x() );
310 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
311 const TQPoint oldPos = vPos;
312
313 vPos.setX( col );
314 vPos.setY( row );
315
316 vChr = charAt( vPos.y(), vPos.x() );
317
318 const TQPoint oldFocus = focusPos;
319
320 focusPos = vPos;
321 focusItem = vChr;
322
323 repaintCell( oldFocus.y(), oldFocus.x(), true );
324 repaintCell( oldPos.y(), oldPos.x(), true );
325 repaintCell( vPos.y(), vPos.x(), true );
326
327 emit highlighted( vChr );
328 emit highlighted();
329
330 emit focusItemChanged( focusItem );
331 emit focusItemChanged();
332
333 e->accept();
334 } else {
335 e->ignore();
336 }
337}
338
339//==================================================================
340void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
341{
342 switch ( e->key() ) {
343 case Key_Left:
344 gotoLeft();
345 break;
346 case Key_Right:
347 gotoRight();
348 break;
349 case Key_Up:
350 gotoUp();
351 break;
352 case Key_Down:
353 gotoDown();
354 break;
355 case Key_Next:
356 if ( tableNum() < 255 ) {
357 setTableNum( tableNum() + 1 );
358 }
359 break;
360 case Key_Prior:
361 if ( tableNum() > 0 ) {
362 setTableNum( tableNum() - 1 );
363 }
364 break;
365 case Key_Space:
366 emit activated( ' ' );
367 emit activated();
368 emit highlighted( ' ' );
369 emit highlighted();
370 break;
371 case Key_Enter: case Key_Return: {
372 const TQPoint oldPos = vPos;
373
374 vPos = focusPos;
375 vChr = focusItem;
376
377 repaintCell( oldPos.y(), oldPos.x(), true );
378 repaintCell( vPos.y(), vPos.x(), true );
379
380 emit activated( vChr );
381 emit activated();
382 emit highlighted( vChr );
383 emit highlighted();
384 } break;
385 }
386}
387
388//==================================================================
389void KCharSelectTable::gotoLeft()
390{
391 if ( focusPos.x() > 0 ) {
392 doGoto(-1, 0);
393 }
394}
395
396//==================================================================
397void KCharSelectTable::gotoRight()
398{
399 if ( focusPos.x() < numCols()-1 ) {
400 doGoto(1, 0);
401 }
402}
403
404//==================================================================
405void KCharSelectTable::gotoUp()
406{
407 if ( focusPos.y() > 0 ) {
408 doGoto(0, -1);
409 } else if ( tableNum() > 0 ) {
410 emit tableNumChanged(--vTableNum);
411 doGoto(0, numRows()-1);
412 repaintContents( false );
413 }
414}
415
416//==================================================================
417void KCharSelectTable::gotoDown()
418{
419 if ( focusPos.y() < numRows()-1 ) {
420 doGoto(0, +1);
421 } else if ( tableNum() < 255 ) {
422 emit tableNumChanged(++vTableNum);
423 doGoto(0, -(numRows()-1));
424 repaintContents( false );
425 }
426}
427
428//==================================================================
429void KCharSelectTable::doGoto(int dx, int dy)
430{
431 TQPoint oldPos = focusPos;
432 focusPos += TQPoint(dx, dy);
433 focusItem = charAt( focusPos.y(), focusPos.x() );
434
435 repaintCell( oldPos.y(), oldPos.x(), true );
436 repaintCell( focusPos.y(), focusPos.x(), true );
437
438 emit focusItemChanged( vChr );
439 emit focusItemChanged();
440}
441
442//==================================================================
443TQChar KCharSelectTable::charAt(int row, int col) const
444{
445 return TQChar( vTableNum * 256 + numCols() * row + col );
446}
447
448/******************************************************************/
449/* Class: KCharSelect */
450/******************************************************************/
451
452//==================================================================
453KCharSelect::KCharSelect( TQWidget *parent, const char *name, const TQString &_font, const TQChar &_chr, int _tableNum )
454 : TQVBox( parent, name ), d(new KCharSelectPrivate)
455{
456 setSpacing( KDialog::spacingHint() );
457 TQHBox* const bar = new TQHBox( this );
458 bar->setSpacing( KDialog::spacingHint() );
459
460 TQLabel* const lFont = new TQLabel( i18n( "Font:" ), bar );
461 lFont->resize( lFont->sizeHint() );
462 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
463 lFont->setMaximumWidth( lFont->sizeHint().width() );
464
465 fontCombo = new TQComboBox( true, bar );
466 fillFontCombo();
467 fontCombo->resize( fontCombo->sizeHint() );
468
469 connect( fontCombo, TQ_SIGNAL( activated( const TQString & ) ), this, TQ_SLOT( fontSelected( const TQString & ) ) );
470
471 TQLabel* const lTable = new TQLabel( i18n( "Table:" ), bar );
472 lTable->resize( lTable->sizeHint() );
473 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
474 lTable->setMaximumWidth( lTable->sizeHint().width() );
475
476 tableSpinBox = new TQSpinBox( 0, 255, 1, bar );
477 tableSpinBox->resize( tableSpinBox->sizeHint() );
478
479 TQLabel* const lUnicode = new TQLabel( i18n( "&Unicode code point:" ), bar );
480 lUnicode->resize( lUnicode->sizeHint() );
481 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
482 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
483
484 const TQRegExp rx( "[a-fA-F0-9]{1,4}" );
485 TQValidator* const validator = new TQRegExpValidator( rx, this );
486
487 d->unicodeLine = new KLineEdit( bar );
488 d->unicodeLine->setValidator(validator);
489 lUnicode->setBuddy(d->unicodeLine);
490 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
491 slotUpdateUnicode(_chr);
492
493 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ), this, TQ_SLOT( slotUnicodeEntered() ) );
494
495 TQHBox* const box = new TQHBox(this);
496 box->setSpacing(0);
497
498 charTable = new KCharSelectTable( box, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
499 const TQSize sz( charTable->contentsWidth() + 4 ,
500 charTable->contentsHeight() + 4 );
501 charTable->resize( sz );
502 //charTable->setMaximumSize( sz );
503 charTable->setMinimumSize( sz );
504 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
505 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
506 charTable->installEventFilter( this );
507
508 d->scrollBar = new TQScrollBar(0, 255, 1, 1, 0, TQt::Vertical, box);
509
510 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
511 setTableNum( _tableNum );
512
513 connect( tableSpinBox, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
514 connect( d->scrollBar, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
515 connect( charTable, TQ_SIGNAL( tableNumChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
516
517 connect( charTable, TQ_SIGNAL( highlighted( const TQChar & ) ), this, TQ_SLOT( slotUpdateUnicode( const TQChar & ) ) );
518 connect( charTable, TQ_SIGNAL( highlighted( const TQChar & ) ), this, TQ_SLOT( charHighlighted( const TQChar & ) ) );
519 connect( charTable, TQ_SIGNAL( highlighted() ), this, TQ_SLOT( charHighlighted() ) );
520 connect( charTable, TQ_SIGNAL( activated( const TQChar & ) ), this, TQ_SLOT( charActivated( const TQChar & ) ) );
521 connect( charTable, TQ_SIGNAL( activated() ), this, TQ_SLOT( charActivated() ) );
522 connect( charTable, TQ_SIGNAL( focusItemChanged( const TQChar & ) ),
523 this, TQ_SLOT( charFocusItemChanged( const TQChar & ) ) );
524 connect( charTable, TQ_SIGNAL( focusItemChanged() ), this, TQ_SLOT( charFocusItemChanged() ) );
525
526 connect( charTable, TQ_SIGNAL(doubleClicked()),this,TQ_SLOT(slotDoubleClicked()));
527
528 setFocusPolicy( TQWidget::StrongFocus );
529 setFocusProxy( charTable );
530}
531
532KCharSelect::~KCharSelect()
533{
534 delete d;
535}
536
537//==================================================================
538TQSize KCharSelect::sizeHint() const
539{
540 return TQVBox::sizeHint();
541}
542
543//==================================================================
544void KCharSelect::setFont( const TQString &_font )
545{
546 const TQValueList<TQString>::Iterator it = fontList.find( _font );
547 if ( it != fontList.end() ) {
548 TQValueList<TQString>::Iterator it2 = fontList.begin();
549 int pos = 0;
550 for ( ; it != it2; ++it2, ++pos);
551 fontCombo->setCurrentItem( pos );
552 charTable->setFont( _font );
553 }
554 else
555 kdWarning() << "Can't find Font: " << _font << endl;
556}
557
558//==================================================================
559void KCharSelect::setChar( const TQChar &_chr )
560{
561 charTable->setChar( _chr );
562 slotUpdateUnicode( _chr );
563}
564
565//==================================================================
566void KCharSelect::setTableNum( int _tableNum )
567{
568 tableSpinBox->setValue( _tableNum );
569 d->scrollBar->setValue( _tableNum );
570 charTable->setTableNum( _tableNum );
571}
572
573//==================================================================
574void KCharSelect::fillFontCombo()
575{
576 if ( !fontDataBase ) {
577 fontDataBase = new TQFontDatabase();
578 tqAddPostRoutine( cleanupFontDatabase );
579 }
580 fontList=fontDataBase->families();
581 fontCombo->insertStringList( fontList );
582}
583
584//==================================================================
585void KCharSelect::fontSelected( const TQString &_font )
586{
587 charTable->setFont( _font );
588 emit fontChanged( _font );
589}
590
591//==================================================================
592void KCharSelect::tableChanged( int _value )
593{
594 setTableNum( _value );
595}
596
597//==================================================================
598void KCharSelect::slotUnicodeEntered( )
599{
600 const TQString s = d->unicodeLine->text();
601 if (s.isEmpty())
602 return;
603
604 bool ok;
605 const int uc = s.toInt(&ok, 16);
606 if (!ok)
607 return;
608
609 const int table = uc / 256;
610 charTable->setTableNum( table );
611 tableSpinBox->setValue(table);
612 d->scrollBar->setValue(table);
613 const TQChar ch(uc);
614 charTable->setChar( ch );
615 charActivated( ch );
616}
617
618//==================================================================
619void KCharSelect::slotUpdateUnicode( const TQChar &c )
620{
621 const int uc = c.unicode();
622 TQString s;
623 s.sprintf("%04X", uc);
624 d->unicodeLine->setText(s);
625}
626
627//==================================================================
628bool KCharSelect::eventFilter( TQObject *obj, TQEvent *e )
629{
630 if ( obj == charTable && e->type() == TQEvent::Wheel ) {
631 // just pass charTable's mouse wheel events to the scrollBar
632 return TQApplication::sendEvent( d->scrollBar, e );
633 }
634 return false;
635}
636
637//==================================================================
638void KCharSelectTable::virtual_hook( int, void*)
639{ /*BASE::virtual_hook( id, data );*/ }
640
641void KCharSelect::virtual_hook( int, void* )
642{ /*BASE::virtual_hook( id, data );*/ }
643
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:453
KCharSelect::setChar
virtual void setChar(const TQChar &chr)
Sets the currently selected character to chr.
Definition kcharselect.cpp:559
KCharSelect::setFont
virtual void setFont(const TQString &font)
Sets the font which is displayed to font.
Definition kcharselect.cpp:544
KCharSelect::sizeHint
virtual TQSize sizeHint() const
Reimplemented.
Definition kcharselect.cpp:538
KCharSelect::setTableNum
virtual void setTableNum(int tableNum)
Sets the currently displayed table to tableNum.
Definition kcharselect.cpp:566
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.9.8
This website is maintained by Timothy Pearson.