/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "findtoolbar.h"
#include "textfindconstants.h"
#include "ifindfilter.h"
#include "../findplaceholder.h"
#include "../utils/stylehelper.h"
#include "../utils/flowlayout.h"

#include <QDebug>
#include <QSettings>

#include <QAbstractItemView>
#include <QClipboard>
#include <QCompleter>
#include <QKeyEvent>
#include <QLineEdit>
#include <QMenu>
#include <QPushButton>
#include <QToolButton>
#include <QPainter>
#include <QPixmapCache>
#include <QStringListModel>

Q_DECLARE_METATYPE(QStringList)
Q_DECLARE_METATYPE(Find::IFindFilter*)

using namespace Find;
using namespace Find::Internal;

FindToolBar::FindToolBar(CurrentDocumentFind *currentDocumentFind)
    : m_currentDocumentFind(currentDocumentFind),
      m_findCompleter(new QCompleter(this)),
      m_replaceCompleter(new QCompleter(this)),
      m_enterFindStringAction(0),
      m_findNextAction(0),
      m_findPreviousAction(0),
      m_replaceAction(0),
      m_replaceNextAction(0),
      m_replacePreviousAction(0),
      m_findIncrementalTimer(this), m_findStepTimer(this),
      m_useFakeVim(false),
      m_eventFiltersInstalled(false)
{
    //setup ui
    m_ui.setupUi(this);

    // compensate for a vertically expanding spacer below the label
    m_ui.replaceLabel->setMinimumHeight(m_ui.replaceEdit->sizeHint().height());
    delete m_ui.replaceButtonsWidget->layout();
    Utils::FlowLayout *flowlayout = new Utils::FlowLayout(m_ui.replaceButtonsWidget, 0, 3, 3);
    flowlayout->addWidget(m_ui.replaceButton);
    flowlayout->addWidget(m_ui.replaceNextButton);
    flowlayout->addWidget(m_ui.replaceAllButton);
    m_ui.replaceButtonsWidget->setLayout(flowlayout);
    setFocusProxy(m_ui.findEdit);
    setProperty("topBorder", true);
    setSingleRow(false);
    m_ui.findEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
    m_ui.replaceEdit->setAttribute(Qt::WA_MacShowFocusRect, false);

    connect(m_ui.findEdit, &Utils::FilterLineEdit::editingFinished, this, &FindToolBar::invokeResetIncrementalSearch);

    // Hack to get the icon sizes right - if left to their own devices they end up too big
    QSize iconSize(18,18);
    m_ui.close->setIconSize(iconSize);
    m_ui.findNextButton->setIconSize(iconSize);
    m_ui.findPreviousButton->setIconSize(iconSize);

    m_ui.close->setIcon(QIcon(QLatin1String(":/texteditor/images/button_close.png")));
    connect(m_ui.close, &QToolButton::clicked, this, &FindToolBar::hideAndResetFocus);

    //m_findCompleter->setModel(m_plugin->findCompletionModel());
    //m_replaceCompleter->setModel(m_plugin->replaceCompletionModel());
    m_ui.findEdit->setCompleter(m_findCompleter);
    m_ui.replaceEdit->setCompleter(m_replaceCompleter);

    QMenu *lineEditMenu = new QMenu(m_ui.findEdit);
    m_ui.findEdit->setButtonMenu(Utils::FancyLineEdit::Left, lineEditMenu);
    m_ui.findEdit->setButtonVisible(Utils::FancyLineEdit::Left, true);
    m_ui.findEdit->setPlaceholderText(QString());
    m_ui.replaceEdit->setPlaceholderText(QString());

    connect(m_ui.findEdit, &Utils::FilterLineEdit::textChanged, this, &FindToolBar::invokeFindIncremental);
    connect(m_ui.findEdit, &Utils::FilterLineEdit::returnPressed, this, &FindToolBar::invokeFindEnter);
    connect(m_ui.replaceEdit, &Utils::FilterLineEdit::returnPressed, this, &FindToolBar::invokeReplaceEnter);

    QAction *shiftEnterAction = new QAction(m_ui.findEdit);
    shiftEnterAction->setShortcut(QKeySequence(tr("Shift+Enter")));
    shiftEnterAction->setShortcutContext(Qt::WidgetShortcut);
    connect(shiftEnterAction, &QAction::triggered, this, &FindToolBar::invokeFindPrevious);
    m_ui.findEdit->addAction(shiftEnterAction);
    QAction *shiftReturnAction = new QAction(m_ui.findEdit);
    shiftReturnAction->setShortcut(QKeySequence(tr("Shift+Return")));
    shiftReturnAction->setShortcutContext(Qt::WidgetShortcut);
    connect(shiftReturnAction, &QAction::triggered, this, &FindToolBar::invokeFindPrevious);
    m_ui.findEdit->addAction(shiftReturnAction);

    QAction *shiftEnterReplaceAction = new QAction(m_ui.replaceEdit);
    shiftEnterReplaceAction->setShortcut(QKeySequence(tr("Shift+Enter")));
    shiftEnterReplaceAction->setShortcutContext(Qt::WidgetShortcut);
    connect(shiftEnterReplaceAction, &QAction::triggered, this, &FindToolBar::invokeReplacePrevious);
    m_ui.replaceEdit->addAction(shiftEnterReplaceAction);
    QAction *shiftReturnReplaceAction = new QAction(m_ui.replaceEdit);
    shiftReturnReplaceAction->setShortcut(QKeySequence(tr("Shift+Return")));
    shiftReturnReplaceAction->setShortcutContext(Qt::WidgetShortcut);
    connect(shiftReturnReplaceAction, &QAction::triggered, this, &FindToolBar::invokeReplacePrevious);
    m_ui.replaceEdit->addAction(shiftReturnReplaceAction);

    // need to make sure QStringList is registered as metatype
    QMetaTypeId<QStringList>::qt_metatype_id();

    // register actions
    //Core::Context globalcontext(Core::Constants::C_GLOBAL);
    //Core::ActionManager *am = Core::ICore::actionManager();
    //Core::ActionContainer *mfind = am->actionContainer(Constants::M_FIND);
    //Core::Command *cmd;

    //m_ui.advancedButton->setDefaultAction(am->command(Constants::ADVANCED_FIND)->action());

    QIcon icon = QIcon::fromTheme(QLatin1String("edit-find-replace"));
    m_findInDocumentAction = new QAction(icon, tr("Find/Replace"), this);
    //cmd = am->registerAction(m_findInDocumentAction, Constants::FIND_IN_DOCUMENT, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence::Find);
    //mfind->addAction(cmd, Constants::G_FIND_CURRENTDOCUMENT);
    connect(m_findInDocumentAction, &QAction::triggered, this, &FindToolBar::openFind);

    if (QApplication::clipboard()->supportsFindBuffer()) {
        m_enterFindStringAction = new QAction(tr("Enter Find String"), this);
        //cmd = am->registerAction(m_enterFindStringAction, "Find.EnterFindString", globalcontext);
        //cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+E")));
        //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
        connect(m_enterFindStringAction, &QAction::triggered, this, &FindToolBar::putSelectionToFindClipboard);
        connect(QApplication::clipboard(), &QClipboard::findBufferChanged, this, &FindToolBar::updateFromFindClipboard);
    }

    m_findNextAction = new QAction(tr("Find Next"), this);
    //cmd = am->registerAction(m_findNextAction, Constants::FIND_NEXT, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence::FindNext);
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_findNextAction, &QAction::triggered, this, &FindToolBar::invokeFindNext);
    m_ui.findNextButton->setDefaultAction(m_findNextAction);

    m_findPreviousAction = new QAction(tr("Find Previous"), this);
    //cmd = am->registerAction(m_findPreviousAction, Constants::FIND_PREVIOUS, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence::FindPrevious);
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_findPreviousAction, &QAction::triggered, this, &FindToolBar::invokeFindPrevious);
    m_ui.findPreviousButton->setDefaultAction(m_findPreviousAction);

    m_findNextSelectedAction = new QAction(tr("Find Next (Selected)"), this);
    //cmd = am->registerAction(m_findNextSelectedAction, Constants::FIND_NEXT_SELECTED, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+F3")));
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_findNextSelectedAction, &QAction::triggered, this, &FindToolBar::findNextSelected);

    m_findPreviousSelectedAction = new QAction(tr("Find Previous (Selected)"), this);
    //cmd = am->registerAction(m_findPreviousSelectedAction, Constants::FIND_PREV_SELECTED, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+F3")));
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_findPreviousSelectedAction, &QAction::triggered, this, &FindToolBar::findPreviousSelected);

    m_replaceAction = new QAction(tr("Replace"), this);
    //cmd = am->registerAction(m_replaceAction, Constants::REPLACE, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence());
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_replaceAction, &QAction::triggered, this, &FindToolBar::invokeReplace);
    m_ui.replaceButton->setDefaultAction(m_replaceAction);

    m_replaceNextAction = new QAction(tr("Replace && Find"), this);
    m_replaceNextAction->setIconText(tr("Replace && Find")); // work around bug in Qt that kills ampersands in tool button
    //cmd = am->registerAction(m_replaceNextAction, Constants::REPLACE_NEXT, globalcontext);
    //cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+=")));
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_replaceNextAction, &QAction::triggered, this, &FindToolBar::invokeReplaceNext);
    m_ui.replaceNextButton->setDefaultAction(m_replaceNextAction);

    m_replacePreviousAction = new QAction(tr("Replace && Find Previous"), this);
    //cmd = am->registerAction(m_replacePreviousAction, Constants::REPLACE_PREVIOUS, globalcontext);
    // shortcut removed, clashes with Ctrl++ on many keyboard layouts
    //cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+=")));
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    connect(m_replacePreviousAction, &QAction::triggered, this, &FindToolBar::invokeReplacePrevious);

    m_replaceAllAction = new QAction(tr("Replace All"), this);
    //cmd = am->registerAction(m_replaceAllAction, Constants::REPLACE_ALL, globalcontext);
    //mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
    //connect(m_replaceAllAction, SIGNAL(triggered()), this, SLOT(invokeReplaceAll()));
    m_ui.replaceAllButton->setDefaultAction(m_replaceAllAction);

    m_caseSensitiveAction = new QAction(tr("Case Sensitive"), this);
    m_caseSensitiveAction->setIcon(QIcon(QLatin1String(":/texteditor/images/casesensitively.png")));
    m_caseSensitiveAction->setCheckable(true);
    m_caseSensitiveAction->setChecked(false);
    //cmd = am->registerAction(m_caseSensitiveAction, Constants::CASE_SENSITIVE, globalcontext);
    //mfind->addAction(cmd, Constants::G_FIND_FLAGS);
    connect(m_caseSensitiveAction, &QAction::triggered, this, &FindToolBar::setCaseSensitive);
    lineEditMenu->addAction(m_caseSensitiveAction);

    m_wholeWordAction = new QAction(tr("Whole Words Only"), this);
    m_wholeWordAction->setIcon(QIcon(QLatin1String(":/texteditor/images/wholewords.png")));
    m_wholeWordAction->setCheckable(true);
    m_wholeWordAction->setChecked(false);
    //cmd = am->registerAction(m_wholeWordAction, Constants::WHOLE_WORDS, globalcontext);
    //mfind->addAction(cmd, Constants::G_FIND_FLAGS);
    connect(m_wholeWordAction, &QAction::triggered, this, &FindToolBar::setWholeWord);
    lineEditMenu->addAction(m_wholeWordAction);

    m_regularExpressionAction = new QAction(tr("Use Regular Expressions"), this);
    m_regularExpressionAction->setIcon(QIcon(QLatin1String(":/texteditor/images/regexp.png")));
    m_regularExpressionAction->setCheckable(true);
    m_regularExpressionAction->setChecked(false);
    //cmd = am->registerAction(m_regularExpressionAction, Constants::REGULAR_EXPRESSIONS, globalcontext);
    //mfind->addAction(cmd, Constants::G_FIND_FLAGS);
    connect(m_regularExpressionAction, &QAction::triggered, this, &FindToolBar::setRegularExpressions);
    lineEditMenu->addAction(m_regularExpressionAction);

    connect(m_currentDocumentFind, &CurrentDocumentFind::candidateChanged, this, &FindToolBar::adaptToCandidate);
    connect(m_currentDocumentFind, &CurrentDocumentFind::changed, this, &FindToolBar::updateToolBar);
    updateToolBar();

    m_findIncrementalTimer.setSingleShot(true);
    m_findStepTimer.setSingleShot(true);
    connect(&m_findIncrementalTimer, &QTimer::timeout,
            this, &FindToolBar::invokeFindIncremental);
    connect(&m_findStepTimer, &QTimer::timeout, this, &FindToolBar::invokeFindStep);
}

