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

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • schema
katecategorydrawer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2009 by Rafael Fernández López <ereslibre@kde.org> *
3  * *
4  * This library is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU Library General Public *
6  * License version 2 as published by the Free Software Foundation. *
7  * *
8  * This library is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
11  * Library General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU Library General Public License
14  * along with this library; see the file COPYING.LIB. If not, write to *
15  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
16  * Boston, MA 02110-1301, USA. *
17  ***************************************************************************/
18 
19 // this code is taken from SystemSettings/icons/CategoryDrawer.{h,cpp}
20 // Rafael agreet to relicense it under LGPLv2 or LGPLv3, just as we need it,
21 // see: http://lists.kde.org/?l=kwrite-devel&m=133061943317199&w=2
22 
23 #include "katecategorydrawer.h"
24 
25 #include <QPainter>
26 #include <QApplication>
27 #include <QStyleOption>
28 
29 KateCategoryDrawer::KateCategoryDrawer() : KCategoryDrawerV3 (0)
30 {
31  setLeftMargin( 7 );
32  setRightMargin( 7 );
33 }
34 
35 void KateCategoryDrawer::drawCategory(const QModelIndex &index,
36  int sortRole,
37  const QStyleOption &option,
38  QPainter *painter) const
39 {
40  Q_UNUSED( sortRole )
41 
42  painter->setRenderHint(QPainter::Antialiasing);
43 
44  const QRect optRect = option.rect;
45  QFont font(QApplication::font());
46  font.setBold(true);
47  const QFontMetrics fontMetrics = QFontMetrics(font);
48  const int height = categoryHeight(index, option);
49  const bool leftToRight = painter->layoutDirection() == Qt::LeftToRight;
50 
51  //BEGIN: decoration gradient
52  {
53  QPainterPath path(optRect.bottomLeft());
54 
55  path.lineTo(QPoint(optRect.topLeft().x(), optRect.topLeft().y() - 3));
56  const QPointF topLeft(optRect.topLeft());
57  QRectF arc(topLeft, QSizeF(4, 4));
58  path.arcTo(arc, 180, -90);
59  path.lineTo(optRect.topRight());
60  path.lineTo(optRect.bottomRight());
61  path.lineTo(optRect.bottomLeft());
62 
63  QColor window(option.palette.window().color());
64  const QColor base(option.palette.base().color());
65 
66  window.setAlphaF(0.4);
67 
68  QLinearGradient decoGradient1;
69  if (leftToRight) {
70  decoGradient1.setStart(optRect.topLeft());
71  decoGradient1.setFinalStop(optRect.bottomLeft());
72  } else {
73  decoGradient1.setStart(optRect.topRight());
74  decoGradient1.setFinalStop(optRect.bottomRight());
75  }
76  decoGradient1.setColorAt(0, window);
77  decoGradient1.setColorAt(1, Qt::transparent);
78 
79  QLinearGradient decoGradient2;
80  if (leftToRight) {
81  decoGradient2.setStart(optRect.topLeft());
82  decoGradient2.setFinalStop(optRect.topRight());
83  } else {
84  decoGradient2.setStart(optRect.topRight());
85  decoGradient2.setFinalStop(optRect.topLeft());
86  }
87  decoGradient2.setColorAt(0, Qt::transparent);
88  decoGradient2.setColorAt(1, base);
89 
90  painter->fillPath(path, decoGradient1);
91  painter->fillPath(path, decoGradient2);
92  }
93  //END: decoration gradient
94 
95  {
96  QRect newOptRect(optRect);
97 
98  if (leftToRight) {
99  newOptRect.translate(1, 1);
100  } else {
101  newOptRect.translate(-1, 1);
102  }
103 
104  //BEGIN: inner top left corner
105  {
106  painter->save();
107  painter->setPen(option.palette.base().color());
108  QRectF arc;
109  if (leftToRight) {
110  const QPointF topLeft(newOptRect.topLeft());
111  arc = QRectF(topLeft, QSizeF(4, 4));
112  arc.translate(0.5, 0.5);
113  painter->drawArc(arc, 1440, 1440);
114  } else {
115  QPointF topRight(newOptRect.topRight());
116  topRight.rx() -= 4;
117  arc = QRectF(topRight, QSizeF(4, 4));
118  arc.translate(-0.5, 0.5);
119  painter->drawArc(arc, 0, 1440);
120  }
121  painter->restore();
122  }
123  //END: inner top left corner
124 
125  //BEGIN: inner left vertical line
126  {
127  QPoint start;
128  QPoint verticalGradBottom;
129  if (leftToRight) {
130  start = newOptRect.topLeft();
131  verticalGradBottom = newOptRect.topLeft();
132  } else {
133  start = newOptRect.topRight();
134  verticalGradBottom = newOptRect.topRight();
135  }
136  start.ry() += 3;
137  verticalGradBottom.ry() += newOptRect.height() - 3;
138  QLinearGradient gradient(start, verticalGradBottom);
139  gradient.setColorAt(0, option.palette.base().color());
140  gradient.setColorAt(1, Qt::transparent);
141  painter->fillRect(QRect(start, QSize(1, newOptRect.height() - 3)), gradient);
142  }
143  //END: inner left vertical line
144 
145  //BEGIN: inner horizontal line
146  {
147  QPoint start;
148  QPoint horizontalGradTop;
149  if (leftToRight) {
150  start = newOptRect.topLeft();
151  horizontalGradTop = newOptRect.topLeft();
152  start.rx() += 3;
153  horizontalGradTop.rx() += newOptRect.width() - 3;
154  } else {
155  start = newOptRect.topRight();
156  horizontalGradTop = newOptRect.topRight();
157  start.rx() -= 3;
158  horizontalGradTop.rx() -= newOptRect.width() - 3;
159  }
160  QLinearGradient gradient(start, horizontalGradTop);
161  gradient.setColorAt(0, option.palette.base().color());
162  gradient.setColorAt(1, Qt::transparent);
163  QSize rectSize;
164  if (leftToRight) {
165  rectSize = QSize(newOptRect.width() - 3, 1);
166  } else {
167  rectSize = QSize(-newOptRect.width() + 3, 1);
168  }
169  painter->fillRect(QRect(start, rectSize), gradient);
170  }
171  //END: inner horizontal line
172  }
173 
174  QColor outlineColor = option.palette.text().color();
175  outlineColor.setAlphaF(0.35);
176 
177  //BEGIN: top left corner
178  {
179  painter->save();
180  painter->setPen(outlineColor);
181  QRectF arc;
182  if (leftToRight) {
183  const QPointF topLeft(optRect.topLeft());
184  arc = QRectF(topLeft, QSizeF(4, 4));
185  arc.translate(0.5, 0.5);
186  painter->drawArc(arc, 1440, 1440);
187  } else {
188  QPointF topRight(optRect.topRight());
189  topRight.rx() -= 4;
190  arc = QRectF(topRight, QSizeF(4, 4));
191  arc.translate(-0.5, 0.5);
192  painter->drawArc(arc, 0, 1440);
193  }
194  painter->restore();
195  }
196  //END: top left corner
197 
198  //BEGIN: left vertical line
199  {
200  QPoint start;
201  QPoint verticalGradBottom;
202  if (leftToRight) {
203  start = optRect.topLeft();
204  verticalGradBottom = optRect.topLeft();
205  } else {
206  start = optRect.topRight();
207  verticalGradBottom = optRect.topRight();
208  }
209  start.ry() += 3;
210  verticalGradBottom.ry() += optRect.height() - 3;
211  QLinearGradient gradient(start, verticalGradBottom);
212  gradient.setColorAt(0, outlineColor);
213  gradient.setColorAt(1, option.palette.base().color());
214  painter->fillRect(QRect(start, QSize(1, optRect.height() - 3)), gradient);
215  }
216  //END: left vertical line
217 
218  //BEGIN: horizontal line
219  {
220  QPoint start;
221  QPoint horizontalGradTop;
222  if (leftToRight) {
223  start = optRect.topLeft();
224  horizontalGradTop = optRect.topLeft();
225  start.rx() += 3;
226  horizontalGradTop.rx() += optRect.width() - 3;
227  } else {
228  start = optRect.topRight();
229  horizontalGradTop = optRect.topRight();
230  start.rx() -= 3;
231  horizontalGradTop.rx() -= optRect.width() - 3;
232  }
233  QLinearGradient gradient(start, horizontalGradTop);
234  gradient.setColorAt(0, outlineColor);
235  gradient.setColorAt(1, option.palette.base().color());
236  QSize rectSize;
237  if (leftToRight) {
238  rectSize = QSize(optRect.width() - 3, 1);
239  } else {
240  rectSize = QSize(-optRect.width() + 3, 1);
241  }
242  painter->fillRect(QRect(start, rectSize), gradient);
243  }
244  //END: horizontal line
245 
246  //BEGIN: draw text
247  {
248  const QString category = index.model()->data(index, Qt::DisplayRole).toString(); // KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
249  QRect textRect = QRect(option.rect.topLeft(), QSize(option.rect.width() - 2 - 3 - 3, height));
250  textRect.setTop(textRect.top() + 2 + 3 /* corner */);
251  textRect.setLeft(textRect.left() + 2 + 3 /* corner */ + 3 /* a bit of margin */);
252  painter->save();
253  painter->setFont(font);
254  QColor penColor(option.palette.text().color());
255  penColor.setAlphaF(0.6);
256  painter->setPen(penColor);
257  painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, category);
258  painter->restore();
259  }
260  //END: draw text
261 }
262 
263 int KateCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
264 {
265  Q_UNUSED( index );
266  Q_UNUSED( option );
267 
268  QFont font(QApplication::font());
269  font.setBold(true);
270  const QFontMetrics fontMetrics = QFontMetrics(font);
271 
272  return fontMetrics.height() + 2 + 12 /* vertical spacing */;
273 }
QModelIndex
QPoint::rx
int & rx()
QPoint::ry
int & ry()
QRect::topRight
QPoint topRight() const
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QGradient::setColorAt
void setColorAt(qreal position, const QColor &color)
QFont
KateCategoryDrawer::categoryHeight
virtual int categoryHeight(const QModelIndex &index, const QStyleOption &option) const
Definition: katecategorydrawer.cpp:263
QRect::bottomRight
QPoint bottomRight() const
QRect::translate
void translate(int dx, int dy)
QPainter::save
void save()
QRect::bottomLeft
QPoint bottomLeft() const
QRect::height
int height() const
QPoint
QFontMetrics
QLinearGradient
QPainter::layoutDirection
Qt::LayoutDirection layoutDirection() const
QPoint::x
int x() const
QPoint::y
int y() const
QPointF
QFont::setBold
void setBold(bool enable)
QStyleOption
QRect
QPainter::setFont
void setFont(const QFont &font)
QApplication::font
QFont font()
QRectF::translate
void translate(qreal dx, qreal dy)
QPainter::drawArc
void drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
QRect::top
int top() const
QPainter::fillPath
void fillPath(const QPainterPath &path, const QBrush &brush)
QPainter::setPen
void setPen(const QColor &color)
QPainterPath::lineTo
void lineTo(const QPointF &endPoint)
QRect::setTop
void setTop(int y)
QRect::left
int left() const
QPainter
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
KCategoryDrawerV3
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
QString
QColor
QLinearGradient::setFinalStop
void setFinalStop(const QPointF &stop)
QSize
QPainter::restore
void restore()
QPainterPath
QRect::width
int width() const
QModelIndex::model
const QAbstractItemModel * model() const
QSizeF
QRectF
katecategorydrawer.h
QFontMetrics::height
int height() const
QRect::topLeft
QPoint topLeft() const
QPointF::rx
qreal & rx()
QLinearGradient::setStart
void setStart(const QPointF &start)
QColor::setAlphaF
void setAlphaF(qreal alpha)
KateCategoryDrawer::drawCategory
virtual void drawCategory(const QModelIndex &index, int sortRole, const QStyleOption &option, QPainter *painter) const
Definition: katecategorydrawer.cpp:35
QVariant::toString
QString toString() const
QRect::setLeft
void setLeft(int x)
KateCategoryDrawer::KateCategoryDrawer
KateCategoryDrawer()
Definition: katecategorydrawer.cpp:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

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