1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "mainwindow.h"
52
53#include <QApplication>
54#include <QPainterPath>
55#include <QPainter>
56#include <QMap>
57#include <QDebug>
58
59void render_qt_text(QPainter *painter, int w, int h, const QColor &color)
60{
61 QPainterPath path;
62 path.moveTo(x: -0.083695, y: 0.283849);
63 path.cubicTo(ctrlPt1x: -0.049581, ctrlPt1y: 0.349613, ctrlPt2x: -0.012720, ctrlPt2y: 0.397969, endPtx: 0.026886, endPty: 0.428917);
64 path.cubicTo(ctrlPt1x: 0.066493, ctrlPt1y: 0.459865, ctrlPt2x: 0.111593, ctrlPt2y: 0.477595, endPtx: 0.162186, endPty: 0.482108);
65 path.lineTo(x: 0.162186, y: 0.500000);
66 path.cubicTo(ctrlPt1x: 0.115929, ctrlPt1y: 0.498066, ctrlPt2x: 0.066565, ctrlPt2y: 0.487669, endPtx: 0.014094, endPty: 0.468810);
67 path.cubicTo(ctrlPt1x: -0.038378, ctrlPt1y: 0.449952, ctrlPt2x: -0.088103, ctrlPt2y: 0.423839, endPtx: -0.135082, endPty: 0.390474);
68 path.cubicTo(ctrlPt1x: -0.182061, ctrlPt1y: 0.357108, ctrlPt2x: -0.222608, ctrlPt2y: 0.321567, endPtx: -0.256722, endPty: 0.283849);
69 path.cubicTo(ctrlPt1x: -0.304712, ctrlPt1y: 0.262250, ctrlPt2x: -0.342874, ctrlPt2y: 0.239362, endPtx: -0.371206, endPty: 0.215184);
70 path.cubicTo(ctrlPt1x: -0.411969, ctrlPt1y: 0.179078, ctrlPt2x: -0.443625, ctrlPt2y: 0.134671, endPtx: -0.466175, endPty: 0.081963);
71 path.cubicTo(ctrlPt1x: -0.488725, ctrlPt1y: 0.029255, ctrlPt2x: -0.500000, ctrlPt2y: -0.033043, endPtx: -0.500000, endPty: -0.104932);
72 path.cubicTo(ctrlPt1x: -0.500000, ctrlPt1y: -0.218407, ctrlPt2x: -0.467042, ctrlPt2y: -0.312621, endPtx: -0.401127, endPty: -0.387573);
73 path.cubicTo(ctrlPt1x: -0.335212, ctrlPt1y: -0.462524, ctrlPt2x: -0.255421, ctrlPt2y: -0.500000, endPtx: -0.161752, endPty: -0.500000);
74 path.cubicTo(ctrlPt1x: -0.072998, ctrlPt1y: -0.500000, ctrlPt2x: 0.003903, ctrlPt2y: -0.462444, endPtx: 0.068951, endPty: -0.387331);
75 path.cubicTo(ctrlPt1x: 0.133998, ctrlPt1y: -0.312218, ctrlPt2x: 0.166522, ctrlPt2y: -0.217440, endPtx: 0.166522, endPty: -0.102998);
76 path.cubicTo(ctrlPt1x: 0.166522, ctrlPt1y: -0.010155, ctrlPt2x: 0.143394, ctrlPt2y: 0.071325, endPtx: 0.097138, endPty: 0.141441);
77 path.cubicTo(ctrlPt1x: 0.050882, ctrlPt1y: 0.211557, ctrlPt2x: -0.009396, ctrlPt2y: 0.259026, endPtx: -0.083695, endPty: 0.283849);
78 path.moveTo(x: -0.167823, y: -0.456963);
79 path.cubicTo(ctrlPt1x: -0.228823, ctrlPt1y: -0.456963, ctrlPt2x: -0.277826, ctrlPt2y: -0.432624, endPtx: -0.314831, endPty: -0.383946);
80 path.cubicTo(ctrlPt1x: -0.361665, ctrlPt1y: -0.323340, ctrlPt2x: -0.385082, ctrlPt2y: -0.230335, endPtx: -0.385082, endPty: -0.104932);
81 path.cubicTo(ctrlPt1x: -0.385082, ctrlPt1y: 0.017569, ctrlPt2x: -0.361376, ctrlPt2y: 0.112025, endPtx: -0.313964, endPty: 0.178433);
82 path.cubicTo(ctrlPt1x: -0.277248, ctrlPt1y: 0.229368, ctrlPt2x: -0.228534, ctrlPt2y: 0.254836, endPtx: -0.167823, endPty: 0.254836);
83 path.cubicTo(ctrlPt1x: -0.105088, ctrlPt1y: 0.254836, ctrlPt2x: -0.054496, ctrlPt2y: 0.229368, endPtx: -0.016045, endPty: 0.178433);
84 path.cubicTo(ctrlPt1x: 0.029055, ctrlPt1y: 0.117827, ctrlPt2x: 0.051605, ctrlPt2y: 0.028691, endPtx: 0.051605, endPty: -0.088975);
85 path.cubicTo(ctrlPt1x: 0.051605, ctrlPt1y: -0.179562, ctrlPt2x: 0.039318, ctrlPt2y: -0.255803, endPtx: 0.014744, endPty: -0.317698);
86 path.cubicTo(ctrlPt1x: -0.004337, ctrlPt1y: -0.365409, ctrlPt2x: -0.029705, ctrlPt2y: -0.400548, endPtx: -0.061362, endPty: -0.423114);
87 path.cubicTo(ctrlPt1x: -0.093018, ctrlPt1y: -0.445680, ctrlPt2x: -0.128505, ctrlPt2y: -0.456963, endPtx: -0.167823, endPty: -0.456963);
88 path.moveTo(x: 0.379011, y: -0.404739);
89 path.lineTo(x: 0.379011, y: -0.236460);
90 path.lineTo(x: 0.486123, y: -0.236460);
91 path.lineTo(x: 0.486123, y: -0.197292);
92 path.lineTo(x: 0.379011, y: -0.197292);
93 path.lineTo(x: 0.379011, y: 0.134913);
94 path.cubicTo(ctrlPt1x: 0.379011, ctrlPt1y: 0.168117, ctrlPt2x: 0.383276, ctrlPt2y: 0.190442, endPtx: 0.391804, endPty: 0.201886);
95 path.cubicTo(ctrlPt1x: 0.400332, ctrlPt1y: 0.213330, ctrlPt2x: 0.411246, ctrlPt2y: 0.219052, endPtx: 0.424545, endPty: 0.219052);
96 path.cubicTo(ctrlPt1x: 0.435531, ctrlPt1y: 0.219052, ctrlPt2x: 0.446227, ctrlPt2y: 0.215264, endPtx: 0.456635, endPty: 0.207689);
97 path.cubicTo(ctrlPt1x: 0.467042, ctrlPt1y: 0.200113, ctrlPt2x: 0.474993, ctrlPt2y: 0.188910, endPtx: 0.480486, endPty: 0.174081);
98 path.lineTo(x: 0.500000, y: 0.174081);
99 path.cubicTo(ctrlPt1x: 0.488436, ctrlPt1y: 0.210509, ctrlPt2x: 0.471957, ctrlPt2y: 0.237911, endPtx: 0.450564, endPty: 0.256286);
100 path.cubicTo(ctrlPt1x: 0.429170, ctrlPt1y: 0.274662, ctrlPt2x: 0.407054, ctrlPt2y: 0.283849, endPtx: 0.384215, endPty: 0.283849);
101 path.cubicTo(ctrlPt1x: 0.368893, ctrlPt1y: 0.283849, ctrlPt2x: 0.353859, ctrlPt2y: 0.279094, endPtx: 0.339115, endPty: 0.269584);
102 path.cubicTo(ctrlPt1x: 0.324371, ctrlPt1y: 0.260074, ctrlPt2x: 0.313530, ctrlPt2y: 0.246534, endPtx: 0.306592, endPty: 0.228965);
103 path.cubicTo(ctrlPt1x: 0.299653, ctrlPt1y: 0.211396, ctrlPt2x: 0.296184, ctrlPt2y: 0.184075, endPtx: 0.296184, endPty: 0.147002);
104 path.lineTo(x: 0.296184, y: -0.197292);
105 path.lineTo(x: 0.223330, y: -0.197292);
106 path.lineTo(x: 0.223330, y: -0.215667);
107 path.cubicTo(ctrlPt1x: 0.241833, ctrlPt1y: -0.224049, ctrlPt2x: 0.260697, ctrlPt2y: -0.237992, endPtx: 0.279922, endPty: -0.257495);
108 path.cubicTo(ctrlPt1x: 0.299147, ctrlPt1y: -0.276999, ctrlPt2x: 0.316276, ctrlPt2y: -0.300129, endPtx: 0.331310, endPty: -0.326886);
109 path.cubicTo(ctrlPt1x: 0.338826, ctrlPt1y: -0.341070, ctrlPt2x: 0.349523, ctrlPt2y: -0.367021, endPtx: 0.363400, endPty: -0.404739);
110 path.lineTo(x: 0.379011, y: -0.404739);
111 path.moveTo(x: -0.535993, y: 0.275629);
112
113 painter->translate(dx: w / 2, dy: h / 2);
114 double scale = qMin(a: w, b: h) * 8 / 10.0;
115 painter->scale(sx: scale, sy: scale);
116
117 painter->setRenderHint(hint: QPainter::Antialiasing);
118
119 painter->save();
120 painter->translate(dx: .1, dy: .1);
121 painter->fillPath(path, brush: QColor(0, 0, 0, 63));
122 painter->restore();
123
124 painter->setBrush(color);
125 painter->setPen(QPen(Qt::black, 0.02, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
126 painter->drawPath(path);
127}
128
129static void usage()
130{
131 qWarning() << "Usage: mainwindow [-SizeHint<color> <width>x<height>] ...";
132 exit(status: 1);
133}
134
135enum ParseCommandLineArgumentsResult {
136 CommandLineArgumentsOk,
137 CommandLineArgumentsError,
138 HelpRequested
139};
140
141static ParseCommandLineArgumentsResult
142 parseCustomSizeHints(const QStringList &arguments, MainWindow::CustomSizeHintMap *result)
143{
144 result->clear();
145 const int argumentCount = arguments.size();
146 for (int i = 1; i < argumentCount; ++i) {
147 const QString &arg = arguments.at(i);
148 if (arg.startsWith(s: QLatin1String("-SizeHint"))) {
149 const QString name = arg.mid(position: 9);
150 if (name.isEmpty())
151 return CommandLineArgumentsError;
152 if (++i == argumentCount)
153 return CommandLineArgumentsError;
154 const QString sizeStr = arguments.at(i);
155 const int idx = sizeStr.indexOf(c: QLatin1Char('x'));
156 if (idx == -1)
157 return CommandLineArgumentsError;
158 bool ok;
159 const int w = sizeStr.leftRef(n: idx).toInt(ok: &ok);
160 if (!ok)
161 return CommandLineArgumentsError;
162 const int h = sizeStr.midRef(position: idx + 1).toInt(ok: &ok);
163 if (!ok)
164 return CommandLineArgumentsError;
165 result->insert(akey: name, avalue: QSize(w, h));
166 } else if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
167 return HelpRequested;
168 } else {
169 return CommandLineArgumentsError;
170 }
171 }
172
173 return CommandLineArgumentsOk;
174}
175
176int main(int argc, char **argv)
177{
178 QApplication::setAttribute(attribute: Qt::AA_EnableHighDpiScaling);
179 QApplication::setAttribute(attribute: Qt::AA_UseHighDpiPixmaps);
180 QApplication app(argc, argv);
181 MainWindow::CustomSizeHintMap customSizeHints;
182 switch (parseCustomSizeHints(arguments: QCoreApplication::arguments(), result: &customSizeHints)) {
183 case CommandLineArgumentsOk:
184 break;
185 case CommandLineArgumentsError:
186 usage();
187 return -1;
188 case HelpRequested:
189 usage();
190 return 0;
191 }
192 MainWindow mainWin(customSizeHints);
193 mainWin.resize(w: 800, h: 600);
194 mainWin.show();
195 return app.exec();
196}
197

source code of qtbase/examples/widgets/mainwindows/mainwindow/main.cpp