2011-10-29 12:24:12 +02:00
|
|
|
/* ============================================================
|
2017-06-08 13:08:38 +02:00
|
|
|
* QupZilla - Qt web browser
|
|
|
|
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
|
2011-10-29 12:24:12 +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/>.
|
|
|
|
* ============================================================ */
|
2011-10-21 23:26:34 +02:00
|
|
|
#include "searchenginesdialog.h"
|
|
|
|
#include "ui_searchenginesdialog.h"
|
|
|
|
#include "editsearchengine.h"
|
|
|
|
#include "mainapplication.h"
|
2017-06-08 13:08:38 +02:00
|
|
|
#include "removeitemfocusdelegate.h"
|
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
SearchEnginesDialog::SearchEnginesDialog(QWidget* parent)
|
2011-10-21 23:26:34 +02:00
|
|
|
: QDialog(parent)
|
|
|
|
, ui(new Ui::SearchEnginesDialog)
|
2012-03-08 13:05:57 +01:00
|
|
|
, m_manager(mApp->searchEnginesManager())
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2014-11-07 18:08:12 +01:00
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
connect(ui->add, SIGNAL(clicked()), this, SLOT(addEngine()));
|
|
|
|
connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeEngine()));
|
|
|
|
connect(ui->edit, SIGNAL(clicked()), this, SLOT(editEngine()));
|
2012-09-27 11:59:09 +02:00
|
|
|
connect(ui->setAsDefault, SIGNAL(clicked()), this, SLOT(setDefaultEngine()));
|
2011-10-21 23:26:34 +02:00
|
|
|
connect(ui->defaults, SIGNAL(clicked()), this, SLOT(defaults()));
|
2011-12-16 19:07:36 +01:00
|
|
|
connect(ui->moveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
|
|
|
|
connect(ui->moveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(editEngine()));
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2017-06-08 13:08:38 +02:00
|
|
|
ui->treeWidget->setItemDelegate(new RemoveItemFocusDelegate(ui->treeWidget));
|
2012-09-01 13:56:00 +02:00
|
|
|
ui->treeWidget->sortByColumn(-1);
|
2011-10-21 23:26:34 +02:00
|
|
|
reloadEngines();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::addEngine()
|
|
|
|
{
|
|
|
|
EditSearchEngine dialog(tr("Add Search Engine"), this);
|
|
|
|
dialog.hideIconLabels();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
SearchEngine engine;
|
|
|
|
engine.name = dialog.name();
|
|
|
|
engine.url = dialog.url();
|
2013-11-09 15:48:24 +01:00
|
|
|
engine.postData = dialog.postData().toUtf8();
|
2011-10-21 23:26:34 +02:00
|
|
|
engine.shortcut = dialog.shortcut();
|
2012-01-31 21:10:22 +01:00
|
|
|
engine.icon = SearchEnginesManager::iconForSearchEngine(QUrl::fromEncoded(dialog.url().toUtf8()));
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (engine.name.isEmpty() || engine.url.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem();
|
2012-09-27 11:59:09 +02:00
|
|
|
setEngine(item, engine);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
changeItemToDefault(item, false);
|
2011-10-21 23:26:34 +02:00
|
|
|
item->setIcon(0, engine.icon);
|
|
|
|
item->setText(1, engine.shortcut);
|
|
|
|
|
|
|
|
ui->treeWidget->addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::removeEngine()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item || ui->treeWidget->topLevelItemCount() == 1) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-28 18:44:45 +02:00
|
|
|
if (isDefaultEngine(item)) {
|
2012-09-27 11:59:09 +02:00
|
|
|
SearchEngine en = getEngine(item);
|
|
|
|
QMessageBox::warning(this, tr("Remove Engine"),
|
|
|
|
tr("You can't remove the default search engine.<br>"
|
2012-09-28 18:50:20 +02:00
|
|
|
"Set a different engine as default before removing %1.").arg(en.name));
|
2012-09-27 11:59:09 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete item;
|
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::editEngine()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
SearchEngine engine = getEngine(item);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
EditSearchEngine dialog(tr("Edit Search Engine"), this);
|
|
|
|
|
|
|
|
dialog.setName(engine.name);
|
|
|
|
dialog.setUrl(engine.url);
|
2013-11-09 15:48:24 +01:00
|
|
|
dialog.setPostData(engine.postData);
|
2011-10-21 23:26:34 +02:00
|
|
|
dialog.setShortcut(engine.shortcut);
|
|
|
|
dialog.setIcon(engine.icon);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
engine.name = dialog.name();
|
|
|
|
engine.url = dialog.url();
|
2013-11-09 15:48:24 +01:00
|
|
|
engine.postData = dialog.postData().toUtf8();
|
2011-10-21 23:26:34 +02:00
|
|
|
engine.shortcut = dialog.shortcut();
|
|
|
|
engine.icon = dialog.icon();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (engine.name.isEmpty() || engine.url.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
setEngine(item, engine);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-28 18:44:45 +02:00
|
|
|
changeItemToDefault(item, isDefaultEngine(item));
|
2011-10-21 23:26:34 +02:00
|
|
|
item->setIcon(0, engine.icon);
|
|
|
|
item->setText(1, engine.shortcut);
|
|
|
|
}
|
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
void SearchEnginesDialog::setDefaultEngine()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->currentItem();
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < ui->treeWidget->topLevelItemCount(); ++j) {
|
|
|
|
QTreeWidgetItem* i = ui->treeWidget->topLevelItem(j);
|
2012-09-28 18:44:45 +02:00
|
|
|
if (isDefaultEngine(i)) {
|
2012-09-27 11:59:09 +02:00
|
|
|
if (i == item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
changeItemToDefault(i, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changeItemToDefault(item, true);
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
void SearchEnginesDialog::defaults()
|
|
|
|
{
|
|
|
|
m_manager->restoreDefaults();
|
|
|
|
reloadEngines();
|
|
|
|
}
|
|
|
|
|
2012-09-28 18:44:45 +02:00
|
|
|
bool SearchEnginesDialog::isDefaultEngine(QTreeWidgetItem* item)
|
|
|
|
{
|
|
|
|
return item->data(0, DefaultRole).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchEngine SearchEnginesDialog::getEngine(QTreeWidgetItem* item)
|
|
|
|
{
|
|
|
|
return item->data(0, EngineRole).value<SearchEngine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::setEngine(QTreeWidgetItem* item, SearchEngine engine)
|
|
|
|
{
|
|
|
|
QVariant v;
|
|
|
|
v.setValue<SearchEngine>(engine);
|
|
|
|
item->setData(0, EngineRole, v);
|
|
|
|
item->setText(0, engine.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::changeItemToDefault(QTreeWidgetItem* item, bool isDefault)
|
|
|
|
{
|
|
|
|
QFont font = item->font(0);
|
|
|
|
font.setBold(isDefault);
|
|
|
|
|
|
|
|
item->setFont(0, font);
|
|
|
|
item->setFont(1, font);
|
|
|
|
item->setData(0, DefaultRole, isDefault);
|
|
|
|
}
|
|
|
|
|
2011-12-16 19:07:36 +01:00
|
|
|
void SearchEnginesDialog::moveUp()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* currentItem = ui->treeWidget->currentItem();
|
|
|
|
int index = ui->treeWidget->indexOfTopLevelItem(currentItem);
|
|
|
|
|
|
|
|
if (!currentItem || index == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->treeWidget->takeTopLevelItem(index);
|
|
|
|
ui->treeWidget->insertTopLevelItem(index - 1, currentItem);
|
|
|
|
ui->treeWidget->setCurrentItem(currentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::moveDown()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* currentItem = ui->treeWidget->currentItem();
|
|
|
|
int index = ui->treeWidget->indexOfTopLevelItem(currentItem);
|
|
|
|
|
|
|
|
if (!currentItem || !ui->treeWidget->itemBelow(currentItem)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->treeWidget->takeTopLevelItem(index);
|
|
|
|
ui->treeWidget->insertTopLevelItem(index + 1, currentItem);
|
|
|
|
ui->treeWidget->setCurrentItem(currentItem);
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
void SearchEnginesDialog::reloadEngines()
|
|
|
|
{
|
|
|
|
ui->treeWidget->clear();
|
2014-02-04 00:25:09 +01:00
|
|
|
const SearchEngine defaultEngine = mApp->searchEnginesManager()->defaultEngine();
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const SearchEngine &en, m_manager->allEngines()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem();
|
2012-09-27 11:59:09 +02:00
|
|
|
setEngine(item, en);
|
2014-02-04 00:25:09 +01:00
|
|
|
changeItemToDefault(item, en == defaultEngine);
|
2011-10-21 23:26:34 +02:00
|
|
|
item->setIcon(0, en.icon);
|
|
|
|
item->setText(1, en.shortcut);
|
|
|
|
|
|
|
|
ui->treeWidget->addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 14:27:18 +01:00
|
|
|
void SearchEnginesDialog::showEvent(QShowEvent *e)
|
|
|
|
{
|
|
|
|
QDialog::showEvent(e);
|
|
|
|
resizeViewHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
|
|
|
QDialog::resizeEvent(e);
|
|
|
|
resizeViewHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::resizeViewHeader()
|
|
|
|
{
|
|
|
|
const int headerWidth = ui->treeWidget->header()->width();
|
|
|
|
ui->treeWidget->header()->resizeSection(0, headerWidth - headerWidth / 4);
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
void SearchEnginesDialog::accept()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (ui->treeWidget->topLevelItemCount() < 1) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
QVector<SearchEngine> allEngines;
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++) {
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->topLevelItem(i);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item) {
|
2011-10-21 23:26:34 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
SearchEngine engine = getEngine(item);
|
2011-10-21 23:26:34 +02:00
|
|
|
allEngines.append(engine);
|
2012-09-27 11:59:09 +02:00
|
|
|
|
2012-09-28 18:44:45 +02:00
|
|
|
if (isDefaultEngine(item)) {
|
2012-09-27 11:59:09 +02:00
|
|
|
m_manager->setDefaultEngine(engine);
|
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_manager->setAllEngines(allEngines);
|
|
|
|
|
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchEnginesDialog::~SearchEnginesDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|