18 #include <QApplication>
22 #include <QMouseEvent>
29 class MarbleLineEditPrivate
32 QLabel* m_clearButton;
34 QLabel* m_decoratorButton;
36 QPixmap m_clearPixmap;
38 QPixmap m_decoratorPixmap;
40 QTimer m_progressTimer;
42 QVector<QPixmap> m_progressAnimation;
48 MarbleLineEditPrivate( MarbleLineEdit* parent );
50 void createProgressAnimation();
53 MarbleLineEditPrivate::MarbleLineEditPrivate( MarbleLineEdit* parent ) :
54 m_clearButton( new QLabel( parent ) ), m_decoratorButton( new QLabel( parent ) ),
55 m_currentFrame( 0 ), m_iconSize( 16 )
57 m_clearButton->setCursor( Qt::ArrowCursor );
58 m_clearButton->setToolTip( QObject::tr(
"Clear" ) );
59 m_decoratorButton->setCursor( Qt::ArrowCursor );
60 createProgressAnimation();
61 m_progressTimer.setInterval( 100 );
67 void MarbleLineEditPrivate::createProgressAnimation()
70 qreal
const h = m_iconSize / 2.0;
71 qreal
const q = h / 2.0;
73 qreal
const r = d / 2.0;
76 QImage canvas( m_iconSize, m_iconSize, QImage::Format_ARGB32 );
78 painter.setRenderHint( QPainter::Antialiasing,
true );
79 painter.setPen( QColor ( Qt::gray ) );
80 painter.setBrush( QColor( Qt::white ) );
83 for(
double t = 0.0; t < 2 *
M_PI; t += M_PI / 8.0 ) {
84 canvas.fill( Qt::transparent );
85 QRectF firstCircle( h - r + q * cos( t ), h - r + q * sin( t ), d, d );
86 QRectF secondCircle( h - r + q * cos( t + M_PI ), h - r + q * sin( t + M_PI ), d, d );
87 painter.drawEllipse( firstCircle );
88 painter.drawEllipse( secondCircle );
89 m_progressAnimation.push_back( QPixmap::fromImage( canvas ) );
94 QLineEdit( parent ), d( new MarbleLineEditPrivate( this ) )
96 updateClearButtonIcon( text() );
100 connect(
this, SIGNAL(textChanged(QString)),
101 SLOT(updateClearButtonIcon(QString)) );
102 connect( &d->m_progressTimer, SIGNAL(timeout()),
103 this, SLOT(updateProgress()) );
113 d->m_decoratorPixmap = decorator;
114 d->m_decoratorButton->setPixmap( d->m_decoratorPixmap );
115 int const padding = 2 + d->m_decoratorPixmap.width();
117 QString
const prefixDirection = layoutDirection() == Qt::LeftToRight ?
"left" :
"right";
118 QString decoratorStyleSheet;
119 if ( !d->m_decoratorPixmap.isNull() ) {
120 decoratorStyleSheet = QString(
"; padding-%1: %2" ).arg( prefixDirection ).arg( padding );
123 QString
const postfixDirection = layoutDirection() == Qt::LeftToRight ?
"right" :
"left";
124 QString styleSheet = QString(
":enabled { padding-%1: %2; %3}").arg( postfixDirection ).arg( padding ).arg( decoratorStyleSheet );
127 setStyleSheet( styleSheet );
134 d->m_progressTimer.start();
136 d->m_progressTimer.stop();
137 d->m_decoratorButton->setPixmap( d->m_decoratorPixmap );
141 void MarbleLineEdit::updateClearButtonIcon(
const QString& text )
143 d->m_clearButton->setVisible( text.length() > 0 );
144 if ( d->m_clearButton->pixmap() && !d->m_clearButton->pixmap()->isNull() ) {
148 QString
const direction = layoutDirection() == Qt::LeftToRight ?
"rtl" :
"ltr";
150 QPixmap pixmap = QPixmap( QString(
":/icons/%1x%1/edit-clear-locationbar-%2.png").arg( size ).arg( direction ) );
151 d->m_clearButton->setPixmap( pixmap );
154 void MarbleLineEdit::updateClearButton()
156 const QSize geom = size();
157 const int frameWidth = style()->pixelMetric( QStyle::PM_DefaultFrameWidth, 0,
this );
158 const int pixmapSize = d->m_clearButton->pixmap()->width() + 1;
159 const int decoratorSize = d->m_decoratorPixmap.width() + 1;
161 int y = ( geom.height() - pixmapSize ) / 2;
162 if ( layoutDirection() == Qt::LeftToRight ) {
163 d->m_clearButton->move( geom.width() - frameWidth - pixmapSize - decoratorSize, y );
164 d->m_decoratorButton->move( frameWidth - decoratorSize + 1, y );
166 d->m_clearButton->move( frameWidth - decoratorSize + 1, y );
167 d->m_decoratorButton->move( geom.width() - frameWidth - pixmapSize - decoratorSize, y );
171 void MarbleLineEdit::updateProgress()
173 if ( !d->m_progressAnimation.isEmpty() ) {
174 d->m_currentFrame = ( d->m_currentFrame + 1 ) % d->m_progressAnimation.size();
175 QPixmap frame = d->m_progressAnimation[d->m_currentFrame];
176 d->m_decoratorButton->setPixmap( frame );
182 if ( d->m_clearButton == childAt( e->pos() ) ) {
184 if ( e->button() == Qt::MidButton ) {
185 newText = QApplication::clipboard()->text( QClipboard::Selection );
188 setSelection( 0, text().size() );
192 emit textChanged( newText );
195 if ( d->m_decoratorButton == childAt( e->pos() ) ) {
199 QLineEdit::mouseReleaseEvent( e );
205 QLineEdit::resizeEvent( event );
210 #include "MarbleLineEdit.moc"
MarbleLineEdit(QWidget *parent=0)
Constructor.
virtual void resizeEvent(QResizeEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)
void setDecorator(const QPixmap &decorator)
static MarbleGlobal * getInstance()
Profiles profiles() const
void decoratorButtonClicked()
void clearButtonClicked()
The clear button was clicked with the left mouse button.
~MarbleLineEdit()
Destructor.