OpenWalnut  1.5.0dev
WQtMessageDock.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <iostream>
26 
27 #include <QAction>
28 #include <QDockWidget>
29 #include <QVBoxLayout>
30 #include <QHBoxLayout>
31 #include <QLabel>
32 #include <QWidget>
33 #include <QComboBox>
34 
35 #include "WQtGui.h"
36 #include "WMainWindow.h"
37 #include "WQtMessagePopup.h"
38 
39 #include "WQtMessageDock.h"
40 
41 #define MAXITEMS 50
42 
43 WQtMessageDock::WQtMessageDock( QString dockTitle, QWidget* parent ):
44  WQtDockWidget( dockTitle, parent )
45 {
46  setObjectName( "MessageDock:" + dockTitle );
47  setAllowedAreas( Qt::AllDockWidgetAreas );
48  setFeatures( QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable );
49 
50  // log messages
51  m_logList = new QListWidget( this );
52  setWidget( m_logList );
53 
54  // filter list
55  m_filterCombo = new QComboBox();
56  m_filterCombo->setToolTip( "Filter the messages by level. When choosing a level, all messages with this and an above level will be displayed." );
57  // m_filterCombo->addItem( "Debug" ); // disabled. See issue #283
58  m_filterCombo->addItem( "Info" );
59  m_filterCombo->addItem( "Warning" );
60  m_filterCombo->addItem( "Error" );
61  m_filterCombo->setCurrentIndex(
62  WMainWindow::getSettings().value( "MessageDockFilterIndex", 1 ).toInt()
63  ); // warning is the default
64 
65  // connect filter combo
66  connect( m_filterCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( handleFilterUpdate() ) );
67 
68  // clear list action
69  QAction* clearAction = new QAction( WQtGui::getMainWindow()->getIconManager()->getIcon( "clear" ), "Clear Messages", this );
70  connect( clearAction, SIGNAL( triggered() ), this, SLOT( clearMessages() ) );
71 
72  // add everything too the title list
73  addTitleAction( clearAction );
75 }
76 
78 {
79  // cleanup
80 }
81 
83 {
84  addLogMessage( QString::fromStdString( entry.getSource() ),
85  QString::fromStdString( entry.getMessage() ),
86  entry.getLogLevel() );
87 }
88 
89 void WQtMessageDock::addLogMessage( QString sender, QString message, WQtMessagePopup::MessageType type )
90 {
91  addMessage( "Log message from " + sender, message, type );
92 }
93 
94 void WQtMessageDock::addMessage( QString title, QString message, WQtMessagePopup::MessageType type )
95 {
96  WQtMessagePopup* w = new WQtMessagePopup( m_logList, title, message, type );
97  w->setAutoClose( false );
98  w->setShowCloseButton( false );
99  w->setAutoPosition( false );
100 
102  item->setSizeHint( QSize( 0, w->sizeHint().height() ) );
103  m_logList->addItem( item );
104  m_logList->setItemWidget( item, w );
105 
106  // hide messages not matching the filter
107  if( type < ( m_filterCombo->currentIndex() + 1 ) )
108  {
109  item->setHidden( true );
110  }
111  else
112  {
113  // if visible, jump to item
114  m_logList->scrollToBottom();
115  }
116 
117  // ensure we only have MAXITEMS items
118  if( m_logList->count() > MAXITEMS )
119  {
120  // clean up the oldest items
121  for( int i = 0; i < m_logList->count() - MAXITEMS; ++i )
122  {
123  QListWidgetItem* li = m_logList->item( i );
124  delete li;
125  }
126  }
127 }
128 
130 {
131  WMainWindow::getSettings().setValue( "MessageDockFilterIndex", m_filterCombo->currentIndex() );
132 }
133 
135 {
136  for( int i = 0; i < m_logList->count(); ++i )
137  {
138  QListWidgetItem* li = m_logList->item( i );
139  QWidget* w = m_logList->itemWidget( li );
140  WQtMessagePopup* popup = dynamic_cast< WQtMessagePopup* >( w );
141  if( popup )
142  {
143  li->setHidden( popup->getType() < ( m_filterCombo->currentIndex() + 1 ) );
144  }
145  }
146 }
147 
149 {
150  m_logList->clear();
151 }
Represents a simple log message with some attributes.
Definition: WLogEntry.h:57
LogLevel getLogLevel() const
Definition: WLogEntry.cpp:116
std::string getMessage() const
Returns the plain message of the entry.
Definition: WLogEntry.cpp:121
std::string getSource() const
Returns the sender of the log.
Definition: WLogEntry.cpp:126
static QSettings & getSettings()
Returns the settings object.
Advanced QDockWidget.
Definition: WQtDockWidget.h:50
virtual void addTitleWidget(QWidget *widget)
Add an arbitrary widget.
virtual void addTitleAction(QAction *action, bool instantPopup=false)
Add the given action to the titlebar.
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
void saveSettings()
Save state to settings.
QListWidget * m_logList
The list.
void addMessage(QString title, QString message, WQtMessagePopup::MessageType type)
Add a message to the dock.
void addLogMessage(QString sender, QString message, WQtMessagePopup::MessageType type)
Add a message to the dock.
QComboBox * m_filterCombo
The message filter.
WQtMessageDock(QString dockTitle, QWidget *parent)
Constructor.
void clearMessages()
Clear the message list.
virtual ~WQtMessageDock()
Destructor.
void handleFilterUpdate()
Handles changes in the filter combo.
Nice looking message popup.
void setAutoClose(bool autoClose=true)
When set to true, the widget gets closed when loosing focus or when clicking the detail button.
MessageType getType() const
Get this popups message type.
void setShowCloseButton(bool showCloseButton=true)
Show or hide the close button.
void setAutoPosition(bool autoPosition=true)
If true, the widget moves itself to the bottom of its parent widget.