• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • widgets
customlogwidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "customlogwidget.h"
19 #include <QTextDocument>
20 #include <QPainter>
21 #include <QApplication>
22 #include <QAbstractTextDocumentLayout>
23 
24 #include <KDebug>
25 using namespace KPIM;
26 
27 LogItemDelegate::LogItemDelegate( QObject *parent )
28  : QStyledItemDelegate( parent )
29 {
30 }
31 
32 LogItemDelegate::~LogItemDelegate()
33 {
34 }
35 
36 QTextDocument* LogItemDelegate::document ( const QStyleOptionViewItem &option, const QModelIndex &index ) const
37 {
38  if ( !index.isValid() )
39  return 0;
40  QTextDocument *document = new QTextDocument ( 0 );
41  document->setDocumentMargin( 1 );
42  const QColor textColor = index.data( Qt::ForegroundRole ).value<QColor>();
43  QStyleOptionViewItemV4 option4 = option;
44  QStyledItemDelegate::initStyleOption( &option4, index );
45 
46  QString text = option4.text;
47 
48  const QString content = QString::fromLatin1 (
49  "<html style=\"color:%1\">"
50  "<body> %2" ).arg ( textColor.name().toUpper() ).arg( text )
51  + QLatin1String ( "</table></body></html>" );
52 
53  document->setHtml ( content );
54 
55  return document;
56 }
57 
58 
59 void LogItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
60 {
61  if ( !index.isValid() )
62  return;
63  QTextDocument *doc = document ( option, index );
64  if ( !doc )
65  return;
66  doc->setTextWidth( option.rect.width() );
67  painter->setRenderHint ( QPainter::Antialiasing );
68 
69  QPen pen = painter->pen();
70 
71  QStyleOptionViewItemV4 opt ( option );
72  opt.showDecorationSelected = true;
73  QApplication::style()->drawPrimitive ( QStyle::PE_PanelItemViewItem, &opt, painter );
74  painter->save();
75  painter->translate ( option.rect.topLeft() );
76 
77  doc->drawContents ( painter );
78 
79  painter->restore();
80  painter->setPen( pen );
81 
82  delete doc;
83 }
84 
85 QSize LogItemDelegate::sizeHint ( const QStyleOptionViewItem &option, const QModelIndex &index ) const
86 {
87  if ( !index.isValid() )
88  return QSize ( 0, 0 );
89 
90  QTextDocument *doc = document ( option, index );
91  if ( !doc )
92  return QSize ( 0, 0 );
93 
94  const QSize size = doc->documentLayout()->documentSize().toSize();
95  delete doc;
96 
97  return size;
98 }
99 
100 QWidget *LogItemDelegate::createEditor ( QWidget *, const QStyleOptionViewItem &, const QModelIndex & ) const
101 {
102  return 0;
103 }
104 
105 
106 
107 CustomLogWidget::CustomLogWidget( QWidget * parent )
108  :QListWidget( parent )
109 {
110  LogItemDelegate *itemDelegate = new LogItemDelegate( this );
111  setItemDelegate( itemDelegate );
112 }
113 
114 CustomLogWidget::~CustomLogWidget()
115 {
116 }
117 
118 void CustomLogWidget::addTitleLogEntry( const QString &log )
119 {
120  QListWidgetItem* item =new QListWidgetItem(log);
121  item->setForeground(Qt::black);
122  QFont font = item->font();
123  font.setBold(true);
124  item->setFont(font);
125  item->setData(ItemLogType, Title);
126  addItem( item );
127  scrollToItem(item);
128 }
129 
130 void CustomLogWidget::addInfoLogEntry( const QString &log )
131 {
132  QListWidgetItem* item =new QListWidgetItem(log);
133  item->setForeground(Qt::blue);
134  item->setData(ItemLogType, Info);
135  addItem( item );
136  scrollToItem(item);
137 }
138 
139 void CustomLogWidget::addErrorLogEntry( const QString &log )
140 {
141  QListWidgetItem* item =new QListWidgetItem(log);
142  item->setForeground(Qt::red);
143  item->setData(ItemLogType, Error);
144  addItem( item );
145  scrollToItem(item);
146 }
147 
148 void CustomLogWidget::addEndLineLogEntry()
149 {
150  QListWidgetItem* item =new QListWidgetItem;
151  item->setData(ItemLogType, EndLine);
152  addItem( item );
153  scrollToItem(item);
154 }
155 
156 QString CustomLogWidget::toHtml() const
157 {
158  QString result = QLatin1String("<html>\n<body>\n");
159  result += QLatin1String("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n");
160  for (int i=0; i < count(); ++i) {
161  QListWidgetItem *itemWidget = item(i);
162  const QString itemText(itemWidget->text());
163  QString logText;
164  LogType type = static_cast<LogType>(itemWidget->data(CustomLogWidget::ItemLogType).toInt());
165  switch(type) {
166  case Title:
167  logText = QString::fromUtf8( "<font color=%1>%2</font>" ).arg(QColor(Qt::black).name()).arg(itemText);
168  break;
169  case Error:
170  logText = QString::fromUtf8( "<font color=%1>%2</font>" ).arg(QColor(Qt::red).name()).arg(itemText);
171  break;
172  case Info:
173  logText = QString::fromUtf8( "<font color=%1>%2</font>" ).arg(QColor(Qt::green).name()).arg(itemText);
174  break;
175  case EndLine:
176  logText = QLatin1String("<br/>");
177  break;
178  default:
179  kDebug()<<"LogType undefined"<<type;
180  logText += itemWidget->text();
181  break;
182  }
183  result += QLatin1String("<p>") + logText + QLatin1String("</p>") + QLatin1Char('\n');
184  }
185  result += QLatin1String( "</body>\n</html>\n" );
186  return result;
187 }
188 
189 QString CustomLogWidget::toPlainText() const
190 {
191  QString result;
192  for (int i=0; i < count(); ++i) {
193  result += item(i)->text() + QLatin1Char('\n');
194  }
195  return result;
196 }
197 
198 bool CustomLogWidget::isEmpty() const
199 {
200  return (count() == 0);
201 }
KPIM::CustomLogWidget::addInfoLogEntry
void addInfoLogEntry(const QString &log)
Definition: customlogwidget.cpp:130
KPIM::LogItemDelegate::LogItemDelegate
LogItemDelegate(QObject *parent)
Definition: customlogwidget.cpp:27
customlogwidget.h
QWidget
QListWidget
QObject
QStyledItemDelegate
KPIM::CustomLogWidget::CustomLogWidget
CustomLogWidget(QWidget *parent=0)
Definition: customlogwidget.cpp:107
KPIM::LogItemDelegate::sizeHint
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: customlogwidget.cpp:85
KPIM::LogItemDelegate::paint
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: customlogwidget.cpp:59
KPIM::LogItemDelegate::createEditor
virtual QWidget * createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
Definition: customlogwidget.cpp:100
KPIM::CustomLogWidget::addEndLineLogEntry
void addEndLineLogEntry()
Definition: customlogwidget.cpp:148
KPIM::CustomLogWidget::toHtml
QString toHtml() const
Definition: customlogwidget.cpp:156
KPIM::LogItemDelegate
Definition: customlogwidget.h:28
KPIM::CustomLogWidget::addErrorLogEntry
void addErrorLogEntry(const QString &log)
Definition: customlogwidget.cpp:139
KPIM::CustomLogWidget::toPlainText
QString toPlainText() const
Definition: customlogwidget.cpp:189
KPIM::CustomLogWidget::~CustomLogWidget
~CustomLogWidget()
Definition: customlogwidget.cpp:114
KPIM::LogItemDelegate::~LogItemDelegate
~LogItemDelegate()
Definition: customlogwidget.cpp:32
KPIM::CustomLogWidget::addTitleLogEntry
void addTitleLogEntry(const QString &log)
Definition: customlogwidget.cpp:118
KPIM::CustomLogWidget::isEmpty
bool isEmpty() const
Definition: customlogwidget.cpp:198
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal