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

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
  • common
fastsizehintitemdelegate.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2012 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 
25 #include "fastsizehintitemdelegate.h"
26 
27 #include <QPainter>
28 #include <QStringBuilder>
29 #include <QTextDocument>
30 
31 FastSizeHintItemDelegate::FastSizeHintItemDelegate(QObject *parent, const QVector<bool>& slc, const QVector<bool>& rtc)
32  : QItemDelegate(parent)
33  , singleLineColumns(slc)
34  , richTextColumns(rtc)
35  , activeScheme(QPalette::Active, KColorScheme::View)
36 {}
37 
38 void FastSizeHintItemDelegate::reset()
39 {
40  cache.clear();
41 }
42 
43 QSize FastSizeHintItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
44 {
45  int lineCount=1;
46  int nPos=20;
47  if (!singleLineColumns.at(index.column()))
48  {
49  QString text=index.data().toString();
50  nPos=text.indexOf('\n');
51  if (nPos==-1)
52  nPos=text.size();
53  else
54  lineCount+=text.count('\n');
55  }
56  static QFontMetrics metrics(option.font);
57  return QSize(metrics.averageCharWidth()*nPos, metrics.height()*lineCount);
58 }
59 
60 void FastSizeHintItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
61 {
62  painter->save();
63 
64  const KColorScheme& scheme=activeScheme;
65  //painter->save();
66  painter->setClipping(true);
67  painter->setClipRect(option.rect);
68  QBrush bgBrush;
69  if (option.state&QStyle::State_MouseOver)
70  bgBrush=scheme.background(KColorScheme::LinkBackground);
71  else if (index.row()%2)
72  bgBrush=scheme.background(KColorScheme::AlternateBackground);
73  else
74  bgBrush=scheme.background(KColorScheme::NormalBackground);
75 
76  painter->fillRect(option.rect, bgBrush);
77  painter->setClipRect(option.rect.adjusted(0,0,-2,0));
78  //painter->setFont(option.font);
79 
80  RowColumnUnion rc;
81  rc.index.row=index.row();
82  rc.index.column=index.column();
83  //TMDBModel* m=static_cast<const TMDBModel*>(index.model());
84  if (!cache.contains(rc.v))
85  {
86  QString text=index.data(FastSizeHintItemDelegate::HtmlDisplayRole).toString();
87  cache.insert(rc.v, new QStaticText(text));
88  cache.object(rc.v)->setTextFormat(richTextColumns.at(index.column())?Qt::RichText:Qt::PlainText);
89  }
90  int rectWidth=option.rect.width();
91  QStaticText* staticText=cache.object(rc.v);
92  //staticText->setTextWidth(rectWidth-4);
93  QPoint textStartPoint=option.rect.topLeft();
94  textStartPoint.rx()+=2;
95  painter->drawStaticText(textStartPoint, *staticText);
96 
97 
98  if (staticText->size().width()<=rectWidth-4)
99  {
100  painter->restore();
101  return;
102  }
103 
104  painter->setPen(bgBrush.color());
105  QPoint p1=option.rect.topRight();
106  QPoint p2=option.rect.bottomRight();
107  int limit=qMin(8, rectWidth-2);
108  int i=limit;
109  while(--i>0)
110  {
111  painter->setOpacity(float(i)/limit);
112  painter->drawLine(p1, p2);
113  p1.rx()--;
114  p2.rx()--;
115  }
116  painter->restore();
117 }
118 
119 QString convertToHtml(QString str, bool italics)
120 {
121 /*
122  if (str.isEmpty())
123  return str;
124 */
125 
126  str=Qt::convertFromPlainText(str); //FIXME use another routine (this has bugs)
127 
128  if (italics)
129  str="<p><i>" % QString::fromRawData(str.unicode()+3, str.length()-3-4) % "</i></p>";
130 
131  return str;
132 }
133 
QModelIndex
QPainter::setOpacity
void setOpacity(qreal opacity)
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QPoint::rx
int & rx()
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QPainter::drawStaticText
void drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
QCache::contains
bool contains(const Key &key) const
QPainter::setClipping
void setClipping(bool enable)
FastSizeHintItemDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: fastsizehintitemdelegate.cpp:43
QString::size
int size() const
QPainter::save
void save()
QCache::object
T * object(const Key &key) const
QBrush
QPoint
QFontMetrics
QPainter::drawLine
void drawLine(const QLineF &line)
fastsizehintitemdelegate.h
QStaticText::size
QSizeF size() const
QBrush::color
const QColor & color() const
Qt::convertFromPlainText
QString convertFromPlainText(const QString &plain, WhiteSpaceMode mode)
QString::fromRawData
QString fromRawData(const QChar *unicode, int size)
FastSizeHintItemDelegate::FastSizeHintItemDelegate
FastSizeHintItemDelegate(QObject *parent, const QVector< bool > &slc, const QVector< bool > &rtc)
Definition: fastsizehintitemdelegate.cpp:31
QStyleOptionViewItem
QObject
QPainter::setPen
void setPen(const QColor &color)
QPainter
QModelIndex::row
int row() const
QItemDelegate
QCache::insert
bool insert(const Key &key, T *object, int cost)
QString
QSize
QPainter::restore
void restore()
QString::unicode
const QChar * unicode() const
QVector::at
const T & at(int i) const
QPainter::setClipRect
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
QVector< bool >
QModelIndex::data
QVariant data(int role) const
QStaticText
convertToHtml
QString convertToHtml(QString str, bool italics)
Definition: fastsizehintitemdelegate.cpp:119
QString::count
int count() const
QFontMetrics::height
int height() const
QModelIndex::column
int column() const
QString::length
int length() const
QFontMetrics::averageCharWidth
int averageCharWidth() const
FastSizeHintItemDelegate::HtmlDisplayRole
Definition: fastsizehintitemdelegate.h:49
FastSizeHintItemDelegate::reset
void reset()
Definition: fastsizehintitemdelegate.cpp:38
QVariant::toString
QString toString() const
QSizeF::width
qreal width() const
QPalette
FastSizeHintItemDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: fastsizehintitemdelegate.cpp:60
QCache::clear
void clear()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

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

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

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