FindToolBar::~FindToolBar()
{
}

void FindToolBar::installEventFilters()
{
    if (!m_eventFiltersInstalled) {
        m_findCompleter->popup()->installEventFilter(this);
        m_ui.findEdit->installEventFilter(this);
        m_ui.replaceEdit->installEventFilter(this);
        this->installEventFilter(this);
        m_eventFiltersInstalled = true;
    }
}

bool FindToolBar::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Down) {
            if (obj == m_ui.findEdit)
                m_findCompleter->complete();
            else if (obj == m_ui.replaceEdit)
                m_replaceCompleter->complete();
        }
    }

    if ((obj == m_ui.findEdit || obj == m_findCompleter->popup())
               && event->type() == QEvent::KeyPress) {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
#ifdef Q_OS_MACOS
        if (ke->key() == Qt::Key_Space && (ke->modifiers() & Qt::MetaModifier)) {
#else
        if (ke->key() == Qt::Key_Space && (ke->modifiers() & Qt::ControlModifier)) {
#endif
            QString completedText = m_currentDocumentFind->completedFindString();
            if (!completedText.isEmpty()) {
                setFindText(completedText);
                ke->accept();
                return true;
            }
        }
    } else if (obj == this && event->type() == QEvent::ShortcutOverride) {
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Escape && !ke->modifiers()
                && !m_findCompleter->popup()->isVisible()
                && !m_replaceCompleter->popup()->isVisible()) {
            if (setFocusToCurrentFindSupport()) {
                event->accept();
                return true;
            }
#ifdef Q_OS_MACOS
        } else if (ke->key() == Qt::Key_Space && (ke->modifiers() & Qt::MetaModifier)) {
#else
        } else if (ke->key() == Qt::Key_Space && (ke->modifiers() & Qt::ControlModifier)) {
#endif
            event->accept();
            return true;
        }
    } else if (obj == this && event->type() == QEvent::Hide) {
        invokeClearResults();
        if (m_currentDocumentFind->isEnabled()) {
            m_currentDocumentFind->clearFindScope();
        }
    }
    return Utils::StyledBar::eventFilter(obj, event);
}

