QtBase  v6.3.1
main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QtWidgets>
30 
31 #ifdef Q_OS_DARWIN
32 #include <private/qcoregraphics_p.h>
33 #include <private/qcore_mac_p.h>
34 #include <Foundation/Foundation.h>
35 #include <private/qfont_p.h>
36 #include <private/qfontengine_p.h>
37 #endif
38 
39 static int s_mode;
40 static QString s_text = QString::fromUtf8("The quick brown \xF0\x9F\xA6\x8A jumps over the lazy \xF0\x9F\x90\xB6");
41 
42 class TextRenderer : public QWidget
43 {
44  Q_OBJECT
45 public:
48 
49  TextRenderer(qreal pointSize, const QString &text, const QColor &textColor = QColor(), const QColor &bgColor = QColor())
50  : m_text(text)
51  {
52  if (pointSize) {
53  QFont f = font();
54  f.setPointSize(pointSize);
55  setFont(f);
56  }
57 
58  if (textColor.isValid()) {
59  QPalette p = palette();
60  p.setColor(QPalette::Text, textColor);
61  setPalette(p);
62  }
63 
64  if (bgColor.isValid()) {
65  QPalette p = palette();
66  p.setColor(QPalette::Window, bgColor);
67  setPalette(p);
68  }
69  }
70 
71  QString text() const
72  {
73  return !m_text.isNull() ? m_text : s_text;
74  }
75 
76  QSize sizeHint() const override
77  {
79  return QSize(fm.boundingRect(text()).width(), fm.height());
80  }
81 
82  bool event(QEvent * event) override
83  {
84  if (event->type() == QEvent::ToolTip) {
85  QString toolTip;
86  QDebug debug(&toolTip);
87  debug << "textColor =" << palette().color(QPalette::Text) << "bgColor =" << palette().color(QPalette::Window);
88  setToolTip(toolTip);
89  }
90 
91  return QWidget::event(event);
92  }
93 
94  void paintEvent(QPaintEvent *) override
95  {
97  image.setDevicePixelRatio(devicePixelRatio());
98 
99  QPainter p(&image);
100  p.fillRect(QRect(0, 0, image.width(), image.height()), palette().window().color());
101 
102  const int ascent = fontMetrics().ascent();
103 
104  QPen metricsPen(QColor(112, 216, 255), 1.0);
105  metricsPen.setCosmetic(true);
106  p.setPen(metricsPen);
107  p.drawLine(QPoint(0, ascent), QPoint(width(), ascent));
108  p.end();
109 
110  if (s_mode == QtRendering)
112  else
114 
115  QPainter wp(this);
116  wp.drawImage(QPoint(0, 0), image);
117  }
118 
120  {
121  QPainter p(&image);
122 
123  const int ascent = fontMetrics().ascent();
124 
125  p.setPen(palette().text().color());
126 
127  QFont f = font();
128  f.setResolveMask(-1);
129  p.setFont(f);
130 
131  p.drawText(QPoint(0, ascent), text());
132  }
133 
135  {
136 #ifdef Q_OS_DARWIN
137  QMacAutoReleasePool pool;
138  QMacCGContext ctx(&image);
139 
140  const auto *fontEngine = QFontPrivate::get(font())->engineForScript(QChar::Script_Common);
141  Q_ASSERT(fontEngine);
142  if (fontEngine->type() == QFontEngine::Multi) {
143  fontEngine = static_cast<const QFontEngineMulti *>(fontEngine)->engine(0);
144  Q_ASSERT(fontEngine);
145  }
146  Q_ASSERT(fontEngine->type() == QFontEngine::Mac);
147 
148  QColor textColor = palette().text().color();
149  auto nsColor = [NSColor colorWithSRGBRed:textColor.redF()
150  green:textColor.greenF()
151  blue:textColor.blueF()
152  alpha:textColor.alphaF()];
153 
154  if (font().styleStrategy() & QFont::NoAntialias)
155  CGContextSetShouldAntialias(ctx, false);
156 
157  // Flip to what CT expects
158  CGContextScaleCTM(ctx, 1, -1);
159  CGContextTranslateCTM(ctx, 0, -height());
160 
161  // Set up baseline
162  CGContextSetTextPosition(ctx, 0, height() - fontMetrics().ascent());
163 
164  auto *attributedString = [[NSAttributedString alloc] initWithString:text().toNSString()
165  attributes:@{
166  NSFontAttributeName : (NSFont *)fontEngine->handle(),
167  NSForegroundColorAttributeName : nsColor
168  }
169  ];
170 
171  QCFType<CTLineRef> line = CTLineCreateWithAttributedString(CFAttributedStringRef([attributedString autorelease]));
172  CTLineDraw(line, ctx);
173 #endif
174  }
175 
176 public:
177 
180 };
181 
182 class TestWidget : public QWidget
183 {
184  Q_OBJECT
185 public:
187  {
188  auto *mainLayout = new QVBoxLayout;
189 
190  m_previews = new QWidget;
192 
193  for (int i = 0; i < 6; ++i) {
194  auto *layout = new QVBoxLayout;
195  QString text;
196  if (i > 0)
197  text = "ABC";
198 
200  switch (i) {
201  case 0: return qMakePair(QColor(), QColor());
202  case 1: return qMakePair(QColor(Qt::black), QColor(Qt::white));
203  case 2: return qMakePair(QColor(Qt::white), QColor(Qt::black));
204  case 3: return qMakePair(QColor(Qt::magenta), QColor(Qt::green));
205  case 4: return qMakePair(QColor(0, 0, 0, 128), QColor(Qt::white));
206  case 5: return qMakePair(QColor(255, 255, 255, 128), QColor(Qt::black));
207  default: return qMakePair(QColor(), QColor());
208  }
209  }();
210 
211  for (int pointSize : {8, 12, 24, 36, 48})
212  layout->addWidget(new TextRenderer(pointSize, text, color.first, color.second));
213 
214  static_cast<QHBoxLayout*>(m_previews->layout())->addLayout(layout);
215  }
216 
217  mainLayout->addWidget(m_previews);
218 
219  auto *controls = new QHBoxLayout;
220  auto *lineEdit = new QLineEdit(s_text);
222  s_text = text;
223  for (TextRenderer *renderer : m_previews->findChildren<TextRenderer *>())
224  renderer->updateGeometry();
225  });
226  controls->addWidget(lineEdit);
227 
228  auto *colorButton = new QPushButton("Color...");
229  connect(colorButton, &QPushButton::clicked, [&] {
230  auto *colorDialog = new QColorDialog(this);
231  colorDialog->setOptions(QColorDialog::NoButtons | QColorDialog::ShowAlphaChannel);
232  colorDialog->setModal(false);
233  connect(colorDialog, &QColorDialog::currentColorChanged, [&](const QColor &color) {
234  QPalette p = palette();
235  p.setColor(QPalette::Text, color);
236  setPalette(p);
237  });
238  colorDialog->setCurrentColor(palette().text().color());
239  colorDialog->setVisible(true);
240  });
241  controls->addWidget(colorButton);
242  auto *fontButton = new QPushButton("Font...");
243  connect(fontButton, &QPushButton::clicked, [&] {
244  auto *fontDialog = new QFontDialog(this);
245  fontDialog->setOptions(QFontDialog::NoButtons);
246  fontDialog->setModal(false);
247  fontDialog->setCurrentFont(m_previews->font());
248  connect(fontDialog, &QFontDialog::currentFontChanged, [&](const QFont &font) {
250  });
251  fontDialog->setVisible(true);
252  });
253  controls->addWidget(fontButton);
254 
255  auto *aaButton = new QCheckBox("NoAntialias");
256  connect(aaButton, &QCheckBox::stateChanged, [&] {
257  for (TextRenderer *renderer : m_previews->findChildren<TextRenderer *>()) {
258  QFont font = renderer->font();
259  font.setStyleStrategy(QFont::StyleStrategy(font.styleStrategy() ^ QFont::NoAntialias));
260  renderer->setFont(font);
261  }
262  });
263  controls->addWidget(aaButton);
264 
265  auto *subpixelAAButton = new QCheckBox("NoSubpixelAntialias");
266  connect(subpixelAAButton, &QCheckBox::stateChanged, [&] {
267  for (TextRenderer *renderer : m_previews->findChildren<TextRenderer *>()) {
268  QFont font = renderer->font();
269  font.setStyleStrategy(QFont::StyleStrategy(font.styleStrategy() ^ QFont::NoSubpixelAntialias));
270  renderer->setFont(font);
271  }
272  });
273  controls->addWidget(subpixelAAButton);
274  controls->addStretch();
275 
276  mainLayout->addLayout(controls);
277 
278  mainLayout->setSizeConstraint(QLayout::SetFixedSize);
279  setLayout(mainLayout);
280 
283  setFocus();
284  }
285 
287  {
288  s_mode = mode;
289  setWindowTitle(s_mode == TextRenderer::QtRendering ? "Qt" : "Native");
290 
291  for (TextRenderer *renderer : m_previews->findChildren<TextRenderer *>())
292  renderer->update();
293  }
294 
295  void mousePressEvent(QMouseEvent *) override
296  {
298  }
299 
300  void keyPressEvent(QKeyEvent *e) override
301  {
302  if (e->key() == Qt::Key_Space)
304  }
305 
307 };
308 
309 int main(int argc, char **argv)
310 {
311  qputenv("QT_MAX_CACHED_GLYPH_SIZE", "97");
312  QApplication app(argc, argv);
313 
315  widget.show();
316  return app.exec();
317 }
318 
319 #include "main.moc"
320 
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
void clicked(bool checked=false)
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
const QColor & color() const
Definition: qbrush.h:157
@ Script_Common
Definition: qchar.h:183
The QCheckBox widget provides a checkbox with a text label.
Definition: qcheckbox.h:55
void stateChanged(int)
The QColorDialog class provides a dialog widget for specifying colors.
Definition: qcolordialog.h:54
void currentColorChanged(const QColor &color)
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
float greenF() const noexcept
Definition: qcolor.cpp:1640
float redF() const noexcept
Definition: qcolor.cpp:1608
float alphaF() const noexcept
Definition: qcolor.cpp:1494
float blueF() const noexcept
Definition: qcolor.cpp:1672
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Definition: qcoreevent.h:58
@ ToolTip
Definition: qcoreevent.h:160
The QFontDialog class provides a dialog widget for selecting a font.
Definition: qfontdialog.h:56
void currentFontChanged(const QFont &font)
The QFont class specifies a query for a font used for drawing text.
Definition: qfont.h:56
@ NoAntialias
Definition: qfont.h:81
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:56
int height() const
QRect boundingRect(QChar) const
int ascent() const
static QFontPrivate * get(const QFont &font)
Definition: qfont_p.h:208
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:272
void setLayout(QGraphicsLayout *layout)
void setFocusPolicy(Qt::FocusPolicy policy)
QPalette palette
the widget's palette
QFont font
the widgets' font
void setWindowTitle(const QString &title)
void setPalette(const QPalette &palette)
QGraphicsLayout * layout
The layout of the widget.
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:114
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
@ Format_ARGB32_Premultiplied
Definition: qimage.h:84
The QKeyEvent class describes a key event.
Definition: qevent.h:471
@ SetFixedSize
Definition: qlayout.h:75
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:64
void textChanged(const QString &)
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:231
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
QList< T > findChildren(const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
Definition: qobject.h:175
qreal devicePixelRatio() const
Definition: qpaintdevice.h:85
The QPaintEvent class contains event parameters for paint events. \inmodule QtGui.
Definition: qevent.h:539
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qpainter.cpp:5175
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:55
const QBrush & text() const
Definition: qpalette.h:122
const QColor & color(ColorGroup cg, ColorRole cr) const
Definition: qpalette.h:101
@ Window
Definition: qpalette.h:87
@ Text
Definition: qpalette.h:87
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:61
void setCosmetic(bool cosmetic)
Definition: qpen.cpp:833
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:52
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool isNull() const
Definition: qstring.h:1078
static QString fromUtf8(QByteArrayView utf8)
Definition: qstring.cpp:5632
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
void setLayout(QLayout *)
Definition: qwidget.cpp:10146
QWidget * window() const
Definition: qwidget.cpp:4319
QSize size
the size of the widget excluding any window frame
Definition: qwidget.h:147
QLayout * layout() const
Definition: qwidget.cpp:10115
void setPalette(const QPalette &)
Definition: qwidget.cpp:4536
QPalette palette
the widget's palette
Definition: qwidget.h:166
int width
the width of the widget excluding any window frame
Definition: qwidget.h:148
QFontMetrics fontMetrics() const
Definition: qwidget.h:880
int height
the height of the widget excluding any window frame
Definition: qwidget.h:149
void setFocus()
Definition: qwidget.h:454
void show()
Definition: qwidget.cpp:7825
bool event(QEvent *event) override
Definition: qwidget.cpp:8772
void setFont(const QFont &)
Definition: qwidget.cpp:4673
QFont font
the font currently set for the widget
Definition: qwidget.h:167
void setMode(TextRenderer::RenderingMode mode)
Definition: main.cpp:286
QWidget(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
QWidget * m_previews
Definition: main.cpp:306
void keyPressEvent(QKeyEvent *e) override
Definition: main.cpp:300
void mousePressEvent(QMouseEvent *) override
Definition: main.cpp:295
TestWidget()
Definition: main.cpp:186
RenderingMode m_mode
Definition: main.cpp:178
@ QtRendering
Definition: main.cpp:46
@ NativeRendering
Definition: main.cpp:46
QString text() const
Definition: main.cpp:71
QString m_text
Definition: main.cpp:179
Q_ENUM(RenderingMode)
void renderNativeText(QImage &image)
Definition: main.cpp:134
QSize sizeHint() const override
Definition: main.cpp:76
void paintEvent(QPaintEvent *) override
Definition: main.cpp:94
TextRenderer(qreal pointSize, const QString &text, const QColor &textColor=QColor(), const QColor &bgColor=QColor())
Definition: main.cpp:49
void renderQtText(QImage &image)
Definition: main.cpp:119
bool event(QEvent *event) override
Definition: main.cpp:82
int main(int argc, char **argv)
Definition: main.cpp:1
QPushButton
[1]
QOpenGLWidget * widget
[1]
double e
@ StrongFocus
Definition: qnamespace.h:135
@ white
Definition: qnamespace.h:62
@ magenta
Definition: qnamespace.h:70
@ black
Definition: qnamespace.h:61
@ green
Definition: qnamespace.h:67
@ Key_Space
Definition: qnamespace.h:538
Definition: image.cpp:51
std::pair< T1, T2 > QPair
Definition: qcontainerfwd.h:56
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
@ text
GLenum mode
GLfloat GLfloat f
GLuint color
[2]
GLeglImageOES image
struct _cl_event * event
Definition: qopenglext.h:2998
GLbyte GLbyte blue
Definition: qopenglext.h:385
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
GLfloat GLfloat GLfloat alpha
Definition: qopenglext.h:418
GLbyte green
Definition: qopenglext.h:385
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition: qpair.h:55
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
#define Q_OBJECT
Definition: qtmetamacros.h:158
QThreadPool pool
QLineEdit * lineEdit
QApplication app(argc, argv)
[0]
backaction setToolTip(browser.historyTitle(-1))
[0]