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