void FindToolBar::adaptToCandidate()
{
    updateFindAction();
    if (findToolBarPlaceHolder() == TextEditor::FindToolBarPlaceHolder::getCurrent()) {
        m_currentDocumentFind->acceptCandidate();
    }
}

void FindToolBar::updateFindAction()
{
    bool enabled = m_currentDocumentFind->candidateIsEnabled();
    m_findInDocumentAction->setEnabled(enabled);
    m_findNextSelectedAction->setEnabled(enabled);
    m_findPreviousSelectedAction->setEnabled(enabled);
}

void FindToolBar::updateToolBar()
{
    bool enabled = m_currentDocumentFind->isEnabled();
    bool replaceEnabled = enabled && m_currentDocumentFind->supportsReplace();
    m_findNextAction->setEnabled(enabled);
    m_findPreviousAction->setEnabled(enabled);

    m_replaceAction->setEnabled(replaceEnabled);
    m_replaceNextAction->setEnabled(replaceEnabled);
    m_replacePreviousAction->setEnabled(replaceEnabled);
    m_replaceAllAction->setEnabled(replaceEnabled);

    m_caseSensitiveAction->setEnabled(enabled);
    m_wholeWordAction->setEnabled(enabled);
    m_regularExpressionAction->setEnabled(enabled);
    if (QApplication::clipboard()->supportsFindBuffer())
        m_enterFindStringAction->setEnabled(enabled);
    bool replaceFocus = m_ui.replaceEdit->hasFocus();
    m_ui.findEdit->setEnabled(enabled);
    m_ui.findLabel->setEnabled(enabled);

    m_ui.replaceEdit->setEnabled(replaceEnabled);
    m_ui.replaceLabel->setEnabled(replaceEnabled);
    m_ui.replaceEdit->setVisible(replaceEnabled);
    m_ui.replaceLabel->setVisible(replaceEnabled);
    m_ui.replaceButtonsWidget->setVisible(replaceEnabled);
    m_ui.replaceAllButton->setVisible(false);
    m_ui.advancedButton->setVisible(false);
    layout()->invalidate();

    if (!replaceEnabled && enabled && replaceFocus)
        m_ui.findEdit->setFocus();
    updateIcons();
    updateFlagMenus();
}

