File indexing completed on 2024-12-29 04:54:49

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "sievetexteditwidget.h"
0008 #include "editor/sievetextedit.h"
0009 #include <QVBoxLayout>
0010 
0011 #include <TextAddonsWidgets/SlideContainer>
0012 #include <TextCustomEditor/PlainTextEditFindBar>
0013 using namespace KSieveUi;
0014 
0015 class KSieveUi::SieveTextEditWidgetPrivate
0016 {
0017 public:
0018     SieveTextEditWidgetPrivate() = default;
0019 
0020     KSieveUi::SieveTextEdit *mTextEdit = nullptr;
0021     TextAddonsWidgets::SlideContainer *mSliderContainer = nullptr;
0022     TextCustomEditor::PlainTextEditFindBar *mFindBar = nullptr;
0023 };
0024 
0025 SieveTextEditWidget::SieveTextEditWidget(KSieveUi::SieveTextEdit *customTextEdit, QWidget *parent)
0026     : QWidget(parent)
0027     , d(new KSieveUi::SieveTextEditWidgetPrivate)
0028 {
0029     initialize(customTextEdit);
0030 }
0031 
0032 SieveTextEditWidget::SieveTextEditWidget(QWidget *parent)
0033     : QWidget(parent)
0034     , d(new KSieveUi::SieveTextEditWidgetPrivate)
0035 {
0036     initialize();
0037 }
0038 
0039 SieveTextEditWidget::~SieveTextEditWidget() = default;
0040 
0041 void SieveTextEditWidget::initialize(KSieveUi::SieveTextEdit *custom)
0042 {
0043     auto mainLayout = new QVBoxLayout(this);
0044     mainLayout->setContentsMargins({});
0045     if (custom) {
0046         d->mTextEdit = custom;
0047     } else {
0048         d->mTextEdit = new KSieveUi::SieveTextEdit;
0049     }
0050     d->mTextEdit->setObjectName(QLatin1StringView("textedit"));
0051     d->mTextEdit->setShowHelpMenu(false);
0052     mainLayout->addWidget(d->mTextEdit);
0053 
0054     d->mSliderContainer = new TextAddonsWidgets::SlideContainer(this);
0055     d->mSliderContainer->setObjectName(QLatin1StringView("slidercontainer"));
0056     d->mFindBar = new TextCustomEditor::PlainTextEditFindBar(d->mTextEdit, this);
0057     d->mFindBar->setObjectName(QLatin1StringView("findbar"));
0058     d->mFindBar->setHideWhenClose(false);
0059     connect(d->mFindBar, &TextCustomEditor::TextEditFindBarBase::hideFindBar, d->mSliderContainer, &TextAddonsWidgets::SlideContainer::slideOut);
0060     d->mSliderContainer->setContent(d->mFindBar);
0061     mainLayout->addWidget(d->mSliderContainer);
0062     connect(d->mTextEdit, &SieveTextEdit::findText, this, &SieveTextEditWidget::slotFind);
0063     connect(d->mTextEdit, &SieveTextEdit::replaceText, this, &SieveTextEditWidget::slotReplace);
0064 }
0065 
0066 void SieveTextEditWidget::setReadOnly(bool readOnly)
0067 {
0068     d->mTextEdit->setReadOnly(readOnly);
0069 }
0070 
0071 void SieveTextEditWidget::slotReplace()
0072 {
0073     if (d->mTextEdit->textCursor().hasSelection()) {
0074         d->mFindBar->setText(d->mTextEdit->textCursor().selectedText());
0075     }
0076     d->mFindBar->showReplace();
0077     d->mSliderContainer->slideIn();
0078     d->mFindBar->focusAndSetCursor();
0079 }
0080 
0081 void SieveTextEditWidget::slotFind()
0082 {
0083     if (d->mTextEdit->textCursor().hasSelection()) {
0084         d->mFindBar->setText(d->mTextEdit->textCursor().selectedText());
0085     }
0086     d->mTextEdit->moveCursor(QTextCursor::Start);
0087     d->mFindBar->showFind();
0088     d->mSliderContainer->slideIn();
0089     d->mFindBar->focusAndSetCursor();
0090 }
0091 
0092 SieveTextEdit *SieveTextEditWidget::textEdit() const
0093 {
0094     return d->mTextEdit;
0095 }
0096 
0097 #include "moc_sievetexteditwidget.cpp"