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

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.8.17
This website is maintained by Timothy Pearson.