void FindToolBar::invokeFindEnter()
{
    if (m_currentDocumentFind->isEnabled()) {
        if (m_useFakeVim)
            setFocusToCurrentFindSupport();
        else
            invokeFindNext();
    }
}

void FindToolBar::invokeReplaceEnter()
{
    if (m_currentDocumentFind->isEnabled() && m_currentDocumentFind->supportsReplace()) {
        invokeReplaceNext();
    }
}

void FindToolBar::invokeClearResults()
{
    if (m_currentDocumentFind->isEnabled()) {
        m_currentDocumentFind->clearResults();
    }
}


void FindToolBar::invokeFindNext()
{
    setFindFlag(Find::FindBackward, false);
    invokeFindStep();
}

void FindToolBar::invokeFindPrevious()
{
    setFindFlag(Find::FindBackward, true);
    invokeFindStep();
}

QString FindToolBar::getFindText()
{
    return m_ui.findEdit->text();
}

QString FindToolBar::getReplaceText()
{
    return m_ui.replaceEdit->text();
}

void FindToolBar::setFindText(const QString &text)
{
    disconnect(m_ui.findEdit, &Utils::FilterLineEdit::textChanged, this, &FindToolBar::invokeFindIncremental);
    if (hasFindFlag(Find::FindRegularExpression))
        m_ui.findEdit->setText(QRegularExpression::escape(text));
    else
        m_ui.findEdit->setText(text);
    connect(m_ui.findEdit, &Utils::FilterLineEdit::textChanged, this, &FindToolBar::invokeFindIncremental);
}

