logtreewidget.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 logtreewidget.cpp
00024  * \version $Id: messagelog.cpp 797 2006-05-09 04:42:35Z edmanm $
00025  * \brief Contains a collection of log messages as LogTreeItems
00026  */
00027 
00028 #include <QScrollBar>
00029 
00030 #include "logtreewidget.h"
00031 #include "logheaderview.h"
00032 
00033 
00034 /** Default constructor. */
00035 LogTreeWidget::LogTreeWidget(QWidget *parent)
00036 : QTreeWidget(parent)
00037 {
00038   setHeader(new LogHeaderView(this));
00039   
00040   setStatusTip(tr("Messages Shown: ") + "0");
00041 
00042   /* Default to always scrolling to the most recent item added */
00043   _scrollOnNewItem = true;
00044   connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
00045           this, SLOT(onVerticalScroll(int)));
00046 }
00047 
00048 /** Called when the user moves the vertical scrollbar. If the user has the
00049  * scrollbar at within one step of its maximum, then always scroll to new
00050  * items when added. Otherwise, leave the scrollbar alone since they are
00051  * probably looking at something in their history. */
00052 void
00053 LogTreeWidget::onVerticalScroll(int value)
00054 {
00055   QScrollBar *scrollbar = verticalScrollBar();
00056   _scrollOnNewItem = (value >= (scrollbar->maximum()-scrollbar->singleStep()));
00057 }
00058 
00059 /** Cast a QList of QTreeWidgetItem pointers to a list of LogTreeWidget
00060  * pointers. There really must be a better way to do this. */
00061 QList<LogTreeItem *>
00062 LogTreeWidget::qlist_cast(QList<QTreeWidgetItem *> inlist)
00063 {
00064   QList<LogTreeItem *> outlist;
00065   foreach (QTreeWidgetItem *item, inlist) {
00066     outlist << (LogTreeItem *)item;
00067   }
00068   return outlist;
00069 }
00070 
00071 /** Sorts the list of pointers to log tree items by timestamp. */
00072 QList<LogTreeItem *>
00073 LogTreeWidget::qlist_sort(QList<LogTreeItem *> inlist)
00074 {
00075   QMap<QDateTime, LogTreeItem *> outlist;
00076   foreach (LogTreeItem *item, inlist) {
00077     outlist.insert(item->timestamp(), item);
00078   }
00079   return outlist.values();
00080 }
00081 
00082 /** The first time the log tree is shown, we need to set the default column
00083  * widths. */
00084 void
00085 LogTreeWidget::showEvent(QShowEvent *event)
00086 {
00087   static bool shown = false;
00088   QTreeWidget::showEvent(event);
00089   if (!shown) {
00090     /* Set the default column widths the first time this is shown */
00091     ((LogHeaderView *)header())->resetColumnWidths();
00092     /* Adjust the message column properly */
00093     adjustMessageColumn();
00094     shown = true;
00095   }
00096 }
00097 
00098 /** Clears all items from the message log and resets the counter in the status
00099  * bar. */
00100 void
00101 LogTreeWidget::clearMessages()
00102 {
00103   /* Clear the messages */
00104   clear();
00105   /* This should always be 0, but just in case clear() doesn't really remove
00106    * all, we'll get the current count again. */
00107   setStatusTip(tr("Messages Shown: %1").arg(messageCount()));
00108 }
00109 
00110 /** Adjusts the message column width to accomodate long messages. */
00111 void
00112 LogTreeWidget::adjustMessageColumn()
00113 {
00114   /* Adjust the message column, based on the longest item. */
00115   ((LogHeaderView *)header())->resize(sizeHintForColumn(MessageColumn));
00116 }
00117 
00118 /** Adds a message log item. */
00119 void
00120 LogTreeWidget::addMessageItem(LogTreeItem *item)
00121 {
00122   /* Add the new item. */
00123   addTopLevelItem(item);
00124   /* Adjust the column headers to accomodate a long message, if necesssary */
00125   adjustMessageColumn();
00126 }
00127 
00128 /** Returns a list of all currently selected items. */
00129 QList<LogTreeItem *>
00130 LogTreeWidget::selectedMessages()
00131 {
00132   QList<LogTreeItem *> items = 
00133     qlist_cast(selectedItems());
00134   return qlist_sort(items);
00135 }
00136 
00137 /** Returns a list of all selected items as a formatted string. */
00138 QString
00139 LogTreeWidget::selectedMessagesText()
00140 {
00141   QString text;
00142   foreach (LogTreeItem *item, selectedMessages()) {
00143     text.append(item->toString());
00144   }
00145   return text;
00146 }
00147 
00148 /** Returns a list of all items in the tree. */
00149 QList<LogTreeItem *>
00150 LogTreeWidget::allMessages()
00151 {
00152   /* Find all items */
00153   QList<LogTreeItem *> items = 
00154     qlist_cast(findItems("*", Qt::MatchWildcard|Qt::MatchWrap, MessageColumn));
00155   return qlist_sort(items);
00156 }
00157 
00158 /** Returns the number of items currently shown. */
00159 int
00160 LogTreeWidget::messageCount()
00161 {
00162   return topLevelItemCount();
00163 }
00164 
00165 /** Sets the maximum number of items in the tree. */
00166 void
00167 LogTreeWidget::setMaximumMessageCount(int max)
00168 {
00169   while (max < messageCount()) {
00170     /* If the new max is less than the currently displayed number of 
00171      * items, then we'll get rid of some. */
00172     delete takeTopLevelItem(0);
00173   }
00174   _maxItemCount = max;
00175 }
00176 
00177 /** Deselects all currently selected items. */
00178 void
00179 LogTreeWidget::deselectAll()
00180 {
00181   foreach(LogTreeItem *item, selectedMessages()) {
00182     setItemSelected(item, false);
00183   }
00184 }
00185 
00186 /** Adds a log item to the tree and returns a pointer to the new item. */
00187 LogTreeItem*
00188 LogTreeWidget::log(LogEvent::Severity type, QString message)
00189 {
00190   LogTreeItem *item = new LogTreeItem(type, message);
00191 
00192   /* If we need to make room, then make some room */
00193   if (messageCount() >= _maxItemCount) {
00194     delete takeTopLevelItem(0);
00195   }
00196   
00197   /* Add the new message item and scroll to it (if necessary) */
00198   addMessageItem(item);
00199   if (_scrollOnNewItem) {
00200     scrollToItem(item);
00201   }
00202 
00203   /* Update our tooltip and return the new log item */
00204   setStatusTip(tr("Messages Shown: %1").arg(messageCount()));
00205   return item;
00206 }
00207 
00208 /** Filters the message log based on the given filter. */
00209 void
00210 LogTreeWidget::filter(uint filter)
00211 {
00212   LogTreeItem *item;
00213   int index = messageCount() - 1;
00214   int itemsShown = 0;
00215 
00216   while (index > -1) {
00217     item = (LogTreeItem *)topLevelItem(index);
00218     if ((itemsShown < _maxItemCount) && (filter & item->severity())) {
00219       itemsShown++;
00220     } else {
00221       delete takeTopLevelItem(index);
00222     }
00223     index--;
00224   }
00225 
00226   setStatusTip(tr("Messages Shown: %1").arg(messageCount()));
00227 }
00228 
00229 /** Searches the log for entries that contain the given text. */
00230 QList<LogTreeItem *>
00231 LogTreeWidget::find(QString text, bool highlight)
00232 {
00233   QList<LogTreeItem *> items = 
00234     qlist_cast(findItems(text, Qt::MatchContains|Qt::MatchWrap, MessageColumn));
00235   
00236   if (highlight) {
00237     /* Deselect all items before highlighting our search results. */
00238     deselectAll();
00239     foreach (LogTreeItem *item, items) {
00240       /* Highlight a matched item */
00241       setItemSelected(item, true);
00242     }
00243   }
00244 
00245   /* Return the results, sorted by timestamp */
00246   return qlist_sort(items);
00247 }
00248 

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