QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #include <QEvent>
24 #include <QStatusBar>
25 
26 QgsStatusBar::QgsStatusBar( QWidget *parent )
27  : QWidget( parent )
28 {
29  mLayout = new QHBoxLayout();
30  mLayout->setContentsMargins( 2, 0, 2, 0 );
31  mLayout->setSpacing( 6 );
32 
33  mLineEdit = new QLineEdit( QString() );
34  mLineEdit->setDisabled( true );
35  mLineEdit->setFrame( false );
36  mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
37  QPalette palette = mLineEdit->palette();
38  palette.setColor( QPalette::Disabled, QPalette::Text, QPalette::WindowText );
39  mLineEdit->setPalette( palette );
40  mLineEdit->setStyleSheet( QStringLiteral( "* { border: 0; background-color: rgba(0, 0, 0, 0); }" ) );
41  mLayout->addWidget( mLineEdit, 10 );
42  setLayout( mLayout );
43 }
44 
45 void QgsStatusBar::addPermanentWidget( QWidget *widget, int stretch, Anchor anchor )
46 {
47  switch ( anchor )
48  {
49  case AnchorLeft:
50  mLayout->insertWidget( 0, widget, stretch, Qt::AlignLeft );
51  break;
52 
53  case AnchorRight:
54  mLayout->addWidget( widget, stretch, Qt::AlignLeft );
55  break;
56  }
57 }
58 
59 void QgsStatusBar::removeWidget( QWidget *widget )
60 {
61  mLayout->removeWidget( widget );
62 }
63 
65 {
66  return mLineEdit->text();
67 }
68 
69 void QgsStatusBar::showMessage( const QString &text, int timeout )
70 {
71  mLineEdit->setText( text );
72  mLineEdit->setCursorPosition( 0 );
73  if ( timeout > 0 )
74  {
75  if ( !mTempMessageTimer )
76  {
77  mTempMessageTimer = new QTimer( this );
78  connect( mTempMessageTimer, &QTimer::timeout, this, &QgsStatusBar::clearMessage );
79  }
80  mTempMessageTimer->start( timeout );
81  }
82  else if ( mTempMessageTimer )
83  {
84  delete mTempMessageTimer;
85  mTempMessageTimer = nullptr;
86  }
87 }
88 
90 {
91  mLineEdit->setText( QString() );
92 }
93 
94 void QgsStatusBar::setParentStatusBar( QStatusBar *statusBar )
95 {
96  if ( mParentStatusBar )
97  mParentStatusBar->disconnect( mShowMessageConnection );
98 
99  mParentStatusBar = statusBar;
100 
101  if ( mParentStatusBar )
102  mShowMessageConnection = connect( mParentStatusBar, &QStatusBar::messageChanged, this, [this]( const QString & message ) { showMessage( message ); } );
103 }
104 
105 void QgsStatusBar::changeEvent( QEvent *event )
106 {
107  QWidget::changeEvent( event );
108 
109  if ( event->type() == QEvent::FontChange )
110  {
111  mLineEdit->setFont( font() );
112  }
113 }
QgsStatusBar::changeEvent
void changeEvent(QEvent *event) override
Definition: qgsstatusbar.cpp:105
QgsStatusBar::setParentStatusBar
void setParentStatusBar(QStatusBar *statusBar)
Sets the parent status bar.
Definition: qgsstatusbar.cpp:94
QgsStatusBar::AnchorRight
@ AnchorRight
Anchor widget to right of status bar.
Definition: qgsstatusbar.h:56
QgsStatusBar::clearMessage
void clearMessage()
Removes any temporary message being shown.
Definition: qgsstatusbar.cpp:89
QgsStatusBar::removeWidget
void removeWidget(QWidget *widget)
Removes a widget from the status bar.
Definition: qgsstatusbar.cpp:59
QgsStatusBar::Anchor
Anchor
Placement anchor for widgets.
Definition: qgsstatusbar.h:54
QgsStatusBar::addPermanentWidget
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't already a ch...
Definition: qgsstatusbar.cpp:45
qgsstatusbar.h
QgsStatusBar::showMessage
void showMessage(const QString &message, int timeout=0)
Displays the given message for the specified number of milli-seconds (timeout).
Definition: qgsstatusbar.cpp:69
QgsStatusBar::AnchorLeft
@ AnchorLeft
Anchor widget to left of status bar.
Definition: qgsstatusbar.h:55
QgsStatusBar::QgsStatusBar
QgsStatusBar(QWidget *parent=nullptr)
Constructor for QgsStatusBar.
Definition: qgsstatusbar.cpp:26
QgsStatusBar::currentMessage
QString currentMessage() const
Returns the current message shown in the status bar.
Definition: qgsstatusbar.cpp:64