void FindToolBar::selectFindText()
{
    m_ui.findEdit->selectAll();
}

void FindToolBar::invokeFindStep()
{
    m_findStepTimer.stop();
    m_findIncrementalTimer.stop();
    if (m_currentDocumentFind->isEnabled()) {
        //m_plugin->updateFindCompletion(getFindText());
        IFindSupport::Result result =
            m_currentDocumentFind->findStep(getFindText(), effectiveFindFlags());
        if (result == IFindSupport::NotYetFound)
            m_findStepTimer.start(50);
    }
}

void FindToolBar::invokeFindIncremental()
{
    m_findIncrementalTimer.stop();
    m_findStepTimer.stop();
    if (m_currentDocumentFind->isEnabled()) {
        QString text = getFindText();
        IFindSupport::Result result =
            m_currentDocumentFind->findIncremental(text, effectiveFindFlags());
        if (result == IFindSupport::NotYetFound)
            m_findIncrementalTimer.start(50);
        if (text.isEmpty())
            m_currentDocumentFind->clearResults();
    }
}

void FindToolBar::invokeReplace()
{
    setFindFlag(Find::FindBackward, false);
    if (m_currentDocumentFind->isEnabled() && m_currentDocumentFind->supportsReplace()) {
        //m_plugin->updateFindCompletion(getFindText());
        //m_plugin->updateReplaceCompletion(getReplaceText());
        m_currentDocumentFind->replace(getFindText(), getReplaceText(), effectiveFindFlags());
    }
}

void FindToolBar::invokeReplaceNext()
{
    setFindFlag(Find::FindBackward, false);
    invokeReplaceStep();
}

void FindToolBar::invokeReplacePrevious()
{
    setFindFlag(Find::FindBackward, true);
    invokeReplaceStep();
}

void FindToolBar::invokeReplaceStep()
{
    if (m_currentDocumentFind->isEnabled() && m_currentDocumentFind->supportsReplace()) {
        //m_plugin->updateFindCompletion(getFindText());
        //m_plugin->updateReplaceCompletion(getReplaceText());
        m_currentDocumentFind->replaceStep(getFindText(), getReplaceText(), effectiveFindFlags());
    }
}

void FindToolBar::invokeReplaceAll()
{
    //m_plugin->updateFindCompletion(getFindText());
    //m_plugin->updateReplaceCompletion(getReplaceText());
    if (m_currentDocumentFind->isEnabled() && m_currentDocumentFind->supportsReplace()) {
        m_currentDocumentFind->replaceAll(getFindText(), getReplaceText(), effectiveFindFlags());
    }
}

void FindToolBar::invokeResetIncrementalSearch()
{
    m_findIncrementalTimer.stop();
    m_findStepTimer.stop();
    if (m_currentDocumentFind->isEnabled())
        m_currentDocumentFind->resetIncrementalSearch();
}


