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 examples 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 "openglwindow.h" |
52 | |
53 | #include <QApplication> |
54 | #include <QFocusEvent> |
55 | #include <QHBoxLayout> |
56 | #include <QKeyEvent> |
57 | #include <QLineEdit> |
58 | #include <QMouseEvent> |
59 | #include <QPainter> |
60 | #include <QWidget> |
61 | |
62 | |
63 | // Making use of the class from the openglwindow example |
64 | class Window : public OpenGLWindow |
65 | { |
66 | Q_OBJECT |
67 | public: |
68 | using OpenGLWindow::OpenGLWindow; |
69 | |
70 | void render(QPainter *p) override |
71 | { |
72 | QLinearGradient g(0, 0, 0, height()); |
73 | g.setColorAt(0, QColor("lightsteelblue" )); |
74 | g.setColorAt(1, Qt::black); |
75 | p->fillRect(0, 0, width(), height(), g); |
76 | |
77 | p->setPen(Qt::white); |
78 | |
79 | p->drawText(20, 30, QLatin1String("This is an OpenGL based QWindow" )); |
80 | |
81 | if (m_key.trimmed().length() > 0) { |
82 | QRect bounds = p->boundingRect(QRect(0, 0, width(), height()), Qt::AlignTop | Qt::AlignLeft, m_key); |
83 | p->save(); |
84 | p->translate(width() / 2.0, height() / 2.0); |
85 | p->scale(10, 10); |
86 | p->translate(-bounds.width() / 2.0, -bounds.height() / 2.0); |
87 | p->drawText(bounds, Qt::AlignCenter, m_key); |
88 | p->restore(); |
89 | } |
90 | |
91 | if (m_focus) |
92 | p->drawText(20, height() - 20, QLatin1String("Window has focus!" )); |
93 | |
94 | p->setRenderHint(QPainter::Antialiasing); |
95 | p->drawPolyline(m_polygon); |
96 | } |
97 | |
98 | void mousePressEvent(QMouseEvent *e) override |
99 | { |
100 | if (!m_mouseDown) { |
101 | m_mouseDown = true; |
102 | m_polygon.clear(); |
103 | m_polygon.append(e->position().toPoint()); |
104 | renderLater(); |
105 | } |
106 | } |
107 | |
108 | void mouseMoveEvent(QMouseEvent *e) override |
109 | { |
110 | if (m_mouseDown) { |
111 | m_polygon.append(e->position().toPoint()); |
112 | renderLater(); |
113 | } |
114 | } |
115 | |
116 | void mouseReleaseEvent(QMouseEvent *e) override |
117 | { |
118 | if (m_mouseDown) { |
119 | m_mouseDown = false; |
120 | m_polygon.append(e->position().toPoint()); |
121 | renderLater(); |
122 | } |
123 | } |
124 | |
125 | void focusInEvent(QFocusEvent *) override |
126 | { |
127 | m_focus = true; |
128 | renderLater(); |
129 | } |
130 | |
131 | void focusOutEvent(QFocusEvent *) override |
132 | { |
133 | m_focus = false; |
134 | m_polygon.clear(); |
135 | renderLater(); |
136 | } |
137 | |
138 | void keyPressEvent(QKeyEvent *e) override |
139 | { |
140 | m_key = e->text(); |
141 | renderLater(); |
142 | } |
143 | |
144 | void keyReleaseEvent(QKeyEvent *) override |
145 | { |
146 | m_key = QString(); |
147 | renderLater(); |
148 | } |
149 | private: |
150 | QPolygon m_polygon; |
151 | QString m_key; |
152 | bool m_mouseDown = false; |
153 | bool m_focus = false; |
154 | }; |
155 | |
156 | |
157 | int main(int argc, char *argv[]) |
158 | { |
159 | QApplication app(argc, argv); |
160 | |
161 | QWidget *widget = new QWidget; |
162 | QHBoxLayout *layout = new QHBoxLayout(widget); |
163 | |
164 | Window *window = new Window; |
165 | |
166 | QWidget *container = QWidget::createWindowContainer(window); |
167 | container->setMinimumSize(300, 300); |
168 | container->setMaximumSize(600, 600); |
169 | container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
170 | container->setFocusPolicy(Qt::StrongFocus); |
171 | |
172 | window->setGeometry(100, 100, 300, 200); |
173 | |
174 | layout->addWidget(new QLineEdit(QLatin1String("A QLineEdit" ))); |
175 | layout->addWidget(container); |
176 | layout->addWidget(new QLineEdit(QLatin1String("A QLabel" ))); |
177 | |
178 | widget->show(); |
179 | |
180 | return app.exec(); |
181 | } |
182 | |
183 | #include "windowcontainer.moc" |
184 | |