1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Add option to set a default search engine and use that for searching in address bar.

This commit is contained in:
Franz Fellner 2012-09-27 11:59:09 +02:00
parent 40f97c73ab
commit 04e8df6e48
13 changed files with 234 additions and 102 deletions

View File

@ -89,6 +89,7 @@ LocationBar::LocationBar(QupZilla* mainClass)
connect(m_bookmarkIcon, SIGNAL(clicked(QPoint)), this, SLOT(bookmarkIconClicked())); connect(m_bookmarkIcon, SIGNAL(clicked(QPoint)), this, SLOT(bookmarkIconClicked()));
connect(down, SIGNAL(clicked(QPoint)), this, SLOT(showMostVisited())); connect(down, SIGNAL(clicked(QPoint)), this, SLOT(showMostVisited()));
connect(mApp->searchEnginesManager(), SIGNAL(activeEngineChanged()), this, SLOT(updatePlaceHolderText())); connect(mApp->searchEnginesManager(), SIGNAL(activeEngineChanged()), this, SLOT(updatePlaceHolderText()));
connect(mApp->searchEnginesManager(), SIGNAL(defaultEngineChanged()), this, SLOT(updatePlaceHolderText()));
connect(mApp, SIGNAL(message(Qz::AppMessageType, bool)), SLOT(onMessage(Qz::AppMessageType, bool))); connect(mApp, SIGNAL(message(Qz::AppMessageType, bool)), SLOT(onMessage(Qz::AppMessageType, bool)));
loadSettings(); loadSettings();
@ -114,7 +115,10 @@ void LocationBar::setText(const QString &text)
void LocationBar::updatePlaceHolderText() void LocationBar::updatePlaceHolderText()
{ {
setPlaceholderText(tr("Enter URL address or search on %1").arg(mApp->searchEnginesManager()->activeEngine().name)); QString engineName = qzSettings->searchWithDefaultEngine ?
mApp->searchEnginesManager()->defaultEngine().name :
mApp->searchEnginesManager()->activeEngine().name;
setPlaceholderText(tr("Enter URL address or search on %1").arg(engineName));
} }
void LocationBar::showCompletion(const QString &newText) void LocationBar::showCompletion(const QString &newText)

View File

