1  /*
2   * wpa_gui - EventHistory class
3   * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4   *
5   * This software may be distributed under the terms of the BSD license.
6   * See README for more details.
7   */
8  
9  #include <QHeaderView>
10  #include <QScrollBar>
11  
12  #include "eventhistory.h"
13  
14  
rowCount(const QModelIndex &) const15  int EventListModel::rowCount(const QModelIndex &) const
16  {
17  	return msgList.count();
18  }
19  
20  
columnCount(const QModelIndex &) const21  int EventListModel::columnCount(const QModelIndex &) const
22  {
23  	return 2;
24  }
25  
26  
data(const QModelIndex & index,int role) const27  QVariant EventListModel::data(const QModelIndex &index, int role) const
28  {
29  	if (!index.isValid())
30  		return QVariant();
31  
32          if (role == Qt::DisplayRole)
33  		if (index.column() == 0) {
34  			if (index.row() >= timeList.size())
35  				return QVariant();
36  			return timeList.at(index.row());
37  		} else {
38  			if (index.row() >= msgList.size())
39  				return QVariant();
40  			return msgList.at(index.row());
41  		}
42          else
43  		return QVariant();
44  }
45  
46  
headerData(int section,Qt::Orientation orientation,int role) const47  QVariant EventListModel::headerData(int section, Qt::Orientation orientation,
48  				    int role) const
49  {
50  	if (role != Qt::DisplayRole)
51  		return QVariant();
52  
53  	if (orientation == Qt::Horizontal) {
54  		switch (section) {
55  		case 0:
56  			return QString(tr("Timestamp"));
57  		case 1:
58  			return QString(tr("Message"));
59  		default:
60  			return QVariant();
61  		}
62  	} else
63  		return QString("%1").arg(section);
64  }
65  
66  
addEvent(QString time,QString msg)67  void EventListModel::addEvent(QString time, QString msg)
68  {
69  	beginInsertRows(QModelIndex(), msgList.size(), msgList.size() + 1);
70  	timeList << time;
71  	msgList << msg;
72  	endInsertRows();
73  }
74  
75  
EventHistory(QWidget * parent,const char *,bool,Qt::WindowFlags)76  EventHistory::EventHistory(QWidget *parent, const char *, bool, Qt::WindowFlags)
77  	: QDialog(parent)
78  {
79  	setupUi(this);
80  
81  	connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
82  
83  	eventListView->setItemsExpandable(false);
84  	eventListView->setRootIsDecorated(false);
85  	elm = new EventListModel(parent);
86  	eventListView->setModel(elm);
87  }
88  
89  
~EventHistory()90  EventHistory::~EventHistory()
91  {
92  	destroy();
93  	delete elm;
94  }
95  
96  
languageChange()97  void EventHistory::languageChange()
98  {
99  	retranslateUi(this);
100  }
101  
102  
addEvents(WpaMsgList msgs)103  void EventHistory::addEvents(WpaMsgList msgs)
104  {
105  	WpaMsgList::iterator it;
106  	for (it = msgs.begin(); it != msgs.end(); it++)
107  		addEvent(*it);
108  }
109  
110  
addEvent(WpaMsg msg)111  void EventHistory::addEvent(WpaMsg msg)
112  {
113  	bool scroll = true;
114  
115  	if (eventListView->verticalScrollBar()->value() <
116  	    eventListView->verticalScrollBar()->maximum())
117  	    	scroll = false;
118  
119  	elm->addEvent(msg.getTimestamp().toString("yyyy-MM-dd hh:mm:ss.zzz"),
120  		      msg.getMsg());
121  
122  	if (scroll)
123  		eventListView->scrollToBottom();
124  }
125