void FindToolBar::putSelectionToFindClipboard()
{
    const QString text = m_currentDocumentFind->currentFindString();
    QApplication::clipboard()->setText(text, QClipboard::FindBuffer);
    setFindText(text);
}


void FindToolBar::updateFromFindClipboard()
{
    if (QApplication::clipboard()->supportsFindBuffer()) {
        const bool blocks = m_ui.findEdit->blockSignals(true);
        setFindText(QApplication::clipboard()->text(QClipboard::FindBuffer));
        m_ui.findEdit->blockSignals(blocks);
    }
}

void FindToolBar::findFlagsChanged()
{
    updateIcons();
    updateFlagMenus();
    invokeClearResults();
    if (isVisible()) {
        m_currentDocumentFind->highlightAll(getFindText(), effectiveFindFlags());
    }
}

void FindToolBar::updateIcons()
{
    Find::FindFlags effectiveFlags = effectiveFindFlags();
    bool casesensitive = effectiveFlags & Find::FindCaseSensitively;
    bool wholewords = effectiveFlags & Find::FindWholeWords;
    bool regexp = effectiveFlags & Find::FindRegularExpression;
    if (!casesensitive && !wholewords && !regexp) {
        QPixmap pixmap(17, 17);
        pixmap.fill(Qt::transparent);
        QPainter painter(&pixmap);
        const QPixmap mag = QPixmap(QLatin1String(":/texteditor/images/magnifier.png"));
        painter.drawPixmap(0, (pixmap.height() - mag.height()) / 2, mag);
        m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left, pixmap);
    } else {
        m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left,
                                       IFindFilter::pixmapForFindFlags(effectiveFlags));
    }
}

Find::FindFlags FindToolBar::effectiveFindFlags()
{
    Find::FindFlags supportedFlags;
    if (m_currentDocumentFind->isEnabled())
        supportedFlags = m_currentDocumentFind->supportedFindFlags();
    else
        supportedFlags = (Find::FindFlags)0xFFFFFF;
    return supportedFlags & m_findFlags;
}

void FindToolBar::updateFlagMenus()
{
    bool wholeOnly = ((m_findFlags & Find::FindWholeWords));
    bool sensitive = ((m_findFlags & Find::FindCaseSensitively));
    bool regexp = ((m_findFlags & Find::FindRegularExpression));
    if (m_wholeWordAction->isChecked() != wholeOnly)
        m_wholeWordAction->setChecked(wholeOnly);
    if (m_caseSensitiveAction->isChecked() != sensitive)
        m_caseSensitiveAction->setChecked(sensitive);
    if (m_regularExpressionAction->isChecked() != regexp)
        m_regularExpressionAction->setChecked(regexp);
    Find::FindFlags supportedFlags;
    if (m_currentDocumentFind->isEnabled())
        supportedFlags = m_currentDocumentFind->supportedFindFlags();
    m_wholeWordAction->setEnabled(supportedFlags & Find::FindWholeWords);
    m_caseSensitiveAction->setEnabled(supportedFlags & Find::FindCaseSensitively);
    m_regularExpressionAction->setEnabled(supportedFlags & Find::FindRegularExpression);
}

bool FindToolBar::setFocusToCurrentFindSupport()
{
    return m_currentDocumentFind->setFocusToCurrentFindSupport();
}

void FindToolBar::hideAndResetFocus()
{
    m_currentDocumentFind->setFocusToCurrentFindSupport();
    hide();
}

TextEditor::FindToolBarPlaceHolder *FindToolBar::findToolBarPlaceHolder() const
{
    QList<TextEditor::FindToolBarPlaceHolder*> placeholders = TextEditor::FindToolBarPlaceHolder::getPlaceholders();
    QWidget *candidate = QApplication::focusWidget();
    while (candidate) {
        foreach (TextEditor::FindToolBarPlaceHolder *ph, placeholders) {
            if (ph->owner() == candidate)
                return ph;
        }
        candidate = candidate->parentWidget();
    }
    return 0;
}

void FindToolBar::openFind(bool focus)
{
    setBackward(false);
    openFindToolBar(focus);
}