@ -20,6 +20,41 @@
#include "editsearchengine.h" #include "editsearchengine.h"
#include "searchenginesmanager.h" #include "searchenginesmanager.h"
#include "mainapplication.h" #include "mainapplication.h"
#include <QMessageBox>
namespace
{
const int EngineRole = Qt::UserRole;
const int DefaultRole = Qt::UserRole + 1;
bool isDefault(QTreeWidgetItem* item)
{
return item->data(0, DefaultRole).toBool();
}
SearchEngine getEngine(QTreeWidgetItem* item)
{
return item->data(0, EngineRole).value<SearchEngine>();
}
void setEngine(QTreeWidgetItem* item, SearchEngine engine)
{
QVariant v;
v.setValue<SearchEngine>(engine);
item->setData(0, EngineRole, v);
}
void changeItemToDefault(QTreeWidgetItem* item, bool isDefault)
{
QString txt = item->data(0, EngineRole).value<SearchEngine>().name;
if (isDefault) {
txt.append(QString(" (%1)").arg(QObject::tr("Default")));
}
item->setText(0, txt);
item->setData(0, DefaultRole, isDefault);
}
}
SearchEnginesDialog::SearchEnginesDialog(QWidget* parent) SearchEnginesDialog::SearchEnginesDialog(QWidget* parent)
: QDialog(parent) : QDialog(parent)
@ -32,6 +67,7 @@ SearchEnginesDialog::SearchEnginesDialog(QWidget* parent)
connect(ui->add, SIGNAL(clicked()), this, SLOT(addEngine())); connect(ui->add, SIGNAL(clicked()), this, SLOT(addEngine()));
connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeEngine())); connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeEngine()));
connect(ui->edit, SIGNAL(clicked()), this, SLOT(editEngine())); connect(ui->edit, SIGNAL(clicked()), this, SLOT(editEngine()));
connect(ui->setAsDefault, SIGNAL(clicked()), this, SLOT(setDefaultEngine()));
connect(ui->defaults, SIGNAL(clicked()), this, SLOT(defaults())); connect(ui->defaults, SIGNAL(clicked()), this, SLOT(defaults()));
connect(ui->moveUp, SIGNAL(clicked()), this, SLOT(moveUp())); connect(ui->moveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
connect(ui->moveDown, SIGNAL(clicked()), this, SLOT(moveDown())); connect(ui->moveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
@ -62,11 +98,9 @@ void SearchEnginesDialog::addEngine()
} }
QTreeWidgetItem* item = new QTreeWidgetItem(); QTreeWidgetItem* item = new QTreeWidgetItem();
QVariant v; setEngine(item, engine);
v.setValue<SearchEngine>(engine);
item->setData(0, Qt::UserRole, v);
item->setText(0, engine.name); changeItemToDefault(item, false);
item->setIcon(0, engine.icon); item->setIcon(0, engine.icon);
item->setText(1, engine.shortcut); item->setText(1, engine.shortcut);
@ -80,8 +114,16 @@ void SearchEnginesDialog::removeEngine()
return; return;
} }
if (isDefault(item)) {
SearchEngine en = getEngine(item);
QMessageBox::warning(this, tr("Remove Engine"),
tr("You can't remove the default search engine.<br>"
"Set a different engine as Default before removing %1.").arg(en.name));
}
else {
delete item; delete item;
} }
}
void SearchEnginesDialog::editEngine() void SearchEnginesDialog::editEngine()
{ {
@ -90,7 +132,7 @@ void SearchEnginesDialog::editEngine()
return; return;
} }
SearchEngine engine = item->data(0, Qt::UserRole).value<SearchEngine>(); SearchEngine engine = getEngine(item);
EditSearchEngine dialog(tr("Edit Search Engine"), this); EditSearchEngine dialog(tr("Edit Search Engine"), this);
@ -112,15 +154,34 @@ void SearchEnginesDialog::editEngine()
return; return;
} }
QVariant v; setEngine(item, engine);
v.setValue<SearchEngine>(engine);
item->setData(0, Qt::UserRole, v);
item->setText(0, engine.name); changeItemToDefault(item, isDefault(item));
item->setIcon(0, engine.icon); item->setIcon(0, engine.icon);
item->setText(1, engine.shortcut); item->setText(1, engine.shortcut);
} }
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);
if (isDefault(i)) {
if (i == item) {
return;
}
changeItemToDefault(i, false);
break;
}
}
changeItemToDefault(item, true);
}
void SearchEnginesDialog::defaults() void SearchEnginesDialog::defaults()
{ {
m_manager->restoreDefaults(); m_manager->restoreDefaults();
@ -158,15 +219,14 @@ void SearchEnginesDialog::moveDown()
void SearchEnginesDialog::reloadEngines() void SearchEnginesDialog::reloadEngines()
{ {
ui->treeWidget->clear(); ui->treeWidget->clear();
const QString defaultEngineName = mApp->searchEnginesManager()->defaultEngine().name;
foreach(const SearchEngine & en, m_manager->allEngines()) { foreach(const SearchEngine & en, m_manager->allEngines()) {
QTreeWidgetItem* item = new QTreeWidgetItem(); QTreeWidgetItem* item = new QTreeWidgetItem();
setEngine(item, en);
changeItemToDefault(item, en.name == defaultEngineName);
item->setIcon(0, en.icon); item->setIcon(0, en.icon);
item->setText(0, en.name);
item->setText(1, en.shortcut); item->setText(1, en.shortcut);
QVariant v;
v.setValue<SearchEngine>(en);
item->setData(0, Qt::UserRole, v);
ui->treeWidget->addTopLevelItem(item); ui->treeWidget->addTopLevelItem(item);
} }
@ -186,8 +246,12 @@ void SearchEnginesDialog::accept()
continue; continue;
} }
SearchEngine engine = item->data(0, Qt::UserRole).value<SearchEngine>(); SearchEngine engine = getEngine(item);
allEngines.append(engine); allEngines.append(engine);
if (isDefault(item)) {
m_manager->setDefaultEngine(engine);
}
} }
m_manager->setAllEngines(allEngines); m_manager->setAllEngines(allEngines);

View File

