Revision control
Copy as Markdown
/****************************************************************************
**
** Copyright (C) 2014 Aaron McCarthy <mccarthy.aaron@gmail.com>
** Copyright (C) 2015 The Qt Company Ltd.
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativegeomapcopyrightsnotice_p.h"
#include <QtGui/QTextDocument>
#include <QtGui/QAbstractTextDocumentLayout>
#include <QtGui/QPainter>
#include <QtQuick/private/qquickanchors_p.h>
#include <QtQuick/private/qquickanchors_p_p.h>
#include <QtLocation/private/qdeclarativegeomap_p.h>
QT_BEGIN_NAMESPACE
/*!
\qmltype MapCopyrightNotice
\instantiates QDeclarativeGeoMapCopyrightNotice
\inqmlmodule QtLocation
\ingroup qml-QtLocation5-maps
\since Qt Location 5.9
\brief The MapCopyrightNotice item displays the current valid
copyright notice for a Map element.
This object can be used to place an additional copyright notices
programmatically.
Note that declaring a MapCopyrightNotice inside a QtLocation::Map element
is not possible, like for any other QQuickItem.
The release of this API with Qt 5.9 is a Technology Preview.
*/
/*!
\qmlproperty bool QtLocation::MapCopyrightNotice::mapSource
This property holds the current map source providing the copyright data shown
in this notice.
In order to let the MapCopyrightNotice display a copyright, this property must
be set, as it is the only data source for this element.
*/
QDeclarativeGeoMapCopyrightNotice::QDeclarativeGeoMapCopyrightNotice(QQuickItem *parent)
: QQuickPaintedItem(parent), m_copyrightsHtml(0), m_copyrightsVisible(true), m_mapSource(0)
{
// If this item is constructed inside the map, automatically anchor it where it always used to be.
if (qobject_cast<QDeclarativeGeoMap *>(parent))
anchorToBottomLeft();
}
QDeclarativeGeoMapCopyrightNotice::~QDeclarativeGeoMapCopyrightNotice()
{
}
void QDeclarativeGeoMapCopyrightNotice::anchorToBottomLeft()
{
if (!parent())
return;
QQuickAnchors *anchors = property("anchors").value<QQuickAnchors *>();
if (anchors) {
anchors->setLeft(QQuickAnchorLine(qobject_cast<QQuickItem *>(parent()), QQuickAnchors::LeftAnchor));
anchors->setBottom(QQuickAnchorLine(qobject_cast<QQuickItem *>(parent()), QQuickAnchors::BottomAnchor));
}
}
void QDeclarativeGeoMapCopyrightNotice::setMapSource(QDeclarativeGeoMap *mapSource)
{
if (m_mapSource == mapSource)
return;
if (m_mapSource) {
// disconnect this object from current map source
m_mapSource->disconnect(this);
m_copyrightsHtml->clear();
m_copyrightsImage = QImage();
m_mapSource = Q_NULLPTR;
}
if (mapSource) {
m_mapSource = mapSource;
// First update the copyright. Only Image will do here, no need to store HTML right away.
if (!mapSource->m_copyrights->m_copyrightsImage.isNull())
m_copyrightsImage = mapSource->m_copyrights->m_copyrightsImage;
connect(m_mapSource, SIGNAL(copyrightsChanged(QImage)),
this, SLOT(copyrightsChanged(QImage)));
connect(m_mapSource, SIGNAL(copyrightsChanged(QString)),
this, SLOT(copyrightsChanged(QString)));
connect(this, SIGNAL(linkActivated(QString)),
m_mapSource, SIGNAL(copyrightLinkActivated(QString)));
}
update();
emit mapSourceChanged();
}
QDeclarativeGeoMap *QDeclarativeGeoMapCopyrightNotice::mapSource()
{
return m_mapSource;
}
/*!
\internal
*/
void QDeclarativeGeoMapCopyrightNotice::paint(QPainter *painter)
{
painter->drawImage(0, 0, m_copyrightsImage);
}
void QDeclarativeGeoMapCopyrightNotice::mousePressEvent(QMouseEvent *event)
{
if (m_copyrightsHtml) {
m_activeAnchor = m_copyrightsHtml->documentLayout()->anchorAt(event->pos());
if (!m_activeAnchor.isEmpty())
return;
}
QQuickPaintedItem::mousePressEvent(event);
}
void QDeclarativeGeoMapCopyrightNotice::mouseReleaseEvent(QMouseEvent *event)
{
if (m_copyrightsHtml) {
QString anchor = m_copyrightsHtml->documentLayout()->anchorAt(event->pos());
if (anchor == m_activeAnchor && !anchor.isEmpty()) {
emit linkActivated(anchor);
m_activeAnchor.clear();
}
}
}
/*!
\internal
*/
void QDeclarativeGeoMapCopyrightNotice::setCopyrightsVisible(bool visible)
{
m_copyrightsVisible = visible;
setVisible(!m_copyrightsImage.isNull() && visible);
}
/*!
\internal
*/
void QDeclarativeGeoMapCopyrightNotice::setCopyrightsZ(int copyrightsZ)
{
setZ(copyrightsZ);
update();
}
/*!
\internal
*/
void QDeclarativeGeoMapCopyrightNotice::copyrightsChanged(const QImage ©rightsImage)
{
delete m_copyrightsHtml;
m_copyrightsHtml = 0;
m_copyrightsImage = copyrightsImage;
setWidth(m_copyrightsImage.width());
setHeight(m_copyrightsImage.height());
setKeepMouseGrab(false);
setAcceptedMouseButtons(Qt::NoButton);
setVisible(m_copyrightsVisible);
update();
}
void QDeclarativeGeoMapCopyrightNotice::copyrightsChanged(const QString ©rightsHtml)
{
if (copyrightsHtml.isEmpty()) {
setVisible(false);
return;
} else if (!m_copyrightsVisible) {
setVisible(false);
} else {
setVisible(true);
}
if (!m_copyrightsHtml)
m_copyrightsHtml = new QTextDocument(this);
m_copyrightsHtml->setHtml(copyrightsHtml);
m_copyrightsImage = QImage(m_copyrightsHtml->size().toSize(),
QImage::Format_ARGB32_Premultiplied);
m_copyrightsImage.fill(qPremultiply(qRgba(255, 255, 255, 128)));
QPainter painter(&m_copyrightsImage);
//m_copyrightsHtml->drawContents(&painter); // <- this uses the default application palette, that might have, f.ex., white text
QAbstractTextDocumentLayout::PaintContext ctx;
ctx.palette.setColor(QPalette::Text, QColor(QStringLiteral("black")));
ctx.palette.setColor(QPalette::Link, QColor(QStringLiteral("blue")));
m_copyrightsHtml->documentLayout()->draw(&painter, ctx);
setWidth(m_copyrightsImage.width());
setHeight(m_copyrightsImage.height());
setContentsSize(m_copyrightsImage.size());
setKeepMouseGrab(true);
setAcceptedMouseButtons(Qt::LeftButton);
update();
}
QT_END_NAMESPACE