QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <QTextBrowser>
30
31#include "moc_qgsmessagebaritem.cpp"
32
34 : QWidget( parent )
35 , mText( text )
36 , mLevel( level )
37 , mDuration( duration )
38{
39 writeContent();
40}
41
42QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, Qgis::MessageLevel level, int duration, QWidget *parent )
43 : QWidget( parent )
44 , mTitle( title )
45 , mText( text )
46 , mLevel( level )
47 , mDuration( duration )
48{
49 writeContent();
50}
51
52QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, Qgis::MessageLevel level, int duration, QWidget *parent )
53 : QWidget( parent )
54 , mTitle( title )
55 , mText( text )
56 , mLevel( level )
57 , mDuration( duration )
58 , mWidget( widget )
59 , mUserIcon( QIcon() )
60
61{
62 writeContent();
63}
64
66 : QWidget( parent )
67 , mLevel( level )
68 , mDuration( duration )
69 , mWidget( widget )
70 , mUserIcon( QIcon() )
71
72{
73 writeContent();
74}
75
76void QgsMessageBarItem::writeContent()
77{
78 if ( mDuration < 0 )
79 mDuration = QgsMessageBar::defaultMessageTimeout( mLevel );
80
81 if ( !mLayout )
82 {
83 mLayout = new QHBoxLayout( this );
84 mLayout->setContentsMargins( 0, 0, 0, 0 );
85 mTextBrowser = nullptr;
86 mLblIcon = nullptr;
87 }
88
89 // ICON
90 if ( !mLblIcon )
91 {
92 mLblIcon = new QLabel( this );
93 mLayout->addWidget( mLblIcon );
94 }
95 QIcon icon;
96 if ( !mUserIcon.isNull() )
97 {
98 icon = mUserIcon;
99 }
100 else
101 {
102 QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
103 switch ( mLevel )
104 {
106 msgIcon = QStringLiteral( "/mIconCritical.svg" );
107 break;
109 msgIcon = QStringLiteral( "/mIconWarning.svg" );
110 break;
112 msgIcon = QStringLiteral( "/mIconSuccess.svg" );
113 break;
114 default:
115 break;
116 }
118 }
119 const int iconSize = std::max( 24.0, fontMetrics().height() * 1.2 );
120 mLblIcon->setPixmap( icon.pixmap( iconSize ) );
121
122
123 // STYLESHEETS
124 QString contentStyleSheet;
125 if ( mLevel == Qgis::MessageLevel::Success )
126 {
127 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
128 "QLabel,QTextEdit { color: black; } " );
129 contentStyleSheet = QStringLiteral( "<style> a, a:visited, a:hover { color:#268300; } </style>" );
130 }
131 else if ( mLevel == Qgis::MessageLevel::Critical )
132 {
133 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
134 "QLabel,QTextEdit { color: white; } " );
135 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#4e0001; }</style>" );
136 }
137 else if ( mLevel == Qgis::MessageLevel::Warning )
138 {
139 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
140 "QLabel,QTextEdit { color: black; } " );
141 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#945a00; }</style>" );
142 }
143 else if ( mLevel == Qgis::MessageLevel::Info )
144 {
145 mStyleSheet = QStringLiteral( "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
146 "QLabel,QTextEdit { color: #2554a1; } " );
147 contentStyleSheet = QStringLiteral( "<style>a, a:visited, a:hover { color:#3bb2fe; }</style>" );
148 }
149 mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
150
151 // TITLE AND TEXT
152 if ( mTitle.isEmpty() && mText.isEmpty() )
153 {
154 if ( mTextBrowser )
155 {
156 delete mTextBrowser;
157 mTextBrowser = nullptr;
158 }
159 }
160 else
161 {
162 if ( !mTextBrowser )
163 {
164 mTextBrowser = new QTextBrowser( this );
165 mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
166 mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
167 mTextBrowser->setReadOnly( true );
168 mTextBrowser->setOpenLinks( false );
169 connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
170
171 mTextBrowser->setFrameShape( QFrame::NoFrame );
172 // stylesheet set here so Qt-style substituted scrollbar arrows can show within limited height
173 // adjusts to height of font set in app options
174 mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
175 "QScrollBar { background-color: rgba(0,0,0,0); } "
176 "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
177 "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
178 mLayout->addWidget( mTextBrowser );
179 }
180 QString content = mText;
181 if ( !mTitle.isEmpty() )
182 {
183 // add ':' to end of title
184 QString t = mTitle.trimmed();
185 if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
186 t += QLatin1String( ": " );
187 content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
188 }
189 content.prepend( contentStyleSheet );
190 mTextBrowser->setText( content );
191 }
192
193 // WIDGET
194 if ( mWidget )
195 {
196 QLayoutItem *item = mLayout->itemAt( 2 );
197 if ( !item || item->widget() != mWidget )
198 {
199 mLayout->addWidget( mWidget );
200 }
201 }
202}
203
205{
206 mText = text;
207 writeContent();
208 return this;
209}
210
212{
213 return mText;
214}
215
217{
218 mTitle = title;
219 writeContent();
220 return this;
221}
222
224{
225 return mTitle;
226}
227
229{
230 if ( level != mLevel )
231 {
232 mLevel = level;
233 writeContent();
234 emit styleChanged( mStyleSheet );
235 }
236
237 return this;
238}
239
241{
242 return mLevel;
243}
244
246{
247 if ( mWidget )
248 {
249 QLayoutItem *item = nullptr;
250 item = mLayout->itemAt( 2 );
251 if ( item->widget() == mWidget )
252 {
253 delete item->widget();
254 }
255 }
256 mWidget = widget;
257 writeContent();
258 return this;
259}
260
262{
263 return mWidget;
264}
265
267{
268 mUserIcon = icon;
269 return this;
270}
271
273{
274 return mUserIcon;
275}
276
277
279{
280 mDuration = duration;
281 return this;
282}
283
285{
286 if ( !mMessageBar )
287 return;
288
289 mMessageBar->popWidget( this );
290}
291
292void QgsMessageBarItem::urlClicked( const QUrl &url )
293{
294 const QFileInfo file( url.toLocalFile() );
295 if ( file.exists() && !file.isDir() )
296 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
297 else
298 QDesktopServices::openUrl( url );
299 dismiss();
300}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:156
@ Warning
Warning message.
Definition qgis.h:158
@ Critical
Critical/error message.
Definition qgis.h:159
@ Info
Information message.
Definition qgis.h:157
@ Success
Used for reporting a successful operation.
Definition qgis.h:160
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:96
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.