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

marble

  • sources
  • kde-4.12
  • 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 )
107 {
108  m_content = html;
109  m_ui.webView->setHtml( html );
110 
111  requestUpdate();
112 }
113 
114 void PopupItem::setTextColor(const QColor &color)
115 {
116  if(color.isValid() && m_ui.titleText != 0) {
117  m_textColor = color;
118  QPalette palette(m_ui.titleText->palette());
119  palette.setColor(QPalette::WindowText, m_textColor);
120  m_ui.titleText->setPalette(palette);
121  }
122 }
123 
124 void PopupItem::setBackgroundColor(const QColor &color)
125 {
126  if(color.isValid()) {
127  m_backColor = color;
128  QPixmapCache::remove( "marble/webpopup/webpopup2" );
129  QPixmapCache::remove( "marble/webpopup/arrow2_topleft" );
130  QPixmapCache::remove( "marble/webpopup/arrow2_bottomleft" );
131  QPixmapCache::remove( "marble/webpopup/arrow2_topright" );
132  QPixmapCache::remove( "marble/webpopup/arrow2_bottomright" );
133  }
134 }
135 
136 void PopupItem::colorize( QImage &img, const QColor &col ) const
137 {
138  if (img.depth() <= 8) return;
139  int pixels = img.width()*img.height();
140  unsigned int *data = (unsigned int *) img.bits();
141  for (int i=0; i < pixels; ++i) {
142  int val = qGray(data[i]);
143  data[i] = qRgba(col.red()*val/255,col.green()*val/255, col.blue()*val/255, qAlpha(data[i]));
144  }
145 }
146 
147 void PopupItem::paint( QPainter *painter )
148 {
149  QRect popupRect;
150  QPixmap image = pixmap("marble/webpopup/arrow2_vertical_topright");
151 
152  if ( alignment() & Qt::AlignRight ) {
153  popupRect.setRect( image.width() - 13, -10,
154  size().width() - ( image.width() - 23 ),
155  size().height() + 20 );
156  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
157  pixmap("marble/webpopup/webpopup2"));
158  if ( alignment() & Qt::AlignTop ) {
159  image = pixmap("marble/webpopup/arrow2_bottomleft");
160  painter->drawPixmap( 0, size().height() - image.height(), image );
161  } else if ( alignment() & Qt::AlignBottom ) {
162  image = pixmap("marble/webpopup/arrow2_topleft");
163  painter->drawPixmap( 0, 0, image );
164  } else { // for no horizontal align value and Qt::AlignVCenter
165  image = pixmap("marble/webpopup/arrow2_topleft");
166  painter->drawPixmap( 0, size().height() / 2, image );
167  }
168  m_widget->render( painter, QPoint( image.width() - 3, 0 ), QRegion() );
169  } else if ( alignment() & Qt::AlignLeft ) {
170  popupRect.setRect( -10, -10,
171  size().width() - ( image.width() - 23 ),
172  size().height() + 20 );
173  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
174  pixmap("marble/webpopup/webpopup2"));
175  if ( alignment() & Qt::AlignTop ) {
176  image = pixmap("marble/webpopup/arrow2_bottomright");
177  painter->drawPixmap( size().width() - image.width(),
178  size().height() - image.height(), image );
179  } else if ( alignment() & Qt::AlignBottom ) {
180  image = pixmap("marble/webpopup/arrow2_topright");
181  painter->drawPixmap( size().width() - image.width(),
182  0, image );
183  } else { // for no horizontal align value and Qt::AlignVCenter
184  image = pixmap("marble/webpopup/arrow2_topright");
185  painter->drawPixmap( size().width() - image.width(),
186  size().height() / 2 - image.height() / 2 + 23, image );
187  }
188  m_widget->render( painter, QPoint( 5, 0 ), QRegion() );
189  } else if ( alignment() & Qt::AlignHCenter )
190  {
191  if ( alignment() & Qt::AlignTop )
192  {
193  image = pixmap("marble/webpopup/arrow2_vertical_bottomright");
194  popupRect.setRect( -10, -10, size().width() + 20,
195  size().height() - image.height() + 23 );
196  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
197  pixmap("marble/webpopup/webpopup2"));
198  painter->drawPixmap( size().width() / 2 - image.width(),
199  size().height() - image.height(), image );
200  m_widget->render( painter, QPoint( 0, 0 ), QRegion() );
201  } else if ( alignment() & Qt::AlignBottom ) {
202  image = pixmap("marble/webpopup/arrow2_vertical_topleft");
203  popupRect.setRect( -10, image.height() - 13, size().width() + 20,
204  size().height() - image.height() + 23 );
205  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
206  pixmap("marble/webpopup/webpopup2"));
207  painter->drawPixmap( size().width() / 2, 0, image );
208  m_widget->render( painter, QPoint( 5, image.height() - 7 ), QRegion() );
209  } else { // for no horizontal align value and Qt::AlignVCenter
210  popupRect.setRect( -10, -10, size().width() + 20,
211  size().height() + 20 );
212  qDrawBorderPixmap(painter, popupRect, QMargins( 20, 20, 20, 20 ),
213  pixmap("marble/webpopup/webpopup2"));
214  m_widget->render( painter, QPoint( 0, 0 ), QRegion() );
215  }
216  }
217  m_widget->setFixedSize( popupRect.width() - 20,
218  popupRect.height() - 20 );
219 }
220 
221 bool PopupItem::eventFilter( QObject *object, QEvent *e )
222 {
223  MarbleWidget *widget = dynamic_cast<MarbleWidget*> ( object );
224  if ( !widget ) {
225  return BillboardGraphicsItem::eventFilter( object, e );
226  }
227 
228  if ( e->type() == QEvent::ContextMenu) {
229  QApplication::sendEvent( m_ui.webView, e );
230  return BillboardGraphicsItem::eventFilter( object, e );
231  }
232 
233  if ( e->type() == QEvent::KeyPress ) {
234  QApplication::sendEvent( m_ui.webView, e );
235  return BillboardGraphicsItem::eventFilter( object, e );
236  }
237 
238  if ( e->type() == QEvent::MouseButtonDblClick
239  || e->type() == QEvent::MouseMove
240  || e->type() == QEvent::MouseButtonPress
241  || e->type() == QEvent::MouseButtonRelease )
242  {
243  // Mouse events are forwarded to the underlying widget
244  QMouseEvent *event = static_cast<QMouseEvent*> ( e );
245  QPoint shiftedPos = event->pos();
246  QWidget* child = transform( shiftedPos );
247  bool const forcedMouseRelease = m_needMouseRelease && e->type() == QEvent::MouseButtonRelease;
248  if ( child || forcedMouseRelease ) {
249  if ( !m_needMouseRelease && e->type() == QEvent::MouseButtonPress ) {
250  m_needMouseRelease = true;
251  } else if ( forcedMouseRelease ) {
252  m_needMouseRelease = false;
253  }
254  if ( !child ) {
255  child = m_ui.webView;
256  }
257  QMouseEvent shiftedEvent = QMouseEvent( e->type(), shiftedPos,
258  event->globalPos(), event->button(), event->buttons(),
259  event->modifiers() );
260  if ( QApplication::sendEvent( child, &shiftedEvent ) ) {
261  widget->setCursor( child->cursor() );
262  emit repaintNeeded();
263  return true;
264  }
265  }
266  } else if ( e->type() == QEvent::Wheel ) {
267  // Wheel events are forwarded to the underlying widget
268  QWheelEvent *event = static_cast<QWheelEvent*> ( e );
269  QPoint shiftedPos = event->pos();
270  QWidget* child = transform( shiftedPos );
271  if ( child ) {
272  QWheelEvent shiftedEvent = QWheelEvent( shiftedPos,
273  event->globalPos(), event->delta(), event->buttons(),
274  event->modifiers() );
275  if ( QApplication::sendEvent( child, &shiftedEvent ) ) {
276  widget->setCursor( child->cursor() );
277  emit repaintNeeded();
278  return true;
279  }
280  }
281  }
282 
283  return BillboardGraphicsItem::eventFilter( object, e );
284 }
285 
286 QWidget* PopupItem::transform( QPoint &point ) const
287 {
288  /*
289  * Fixes for mouse events to trigger when the web popup
290  * is shifted in accordance with the horizontal alignment
291  */
292  if ( alignment() & Qt::AlignRight )
293  point -= QPoint( 117, 0 );
294  else if ( alignment() & Qt::AlignLeft )
295  point -= QPoint( 5, 0 );
296  else if ( alignment() & Qt::AlignHCenter )
297  {
298  if ( alignment() & Qt::AlignTop )
299  {
300  point -= QPoint( 0, 0 );
301  } else if ( alignment() & Qt::AlignBottom )
302  {
303  point-= QPoint( 5, 57 );
304  } else {
305  point -= QPoint( 0, 0 );
306  }
307  }
308 
309  QList<QPointF> widgetPositions = positions();
310  QList<QPointF>::const_iterator it = widgetPositions.constBegin();
311  for( ; it != widgetPositions.constEnd(); ++it ) {
312  if ( QRectF( *it, size() ).contains( point ) ) {
313  point -= it->toPoint();
314  QWidget* child = m_widget->childAt( point );
315  if ( child ) {
316  point -= child->pos();
317  }
318  return child;
319  }
320  }
321  return 0;
322 }
323 
324 void PopupItem::clearHistory()
325 {
326  m_content.clear();
327  m_ui.webView->setUrl( QUrl( "about:blank" ) );
328  m_ui.webView->history()->clear();
329 }
330 
331 void PopupItem::requestUpdate()
332 {
333  update();
334  emit repaintNeeded();
335 }
336 
337 void PopupItem::printContent()
338 {
339 #ifndef QT_NO_PRINTER
340  QPrinter printer;
341  QPointer<QPrintDialog> dialog = new QPrintDialog(&printer);
342  if (dialog->exec() == QPrintDialog::Accepted) {
343  m_ui.webView->print(&printer);
344  }
345  delete dialog;
346 #endif
347 }
348 
349 void PopupItem::updateBackButton()
350 {
351  bool const hasHistory = m_ui.webView->history()->count() > 1;
352  bool const previousIsHtml = !m_content.isEmpty() && m_ui.webView->history()->currentItemIndex() == 1;
353  bool const atStart = m_ui.webView->history()->currentItemIndex() <= 1;
354  bool const currentIsHtml = m_ui.webView->url() == QUrl( "about:blank" );
355  m_ui.goBackButton->setVisible( hasHistory && !currentIsHtml && ( previousIsHtml || !atStart ) );
356 }
357 
358 void PopupItem::goBack()
359 {
360  if ( m_ui.webView->history()->currentItemIndex() == 1 && !m_content.isEmpty() ) {
361  m_ui.webView->setHtml( m_content );
362  } else {
363  m_ui.webView->back();
364  }
365  updateBackButton();
366 }
367 
368 QPixmap PopupItem::pixmap( const QString &imageId ) const
369 {
370  QPixmap result;
371  if ( !QPixmapCache::find( imageId, result ) ) {
372  QImage bottom = QImage( QString( ":/%1_shadow.png" ).arg( imageId) );
373  QImage top = QImage( QString( ":/%1.png" ).arg( imageId) );
374  colorize( top, m_backColor );
375  QPainter painter( &bottom );
376  painter.drawImage( QPoint(0,0), top );
377 
378  result = QPixmap::fromImage( bottom );
379  QPixmapCache::insert( imageId, result );
380  }
381 
382  return result;
383 }
384 
385 }
386 
387 #include "PopupItem.moc"
QPainter
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
Marble::PopupItem::repaintNeeded
void repaintNeeded()
Marble::BillboardGraphicsItem::positions
QList< QPointF > positions() const
Returns the absolute screen positions of the item.
Definition: BillboardGraphicsItem.cpp:103
MarbleWebView.h
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:114
QWidget
PopupItem.h
Marble::MarbleGraphicsItem::size
QSizeF size() const
Returns the size of the item.
Definition: MarbleGraphicsItem.cpp:136
QObject
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:102
Marble::PopupItem::setContent
void setContent(const QString &html)
Set content of the popup.
Definition: PopupItem.cpp:106
Marble::MarbleGraphicsItem::ItemCoordinateCache
Definition: MarbleGraphicsItem.h:41
Marble::PopupItem::PopupItem
PopupItem(QObject *parent=0)
Definition: PopupItem.cpp:37
Marble::PopupItem::eventFilter
virtual bool eventFilter(QObject *, QEvent *e)
Definition: PopupItem.cpp:221
Marble::PopupItem::clearHistory
void clearHistory()
Definition: PopupItem.cpp:324
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
Marble::PopupItem::paint
void paint(QPainter *painter)
Paints the item in item coordinates.
Definition: PopupItem.cpp:147
Marble::PopupItem::hide
void hide()
Marble::PopupItem::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets background color of the bubble.
Definition: PopupItem.cpp:124
Marble::MarbleGraphicsItem::setCacheMode
void setCacheMode(CacheMode mode)
Set the cache mode of the item.
Definition: MarbleGraphicsItem.cpp:159
Marble::MarbleGraphicsItem::setVisible
void setVisible(bool visible)
Makes the item visible or invisible, depending on visible.
Definition: MarbleGraphicsItem.cpp:182
Marble::MarbleGraphicsItem::update
void update()
Marks the item and all parent items as invalid.
Definition: MarbleGraphicsItem.cpp:167
MarbleWidget.h
This file contains the headers for MarbleWidget.
Marble::MarbleGraphicsItem::eventFilter
virtual bool eventFilter(QObject *object, QEvent *e)
Definition: MarbleGraphicsItem.cpp:225
Marble::PopupItem::~PopupItem
~PopupItem()
Definition: PopupItem.cpp:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:52 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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