File indexing completed on 2024-07-14 03:42:04

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool...
0003     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0004     SPDX-FileCopyrightText: 2011 Daniel E. Moctezuma <democtezuma@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "searchstringinput.h"
0010 
0011 #include "kiten.h"
0012 #include "kitenconfig.h"
0013 
0014 #include <KActionCollection>
0015 #include <KHistoryComboBox>
0016 #include <KLocalizedString>
0017 #include <KSelectAction>
0018 #include <KToolBar>
0019 
0020 #include <QLineEdit>
0021 
0022 SearchStringInput::SearchStringInput(Kiten *parent)
0023     : QObject(parent)
0024 {
0025     _parent = parent;
0026     _actionFilterRare = _parent->actionCollection()->add<KSelectAction>(QStringLiteral("filter_rare"));
0027     _actionFilterRare->setText(i18n("Filter Type"));
0028     _actionFilterRare->addAction(i18n("No filter"));
0029     _actionFilterRare->addAction(i18n("Filter out rare"));
0030     _actionFilterRare->addAction(i18n("Common/Uncommon"));
0031 
0032     _actionSearchSection = _parent->actionCollection()->add<KSelectAction>(QStringLiteral("search_searchType"));
0033     _actionSearchSection->setText(i18n("Match Type"));
0034     _actionSearchSection->addAction(i18n("Exact Match"));
0035     _actionSearchSection->addAction(i18n("Match Beginning"));
0036     _actionSearchSection->addAction(i18n("Match Ending"));
0037     _actionSearchSection->addAction(i18n("Match Anywhere"));
0038 
0039     _actionSelectWordType = _parent->actionCollection()->add<KSelectAction>(QStringLiteral("search_wordType"));
0040     _actionSelectWordType->setText(i18n("Word Type"));
0041     _actionSelectWordType->addAction(i18n("Any"));
0042     _actionSelectWordType->addAction(i18n("Verb"));
0043     _actionSelectWordType->addAction(i18n("Noun"));
0044     _actionSelectWordType->addAction(i18n("Adjective"));
0045     _actionSelectWordType->addAction(i18n("Adverb"));
0046     _actionSelectWordType->addAction(i18n("Prefix"));
0047     _actionSelectWordType->addAction(i18n("Suffix"));
0048     _actionSelectWordType->addAction(i18n("Expression"));
0049 
0050     _actionTextInput = new KHistoryComboBox(_parent);
0051     _actionTextInput->setDuplicatesEnabled(false);
0052     _actionTextInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
0053     updateFontFromConfig();
0054 
0055     _actionFocusInput = _parent->actionCollection()->addAction(QStringLiteral("focusinputfield"), this, SLOT(focusInput()));
0056     _parent->actionCollection()->setDefaultShortcut(_actionFocusInput, Qt::CTRL | Qt::Key_L);
0057     _actionFocusInput->setText(i18n("Focus input field"));
0058 
0059     auto actionsearchbox = new QWidgetAction(this);
0060     actionsearchbox->setText(i18n("Search Bar"));
0061     actionsearchbox->setDefaultWidget(_actionTextInput);
0062     _parent->actionCollection()->addAction(QStringLiteral("searchbox"), actionsearchbox);
0063 
0064     if (!_actionFilterRare || !_actionSearchSection || !_actionSelectWordType || !actionsearchbox) {
0065         qCritical() << "Error creating user interface elements: " << !_actionFilterRare << !_actionSearchSection << !_actionSelectWordType << !actionsearchbox;
0066     }
0067 
0068     // connect(actionTextInput, SIGNAL(returnPressed()), this, SIGNAL(search()));
0069     connect(_actionTextInput, static_cast<void (KHistoryComboBox::*)(const QString &)>(&KHistoryComboBox::textActivated), this, &SearchStringInput::test);
0070 }
0071 
0072 void SearchStringInput::focusInput()
0073 {
0074     _actionTextInput->setFocus();
0075     _actionTextInput->lineEdit()->selectAll();
0076 }
0077 
0078 DictQuery SearchStringInput::getSearchQuery() const
0079 {
0080     DictQuery result(_actionTextInput->currentText());
0081 
0082     if (_actionFilterRare->currentItem() == DictQuery::Rare) {
0083         result.setProperty(QStringLiteral("common"), QStringLiteral("1"));
0084     }
0085 
0086     result.setFilterType((DictQuery::FilterType)_actionFilterRare->currentItem());
0087     result.setMatchType((DictQuery::MatchType)_actionSearchSection->currentItem());
0088     result.setMatchWordType((DictQuery::MatchWordType)_actionSelectWordType->currentItem());
0089 
0090     return result;
0091 }
0092 
0093 void SearchStringInput::setDefaultsFromConfig()
0094 {
0095     KitenConfigSkeleton *config = KitenConfigSkeleton::self();
0096     _actionFilterRare->setCurrentItem(config->filter_rare());
0097     _actionSearchSection->setCurrentItem(config->search_precision());
0098     _actionSelectWordType->setCurrentItem(config->search_limit_to_wordtype());
0099 }
0100 
0101 void SearchStringInput::setSearchQuery(const DictQuery &query)
0102 {
0103     qDebug() << "------------------------------Set Triggered";
0104     // First we set the various actions according to the query
0105     _actionFilterRare->setCurrentItem(query.getFilterType());
0106     _actionSearchSection->setCurrentItem(query.getMatchType());
0107     _actionSelectWordType->setCurrentItem(query.getMatchWordType());
0108 
0109     // Secondly we remove aspects that are visible in the gui from the search string
0110     DictQuery copy(query);
0111     for (KToolBar *bar : _parent->toolBars()) {
0112         if (bar->widgetForAction(_actionFilterRare) != nullptr)
0113             copy.removeProperty(QStringLiteral("common"));
0114 
0115         if (bar->widgetForAction(_actionSelectWordType) != nullptr)
0116             copy.removeProperty(QStringLiteral("type"));
0117     }
0118 
0119     _actionTextInput->addToHistory(copy.toString()); // Update history and completion list
0120     _actionTextInput->setCurrentItem(copy.toString(), true);
0121     _actionTextInput->reset(); // Call this manually when you call setCurrentItem
0122 }
0123 
0124 void SearchStringInput::test()
0125 {
0126     qDebug() << "test1";
0127     Q_EMIT search();
0128     qDebug() << "test2";
0129 }
0130 
0131 void SearchStringInput::updateFontFromConfig()
0132 {
0133     _actionTextInput->setFont(KitenConfigSkeleton::self()->font());
0134 }
0135 
0136 #include "moc_searchstringinput.cpp"