File indexing completed on 2024-04-21 16:29:35

0001 /***************************************************************************
0002  *   Copyright (C) 2009 by Rafael Fernández López <ereslibre@kde.org>    *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
0018  ***************************************************************************/
0019 
0020 #include "CategoryDrawer.h"
0021 
0022 #include <KCategorizedSortFilterProxyModel>
0023 
0024 #include <QPainter>
0025 #include <QApplication>
0026 #include <QStyleOption>
0027 
0028 CategoryDrawer::CategoryDrawer(KCategorizedView *view)
0029  : KCategoryDrawer(view)
0030 {
0031     //! setLeftMargin( 7 );
0032     //! setRightMargin( 7 );
0033 }
0034 
0035 void CategoryDrawer::drawCategory(const QModelIndex &index,
0036                                             int sortRole,
0037                                             const QStyleOption &option,
0038                                             QPainter *painter) const
0039 {
0040     Q_UNUSED( option )
0041     Q_UNUSED( painter )
0042     Q_UNUSED( sortRole )
0043 
0044     painter->setRenderHint(QPainter::Antialiasing);
0045 
0046     const QRect optRect = option.rect;
0047     QFont font(QApplication::font());
0048     font.setBold(true);
0049     const QFontMetrics fontMetrics = QFontMetrics(font);
0050     const int height = categoryHeight(index, option);
0051 
0052     {
0053         QRect newOptRect(optRect);
0054         newOptRect.setLeft(newOptRect.left() + 1);
0055         newOptRect.setTop(newOptRect.top() + 1);
0056 
0057         //BEGIN: inner top left corner
0058         {
0059             painter->save();
0060             painter->setPen(option.palette.base().color());
0061             const QPointF topLeft(newOptRect.topLeft());
0062             QRectF arc(topLeft, QSizeF(4, 4));
0063             arc.translate(0.5, 0.5);
0064             painter->drawArc(arc, 1440, 1440);
0065             painter->restore();
0066         }
0067         //END: inner top left corner
0068 
0069         //BEGIN: inner left vertical line
0070         {
0071             QPoint start(newOptRect.topLeft());
0072             start.ry() += 3;
0073             QPoint verticalGradBottom(newOptRect.topLeft());
0074             verticalGradBottom.ry() += newOptRect.height() - 3;
0075             QLinearGradient gradient(start, verticalGradBottom);
0076             gradient.setColorAt(0, option.palette.base().color());
0077             gradient.setColorAt(1, Qt::transparent);
0078             painter->fillRect(QRect(start, QSize(1, newOptRect.height() - 3)), gradient);
0079         }
0080         //END: inner left vertical line
0081 
0082         //BEGIN: inner horizontal line
0083         {
0084             QPoint start(newOptRect.topLeft());
0085             start.rx() += 3;
0086             QPoint horizontalGradTop(newOptRect.topLeft());
0087             horizontalGradTop.rx() += newOptRect.width() - 3;
0088             QLinearGradient gradient(start, horizontalGradTop);
0089             gradient.setColorAt(0, option.palette.base().color());
0090             gradient.setColorAt(1, Qt::transparent);
0091             painter->fillRect(QRect(start, QSize(newOptRect.width() - 3, 1)), gradient);
0092         }
0093         //END: inner horizontal line
0094     }
0095 
0096     QColor outlineColor = option.palette.text().color();
0097     outlineColor.setAlphaF(0.35);
0098 
0099     //BEGIN: top left corner
0100     {
0101         painter->save();
0102         painter->setPen(outlineColor);
0103         const QPointF topLeft(optRect.topLeft());
0104         QRectF arc(topLeft, QSizeF(4, 4));
0105         arc.translate(0.5, 0.5);
0106         painter->drawArc(arc, 1440, 1440);
0107         painter->restore();
0108     }
0109     //END: top left corner
0110 
0111     //BEGIN: left vertical line
0112     {
0113         QPoint start(optRect.topLeft());
0114         start.ry() += 3;
0115         QPoint verticalGradBottom(optRect.topLeft());
0116         verticalGradBottom.ry() += optRect.height() - 3;
0117         QLinearGradient gradient(start, verticalGradBottom);
0118         gradient.setColorAt(0, outlineColor);
0119         gradient.setColorAt(1, option.palette.base().color());
0120         painter->fillRect(QRect(start, QSize(1, optRect.height() - 3)), gradient);
0121     }
0122     //END: left vertical line
0123 
0124     //BEGIN: horizontal line
0125     {
0126         QPoint start(optRect.topLeft());
0127         start.rx() += 3;
0128         QPoint horizontalGradTop(optRect.topLeft());
0129         horizontalGradTop.rx() += optRect.width() - 3;
0130         QLinearGradient gradient(start, horizontalGradTop);
0131         gradient.setColorAt(0, outlineColor);
0132         gradient.setColorAt(1, option.palette.base().color());
0133         painter->fillRect(QRect(start, QSize(optRect.width() - 3, 1)), gradient);
0134     }
0135     //END: horizontal line
0136 
0137     //BEGIN: draw text
0138     {
0139         const QString category = index.data(KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
0140         QRect textRect = QRect(option.rect.topLeft(), QSize(option.rect.width(), height));
0141         textRect.setTop(textRect.top() + 2 + 3 /* corner */);
0142         textRect.setLeft(textRect.left() + 2 + 3 /* corner */ + 3 /* a bit of margin */);
0143         painter->save();
0144         painter->setFont(font);
0145         QColor penColor(option.palette.text().color());
0146         penColor.setAlphaF(0.6);
0147         painter->setPen(penColor);
0148         painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, category);
0149         painter->restore();
0150     }
0151     //END: draw text
0152 }
0153 
0154 int CategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
0155 {
0156     Q_UNUSED( index );
0157     Q_UNUSED( option );
0158 
0159     QFont font(QApplication::font());
0160     font.setBold(true);
0161     const QFontMetrics fontMetrics = QFontMetrics(font);
0162 
0163     return fontMetrics.height() + 2 + 12 /* vertical spacing */;
0164 }