20 #include <QWebHistory>
22 #include <QPrintDialog>
23 #include <QMouseEvent>
24 #include <QApplication>
25 #include <QVBoxLayout>
27 #include <QPushButton>
28 #include <QPixmapCache>
32 #include <qdrawutil.h>
41 m_textColor( QColor(Qt::black) ),
42 m_backColor( QColor(Qt::white) ),
43 m_needMouseRelease(false)
47 setSize( QSizeF( 240.0, 320.0 ) );
49 m_ui.setupUi( m_widget );
51 m_ui.goBackButton->setVisible(
false );
52 connect( m_ui.goBackButton, SIGNAL(clicked()),
this, SLOT(goBack()) );
55 m_ui.printButton->setVisible(
false );
57 m_ui.printButton->setVisible(
true );
58 connect( m_ui.printButton, SIGNAL(clicked()),
this, SLOT(printContent()) );
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" ) );
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()) );
74 connect( m_ui.webView->page(), SIGNAL(repaintRequested(QRect)),
this, SLOT(requestUpdate()) );
84 return m_ui.printButton->isVisible();
89 m_ui.printButton->setVisible( display );
94 m_ui.webView->setUrl( url );
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);
109 m_ui.webView->setHtml( html );
116 if(color.isValid() && m_ui.titleText != 0) {
118 QPalette palette(m_ui.titleText->palette());
119 palette.setColor(QPalette::WindowText, m_textColor);
120 m_ui.titleText->setPalette(palette);
126 if(color.isValid()) {
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" );
136 void PopupItem::colorize( QImage &img,
const QColor &col )
const
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]));
150 QPixmap image = pixmap(
"marble/webpopup/arrow2_vertical_topright");
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"));
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 );
165 image = pixmap(
"marble/webpopup/arrow2_topleft");
166 painter->drawPixmap( 0,
size().height() / 2, image );
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"));
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(),
184 image = pixmap(
"marble/webpopup/arrow2_topright");
185 painter->drawPixmap(
size().width() - image.width(),
186 size().height() / 2 - image.height() / 2 + 23, image );
188 m_widget->render( painter, QPoint( 5, 0 ), QRegion() );
189 }
else if (
alignment() & Qt::AlignHCenter )
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() );
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() );
217 m_widget->setFixedSize( popupRect.width() - 20,
218 popupRect.height() - 20 );
228 if ( e->type() == QEvent::ContextMenu) {
229 QApplication::sendEvent( m_ui.webView, e );
233 if ( e->type() == QEvent::KeyPress ) {
234 QApplication::sendEvent( m_ui.webView, e );
238 if ( e->type() == QEvent::MouseButtonDblClick
239 || e->type() == QEvent::MouseMove
240 || e->type() == QEvent::MouseButtonPress
241 || e->type() == QEvent::MouseButtonRelease )
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;
255 child = m_ui.webView;
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() );
266 }
else if ( e->type() == QEvent::Wheel ) {
268 QWheelEvent *
event =
static_cast<QWheelEvent*
> ( e );
269 QPoint shiftedPos =
event->pos();
270 QWidget* child = transform( shiftedPos );
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() );
286 QWidget* PopupItem::transform( QPoint &point )
const
293 point -= QPoint( 117, 0 );
295 point -= QPoint( 5, 0 );
296 else if (
alignment() & Qt::AlignHCenter )
300 point -= QPoint( 0, 0 );
301 }
else if (
alignment() & Qt::AlignBottom )
303 point-= QPoint( 5, 57 );
305 point -= QPoint( 0, 0 );
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 );
316 point -= child->pos();
327 m_ui.webView->setUrl( QUrl(
"about:blank" ) );
328 m_ui.webView->history()->clear();
331 void PopupItem::requestUpdate()
337 void PopupItem::printContent()
339 #ifndef QT_NO_PRINTER
341 QPointer<QPrintDialog> dialog =
new QPrintDialog(&printer);
342 if (dialog->exec() == QPrintDialog::Accepted) {
343 m_ui.webView->print(&printer);
349 void PopupItem::updateBackButton()
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 ) );
358 void PopupItem::goBack()
360 if ( m_ui.webView->history()->currentItemIndex() == 1 && !m_content.isEmpty() ) {
361 m_ui.webView->setHtml( m_content );
363 m_ui.webView->back();
368 QPixmap PopupItem::pixmap(
const QString &imageId )
const
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 );
376 painter.drawImage( QPoint(0,0), top );
378 result = QPixmap::fromImage( bottom );
379 QPixmapCache::insert( imageId, result );
387 #include "PopupItem.moc"
Base class for all 2D labels (a.k.a.
void setSize(const QSizeF &size)
Set the size of the item.
QList< QPointF > positions() const
Returns the absolute screen positions of the item.
QSizeF size() const
Returns the size of the item.
Qt::Alignment alignment() const
void setCacheMode(CacheMode mode)
Set the cache mode of the item.
void setVisible(bool visible)
Makes the item visible or invisible, depending on visible.
void update()
Marks the item and all parent items as invalid.
virtual bool eventFilter(QObject *object, QEvent *e)