Marble

PopupLayer.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2012 Mohammed Nafees <[email protected]>
4 // SPDX-FileCopyrightText: 2012 Dennis Nienhüser <[email protected]>
5 // SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <[email protected]>
6 // SPDX-FileCopyrightText: 2015 Imran Tatriev <[email protected]>
7 //
8 
9 #include "PopupLayer.h"
10 
11 #include "GeoDataCoordinates.h"
12 #include "GeoPainter.h"
13 #include "MarbleWidget.h"
14 #include "PopupItem.h"
15 #include "ViewportParams.h"
16 #include "RenderPlugin.h"
17 #include "RenderState.h"
18 
19 #include <QSizeF>
20 
21 namespace Marble
22 {
23 
24 class Q_DECL_HIDDEN PopupLayer::Private
25 {
26 public:
27  Private( MarbleWidget *marbleWidget, PopupLayer *q );
28 
29  /**
30  * @brief Sets size of the popup item, based on the requested size and viewport size
31  * @param viewport required to compute the maximum dimensions
32  */
33  void setAppropriateSize( const ViewportParams *viewport );
34 
35  static QString filterEmptyShortDescription(const QString &description);
36  void setupDialogSatellite( const GeoDataPlacemark *index );
37  void setupDialogCity( const GeoDataPlacemark *index );
38  void setupDialogNation( const GeoDataPlacemark *index );
39  void setupDialogGeoPlaces( const GeoDataPlacemark *index );
40  void setupDialogSkyPlaces( const GeoDataPlacemark *index );
41 
42  PopupItem *const m_popupItem;
43  MarbleWidget *const m_widget;
44  QSizeF m_requestedSize;
45  bool m_hasCrosshairsPlugin;
46  bool m_crosshairsVisible;
47 };
48 
49 PopupLayer::Private::Private( MarbleWidget *marbleWidget, PopupLayer *q ) :
50  m_popupItem( new PopupItem( q ) ),
51  m_widget( marbleWidget ),
52  m_hasCrosshairsPlugin( false ),
53  m_crosshairsVisible( true )
54 {
55 }
56 
57 PopupLayer::PopupLayer( MarbleWidget *marbleWidget, QObject *parent ) :
58  QObject( parent ),
59  d( new Private( marbleWidget, this ) )
60 {
61  for (const RenderPlugin *renderPlugin: d->m_widget->renderPlugins()) {
62  if (renderPlugin->nameId() == QLatin1String("crosshairs")) {
63  d->m_hasCrosshairsPlugin = true;
64  break;
65  }
66  }
67 
68  connect( d->m_popupItem, SIGNAL(repaintNeeded()), this, SIGNAL(repaintNeeded()) );
69  connect( d->m_popupItem, SIGNAL(hide()), this, SLOT(hidePopupItem()) );
70 }
71 
72 PopupLayer::~PopupLayer()
73 {
74  delete d;
75 }
76 
77 QStringList PopupLayer::renderPosition() const
78 {
79  return QStringList(QStringLiteral("ALWAYS_ON_TOP"));
80 }
81 
82 bool PopupLayer::render( GeoPainter *painter, ViewportParams *viewport,
83  const QString&, GeoSceneLayer* )
84 {
85  if ( visible() ) {
86  d->setAppropriateSize( viewport );
87  d->m_popupItem->paintEvent( painter, viewport );
88  }
89 
90  return true;
91 }
92 
93 bool PopupLayer::eventFilter( QObject *object, QEvent *e )
94 {
95  return visible() && d->m_popupItem->eventFilter( object, e );
96 }
97 
98 qreal PopupLayer::zValue() const
99 {
100  return 4711.23;
101 }
102 
103 RenderState PopupLayer::renderState() const
104 {
105  return RenderState(QStringLiteral("Popup Window"));
106 }
107 
109 {
110  return d->m_popupItem->visible();
111 }
112 
113 void PopupLayer::setVisible( bool visible )
114 {
115  d->m_popupItem->setVisible( visible );
116  if ( !visible ) {
117  disconnect( d->m_popupItem, SIGNAL(repaintNeeded()), this, SIGNAL(repaintNeeded()) );
118  d->m_popupItem->clearHistory();
119  emit repaintNeeded();
120  }
121  else {
122  connect( d->m_popupItem, SIGNAL(repaintNeeded()), this, SIGNAL(repaintNeeded()) );
123  }
124 }
125 
127 {
128  GeoDataCoordinates coords = d->m_popupItem->coordinate();
129  ViewportParams viewport( d->m_widget->viewport()->projection(),
130  coords.longitude(), coords.latitude(), d->m_widget->viewport()->radius(),
131  d->m_widget->viewport()->size() );
132  qreal sx, sy, lon, lat;
133  viewport.screenCoordinates( coords, sx, sy );
134  sx = viewport.radius() < viewport.width() ? 0.5 * (viewport.width() + viewport.radius()) : 0.75 * viewport.width();
135  viewport.geoCoordinates( sx, sy, lon, lat, GeoDataCoordinates::Radian );
136  coords.setLatitude( lat );
137  coords.setLongitude( lon );
138  d->m_widget->centerOn( coords, true );
139 
140  if( d->m_hasCrosshairsPlugin ) {
141  d->m_crosshairsVisible = d->m_widget->showCrosshairs();
142 
143  if( d->m_crosshairsVisible ) {
144  d->m_widget->setShowCrosshairs( false );
145  }
146  }
147 
148  setVisible( true );
149 }
150 
151 void PopupLayer::setCoordinates( const GeoDataCoordinates &coordinates , Qt::Alignment alignment )
152 {
153  d->m_popupItem->setCoordinate( coordinates );
154  d->m_popupItem->setAlignment( alignment );
155 }
156 
157 void PopupLayer::setUrl( const QUrl &url )
158 {
159  d->m_popupItem->setUrl( url );
160 }
161 
162 void PopupLayer::setContent( const QString &html, const QUrl &baseUrl )
163 {
164  d->m_popupItem->setContent( html, baseUrl );
165 }
166 
168 {
169  if(color.isValid()) {
170  d->m_popupItem->setBackgroundColor(color);
171  }
172 }
173 
175 {
176  if(color.isValid()) {
177  d->m_popupItem->setTextColor(color);
178  }
179 }
180 
181 void PopupLayer::setSize( const QSizeF &size )
182 {
183  d->m_requestedSize = size;
184 }
185 
186 void PopupLayer::Private::setAppropriateSize( const ViewportParams *viewport )
187 {
188  qreal margin = 15.0;
189 
190  QSizeF maximumSize;
191  maximumSize.setWidth( viewport->width() - margin );
192  maximumSize.setHeight( viewport->height() - margin );
193 
194  QSizeF minimumSize( 100.0, 100.0 );
195 
196  m_popupItem->setSize( m_requestedSize.boundedTo( maximumSize ).expandedTo( minimumSize ) );
197 }
198 
199 void PopupLayer::hidePopupItem()
200 {
201  if( d->m_hasCrosshairsPlugin && d->m_crosshairsVisible ) {
202  d->m_widget->setShowCrosshairs( d->m_crosshairsVisible );
203  }
204 
205  setVisible( false );
206 }
207 
208 }
209 
210 #include "moc_PopupLayer.cpp"
void setContent(const QString &html, const QUrl &baseUrl=QUrl())
Sets content of the browser.
Definition: PopupLayer.cpp:162
A 3d point representation.
typedef Alignment
void setLongitude(qreal lon, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian)
set the longitude in a GeoDataCoordinates object
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
bool visible() const
Is popup item visible.
Definition: PopupLayer.cpp:108
void setBackgroundColor(const QColor &color)
Sets background color of the header.
Definition: PopupLayer.cpp:167
void setSize(const QSizeF &size)
Sets size of popup item.
Definition: PopupLayer.cpp:181
void setUrl(const QUrl &url)
Sets URL of the browser.
Definition: PopupLayer.cpp:157
qreal longitude(GeoDataCoordinates::Unit unit) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
void setCoordinates(const GeoDataCoordinates &coordinates, Qt::Alignment alignment)
Sets coordinates.
Definition: PopupLayer.cpp:151
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setLatitude(qreal lat, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian)
set the longitude in a GeoDataCoordinates object
A public class that controls what is visible in the viewport of a Marble map.
void setVisible(bool visible)
Set visibility of the item.
Definition: PopupLayer.cpp:113
qreal latitude(GeoDataCoordinates::Unit unit) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Binds a QML item to a specific geodetic location in screen coordinates.
void setTextColor(const QColor &color)
Sets text color of the header.
Definition: PopupLayer.cpp:174
void popup()
Make the dialog pop up.
Definition: PopupLayer.cpp:126
bool isValid() const const
void setWidth(qreal width)
bool geoCoordinates(const int x, const int y, qreal &lon, qreal &lat, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Degree) const
Get the earth coordinates corresponding to a pixel in the map.
void setHeight(qreal height)
bool screenCoordinates(const qreal lon, const qreal lat, qreal &x, qreal &y) const
Get the screen coordinates corresponding to geographical coordinates in the map.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 29 2023 03:52:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.