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 | |
59 | void render_qt_text(QPainter *painter, int w, int h, const QColor &color) |
60 | { |
61 | QPainterPath path; |
62 | path.moveTo(-0.083695, 0.283849); |
63 | path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917); |
64 | path.cubicTo(0.066493, 0.459865, 0.111593, 0.477595, 0.162186, 0.482108); |
65 | path.lineTo(0.162186, 0.500000); |
66 | path.cubicTo(0.115929, 0.498066, 0.066565, 0.487669, 0.014094, 0.468810); |
67 | path.cubicTo(-0.038378, 0.449952, -0.088103, 0.423839, -0.135082, 0.390474); |
68 | path.cubicTo(-0.182061, 0.357108, -0.222608, 0.321567, -0.256722, 0.283849); |
69 | path.cubicTo(-0.304712, 0.262250, -0.342874, 0.239362, -0.371206, 0.215184); |
70 | path.cubicTo(-0.411969, 0.179078, -0.443625, 0.134671, -0.466175, 0.081963); |
71 | path.cubicTo(-0.488725, 0.029255, -0.500000, -0.033043, -0.500000, -0.104932); |
72 | path.cubicTo(-0.500000, -0.218407, -0.467042, -0.312621, -0.401127, -0.387573); |
73 | path.cubicTo(-0.335212, -0.462524, -0.255421, -0.500000, -0.161752, -0.500000); |
74 | path.cubicTo(-0.072998, -0.500000, 0.003903, -0.462444, 0.068951, -0.387331); |
75 | path.cubicTo(0.133998, -0.312218, 0.166522, -0.217440, 0.166522, -0.102998); |
76 | path.cubicTo(0.166522, -0.010155, 0.143394, 0.071325, 0.097138, 0.141441); |
77 | path.cubicTo(0.050882, 0.211557, -0.009396, 0.259026, -0.083695, 0.283849); |
78 | path.moveTo(-0.167823, -0.456963); |
79 | path.cubicTo(-0.228823, -0.456963, -0.277826, -0.432624, -0.314831, -0.383946); |
80 | path.cubicTo(-0.361665, -0.323340, -0.385082, -0.230335, -0.385082, -0.104932); |
81 | path.cubicTo(-0.385082, 0.017569, -0.361376, 0.112025, -0.313964, 0.178433); |
82 | path.cubicTo(-0.277248, 0.229368, -0.228534, 0.254836, -0.167823, 0.254836); |
83 | path.cubicTo(-0.105088, 0.254836, -0.054496, 0.229368, -0.016045, 0.178433); |
84 | path.cubicTo(0.029055, 0.117827, 0.051605, 0.028691, 0.051605, -0.088975); |
85 | path.cubicTo(0.051605, -0.179562, 0.039318, -0.255803, 0.014744, -0.317698); |
86 | path.cubicTo(-0.004337, -0.365409, -0.029705, -0.400548, -0.061362, -0.423114); |
87 | path.cubicTo(-0.093018, -0.445680, -0.128505, -0.456963, -0.167823, -0.456963); |
88 | path.moveTo(0.379011, -0.404739); |
89 | path.lineTo(0.379011, -0.236460); |
90 | path.lineTo(0.486123, -0.236460); |
91 | path.lineTo(0.486123, -0.197292); |
92 | path.lineTo(0.379011, -0.197292); |
93 | path.lineTo(0.379011, 0.134913); |
94 | path.cubicTo(0.379011, 0.168117, 0.383276, 0.190442, 0.391804, 0.201886); |
95 | path.cubicTo(0.400332, 0.213330, 0.411246, 0.219052, 0.424545, 0.219052); |
96 | path.cubicTo(0.435531, 0.219052, 0.446227, 0.215264, 0.456635, 0.207689); |
97 | path.cubicTo(0.467042, 0.200113, 0.474993, 0.188910, 0.480486, 0.174081); |
98 | path.lineTo(0.500000, 0.174081); |
99 | path.cubicTo(0.488436, 0.210509, 0.471957, 0.237911, 0.450564, 0.256286); |
100 | path.cubicTo(0.429170, 0.274662, 0.407054, 0.283849, 0.384215, 0.283849); |
101 | path.cubicTo(0.368893, 0.283849, 0.353859, 0.279094, 0.339115, 0.269584); |
102 | path.cubicTo(0.324371, 0.260074, 0.313530, 0.246534, 0.306592, 0.228965); |
103 | path.cubicTo(0.299653, 0.211396, 0.296184, 0.184075, 0.296184, 0.147002); |
104 | path.lineTo(0.296184, -0.197292); |
105 | path.lineTo(0.223330, -0.197292); |
106 | path.lineTo(0.223330, -0.215667); |
107 | path.cubicTo(0.241833, -0.224049, 0.260697, -0.237992, 0.279922, -0.257495); |
108 | path.cubicTo(0.299147, -0.276999, 0.316276, -0.300129, 0.331310, -0.326886); |
109 | path.cubicTo(0.338826, -0.341070, 0.349523, -0.367021, 0.363400, -0.404739); |
110 | path.lineTo(0.379011, -0.404739); |
111 | path.moveTo(-0.535993, 0.275629); |
112 | |
113 | painter->translate(w / 2, h / 2); |
114 | double scale = qMin(w, h) * 8 / 10.0; |
115 | painter->scale(scale, scale); |
116 | |
117 | painter->setRenderHint(QPainter::Antialiasing); |
118 | |
119 | painter->save(); |
120 | painter->translate(.1, .1); |
121 | painter->fillPath(path, 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 | |
129 | static void usage() |
130 | { |
131 | qWarning() << "Usage: mainwindow [-SizeHint<color> <width>x<height>] ..." ; |
132 | exit(1); |
133 | } |
134 | |
135 | enum ParseCommandLineArgumentsResult { |
136 | CommandLineArgumentsOk, |
137 | CommandLineArgumentsError, |
138 | HelpRequested |
139 | }; |
140 | |
141 | static 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(QLatin1String("-SizeHint" ))) { |
149 | const QString name = arg.mid(9); |
150 | if (name.isEmpty()) |
151 | return CommandLineArgumentsError; |
152 | if (++i == argumentCount) |
153 | return CommandLineArgumentsError; |
154 | const QStringView sizeStr{ arguments.at(i) }; |
155 | const int idx = sizeStr.indexOf(QLatin1Char('x')); |
156 | if (idx == -1) |
157 | return CommandLineArgumentsError; |
158 | bool ok; |
159 | const int w = sizeStr.left(idx).toInt(&ok); |
160 | if (!ok) |
161 | return CommandLineArgumentsError; |
162 | const int h = sizeStr.mid(idx + 1).toInt(&ok); |
163 | if (!ok) |
164 | return CommandLineArgumentsError; |
165 | result->insert(name, 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 | |
176 | int main(int argc, char **argv) |
177 | { |
178 | QApplication app(argc, argv); |
179 | MainWindow::CustomSizeHintMap customSizeHints; |
180 | switch (parseCustomSizeHints(QCoreApplication::arguments(), &customSizeHints)) { |
181 | case CommandLineArgumentsOk: |
182 | break; |
183 | case CommandLineArgumentsError: |
184 | usage(); |
185 | return -1; |
186 | case HelpRequested: |
187 | usage(); |
188 | return 0; |
189 | } |
190 | MainWindow mainWin(customSizeHints); |
191 | mainWin.resize(800, 600); |
192 | mainWin.show(); |
193 | return app.exec(); |
194 | } |
195 | |