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

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 #ifndef DIGIKAM_DTEXT_EDIT_H
0016 #define DIGIKAM_DTEXT_EDIT_H
0017 
0018 // Qt includes
0019 
0020 #include <QString>
0021 #include <QTextEdit>
0022 #include <QPlainTextEdit>
0023 
0024 // Local includes
0025 
0026 #include "digikam_export.h"
0027 
0028 namespace Digikam
0029 {
0030 
0031 class LocalizeContainer;
0032 
0033 /**
0034  * A text edit widget based on QTextEdit with spell checker capabilities based on Sonnet (optional).
0035  * Widget size can be constrained with the number of visible lines.
0036  * A single line constraint will emulate QLineEdit. See setLinesVisible() for details.
0037  * The maximum number of characters can be limited with setMaxLenght().
0038  * The characters can be limited in editor by setIgnoredCharacters() and setAcceptedCharacters().
0039  * Implementation: dtextedit.cpp
0040  */
0041 class DIGIKAM_EXPORT DTextEdit : public QTextEdit
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046 
0047     /**
0048      * Default constructor.
0049      */
0050     explicit DTextEdit(QWidget* const parent = nullptr);
0051 
0052     /**
0053      * Constructor with a number of lines. Zero lines do not apply a size constraint.
0054      */
0055     explicit DTextEdit(unsigned int lines, QWidget* const parent = nullptr);
0056 
0057     /**
0058      * Constructor with text contents to use.
0059      */
0060     explicit DTextEdit(const QString& contents, QWidget* const parent = nullptr);
0061 
0062     /**
0063      * Standard destructor.
0064      */
0065     ~DTextEdit() override;
0066 
0067     /**
0068      * This property holds whether the edit widget handle text contents as plain text.
0069      * If ignored or accepted characters masks are set, text is filtered accordingly.
0070      */
0071     QString text() const;
0072     void setText(const QString& text);
0073 
0074     /**
0075      * This property holds whether the edit widget displays a clear button when it is not empty.
0076      * If enabled, the edit widget displays a trailing clear button when it contains some text,
0077      * otherwise the edit widget does not show a clear button.
0078      * This option only take effect in QLineEdit emulation mode when lines visible is set to 1.
0079      * See setLinesVisible() for details.
0080      */
0081     bool  isClearButtonEnabled() const;
0082     void  setClearButtonEnabled(bool enable);
0083 
0084     /**
0085      * This property holds whether the edit widget handle the mask of ignored characters in text editor.
0086      * The mask of characters is passed as string (ex: "+/!()").
0087      * By default the mask is empty.
0088      */
0089     QString ignoredCharacters() const;
0090     void setIgnoredCharacters(const QString& mask);
0091 
0092     /**
0093      * This property holds whether the edit widget handle the mask of accepted characters in text editor.
0094      * The mask of characters is passed as string (ex: "abcABC").
0095      * By default the mask is empty.
0096      */
0097     QString acceptedCharacters() const;
0098     void setAcceptedCharacters(const QString& mask);
0099 
0100     /**
0101      * This property holds whether the edit widget handle visible lines used by the widget to show text.
0102      * Lines must be superior or egal to 1 to apply a size constraint.
0103      * Notes: if a single visible line is used, the widget will emulate QLineEdit.
0104      *        a null value do not apply a size constraint.
0105      */
0106     void setLinesVisible(unsigned int lines);
0107     unsigned int linesVisible() const;
0108 
0109     /**
0110      * This property holds whether the edit widget handle a specific spell-checker language (2 letters code based as "en", "fr", "es", etc.).
0111      * If this property is not set, spell-checker will try to auto-detect language by parsing the text.
0112      * To reset this setting, pass a empty string as language.
0113      * If Sonnet depedencies is not resolved, these method do nothing.
0114      */
0115     void setCurrentLanguage(const QString& lang);
0116     QString currentLanguage() const;
0117 
0118     /**
0119      * This property holds whether the edit widget handle the Spellcheck settings.
0120      * See LocalizeContainer class for details.
0121      */
0122     LocalizeContainer spellCheckSettings() const;
0123     void setLocalizeSettings(const LocalizeContainer& settings);
0124 
0125     /**
0126      * This property holds whether the edit widget handle the maximum of characters
0127      * that user can enter in editor.
0128      * By default no limit is set.
0129      * A zero length reset a limit.
0130      */
0131     void setMaxLength(int length);
0132     int  maxLength()     const;
0133 
0134     /**
0135      * Return the left characters that user can enter if a limit have been previously set with setMaxLeght().
0136      */
0137     int leftCharacters() const;
0138 
0139 Q_SIGNALS:
0140 
0141     /**
0142      * Emmited only when mimic QLineEdit mode is enabled. See setLinesVisible() for details.
0143      */
0144     void returnPressed();
0145     void textEdited(const QString&);
0146 
0147 protected:
0148 
0149     void insertFromMimeData(const QMimeData* source) override;
0150     void keyPressEvent(QKeyEvent* e)                 override;
0151 
0152 private Q_SLOTS:
0153 
0154     /**
0155      * Internal slot used to display a tooltips of left characters available when enter text in editor.
0156      * This slot do nothing is no limit is set with setMaxLenght§).
0157      */
0158     void slotChanged();
0159 
0160 private:
0161 
0162     class Private;
0163     Private* const d;
0164 };
0165 
0166 // ---------------------------------------------------------------------------
0167 
0168 /**
0169  * A text edit widget based on QPlainTextEdit with spell checker capabilities based on Sonnet (optional).
0170  * Widget size can be constrained with the number of visible lines.
0171  * A single line constraint will emulate QLineEdit. See setLinesVisible() for details.
0172  * The maximum number of characters can be limited with setMaxLenght().
0173  * The characters can be limited in editor by setIgnoredCharacters() and setAcceptedCharacters().
0174  * Implementation: dplaintextedit.cpp
0175  */
0176 class DIGIKAM_EXPORT DPlainTextEdit : public QPlainTextEdit
0177 {
0178     Q_OBJECT
0179 
0180 public:
0181 
0182     /**
0183      * Default constructor.
0184      */
0185     explicit DPlainTextEdit(QWidget* const parent = nullptr);
0186 
0187     /**
0188      * Constructor with a number of lines. Zero lines do not apply a size constraint.
0189      */
0190     explicit DPlainTextEdit(unsigned int lines, QWidget* const parent = nullptr);
0191 
0192     /**
0193      * Constructor with text contents to use.
0194      */
0195     explicit DPlainTextEdit(const QString& contents, QWidget* const parent = nullptr);
0196 
0197     /**
0198      * Standard destructor.
0199      */
0200     ~DPlainTextEdit() override;
0201 
0202     /**
0203      * This property holds whether the edit widget handle text contents as plain text.
0204      * If ignored or accepted characters masks are set, text is filtered accordingly.
0205      */
0206     QString text() const;
0207     void setText(const QString& text);
0208 
0209     /**
0210      * This property holds whether the edit widget displays a clear button when it is not empty.
0211      * If enabled, the edit widget displays a trailing clear button when it contains some text,
0212      * otherwise the edit widget does not show a clear button.
0213      * This option only take effect in QLineEdit emulation mode when lines visible is set to 1.
0214      * See setLinesVisible() for details.
0215      */
0216     bool  isClearButtonEnabled() const;
0217     void  setClearButtonEnabled(bool enable);
0218 
0219     /**
0220      * This property holds whether the edit widget handle the mask of ignored characters in text editor.
0221      * The mask of characters is passed as string (ex: "+/!()").
0222      * By default the mask is empty.
0223      */
0224     QString ignoredCharacters() const;
0225     void setIgnoredCharacters(const QString& mask);
0226 
0227     /**
0228      * This property holds whether the edit widget handle the mask of accepted characters in text editor.
0229      * The mask of characters is passed as string (ex: "abcABC").
0230      * By default the mask is empty.
0231      */
0232     QString acceptedCharacters() const;
0233     void setAcceptedCharacters(const QString& mask);
0234 
0235     /**
0236      * This property holds whether the edit widget handle visible lines used by the widget to show text.
0237      * Lines must be superior or egal to 1 to apply a size constraint.
0238      * Notes: if a single visible line is used, the widget emulate QLineEdit.
0239      *        a null value do not apply a size constraint.
0240      */
0241     void setLinesVisible(unsigned int lines);
0242     unsigned int linesVisible() const;
0243 
0244     /**
0245      * This property holds whether the edit widget handle a specific spell-checker language (2 letters code based as "en", "fr", "es", etc.).
0246      * If this property is not set, spell-checker will try to auto-detect language by parsing the text.
0247      * To reset this setting, pass a empty string as language.
0248      * If Sonnet depedencies is not resolved, these method do nothing.
0249      */
0250     void setCurrentLanguage(const QString& lang);
0251     QString currentLanguage() const;
0252 
0253     /**
0254      * This property holds whether the edit widget handle the Spellcheck settings.
0255      * See LocalizeContainer class for details.
0256      */
0257     LocalizeContainer spellCheckSettings() const;
0258     void setLocalizeSettings(const LocalizeContainer& settings);
0259 
0260     /**
0261      * This property holds whether the edit widget handle the maximum of characters
0262      * that user can enter in editor.
0263      * By default no limit is set.
0264      * A zero length reset a limit.
0265      */
0266     void setMaxLength(int length);
0267     int  maxLength()     const;
0268 
0269     /**
0270      * Return the left characters that user can enter if a limit have been previously set with setMaxLeght().
0271      */
0272     int leftCharacters() const;
0273 
0274 Q_SIGNALS:
0275 
0276     /**
0277      * Emmited only when mimic QLineEdit mode is enabled. See setLinesVisible() for details.
0278      */
0279     void returnPressed();
0280     void textEdited(const QString&);
0281 
0282 protected:
0283 
0284     void insertFromMimeData(const QMimeData* source) override;
0285     void keyPressEvent(QKeyEvent* e)                 override;
0286 
0287 private Q_SLOTS:
0288 
0289     /**
0290      * Internal slot used to display a tooltips of left characters available when enter text in editor.
0291      * This slot do nothing is no limit is set with setMaxLenght§).
0292      */
0293     void slotChanged();
0294 
0295 private:
0296 
0297     class Private;
0298     Private* const d;
0299 };
0300 
0301 } // namespace Digikam
0302 
0303 #endif // DIGIKAM_LINE_EDIT_SPELL_CHECKER_H