File indexing completed on 2024-12-01 11:10:33

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "layoutsheaderview.h"
0007 
0008 // local
0009 #include "layoutsmodel.h"
0010 
0011 // Qt
0012 #include <QAbstractItemModel>
0013 #include <QPainter>
0014 
0015 
0016 namespace Latte {
0017 namespace Settings {
0018 namespace Layouts {
0019 
0020 HeaderView::HeaderView(Qt::Orientation orientation, QWidget *parent)
0021     : QHeaderView(orientation, parent)
0022 {
0023     setSectionsClickable(true);
0024     setSectionsMovable(true);
0025     setSortIndicatorShown(true);
0026 }
0027 
0028 void HeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
0029 {   
0030     painter->setRenderHint(QPainter::Antialiasing, true);
0031 
0032     if (logicalIndex == Model::Layouts::BACKGROUNDCOLUMN) {
0033         QString text = model()->headerData(Model::Layouts::BACKGROUNDCOLUMN, Qt::Horizontal, Qt::DisplayRole).toString();
0034         QIcon icon = model()->headerData(Model::Layouts::BACKGROUNDCOLUMN, Qt::Horizontal, Qt::DecorationRole).value<QIcon>();
0035 
0036         if (text.isEmpty() && !icon.isNull()) {
0037             //! draw centered icon
0038             QHeaderView::paintSection(painter, rect, Model::Layouts::HIDDENTEXTCOLUMN);
0039 
0040             int margin = 4;
0041             int thick = rect.height() - 2*margin;
0042             int iX = rect.x() + (rect.width()/2) - (thick/2);
0043 
0044             painter->drawPixmap(QRect(iX, rect.y() + margin, thick, thick), icon.pixmap(thick, thick, QIcon::Normal));
0045             return;
0046         }
0047     }
0048 
0049     QHeaderView::paintSection(painter, rect, logicalIndex);
0050 }
0051 
0052 }
0053 }
0054 }
0055