logtreeitem.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file logtreeitem.cpp
00024  * \version $Id: logtreeitem.cpp 1751 2007-05-26 04:08:20Z edmanm $
00025  * \brief Item representing a single message in the message log
00026  */
00027 
00028 #include <util/string.h>
00029 
00030 #include "logtreeitem.h"
00031 #include "logtreewidget.h"
00032 
00033 /** Defines the format used for displaying the date and time of a log message.*/
00034 #define DATETIME_FMT  "MMM dd hh:mm:ss.zzz"
00035 
00036 /* Column index values */
00037 #define COL_TIME    LogTreeWidget::TimeColumn
00038 #define COL_TYPE    LogTreeWidget::TypeColumn
00039 #define COL_MESG    LogTreeWidget::MessageColumn
00040 #define ROLE_TYPE   Qt::UserRole
00041 
00042 
00043 /** Default constructor. */
00044 LogTreeItem::LogTreeItem(LogEvent::Severity type, QString message, 
00045                          QDateTime timestamp)
00046 : QTreeWidgetItem()
00047 {
00048   static quint32 seqnum = 0;
00049   
00050   /* Set this message's sequence number */
00051   _seqnum = seqnum++;
00052   /* Set the item's log time */
00053   setTimestamp(timestamp);
00054   /* Set the item's severity and appropriate color. */
00055   setSeverity(type);
00056   /* Set the item's message text. */
00057   setMessage(message);
00058 }
00059 
00060 /** Returns a printable string representing the fields of this item. */
00061 QString
00062 LogTreeItem::toString() const
00063 {
00064   return QString("%1 [%2] %3\n").arg(text(COL_TIME))
00065                                 .arg(text(COL_TYPE))
00066                                 .arg(text(COL_MESG).trimmed());
00067 }
00068 
00069 /** Sets the item's log time. */
00070 void
00071 LogTreeItem::setTimestamp(QDateTime timestamp)
00072 {
00073   QString strtime = timestamp.toString(DATETIME_FMT);
00074   setText(COL_TIME, strtime);
00075   setToolTip(COL_TIME, strtime);
00076 }
00077 
00078 /** Sets the item's severity and the appropriate background color. */
00079 void
00080 LogTreeItem::setSeverity(LogEvent::Severity type)
00081 {
00082   /* Change row and text color for serious warnings and errors. */
00083   if (type == LogEvent::Error) {
00084     /* Critical messages are red with white text. */
00085     for (int i = 0; i < 3; i++) {
00086       setBackgroundColor(i, Qt::red);
00087       setTextColor(i, Qt::white);
00088     }
00089   } else if (type == LogEvent::Warn) {
00090     /* Warning messages are yellow with black text. */
00091     for (int i = 0; i < 3; i++) {
00092       setBackgroundColor(i, Qt::yellow);
00093     }
00094   }
00095   
00096   setTextAlignment(COL_TYPE, Qt::AlignCenter);
00097   setText(COL_TYPE, LogEvent::severityToString(type));
00098   setData(COL_TYPE, ROLE_TYPE, (uint)type);
00099 }
00100 
00101 /** Sets the item's message text. */
00102 void
00103 LogTreeItem::setMessage(QString message)
00104 {
00105   setText(COL_MESG, message);
00106   setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n"));
00107 }
00108 
00109 /** Returns the severity associated with this log item. */
00110 LogEvent::Severity
00111 LogTreeItem::severity() const
00112 {
00113   return (LogEvent::Severity)data(COL_TYPE, ROLE_TYPE).toUInt();
00114 }
00115 
00116 /** Returns the timestamp for this log message. */
00117 QDateTime
00118 LogTreeItem::timestamp() const
00119 {
00120   return QDateTime::fromString(text(COL_TIME), DATETIME_FMT);
00121 }
00122 
00123 /** Returns the message for this log item. */
00124 QString
00125 LogTreeItem::message() const
00126 {
00127   return text(COL_MESG);
00128 }
00129 
00130 /** Compares <b>other</b> to this log message item based on the current sort
00131  * column. */
00132 bool
00133 LogTreeItem::operator<(const QTreeWidgetItem &other) const
00134 {
00135   LogTreeItem *that = (LogTreeItem *)&other;
00136   int sortColumn = (treeWidget() ? treeWidget()->sortColumn() : COL_TIME);
00137    
00138   switch (sortColumn) {
00139     case COL_TIME:
00140       /* Sort chronologically */
00141       return (this->_seqnum < that->_seqnum);
00142     case COL_TYPE:
00143       /* Sort by severity, then chronologically */
00144       if (this->severity() == that->severity()) {
00145         return (this->_seqnum < that->_seqnum);
00146       }
00147       /* The comparison is flipped because higher severities have 
00148        * lower numeric values */
00149       return (this->severity() > that->severity());
00150     default:
00151       /* Sort by message, then chronologically */
00152       QString thisMessage = this->message().toLower();
00153       QString thatMessage = that->message().toLower();
00154       
00155       if (thisMessage == thatMessage) {
00156         return (this->_seqnum < that->_seqnum);
00157       }
00158       return (thisMessage < thatMessage);
00159   }
00160   return QTreeWidgetItem::operator<(other);
00161 }
00162 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3