From 9df1b8ca1eaeea4c91bfbddfb10ba7aa7b690c4c Mon Sep 17 00:00:00 2001 From: nowrep Date: Tue, 28 Feb 2012 18:57:05 +0100 Subject: [PATCH] Don't reload search results when saving search engines. --- src/lib/navigation/websearchbar.cpp | 10 ++++++---- src/lib/navigation/websearchbar.h | 2 +- src/lib/tools/buttonwithmenu.cpp | 25 ++++++++++++------------- src/lib/tools/buttonwithmenu.h | 17 +++++++++++++---- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/lib/navigation/websearchbar.cpp b/src/lib/navigation/websearchbar.cpp index 850088c67..03d8c7e3f 100644 --- a/src/lib/navigation/websearchbar.cpp +++ b/src/lib/navigation/websearchbar.cpp @@ -120,14 +120,16 @@ void WebSearchBar::setupEngines() m_boxSearchType->addItem(item); if (item.text == activeEngine) { - m_boxSearchType->setCurrentItem(item); + m_boxSearchType->setCurrentItem(item, false); } } + searchChanged(m_boxSearchType->currentItem(), false); + connect(m_searchManager, SIGNAL(enginesChanged()), this, SLOT(setupEngines())); } -void WebSearchBar::searchChanged(const ButtonWithMenu::Item &item) +void WebSearchBar::searchChanged(const ButtonWithMenu::Item &item, bool reload) { setPlaceholderText(item.text); m_completerModel->setStringList(QStringList()); @@ -139,7 +141,7 @@ void WebSearchBar::searchChanged(const ButtonWithMenu::Item &item) m_searchManager->setActiveEngine(m_activeEngine); - if (!text().isEmpty()) { + if (reload && !text().isEmpty()) { search(); } } @@ -261,7 +263,7 @@ void WebSearchBar::contextMenuEvent(QContextMenuEvent* event) void WebSearchBar::focusOutEvent(QFocusEvent* e) { if (text().isEmpty()) { - QString search = m_boxSearchType->currentItem()->text; + QString search = m_boxSearchType->currentItem().text; setPlaceholderText(search); } QLineEdit::focusOutEvent(e); diff --git a/src/lib/navigation/websearchbar.h b/src/lib/navigation/websearchbar.h index fc45521f4..6adcaa539 100644 --- a/src/lib/navigation/websearchbar.h +++ b/src/lib/navigation/websearchbar.h @@ -47,7 +47,7 @@ public: explicit WebSearchBar(QupZilla* mainClass, QWidget* parent = 0); private slots: - void searchChanged(const ButtonWithMenu::Item &item); + void searchChanged(const ButtonWithMenu::Item &item, bool reload = true); void setupEngines(); void search(); diff --git a/src/lib/tools/buttonwithmenu.cpp b/src/lib/tools/buttonwithmenu.cpp index 31cf95ada..838f7e5d6 100644 --- a/src/lib/tools/buttonwithmenu.cpp +++ b/src/lib/tools/buttonwithmenu.cpp @@ -20,7 +20,6 @@ ButtonWithMenu::ButtonWithMenu(QWidget* parent) : ToolButton(parent) , m_menu(new QMenu(this)) - , m_currentItem(0) { setPopupMode(QToolButton::InstantPopup); setCursor(Qt::ArrowCursor); @@ -41,15 +40,13 @@ void ButtonWithMenu::clearItems() { m_menu->clear(); m_items.clear(); - - m_currentItem = 0; } void ButtonWithMenu::addItem(const Item &item) { m_items.append(item); - if (!m_currentItem) { + if (m_items.count() == 1) { setCurrentItem(item); } @@ -77,29 +74,31 @@ void ButtonWithMenu::removeItem(const Item &item) return; } - if (*m_currentItem == item) { + if (m_currentItem == item) { setCurrentItem(m_items.first()); } } -void ButtonWithMenu::setCurrentItem(const Item &item) +void ButtonWithMenu::setCurrentItem(const Item &item, bool emitSignal) { int index = m_items.indexOf(item); - if (index < 0) { + if (index < 0 || m_currentItem == item) { return; } - m_currentItem = const_cast(&m_items.at(index)); + m_currentItem = item; - setIcon(m_currentItem->icon); - setToolTip(m_currentItem->text); + setIcon(m_currentItem.icon); + setToolTip(m_currentItem.text); - emit activeItemChanged(*m_currentItem); + if (emitSignal) { + emit activeItemChanged(m_currentItem); + } } void ButtonWithMenu::wheelEvent(QWheelEvent* event) { - int currItemIndex = m_items.indexOf(*m_currentItem); + int currItemIndex = m_items.indexOf(m_currentItem); int itemsCount = m_items.count(); if (itemsCount == 0) { @@ -120,7 +119,7 @@ void ButtonWithMenu::wheelEvent(QWheelEvent* event) event->accept(); } -ButtonWithMenu::Item* ButtonWithMenu::currentItem() +ButtonWithMenu::Item ButtonWithMenu::currentItem() { return m_currentItem; } diff --git a/src/lib/tools/buttonwithmenu.h b/src/lib/tools/buttonwithmenu.h index bdc098fa3..f3cd632ef 100644 --- a/src/lib/tools/buttonwithmenu.h +++ b/src/lib/tools/buttonwithmenu.h @@ -42,6 +42,16 @@ public: bool operator==(const Item &a) { return (a.text == text) && (a.icon.pixmap(16, 16).toImage() == icon.pixmap(16, 16).toImage()); } + + bool isEmpty() { + return (text.isEmpty() && icon.isNull()); + } + + void clear() { + text = QString(); + icon = QIcon(); + userData = QVariant(); + } }; explicit ButtonWithMenu(QWidget* parent = 0); @@ -50,9 +60,9 @@ public: void addItem(const Item &item); void addItems(const QList &items); void removeItem(const Item &item); - void setCurrentItem(const Item &item); + void setCurrentItem(const Item &item, bool emitSignal = true); - Item* currentItem(); + Item currentItem(); QList allItems() { return m_items; } QMenu* menu() const; @@ -73,8 +83,7 @@ private: QMenu* m_menu; QList m_items; - Item* m_currentItem; - + Item m_currentItem; }; Q_DECLARE_METATYPE(ButtonWithMenu::Item)