File indexing completed on 2025-04-27 03:58:40

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2022-08-01
0007  * Description : Text edit widgets with spellcheck support and edition limitations.
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dtextedit_p.h"
0016 
0017 namespace Digikam
0018 {
0019 
0020 DTextEditClearButton::DTextEditClearButton(QWidget* const parent)
0021     : QLabel(parent)
0022 {
0023     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
0024     setFocusPolicy(Qt::NoFocus);
0025     setContentsMargins(QMargins());
0026     setScaledContents(false);
0027     setMouseTracking(false);
0028     QIcon ico = qApp->style()->standardIcon(QStyle::SP_LineEditClearButton);
0029     int s     = qApp->style()->pixelMetric(QStyle::PM_SliderLength) -
0030                 qApp->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
0031     setPixmap(ico.pixmap(QSize(s, s)));
0032 }
0033 
0034 void DTextEditClearButton::mousePressEvent(QMouseEvent* e)
0035 {
0036     Q_EMIT clicked();
0037     QLabel::mousePressEvent(e);
0038 }
0039 
0040 // -------------------------------------------------------------------------------
0041 
0042 DTextEdit::Private::Private()
0043 {
0044 }
0045 
0046 void DTextEdit::Private::init(DTextEdit* const parent)
0047 {
0048 
0049 #ifdef HAVE_SONNET
0050 
0051     spellChecker                   = new SpellCheckDecorator(parent);
0052 
0053     // Auto-detect language enabled by default.
0054 
0055     spellChecker->highlighter()->setAutoDetectLanguageDisabled(false);
0056 
0057     LocalizeSettings* const config = LocalizeSettings::instance();
0058 
0059     if (config)
0060     {
0061         parent->setLocalizeSettings(config->settings());
0062 
0063         QObject::connect(config, &LocalizeSettings::signalSettingsChanged,
0064                          parent, [=]()
0065             {
0066                 parent->setLocalizeSettings(config->settings());
0067             }
0068         );
0069     }
0070 
0071 #endif
0072 
0073     parent->setLinesVisible(lines);
0074     parent->setTabChangesFocus(true);
0075 
0076     clrBtn = new DTextEditClearButton(parent);
0077     parent->setCornerWidget(clrBtn);
0078 
0079     connect(clrBtn, &DTextEditClearButton::clicked,
0080             parent, &QTextEdit::clear);
0081 
0082     // Mimic QLineEdit
0083 
0084     QObject::connect(parent, &QTextEdit::textChanged,
0085                      parent, [=]()
0086         {
0087             if (clearBtnEnable && (lines == 1))
0088             {
0089                 clrBtn->setVisible(!parent->text().isEmpty());
0090             }
0091         }
0092     );
0093 
0094     QObject::connect(parent, &QTextEdit::textChanged,
0095                      parent, &DTextEdit::slotChanged);
0096 }
0097 
0098 // ------------------------------------------------------------------------------------------------
0099 
0100 DPlainTextEdit::Private::Private()
0101 {
0102 }
0103 
0104 void DPlainTextEdit::Private::init(DPlainTextEdit* const parent)
0105 {
0106 
0107 #ifdef HAVE_SONNET
0108 
0109     spellChecker                   = new SpellCheckDecorator(parent);
0110 
0111     // Auto-detect language enabled by default.
0112 
0113     spellChecker->highlighter()->setAutoDetectLanguageDisabled(false);
0114 
0115     LocalizeSettings* const config = LocalizeSettings::instance();
0116 
0117     if (config)
0118     {
0119         parent->setLocalizeSettings(config->settings());
0120 
0121         QObject::connect(config, &LocalizeSettings::signalSettingsChanged,
0122                          parent, [=]()
0123             {
0124                 parent->setLocalizeSettings(config->settings());
0125             }
0126         );
0127     }
0128 
0129 #endif
0130 
0131     parent->setLinesVisible(lines);
0132 
0133     clrBtn = new DTextEditClearButton(parent);
0134     parent->setCornerWidget(clrBtn);
0135 
0136     connect(clrBtn, &DTextEditClearButton::clicked,
0137             parent, &QPlainTextEdit::clear);
0138 
0139     // Mimic QLineEdit
0140 
0141     QObject::connect(parent, &QPlainTextEdit::textChanged,
0142                      parent, [=]()
0143         {
0144             if (clearBtnEnable && (lines == 1))
0145             {
0146                 clrBtn->setVisible(!parent->text().isEmpty());
0147             }
0148         }
0149     );
0150 
0151     QObject::connect(parent, &QPlainTextEdit::textChanged,
0152                      parent, &DPlainTextEdit::slotChanged);
0153 }
0154 
0155 } // namespace Digikam
0156 
0157 #include "moc_dtextedit_p.cpp"