File indexing completed on 2024-05-19 05:42:25
0001 /* 0002 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0003 // SPDX-License-Identifier: Apache-2.0 0004 // 0005 // Licensed under the Apache License, Version 2.0 (the "License"); 0006 // you may not use this file except in compliance with the License. 0007 // You may obtain a copy of the License at 0008 // 0009 // http://www.apache.org/licenses/LICENSE-2.0 0010 // 0011 // Unless required by applicable law or agreed to in writing, software 0012 // distributed under the License is distributed on an "AS IS" BASIS, 0013 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0014 // See the License for the specific language governing permissions and 0015 // limitations under the License. 0016 */ 0017 0018 #include <QApplication> 0019 #include <QDebug> 0020 #include <QModelIndex> 0021 #include <QMouseEvent> 0022 #include <QPainter> 0023 #include <QStyleOptionViewItem> 0024 0025 #include <ct_lvtmdl_modelhelpers.h> 0026 #include <ct_lvtqtc_iconhelpers.h> 0027 #include <ct_lvtqtd_packageviewdelegate.h> 0028 0029 namespace Codethink::lvtqtd { 0030 struct PackageViewDelegate::Private { }; 0031 0032 PackageViewDelegate::PackageViewDelegate(QObject *parent): 0033 QStyledItemDelegate(parent), d(std::make_unique<PackageViewDelegate::Private>()) 0034 { 0035 } 0036 0037 PackageViewDelegate::~PackageViewDelegate() noexcept = default; 0038 0039 void PackageViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 0040 { 0041 QStyledItemDelegate::paint(painter, option, index); 0042 if (!index.isValid()) { 0043 return; 0044 } 0045 0046 QRect rect = option.rect.adjusted(1, 1, -1, -1); 0047 rect.setX(rect.x() + rect.width() - rect.height()); 0048 rect.setWidth(rect.height()); 0049 0050 if (!index.data(lvtmdl::ModelRoles::e_RecursiveLakosian).toBool()) { 0051 static QIcon warningIcon(":/icons/yellow-warning"); 0052 warningIcon.paint(painter, rect); 0053 } 0054 } 0055 0056 void PackageViewDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex& index) const 0057 { 0058 QStyledItemDelegate::initStyleOption(option, index); 0059 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0060 if (auto *v4 = qstyleoption_cast<QStyleOptionViewItemV4 *>(option)) { 0061 const auto origIcon = index.data(Qt::DecorationRole).value<QIcon>(); 0062 v4->icon = IconHelpers::iconFrom(origIcon); 0063 } 0064 #else 0065 if (auto *v4 = qstyleoption_cast<QStyleOptionViewItem *>(option)) { 0066 const auto origIcon = index.data(Qt::DecorationRole).value<QIcon>(); 0067 v4->icon = IconHelpers::iconFrom(origIcon); 0068 } 0069 #endif 0070 } 0071 0072 } // namespace Codethink::lvtqtd