QGIS API Documentation  2.99.0-Master (7d4f81d)
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
6  email : [email protected]
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 "qgsapplication.h"
19 #include "qgsmessagebaritem.h"
20 #include "qgsmessagebar.h"
21 
22 #include <QHBoxLayout>
23 #include <QLabel>
24 #include <QTextEdit>
25 
26 QgsMessageBarItem::QgsMessageBarItem( const QString &text, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
27  : QWidget( parent )
28  , mTitle( QLatin1String( "" ) )
29  , mText( text )
30  , mLevel( level )
31  , mDuration( duration )
32  , mWidget( nullptr )
33  , mUserIcon( QIcon() )
34  , mLayout( nullptr )
35 {
36  writeContent();
37 }
38 
39 QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
40  : QWidget( parent )
41  , mTitle( title )
42  , mText( text )
43  , mLevel( level )
44  , mDuration( duration )
45  , mWidget( nullptr )
46  , mUserIcon( QIcon() )
47  , mLayout( nullptr )
48 {
49  writeContent();
50 }
51 
52 QgsMessageBarItem::QgsMessageBarItem( const QString &title, const QString &text, QWidget *widget, QgsMessageBar::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  , mLayout( nullptr )
61 {
62  writeContent();
63 }
64 
66  : QWidget( parent )
67  , mTitle( QLatin1String( "" ) )
68  , mText( QLatin1String( "" ) )
69  , mLevel( level )
70  , mDuration( duration )
71  , mWidget( widget )
72  , mUserIcon( QIcon() )
73  , mLayout( nullptr )
74 {
75  writeContent();
76 }
77 
78 void QgsMessageBarItem::writeContent()
79 {
80  if ( !mLayout )
81  {
82  mLayout = new QHBoxLayout( this );
83  mLayout->setContentsMargins( 0, 0, 0, 0 );
84  mTextEdit = nullptr;
85  mLblIcon = nullptr;
86  }
87 
88  // ICON
89  if ( !mLblIcon )
90  {
91  mLblIcon = new QLabel( this );
92  mLayout->addWidget( mLblIcon );
93  }
94  QIcon icon;
95  if ( !mUserIcon.isNull() )
96  {
97  icon = mUserIcon;
98  }
99  else
100  {
101  QString msgIcon( QStringLiteral( "/mIconInfo.svg" ) );
102  switch ( mLevel )
103  {
105  msgIcon = QStringLiteral( "/mIconCritical.png" );
106  break;
108  msgIcon = QStringLiteral( "/mIconWarning.svg" );
109  break;
111  msgIcon = QStringLiteral( "/mIconSuccess.png" );
112  break;
113  default:
114  break;
115  }
116  icon = QgsApplication::getThemeIcon( msgIcon );
117  }
118  mLblIcon->setPixmap( icon.pixmap( 24 ) );
119 
120  // TITLE AND TEXT
121  if ( mTitle.isEmpty() && mText.isEmpty() )
122  {
123  if ( mTextEdit )
124  {
125  delete mTextEdit;
126  mTextEdit = nullptr;
127  }
128  }
129  else
130  {
131  if ( !mTextEdit )
132  {
133  mTextEdit = new QTextEdit( this );
134  mTextEdit->setObjectName( QStringLiteral( "textEdit" ) );
135  mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
136  mTextEdit->setReadOnly( true );
137  mTextEdit->setFrameShape( QFrame::NoFrame );
138  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
139  // adjusts to height of font set in app options
140  mTextEdit->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
141  "QScrollBar { background-color: rgba(0,0,0,0); } "
142  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
143  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
144  mLayout->addWidget( mTextEdit );
145  }
146  QString content = mText;
147  if ( !mTitle.isEmpty() )
148  {
149  // add ':' to end of title
150  QString t = mTitle.trimmed();
151  if ( !content.isEmpty() && !t.endsWith( ':' ) && !t.endsWith( QLatin1String( ": " ) ) )
152  t += QLatin1String( ": " );
153  content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
154  }
155  mTextEdit->setText( content );
156  }
157 
158  // WIDGET
159  if ( mWidget )
160  {
161  QLayoutItem *item = mLayout->itemAt( 2 );
162  if ( !item || item->widget() != mWidget )
163  {
164  mLayout->addWidget( mWidget );
165  }
166  }
167 
168  // STYLESHEET
169  if ( mLevel == QgsMessageBar::SUCCESS )
170  {
171  mStyleSheet = "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
172  "QLabel,QTextEdit { color: black; } ";
173  }
174  else if ( mLevel == QgsMessageBar::CRITICAL )
175  {
176  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
177  "QLabel,QTextEdit { color: white; } ";
178  }
179  else if ( mLevel == QgsMessageBar::WARNING )
180  {
181  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
182  "QLabel,QTextEdit { color: black; } ";
183  }
184  else if ( mLevel == QgsMessageBar::INFO )
185  {
186  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
187  "QLabel,QTextEdit { color: #2554a1; } ";
188  }
189  mStyleSheet += QLatin1String( "QLabel#mItemCount { font-style: italic; }" );
190 }
191 
193 {
194  mText = text;
195  writeContent();
196  return this;
197 }
198 
199 QString QgsMessageBarItem::text() const
200 {
201  return mText;
202 }
203 
205 {
206  mTitle = title;
207  writeContent();
208  return this;
209 }
210 
212 {
213  return mTitle;
214 }
215 
217 {
218  mLevel = level;
219  writeContent();
220  emit styleChanged( mStyleSheet );
221  return this;
222 }
223 
225 {
226  return mLevel;
227 }
228 
230 {
231  if ( mWidget )
232  {
233  QLayoutItem *item = nullptr;
234  item = mLayout->itemAt( 2 );
235  if ( item->widget() == mWidget )
236  {
237  delete item->widget();
238  }
239  }
240  mWidget = widget;
241  writeContent();
242  return this;
243 }
244 
246 {
247  return mWidget;
248 }
249 
251 {
252  mUserIcon = icon;
253  return this;
254 }
255 
257 {
258  return mUserIcon;
259 }
260 
261 
263 {
264  mDuration = duration;
265  return this;
266 }
267 
QgsMessageBarItem(const QString &text, QgsMessageBar::MessageLevel level=QgsMessageBar::INFO, int duration=0, QWidget *parent=0)
make out a widget containing a message to be displayed on the bar
QString title() const
Returns the title for the message.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
int duration() const
returns the duration in second of the message
QIcon icon() const
Returns the icon for the message.
QgsMessageBarItem * setTitle(const QString &title)
QgsMessageBarItem * setWidget(QWidget *widget)
QgsMessageBar::MessageLevel level() const
Returns the message level for the message.
QgsMessageBarItem * setText(const QString &text)
QgsMessageBarItem * setIcon(const QIcon &icon)
QString text() const
Returns the text for the message.
QgsMessageBarItem * setLevel(QgsMessageBar::MessageLevel level)
QgsMessageBarItem * setDuration(int duration)
QWidget * widget() const
Returns the widget for the message.
void styleChanged(const QString &styleSheet)
emitted when the message level has changed