logtreeitem.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file logtreeitem.cpp
00013 ** \version $Id: logtreeitem.cpp 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Item representing a single message in the message log
00015 */
00016 
00017 #include <stringutil.h>
00018 
00019 #include "logtreeitem.h"
00020 #include "logtreewidget.h"
00021 
00022 /** Defines the format used for displaying the date and time of a log message.*/
00023 #define DATETIME_FMT  "MMM dd hh:mm:ss.zzz"
00024 
00025 /* Column index values */
00026 #define COL_TIME    LogTreeWidget::TimeColumn
00027 #define COL_TYPE    LogTreeWidget::TypeColumn
00028 #define COL_MESG    LogTreeWidget::MessageColumn
00029 #define ROLE_TYPE   Qt::UserRole
00030 
00031 
00032 /** Default constructor. */
00033 LogTreeItem::LogTreeItem(LogEvent::Severity type, QString message, 
00034                          QDateTime timestamp)
00035 : QTreeWidgetItem()
00036 {
00037   static quint32 seqnum = 0;
00038   
00039   /* Set this message's sequence number */
00040   _seqnum = seqnum++;
00041   /* Set the item's log time */
00042   setTimestamp(timestamp);
00043   /* Set the item's severity and appropriate color. */
00044   setSeverity(type);
00045   /* Set the item's message text. */
00046   setMessage(message);
00047 }
00048 
00049 /** Returns a printable string representing the fields of this item. */
00050 QString
00051 LogTreeItem::toString() const
00052 {
00053   return QString("%1 [%2] %3\n").arg(text(COL_TIME))
00054                                 .arg(text(COL_TYPE))
00055                                 .arg(text(COL_MESG).trimmed());
00056 }
00057 
00058 /** Sets the item's log time. */
00059 void
00060 LogTreeItem::setTimestamp(QDateTime timestamp)
00061 {
00062   QString strtime = timestamp.toString(DATETIME_FMT);
00063   setText(COL_TIME, strtime);
00064   setToolTip(COL_TIME, strtime);
00065 }
00066 
00067 /** Sets the item's severity and the appropriate background color. */
00068 void
00069 LogTreeItem::setSeverity(LogEvent::Severity type)
00070 {
00071   /* Change row and text color for serious warnings and errors. */
00072   if (type == LogEvent::Error) {
00073     /* Critical messages are red with white text. */
00074     for (int i = 0; i < 3; i++) {
00075       setBackgroundColor(i, Qt::red);
00076       setTextColor(i, Qt::white);
00077     }
00078   } else if (type == LogEvent::Warn) {
00079     /* Warning messages are yellow with black text. */
00080     for (int i = 0; i < 3; i++) {
00081       setBackgroundColor(i, Qt::yellow);
00082     }
00083   }
00084   
00085   setTextAlignment(COL_TYPE, Qt::AlignCenter);
00086   setText(COL_TYPE, LogEvent::severityToString(type));
00087   setData(COL_TYPE, ROLE_TYPE, (uint)type);
00088 }
00089 
00090 /** Sets the item's message text. */
00091 void
00092 LogTreeItem::setMessage(QString message)
00093 {
00094   setText(COL_MESG, message);
00095   setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n"));
00096 }
00097 
00098 /** Returns the severity associated with this log item. */
00099 LogEvent::Severity
00100 LogTreeItem::severity() const
00101 {
00102   return (LogEvent::Severity)data(COL_TYPE, ROLE_TYPE).toUInt();
00103 }
00104 
00105 /** Returns the timestamp for this log message. */
00106 QDateTime
00107 LogTreeItem::timestamp() const
00108 {
00109   return QDateTime::fromString(text(COL_TIME), DATETIME_FMT);
00110 }
00111 
00112 /** Returns the message for this log item. */
00113 QString
00114 LogTreeItem::message() const
00115 {
00116   return text(COL_MESG);
00117 }
00118 
00119 /** Compares <b>other</b> to this log message item based on the current sort
00120  * column. */
00121 bool
00122 LogTreeItem::operator<(const QTreeWidgetItem &other) const
00123 {
00124   LogTreeItem *that = (LogTreeItem *)&other;
00125   int sortColumn = (treeWidget() ? treeWidget()->sortColumn() : COL_TIME);
00126    
00127   switch (sortColumn) {
00128     case COL_TIME:
00129       /* Sort chronologically */
00130       return (this->_seqnum < that->_seqnum);
00131     case COL_TYPE:
00132       /* Sort by severity, then chronologically */
00133       if (this->severity() == that->severity()) {
00134         return (this->_seqnum < that->_seqnum);
00135       }
00136       /* The comparison is flipped because higher severities have 
00137        * lower numeric values */
00138       return (this->severity() > that->severity());
00139     default:
00140       /* Sort by message, then chronologically */
00141       QString thisMessage = this->message().toLower();
00142       QString thatMessage = that->message().toLower();
00143       
00144       if (thisMessage == thatMessage) {
00145         return (this->_seqnum < that->_seqnum);
00146       }
00147       return (thisMessage < thatMessage);
00148   }
00149   return QTreeWidgetItem::operator<(other);
00150 }
00151 

Generated on Wed Nov 26 21:02:42 2008 for Vidalia by  doxygen 1.5.7.1