@ -22,6 +22,8 @@
#include <QDialog> #include <QDialog>
class QTreeWidgetItem;
namespace Ui namespace Ui
{ {
class SearchEnginesDialog; class SearchEnginesDialog;
@ -43,6 +45,7 @@ private slots:
void addEngine(); void addEngine();
void removeEngine(); void removeEngine();
void editEngine(); void editEngine();
void setDefaultEngine();
void moveUp(); void moveUp();
void moveDown(); void moveDown();

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>460</width> <width>465</width>
<height>299</height> <height>299</height>
</rect> </rect>
</property> </property>
@ -37,6 +37,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="setAsDefault">
<property name="text">
<string>Set as Default</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">

View File

@ -25,6 +25,7 @@
#include "opensearchengine.h" #include "opensearchengine.h"
#include "databasewriter.h" #include "databasewriter.h"
#include "settings.h" #include "settings.h"
#include "qzsettings.h"
#include "webview.h" #include "webview.h"
#include <QNetworkReply> #include <QNetworkReply>
@ -51,6 +52,7 @@ SearchEnginesManager::SearchEnginesManager()
Settings settings; Settings settings;
settings.beginGroup("SearchEngines"); settings.beginGroup("SearchEngines");
m_startingEngineName = settings.value("activeEngine", "Google").toString(); m_startingEngineName = settings.value("activeEngine", "Google").toString();
m_defaultEngineName = settings.value("DefaultEngine", "Google").toString();
settings.endGroup(); settings.endGroup();
connect(this, SIGNAL(enginesChanged()), this, SLOT(scheduleSave())); connect(this, SIGNAL(enginesChanged()), this, SLOT(scheduleSave()));
@ -71,11 +73,19 @@ void SearchEnginesManager::loadSettings()
en.suggestionsUrl = query.value(4).toString(); en.suggestionsUrl = query.value(4).toString();
m_allEngines.append(en); m_allEngines.append(en);
if (en.name == m_defaultEngineName) {
m_defaultEngine = en;
}
} }
if (m_allEngines.isEmpty()) { if (m_allEngines.isEmpty()) {
restoreDefaults(); restoreDefaults();
} }
if (m_defaultEngine.name.isEmpty()) {
m_defaultEngine = m_allEngines[0];
}
} }
SearchEngine SearchEnginesManager::engineForShortcut(const QString &shortcut) SearchEngine SearchEnginesManager::engineForShortcut(const QString &shortcut)
@ -106,7 +116,8 @@ QUrl SearchEnginesManager::searchUrl(const Engine &engine, const QString &string
QUrl SearchEnginesManager::searchUrl(const QString &string) QUrl SearchEnginesManager::searchUrl(const QString &string)
{ {
return searchUrl(m_activeEngine, string); const Engine &en = qzSettings->searchWithDefaultEngine ? m_defaultEngine : m_activeEngine;
return searchUrl(en, string);
} }
void SearchEnginesManager::restoreDefaults() void SearchEnginesManager::restoreDefaults()
@ -143,6 +154,8 @@ void SearchEnginesManager::restoreDefaults()
addEngine(yt); addEngine(yt);
addEngine(duck); addEngine(duck);
m_defaultEngine = google;
emit enginesChanged(); emit enginesChanged();
} }
@ -344,6 +357,18 @@ void SearchEnginesManager::setActiveEngine(const Engine &engine)
emit activeEngineChanged(); emit activeEngineChanged();
} }
void SearchEnginesManager::setDefaultEngine(const SearchEnginesManager::Engine &engine)
{
ENSURE_LOADED;
if (!m_allEngines.contains(engine)) {
return;
}
m_defaultEngine = engine;
emit defaultEngineChanged();
}
void SearchEnginesManager::removeEngine(const Engine &engine) void SearchEnginesManager::removeEngine(const Engine &engine)
{ {
ENSURE_LOADED; ENSURE_LOADED;
@ -382,6 +407,7 @@ void SearchEnginesManager::saveSettings()
Settings settings; Settings settings;
settings.beginGroup("SearchEngines"); settings.beginGroup("SearchEngines");
settings.setValue("activeEngine", m_activeEngine.name); settings.setValue("activeEngine", m_activeEngine.name);
settings.setValue("DefaultEngine", m_defaultEngine.name);
settings.endGroup(); settings.endGroup();
if (!m_saveScheduled) { if (!m_saveScheduled) {

View File

@ -66,7 +66,11 @@ public:
void removeEngine(const Engine &engine); void removeEngine(const Engine &engine);
void setActiveEngine(const Engine &engine); void setActiveEngine(const Engine &engine);
Engine activeEngine() { return m_activeEngine; } Engine activeEngine() const { return m_activeEngine; }
void setDefaultEngine(const Engine &engine);
Engine defaultEngine() const { return m_defaultEngine; }
void editEngine(const Engine &before, const Engine &after); void editEngine(const Engine &before, const Engine &after);
Engine engineForShortcut(const QString &shortcut); Engine engineForShortcut(const QString &shortcut);
@ -84,6 +88,7 @@ public:
signals: signals:
void enginesChanged(); void enginesChanged();
void activeEngineChanged(); void activeEngineChanged();
void defaultEngineChanged();
public slots: public slots:
@ -102,8 +107,10 @@ private:
bool m_saveScheduled; bool m_saveScheduled;
QString m_startingEngineName; QString m_startingEngineName;
QString m_defaultEngineName;
QList<Engine> m_allEngines; QList<Engine> m_allEngines;
Engine m_activeEngine; Engine m_activeEngine;
Engine m_defaultEngine;
}; };

View File

@ -37,6 +37,7 @@ void QzSettings::loadSettings()
settings.beginGroup("SearchEngines"); settings.beginGroup("SearchEngines");
showSearchSuggestions = settings.value("showSuggestions", true).toBool(); showSearchSuggestions = settings.value("showSuggestions", true).toBool();
searchOnEngineChange = settings.value("SearchOnEngineChange", true).toBool(); searchOnEngineChange = settings.value("SearchOnEngineChange", true).toBool();
searchWithDefaultEngine = settings.value("SearchWithDefaultEngine", false).toBool();
settings.endGroup(); settings.endGroup();
settings.beginGroup("Web-Browser-Settings"); settings.beginGroup("Web-Browser-Settings");

View File

@ -41,6 +41,7 @@ public:
// SearchEngines // SearchEngines
bool showSearchSuggestions; bool showSearchSuggestions;
bool searchOnEngineChange; bool searchOnEngineChange;
bool searchWithDefaultEngine;
// Web-Browser-Settings // Web-Browser-Settings
int defaultZoom; int defaultZoom;

View File

@ -77,7 +77,8 @@ void SourceViewerSearch::searchWholeWords()
{ {
if (ui->wholeWords->isChecked()) { if (ui->wholeWords->isChecked()) {
m_findFlags |= QTextDocument::FindWholeWords; m_findFlags |= QTextDocument::FindWholeWords;
} else { }
else {
m_findFlags &= (~QTextDocument::FindWholeWords); m_findFlags &= (~QTextDocument::FindWholeWords);
} }
} }

View File

@ -210,6 +210,10 @@ Preferences::Preferences(QupZilla* mainClass, QWidget* parent)
connect(ui->setProgressBarColorToHighlightButton, SIGNAL(clicked()), SLOT(setProgressBarColorIcon())); connect(ui->setProgressBarColorToHighlightButton, SIGNAL(clicked()), SLOT(setProgressBarColorIcon()));
settings.endGroup(); settings.endGroup();
settings.beginGroup("SearchEngines");
ui->searchWithDefaultEngine->setChecked(settings.value("SearchWithDefaultEngine", false).toBool());
settings.endGroup();
//BROWSING //BROWSING
settings.beginGroup("Web-Browser-Settings"); settings.beginGroup("Web-Browser-Settings");
ui->allowPlugins->setChecked(settings.value("allowFlash", true).toBool()); ui->allowPlugins->setChecked(settings.value("allowFlash", true).toBool());
@ -897,6 +901,10 @@ void Preferences::saveSettings()
settings.setValue("CustomProgressColor", ui->customColorToolButton->property("ProgressColor").value<QColor>()); settings.setValue("CustomProgressColor", ui->customColorToolButton->property("ProgressColor").value<QColor>());
settings.endGroup(); settings.endGroup();
settings.beginGroup("SearchEngines");
settings.setValue("SearchWithDefaultEngine", ui->searchWithDefaultEngine->isChecked());
settings.endGroup();
//Languages //Languages
settings.beginGroup("Language"); settings.beginGroup("Language");
settings.setValue("language", ui->languages->itemData(ui->languages->currentIndex()).toString()); settings.setValue("language", ui->languages->itemData(ui->languages->currentIndex()).toString());

View File

@ -888,6 +888,16 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="searchWithDefaultEngine">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If enabled the default engine will be taken for searching without search shortcut in the address bar instead of the engine currently selected in the web search bar.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Search with Default Engine</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer_7"> <spacer name="verticalSpacer_7">
<property name="orientation"> <property name="orientation">