1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Designer of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 //
30 //  W A R N I N G
31 //  -------------
32 //
33 // This file is not part of the Qt API.  It exists for the convenience
34 // of Qt Designer.  This header
35 // file may change from version to version without notice, or even be removed.
36 //
37 // We mean it.
38 //
39 
40 #ifndef TEXTPROPERTYEDITOR_H
41 #define TEXTPROPERTYEDITOR_H
42 
43 #include "shared_global_p.h"
44 #include "shared_enums_p.h"
45 
46 #include <QtWidgets/qwidget.h>
47 
48 QT_BEGIN_NAMESPACE
49 
50 namespace qdesigner_internal {
51 
52     class PropertyLineEdit;
53 
54     // Inline-Editor for text properties. Does escaping of newline characters
55     // to '\n' and back and provides validation modes. The interface
56     // corresponds to that of QLineEdit.
57     class QDESIGNER_SHARED_EXPORT TextPropertyEditor : public QWidget
58     {
59         TextPropertyEditor(const TextPropertyEditor &);
60         TextPropertyEditor& operator=(const TextPropertyEditor &);
61         Q_OBJECT
62         Q_PROPERTY(QString text READ text WRITE setText USER true)
63     public:
64         enum EmbeddingMode {
65             // Stand-alone widget
66             EmbeddingNone,
67                 // Disable frame
68                 EmbeddingTreeView,
69                 // For editing in forms
70                 EmbeddingInPlace
71         };
72 
73         enum UpdateMode {
74             // Emit textChanged() as the user types
75             UpdateAsYouType,
76             // Emit textChanged() only when the user finishes (for QUrl, etc.)
77             UpdateOnFinished
78         };
79 
80         explicit TextPropertyEditor(QWidget *parent = nullptr, EmbeddingMode embeddingMode = EmbeddingNone, TextPropertyValidationMode validationMode = ValidationMultiLine);
81 
textPropertyValidationMode()82         TextPropertyValidationMode textPropertyValidationMode() const { return m_validationMode; }
83         void setTextPropertyValidationMode(TextPropertyValidationMode vm);
84 
updateMode()85         UpdateMode updateMode() const                { return m_updateMode; }
setUpdateMode(UpdateMode um)86         void setUpdateMode(UpdateMode um) { m_updateMode = um; }
87 
88         QString text() const;
89 
90         QSize sizeHint () const override;
91         QSize minimumSizeHint () const override;
92 
93         void setAlignment(Qt::Alignment alignment);
94 
95         bool hasAcceptableInput() const;
96 
97         // installs an event filter object on the private QLineEdit
98         void installEventFilter(QObject *filterObject);
99 
100         // Replace newline characters by literal "\n" for inline editing
101         // in mode ValidationMultiLine
102         static QString stringToEditorString(const QString &s, TextPropertyValidationMode validationMode = ValidationMultiLine);
103 
104         // Replace literal "\n"  by actual new lines in mode ValidationMultiLine
105         static QString editorStringToString(const QString &s, TextPropertyValidationMode validationMode = ValidationMultiLine);
106 
107         // Returns whether newline characters are valid in validationMode.
108         static bool multiLine(TextPropertyValidationMode validationMode);
109 
110     signals:
111         void textChanged(const QString &text);
112         void editingFinished();
113 
114     public slots:
115         void setText(const QString &text);
116         void selectAll();
117         void clear();
118 
119     protected:
120         void resizeEvent(QResizeEvent * event ) override;
121 
122     private slots:
123         void slotTextChanged(const QString &text);
124         void slotTextEdited();
125         void slotEditingFinished();
126 
127     private:
128         void setRegularExpressionValidator(const QString &pattern);
129         void markIntermediateState();
130 
131         TextPropertyValidationMode m_validationMode = ValidationSingleLine;
132         UpdateMode m_updateMode = UpdateAsYouType;
133         PropertyLineEdit* m_lineEdit;
134 
135         // Cached text containing real newline characters.
136         QString m_cachedText;
137         bool m_textEdited = false;
138     };
139 }
140 
141 QT_END_NAMESPACE
142 
143 #endif // TEXTPROPERTYEDITOR_H
144