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

Edited WebSearchBar to support changing search engines with mouse whell

This commit is contained in:
nowrep 2011-06-06 19:38:37 +02:00
parent f6e4788bd1
commit f370877bda
6 changed files with 194 additions and 51 deletions

View File

@ -121,7 +121,8 @@ SOURCES += main.cpp\
tools/buttonbox.cpp \
tools/widget.cpp \
3rdparty/squeezelabelv2.cpp \
3rdparty/squeezelabelv1.cpp
3rdparty/squeezelabelv1.cpp \
tools/buttonwithmenu.cpp
HEADERS += \
3rdparty/qtwin.h \
@ -203,7 +204,8 @@ HEADERS += \
tools/buttonbox.h \
tools/widget.h \
3rdparty/squeezelabelv2.h \
3rdparty/squeezelabelv1.h
3rdparty/squeezelabelv1.h \
tools/buttonwithmenu.h
FORMS += \
preferences/autofillmanager.ui \

View File

@ -19,6 +19,7 @@
#include "qupzilla.h"
#include "webview.h"
#include "clickablelabel.h"
#include "buttonwithmenu.h"
WebSearchBar::WebSearchBar(QupZilla* mainClass, QWidget* parent)
:LineEdit(parent)
@ -30,12 +31,9 @@ WebSearchBar::WebSearchBar(QupZilla* mainClass, QWidget* parent)
m_buttonSearch->setStyleSheet("QLabel{margin-bottom:2px;}");
m_buttonSearch->setFocusPolicy(Qt::ClickFocus);
m_boxSearchType = new QToolButton(this);
m_boxSearchType->setPopupMode(QToolButton::InstantPopup);
m_boxSearchType->setCursor(Qt::ArrowCursor);
m_boxSearchType = new ButtonWithMenu(this);
m_boxSearchType->setMaximumSize(35, 25);
m_boxSearchType->setMinimumSize(35, 25);
m_boxSearchType->setFocusPolicy(Qt::ClickFocus);
this->setMinimumHeight(25);
this->setMaximumHeight(25);
@ -44,70 +42,51 @@ WebSearchBar::WebSearchBar(QupZilla* mainClass, QWidget* parent)
addWidget(m_buttonSearch, LineEdit::RightSide);
setupSearchTypes();
connect(this, SIGNAL(returnPressed()), this, SLOT(search()));
connect(m_buttonSearch, SIGNAL(clicked(QPoint)), this, SLOT(search()));
connect(m_boxSearchType, SIGNAL(activeItemChanged(ButtonWithMenu::Item)), this, SLOT(searchChanged(ButtonWithMenu::Item)));
setStyleSheet("QLineEdit { background: transparent; border-image: url(:/icons/locationbar/lineedit.png) ;border-width:4;color:black;}");
setLeftMargin(33);
setWidgetSpacing(0);
setupSearchTypes();
}
void WebSearchBar::setupSearchTypes()
{
QMenu* menu = new QMenu(this);
menu->addAction(QIcon(":/icons/menu/google.png"),"Google", this, SLOT(searchChanged()))->setData("Google");
menu->addAction(QIcon(":/icons/menu/cz_seznam.png"),"Seznam", this, SLOT(searchChanged()))->setData("Seznam");
menu->addAction(QIcon(":/icons/menu/icon-wikipedia.png"),"Wikipedia (en)", this, SLOT(searchChanged()))->setData("Wikipedia (en)");
menu->addAction(QIcon(":/icons/menu/icon-wikipedia.png"),"Wikipedia (cs)", this, SLOT(searchChanged()))->setData("Wikipedia (cs)");
menu->addAction(QIcon(":/icons/menu/csfd.png"),"CSFD", this, SLOT(searchChanged()))->setData("CSFD");
menu->addAction(QIcon(":/icons/menu/youtube.png"),"Youtube", this, SLOT(searchChanged()))->setData("Youtube");
m_boxSearchType->setMenu(menu);
m_boxSearchType->setIcon(QIcon(":/icons/menu/google.png"));
m_boxSearchType->setToolTip("Google");
setPlaceholderText("Google");
QList<ButtonWithMenu::Item> items;
items.append(ButtonWithMenu::Item("Google", QIcon(":/icons/menu/google.png")));
items.append(ButtonWithMenu::Item("Seznam", QIcon(":/icons/menu/cz_seznam.png")));
items.append(ButtonWithMenu::Item("Wikipedia (en)", QIcon(":/icons/menu/icon-wikipedia.png")));
items.append(ButtonWithMenu::Item("Wikipedia (cs)", QIcon(":/icons/menu/icon-wikipedia.png")));
items.append(ButtonWithMenu::Item("CSFD", QIcon(":/icons/menu/csfd.png")));
items.append(ButtonWithMenu::Item("Youtube", QIcon(":/icons/menu/youtube.png")));
m_boxSearchType->addItems(items);
}
void WebSearchBar::searchChanged()
void WebSearchBar::searchChanged(const ButtonWithMenu::Item &item)
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
if (action->data().toString() == "Google")
m_boxSearchType->setIcon(QIcon(":/icons/menu/google.png"));
else if (action->data().toString() == "Seznam")
m_boxSearchType->setIcon(QIcon(":/icons/menu/cz_seznam.png"));
else if (action->data().toString().contains("Wikipedia"))
m_boxSearchType->setIcon(QIcon(":/icons/menu/icon-wikipedia.png"));
else if (action->data().toString() == "CSFD")
m_boxSearchType->setIcon(QIcon(":/icons/menu/csfd.png"));
else if (action->data().toString() == "Youtube")
m_boxSearchType->setIcon(QIcon(":/icons/menu/youtube.png"));
m_boxSearchType->setToolTip(action->data().toString());
setPlaceholderText(action->data().toString());
}
setPlaceholderText(item.text);
}
void WebSearchBar::search()
{
// if (text().isEmpty())
// return;
ButtonWithMenu::Item* item = m_boxSearchType->activeItem();
QUrl searchUrl;
if (m_boxSearchType->toolTip() == "Google")
if (item->text == "Google")
searchUrl = QUrl("http://www.google.com/search?client=qupzilla&q="+text());
if (m_boxSearchType->toolTip() == "Seznam")
if (item->text == "Seznam")
searchUrl = QUrl("http://search.seznam.cz/?q="+text());
if (m_boxSearchType->toolTip() == "Wikipedia (cs)")
if (item->text == "Wikipedia (cs)")
searchUrl = QUrl("http://cs.wikipedia.org/w/index.php?search="+text());
if (m_boxSearchType->toolTip() == "Wikipedia (en)")
if (item->text == "Wikipedia (en)")
searchUrl = QUrl("http://en.wikipedia.org/w/index.php?search="+text());
if (m_boxSearchType->toolTip() == "CSFD")
if (item->text == "CSFD")
searchUrl = QUrl("http://www.csfd.cz/hledat/?q="+text());
if (m_boxSearchType->toolTip() == "Youtube")
if (item->text == "Youtube")
searchUrl = QUrl("http://www.youtube.com/results?search_query="+text());
p_QupZilla->weView()->load(searchUrl);
@ -117,7 +96,7 @@ void WebSearchBar::search()
void WebSearchBar::focusOutEvent(QFocusEvent* e)
{
if (text().isEmpty()) {
QString search = m_boxSearchType->toolTip();
QString search = m_boxSearchType->activeItem()->text;
//clear();
setPlaceholderText(search);
}

View File

@ -23,23 +23,24 @@
#include <QToolButton>
#include <QMenu>
#include "lineedit.h"
#include "buttonwithmenu.h"
class QupZilla;
class LineEdit;
class ClickableLabel;
class WebSearchBar : public LineEdit
{
Q_OBJECT;
Q_OBJECT
public:
explicit WebSearchBar(QupZilla* mainClass, QWidget* parent = 0);
private slots:
void searchChanged();
void searchChanged(const ButtonWithMenu::Item &item);
void search();
private:
ClickableLabel* m_buttonSearch;
QToolButton* m_boxSearchType;
ButtonWithMenu* m_boxSearchType;
void setupSearchTypes();
void focusInEvent(QFocusEvent* e);

View File

@ -0,0 +1,101 @@
#include "buttonwithmenu.h"
ButtonWithMenu::ButtonWithMenu(QWidget* parent) :
QToolButton(parent)
, m_menu(new QMenu(this))
, m_currentItem(0)
{
setPopupMode(QToolButton::InstantPopup);
setCursor(Qt::ArrowCursor);
setFocusPolicy(Qt::ClickFocus);
setMenu(m_menu);
}
void ButtonWithMenu::setCurrentItem()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
setActiveItem(action->data().value<Item>());
}
}
void ButtonWithMenu::addItem(const Item &item)
{
m_items.append(item);
if (!m_currentItem)
setActiveItem(item);
QVariant variant;
variant.setValue<Item>(item);
m_menu->addAction(item.icon, item.text, this, SLOT(setCurrentItem()))->setData(variant);
emit itemAdded(item);
}
void ButtonWithMenu::addItems(const QList<Item> &items)
{
foreach (const Item &item, items) {
addItem(item);
}
}
void ButtonWithMenu::removeItem(const Item &item)
{
int index = m_items.indexOf(item);
if (index < 0)
return;
m_items.removeOne(item);
if (*m_currentItem == item)
setActiveItem(m_items.takeFirst());
generateMenu();
}
void ButtonWithMenu::setActiveItem(const Item &item)
{
int index = m_items.indexOf(item);
if (index < 0)
return;
m_currentItem = const_cast<Item*>(&m_items.at(index));
setIcon(m_currentItem->icon);
setToolTip(m_currentItem->text);
emit activeItemChanged(*m_currentItem);
}
void ButtonWithMenu::wheelEvent(QWheelEvent *event)
{
int currItemIndex = m_items.indexOf(*m_currentItem);
int itemsCount = m_items.count();
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15;
if (numSteps == 1) {
if (currItemIndex != 0)
setActiveItem(m_items.at(currItemIndex - 1));
} else {
if (currItemIndex < itemsCount - 1)
setActiveItem(m_items.at(currItemIndex + 1));
}
event->accept();
}
ButtonWithMenu::Item* ButtonWithMenu::activeItem()
{
return m_currentItem;
}
void ButtonWithMenu::generateMenu()
{
m_menu->clear();
addItems(m_items);
}
ButtonWithMenu::~ButtonWithMenu()
{
delete m_menu;
}

View File

@ -0,0 +1,62 @@
#ifndef BUTTONWITHMENU_H
#define BUTTONWITHMENU_H
#include <QToolButton>
#include <QAction>
#include <QMenu>
#include <QWheelEvent>
class ButtonWithMenu : public QToolButton
{
Q_OBJECT
public:
struct Item {
QString text;
QIcon icon;
QVariant userData;
Item(const QString &a = QString(), const QIcon &b = QIcon())
{
text = a;
icon = b;
}
bool operator==(const Item &a)
{
return (a.text == text) && (a.icon.pixmap(16,16).toImage() == icon.pixmap(16,16).toImage());
}
};
explicit ButtonWithMenu(QWidget* parent = 0);
~ButtonWithMenu();
void addItem(const Item &item);
void addItems(const QList<Item> &items);
void removeItem(const Item &item);
void setActiveItem(const Item &item);
Item* activeItem();
signals:
void activeItemChanged(const ButtonWithMenu::Item &item);
void itemAdded(const ButtonWithMenu::Item &item);
void itemRemoved(const ButtonWithMenu::Item &item);
public slots:
private slots:
void setCurrentItem();
private:
void wheelEvent(QWheelEvent *event);
void generateMenu();
QMenu* m_menu;
QList<Item> m_items;
Item* m_currentItem;
};
Q_DECLARE_METATYPE(ButtonWithMenu::Item)
#endif // BUTTONWITHMENU_H

View File

@ -213,7 +213,7 @@ void WebView::loadFinished(bool state)
if (animationLoading(tabIndex(), false)->movie())
animationLoading(tabIndex(), false)->movie()->stop();
if (m_progress>100) qDebug() << "bug"; //cannot be more than 100
m_isLoading = false;
if (m_lastUrl!=url())
@ -223,8 +223,6 @@ void WebView::loadFinished(bool state)
iconChanged();
m_lastUrl = url();
// if (!p_QupZilla->locationBar()->hasFocus()) Ok lets disable it, confusing with gaining focus
// setFocus();
//Fix the bug where sometimes icon is not available at the moment
if (icon().isNull())