File indexing completed on 2025-04-27 03:58:28

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-01-16
0007  * Description : drawing item view based on DCategorizedView
0008  *
0009  * SPDX-FileCopyrightText: 2007      by Rafael Fernández López <ereslibre at kde dot org>
0010  * SPDX-FileCopyrightText: 2009-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "dcategorydrawer.h"
0018 
0019 // Qt includes
0020 
0021 #include <QPainter>
0022 #include <QStyleOption>
0023 #include <QApplication>
0024 
0025 // Local includes
0026 
0027 #include "dcategorizedview.h"
0028 #include "dcategorizedsortfilterproxymodel.h"
0029 
0030 namespace Digikam
0031 {
0032 
0033 class Q_DECL_HIDDEN DCategoryDrawer::Private
0034 {
0035 public:
0036     explicit Private(DCategorizedView* const view)
0037         : view(view)
0038     {
0039     }
0040 
0041     ~Private()
0042     {
0043     }
0044 
0045     DCategorizedView* view;
0046 };
0047 
0048 
0049 DCategoryDrawer::DCategoryDrawer(DCategorizedView* const view)
0050      : QObject(view),
0051        d      (new Private(view))
0052 {
0053 }
0054 
0055 DCategoryDrawer::~DCategoryDrawer()
0056 {
0057     delete d;
0058 }
0059 
0060 void DCategoryDrawer::drawCategory(const QModelIndex& index,
0061                                    int /*sortRole*/,
0062                                    const QStyleOption& option,
0063                                    QPainter* painter) const
0064 {
0065     painter->setRenderHint(QPainter::Antialiasing);
0066 
0067     const QString category         = index.model()->data(index, DCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
0068     const QRect optRect            = option.rect;
0069     QFont font(QApplication::font());
0070     font.setBold(true);
0071     const QFontMetrics fontMetrics = QFontMetrics(font);
0072     QColor outlineColor            = option.palette.text().color();
0073     outlineColor.setAlphaF(0.35);
0074 
0075     //BEGIN: top left corner
0076     {
0077         painter->save();
0078         painter->setPen(outlineColor);
0079         const QPointF topLeft(optRect.topLeft());
0080         QRectF arc(topLeft, QSizeF(4, 4));
0081         arc.translate(0.5, 0.5);
0082         painter->drawArc(arc, 1440, 1440);
0083         painter->restore();
0084     }
0085     //END: top left corner
0086 
0087     //BEGIN: left vertical line
0088     {
0089         QPoint start(optRect.topLeft());
0090         start.ry()              += 3;
0091         QPoint verticalGradBottom(optRect.topLeft());
0092         verticalGradBottom.ry() += fontMetrics.height() + 5;
0093         QLinearGradient gradient(start, verticalGradBottom);
0094         gradient.setColorAt(0, outlineColor);
0095         gradient.setColorAt(1, Qt::transparent);
0096         painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient);
0097     }
0098     //END: left vertical line
0099 
0100     //BEGIN: horizontal line
0101     {
0102         QPoint start(optRect.topLeft());
0103         start.rx()             += 3;
0104         QPoint horizontalGradTop(optRect.topLeft());
0105         horizontalGradTop.rx() += optRect.width() - 6;
0106         painter->fillRect(QRect(start, QSize(optRect.width() - 6, 1)), outlineColor);
0107     }
0108     //END: horizontal line
0109 
0110     //BEGIN: top right corner
0111     {
0112         painter->save();
0113         painter->setPen(outlineColor);
0114         QPointF topRight(optRect.topRight());
0115         topRight.rx() -= 4;
0116         QRectF arc(topRight, QSizeF(4, 4));
0117         arc.translate(0.5, 0.5);
0118         painter->drawArc(arc, 0, 1440);
0119         painter->restore();
0120     }
0121     //END: top right corner
0122 
0123     //BEGIN: right vertical line
0124     {
0125         QPoint start(optRect.topRight());
0126         start.ry()              += 3;
0127         QPoint verticalGradBottom(optRect.topRight());
0128         verticalGradBottom.ry() += fontMetrics.height() + 5;
0129         QLinearGradient gradient(start, verticalGradBottom);
0130         gradient.setColorAt(0, outlineColor);
0131         gradient.setColorAt(1, Qt::transparent);
0132         painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient);
0133     }
0134     //END: right vertical line
0135 
0136     //BEGIN: text
0137     {
0138         QRect textRect(option.rect);
0139         textRect.setTop(textRect.top() + 7);
0140         textRect.setLeft(textRect.left() + 7);
0141         textRect.setHeight(fontMetrics.height());
0142         textRect.setRight(textRect.right() - 7);
0143 
0144         painter->save();
0145         painter->setFont(font);
0146         QColor penColor(option.palette.text().color());
0147         penColor.setAlphaF(0.6);
0148         painter->setPen(penColor);
0149         painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, category);
0150         painter->restore();
0151     }
0152     //END: text
0153 }
0154 
0155 int DCategoryDrawer::categoryHeight(const QModelIndex& index, const QStyleOption& option) const
0156 {
0157     Q_UNUSED(index);
0158     Q_UNUSED(option)
0159 
0160     QFont font(QApplication::font());
0161     font.setBold(true);
0162     QFontMetrics fontMetrics(font);
0163 
0164     const int height = fontMetrics.height() + 1  /* 1 pixel-width gradient */
0165                                             + 11 /* top and bottom separation */;
0166     return height;
0167 }
0168 
0169 int DCategoryDrawer::leftMargin() const
0170 {
0171     return 0;
0172 }
0173 
0174 int DCategoryDrawer::rightMargin() const
0175 {
0176     return 0;
0177 }
0178 
0179 DCategorizedView *DCategoryDrawer::view() const
0180 {
0181     return d->view;
0182 }
0183 
0184 void DCategoryDrawer::mouseButtonPressed(const QModelIndex&, const QRect&, QMouseEvent* event)
0185 {
0186     event->ignore();
0187 }
0188 
0189 void DCategoryDrawer::mouseButtonReleased(const QModelIndex&, const QRect&, QMouseEvent* event)
0190 {
0191     event->ignore();
0192 }
0193 
0194 void DCategoryDrawer::mouseMoved(const QModelIndex&, const QRect&, QMouseEvent* event)
0195 {
0196     event->ignore();
0197 }
0198 
0199 void DCategoryDrawer::mouseButtonDoubleClicked(const QModelIndex&, const QRect&, QMouseEvent* event)
0200 {
0201     event->ignore();
0202 }
0203 
0204 void DCategoryDrawer::mouseLeft(const QModelIndex&, const QRect&)
0205 {
0206 }
0207 
0208 } // namespace Digikam
0209 
0210 #include "moc_dcategorydrawer.cpp"