File indexing completed on 2024-04-28 08:50:25

0001 /* This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2004 Arend van Beelen jr. <arend@auton.nl>
0003     SPDX-FileCopyrightText: 2009 Fredy Yanardi <fyanardi@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef SEARCHBAR_PLUGIN
0009 #define SEARCHBAR_PLUGIN
0010 
0011 #include <KHistoryComboBox>
0012 #include <KUriFilter>
0013 
0014 #include "asyncselectorinterface.h"
0015 
0016 #if QT_VERSION_MAJOR < 6
0017 #include <KParts/SelectorInterface>
0018 #endif
0019 
0020 #include <konq_kpart_plugin.h>
0021 
0022 #include <QStringList>
0023 #include <QItemDelegate>
0024 #include <QPixmap>
0025 
0026 
0027 namespace KParts
0028 {
0029 class Part;
0030 class ReadOnlyPart;
0031 }
0032 
0033 class WebShortcutWidget;
0034 
0035 class QAction;
0036 class QMenu;
0037 class QTimer;
0038 class QWidgetAction;
0039 
0040 /**
0041  * Combo box which catches mouse clicks on the pixmap.
0042  */
0043 class SearchBarCombo : public KHistoryComboBox
0044 {
0045     Q_OBJECT
0046 
0047 public:
0048     /**
0049      * Constructor.
0050      */
0051     SearchBarCombo(QWidget *parent);
0052 
0053     /**
0054      * Destructor.
0055      */
0056     ~SearchBarCombo() override;
0057 
0058     /**
0059      * Returns the icon currently displayed in the combo box.
0060      */
0061     const QPixmap &icon() const;
0062 
0063     /**
0064      * Sets the icon displayed in the combo box.
0065      */
0066     void setIcon(const QPixmap &icon);
0067 
0068     /**
0069      * Finds a history item by its text.
0070      * @return The item number, or -1 if the item is not found.
0071      */
0072     int findHistoryItem(const QString &text);
0073 
0074     /**
0075      * Sets whether the plugin is active. It can be inactive
0076      * in case the current Konqueror part isn't a KHTML part.
0077      */
0078     void setPluginActive(bool pluginActive);
0079 
0080     /**
0081      * Set the suggestion items after to be displayed by this ComboBox
0082      * This method will automatically pop up the ComboBox's list.
0083      */
0084     void setSuggestionItems(const QStringList &suggestions);
0085 
0086     /**
0087      * Clear all previously set suggestion items for this ComboBox
0088      */
0089     void clearSuggestions();
0090 
0091 Q_SIGNALS:
0092     /**
0093      * Emitted when the icon was clicked.
0094      */
0095     void iconClicked();
0096 
0097 protected:
0098     /**
0099      * Captures mouse clicks and emits iconClicked() if the icon
0100      * was clicked.
0101      */
0102     void mousePressEvent(QMouseEvent *e) override;
0103 
0104     /**
0105      * Captures context menu requests and ignores any over the icon.
0106      */
0107     void contextMenuEvent(QContextMenuEvent *e) override;
0108 
0109 private:
0110     /**
0111      * See whether a mouse click is over the search engine icon.
0112      *
0113      * @param x X coordinate of mouse event
0114      * @return @c true if the click was over the icon
0115      */
0116     bool overIcon(int x);
0117 
0118 private Q_SLOTS:
0119     void historyCleared();
0120 
0121 private:
0122     QPixmap m_icon;
0123     QStringList m_suggestions;
0124 };
0125 
0126 /**
0127  * Plugin that provides a search bar for Konqueror. This search bar is located
0128  * next to the location bar and will show a small icon indicating the search
0129  * provider it will use.
0130  *
0131  * @author Arend van Beelen jr. <arend@auton.nl>
0132  * @version $Id$
0133  */
0134 class SearchBarPlugin : public KonqParts::Plugin
0135 {
0136     Q_OBJECT
0137 
0138 public:
0139     /** Possible search modes */
0140     enum SearchModes { FindInThisPage = 0, UseSearchProvider };
0141 
0142     SearchBarPlugin(QObject *parent,
0143                     const QVariantList &);
0144     ~SearchBarPlugin() override;
0145 
0146 protected:
0147     bool eventFilter(QObject *o, QEvent *e) override;
0148 
0149 private Q_SLOTS:
0150     /**
0151      * Starts a search by putting the query URL from the selected
0152      * search provider in the locationbar and calling goURL()
0153      */
0154     void startSearch(const QString &search);
0155 
0156     /**
0157      * Sets the icon to indicate which search engine is used.
0158      */
0159     void setIcon();
0160 
0161     /**
0162      * Opens the selection menu.
0163      */
0164     void showSelectionMenu();
0165 
0166     void useFindInThisPage();
0167     void menuActionTriggered(QAction *);
0168     void selectSearchEngines();
0169     void configurationChanged();
0170     void reloadConfiguration();
0171 
0172     /**
0173      * Show or hide the combo box
0174      */
0175     void updateComboVisibility();
0176 
0177     void focusSearchbar();
0178 
0179     void searchTextChanged(const QString &text);
0180 
0181     void addSearchSuggestion(const QStringList &suggestion);
0182 
0183     void HTMLLoadingStarted();
0184 
0185     void HTMLDocLoaded();
0186 
0187     void insertOpenSearchEntries(const QList<AsyncSelectorInterface::Element> &elements);
0188 
0189     void openSearchEngineAdded(const QString &name, const QString &searchUrl, const QString &fileName);
0190 
0191     void webShortcutSet(const QString &name, const QString &webShortcut, const QString &fileName);
0192 
0193 private:
0194     bool enableFindInPage() const;
0195     void nextSearchEntry();
0196     void previousSearchEntry();
0197 
0198     QPointer<KParts::ReadOnlyPart> m_part;
0199     SearchBarCombo *m_searchCombo;
0200     QWidgetAction *m_searchComboAction;
0201     QList<QAction *> m_addSearchActions;
0202     QMenu *m_popupMenu;
0203     WebShortcutWidget *m_addWSWidget;
0204     QPixmap m_searchIcon;
0205     SearchModes m_searchMode;
0206     QString m_providerName;
0207     bool m_urlEnterLock;
0208     QString m_lastSearch;
0209     QString m_currentEngine;
0210     QStringList m_searchEngines;
0211     QMap<QString, KUriFilterSearchProvider> m_searchProviders;
0212     QChar m_delimiter;
0213     QMap<QString, QString> m_openSearchDescs;
0214     bool m_reloadConfiguration;
0215     QString m_searchProvidersDir;
0216 };
0217 
0218 /**
0219  * An item delegate for combo box completion items, to give some fancy stuff
0220  * to the completion items
0221  */
0222 class SearchBarItemDelegate : public QItemDelegate
0223 {
0224 public:
0225     SearchBarItemDelegate(QObject *parent = nullptr);
0226     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0227 };
0228 
0229 #endif // SEARCHBAR_PLUGIN