2
// This file is part of the Marble Virtual Globe.
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
8
// The code in this file is largely based on KDE's KLineEdit class
9
// as included in KDE 4.5. See there for its authors:
10
// http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/klineedit_8cpp.html
12
// Copyright 2010 Dennis Nienhüser <earthwings@gentoo.org>
15
#include "RoutingLineEdit.h"
18
#include <QtGui/QApplication>
19
#include <QtGui/QClipboard>
20
#include <QtGui/QLabel>
21
#include <QtGui/QStyle>
22
#include <QtGui/QMouseEvent>
27
class RoutingLineEditPrivate
30
QLabel* m_clearButton;
32
QPixmap m_clearPixmap;
34
RoutingLineEditPrivate( RoutingLineEdit* parent );
37
RoutingLineEditPrivate::RoutingLineEditPrivate( RoutingLineEdit* parent ) :
38
m_clearButton( new QLabel( parent ) )
40
m_clearButton->setCursor( Qt::ArrowCursor );
41
m_clearButton->setToolTip( QObject::tr( "Clear" ) );
44
RoutingLineEdit::RoutingLineEdit( QWidget *parent ) :
45
QLineEdit( parent ), d( new RoutingLineEditPrivate( this ) )
47
updateClearButtonIcon( text() );
50
// Padding for clear button to avoid text underflow
51
QString const direction = layoutDirection() == Qt::LeftToRight ? "right" : "left";
52
if ( !MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ) {
53
setStyleSheet( QString( ":enabled { padding-%1: %2; }").arg( direction).arg( 18 ) );
56
connect( this, SIGNAL( textChanged( QString ) ), SLOT( updateClearButtonIcon( QString ) ) );
59
RoutingLineEdit::~RoutingLineEdit()
64
void RoutingLineEdit::updateClearButtonIcon( const QString& text )
66
d->m_clearButton->setVisible( text.length() > 0 );
67
if ( d->m_clearButton->pixmap() && !d->m_clearButton->pixmap()->isNull() ) {
71
QString const direction = layoutDirection() == Qt::LeftToRight ? "rtl" : "ltr";
72
int const size = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ? 32 : 16;
73
QPixmap pixmap = QPixmap( QString( ":/icons/%1x%1/edit-clear-locationbar-%2.png").arg( size ).arg( direction ) );
74
d->m_clearButton->setPixmap( pixmap );
77
void RoutingLineEdit::updateClearButton()
79
const QSize geom = size();
80
const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0, this );
81
const int pixmapSize = d->m_clearButton->pixmap()->width();
83
int y = ( geom.height() - pixmapSize ) / 2;
84
if ( layoutDirection() == Qt::LeftToRight ) {
85
d->m_clearButton->move( geom.width() - frameWidth - pixmapSize - 1, y );
87
d->m_clearButton->move( frameWidth + 1, y );
91
void RoutingLineEdit::mouseReleaseEvent( QMouseEvent* e )
93
if ( d->m_clearButton == childAt( e->pos() ) ) {
95
if ( e->button() == Qt::MidButton ) {
96
newText = QApplication::clipboard()->text( QClipboard::Selection );
99
setSelection( 0, text().size() );
101
emit clearButtonClicked();
103
emit textChanged( newText );
106
QLineEdit::mouseReleaseEvent( e );
109
void RoutingLineEdit::resizeEvent( QResizeEvent * event )
112
QLineEdit::resizeEvent( event );
115
} // namespace Marble
117
#include "RoutingLineEdit.moc"