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: messagelog.cpp 797 2006-05-09 04:42:35Z edmanm $
00025  * \brief Item representing a single message in the message log
00026  */
00027 
00028 #include <util/string.h>
00029 #include "logtreeitem.h"
00030 #include "logtreewidget.h"
00031 
00032 /** Defines the format used for displaying the date and time of a log message.*/
00033 #define DATETIME_FMT  "MMM dd hh:mm:ss:zzz"
00034 
00035 /* Column index values */
00036 #define COL_TIME    LogTreeWidget::TimeColumn
00037 #define COL_TYPE    LogTreeWidget::TypeColumn
00038 #define COL_MESG    LogTreeWidget::MessageColumn
00039 #define ROLE_TYPE   Qt::UserRole
00040 
00041 /** Vertical padding on message log rows. */
00042 #if defined(Q_WS_MAC)
00043 /* OS X requires less vertical padding than the rest of the world, for
00044  * some unknown reason. */
00045 #define VERTICAL_PADDING  1
00046 #else
00047 #define VERTICAL_PADDING  10
00048 #endif
00049 
00050 
00051 /** Default constructor. */
00052 LogTreeItem::LogTreeItem(LogEvent::Severity type, QString message, 
00053                          QDateTime timestamp)
00054 : QTreeWidgetItem()
00055 {
00056   /* Set the item's log time */
00057   setTimestamp(timestamp);
00058   /* Set the item's severity and appropriate color. */
00059   setSeverity(type);
00060   /* Set the item's message text. */
00061   setMessage(message);
00062 
00063 #if QT_VERSION > 0x040100
00064   /* Qt versions newer than 4.1.0 have a quirk in that they make the message
00065    * log rows appear very tall. So, make them just a hair taller than the font
00066    * height. */
00067   int rowHeight = font(COL_MESG).pointSize()+VERTICAL_PADDING;
00068   setSizeHint(COL_TIME, QSize(sizeHint(COL_TIME).width(), rowHeight));
00069 #endif
00070 }
00071 
00072 /** Returns a printable string representing the fields of this item. */
00073 QString
00074 LogTreeItem::toString()
00075 {
00076   return QString("%1 [%2] %3\n").arg(text(COL_TIME))
00077                                 .arg(text(COL_TYPE))
00078                                 .arg(text(COL_MESG).trimmed());
00079 }
00080 
00081 /** Sets the item's log time. */
00082 void
00083 LogTreeItem::setTimestamp(QDateTime timestamp)
00084 {
00085   QString strtime = timestamp.toString(DATETIME_FMT);
00086   setText(COL_TIME, strtime);
00087   setToolTip(COL_TIME, strtime);
00088 }
00089 
00090 /** Sets the item's severity and the appropriate background color. */
00091 void
00092 LogTreeItem::setSeverity(LogEvent::Severity type)
00093 {
00094   /* Change row and text color for serious warnings and errors. */
00095   if (type == LogEvent::Error) {
00096     /* Critical messages are red with white text. */
00097     for (int i = 0; i < 3; i++) {
00098       setBackgroundColor(i, Qt::red);
00099       setTextColor(i, Qt::white);
00100     }
00101   } else if (type == LogEvent::Warn) {
00102     /* Warning messages are yellow with black text. */
00103     for (int i = 0; i < 3; i++) {
00104       setBackgroundColor(i, Qt::yellow);
00105     }
00106   }
00107   
00108   setTextAlignment(COL_TYPE, Qt::AlignCenter);
00109   setText(COL_TYPE, LogEvent::severityToString(type));
00110   setData(COL_TYPE, ROLE_TYPE, (uint)type);
00111 }
00112 
00113 /** Sets the item's message text. */
00114 void
00115 LogTreeItem::setMessage(QString message)
00116 {
00117   setText(COL_MESG, message);
00118   setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n"));
00119 }
00120 
00121 /** Returns the severity associated with this log item. */
00122 LogEvent::Severity
00123 LogTreeItem::severity()
00124 {
00125   return (LogEvent::Severity)data(COL_TYPE, ROLE_TYPE).toUInt();
00126 }
00127 
00128 /** Returns the timestamp for this log message. */
00129 QDateTime
00130 LogTreeItem::timestamp() const
00131 {
00132   return QDateTime::fromString(text(COL_TIME), DATETIME_FMT);
00133 }
00134 
00135 /** Returns the message for this log item. */
00136 QString
00137 LogTreeItem::message()
00138 {
00139   return text(COL_MESG);
00140 }
00141 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0