• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
PopupItem.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2012 Torsten Rahn <tackat@kde.org>
9 // Copyright 2013 Mohammed Nafees <nafees.technocool@gmail.com>
10 // Copyright 2012 Dennis Nienhüser <earthwings@gentoo.org>
11 // Copyright 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
12 //
13 
14 #include "PopupItem.h"
15 #include "MarbleWidget.h"
16 #include "MarbleWebView.h"
17 
18 #include <QPointer>
19 #include <QWebView>
20 #include <QWebHistory>
21 #include <QPrinter>
22 #include <QPrintDialog>
23 #include <QMouseEvent>
24 #include <QApplication>
25 #include <QVBoxLayout>
26 #include <QLabel>
27 #include <QPushButton>
28 #include <QPixmapCache>
29 #include <QMenu>
30 #include <QKeyEvent>
31 #include <QClipboard>
32 #include <qdrawutil.h>
33 
34 namespace Marble
35 {
36 
37 PopupItem::PopupItem( QObject* parent ) :
38  QObject( parent ),
39  BillboardGraphicsItem(),
40  m_widget( new QWidget ),
41  m_textColor( QColor(Qt::black) ),
42  m_backColor( QColor(Qt::white) ),
43  m_needMouseRelease(false)
44 {
45  setCacheMode( ItemCoordinateCache );
46  setVisible( false );
47  setSize( QSizeF( 240.0, 320.0 ) );
48 
49  m_ui.setupUi( m_widget );
50 
51  m_ui.goBackButton->setVisible( false );
52  connect( m_ui.goBackButton, SIGNAL(clicked()), this, SLOT(goBack()) );
53 
54 #ifdef QT_NO_PRINTER
55  m_ui.printButton->setVisible( false );
56 #else
57  m_ui.printButton->setVisible( true );
58  connect( m_ui.printButton, SIGNAL(clicked()), this, SLOT(printContent()) );
59 #endif
60 
61  m_widget->setAttribute( Qt::WA_NoSystemBackground, true );
62  QPalette palette = m_ui.webView->palette();
63  palette.setBrush(QPalette::Base, Qt::transparent);
64  m_ui.webView->setPalette(palette);
65  m_ui.webView->page()->setPalette(palette);
66  m_ui.webView->setAttribute(Qt::WA_OpaquePaintEvent, false);
67  m_ui.webView->setUrl( QUrl( "about:blank" ) );
68 
69  connect( m_ui.webView, SIGNAL(titleChanged(QString)), m_ui.titleText, SLOT(setText(QString)) );
70  connect( m_ui.webView, SIGNAL(urlChanged(QUrl)), this, SLOT(updateBackButton()) );
71  connect( m_ui.hideButton, SIGNAL(clicked()), this, SIGNAL(hide()) );
72 
73  // Update the popupitem on changes while loading the webpage
74  connect( m_ui.webView->page(), SIGNAL(repaintRequested(QRect)), this, SLOT(requestUpdate()) );
75 }
76 
77 PopupItem::~PopupItem()
78 {
79  delete m_widget;
80 }
81 
82 bool PopupItem::isPrintButtonVisible() const
83 {
84  return m_ui.printButton->isVisible();
85 }
86 
87 void PopupItem::setPrintButtonVisible( bool display )
88 {
89  m_ui.printButton->setVisible( display );
90 }
91 
92 void PopupItem::setUrl( const QUrl &url )
93 {
94  m_ui.webView->setUrl( url );
95  setVisible( true );
96 
97  QPalette palette = m_ui.webView->palette();
98  palette.setBrush(QPalette::Base, Qt::transparent);
99  m_ui.webView->setPalette(palette);
100  m_ui.webView->page()->setPalette(palette);
101  m_ui.webView->setAttribute(Qt::WA_OpaquePaintEvent, false);
102 
103  requestUpdate();
104 }
105 
106 void PopupItem::setContent( const QString &html, const QUrl &baseUrl )
107 {
108  m_content = html;
109  m_baseUrl = baseUrl;
110  m_ui.webView->setHtml( html, baseUrl );
111 
112  requestUpdate();
113 }
114 
115 void PopupItem::setTextColor(const QColor &color)
116 {
117  if(color.isValid() && m_ui.titleText != 0) {
118  m_textColor = color;
119  QPalette palette(m_ui.titleText->palette());
120  palette.setColor(QPalette::WindowText, m_textColor);
121  m_ui.titleText->setPalette(palette);
122 
123  requestUpdate();
124  }
125 }
126 
127 void PopupItem::setBackgroundColor(const QColor &color)
128 {
129  if(color.isValid()) {
130  m_backColor = color;
131  QPixmapCache::remove( "marble/webpopup/webpopup2" );
132  QPixmapCache::remove( "marble/webpopup/arrow2_topleft" );
133  QPixmapCache::remove( "marble/webpopup/arrow2_bottomleft" );
134  QPixmapCache::remove( "marble/webpopup/arrow2_topright" );
135  QPixmapCache::remove( "marble/webpopup/arrow2_bottomright" );
136 
137  requestUpdate();
138  }
139 }
140 
141 void PopupItem::colorize( QImage &img, const QColor &col )
142 {
143  if (img.depth() <= 8) return;
144  int pixels = img.width()*img.height();
145  unsigned int *data = (unsigned int *) img.bits();
146  for (int i=0; i < pixels; ++i) {
147  int val = qGray(data[i]);
148  data[i] = qRgba(col.red()*val/255,col.green()*val/255, col.blue()*val/255, qAlpha(data[i]));
149  }
150 }
151 
152 void PopupItem::paint( QPainter *painter )
153 {
154  QRect popupRect;
155  QPixmap image = pixmap("marble/webpopup/arrow2_vertical_topright");
156 
157  if ( alignment() & Qt::AlignRight ) {
158  popupRect.setRect( image.width() - 13, -10,
159  size().width() - ( image.width() - 23 ),
160  size().height() + 20 );
161  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
162  pixmap("marble/webpopup/webpopup2"));
163  if ( alignment() & Qt::AlignTop ) {
164  image = pixmap("marble/webpopup/arrow2_bottomleft");
165  painter->drawPixmap( 0, size().height() - image.height(), image );
166  } else if ( alignment() & Qt::AlignBottom ) {
167  image = pixmap("marble/webpopup/arrow2_topleft");
168  painter->drawPixmap( 0, 0, image );
169  } else { // for no horizontal align value and Qt::AlignVCenter
170  image = pixmap("marble/webpopup/arrow2_topleft");
171  painter->drawPixmap( 0, size().height() / 2, image );
172  }
173  m_widget->render( painter, QPoint( image.width() - 3, 0 ), QRegion() );
174  } else if ( alignment() & Qt::AlignLeft ) {
175  popupRect.setRect( -10, -10,
176  size().width() - ( image.width() - 23 ),
177  size().height() + 20 );
178  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
179  pixmap("marble/webpopup/webpopup2"));
180  if ( alignment() & Qt::AlignTop ) {
181  image = pixmap("marble/webpopup/arrow2_bottomright");
182  painter->drawPixmap( size().width() - image.width(),
183  size().height() - image.height(), image );
184  } else if ( alignment() & Qt::AlignBottom ) {
185  image = pixmap("marble/webpopup/arrow2_topright");
186  painter->drawPixmap( size().width() - image.width(),
187  0, image );
188  } else { // for no horizontal align value and Qt::AlignVCenter
189  image = pixmap("marble/webpopup/arrow2_topright");
190  painter->drawPixmap( size().width() - image.width(),
191  size().height() / 2 - image.height() / 2 + 23, image );
192  }
193  m_widget->render( painter, QPoint( 5, 0 ), QRegion() );
194  } else if ( alignment() & Qt::AlignHCenter )
195  {
196  if ( alignment() & Qt::AlignTop )
197  {
198  image = pixmap("marble/webpopup/arrow2_vertical_bottomright");
199  popupRect.setRect( -10, -10, size().width() + 20,
200  size().height() - image.height() + 23 );
201  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
202  pixmap("marble/webpopup/webpopup2"));
203  painter->drawPixmap( size().width() / 2 - image.width(),
204  size().height() - image.height(), image );
205  m_widget->render( painter, QPoint( 0, 0 ), QRegion() );
206  } else if ( alignment() & Qt::AlignBottom ) {
207  image = pixmap("marble/webpopup/arrow2_vertical_topleft");
208  popupRect.setRect( -10, image.height() - 13, size().width() + 20,
209  size().height() - image.height() + 23 );
210  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
211  pixmap("marble/webpopup/webpopup2"));
212  painter->drawPixmap( size().width() / 2, 0, image );
213  m_widget->render( painter, QPoint( 5, image.height() - 7 ), QRegion() );
214  } else { // for no horizontal align value and Qt::AlignVCenter
215  popupRect.setRect( -10, -10, size().width() + 20,
216  size().height() + 20 );
217  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
218  pixmap("marble/webpopup/webpopup2"));
219  m_widget->render( painter, QPoint( 0, 0 ), QRegion() );
220  }
221  }
222  m_widget->setFixedSize( popupRect.width() - 20,
223  popupRect.height() - 20 );
224 }
225 
226 bool PopupItem::eventFilter( QObject *object, QEvent *e )
227 {
228  MarbleWidget *widget = dynamic_cast<MarbleWidget*> ( object );
229  if ( !widget ) {
230  return BillboardGraphicsItem::eventFilter( object, e );
231  }
232 
233  if ( e->type() == QEvent::ContextMenu) {
234  QApplication::sendEvent( m_ui.webView, e );
235  return BillboardGraphicsItem::eventFilter( object, e );
236  }
237 
238  if ( e->type() == QEvent::KeyPress ) {
239  QApplication::sendEvent( m_ui.webView, e );
240  return BillboardGraphicsItem::eventFilter( object, e );
241  }
242 
243  if ( e->type() == QEvent::MouseButtonDblClick
244  || e->type() == QEvent::MouseMove
245  || e->type() == QEvent::MouseButtonPress
246  || e->type() == QEvent::MouseButtonRelease )
247  {
248  // Mouse events are forwarded to the underlying widget
249  QMouseEvent *event = static_cast<QMouseEvent*> ( e );
250  QPoint shiftedPos = event->pos();
251  QWidget* child = transform( shiftedPos );
252  bool const forcedMouseRelease = m_needMouseRelease && e->type() == QEvent::MouseButtonRelease;
253  if ( child || forcedMouseRelease ) {
254  if ( !m_needMouseRelease && e->type() == QEvent::MouseButtonPress ) {
255  m_needMouseRelease = true;
256  } else if ( forcedMouseRelease ) {
257  m_needMouseRelease = false;
258  }
259  if ( !child ) {
260  child = m_ui.webView;
261  }
262  QMouseEvent shiftedEvent = QMouseEvent( e->type(), shiftedPos,
263  event->globalPos(), event->button(), event->buttons(),
264  event->modifiers() );
265  if ( QApplication::sendEvent( child, &shiftedEvent ) ) {
266  widget->setCursor( child->cursor() );
267  emit repaintNeeded();
268  return true;
269  }
270  }
271  } else if ( e->type() == QEvent::Wheel ) {
272  // Wheel events are forwarded to the underlying widget
273  QWheelEvent *event = static_cast<QWheelEvent*> ( e );
274  QPoint shiftedPos = event->pos();
275  QWidget* child = transform( shiftedPos );
276  if ( child ) {
277  QWheelEvent shiftedEvent = QWheelEvent( shiftedPos,
278  event->globalPos(), event->delta(), event->buttons(),
279  event->modifiers() );
280  if ( QApplication::sendEvent( child, &shiftedEvent ) ) {
281  widget->setCursor( child->cursor() );
282  emit repaintNeeded();
283  return true;
284  }
285  }
286  }
287 
288  return BillboardGraphicsItem::eventFilter( object, e );
289 }
290 
291 QWidget* PopupItem::transform( QPoint &point ) const
292 {
293  /*
294  * Fixes for mouse events to trigger when the web popup
295  * is shifted in accordance with the horizontal alignment
296  */
297  if ( alignment() & Qt::AlignRight )
298  point -= QPoint( 117, 0 );
299  else if ( alignment() & Qt::AlignLeft )
300  point -= QPoint( 5, 0 );
301  else if ( alignment() & Qt::AlignHCenter )
302  {
303  if ( alignment() & Qt::AlignTop )
304  {
305  point -= QPoint( 0, 0 );
306  } else if ( alignment() & Qt::AlignBottom )
307  {
308  point-= QPoint( 5, 57 );
309  } else {
310  point -= QPoint( 0, 0 );
311  }
312  }
313 
314  QList<QPointF> widgetPositions = positions();
315  QList<QPointF>::const_iterator it = widgetPositions.constBegin();
316  for( ; it != widgetPositions.constEnd(); ++it ) {
317  if ( QRectF( *it, size() ).contains( point ) ) {
318  point -= it->toPoint();
319  QWidget* child = m_widget->childAt( point );
320  if ( child ) {
321  point -= child->pos();
322  }
323  return child;
324  }
325  }
326  return 0;
327 }
328 
329 void PopupItem::clearHistory()
330 {
331  m_content.clear();
332  m_ui.webView->setUrl( QUrl( "about:blank" ) );
333  m_ui.webView->history()->clear();
334 }
335 
336 void PopupItem::requestUpdate()
337 {
338  update();
339  emit repaintNeeded();
340 }
341 
342 void PopupItem::printContent() const
343 {
344 #ifndef QT_NO_PRINTER
345  QPrinter printer;
346  QPointer<QPrintDialog> dialog = new QPrintDialog(&printer);
347  if (dialog->exec() == QPrintDialog::Accepted) {
348  m_ui.webView->print(&printer);
349  }
350  delete dialog;
351 #endif
352 }
353 
354 void PopupItem::updateBackButton()
355 {
356  bool const hasHistory = m_ui.webView->history()->count() > 1;
357  bool const previousIsHtml = !m_content.isEmpty() && m_ui.webView->history()->currentItemIndex() == 1;
358  bool const atStart = m_ui.webView->history()->currentItemIndex() <= 1;
359  bool const currentIsHtml = m_ui.webView->url() == QUrl( "about:blank" );
360  m_ui.goBackButton->setVisible( hasHistory && !currentIsHtml && ( previousIsHtml || !atStart ) );
361 }
362 
363 void PopupItem::goBack()
364 {
365  if ( m_ui.webView->history()->currentItemIndex() == 1 && !m_content.isEmpty() ) {
366  m_ui.webView->setHtml( m_content, m_baseUrl );
367  } else {
368  m_ui.webView->back();
369  }
370  updateBackButton();
371 }
372 
373 QPixmap PopupItem::pixmap( const QString &imageId ) const
374 {
375  QPixmap result;
376  if ( !QPixmapCache::find( imageId, result ) ) {
377  QImage bottom = QImage( QString( ":/%1_shadow.png" ).arg( imageId) );
378  QImage top = QImage( QString( ":/%1.png" ).arg( imageId) );
379  colorize( top, m_backColor );
380  QPainter painter( &bottom );
381  painter.drawImage( QPoint(0,0), top );
382 
383  result = QPixmap::fromImage( bottom );
384  QPixmapCache::insert( imageId, result );
385  }
386 
387  return result;
388 }
389 
390 }
391 
392 #include "PopupItem.moc"
QObject::child
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QPalette::setBrush
void setBrush(ColorRole role, const QBrush &brush)
QEvent
QWidget
QEvent::type
Type type() const
QPixmap::width
int width() const
QWidget::setCursor
void setCursor(const QCursor &)
Marble::BillboardGraphicsItem
Base class for all 2D labels (a.k.a.
Definition: BillboardGraphicsItem.h:32
Marble::MarbleGraphicsItem::setSize
void setSize(const QSizeF &size)
Set the size of the item.
Definition: MarbleGraphicsItem.cpp:197
QPalette::setColor
void setColor(ColorGroup group, ColorRole role, const QColor &color)
QPixmapCache::remove
void remove(const QString &key)
Marble::PopupItem::repaintNeeded
void repaintNeeded()
QPrinter
Marble::BillboardGraphicsItem::positions
QList< QPointF > positions() const
Returns the absolute screen positions of the item.
Definition: BillboardGraphicsItem.cpp:103
MarbleWebView.h
QImage::depth
int depth() const
Marble::PopupItem::setUrl
void setUrl(const QUrl &url)
Set URL for web window.
Definition: PopupItem.cpp:92
Marble::PopupItem::setTextColor
void setTextColor(const QColor &color)
Sets text color of the header.
Definition: PopupItem.cpp:115
QWidget::childAt
QWidget * childAt(int x, int y) const
QPointer
QWheelEvent
QWidget::setAttribute
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QRect::height
int height() const
PopupItem.h
Marble::MarbleGraphicsItem::size
QSizeF size() const
Returns the size of the item.
Definition: MarbleGraphicsItem.cpp:136
QPoint
QMouseEvent
Marble::PopupItem::setPrintButtonVisible
void setPrintButtonVisible(bool display)
Sets visibility of the print button.
Definition: PopupItem.cpp:87
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:104
QObject::event
virtual bool event(QEvent *e)
QString::clear
void clear()
QRect
QPrintDialog
QObject
QColor::red
int red() const
QImage::width
int width() const
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
QString::isEmpty
bool isEmpty() const
QCoreApplication::sendEvent
bool sendEvent(QObject *receiver, QEvent *event)
Marble::MarbleGraphicsItem::ItemCoordinateCache
Definition: MarbleGraphicsItem.h:41
QWidget::pos
pos
Marble::PopupItem::PopupItem
PopupItem(QObject *parent=0)
Definition: PopupItem.cpp:37
Marble::PopupItem::eventFilter
virtual bool eventFilter(QObject *, QEvent *e)
Definition: PopupItem.cpp:226
Marble::PopupItem::clearHistory
void clearHistory()
Definition: PopupItem.cpp:329
QString
QList< QPointF >
QColor
Marble::PopupItem::isPrintButtonVisible
bool isPrintButtonVisible() const
Print button visibility indicator.
Definition: PopupItem.cpp:82
Marble::BillboardGraphicsItem::alignment
Qt::Alignment alignment() const
Definition: BillboardGraphicsItem.cpp:128
QPixmap
QColor::green
int green() const
Marble::PopupItem::paint
void paint(QPainter *painter)
Paints the item in item coordinates.
Definition: PopupItem.cpp:152
QUrl
QWidget::setFixedSize
void setFixedSize(const QSize &s)
Marble::PopupItem::hide
void hide()
QPixmap::height
int height() const
Marble::PopupItem::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets background color of the bubble.
Definition: PopupItem.cpp:127
QImage
QColor::blue
int blue() const
QRect::width
int width() const
QSizeF
Marble::MarbleGraphicsItem::setCacheMode
void setCacheMode(CacheMode mode)
Set the cache mode of the item.
Definition: MarbleGraphicsItem.cpp:159
QRect::setRect
void setRect(int x, int y, int width, int height)
QRectF
QMargins
Marble::MarbleGraphicsItem::setVisible
void setVisible(bool visible)
Makes the item visible or invisible, depending on visible.
Definition: MarbleGraphicsItem.cpp:182
QPixmapCache::find
QPixmap * find(const QString &key)
Marble::MarbleGraphicsItem::update
void update()
Marks the item and all parent items as invalid.
Definition: MarbleGraphicsItem.cpp:167
QImage::bits
uchar * bits()
MarbleWidget.h
This file contains the headers for MarbleWidget.
QImage::height
int height() const
QRectF::contains
bool contains(const QPointF &point) const
Marble::PopupItem::setContent
void setContent(const QString &html, const QUrl &baseUrl=QUrl())
Set content of the popup.
Definition: PopupItem.cpp:106
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QSizeF::height
qreal height() const
QPixmapCache::insert
bool insert(const QString &key, const QPixmap &pixmap)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Marble::MarbleGraphicsItem::eventFilter
virtual bool eventFilter(QObject *object, QEvent *e)
Definition: MarbleGraphicsItem.cpp:225
QSizeF::width
qreal width() const
QPalette
QRegion
QWidget::render
void render(QPaintDevice *target, const QPoint &targetOffset, const QRegion &sourceRegion, QFlags< QWidget::RenderFlag > renderFlags)
QColor::isValid
bool isValid() const
Marble::PopupItem::~PopupItem
~PopupItem()
Definition: PopupItem.cpp:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal