All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
34 te::qt::widgets::CharMapWidget::CharMapWidget(QWidget* parent, Qt::WindowFlags f)
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 {
46 }
47 
49 {
50  return QSize(m_columns * m_squareSize, (65536 / m_columns) * m_squareSize);
51 }
52 
54 {
55  return m_currentChar;
56 }
57 
59 {
60  m_font.setFamily(font.family());
61  update();
62 }
63 
65 {
66  QPoint widgetPosition = mapFromGlobal(e->globalPos());
67  uint key = (widgetPosition.y() / m_squareSize) * m_columns + widgetPosition.x() / m_squareSize;
68 
69  QString text = QString::fromLatin1("<p>Char: <span style=\"font-size: 28pt; font-family: %1\">").arg(m_font.family())
70  + QChar(key)
71  + QString::fromLatin1("</span><p>Value: 0x")
72  + QString::number(key, 16);
73 
74  QToolTip::showText(e->globalPos(), text, this);
75 }
76 
78 {
79  if(e->button() != Qt::LeftButton)
80  {
81  QWidget::mousePressEvent(e);
82  return;
83  }
84 
85  m_currentChar = (e->y() / m_squareSize) * m_columns + e->x() / m_squareSize;
86 #if (QT_VERSION >= 0x050000)
87  if(QChar(m_currentChar).category() != QChar::Other_NotAssigned)
88 #else
89  if(QChar(m_currentChar).category() != QChar::NoCategory)
90 #endif
91  emit charSelected(m_currentChar);
92 
93  update();
94 }
95 
97 {
98  QPainter painter(this);
99  painter.fillRect(e->rect(), Qt::white);
100  painter.setFont(m_font);
101 
102  QRect redrawRect = e->rect();
103  int beginRow = redrawRect.top() / m_squareSize;
104  int endRow = redrawRect.bottom() / m_squareSize;
105  int beginColumn = redrawRect.left() / m_squareSize;
106  int endColumn = redrawRect.right() / m_squareSize;
107 
108  painter.setPen(Qt::gray);
109  for(int row = beginRow; row <= endRow; ++row)
110  for(int column = beginColumn; column <= endColumn; ++column)
111  painter.drawRect(column * m_squareSize, row * m_squareSize, m_squareSize, m_squareSize);
112 
113  painter.setPen(Qt::black);
114  QFontMetrics fontMetrics(m_font);
115  for(int row = beginRow; row <= endRow; ++row)
116  {
117  for(int column = beginColumn; column <= endColumn; ++column)
118  {
119  int key = row * m_columns + column;
120  painter.setClipRect(column * m_squareSize, row * m_squareSize, m_squareSize, m_squareSize);
121 
122  if(key == m_currentChar)
123  painter.fillRect(column * m_squareSize + 1, row * m_squareSize + 1, m_squareSize, m_squareSize, Qt::lightGray);
124 
125  painter.drawText(column * m_squareSize + (m_squareSize / 2) - fontMetrics.width(QChar(key))/2,
126  row * m_squareSize + 4 + fontMetrics.ascent(),
127  QString(QChar(key)));
128  }
129  }
130 }
QFont m_font
Current font.
Definition: CharMapWidget.h:89
void paintEvent(QPaintEvent *e)
A widget used to show the set of characters of a specified font.
void mouseMoveEvent(QMouseEvent *e)
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)