1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-23 02:32:10 +02:00
falkonOfficial/src/navigation/websearchbar.cpp

124 lines
4.2 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2011-10-17 09:57:07 +02:00
* Copyright (C) 2010-2011 David Rosca
2011-03-03 18:29:20 +01: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-03-02 16:57:41 +01:00
#include "websearchbar.h"
#include "qupzilla.h"
#include "webview.h"
#include "clickablelabel.h"
#include "buttonwithmenu.h"
2011-03-02 16:57:41 +01:00
2011-03-17 17:03:04 +01:00
WebSearchBar::WebSearchBar(QupZilla* mainClass, QWidget* parent)
2011-03-02 16:57:41 +01:00
:LineEdit(parent)
,p_QupZilla(mainClass)
{
setObjectName("websearchbar");
2011-03-02 16:57:41 +01:00
m_buttonSearch = new ClickableLabel(this);
m_buttonSearch->setObjectName("websearchbar-searchbutton");
2011-03-02 16:57:41 +01:00
m_buttonSearch->setCursor(QCursor(Qt::PointingHandCursor));
2011-03-02 17:54:22 +01:00
m_buttonSearch->setFocusPolicy(Qt::ClickFocus);
2011-03-02 16:57:41 +01:00
m_boxSearchType = new ButtonWithMenu(this);
m_boxSearchType->setObjectName("websearchbar-searchprovider-comobobox");
2011-03-02 16:57:41 +01:00
addWidget(m_buttonSearch, LineEdit::RightSide);
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)));
2011-03-02 16:57:41 +01:00
setWidgetSpacing(0);
setupSearchTypes();
#ifdef Q_WS_WIN
setMaximumWidth(350);
#endif
2011-03-02 16:57:41 +01:00
}
void WebSearchBar::setupSearchTypes()
{
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);
2011-03-02 16:57:41 +01:00
}
void WebSearchBar::searchChanged(const ButtonWithMenu::Item &item)
2011-03-02 16:57:41 +01:00
{
setPlaceholderText(item.text);
2011-03-02 16:57:41 +01:00
}
void WebSearchBar::search()
{
// if (text().isEmpty())
// return;
ButtonWithMenu::Item* item = m_boxSearchType->activeItem();
2011-03-02 16:57:41 +01:00
QUrl searchUrl;
if (item->text == "Google")
2011-03-02 16:57:41 +01:00
searchUrl = QUrl("http://www.google.com/search?client=qupzilla&q="+text());
if (item->text == "Seznam")
2011-03-02 16:57:41 +01:00
searchUrl = QUrl("http://search.seznam.cz/?q="+text());
if (item->text == "Wikipedia (cs)")
2011-03-02 16:57:41 +01:00
searchUrl = QUrl("http://cs.wikipedia.org/w/index.php?search="+text());
if (item->text == "Wikipedia (en)")
2011-03-02 16:57:41 +01:00
searchUrl = QUrl("http://en.wikipedia.org/w/index.php?search="+text());
if (item->text == "CSFD")
searchUrl = QUrl("http://www.csfd.cz/hledat/?q="+text());
if (item->text == "Youtube")
2011-03-02 16:57:41 +01:00
searchUrl = QUrl("http://www.youtube.com/results?search_query="+text());
p_QupZilla->weView()->load(searchUrl);
p_QupZilla->weView()->setFocus();
}
2011-03-17 17:03:04 +01:00
void WebSearchBar::focusOutEvent(QFocusEvent* e)
2011-03-02 16:57:41 +01:00
{
if (text().isEmpty()) {
QString search = m_boxSearchType->activeItem()->text;
2011-03-02 16:57:41 +01:00
//clear();
setPlaceholderText(search);
}
QLineEdit::focusOutEvent(e);
}
2011-03-17 17:03:04 +01:00
void WebSearchBar::focusInEvent(QFocusEvent* e)
2011-03-02 16:57:41 +01:00
{
QString search = m_boxSearchType->toolTip();
if (text() == search) {
clear();
} else {
selectAll();
2011-03-02 16:57:41 +01:00
}
QLineEdit::focusInEvent(e);
}
void WebSearchBar::dropEvent(QDropEvent* event)
{
if (event->mimeData()->hasText()) {
QString dropText = event->mimeData()->text();
setText(dropText);
search();
QLineEdit::focusOutEvent(new QFocusEvent(QFocusEvent::FocusOut));
return;
}
QLineEdit::dropEvent(event);
}