2012-05-22 11:46:22 +02:00
|
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
|
* Falkon - Qt web browser
|
2017-12-18 13:06:42 +01:00
|
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2012-05-22 11:46:22 +02:00
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
* ============================================================ */
|
|
|
|
|
#include "iconchooser.h"
|
|
|
|
|
#include "ui_iconchooser.h"
|
2014-02-11 22:53:06 +01:00
|
|
|
|
#include "mainapplication.h"
|
|
|
|
|
#include "proxystyle.h"
|
2013-12-25 19:06:58 +01:00
|
|
|
|
#include "qztools.h"
|
2017-12-18 13:06:42 +01:00
|
|
|
|
#include "sqldatabase.h"
|
2012-05-22 11:46:22 +02:00
|
|
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
|
|
|
|
|
IconChooser::IconChooser(QWidget* parent)
|
|
|
|
|
: QDialog(parent),
|
|
|
|
|
ui(new Ui::IconChooser)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
|
|
ui->iconList->setItemDelegate(new IconChooserDelegate(ui->iconList));
|
|
|
|
|
|
|
|
|
|
connect(ui->chooseFile, SIGNAL(clicked()), this, SLOT(chooseFile()));
|
|
|
|
|
connect(ui->siteUrl, SIGNAL(textChanged(QString)), this, SLOT(searchIcon(QString)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IconChooser::chooseFile()
|
|
|
|
|
{
|
2013-12-30 13:43:48 +01:00
|
|
|
|
const QString fileTypes = QString("%3(*.png *.jpg *.jpeg *.gif)").arg(tr("Image files"));
|
|
|
|
|
const QString path = QzTools::getOpenFileName("IconChooser-ChangeIcon", this, tr("Choose icon..."), QDir::homePath(), fileTypes);
|
2012-05-22 11:46:22 +02:00
|
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui->iconList->clear();
|
|
|
|
|
QIcon icon(path);
|
|
|
|
|
|
|
|
|
|
if (!icon.isNull()) {
|
|
|
|
|
QListWidgetItem* item = new QListWidgetItem(ui->iconList);
|
|
|
|
|
item->setIcon(icon);
|
|
|
|
|
|
|
|
|
|
ui->iconList->setCurrentItem(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IconChooser::searchIcon(const QString &string)
|
|
|
|
|
{
|
|
|
|
|
if (string.size() < 4) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui->iconList->clear();
|
|
|
|
|
|
2017-12-18 13:06:42 +01:00
|
|
|
|
QSqlQuery query(SqlDatabase::instance()->database());
|
2016-12-20 14:19:58 +01:00
|
|
|
|
query.prepare(QSL("SELECT icon FROM icons WHERE url GLOB ? LIMIT 20"));
|
|
|
|
|
query.addBindValue(QString(QL1S("*%1*")).arg(QzTools::escapeSqlGlobString(string)));
|
2012-05-22 11:46:22 +02:00
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
|
|
while (query.next()) {
|
|
|
|
|
QImage image = QImage::fromData(query.value(0).toByteArray());
|
|
|
|
|
if (!image.isNull()) {
|
|
|
|
|
QListWidgetItem* item = new QListWidgetItem(ui->iconList);
|
|
|
|
|
item->setIcon(QPixmap::fromImage(image));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
|
QIcon IconChooser::getIcon()
|
2012-05-22 11:46:22 +02:00
|
|
|
|
{
|
|
|
|
|
QIcon icon;
|
|
|
|
|
int status = QDialog::exec();
|
|
|
|
|
|
|
|
|
|
if (status == QDialog::Accepted) {
|
|
|
|
|
QList<QListWidgetItem*> selectedItems = ui->iconList->selectedItems();
|
|
|
|
|
if (!selectedItems.isEmpty()) {
|
|
|
|
|
icon = selectedItems.at(0)->icon();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure we are returning 16×16px icon
|
|
|
|
|
if (!icon.isNull()) {
|
|
|
|
|
icon = icon.pixmap(16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IconChooser::~IconChooser()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IconChooserDelegate::IconChooserDelegate(QWidget* parent)
|
|
|
|
|
: QStyledItemDelegate(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IconChooserDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
2016-04-24 10:17:13 +02:00
|
|
|
|
QStyleOptionViewItem opt = option;
|
2012-05-22 11:46:22 +02:00
|
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
|
|
const QWidget* w = opt.widget;
|
|
|
|
|
const QStyle* style = w ? w->style() : QApplication::style();
|
|
|
|
|
|
|
|
|
|
// Draw background
|
2016-12-25 20:35:57 +01:00
|
|
|
|
opt.showDecorationSelected = true;
|
|
|
|
|
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, w);
|
2012-05-22 11:46:22 +02:00
|
|
|
|
|
|
|
|
|
// Draw icon
|
2016-12-27 14:18:33 +01:00
|
|
|
|
const int padding = opt.rect.width() / 4;
|
2016-12-25 20:35:57 +01:00
|
|
|
|
const QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
|
2016-12-27 14:18:33 +01:00
|
|
|
|
icon.paint(painter, opt.rect.adjusted(padding, padding, -padding, -padding));
|
2012-05-22 11:46:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize IconChooserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(option)
|
|
|
|
|
Q_UNUSED(index)
|
|
|
|
|
|
|
|
|
|
return QSize(48, 48);
|
|
|
|
|
}
|