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