QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 : denis.rouzaud@gmail.com
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( "" )
29  , mText( text )
30  , mLevel( level )
31  , mDuration( duration )
32  , mWidget( 0 )
33  , mUserIcon( QIcon() )
34  , mLayout( 0 )
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( 0 )
46  , mUserIcon( QIcon() )
47  , mLayout( 0 )
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( 0 )
61 {
62  writeContent();
63 }
64 
65 QgsMessageBarItem::QgsMessageBarItem( QWidget *widget, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
66  : QWidget( parent )
67  , mTitle( "" )
68  , mText( "" )
69  , mLevel( level )
70  , mDuration( duration )
71  , mWidget( widget )
72  , mUserIcon( QIcon() )
73  , mLayout( 0 )
74 {
75  writeContent();
76 }
77 
79 {
80 }
81 
82 void QgsMessageBarItem::writeContent()
83 {
84  if ( mLayout == 0 )
85  {
86  mLayout = new QHBoxLayout( this );
87  mLayout->setContentsMargins( 0, 0, 0, 0 );
88  mTextEdit = 0;
89  mLblIcon = 0;
90  }
91 
92  // ICON
93  if ( mLblIcon == 0 )
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( "/mIconInfo.png" );
106  switch ( mLevel )
107  {
109  msgIcon = QString( "/mIconCritical.png" );
110  break;
112  msgIcon = QString( "/mIconWarn.png" );
113  break;
114  default:
115  break;
116  }
117  icon = QgsApplication::getThemeIcon( msgIcon );
118  }
119  mLblIcon->setPixmap( icon.pixmap( 24 ) );
120 
121  // TITLE AND TEXT
122  if ( mTitle.isEmpty() && mText.isEmpty() )
123  {
124  if ( mTextEdit != 0 )
125  {
126  delete mTextEdit;
127  mTextEdit = 0;
128  }
129  }
130  else
131  {
132  if ( mTextEdit == 0 )
133  {
134  mTextEdit = new QTextEdit( this );
135  mTextEdit->setObjectName( "textEdit" );
136  mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
137  mTextEdit->setReadOnly( true );
138  mTextEdit->setFrameShape( QFrame::NoFrame );
139  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
140  // adjusts to height of font set in app options
141  mTextEdit->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
142  "QScrollBar { background-color: rgba(0,0,0,0); } "
143  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
144  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
145  mLayout->addWidget( mTextEdit );
146  }
147  QString content = mText;
148  if ( !mTitle.isEmpty() )
149  {
150  // add ':' to end of title
151  QString t = mTitle.trimmed();
152  if ( !content.isEmpty() && !t.endsWith( ":" ) && !t.endsWith( ": " ) )
153  t += ": ";
154  content.prepend( QString( "<b>" ) + t + " </b>" );
155  }
156  mTextEdit->setText( content );
157  }
158 
159  // WIDGET
160  if ( mWidget != 0 )
161  {
162  QLayoutItem *item = mLayout->itemAt( 2 );
163  if ( !item || item->widget() != mWidget )
164  {
165  mLayout->addWidget( mWidget );
166  }
167  }
168 
169  // STYLESHEET
170  if ( mLevel >= QgsMessageBar::CRITICAL )
171  {
172  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
173  "QLabel,QTextEdit { color: white; } ";
174  }
175  else if ( mLevel == QgsMessageBar::WARNING )
176  {
177  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
178  "QLabel,QTextEdit { color: black; } ";
179  }
180  else if ( mLevel <= QgsMessageBar::INFO )
181  {
182  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
183  "QLabel,QTextEdit { color: #2554a1; } ";
184  }
185  mStyleSheet += "QLabel#mItemCount { font-style: italic; }";
186 }
187 
189 {
190  mText = text;
191  writeContent();
192  return this;
193 }
194 
196 {
197  mTitle = title;
198  writeContent();
199  return this;
200 }
201 
203 {
204  mLevel = level;
205  writeContent();
206  emit styleChanged( mStyleSheet );
207  return this;
208 }
209 
211 {
212  if ( mWidget != 0 )
213  {
214  QLayoutItem *item;
215  item = mLayout->itemAt( 2 );
216  if ( item->widget() == mWidget )
217  {
218  delete item->widget();
219  }
220  }
221  mWidget = widget;
222  writeContent();
223  return this;
224 }
225 
227 {
228  mUserIcon = icon;
229  return this;
230 }
231 
232 
234 {
235  mDuration = duration;
236  return this;
237 }
238 
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
QgsMessageBarItem * setText(QString text)
void styleChanged(QString styleSheet)
emitted when the message level has changed
QgsMessageBarItem * setTitle(QString title)
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
static QIcon icon(QString icon)
QgsMessageBar::MessageLevel level()
returns the level
QgsMessageBarItem * setWidget(QWidget *widget)
QgsMessageBarItem * setIcon(const QIcon &icon)
int duration() const
returns the duration in second of the message
QgsMessageBarItem * setLevel(QgsMessageBar::MessageLevel level)
QgsMessageBarItem * setDuration(int duration)