QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsmessagebaritem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmessagebaritem.h - description
3 -------------------
4 begin : August 2013
5 copyright : (C) 2013 by Denis Rouzaud
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 "qgsmessagebaritem.h"
19
20#include "qgsapplication.h"
21#include "qgsgui.h"
22#include "qgsmessagebar.h"
23#include "qgsnative.h"
24
25#include <QDesktopServices>
26#include <QFileInfo>
27#include <QHBoxLayout>
28#include <QLabel>
29#include <QString>
30#include <QTextBrowser>
31
32#include "moc_qgsmessagebaritem.cpp"
33
34using namespace Qt::StringLiterals;
35
37 : QWidget( parent )
38 , mText( text )
39 , mLevel( level )
40 , mDuration( duration )
41{
42 writeContent();
43}
44
45QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
46 : QWidget( parent )
47 , mTitle( title )
48 , mText( text )
49 , mLevel( level )
50 , mDuration( duration )
51{
52 writeContent();
53}
54
55QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
56 : QWidget( parent )
57 , mTitle( title )
58 , mText( text )
59 , mLevel( level )
60 , mDuration( duration )
61 , mWidget( widget )
62 , mUserIcon( QIcon() )
63
64{
65 writeContent();
66}
67
69 : QWidget( parent )
70 , mLevel( level )
71 , mDuration( duration )
72 , mWidget( widget )
73 , mUserIcon( QIcon() )
74
75{
76 writeContent();
77}
78
79void QgsMessageBarItem::writeContent()
80{
81 if ( mDuration < 0 )
82 mDuration = QgsMessageBar::defaultMessageTimeout( mLevel );
83
84 if ( !mLayout )
85 {
86 mLayout = new QHBoxLayout( this );
87 mLayout->setContentsMargins( 0, 0, 0, 0 );
88 mTextBrowser = nullptr;
89 mLblIcon = nullptr;
90 }
91
92 // ICON
93 if ( !mLblIcon )
94 {
95 mLblIcon = new QLabel( this );
96 mLayout->addWidget( mLblIcon );
97 }
98 QIcon icon;
99 if ( !mUserIcon.isNull() )
100 {
101 icon = mUserIcon;
102 }
103 else
104 {
105 QString msgIcon( u"/mIconInfo.svg"_s );
106 switch ( mLevel )
107 {
109 msgIcon = u"/mIconCritical.svg"_s;
110 break;
112 msgIcon = u"/mIconWarning.svg"_s;
113 break;
115 msgIcon = u"/mIconSuccess.svg"_s;
116 break;
117 default:
118 break;
119 }
121 }
122 const int iconSize = std::max( 24.0, fontMetrics().height() * 1.2 );
123 mLblIcon->setPixmap( icon.pixmap( iconSize ) );
124
125
126 // STYLESHEETS
127 QString contentStyleSheet;
128 if ( mLevel == Qgis::MessageLevel::Success )
129 {
130 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
131 "QLabel,QTextEdit { color: black; } " );
132 contentStyleSheet = u"<style> a, a:visited, a:hover { color:#268300; } </style>"_s;
133 }
134 else if ( mLevel == Qgis::MessageLevel::Critical )
135 {
136 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
137 "QLabel,QTextEdit { color: white; } " );
138 contentStyleSheet = u"<style>a, a:visited, a:hover { color:#4e0001; }</style>"_s;
139 }
140 else if ( mLevel == Qgis::MessageLevel::Warning )
141 {
142 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
143 "QLabel,QTextEdit { color: black; } " );
144 contentStyleSheet = u"<style>a, a:visited, a:hover { color:#945a00; }</style>"_s;
145 }
146 else if ( mLevel == Qgis::MessageLevel::Info )
147 {
148 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
149 "QLabel,QTextEdit { color: #2554a1; } " );
150 contentStyleSheet = u"<style>a, a:visited, a:hover { color:#3bb2fe; }</style>"_s;
151 }
152 mStyleSheet += "QLabel#mItemCount { font-style: italic; }"_L1;
153
154 // TITLE AND TEXT
155 if ( mTitle.isEmpty() && mText.isEmpty() )
156 {
157 if ( mTextBrowser )
158 {
159 delete mTextBrowser;
160 mTextBrowser = nullptr;
161 }
162 }
163 else
164 {
165 if ( !mTextBrowser )
166 {
167 mTextBrowser = new QTextBrowser( this );
168 mTextBrowser->setObjectName( u"textEdit"_s );
169 mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
170 mTextBrowser->setReadOnly( true );
171 mTextBrowser->setOpenLinks( false );
172 connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
173
174 mTextBrowser->setFrameShape( QFrame::NoFrame );
175 // stylesheet set here so Qt-style substituted scrollbar arrows can show within limited height
176 // adjusts to height of font set in app options
177 mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
178 "QScrollBar { background-color: rgba(0,0,0,0); } "
179 "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
180 "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
181 mLayout->addWidget( mTextBrowser );
182 }
183 QString content = mText;
184 if ( !mTitle.isEmpty() )
185 {
186 // add ':' to end of title
187 QString t = mTitle.trimmed();
188 if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( ": "_L1 ) )
189 t += ": "_L1;
190 content.prepend( u"<b>"_s + t + " </b>" );
191 }
192 content.prepend( contentStyleSheet );
193 mTextBrowser->setText( content );
194 }
195
196 // WIDGET
197 if ( mWidget )
198 {
199 QLayoutItem *item = mLayout->itemAt( 2 );
200 if ( !item || item->widget() != mWidget )
201 {
202 mLayout->addWidget( mWidget );
203 }
204 }
205}
206
208{
209 mText = text;
210 writeContent();
211 return this;
212}
213
215{
216 return mText;
217}
218
220{
221 mTitle = title;
222 writeContent();
223 return this;
224}
225
227{
228 return mTitle;
229}
230
232{
233 if ( level != mLevel )
234 {
235 mLevel = level;
236 writeContent();
237 emit styleChanged( mStyleSheet );
238 }
239
240 return this;
241}
242
244{
245 return mLevel;
246}
247
249{
250 if ( mWidget )
251 {
252 QLayoutItem *item = nullptr;
253 item = mLayout->itemAt( 2 );
254 if ( item->widget() == mWidget )
255 {
256 delete item->widget();
257 }
258 }
259 mWidget = widget;
260 writeContent();
261 return this;
262}
263
265{
266 return mWidget;
267}
268
270{
271 mUserIcon = icon;
272 return this;
273}
274
276{
277 return mUserIcon;
278}
279
280
282{
283 mDuration = duration;
284 return this;
285}
286
288{
289 if ( !mMessageBar )
290 return;
291
292 mMessageBar->popWidget( this );
293}
294
295void QgsMessageBarItem::urlClicked( const QUrl &url )
296{
297 const QFileInfo file( url.toLocalFile() );
298 if ( file.exists() && !file.isDir() )
299 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
300 else
301 QDesktopServices::openUrl( url );
302 dismiss();
303}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:159
@ Warning
Warning message.
Definition qgis.h:161
@ Critical
Critical/error message.
Definition qgis.h:162
@ Info
Information message.
Definition qgis.h:160
@ Success
Used for reporting a successful operation.
Definition qgis.h:163
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
Definition qgsgui.cpp:99
void styleChanged(const QString &styleSheet)
Emitted when the item's message level has changed and the message bar style will need to be updated a...
QIcon icon() const
Returns the icon for the message.
QgsMessageBarItem * setLevel(Qgis::MessageLevel level)
Sets the message level for the item, which controls how the message bar is styled when the item is di...
int duration() const
Returns the duration (in seconds) of the message.
QgsMessageBarItem * setText(const QString &text)
Sets the message text to show in the item.
QgsMessageBarItem * setIcon(const QIcon &icon)
Sets the icon associated with the message.
QString text() const
Returns the text for the message.
QWidget * widget() const
Returns the widget for the message.
QgsMessageBarItem(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=0, QWidget *parent=nullptr)
Constructor for QgsMessageBarItem, containing a message with the specified text to be displayed on th...
QString title() const
Returns the title for the message.
QgsMessageBarItem * setTitle(const QString &title)
Sets the title for in the item.
Qgis::MessageLevel level() const
Returns the message level for the message.
QgsMessageBarItem * setDuration(int duration)
Sets the duration (in seconds) to show the message for.
QgsMessageBarItem * setWidget(QWidget *widget)
Sets a custom widget to show in the item.
void dismiss()
Dismisses the item, removing it from the message bar and deleting it.
static int defaultMessageTimeout(Qgis::MessageLevel level=Qgis::MessageLevel::NoLevel)
Returns the default timeout in seconds for timed messages of the specified level.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.