QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
131 "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
132 "QLabel,QTextEdit { color: black; } "
133 );
134 contentStyleSheet = u"<style> a, a:visited, a:hover { color:#268300; } </style>"_s;
135 }
136 else if ( mLevel == Qgis::MessageLevel::Critical )
137 {
138 mStyleSheet = QStringLiteral(
139 "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
140 "QLabel,QTextEdit { color: white; } "
141 );
142 contentStyleSheet = u"<style>a, a:visited, a:hover { color:#4e0001; }</style>"_s;
143 }
144 else if ( mLevel == Qgis::MessageLevel::Warning )
145 {
146 mStyleSheet = QStringLiteral(
147 "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
148 "QLabel,QTextEdit { color: black; } "
149 );
150 contentStyleSheet = u"<style>a, a:visited, a:hover { color:#945a00; }</style>"_s;
151 }
152 else if ( mLevel == Qgis::MessageLevel::Info )
153 {
154 mStyleSheet = QStringLiteral(
155 "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
156 "QLabel,QTextEdit { color: #2554a1; } "
157 );
158 contentStyleSheet = u"<style>a, a:visited, a:hover { color:#3bb2fe; }</style>"_s;
159 }
160 mStyleSheet += "QLabel#mItemCount { font-style: italic; }"_L1;
161
162 // TITLE AND TEXT
163 if ( mTitle.isEmpty() && mText.isEmpty() )
164 {
165 if ( mTextBrowser )
166 {
167 delete mTextBrowser;
168 mTextBrowser = nullptr;
169 }
170 }
171 else
172 {
173 if ( !mTextBrowser )
174 {
175 mTextBrowser = new QTextBrowser( this );
176 mTextBrowser->setObjectName( u"textEdit"_s );
177 mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
178 mTextBrowser->setReadOnly( true );
179 mTextBrowser->setOpenLinks( false );
180 connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );
181
182 mTextBrowser->setFrameShape( QFrame::NoFrame );
183 // stylesheet set here so Qt-style substituted scrollbar arrows can show within limited height
184 // adjusts to height of font set in app options
185 mTextBrowser->setStyleSheet(
186 "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
187 "QScrollBar { background-color: rgba(0,0,0,0); } "
188 "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
189 "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } "
190 );
191 mLayout->addWidget( mTextBrowser );
192 }
193 QString content = mText;
194 if ( !mTitle.isEmpty() )
195 {
196 // add ':' to end of title
197 QString t = mTitle.trimmed();
198 if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( ": "_L1 ) )
199 t += ": "_L1;
200 content.prepend( u"<b>"_s + t + " </b>" );
201 }
202 content.prepend( contentStyleSheet );
203 mTextBrowser->setText( content );
204 }
205
206 // WIDGET
207 if ( mWidget )
208 {
209 QLayoutItem *item = mLayout->itemAt( 2 );
210 if ( !item || item->widget() != mWidget )
211 {
212 mLayout->addWidget( mWidget );
213 }
214 }
215}
216
218{
219 mText = text;
220 writeContent();
221 return this;
222}
223
225{
226 return mText;
227}
228
230{
231 mTitle = title;
232 writeContent();
233 return this;
234}
235
237{
238 return mTitle;
239}
240
242{
243 if ( level != mLevel )
244 {
245 mLevel = level;
246 writeContent();
247 emit styleChanged( mStyleSheet );
248 }
249
250 return this;
251}
252
254{
255 return mLevel;
256}
257
259{
260 if ( mWidget )
261 {
262 QLayoutItem *item = nullptr;
263 item = mLayout->itemAt( 2 );
264 if ( item->widget() == mWidget )
265 {
266 delete item->widget();
267 }
268 }
269 mWidget = widget;
270 writeContent();
271 return this;
272}
273
275{
276 return mWidget;
277}
278
280{
281 mUserIcon = icon;
282 return this;
283}
284
286{
287 return mUserIcon;
288}
289
290
292{
293 mDuration = duration;
294 return this;
295}
296
298{
299 if ( !mMessageBar )
300 return;
301
302 mMessageBar->popWidget( this );
303}
304
305void QgsMessageBarItem::urlClicked( const QUrl &url )
306{
307 const QFileInfo file( url.toLocalFile() );
308 if ( file.exists() && !file.isDir() )
309 QgsGui::nativePlatformInterface()->openFileExplorerAndSelectFile( url.toLocalFile() );
310 else
311 QDesktopServices::openUrl( url );
312 dismiss();
313}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
@ Warning
Warning message.
Definition qgis.h:162
@ Critical
Critical/error message.
Definition qgis.h:163
@ Info
Information message.
Definition qgis.h:161
@ Success
Used for reporting a successful operation.
Definition qgis.h:164
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.