QGIS API Documentation  master-59fd5e0
src/gui/symbology-ng/characterwidget.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This library/program is free software; you can redistribute it
00008 ** and/or modify it under the terms of the GNU Library General Public
00009 ** License as published by the Free Software Foundation; either
00010 ** version 2 of the License, or ( at your option ) any later version.
00011 **
00012 ** This file is part of the examples of the Qt Toolkit.
00013 **
00014 ** $QT_BEGIN_LICENSE:LGPL$
00015 ** Commercial Usage
00016 ** Licensees holding valid Qt Commercial licenses may use this file in
00017 ** accordance with the Qt Commercial License Agreement provided with the
00018 ** Software or, alternatively, in accordance with the terms contained in
00019 ** a written agreement between you and Nokia.
00020 **
00021 ** GNU Lesser General Public License Usage
00022 ** Alternatively, this file may be used under the terms of the
00023 ** GNU Lesser General Public License version 2.1 as published by the Free Software
00024 ** Foundation and appearing in the file LICENSE.LGPL included in the
00025 ** packaging of this file.  Please review the following information to
00026 ** ensure the GNU Lesser General Public License version 2.1 requirements
00027 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00028 ** In addition, as a special exception, Nokia gives you certain additional
00029 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00030 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
00031 **
00032 ** GNU General Public License Usage
00033 ** Alternatively, this file may be used under the terms of the GNU
00034 ** General Public License version 3.0 as published by the Free Software
00035 ** Foundation and appearing in the file LICENSE.GPL included in the
00036 ** packaging of this file.  Please review the following information to
00037 ** ensure the GNU General Public License version 3.0 requirements will be
00038 ** met: http://www.gnu.org/copyleft/gpl.html.
00039 **
00040 ** If you have questions regarding the use of this file, please contact
00041 ** Nokia at qt-info@nokia.com.
00042 ** $QT_END_LICENSE$
00043 **
00044 ****************************************************************************/
00045 
00046 #include <QtGui>
00047 
00048 #include "characterwidget.h"
00049 
00051 CharacterWidget::CharacterWidget( QWidget *parent )
00052     : QWidget( parent )
00053 {
00054   squareSize = 24;
00055   columns = 16;
00056   lastKey = -1;
00057   setMouseTracking( true );
00058 }
00060 
00062 void CharacterWidget::updateFont( const QFont &font )
00063 {
00064   displayFont.setFamily( font.family() );
00065   squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
00066   adjustSize();
00067   update();
00068 }
00070 
00072 void CharacterWidget::updateSize( double fontSize )
00073 {
00074   displayFont.setPointSizeF( fontSize );
00075   squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
00076   adjustSize();
00077   update();
00078 }
00080 
00081 void CharacterWidget::updateStyle( const QString &fontStyle )
00082 {
00083   QFontDatabase fontDatabase;
00084   const QFont::StyleStrategy oldStrategy = displayFont.styleStrategy();
00085   displayFont = fontDatabase.font( displayFont.family(), fontStyle, displayFont.pointSize() );
00086   displayFont.setStyleStrategy( oldStrategy );
00087   squareSize = qMax( 24, QFontMetrics( displayFont ).xHeight() * 3 );
00088   adjustSize();
00089   update();
00090 }
00091 
00092 void CharacterWidget::updateFontMerging( bool enable )
00093 {
00094   if ( enable )
00095     displayFont.setStyleStrategy( QFont::PreferDefault );
00096   else
00097     displayFont.setStyleStrategy( QFont::NoFontMerging );
00098   adjustSize();
00099   update();
00100 }
00101 
00102 void CharacterWidget::updateColumns( int cols )
00103 {
00104   if ( columns == cols || cols < 1 )
00105     return;
00106   columns = cols;
00107   adjustSize();
00108   update();
00109 }
00110 
00112 QSize CharacterWidget::sizeHint() const
00113 {
00114   return QSize( columns*squareSize, ( 65536 / columns )*squareSize );
00115 }
00117 
00119 void CharacterWidget::mouseMoveEvent( QMouseEvent *event )
00120 {
00121   QPoint widgetPosition = mapFromGlobal( event->globalPos() );
00122   uint key = ( widgetPosition.y() / squareSize ) * columns + widgetPosition.x() / squareSize;
00123 
00124   QString text = tr( "<p>Character: <span style=\"font-size: 24pt; font-family: %1\">%2</span><p>Value: 0x%3" )
00125                  .arg( displayFont.family() )
00126                  .arg( QChar( key ) )
00127                  .arg( key, 16 );
00128   QToolTip::showText( event->globalPos(), text, this );
00129 }
00131 
00133 void CharacterWidget::mousePressEvent( QMouseEvent *event )
00134 {
00135   if ( event->button() == Qt::LeftButton )
00136   {
00137     lastKey = ( event->y() / squareSize ) * columns + event->x() / squareSize;
00138     if ( QChar( lastKey ).category() != QChar::NoCategory )
00139       emit characterSelected( QChar( lastKey ) );
00140     update();
00141   }
00142   else
00143     QWidget::mousePressEvent( event );
00144 }
00146 
00148 void CharacterWidget::paintEvent( QPaintEvent *event )
00149 {
00150   QPainter painter( this );
00151   painter.fillRect( event->rect(), QBrush( Qt::white ) );
00152   painter.setFont( displayFont );
00154 
00156   QRect redrawRect = event->rect();
00157   int beginRow = redrawRect.top() / squareSize;
00158   int endRow = redrawRect.bottom() / squareSize;
00159   int beginColumn = redrawRect.left() / squareSize;
00160   int endColumn = redrawRect.right() / squareSize;
00162 
00164   painter.setPen( QPen( Qt::gray ) );
00165   for ( int row = beginRow; row <= endRow; ++row )
00166   {
00167     for ( int column = beginColumn; column <= endColumn; ++column )
00168     {
00169       painter.drawRect( column*squareSize, row*squareSize, squareSize, squareSize );
00170     }
00172   }
00174 
00176   QFontMetrics fontMetrics( displayFont );
00177   painter.setPen( QPen( Qt::black ) );
00178   for ( int row = beginRow; row <= endRow; ++row )
00179   {
00180 
00181     for ( int column = beginColumn; column <= endColumn; ++column )
00182     {
00183 
00184       int key = row * columns + column;
00185       painter.setClipRect( column*squareSize, row*squareSize, squareSize, squareSize );
00186 
00187       if ( key == lastKey )
00188         painter.fillRect( column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush( Qt::red ) );
00189 
00190       painter.drawText( column*squareSize + ( squareSize / 2 ) - fontMetrics.width( QChar( key ) ) / 2,
00191                         row*squareSize + 4 + fontMetrics.ascent(),
00192                         QString( QChar( key ) ) );
00193     }
00194   }
00195 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines