MarbleLineEdit.cpp 6.6 KB
Newer Older
Dennis Nienhüser's avatar
Dennis Nienhüser committed
1
2
3
4
5
6
7
8
9
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// The code in this file is largely based on KDE's KLineEdit class
// as included in KDE 4.5. See there for its authors:
10
// https://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/klineedit_8cpp.html
Dennis Nienhüser's avatar
Dennis Nienhüser committed
11
//
Dennis Nienhüser's avatar
Dennis Nienhüser committed
12
// Copyright 2010,2012      Dennis Nienhüser <nienhueser@kde.org>
Dennis Nienhüser's avatar
Dennis Nienhüser committed
13
14
15
//

#include "MarbleLineEdit.h"
16
#include "MarbleGlobal.h"
Dennis Nienhüser's avatar
Dennis Nienhüser committed
17

18
19
20
21
22
23
24
#include <QApplication>
#include <QClipboard>
#include <QLabel>
#include <QStyle>
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
Dennis Nienhüser's avatar
Dennis Nienhüser committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

namespace Marble
{

class MarbleLineEditPrivate
{
public:
    QLabel* m_clearButton;

    QLabel* m_decoratorButton;

    QPixmap m_clearPixmap;

    QPixmap m_decoratorPixmap;

    QTimer m_progressTimer;

    QVector<QPixmap> m_progressAnimation;

    int m_currentFrame;

    int m_iconSize;

48
    explicit MarbleLineEditPrivate( MarbleLineEdit* parent );
Dennis Nienhüser's avatar
Dennis Nienhüser committed
49
50
51
52
53
54

    void createProgressAnimation();
};

MarbleLineEditPrivate::MarbleLineEditPrivate( MarbleLineEdit* parent ) :
    m_clearButton( new QLabel( parent ) ), m_decoratorButton( new QLabel( parent ) ),
55
    m_currentFrame( 0 ), m_iconSize( 16 )
Dennis Nienhüser's avatar
Dennis Nienhüser committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
    m_clearButton->setCursor( Qt::ArrowCursor );
    m_clearButton->setToolTip( QObject::tr( "Clear" ) );
    m_decoratorButton->setCursor( Qt::ArrowCursor );
    createProgressAnimation();
    m_progressTimer.setInterval( 100 );
    if ( MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ) {
        m_iconSize = 32;
    }
}

void MarbleLineEditPrivate::createProgressAnimation()
{
    // Size parameters
    qreal const h = m_iconSize / 2.0; // Half of the icon size
    qreal const q = h / 2.0; // Quarter of the icon size
    qreal const d = 7.5; // Circle diameter
    qreal const r = d / 2.0; // Circle radius

    // Canvas parameters
    QImage canvas( m_iconSize, m_iconSize, QImage::Format_ARGB32 );
    QPainter painter( &canvas );
    painter.setRenderHint( QPainter::Antialiasing, true );
    painter.setPen( QColor ( Qt::gray ) );
    painter.setBrush( QColor( Qt::white ) );

    // Create all frames
    for( double t = 0.0; t < 2 * M_PI; t += M_PI / 8.0 ) {
        canvas.fill( Qt::transparent );
        QRectF firstCircle( h - r + q * cos( t ), h - r + q * sin( t ), d, d );
        QRectF secondCircle( h - r + q * cos( t + M_PI ), h - r + q * sin( t + M_PI ), d, d );
        painter.drawEllipse( firstCircle );
        painter.drawEllipse( secondCircle );
        m_progressAnimation.push_back( QPixmap::fromImage( canvas ) );
    }
}

MarbleLineEdit::MarbleLineEdit( QWidget *parent ) :
        QLineEdit( parent ), d( new MarbleLineEditPrivate( this ) )
{
    updateClearButtonIcon( text() );
    updateClearButton();

    setDecorator( d->m_decoratorPixmap );
100
101
102
103
    connect( this, SIGNAL(textChanged(QString)),
             SLOT(updateClearButtonIcon(QString)) );
    connect( &d->m_progressTimer, SIGNAL(timeout()),
             this, SLOT(updateProgress()) );
Dennis Nienhüser's avatar
Dennis Nienhüser committed
104
105
106
107
108
109
110
111
112
113
114
}

MarbleLineEdit::~MarbleLineEdit()
{
    delete d;
}

void MarbleLineEdit::setDecorator(const QPixmap &decorator)
{
    d->m_decoratorPixmap = decorator;
    d->m_decoratorButton->setPixmap( d->m_decoratorPixmap );
115
    int const padding = 2 + d->m_decoratorPixmap.width();
Dennis Nienhüser's avatar
Dennis Nienhüser committed
116
117
118
119

    QString const prefixDirection = layoutDirection() == Qt::LeftToRight ? "left" : "right";
    QString decoratorStyleSheet;
    if ( !d->m_decoratorPixmap.isNull() ) {
120
        decoratorStyleSheet = QString( "; padding-%1: %2" ).arg( prefixDirection ).arg( padding );
Dennis Nienhüser's avatar
Dennis Nienhüser committed
121
122
123
    }
    // Padding for clear button to avoid text underflow
    QString const postfixDirection  = layoutDirection() == Qt::LeftToRight ? "right" : "left";
124
    QString styleSheet = QString( ":enabled { padding-%1: %2; %3}").arg( postfixDirection ).arg( padding ).arg( decoratorStyleSheet );
Dennis Nienhüser's avatar
Dennis Nienhüser committed
125

Dennis Nienhüser's avatar
Dennis Nienhüser committed
126
127
    bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
    if ( !smallScreen ) {
Dennis Nienhüser's avatar
Dennis Nienhüser committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        setStyleSheet( styleSheet );
    }
}

void MarbleLineEdit::setBusy(bool busy)
{
    if ( busy ) {
        d->m_progressTimer.start();
    } else {
        d->m_progressTimer.stop();
        d->m_decoratorButton->setPixmap( d->m_decoratorPixmap );
    }
}

void MarbleLineEdit::updateClearButtonIcon( const QString& text )
{
    d->m_clearButton->setVisible( text.length() > 0 );
    if ( d->m_clearButton->pixmap() && !d->m_clearButton->pixmap()->isNull() ) {
        return;
    }

    QString const direction = layoutDirection() == Qt::LeftToRight ? "rtl" : "ltr";
150
    int const size = (MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen) ? 32 : 16;
151
    QPixmap pixmap = QPixmap(QStringLiteral(":/icons/%1x%1/edit-clear-locationbar-%2.png").arg(size).arg(direction));
Dennis Nienhüser's avatar
Dennis Nienhüser committed
152
153
154
155
156
157
    d->m_clearButton->setPixmap( pixmap );
}

void MarbleLineEdit::updateClearButton()
{
    const QSize geom = size();
158
    const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, nullptr, this );
Dennis Nienhüser's avatar
Dennis Nienhüser committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    const int pixmapSize = d->m_clearButton->pixmap()->width() + 1;
    const int decoratorSize = d->m_decoratorPixmap.width() + 1;

    int y = ( geom.height() - pixmapSize ) / 2;
    if ( layoutDirection() == Qt::LeftToRight ) {
        d->m_clearButton->move( geom.width() - frameWidth - pixmapSize - decoratorSize, y );
        d->m_decoratorButton->move( frameWidth - decoratorSize + 1, y );
    } else {
        d->m_clearButton->move( frameWidth - decoratorSize + 1, y );
        d->m_decoratorButton->move( geom.width() - frameWidth - pixmapSize - decoratorSize, y );
    }
}

void MarbleLineEdit::updateProgress()
{
    if ( !d->m_progressAnimation.isEmpty() ) {
        d->m_currentFrame = ( d->m_currentFrame + 1 ) % d->m_progressAnimation.size();
        QPixmap frame = d->m_progressAnimation[d->m_currentFrame];
        d->m_decoratorButton->setPixmap( frame );
    }
}

void MarbleLineEdit::mouseReleaseEvent( QMouseEvent* e )
{
    if ( d->m_clearButton == childAt( e->pos() ) ) {
        QString newText;
185
        if ( e->button() == Qt::MiddleButton ) {
Dennis Nienhüser's avatar
Dennis Nienhüser committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
            newText = QApplication::clipboard()->text( QClipboard::Selection );
            setText( newText );
        } else {
            setSelection( 0, text().size() );
            del();
            emit clearButtonClicked();
        }
        emit textChanged( newText );
    }

    if ( d->m_decoratorButton == childAt( e->pos() ) ) {
        emit decoratorButtonClicked();
    }

    QLineEdit::mouseReleaseEvent( e );
}

void MarbleLineEdit::resizeEvent( QResizeEvent * event )
{
    updateClearButton();
    QLineEdit::resizeEvent( event );
}

} // namespace Marble

211
#include "moc_MarbleLineEdit.cpp"