• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • lib
  • marble
MarbleLineEdit.cpp
Go to the documentation of this file.
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,2012 Dennis Nienhüser <earthwings@gentoo.org>
13 //
14 
15 #include "MarbleLineEdit.h"
16 #include "MarbleGlobal.h"
17 
18 #include <QApplication>
19 #include <QClipboard>
20 #include <QLabel>
21 #include <QStyle>
22 #include <QMouseEvent>
23 #include <QPainter>
24 #include <QTimer>
25 
26 namespace Marble
27 {
28 
29 class MarbleLineEditPrivate
30 {
31 public:
32  QLabel* m_clearButton;
33 
34  QLabel* m_decoratorButton;
35 
36  QPixmap m_clearPixmap;
37 
38  QPixmap m_decoratorPixmap;
39 
40  QTimer m_progressTimer;
41 
42  QVector<QPixmap> m_progressAnimation;
43 
44  int m_currentFrame;
45 
46  int m_iconSize;
47 
48  MarbleLineEditPrivate( MarbleLineEdit* parent );
49 
50  void createProgressAnimation();
51 };
52 
53 MarbleLineEditPrivate::MarbleLineEditPrivate( MarbleLineEdit* parent ) :
54  m_clearButton( new QLabel( parent ) ), m_decoratorButton( new QLabel( parent ) ),
55  m_currentFrame( 0 ), m_iconSize( 16 )
56 {
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 );
62  if ( MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ) {
63  m_iconSize = 32;
64  }
65 }
66 
67 void MarbleLineEditPrivate::createProgressAnimation()
68 {
69  // Size parameters
70  qreal const h = m_iconSize / 2.0; // Half of the icon size
71  qreal const q = h / 2.0; // Quarter of the icon size
72  qreal const d = 7.5; // Circle diameter
73  qreal const r = d / 2.0; // Circle radius
74 
75  // Canvas parameters
76  QImage canvas( m_iconSize, m_iconSize, QImage::Format_ARGB32 );
77  QPainter painter( &canvas );
78  painter.setRenderHint( QPainter::Antialiasing, true );
79  painter.setPen( QColor ( Qt::gray ) );
80  painter.setBrush( QColor( Qt::white ) );
81 
82  // Create all frames
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 ) );
90  }
91 }
92 
93 MarbleLineEdit::MarbleLineEdit( QWidget *parent ) :
94  QLineEdit( parent ), d( new MarbleLineEditPrivate( this ) )
95 {
96  updateClearButtonIcon( text() );
97  updateClearButton();
98 
99  setDecorator( d->m_decoratorPixmap );
100  connect( this, SIGNAL(textChanged(QString)),
101  SLOT(updateClearButtonIcon(QString)) );
102  connect( &d->m_progressTimer, SIGNAL(timeout()),
103  this, SLOT(updateProgress()) );
104 }
105 
106 MarbleLineEdit::~MarbleLineEdit()
107 {
108  delete d;
109 }
110 
111 void MarbleLineEdit::setDecorator(const QPixmap &decorator)
112 {
113  d->m_decoratorPixmap = decorator;
114  d->m_decoratorButton->setPixmap( d->m_decoratorPixmap );
115  int const padding = 2 + d->m_decoratorPixmap.width();
116 
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 );
121  }
122  // Padding for clear button to avoid text underflow
123  QString const postfixDirection = layoutDirection() == Qt::LeftToRight ? "right" : "left";
124  QString styleSheet = QString( ":enabled { padding-%1: %2; %3}").arg( postfixDirection ).arg( padding ).arg( decoratorStyleSheet );
125 
126  if ( !MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ) {
127  setStyleSheet( styleSheet );
128  }
129 }
130 
131 void MarbleLineEdit::setBusy(bool busy)
132 {
133  if ( busy ) {
134  d->m_progressTimer.start();
135  } else {
136  d->m_progressTimer.stop();
137  d->m_decoratorButton->setPixmap( d->m_decoratorPixmap );
138  }
139 }
140 
141 void MarbleLineEdit::updateClearButtonIcon( const QString& text )
142 {
143  d->m_clearButton->setVisible( text.length() > 0 );
144  if ( d->m_clearButton->pixmap() && !d->m_clearButton->pixmap()->isNull() ) {
145  return;
146  }
147 
148  QString const direction = layoutDirection() == Qt::LeftToRight ? "rtl" : "ltr";
149  int const size = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen ? 32 : 16;
150  QPixmap pixmap = QPixmap( QString( ":/icons/%1x%1/edit-clear-locationbar-%2.png").arg( size ).arg( direction ) );
151  d->m_clearButton->setPixmap( pixmap );
152 }
153 
154 void MarbleLineEdit::updateClearButton()
155 {
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;
160 
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 );
165  } else {
166  d->m_clearButton->move( frameWidth - decoratorSize + 1, y );
167  d->m_decoratorButton->move( geom.width() - frameWidth - pixmapSize - decoratorSize, y );
168  }
169 }
170 
171 void MarbleLineEdit::updateProgress()
172 {
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 );
177  }
178 }
179 
180 void MarbleLineEdit::mouseReleaseEvent( QMouseEvent* e )
181 {
182  if ( d->m_clearButton == childAt( e->pos() ) ) {
183  QString newText;
184  if ( e->button() == Qt::MidButton ) {
185  newText = QApplication::clipboard()->text( QClipboard::Selection );
186  setText( newText );
187  } else {
188  setSelection( 0, text().size() );
189  del();
190  emit clearButtonClicked();
191  }
192  emit textChanged( newText );
193  }
194 
195  if ( d->m_decoratorButton == childAt( e->pos() ) ) {
196  emit decoratorButtonClicked();
197  }
198 
199  QLineEdit::mouseReleaseEvent( e );
200 }
201 
202 void MarbleLineEdit::resizeEvent( QResizeEvent * event )
203 {
204  updateClearButton();
205  QLineEdit::resizeEvent( event );
206 }
207 
208 } // namespace Marble
209 
210 #include "MarbleLineEdit.moc"
QPainter
Marble::MarbleLineEdit::MarbleLineEdit
MarbleLineEdit(QWidget *parent=0)
Constructor.
Definition: MarbleLineEdit.cpp:93
QWidget
Marble::MarbleLineEdit::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
Definition: MarbleLineEdit.cpp:202
Marble::MarbleLineEdit::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: MarbleLineEdit.cpp:180
Marble::MarbleLineEdit::setDecorator
void setDecorator(const QPixmap &decorator)
Definition: MarbleLineEdit.cpp:111
MarbleGlobal.h
Marble::MarbleGlobal::SmallScreen
Definition: MarbleGlobal.h:268
Marble::MarbleGlobal::getInstance
static MarbleGlobal * getInstance()
Definition: MarbleGlobal.cpp:37
Marble::MarbleLineEdit::setBusy
void setBusy(bool busy)
Definition: MarbleLineEdit.cpp:131
direction
qreal direction
Definition: tools/osm-addresses/OsmParser.cpp:39
MarbleLineEdit.h
Marble::MarbleGlobal::profiles
Profiles profiles() const
Definition: MarbleGlobal.cpp:48
QLineEdit
M_PI
#define M_PI
Definition: GeoDataCoordinates.h:26
Marble::MarbleLineEdit::decoratorButtonClicked
void decoratorButtonClicked()
Marble::MarbleLineEdit::clearButtonClicked
void clearButtonClicked()
The clear button was clicked with the left mouse button.
Marble::MarbleLineEdit::~MarbleLineEdit
~MarbleLineEdit()
Destructor.
Definition: MarbleLineEdit.cpp:106
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal