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