CharMapWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/se/CharMapWidget.cpp
22 
23  \brief A widget used to show the set of characters of a specified font.
24 */
25 
26 // TerraLib
27 #include "CharMapWidget.h"
28 
29 // Qt
30 #include <QMouseEvent>
31 #include <QPainter>
32 #include <QToolTip>
33 
35  : QWidget(parent, f),
36  m_currentChar(0),
37  m_columns(11),
38  m_squareSize(24)
39 {
40  m_font.setPointSize(12);
41  setMouseTracking(true);
42 }
43 
45 
47 {
48  return QSize(m_columns * m_squareSize, (65536 / m_columns) * m_squareSize);
49 }
50 
52 {
53  return m_currentChar;
54 }
55 
57 {
58  m_font.setFamily(font.family());
59  update();
60 }
61 
63 {
64  QPoint widgetPosition = mapFromGlobal(e->globalPos());
65  uint key = (widgetPosition.y() / m_squareSize) * m_columns + widgetPosition.x() / m_squareSize;
66 
67  QString text = QString::fromUtf8("<p>Char: <span style=\"font-size: 28pt; font-family: %1\">").arg(m_font.family())
68  + QChar(key)
69  + QString::fromUtf8("</span><p>Value: 0x")
70  + QString::number(key, 16);
71 
72  QToolTip::showText(e->globalPos(), text, this);
73 }
74 
76 {
77  if(e->button() != Qt::LeftButton)
78  {
79  QWidget::mousePressEvent(e);
80  return;
81  }
82 
83  m_currentChar = (e->y() / m_squareSize) * m_columns + e->x() / m_squareSize;
84 #if (QT_VERSION >= 0x050000)
85  if(QChar(m_currentChar).category() != QChar::Other_NotAssigned)
86 #else
87  if(QChar(m_currentChar).category() != QChar::NoCategory)
88 #endif
90 
91  update();
92 }
93 
95 {
96  QPainter painter(this);
97  painter.fillRect(e->rect(), Qt::white);
98  painter.setFont(m_font);
99 
100  QRect redrawRect = e->rect();
101  int beginRow = redrawRect.top() / m_squareSize;
102  int endRow = redrawRect.bottom() / m_squareSize;
103  int beginColumn = redrawRect.left() / m_squareSize;
104  int endColumn = redrawRect.right() / m_squareSize;
105 
106  painter.setPen(Qt::gray);
107  for(int row = beginRow; row <= endRow; ++row)
108  for(int column = beginColumn; column <= endColumn; ++column)
109  painter.drawRect(column * m_squareSize, row * m_squareSize, m_squareSize, m_squareSize);
110 
111  painter.setPen(Qt::black);
112  QFontMetrics fontMetrics(m_font);
113  for(int row = beginRow; row <= endRow; ++row)
114  {
115  for(int column = beginColumn; column <= endColumn; ++column)
116  {
117  int key = row * m_columns + column;
118  painter.setClipRect(column * m_squareSize, row * m_squareSize, m_squareSize, m_squareSize);
119 
120  if(key == static_cast<int>(m_currentChar))
121  painter.fillRect(column * m_squareSize + 1, row * m_squareSize + 1, m_squareSize, m_squareSize, Qt::lightGray);
122 
123  painter.drawText(column * m_squareSize + (m_squareSize / 2) - fontMetrics.width(QChar(key))/2,
124  row * m_squareSize + 4 + fontMetrics.ascent(),
125  QString(QChar(key)));
126  }
127  }
128 }
void charSelected(const unsigned int &charCode)
QFont m_font
Current font.
Definition: CharMapWidget.h:89
void paintEvent(QPaintEvent *e)
int m_squareSize
Cell size of character map.
Definition: CharMapWidget.h:92
A widget used to show the set of characters of a specified font.
void mouseMoveEvent(QMouseEvent *e)
int m_columns
Character map number of column.
Definition: CharMapWidget.h:91
void updateFont(const QFont &font)
CharMapWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs the character map widget which is a child of parent, with widget flags set to f...
void mousePressEvent(QMouseEvent *e)
unsigned int m_currentChar
Current selected char.
Definition: CharMapWidget.h:90