Falkon Develop
Cross-platform Qt-based web browser
iconchooser.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "iconchooser.h"
19#include "ui_iconchooser.h"
20#include "mainapplication.h"
21#include "proxystyle.h"
22#include "qztools.h"
23#include "sqldatabase.h"
24
25#include <QFileDialog>
26
28 : QDialog(parent),
29 ui(new Ui::IconChooser)
30{
31 ui->setupUi(this);
32
33 ui->iconList->setItemDelegate(new IconChooserDelegate(ui->iconList));
34
35 connect(ui->chooseFile, &QAbstractButton::clicked, this, &IconChooser::chooseFile);
36 connect(ui->siteUrl, &QLineEdit::textChanged, this, &IconChooser::searchIcon);
37}
38
39void IconChooser::chooseFile()
40{
41 const QString fileTypes = QSL("%3(*.png *.jpg *.jpeg *.gif)").arg(tr("Image files"));
42 const QString path = QzTools::getOpenFileName(QSL("IconChooser-ChangeIcon"), this, tr("Choose icon..."), QDir::homePath(), fileTypes);
43
44 if (path.isEmpty()) {
45 return;
46 }
47
48 ui->iconList->clear();
49 QIcon icon(path);
50
51 if (!icon.isNull()) {
52 auto* item = new QListWidgetItem(ui->iconList);
53 item->setIcon(icon);
54
55 ui->iconList->setCurrentItem(item);
56 }
57}
58
59void IconChooser::searchIcon(const QString &string)
60{
61 if (string.size() < 4) {
62 return;
63 }
64
65 ui->iconList->clear();
66
67 QSqlQuery query(SqlDatabase::instance()->database());
68 query.prepare(QSL("SELECT icon FROM icons WHERE url GLOB ? LIMIT 20"));
69 query.addBindValue(QString(QL1S("*%1*")).arg(QzTools::escapeSqlGlobString(string)));
70 query.exec();
71
72 while (query.next()) {
73 QImage image = QImage::fromData(query.value(0).toByteArray());
74 if (!image.isNull()) {
75 auto* item = new QListWidgetItem(ui->iconList);
76 item->setIcon(QPixmap::fromImage(image));
77 }
78 }
79}
80
82{
83 QIcon icon;
84 int status = QDialog::exec();
85
86 if (status == QDialog::Accepted) {
87 QList<QListWidgetItem*> selectedItems = ui->iconList->selectedItems();
88 if (!selectedItems.isEmpty()) {
89 icon = selectedItems.at(0)->icon();
90 }
91 }
92
93 // Ensure we are returning 16×16px icon
94 if (!icon.isNull()) {
95 icon = icon.pixmap(16);
96 }
97
98 return icon;
99}
100
102{
103 delete ui;
104}
105
107 : QStyledItemDelegate(parent)
108{
109}
110
111void IconChooserDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
112{
113 QStyleOptionViewItem opt = option;
114 initStyleOption(&opt, index);
115
116 const QWidget* w = opt.widget;
117 const QStyle* style = w ? w->style() : QApplication::style();
118
119 // Draw background
120 opt.showDecorationSelected = true;
121 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
122
123 // Draw icon
124 const int padding = opt.rect.width() / 4;
125 const QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
126 icon.paint(painter, opt.rect.adjusted(padding, padding, -padding, -padding));
127}
128
129QSize IconChooserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
130{
131 Q_UNUSED(option)
132 Q_UNUSED(index)
133
134 return QSize(48, 48);
135}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
IconChooserDelegate(QWidget *parent=nullptr)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
IconChooser(QWidget *parent=nullptr)
Definition: iconchooser.cpp:27
QIcon getIcon()
Definition: iconchooser.cpp:81
static QString escapeSqlGlobString(QString urlString)
Definition: qztools.cpp:247
static QString getOpenFileName(const QString &name, QWidget *parent=nullptr, const QString &caption=QString(), const QString &dir=QString(), const QString &filter=QString(), QString *selectedFilter=nullptr, QFileDialog::Options options=QFileDialog::Options())
Definition: qztools.cpp:659
static SqlDatabase * instance()
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40