Warning, file /utilities/okteta/kasten/controllers/view/replace/replacecontroller.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "replacecontroller.hpp"
0010 
0011 // controller
0012 #include "replacedialog.hpp"
0013 #include "replaceprompt.hpp"
0014 #include "replacetool.hpp"
0015 // KF
0016 #include <KXmlGuiWindow>
0017 #include <KLocalizedString>
0018 #include <KActionCollection>
0019 #include <KStandardAction>
0020 #include <KMessageBox>
0021 // Qt
0022 #include <QAction>
0023 
0024 namespace Kasten {
0025 
0026 // TODO: for docked widgets signal widgets if embedded or floating, if horizontal/vertical
0027 ReplaceController::ReplaceController(KXMLGUIClient* guiClient, QWidget* parentWidget)
0028     : mParentWidget(parentWidget)
0029 {
0030     mReplaceAction = KStandardAction::replace(this, &ReplaceController::replace, this);
0031 
0032     guiClient->actionCollection()->addAction(mReplaceAction->objectName(), mReplaceAction);
0033 
0034     mTool = new ReplaceTool();
0035     mTool->setUserQueryAgent(this);
0036 
0037     connect(mTool, &ReplaceTool::isApplyableChanged,
0038             mReplaceAction, &QAction::setEnabled);
0039 
0040     connect(mTool, &ReplaceTool::finished, this, &ReplaceController::onFinished);
0041 
0042     mReplaceAction->setEnabled(false);
0043 }
0044 
0045 ReplaceController::~ReplaceController()
0046 {
0047     delete mReplaceDialog;
0048     delete mReplacePrompt;
0049     delete mTool;
0050 }
0051 
0052 void ReplaceController::setTargetModel(AbstractModel* model)
0053 {
0054     mTool->setTargetModel(model);
0055 }
0056 
0057 void ReplaceController::replace()
0058 {
0059     // ensure dialog
0060     if (!mReplaceDialog) {
0061         mReplaceDialog = new ReplaceDialog(mTool, mParentWidget);
0062     }
0063 
0064     mReplaceDialog->show();
0065 }
0066 
0067 void ReplaceController::onFinished(bool previousFound, int noOfReplacements)
0068 {
0069     if (mReplacePrompt) {
0070         mReplacePrompt->hide();
0071     }
0072 
0073     const QString messageBoxTitle = i18nc("@title:window", "Replace");
0074     const QString replacementReport = (noOfReplacements == 0) ?
0075                                       i18nc("@info", "No replacements made.") :
0076                                       i18ncp("@info", "1 replacement made.", "%1 replacements made.", noOfReplacements);
0077 
0078     if (!previousFound) {
0079         KMessageBox::information(mParentWidget, i18nc("@info", "Replace pattern not found in byte array."), messageBoxTitle);
0080     } else {
0081         KMessageBox::information(mParentWidget, replacementReport, messageBoxTitle);
0082     }
0083 }
0084 
0085 void ReplaceController::queryContinue(FindDirection direction, int noOfReplacements)
0086 {
0087     const QString messageBoxTitle = i18nc("@title:window", "Replace");
0088     const QString replacementReport = (noOfReplacements == 0) ?
0089                                       i18nc("@info", "No replacements made.") :
0090                                       i18ncp("@info", "1 replacement made.", "%1 replacements made.", noOfReplacements);
0091     const QString question = (direction == FindForward) ?
0092                              xi18nc("@info", "End of byte array reached.<nl/>Continue from the beginning?") :
0093                              xi18nc("@info", "Beginning of byte array reached.<nl/>Continue from the end?");
0094 
0095     const QString message = replacementReport + QLatin1String("<br /><br />") + question;
0096     const int answer = KMessageBox::questionTwoActions(mParentWidget,
0097                                                        message, messageBoxTitle,
0098                                                        KStandardGuiItem::cont(), KStandardGuiItem::cancel());
0099 
0100     const bool result = (answer != KMessageBox::SecondaryAction);
0101 
0102     Q_EMIT queryContinueFinished(result);
0103 }
0104 
0105 void ReplaceController::queryReplaceCurrent()
0106 {
0107     if (!mReplacePrompt) {
0108         mReplacePrompt = new ReplacePrompt(mParentWidget);
0109         connect(mReplacePrompt, &ReplacePrompt::finished,
0110                 this, &ReplaceController::onPromptReply);
0111     }
0112     mReplacePrompt->show();
0113 }
0114 
0115 void ReplaceController::onPromptReply(ReplaceBehaviour behaviour)
0116 {
0117     if (behaviour == ReplaceAll || behaviour == CancelReplacing) {
0118         mReplacePrompt->hide();
0119     }
0120 
0121     Q_EMIT queryReplaceCurrentFinished(behaviour);
0122 }
0123 
0124 }
0125 
0126 #include "moc_replacecontroller.cpp"