QGIS API Documentation  3.0.2-Girona (307d082)
qgsstatusbar.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstatusbar.cpp
3  ----------------
4  begin : May 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsstatusbar.h"
19 #include <QLayout>
20 #include <QLineEdit>
21 #include <QPalette>
22 #include <QTimer>
23 
24 QgsStatusBar::QgsStatusBar( QWidget *parent )
25  : QWidget( parent )
26 {
27  mLayout = new QHBoxLayout();
28  mLayout->setMargin( 0 );
29  mLayout->setContentsMargins( 2, 0, 2, 0 );
30  mLayout->setSpacing( 6 );
31 
32  mLineEdit = new QLineEdit( QString() );
33  mLineEdit->setDisabled( true );
34  mLineEdit->setFrame( false );
35  mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
36  QPalette palette;
37  palette.setColor( QPalette::Disabled, QPalette::Text, QPalette::WindowText );
38  mLineEdit->setPalette( palette );
39  mLineEdit->setStyleSheet( QStringLiteral( "* { background-color: rgba(0, 0, 0, 0); }" ) );
40  mLayout->addWidget( mLineEdit, 10 );
41  setLayout( mLayout );
42 }
43 
44 void QgsStatusBar::addPermanentWidget( QWidget *widget, int stretch, Anchor anchor )
45 {
46  switch ( anchor )
47  {
48  case AnchorLeft:
49  mLayout->insertWidget( 0, widget, stretch, Qt::AlignLeft );
50  break;
51 
52  case AnchorRight:
53  mLayout->addWidget( widget, stretch, Qt::AlignLeft );
54  break;
55  }
56 }
57 
58 void QgsStatusBar::removeWidget( QWidget *widget )
59 {
60  mLayout->removeWidget( widget );
61 }
62 
64 {
65  return mLineEdit->text();
66 }
67 
68 void QgsStatusBar::showMessage( const QString &text, int timeout )
69 {
70  mLineEdit->setText( text );
71  mLineEdit->setCursorPosition( 0 );
72  if ( timeout > 0 )
73  {
74  if ( !mTempMessageTimer )
75  {
76  mTempMessageTimer = new QTimer( this );
77  connect( mTempMessageTimer, &QTimer::timeout, this, &QgsStatusBar::clearMessage );
78  }
79  mTempMessageTimer->start( timeout );
80  }
81  else if ( mTempMessageTimer )
82  {
83  delete mTempMessageTimer;
84  mTempMessageTimer = nullptr;
85  }
86 }
87 
89 {
90  mLineEdit->setText( QString() );
91 }
Anchor
Placement anchor for widgets.
Definition: qgsstatusbar.h:52
void clearMessage()
Removes any temporary message being shown.
QString currentMessage() const
Returns the current message shown in the status bar.
void addPermanentWidget(QWidget *widget, int stretch=0, Anchor anchor=AnchorRight)
Adds the given widget permanently to this status bar, reparenting the widget if it isn&#39;t already a ch...
Anchor widget to left of status bar.
Definition: qgsstatusbar.h:54
void showMessage(const QString &message, int timeout=0)
Displays the given message for the specified number of milli-seconds (timeout).
void removeWidget(QWidget *widget)
Removes a widget from the status bar.
Anchor widget to right of status bar.
Definition: qgsstatusbar.h:55
QgsStatusBar(QWidget *parent=nullptr)
Constructor for QgsStatusBar.