Vidalia 0.3.1
LogTreeItem.cpp
Go to the documentation of this file.
1/*
2** This file is part of Vidalia, and is subject to the license terms in the
3** LICENSE file, found in the top level directory of this distribution. If you
4** did not receive the LICENSE file with this file, you may obtain it from the
5** Vidalia source package distributed by the Vidalia Project at
6** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7** including this file, may be copied, modified, propagated, or distributed
8** except according to the terms described in the LICENSE file.
9*/
10
11/*
12** \file LogTreeItem.cpp
13** \brief Item representing a single message in the message log
14*/
15
16#include "LogTreeItem.h"
17#include "LogTreeWidget.h"
18
19#include "stringutil.h"
20
21/** Defines the format used for displaying the date and time of a log message.*/
22#define DATETIME_FMT "MMM dd hh:mm:ss.zzz"
23
24/* Column index values */
25#define COL_TIME LogTreeWidget::TimeColumn
26#define COL_TYPE LogTreeWidget::TypeColumn
27#define COL_MESG LogTreeWidget::MessageColumn
28#define ROLE_TYPE Qt::UserRole
29
30
31/** Default constructor. */
32LogTreeItem::LogTreeItem(tc::Severity type, const QString &message,
33 const QDateTime &timestamp)
34 : QTreeWidgetItem()
35{
36 static quint32 seqnum = 0;
37
38 /* Set this message's sequence number */
39 _seqnum = seqnum++;
40 /* Set the item's log time */
42 /* Set the item's severity and appropriate color. */
43 setSeverity(type);
44 /* Set the item's message text. */
46}
47
48/** Returns a printable string representing the fields of this item. */
49QString
51{
52 return QString("%1 [%2] %3").arg(text(COL_TIME))
53 .arg(text(COL_TYPE))
54 .arg(text(COL_MESG).trimmed());
55}
56
57/** Sets the item's log time. */
58void
59LogTreeItem::setTimestamp(const QDateTime &timestamp)
60{
61 QString strtime = timestamp.toString(DATETIME_FMT);
62 setText(COL_TIME, strtime);
63 setToolTip(COL_TIME, strtime);
64}
65
66/** Sets the item's severity and the appropriate background color. */
67void
69{
70 /* Change row and text color for serious warnings and errors. */
71 if (type == tc::ErrorSeverity) {
72 /* Critical messages are red with white text. */
73 for (int i = 0; i < 3; i++) {
74 setBackgroundColor(i, Qt::red);
75 setTextColor(i, Qt::white);
76 }
77 } else if (type == tc::WarnSeverity) {
78 /* Warning messages are yellow with black text. */
79 for (int i = 0; i < 3; i++) {
80 setBackgroundColor(i, Qt::yellow);
81 }
82 }
83
84 setTextAlignment(COL_TYPE, Qt::AlignCenter);
85 setText(COL_TYPE, severityToString(type));
86 setData(COL_TYPE, ROLE_TYPE, (uint)type);
87}
88
89/** Sets the item's message text. */
90void
91LogTreeItem::setMessage(const QString &message)
92{
93 setText(COL_MESG, message);
94 setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n"));
95}
96
97/** Returns the severity associated with this log item. */
100{
101 return (tc::Severity)data(COL_TYPE, ROLE_TYPE).toUInt();
102}
103
104/** Returns the timestamp for this log message. */
105QDateTime
107{
108 return QDateTime::fromString(text(COL_TIME), DATETIME_FMT);
109}
110
111/** Returns the message for this log item. */
112QString
114{
115 return text(COL_MESG);
116}
117
118/** Converts a tc::Severity enum value to a localized string description. */
119QString
121{
122 QString str;
123 switch (severity) {
124 case tc::DebugSeverity: str = tr("Debug"); break;
125 case tc::InfoSeverity: str = tr("Info"); break;
126 case tc::NoticeSeverity: str = tr("Notice"); break;
127 case tc::WarnSeverity: str = tr("Warning"); break;
128 case tc::ErrorSeverity: str = tr("Error"); break;
129 default: str = tr("Unknown"); break;
130 }
131 return str;
132}
133
134/** Compares <b>other</b> to this log message item based on the current sort
135 * column. */
136bool
137LogTreeItem::operator<(const QTreeWidgetItem &other) const
138{
139 LogTreeItem *that = (LogTreeItem *)&other;
140 int sortColumn = (treeWidget() ? treeWidget()->sortColumn() : COL_TIME);
141
142 switch (sortColumn) {
143 case COL_TIME:
144 /* Sort chronologically */
145 return (this->_seqnum < that->_seqnum);
146 case COL_TYPE:
147 /* Sort by severity, then chronologically */
148 if (this->severity() == that->severity()) {
149 return (this->_seqnum < that->_seqnum);
150 }
151 /* The comparison is flipped because higher severities have
152 * lower numeric values */
153 return (this->severity() > that->severity());
154 default:
155 /* Sort by message, then chronologically */
156 QString thisMessage = this->message().toLower();
157 QString thatMessage = that->message().toLower();
158
159 if (thisMessage == thatMessage) {
160 return (this->_seqnum < that->_seqnum);
161 }
162 return (thisMessage < thatMessage);
163 }
164 return QTreeWidgetItem::operator<(other);
165}
166
#define ROLE_TYPE
Definition: LogTreeItem.cpp:28
#define COL_TYPE
Definition: LogTreeItem.cpp:26
#define DATETIME_FMT
Definition: LogTreeItem.cpp:22
#define COL_MESG
Definition: LogTreeItem.cpp:27
#define COL_TIME
Definition: LogTreeItem.cpp:25
QDateTime timestamp() const
void setMessage(const QString &message)
Definition: LogTreeItem.cpp:91
tc::Severity severity() const
Definition: LogTreeItem.cpp:99
quint32 _seqnum
Definition: LogTreeItem.h:61
QString toString() const
Definition: LogTreeItem.cpp:50
void setSeverity(tc::Severity type)
Definition: LogTreeItem.cpp:68
LogTreeItem(tc::Severity type, const QString &message, const QDateTime &timestamp=QDateTime::currentDateTime())
Definition: LogTreeItem.cpp:32
QString message() const
static QString severityToString(tc::Severity severity)
void setTimestamp(const QDateTime &timestamp)
Definition: LogTreeItem.cpp:59
virtual bool operator<(const QTreeWidgetItem &other) const
QString i(QString str)
Definition: html.cpp:32
Severity
Definition: tcglobal.h:69
@ DebugSeverity
Definition: tcglobal.h:71
@ NoticeSeverity
Definition: tcglobal.h:73
@ ErrorSeverity
Definition: tcglobal.h:75
@ WarnSeverity
Definition: tcglobal.h:74
@ InfoSeverity
Definition: tcglobal.h:72
QString string_wrap(const QString &str, int width, const QString &sep, const QString &le)
Definition: stringutil.cpp:75