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