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