File indexing completed on 2024-05-12 15:59:58
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2019 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <QAbstractItemView> 0008 #include <QPainter> 0009 #include <QApplication> 0010 #include <QStyle> 0011 #include <QDebug> 0012 #include <QMessageBox> 0013 0014 #include <QListView> 0015 0016 #include "KisResourceTypes.h" 0017 #include "KisResourceModel.h" 0018 #include "KisStorageChooserWidget.h" 0019 #include "KisStorageModel.h" 0020 #include "KisStorageFilterProxyModel.h" 0021 #include <KoIcon.h> 0022 0023 KisStorageChooserDelegate::KisStorageChooserDelegate(QObject *parent) 0024 : QAbstractItemDelegate(parent) 0025 { 0026 } 0027 0028 void KisStorageChooserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0029 { 0030 if (!index.isValid()) return; 0031 0032 painter->save(); 0033 0034 QString name = index.sibling(index.row(), KisStorageModel::DisplayName).data(Qt::DisplayRole).value<QString>(); 0035 QString location = index.sibling(index.row(), KisStorageModel::Location).data(Qt::DisplayRole).value<QString>(); 0036 bool active = index.data(Qt::UserRole + KisStorageModel::Active).value<bool>(); 0037 QString storageType = index.data(Qt::UserRole + KisStorageModel::StorageType).value<QString>(); 0038 0039 QImage thumbnail = index.data(Qt::UserRole + + KisStorageModel::Thumbnail).value<QImage>(); 0040 0041 qreal devicePixelRatioF = painter->device()->devicePixelRatioF(); 0042 0043 if (thumbnail.isNull()) { 0044 //fallback on cute icons. 0045 thumbnail = koIcon("warning").pixmap(option.decorationSize).toImage(); 0046 if (storageType == "Folder") { 0047 thumbnail = koIcon("document-open").pixmap(option.decorationSize).toImage(); 0048 } 0049 else if (storageType == "Adobe Style Library") { 0050 thumbnail = koIcon("layer-style-enabled").pixmap(option.decorationSize).toImage(); 0051 if (!thumbnail.isNull()) { 0052 thumbnail = thumbnail.scaled(option.decorationSize, Qt::KeepAspectRatio, Qt::FastTransformation); 0053 } 0054 } 0055 else if (storageType == "Adobe Brush Library") { 0056 thumbnail = koIcon("select-all").pixmap(option.decorationSize).toImage(); 0057 } 0058 else if (storageType == "Memory") { 0059 if (location != "memory") { 0060 thumbnail = koIcon("document-new").pixmap(option.decorationSize).toImage(); 0061 } else { 0062 thumbnail = koIcon("drive-harddisk").pixmap(option.decorationSize).toImage(); 0063 } 0064 0065 } 0066 else if (storageType == "Bundle") { 0067 thumbnail = koIcon("bundle_archive").pixmap(option.decorationSize).toImage(); 0068 } 0069 0070 } else { 0071 if (!thumbnail.isNull()) { 0072 thumbnail = thumbnail.scaled(option.decorationSize*devicePixelRatioF, Qt::KeepAspectRatio, Qt::SmoothTransformation); 0073 } 0074 thumbnail.setDevicePixelRatio(devicePixelRatioF); 0075 } 0076 0077 QColor penColor(option.palette.text().color()); 0078 0079 QStyleOptionViewItem opt = option; 0080 0081 if (active) { 0082 opt.state = QStyle::State_Sunken; 0083 } 0084 0085 QApplication::style()->drawPrimitive(QStyle::PE_PanelButtonTool, &opt, painter); 0086 0087 painter->setPen(penColor); 0088 painter->drawImage(option.rect.topLeft()+QPoint(4, 4), thumbnail, thumbnail.rect()); 0089 QRect text = option.rect; 0090 text.setLeft(text.left()+option.decorationSize.width()+8); 0091 text.setTop(text.top()+4); 0092 painter->drawText(text, Qt::TextWordWrap, name.split("_").join(" ")); 0093 0094 painter->restore(); 0095 } 0096 0097 QSize KisStorageChooserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0098 { 0099 Q_UNUSED(index); 0100 int w = 200; 0101 int h = option.decorationSize.height()+8; 0102 return QSize(w, h); 0103 } 0104 0105 KisStorageChooserWidget::KisStorageChooserWidget(const QString &resourceType, QWidget *parent) 0106 : KisPopupButton(parent) 0107 , m_resourceType(resourceType) 0108 { 0109 QListView *view = new QListView(this); 0110 0111 KisStorageFilterProxyModel *proxyModel = new KisStorageFilterProxyModel(this); 0112 0113 proxyModel->setSourceModel(KisStorageModel::instance()); 0114 0115 QStringList filter; 0116 filter << KisResourceStorage::storageTypeToUntranslatedString(KisResourceStorage::StorageType::Bundle); 0117 if (m_resourceType == ResourceType::Brushes) { 0118 filter << KisResourceStorage::storageTypeToUntranslatedString(KisResourceStorage::StorageType::AdobeBrushLibrary); 0119 } 0120 if (m_resourceType == ResourceType::LayerStyles) { 0121 filter << KisResourceStorage::storageTypeToUntranslatedString(KisResourceStorage::StorageType::AdobeStyleLibrary); 0122 } 0123 0124 proxyModel->setFilter(KisStorageFilterProxyModel::ByStorageType, filter); 0125 view->setModel(proxyModel); 0126 view->setIconSize(QSize(64, 64)); 0127 view->setItemDelegate(new KisStorageChooserDelegate(this)); 0128 view->setSelectionMode(QAbstractItemView::SingleSelection); 0129 connect(view, SIGNAL(clicked(QModelIndex)), this, SLOT(activated(QModelIndex))); 0130 this->setPopupWidget(view); 0131 } 0132 0133 void KisStorageChooserWidget::activated(const QModelIndex &index) 0134 { 0135 if (!index.isValid()) return; 0136 0137 bool active = index.data(Qt::UserRole + KisStorageModel::Active).value<bool>(); 0138 KisStorageModel::instance()->setData(index, !active, Qt::CheckStateRole); 0139 0140 KisStorageFilterProxyModel proxy; 0141 proxy.setSourceModel(KisStorageModel::instance()); 0142 0143 QStringList filter; 0144 filter << KisResourceStorage::storageTypeToUntranslatedString(KisResourceStorage::StorageType::Bundle); 0145 if (m_resourceType == ResourceType::Brushes) { 0146 filter << KisResourceStorage::storageTypeToUntranslatedString(KisResourceStorage::StorageType::AdobeBrushLibrary); 0147 } 0148 if (m_resourceType == ResourceType::LayerStyles) { 0149 filter << KisResourceStorage::storageTypeToUntranslatedString(KisResourceStorage::StorageType::AdobeStyleLibrary); 0150 } 0151 0152 proxy.setFilter(KisStorageFilterProxyModel::ByStorageType, filter); 0153 0154 QString warning; 0155 if (!proxy.rowCount()) { 0156 warning = i18n("All bundles have been deactivated."); 0157 } 0158 0159 KisResourceModel resourceModel(m_resourceType); 0160 resourceModel.setResourceFilter(KisResourceModel::ShowActiveResources); 0161 if (!resourceModel.rowCount()) { 0162 warning += i18n("\nThere are no resources of type %1 available. Please enable at least one bundle.", ResourceName::resourceTypeToName(m_resourceType)); 0163 0164 } 0165 0166 if (!warning.isEmpty()) { 0167 QMessageBox::critical(qApp->activeWindow(), i18nc("@title:window", "Krita"), warning); 0168 } 0169 0170 } 0171 0172 KisStorageChooserWidget::~KisStorageChooserWidget() 0173 { 0174 0175 } 0176 0177