Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
texteditor.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
5#include "texteditor.h"
6
7#include <abstractdialoggui_p.h>
8#include <iconselector_p.h>
9#include <stylesheeteditor_p.h>
10#include <richtexteditor_p.h>
11#include <plaintexteditor_p.h>
12
13#include <QtDesigner/abstractformeditor.h>
14
15#include <QtWidgets/qapplication.h>
16#include <QtWidgets/qboxlayout.h>
17#include <QtWidgets/qlabel.h>
18#include <QtWidgets/qmenu.h>
19#include <QtWidgets/qtoolbutton.h>
20
21#include <QtGui/qaction.h>
22
24
25using namespace Qt::StringLiterals;
26
27namespace qdesigner_internal
28{
29
30TextEditor::TextEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
31 QWidget(parent),
32 m_editor(new TextPropertyEditor(this)),
33 m_themeEditor(new IconThemeEditor(this, false)),
34 m_iconThemeModeEnabled(false),
35 m_richTextDefaultFont(QApplication::font()),
36 m_button(new QToolButton(this)),
37 m_menu(new QMenu(this)),
38 m_resourceAction(new QAction(tr("Choose Resource..."), this)),
39 m_fileAction(new QAction(tr("Choose File..."), this)),
40 m_layout(new QHBoxLayout(this)),
41 m_core(core)
42{
43 m_themeEditor->setVisible(false);
44 m_button->setVisible(false);
45
46 m_layout->addWidget(m_editor);
47 m_layout->addWidget(m_themeEditor);
48 m_button->setText(tr("..."));
49 m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
50 m_button->setFixedWidth(20);
51 m_layout->addWidget(m_button);
52 m_layout->setContentsMargins(QMargins());
53 m_layout->setSpacing(0);
54
55 connect(m_resourceAction, &QAction::triggered, this, &TextEditor::resourceActionActivated);
56 connect(m_fileAction, &QAction::triggered, this, &TextEditor::fileActionActivated);
57 connect(m_editor, &TextPropertyEditor::textChanged, this, &TextEditor::textChanged);
58 connect(m_themeEditor, &IconThemeEditor::edited, this, &TextEditor::textChanged);
59 connect(m_button, &QAbstractButton::clicked, this, &TextEditor::buttonClicked);
60
61 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
62 setFocusProxy(m_editor);
63
64 m_menu->addAction(m_resourceAction);
65 m_menu->addAction(m_fileAction);
66}
67
68void TextEditor::setSpacing(int spacing)
69{
70 m_layout->setSpacing(spacing);
71}
72
73void TextEditor::setIconThemeModeEnabled(bool enable)
74{
75 if (m_iconThemeModeEnabled == enable)
76 return; // nothing changes
77 m_iconThemeModeEnabled = enable;
78 m_editor->setVisible(!enable);
79 m_themeEditor->setVisible(enable);
80 if (enable) {
81 m_themeEditor->setTheme(m_editor->text());
82 setFocusProxy(m_themeEditor);
83 } else {
84 m_editor->setText(m_themeEditor->theme());
85 setFocusProxy(m_editor);
86 }
87}
88
90{
91 return m_editor->textPropertyValidationMode();
92}
93
95{
96 m_editor->setTextPropertyValidationMode(vm);
97 if (vm == ValidationURL) {
98 m_button->setMenu(m_menu);
99 m_button->setFixedWidth(30);
100 m_button->setPopupMode(QToolButton::MenuButtonPopup);
101 } else {
102 m_button->setMenu(nullptr);
103 m_button->setFixedWidth(20);
104 m_button->setPopupMode(QToolButton::DelayedPopup);
105 }
106 m_button->setVisible(vm == ValidationStyleSheet || vm == ValidationRichText || vm == ValidationMultiLine || vm == ValidationURL);
107}
108
109void TextEditor::setText(const QString &text)
110{
111 if (m_iconThemeModeEnabled)
112 m_themeEditor->setTheme(text);
113 else
114 m_editor->setText(text);
115}
116
117void TextEditor::buttonClicked()
118{
119 const QString oldText = m_editor->text();
120 QString newText;
123 StyleSheetEditorDialog dlg(m_core, this);
124 dlg.setText(oldText);
125 if (dlg.exec() != QDialog::Accepted)
126 return;
127 newText = dlg.text();
128 }
129 break;
130 case ValidationRichText: {
131 RichTextEditorDialog dlg(m_core, this);
132 dlg.setDefaultFont(m_richTextDefaultFont);
133 dlg.setText(oldText);
134 if (dlg.showDialog() != QDialog::Accepted)
135 return;
136 newText = dlg.text(Qt::AutoText);
137 }
138 break;
139 case ValidationMultiLine: {
140 PlainTextEditorDialog dlg(m_core, this);
141 dlg.setDefaultFont(m_richTextDefaultFont);
142 dlg.setText(oldText);
143 if (dlg.showDialog() != QDialog::Accepted)
144 return;
145 newText = dlg.text();
146 }
147 break;
148 case ValidationURL:
149 if (oldText.isEmpty() || oldText.startsWith("qrc:"_L1))
150 resourceActionActivated();
151 else
152 fileActionActivated();
153 return;
154 default:
155 return;
156 }
157 if (newText != oldText) {
158 m_editor->setText(newText);
159 emit textChanged(newText);
160 }
161}
162
163void TextEditor::resourceActionActivated()
164{
165 QString oldPath = m_editor->text();
166 if (oldPath.startsWith("qrc:"_L1))
167 oldPath.remove(0, 4);
168 // returns ':/file'
169 QString newPath = IconSelector::choosePixmapResource(m_core, m_core->resourceModel(), oldPath, this);
170 if (newPath.startsWith(u':'))
171 newPath.remove(0, 1);
172 if (newPath.isEmpty() || newPath == oldPath)
173 return;
174 const QString newText = "qrc:"_L1 + newPath;
175 m_editor->setText(newText);
176 emit textChanged(newText);
177}
178
179void TextEditor::fileActionActivated()
180{
181 QString oldPath = m_editor->text();
182 if (oldPath.startsWith("file:"_L1))
183 oldPath = oldPath.mid(5);
184 const QString newPath = m_core->dialogGui()->getOpenFileName(this, tr("Choose a File"), oldPath);
185 if (newPath.isEmpty() || newPath == oldPath)
186 return;
187 const QString newText = QUrl::fromLocalFile(newPath).toString();
188 m_editor->setText(newText);
189 emit textChanged(newText);
190}
191
192} // namespace qdesigner_internal
193
194QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:432
TextPropertyValidationMode textPropertyValidationMode() const
void setTextPropertyValidationMode(TextPropertyValidationMode vm)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.