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