~ubuntu-branches/ubuntu/quantal/marble/quantal

« back to all changes in this revision

Viewing changes to src/lib/routing/RoutingLineEdit.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Kolberg
  • Date: 2012-05-26 15:14:42 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20120526151442-qihwzeki8qlr9ip4
Tags: 4:4.8.80-0ubuntu1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
// 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
11
 
//
12
 
// Copyright 2010      Dennis Nienhüser <earthwings@gentoo.org>
13
 
//
14
 
 
15
 
#include "RoutingLineEdit.h"
16
 
#include "global.h"
17
 
 
18
 
#include <QtGui/QApplication>
19
 
#include <QtGui/QClipboard>
20
 
#include <QtGui/QLabel>
21
 
#include <QtGui/QStyle>
22
 
#include <QtGui/QMouseEvent>
23
 
 
24
 
namespace Marble
25
 
{
26
 
 
27
 
class RoutingLineEditPrivate
28
 
{
29
 
public:
30
 
    QLabel* m_clearButton;
31
 
 
32
 
    QPixmap m_clearPixmap;
33
 
 
34
 
    RoutingLineEditPrivate( RoutingLineEdit* parent );
35
 
};
36
 
 
37
 
RoutingLineEditPrivate::RoutingLineEditPrivate( RoutingLineEdit* parent ) :
38
 
        m_clearButton( new QLabel( parent ) )
39
 
{
40
 
    m_clearButton->setCursor( Qt::ArrowCursor );
41
 
    m_clearButton->setToolTip( QObject::tr( "Clear" ) );
42
 
}
43
 
 
44
 
RoutingLineEdit::RoutingLineEdit( QWidget *parent ) :
45
 
        QLineEdit( parent ), d( new RoutingLineEditPrivate( this ) )
46
 
{
47
 
    updateClearButtonIcon( text() );
48
 
    updateClearButton();
49
 
 
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 ) );
54
 
    }
55
 
 
56
 
    connect( this, SIGNAL( textChanged( QString ) ), SLOT( updateClearButtonIcon( QString ) ) );
57
 
}
58
 
 
59
 
RoutingLineEdit::~RoutingLineEdit()
60
 
{
61
 
    delete d;
62
 
}
63
 
 
64
 
void RoutingLineEdit::updateClearButtonIcon( const QString& text )
65
 
{
66
 
    d->m_clearButton->setVisible( text.length() > 0 );
67
 
    if ( d->m_clearButton->pixmap() && !d->m_clearButton->pixmap()->isNull() ) {
68
 
        return;
69
 
    }
70
 
 
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 );
75
 
}
76
 
 
77
 
void RoutingLineEdit::updateClearButton()
78
 
{
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();
82
 
 
83
 
    int y = ( geom.height() - pixmapSize ) / 2;
84
 
    if ( layoutDirection() == Qt::LeftToRight ) {
85
 
        d->m_clearButton->move( geom.width() - frameWidth - pixmapSize - 1, y );
86
 
    } else {
87
 
        d->m_clearButton->move( frameWidth + 1, y );
88
 
    }
89
 
}
90
 
 
91
 
void RoutingLineEdit::mouseReleaseEvent( QMouseEvent* e )
92
 
{
93
 
    if ( d->m_clearButton == childAt( e->pos() ) ) {
94
 
        QString newText;
95
 
        if ( e->button() == Qt::MidButton ) {
96
 
            newText = QApplication::clipboard()->text( QClipboard::Selection );
97
 
            setText( newText );
98
 
        } else {
99
 
            setSelection( 0, text().size() );
100
 
            del();
101
 
            emit clearButtonClicked();
102
 
        }
103
 
        emit textChanged( newText );
104
 
    }
105
 
 
106
 
    QLineEdit::mouseReleaseEvent( e );
107
 
}
108
 
 
109
 
void RoutingLineEdit::resizeEvent( QResizeEvent * event )
110
 
{
111
 
    updateClearButton();
112
 
    QLineEdit::resizeEvent( event );
113
 
}
114
 
 
115
 
} // namespace Marble
116
 
 
117
 
#include "RoutingLineEdit.moc"