void FindToolBar::openFindToolBar(bool focus, QString initialText)
{
    installEventFilters();
    if (!m_currentDocumentFind->candidateIsEnabled())
        return;
    TextEditor::FindToolBarPlaceHolder *holder = findToolBarPlaceHolder();
    if (!holder)
        return;
    TextEditor::FindToolBarPlaceHolder *previousHolder = TextEditor::FindToolBarPlaceHolder::getCurrent();
    if (previousHolder)
        previousHolder->setWidget(0);
    TextEditor::FindToolBarPlaceHolder::setCurrent(holder);
    m_currentDocumentFind->acceptCandidate();
    holder->setWidget(this);
    holder->setVisible(true);
    setVisible(true);
    if (focus)
        setFocus();
    QString text = m_currentDocumentFind->currentFindString();
    if (!initialText.isEmpty())
        setFindText(initialText);
    else if (!text.isEmpty())
        setFindText(text);
    m_currentDocumentFind->defineFindScope();
    m_currentDocumentFind->highlightAll(getFindText(), effectiveFindFlags());
    if (focus)
        selectFindText();
}

void FindToolBar::findNextSelected()
{
    openFind(false);
    invokeFindNext();
}

void FindToolBar::findPreviousSelected()
{
    openFind(false);
    invokeFindPrevious();
}

bool FindToolBar::focusNextPrevChild(bool next)
{
    // close tab order change
    if (next && m_ui.replaceAllButton->hasFocus())
        m_ui.findEdit->setFocus(Qt::TabFocusReason);
    else if (!next && m_ui.findEdit->hasFocus())
        m_ui.replaceAllButton->setFocus(Qt::TabFocusReason);
    else
        return Utils::StyledBar::focusNextPrevChild(next);
    return true;
}

void FindToolBar::writeSettings(QSettings &settings)
{
    settings.beginGroup(QLatin1String("Find"));
    settings.beginGroup(QLatin1String("FindToolBar"));
    settings.setValue(QLatin1String("Backward"), QVariant((m_findFlags & Find::FindBackward) != 0));
    settings.setValue(QLatin1String("CaseSensitively"), QVariant((m_findFlags & Find::FindCaseSensitively) != 0));
    settings.setValue(QLatin1String("WholeWords"), QVariant((m_findFlags & Find::FindWholeWords) != 0));
    settings.setValue(QLatin1String("RegularExpression"), QVariant((m_findFlags & Find::FindRegularExpression) != 0));
    settings.endGroup();
    settings.endGroup();
}

void FindToolBar::readSettings(QSettings &settings)
{
    settings.beginGroup(QLatin1String("Find"));
    settings.beginGroup(QLatin1String("FindToolBar"));
    Find::FindFlags flags;
    if (settings.value(QLatin1String("Backward"), false).toBool())
        flags |= Find::FindBackward;
    if (settings.value(QLatin1String("CaseSensitively"), false).toBool())
        flags |= Find::FindCaseSensitively;
    if (settings.value(QLatin1String("WholeWords"), false).toBool())
        flags |= Find::FindWholeWords;
    if (settings.value(QLatin1String("RegularExpression"), false).toBool())
        flags |= Find::FindRegularExpression;
    settings.endGroup();
    settings.endGroup();
    m_findFlags = flags;
    findFlagsChanged();
}

void FindToolBar::setUseFakeVim(bool on)
{
    m_useFakeVim = on;
}

void FindToolBar::setFindFlag(Find::FindFlag flag, bool enabled)
{
    bool hasFlag = hasFindFlag(flag);
    if ((hasFlag && enabled) || (!hasFlag && !enabled))
        return;
    if (enabled)
        m_findFlags |= flag;
    else
        m_findFlags &= ~flag;
    if (flag != Find::FindBackward)
        findFlagsChanged();
}

bool FindToolBar::hasFindFlag(Find::FindFlag flag)
{
    return m_findFlags & flag;
}

void FindToolBar::setCaseSensitive(bool sensitive)
{
    setFindFlag(Find::FindCaseSensitively, sensitive);
}

void FindToolBar::setWholeWord(bool wholeOnly)
{
    setFindFlag(Find::FindWholeWords, wholeOnly);
}

void FindToolBar::setRegularExpressions(bool regexp)
{
    setFindFlag(Find::FindRegularExpression, regexp);
}

void FindToolBar::setBackward(bool backward)
{
    setFindFlag(Find::FindBackward, backward);
}

void FindToolBar::triggerFindNext()
{
    m_findNextAction->activate(QAction::Trigger);
}

void FindToolBar::triggerFindPrevious()
{
    m_findPreviousAction->activate(QAction::Trigger);
}
