Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdeclarativegeomapcopyrightsnotice.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 Aaron McCarthy <mccarthy.aaron@gmail.com>
2// Copyright (C) 2022 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6
7#include <QtGui/QTextDocument>
8#include <QtGui/QAbstractTextDocumentLayout>
9#include <QtGui/QPainter>
10#include <QtGui/QImage>
11#include <QtQuick/private/qquickanchors_p.h>
12#include <QtLocation/private/qdeclarativegeomap_p.h>
13#include <QtQuick/private/qquickpainteditem_p.h>
14
16
18{
19 Q_DECLARE_PUBLIC(QDeclarativeGeoMapCopyrightNotice)
20public:
21 void setVisible(bool visible) override;
22};
23
67: QQuickPaintedItem(parent)
68{
69 // If this item is constructed inside the map, automatically anchor it where it always used to be.
70 if (qobject_cast<QDeclarativeGeoMap *>(parent))
72}
73
78
80{
81 if (!parent())
82 return;
83 QQuickAnchors *anchors = property("anchors").value<QQuickAnchors *>();
84 if (anchors) {
85 anchors->setLeft(QQuickAnchorLine(qobject_cast<QQuickItem *>(parent()), QQuickAnchors::LeftAnchor));
87 }
88}
89
91{
92 if (m_mapSource == map)
93 return;
94
95 if (m_mapSource) {
96 // disconnect this object from current map source
97 m_mapSource->detachCopyrightNotice(copyrightsVisible());
98 m_mapSource->disconnect(this);
99 m_mapSource->m_map->disconnect(this);
100 if (m_copyrightsHtml)
101 m_copyrightsHtml->clear();
102 m_copyrightsImage = QImage();
103 m_mapSource = nullptr;
104 }
105
106 if (map) {
107 m_mapSource = map;
108 m_mapSource->attachCopyrightNotice(copyrightsVisible());
110 mapSource(), &QDeclarativeGeoMap::onAttachedCopyrightNoticeVisibilityChanged);
111
112 // First update the copyright. Only Image will do here, no need to store HTML right away.
113 if (m_mapSource->m_copyrights && !m_mapSource->m_copyrights->m_copyrightsImage.isNull())
114 m_copyrightsImage = m_mapSource->m_copyrights->m_copyrightsImage;
115
120
121 if (m_mapSource->m_map)
122 connectMap();
123 else
125 }
126}
127
140
142{
143 return m_mapSource.data();
144}
145
147{
148 return m_styleSheet;
149}
150
152{
153 m_userDefinedStyleSheet = true;
154
155 if (styleSheet == m_styleSheet)
156 return;
157
158 m_styleSheet = styleSheet;
159 if (!m_html.isEmpty() && m_copyrightsHtml) {
160 delete m_copyrightsHtml;
161 createCopyright();
162#if QT_CONFIG(texthtmlparser)
163 m_copyrightsHtml->setHtml(m_html);
164#else
165 m_copyrightsHtml->setPlainText(m_html);
166#endif
167 }
169 emit styleSheetChanged(m_styleSheet);
170}
171
176{
177 painter->drawImage(0, 0, m_copyrightsImage);
178}
179
181{
182 if (m_copyrightsHtml) {
183 m_activeAnchor = m_copyrightsHtml->documentLayout()->anchorAt(event->pos());
184 if (!m_activeAnchor.isEmpty())
185 return;
186 }
187
189}
190
192{
193 if (m_copyrightsHtml) {
194 QString anchor = m_copyrightsHtml->documentLayout()->anchorAt(event->pos());
195 if (anchor == m_activeAnchor && !anchor.isEmpty()) {
196 emit linkActivated(anchor);
197 m_activeAnchor.clear();
198 }
199 }
200}
201
203{
204 if (!m_copyrightsHtml || m_copyrightsHtml->isEmpty())
205 return;
206
207 m_copyrightsImage = QImage(m_copyrightsHtml->size().toSize(),
209
210 m_copyrightsImage.fill(qPremultiply(QColor(Qt::transparent).rgba()));
211 QPainter painter(&m_copyrightsImage);
213 ctx.palette.setColor(QPalette::Text, QColor::fromString("black"));
214 m_copyrightsHtml->documentLayout()->draw(&painter, ctx);
215
216 setImplicitSize(m_copyrightsImage.width(), m_copyrightsImage.height());
217 setContentsSize(m_copyrightsImage.size());
218
219 setKeepMouseGrab(true);
221
222 update();
223}
224
225void QDeclarativeGeoMapCopyrightNotice::createCopyright()
226{
227 m_copyrightsHtml = new QTextDocument(this);
228#if QT_CONFIG(cssparser)
229 if (!m_styleSheet.isEmpty())
230 m_copyrightsHtml->setDefaultStyleSheet(m_styleSheet);
231#endif
232
233 // The default 4 makes the copyright too wide and tall.
234 m_copyrightsHtml->setDocumentMargin(0);
235}
236
238{
240 q->m_copyrightsVisible = visible;
242}
243
248{
250 if (visible == m_copyrightsVisible)
251 return;
252
253 m_copyrightsVisible = visible;
254 d->QQuickItemPrivate::setVisible(!m_copyrightsImage.isNull() && visible);
256}
257
259{
260 return m_copyrightsVisible;
261}
262
267{
268 setZ(copyrightsZ);
269 update();
270}
271
276{
278 delete m_copyrightsHtml;
279 m_copyrightsHtml = nullptr;
280
281 m_copyrightsImage = copyrightsImage;
282
283 setImplicitSize(m_copyrightsImage.width(), m_copyrightsImage.height());
284
285 setKeepMouseGrab(false);
287 d->QQuickItemPrivate::setVisible(m_copyrightsVisible && !m_copyrightsImage.isNull());
288
289 update();
290}
291
293{
295 if (copyrightsHtml.isEmpty()) {
296 d->QQuickItemPrivate::setVisible(false);
297 return;
298 } else {
299 d->QQuickItemPrivate::setVisible(m_copyrightsVisible);
300 }
301
302 // Divfy, so we can style the background. The extra <span> is a
303 // workaround to QTBUG-58838 and should be removed when it gets fixed.
304#if QT_CONFIG(texthtmlparser)
305 m_html = QStringLiteral("<div id='copyright-root'><span>") + copyrightsHtml + QStringLiteral("</span></div>");
306#else
307 m_html = copyrightsHtml;
308#endif
309
310 if (!m_copyrightsHtml)
311 createCopyright();
312
313#if QT_CONFIG(texthtmlparser)
314 m_copyrightsHtml->setHtml(m_html);
315#else
316 m_copyrightsHtml->setPlainText(m_html);
317#endif
319}
320
322{
323 if (m_userDefinedStyleSheet || styleSheet == m_styleSheet)
324 return;
325
326 m_styleSheet = styleSheet;
327 if (!m_html.isEmpty() && m_copyrightsHtml) {
328 delete m_copyrightsHtml;
329 createCopyright();
330#if QT_CONFIG(texthtmlparser)
331 m_copyrightsHtml->setHtml(m_html);
332#else
333 m_copyrightsHtml->setPlainText(m_html);
334#endif
335 }
337 emit styleSheetChanged(m_styleSheet);
338}
339
QString anchorAt(const QPointF &pos) const
Returns the reference of the anchor the given position, or an empty string if no anchor exists at tha...
virtual void draw(QPainter *painter, const PaintContext &context)=0
Draws the layout with the given painter using the given context.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QColor fromString(QAnyStringView name) noexcept
Definition qcolor.cpp:980
void copyrightsChanged(const QString &copyrightsHtml)
void copyrightsImageChanged(const QImage &copyrightsImage)
void linkActivated(const QString &link)
void styleSheetChanged(const QString &styleSheet)
void mousePressEvent(QMouseEvent *event) override
This event handler can be reimplemented in a subclass to receive mouse press events for an item.
QDeclarativeGeoMapCopyrightNotice(QQuickItem *parent=nullptr)
\qmltype MapCopyrightNotice \instantiates QDeclarativeGeoMapCopyrightNotice \inqmlmodule QtLocation
void onCopyrightsStyleSheetChanged(const QString &styleSheet)
void mouseReleaseEvent(QMouseEvent *event) override
This event handler can be reimplemented in a subclass to receive mouse release events for an item.
void setMapSource(QDeclarativeGeoMap *mapSource)
void copyrightsChanged(const QString &copyrightsHtml)
void mapReadyChanged(bool ready)
void copyrightsImageChanged(const QImage &copyrightsImage)
void copyrightLinkActivated(const QString &link)
void copyrightsStyleSheetChanged(const QString &styleSheet)
virtual QString copyrightsStyleSheet() const
Definition qgeomap.cpp:244
\inmodule QtGui
Definition qimage.h:37
QSize size() const
Returns the size of the image, i.e.
int width() const
Returns the width of the image.
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition qimage.cpp:1222
int height() const
Returns the height of the image.
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition qimage.cpp:1758
\inmodule QtGui
Definition qevent.h:196
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags=Qt::AutoColor)
Draws the rectangular portion source of the given image into the target rectangle in the paint device...
T * data() const noexcept
Definition qpointer.h:73
void setBottom(const QQuickAnchorLine &edge)
void setLeft(const QQuickAnchorLine &edge)
virtual void setVisible(bool visible)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
bool visible
\qmlproperty bool QtQuick::Item::visible
Definition qquickitem.h:80
void setAcceptedMouseButtons(Qt::MouseButtons buttons)
Sets the mouse buttons accepted by this item to buttons.
virtual void mousePressEvent(QMouseEvent *event)
This event handler can be reimplemented in a subclass to receive mouse press events for an item.
QQuickItem * parent
\qmlproperty Item QtQuick::Item::parent This property holds the visual parent of the item.
Definition qquickitem.h:67
void setKeepMouseGrab(bool)
Sets whether the mouse input should remain exclusively with this item.
void setZ(qreal)
void update()
Schedules a call to updatePaintNode() for this item.
void setImplicitSize(qreal, qreal)
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
void setContentsSize(const QSize &)
constexpr QSize toSize() const noexcept
Returns an integer based copy of this size.
Definition qsize.h:401
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
\reentrant \inmodule QtGui
void setHtml(const QString &html)
Replaces the entire contents of the document with the given HTML-formatted text in the html string.
bool isEmpty() const
Returns true if the document is empty; otherwise returns false.
QAbstractTextDocumentLayout * documentLayout() const
Returns the document layout for this document.
QSizeF size
the actual size of the document. This is equivalent to documentLayout()->documentSize();
virtual void clear()
Clears the document.
void setDefaultStyleSheet(const QString &sheet)
void setDocumentMargin(qreal margin)
void setPlainText(const QString &text)
Replaces the entire contents of the document with the given plain text.
EGLContext ctx
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
@ LeftButton
Definition qnamespace.h:58
@ NoButton
Definition qnamespace.h:57
@ transparent
Definition qnamespace.h:47
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QQuickAnchors * anchors(QQuickItem *item)
constexpr QRgb qPremultiply(QRgb x)
Definition qrgb.h:45
#define QStringLiteral(str)
#define emit
double qreal
Definition qtypes.h:187
const char property[13]
Definition qwizard.cpp:101
QPainter painter(this)
[7]