Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > media > contrib-backports > by-pkgid > 9601c7beb4ff23e834bfa171795ed560 > files > 303

vidalia-0.2.9-1mdv2010.1.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Vidalia: StatusEventItemDelegate.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.3 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_695ba8f8357372e02d38c40b3c6dbe81.html">vidalia</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_b5419f0f3b920f7a66865768144dfe22.html">log</a>
  </div>
</div>
<div class="contents">
<h1>StatusEventItemDelegate.cpp</h1><a href="_status_event_item_delegate_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment">**  This file is part of Vidalia, and is subject to the license terms in the</span>
<a name="l00003"></a>00003 <span class="comment">**  LICENSE file, found in the top level directory of this distribution. If you</span>
<a name="l00004"></a>00004 <span class="comment">**  did not receive the LICENSE file with this file, you may obtain it from the</span>
<a name="l00005"></a>00005 <span class="comment">**  Vidalia source package distributed by the Vidalia Project at</span>
<a name="l00006"></a>00006 <span class="comment">**  http://www.vidalia-project.net/. No part of Vidalia, including this file,</span>
<a name="l00007"></a>00007 <span class="comment">**  may be copied, modified, propagated, or distributed except according to the</span>
<a name="l00008"></a>00008 <span class="comment">**  terms described in the LICENSE file.</span>
<a name="l00009"></a>00009 <span class="comment">*/</span>
<a name="l00010"></a>00010 
<a name="l00011"></a>00011 <span class="comment">/*</span>
<a name="l00012"></a>00012 <span class="comment">** \file StatusEventItemDelegate.cpp</span>
<a name="l00013"></a>00013 <span class="comment">** \version $Id: StatusEventItemDelegate.cpp 4080 2009-08-28 18:14:48Z edmanm $</span>
<a name="l00014"></a>00014 <span class="comment">** \brief Handles custom painting of items in a StatusEventWidget</span>
<a name="l00015"></a>00015 <span class="comment">*/</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="preprocessor">#include &quot;<a class="code" href="_status_event_item_delegate_8h.html">StatusEventItemDelegate.h</a>&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="_status_event_item_8h.html">StatusEventItem.h</a>&quot;</span>
<a name="l00019"></a>00019 
<a name="l00020"></a>00020 <span class="preprocessor">#include &quot;<a class="code" href="_vidalia_8h.html">Vidalia.h</a>&quot;</span>
<a name="l00021"></a>00021 
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;QPainter&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;QTextLine&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;QTextLayout&gt;</span>
<a name="l00025"></a>00025 
<a name="l00026"></a><a class="code" href="class_status_event_item_delegate.html#ab67f11336528a0e2c62f80c1b6e0d4d6">00026</a> <a class="code" href="class_status_event_item_delegate.html#ab67f11336528a0e2c62f80c1b6e0d4d6">StatusEventItemDelegate::StatusEventItemDelegate</a>(QObject *parent)
<a name="l00027"></a>00027   : QItemDelegate(parent)
<a name="l00028"></a>00028 {
<a name="l00029"></a>00029   <a class="code" href="class_status_event_item_delegate.html#abd0807631bb84e8e087e884bf768fb29">_helpIcon</a> = QPixmap(<span class="stringliteral">&quot;:/images/16x16/system-help.png&quot;</span>);
<a name="l00030"></a>00030 }
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 <span class="keywordtype">void</span>
<a name="l00033"></a><a class="code" href="class_status_event_item_delegate.html#ac42fa7c064f411bc234cbb9e8c10112e">00033</a> <a class="code" href="class_status_event_item_delegate.html#ac42fa7c064f411bc234cbb9e8c10112e">StatusEventItemDelegate::paint</a>(QPainter *painter,
<a name="l00034"></a>00034                                <span class="keyword">const</span> QStyleOptionViewItem &amp;option,
<a name="l00035"></a>00035                                <span class="keyword">const</span> QModelIndex &amp;index)<span class="keyword"> const</span>
<a name="l00036"></a>00036 <span class="keyword"></span>{
<a name="l00037"></a>00037   <a class="code" href="class_status_event_item_delegate.html#ac42fa7c064f411bc234cbb9e8c10112e">QItemDelegate::paint</a>(painter, option, index);
<a name="l00038"></a>00038 
<a name="l00039"></a>00039   painter-&gt;save();
<a name="l00040"></a>00040   <span class="keywordflow">if</span> (option.state &amp; QStyle::State_Selected)
<a name="l00041"></a>00041     painter-&gt;setPen(option.palette.highlightedText().color());
<a name="l00042"></a>00042 
<a name="l00043"></a>00043   QPixmap icon  = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26acde77cc12e5744c35a48b1ea93ce3c1d">StatusEventItem::IconRole</a>).value&lt;QPixmap&gt;();
<a name="l00044"></a>00044   QTime tstamp  = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26a7deeaa0a8e64df7a502dc5261232be5b">StatusEventItem::TimestampRole</a>).toTime();
<a name="l00045"></a>00045   QString title = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26a47e45d12f9a667b16e478e448a51b790">StatusEventItem::TitleRole</a>).toString();
<a name="l00046"></a>00046   QString text  = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26ae110fce2d277b831bc13605644cf3d0c">StatusEventItem::DescriptionRole</a>).toString();
<a name="l00047"></a>00047   QFont font    = option.font;
<a name="l00048"></a>00048   QFontMetrics fm = option.fontMetrics;
<a name="l00049"></a>00049 
<a name="l00050"></a>00050   <span class="comment">/* XXX: Handle right-to-left layouts here. */</span>
<a name="l00051"></a>00051   QRect iconRect(option.rect.x(),
<a name="l00052"></a>00052                  option.rect.y(),
<a name="l00053"></a>00053                  qMax(fm.width(tstamp.toString()), icon.width()) + 16,
<a name="l00054"></a>00054                  option.rect.height());
<a name="l00055"></a>00055   QRect textRect(iconRect.topRight(), option.rect.bottomRight());
<a name="l00056"></a>00056 
<a name="l00057"></a>00057   <span class="comment">// Draw the status icon</span>
<a name="l00058"></a>00058   QPoint center = iconRect.center();
<a name="l00059"></a>00059   <span class="keywordtype">int</span> x = center.x() - qRound(icon.width() / 2.0);
<a name="l00060"></a>00060   <span class="keywordtype">int</span> y = center.y() - qRound((icon.height() + fm.lineSpacing()) / 2.0);
<a name="l00061"></a>00061   painter-&gt;drawPixmap(x, y, icon);
<a name="l00062"></a>00062 
<a name="l00063"></a>00063   <span class="comment">// Draw the timestamp text underneath the status icon</span>
<a name="l00064"></a>00064   x = iconRect.x();
<a name="l00065"></a>00065   y = y + icon.height();
<a name="l00066"></a>00066   painter-&gt;drawText(x, y,
<a name="l00067"></a>00067                     iconRect.width(),
<a name="l00068"></a>00068                     fm.lineSpacing(),
<a name="l00069"></a>00069                     Qt::AlignCenter,
<a name="l00070"></a>00070                     tstamp.toString());
<a name="l00071"></a>00071 
<a name="l00072"></a>00072   <span class="comment">// Draw the event&#39;s title in a bold font. If the current item has an</span>
<a name="l00073"></a>00073   <span class="comment">// associated help URL, draw the little &quot;?&quot; icon to the right of the</span>
<a name="l00074"></a>00074   <span class="comment">// title text</span>
<a name="l00075"></a>00075   font.setBold(<span class="keyword">true</span>);
<a name="l00076"></a>00076   painter-&gt;setFont(font);
<a name="l00077"></a>00077   <span class="keywordflow">if</span> (! index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26a88ced42352d45337ce4063479d3926cc">StatusEventItem::HelpUrlRole</a>).isNull()) {
<a name="l00078"></a>00078     <span class="comment">// Draw the little &quot;?&quot; icon in the corner of the list item and</span>
<a name="l00079"></a>00079     <span class="comment">// account for it when eliding the title</span>
<a name="l00080"></a>00080     title = fm.elidedText(title,
<a name="l00081"></a>00081                           Qt::ElideRight,
<a name="l00082"></a>00082                           textRect.width() - <a class="code" href="class_status_event_item_delegate.html#abd0807631bb84e8e087e884bf768fb29">_helpIcon</a>.width() - 24);
<a name="l00083"></a>00083 
<a name="l00084"></a>00084     x = textRect.topRight().x() - <a class="code" href="class_status_event_item_delegate.html#abd0807631bb84e8e087e884bf768fb29">_helpIcon</a>.width() - 8;
<a name="l00085"></a>00085     y = textRect.y() + 8;
<a name="l00086"></a>00086     painter-&gt;drawPixmap(x, y, <a class="code" href="class_status_event_item_delegate.html#abd0807631bb84e8e087e884bf768fb29">_helpIcon</a>);
<a name="l00087"></a>00087   } <span class="keywordflow">else</span> {
<a name="l00088"></a>00088     title = fm.elidedText(title, Qt::ElideRight, textRect.width() - 16);
<a name="l00089"></a>00089   }
<a name="l00090"></a>00090   painter-&gt;drawText(textRect.x(),
<a name="l00091"></a>00091                     textRect.y() + 8,
<a name="l00092"></a>00092                     textRect.width(),
<a name="l00093"></a>00093                     fm.lineSpacing(),
<a name="l00094"></a>00094                     Qt::AlignVCenter | Qt::AlignLeft, title);
<a name="l00095"></a>00095 
<a name="l00096"></a>00096   <span class="comment">// Draw the rest of the event text, up to a maximum of 2 lines for</span>
<a name="l00097"></a>00097   <span class="comment">// unselected items or 5 lines for selected items. Any extra text will</span>
<a name="l00098"></a>00098   <span class="comment">// be elided.</span>
<a name="l00099"></a>00099   font.setBold(<span class="keyword">false</span>);
<a name="l00100"></a>00100   painter-&gt;setFont(font);
<a name="l00101"></a>00101   <span class="keywordflow">if</span> (option.state &amp; QStyle::State_Selected)
<a name="l00102"></a>00102     text = <a class="code" href="class_status_event_item_delegate.html#a48a3940e194baaa9dbe9701db65fe085">layoutText</a>(text, font, textRect.width(), 6).join(<span class="stringliteral">&quot;\n&quot;</span>);
<a name="l00103"></a>00103   <span class="keywordflow">else</span>
<a name="l00104"></a>00104     text = <a class="code" href="class_status_event_item_delegate.html#a48a3940e194baaa9dbe9701db65fe085">layoutText</a>(text, font, textRect.width(), 3).join(<span class="stringliteral">&quot;\n&quot;</span>);
<a name="l00105"></a>00105 
<a name="l00106"></a>00106   x = textRect.x();
<a name="l00107"></a>00107   y = textRect.y() + 8 + fm.leading() + fm.lineSpacing();
<a name="l00108"></a>00108   painter-&gt;drawText(x, y,
<a name="l00109"></a>00109                     textRect.width(),
<a name="l00110"></a>00110                     textRect.height() - (y - textRect.y()),
<a name="l00111"></a>00111                     Qt::AlignTop | Qt::AlignLeft, text);
<a name="l00112"></a>00112 
<a name="l00113"></a>00113   painter-&gt;restore();
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 
<a name="l00116"></a>00116 QSize
<a name="l00117"></a><a class="code" href="class_status_event_item_delegate.html#a89527471b548bf7876f899de0772c890">00117</a> <a class="code" href="class_status_event_item_delegate.html#a89527471b548bf7876f899de0772c890">StatusEventItemDelegate::sizeHint</a>(<span class="keyword">const</span> QStyleOptionViewItem &amp;option,
<a name="l00118"></a>00118                                   <span class="keyword">const</span> QModelIndex &amp;index)<span class="keyword"> const</span>
<a name="l00119"></a>00119 <span class="keyword"></span>{
<a name="l00120"></a>00120   <span class="keywordtype">int</span> iconHeight, iconWidth;
<a name="l00121"></a>00121   <span class="keywordtype">int</span> textWidth, textHeight;
<a name="l00122"></a>00122   QFontMetrics fontMetrics = option.fontMetrics;
<a name="l00123"></a>00123 
<a name="l00124"></a>00124   QPixmap icon = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26acde77cc12e5744c35a48b1ea93ce3c1d">StatusEventItem::IconRole</a>).value&lt;QPixmap&gt;();
<a name="l00125"></a>00125   QString text = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26ae110fce2d277b831bc13605644cf3d0c">StatusEventItem::DescriptionRole</a>).toString();
<a name="l00126"></a>00126   QTime tstamp = index.data(<a class="code" href="class_status_event_item.html#a6ed2b03097a6b4bc1e56add5e7e04c26a7deeaa0a8e64df7a502dc5261232be5b">StatusEventItem::TimestampRole</a>).toTime();
<a name="l00127"></a>00127 
<a name="l00128"></a>00128   iconHeight = icon.height() + fontMetrics.lineSpacing() + 16;
<a name="l00129"></a>00129   iconWidth  = qMax(fontMetrics.width(tstamp.toString()), icon.width()) + 16;
<a name="l00130"></a>00130   textWidth  = option.rect.width() - iconWidth;
<a name="l00131"></a>00131 
<a name="l00132"></a>00132   <span class="keywordflow">if</span> (option.state &amp; QStyle::State_Selected)
<a name="l00133"></a>00133     <a class="code" href="class_status_event_item_delegate.html#a48a3940e194baaa9dbe9701db65fe085">layoutText</a>(text, option.font, textWidth, 6, &amp;textHeight);
<a name="l00134"></a>00134   <span class="keywordflow">else</span>
<a name="l00135"></a>00135     <a class="code" href="class_status_event_item_delegate.html#a48a3940e194baaa9dbe9701db65fe085">layoutText</a>(text, option.font, textWidth, 3, &amp;textHeight);
<a name="l00136"></a>00136   textHeight += 8 + fontMetrics.leading() + fontMetrics.lineSpacing();
<a name="l00137"></a>00137 
<a name="l00138"></a>00138   <span class="keywordflow">return</span> QSize(option.rect.width(), qMax(iconHeight, textHeight));
<a name="l00139"></a>00139 }
<a name="l00140"></a>00140 
<a name="l00141"></a>00141 QStringList
<a name="l00142"></a><a class="code" href="class_status_event_item_delegate.html#a48a3940e194baaa9dbe9701db65fe085">00142</a> <a class="code" href="class_status_event_item_delegate.html#a48a3940e194baaa9dbe9701db65fe085">StatusEventItemDelegate::layoutText</a>(<span class="keyword">const</span> QString &amp;text,
<a name="l00143"></a>00143                                     <span class="keyword">const</span> QFont &amp;font,
<a name="l00144"></a>00144                                     <span class="keywordtype">int</span> maxLineWidth,
<a name="l00145"></a>00145                                     <span class="keywordtype">int</span> maxLines,
<a name="l00146"></a>00146                                     <span class="keywordtype">int</span> *textHeight)
<a name="l00147"></a>00147 {
<a name="l00148"></a>00148   QTextLayout textLayout(text, font);
<a name="l00149"></a>00149   QFontMetrics fontMetrics(font);
<a name="l00150"></a>00150   QStringList lines;
<a name="l00151"></a>00151   qreal height = 0.0;
<a name="l00152"></a>00152 
<a name="l00153"></a>00153   textLayout.beginLayout();
<a name="l00154"></a>00154   <span class="keywordflow">while</span> (lines.size() &lt; maxLines) {
<a name="l00155"></a>00155     QTextLine line = textLayout.createLine();
<a name="l00156"></a>00156     <span class="keywordflow">if</span> (! line.isValid())
<a name="l00157"></a>00157       <span class="keywordflow">break</span>;
<a name="l00158"></a>00158     <span class="keywordflow">if</span> (maxLines &lt;= 0 || lines.size() &lt; maxLines-1) {
<a name="l00159"></a>00159       <span class="comment">// Wrap the current line at or below the maximum line width</span>
<a name="l00160"></a>00160       line.setLineWidth(maxLineWidth);
<a name="l00161"></a>00161       lines.append(text.mid(line.textStart(), line.textLength()));
<a name="l00162"></a>00162     } <span class="keywordflow">else</span> {
<a name="l00163"></a>00163       <span class="comment">// Set the line width beyond the max line width, and then elide it</span>
<a name="l00164"></a>00164       <span class="comment">// so the user has a visible indication that the full message is</span>
<a name="l00165"></a>00165       <span class="comment">// longer than what is visible.</span>
<a name="l00166"></a>00166       line.setLineWidth(2 * maxLineWidth);
<a name="l00167"></a>00167       lines.append(fontMetrics.elidedText(text.mid(line.textStart()),
<a name="l00168"></a>00168                                           Qt::ElideRight,
<a name="l00169"></a>00169                                           maxLineWidth));
<a name="l00170"></a>00170     }
<a name="l00171"></a>00171     height += fontMetrics.leading() + line.height();
<a name="l00172"></a>00172   }
<a name="l00173"></a>00173   textLayout.endLayout();
<a name="l00174"></a>00174 
<a name="l00175"></a>00175   <span class="keywordflow">if</span> (textHeight)
<a name="l00176"></a>00176     *textHeight = qRound(height);
<a name="l00177"></a>00177 
<a name="l00178"></a>00178   <span class="keywordflow">return</span> lines;
<a name="l00179"></a>00179 }
<a name="l00180"></a>00180 
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:55 2010 for Vidalia by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>