File indexing completed on 2024-06-16 05:24:57

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-2009 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "searchcontroller.hpp"
0010 
0011 // controller
0012 #include "searchdialog.hpp"
0013 #include "searchtool.hpp"
0014 // KF
0015 #include <KXMLGUIClient>
0016 #include <KLocalizedString>
0017 #include <KActionCollection>
0018 #include <KStandardAction>
0019 #include <KMessageBox>
0020 // Qt
0021 #include <QAction>
0022 
0023 namespace Kasten {
0024 
0025 // TODO: for docked widgets signal widgets if embedded or floating, if horizontal/vertical
0026 SearchController::SearchController(KXMLGUIClient* guiClient, QWidget* parentWidget)
0027     : mParentWidget(parentWidget)
0028 {
0029     mFindAction     = KStandardAction::find(    this, &SearchController::find,         this);
0030     mFindNextAction = KStandardAction::findNext(this, &SearchController::findNext,     this);
0031     mFindPrevAction = KStandardAction::findPrev(this, &SearchController::findPrevious, this);
0032 
0033     guiClient->actionCollection()->addActions({
0034         mFindAction,
0035         mFindNextAction,
0036         mFindPrevAction
0037     });
0038 
0039 
0040     mTool = new SearchTool();
0041     mTool->setUserQueryAgent(this);
0042 
0043     connect(mTool, &SearchTool::isApplyableChanged,
0044             mFindAction, &QAction::setEnabled);
0045     connect(mTool, &SearchTool::isApplyableChanged,
0046             mFindNextAction, &QAction::setEnabled);
0047     connect(mTool, &SearchTool::isApplyableChanged,
0048             mFindPrevAction, &QAction::setEnabled);
0049 
0050     connect(mTool, &SearchTool::dataNotFound, this, &SearchController::onDataNotFound);
0051 
0052     mFindAction->setEnabled(false);
0053     mFindNextAction->setEnabled(false);
0054     mFindPrevAction->setEnabled(false);
0055 }
0056 
0057 SearchController::~SearchController()
0058 {
0059     delete mSearchDialog;
0060     delete mTool;
0061 }
0062 
0063 void SearchController::setTargetModel(AbstractModel* model)
0064 {
0065     mTool->setTargetModel(model);
0066 }
0067 
0068 void SearchController::find()
0069 {
0070     // ensure dialog
0071     if (!mSearchDialog) {
0072         mSearchDialog = new SearchDialog(mTool, mParentWidget);
0073     }
0074 
0075     mSearchDialog->show();
0076 }
0077 
0078 void SearchController::findNext()
0079 {
0080     if (mTool->searchData().isEmpty()) {
0081         showDialog(FindForward);
0082     } else {
0083         mTool->search(FindForward, true, false);
0084     }
0085     ;
0086 }
0087 
0088 void SearchController::findPrevious()
0089 {
0090     if (mTool->searchData().isEmpty()) {
0091         showDialog(FindBackward);
0092     } else {
0093         mTool->search(FindBackward, true, false);
0094     }
0095 }
0096 
0097 void SearchController::showDialog(FindDirection direction)
0098 {
0099     // ensure dialog
0100     if (!mSearchDialog) {
0101         mSearchDialog = new SearchDialog(mTool, mParentWidget);
0102     }
0103 
0104     mSearchDialog->setDirection(direction);
0105 
0106     mSearchDialog->show();
0107 }
0108 
0109 void SearchController::onDataNotFound()
0110 {
0111     const QString messageBoxTitle = i18nc("@title:window", "Find");
0112     KMessageBox::information(mParentWidget, i18nc("@info", "Search key not found in byte array."), messageBoxTitle);
0113 }
0114 
0115 bool SearchController::queryContinue(FindDirection direction) const
0116 {
0117     const QString messageBoxTitle = i18nc("@title:window", "Find");
0118     const QString question = (direction == FindForward) ?
0119                              xi18nc("@info", "End of byte array reached.<nl/>Continue from the beginning?") :
0120                              xi18nc("@info", "Beginning of byte array reached.<nl/>Continue from the end?");
0121 
0122     const int answer = KMessageBox::questionTwoActions(mParentWidget,
0123                                                        question, messageBoxTitle,
0124                                                        KStandardGuiItem::cont(),
0125                                                        KStandardGuiItem::cancel());
0126 
0127     const bool result = (answer != KMessageBox::SecondaryAction);
0128 
0129     return result;
0130 }
0131 
0132 }
0133 
0134 #include "moc_searchcontroller.cpp"