QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 : [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( "" )
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;
115  msgIcon = QString( "/mIconSuccess.png" );
116  break;
117  default:
118  break;
119  }
120  icon = QgsApplication::getThemeIcon( msgIcon );
121  }
122  mLblIcon->setPixmap( icon.pixmap( 24 ) );
123 
124  // TITLE AND TEXT
125  if ( mTitle.isEmpty() && mText.isEmpty() )
126  {
127  if ( mTextEdit != 0 )
128  {
129  delete mTextEdit;
130  mTextEdit = 0;
131  }
132  }
133  else
134  {
135  if ( mTextEdit == 0 )
136  {
137  mTextEdit = new QTextEdit( this );
138  mTextEdit->setObjectName( "textEdit" );
139  mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
140  mTextEdit->setReadOnly( true );
141  mTextEdit->setFrameShape( QFrame::NoFrame );
142  // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
143  // adjusts to height of font set in app options
144  mTextEdit->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
145  "QScrollBar { background-color: rgba(0,0,0,0); } "
146  "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
147  "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
148  mLayout->addWidget( mTextEdit );
149  }
150  QString content = mText;
151  if ( !mTitle.isEmpty() )
152  {
153  // add ':' to end of title
154  QString t = mTitle.trimmed();
155  if ( !content.isEmpty() && !t.endsWith( ":" ) && !t.endsWith( ": " ) )
156  t += ": ";
157  content.prepend( QString( "<b>" ) + t + " </b>" );
158  }
159  mTextEdit->setText( content );
160  }
161 
162  // WIDGET
163  if ( mWidget != 0 )
164  {
165  QLayoutItem *item = mLayout->itemAt( 2 );
166  if ( !item || item->widget() != mWidget )
167  {
168  mLayout->addWidget( mWidget );
169  }
170  }
171 
172  // STYLESHEET
173  if ( mLevel == QgsMessageBar::SUCCESS )
174  {
175  mStyleSheet = "QgsMessageBar { background-color: #dff0d8; border: 1px solid #8e998a; } "
176  "QLabel,QTextEdit { color: black; } ";
177  }
178  else if ( mLevel == QgsMessageBar::CRITICAL )
179  {
180  mStyleSheet = "QgsMessageBar { background-color: #d65253; border: 1px solid #9b3d3d; } "
181  "QLabel,QTextEdit { color: white; } ";
182  }
183  else if ( mLevel == QgsMessageBar::WARNING )
184  {
185  mStyleSheet = "QgsMessageBar { background-color: #ffc800; border: 1px solid #e0aa00; } "
186  "QLabel,QTextEdit { color: black; } ";
187  }
188  else if ( mLevel == QgsMessageBar::INFO )
189  {
190  mStyleSheet = "QgsMessageBar { background-color: #e7f5fe; border: 1px solid #b9cfe4; } "
191  "QLabel,QTextEdit { color: #2554a1; } ";
192  }
193  mStyleSheet += "QLabel#mItemCount { font-style: italic; }";
194 }
195 
197 {
198  mText = text;
199  writeContent();
200  return this;
201 }
202 
204 {
205  mTitle = title;
206  writeContent();
207  return this;
208 }
209 
211 {
212  mLevel = level;
213  writeContent();
214  emit styleChanged( mStyleSheet );
215  return this;
216 }
217 
219 {
220  if ( mWidget != 0 )
221  {
222  QLayoutItem *item;
223  item = mLayout->itemAt( 2 );
224  if ( item->widget() == mWidget )
225  {
226  delete item->widget();
227  }
228  }
229  mWidget = widget;
230  writeContent();
231  return this;
232 }
233 
235 {
236  mUserIcon = icon;
237  return this;
238 }
239 
240 
242 {
243  mDuration = duration;
244  return this;
245 }
246