File indexing completed on 2024-05-12 04:58:12
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "iconchooser.h" 0019 #include "ui_iconchooser.h" 0020 #include "mainapplication.h" 0021 #include "proxystyle.h" 0022 #include "qztools.h" 0023 #include "sqldatabase.h" 0024 0025 #include <QFileDialog> 0026 0027 IconChooser::IconChooser(QWidget* parent) 0028 : QDialog(parent), 0029 ui(new Ui::IconChooser) 0030 { 0031 ui->setupUi(this); 0032 0033 ui->iconList->setItemDelegate(new IconChooserDelegate(ui->iconList)); 0034 0035 connect(ui->chooseFile, &QAbstractButton::clicked, this, &IconChooser::chooseFile); 0036 connect(ui->siteUrl, &QLineEdit::textChanged, this, &IconChooser::searchIcon); 0037 } 0038 0039 void IconChooser::chooseFile() 0040 { 0041 const QString fileTypes = QSL("%3(*.png *.jpg *.jpeg *.gif)").arg(tr("Image files")); 0042 const QString path = QzTools::getOpenFileName(QSL("IconChooser-ChangeIcon"), this, tr("Choose icon..."), QDir::homePath(), fileTypes); 0043 0044 if (path.isEmpty()) { 0045 return; 0046 } 0047 0048 ui->iconList->clear(); 0049 QIcon icon(path); 0050 0051 if (!icon.isNull()) { 0052 auto* item = new QListWidgetItem(ui->iconList); 0053 item->setIcon(icon); 0054 0055 ui->iconList->setCurrentItem(item); 0056 } 0057 } 0058 0059 void IconChooser::searchIcon(const QString &string) 0060 { 0061 if (string.size() < 4) { 0062 return; 0063 } 0064 0065 ui->iconList->clear(); 0066 0067 QSqlQuery query(SqlDatabase::instance()->database()); 0068 query.prepare(QSL("SELECT icon FROM icons WHERE url GLOB ? LIMIT 20")); 0069 query.addBindValue(QString(QL1S("*%1*")).arg(QzTools::escapeSqlGlobString(string))); 0070 query.exec(); 0071 0072 while (query.next()) { 0073 QImage image = QImage::fromData(query.value(0).toByteArray()); 0074 if (!image.isNull()) { 0075 auto* item = new QListWidgetItem(ui->iconList); 0076 item->setIcon(QPixmap::fromImage(image)); 0077 } 0078 } 0079 } 0080 0081 QIcon IconChooser::getIcon() 0082 { 0083 QIcon icon; 0084 int status = QDialog::exec(); 0085 0086 if (status == QDialog::Accepted) { 0087 QList<QListWidgetItem*> selectedItems = ui->iconList->selectedItems(); 0088 if (!selectedItems.isEmpty()) { 0089 icon = selectedItems.at(0)->icon(); 0090 } 0091 } 0092 0093 // Ensure we are returning 16×16px icon 0094 if (!icon.isNull()) { 0095 icon = icon.pixmap(16); 0096 } 0097 0098 return icon; 0099 } 0100 0101 IconChooser::~IconChooser() 0102 { 0103 delete ui; 0104 } 0105 0106 IconChooserDelegate::IconChooserDelegate(QWidget* parent) 0107 : QStyledItemDelegate(parent) 0108 { 0109 } 0110 0111 void IconChooserDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0112 { 0113 QStyleOptionViewItem opt = option; 0114 initStyleOption(&opt, index); 0115 0116 const QWidget* w = opt.widget; 0117 const QStyle* style = w ? w->style() : QApplication::style(); 0118 0119 // Draw background 0120 opt.showDecorationSelected = true; 0121 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w); 0122 0123 // Draw icon 0124 const int padding = opt.rect.width() / 4; 0125 const QIcon icon = index.data(Qt::DecorationRole).value<QIcon>(); 0126 icon.paint(painter, opt.rect.adjusted(padding, padding, -padding, -padding)); 0127 } 0128 0129 QSize IconChooserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0130 { 0131 Q_UNUSED(option) 0132 Q_UNUSED(index) 0133 0134 return QSize(48, 48); 0135 }