Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qmlformpreviewview.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
"qmlformpreviewview.h"
5
6
#
include
"messagemodel.h"
7
8
#
include
<
QDir
>
9
#
include
<
QQuickWidget
>
10
#
include
<
QApplication
>
11
#
include
<
QVBoxLayout
>
12
#
include
<
QQuickItem
>
13
#
include
<
QHash
>
14
#
include
<
QMessageBox
>
15
16
using
namespace
Qt::Literals::StringLiterals;
17
18
namespace
{
19
20
void
traverseQml(QObject *obj, QHash<QString, QList<QObject *>> &targets)
21
{
22
if
(obj) {
23
if
(QVariant text = obj->property(
"text"
); text.isValid()) {
24
if
(
auto
itr = targets.find(text.toString()); itr != targets.end()) {
25
itr->append(obj);
26
return
;
27
}
28
}
29
for
(QObject *child : obj->children())
30
traverseQml(child, targets);
31
}
32
}
33
34
void
matchSources(QQuickItem *root, QHash<QString, QList<QObject *>> &targets)
35
{
36
traverseQml(root, targets);
37
}
38
39
QHash<QString, QList<QObject *>> extractSources(DataModel *m,
const
QString &contextName)
40
{
41
QHash<QString, QList<QObject *>> t;
42
GroupItem *ctx = m->findGroup(contextName, TEXTBASED);
43
if
(ctx)
44
for
(
int
j = 0; j < ctx->messageCount(); j++)
45
t[ctx->messageItem(j)->text()] = {};
46
47
for
(DataModelIterator it(IDBASED, m); it.isValid(); ++it)
48
t[it.current()->text()] = {};
49
return
t;
50
}
51
}
// namespace
52
53
QT_BEGIN_NAMESPACE
54
55
QmlFormPreviewView::QmlFormPreviewView(MultiDataModel *dataModel)
56
: QQuickWidget(0), m_dataModel(dataModel) {}
57
58
bool
QmlFormPreviewView
::
setSourceContext
(
int
model,
MessageItem
*messageItem)
59
{
60
if
(model < 0 || !messageItem) {
61
m_lastModel = -1;
62
return
true
;
63
}
64
const
QDir dir = QFileInfo(m_dataModel->srcFileName(model)).dir();
65
const
QString fileName = QDir::cleanPath(dir.absoluteFilePath(messageItem->fileName()));
66
if
(m_lastFormName != fileName) {
67
m_lastFormName = fileName;
68
69
setAttribute(Qt::WA_TransparentForMouseEvents, !fileName.endsWith(
".ui.qml"_L1
));
70
71
setSource(QUrl::fromLocalFile(fileName));
72
if
(!errors().empty()) {
73
m_lastError =
true
;
74
return
false
;
75
}
76
77
m_targets = extractSources(m_dataModel->model(model), messageItem->context());
78
matchSources(rootObject(), m_targets);
79
80
setResizeMode(SizeViewToRootObject);
81
show();
82
}
else
if
(m_lastError) {
83
return
false
;
84
}
85
if
(m_lastModel != model) {
86
for
(
DataModelIterator
it
(
IDBASED
,
m_dataModel
->
model
(
model
)
)
; it
.
isValid
(
)
;
++
it) {
87
MessageItem
*message = it
.
current
(
)
;
88
for
(QObject *item : std::as_const(m_targets[message->text()]))
89
item->setProperty(
"text"
, message->translation());
90
}
91
for
(
DataModelIterator
it
(
TEXTBASED
,
m_dataModel
->
model
(
model
)
)
; it
.
isValid
(
)
;
++
it) {
92
MessageItem
*message = it
.
current
(
)
;
93
for
(QObject *item : std::as_const(m_targets[message->text()]))
94
item->setProperty(
"text"
, message->translation());
95
}
96
m_lastModel = model;
97
}
else
{
98
for
(QObject *item : std::as_const(m_targets[messageItem->text()]))
99
item->setProperty(
"text"
, messageItem->translation());
100
}
101
m_lastError =
false
;
102
return
true
;
103
}
104
105
QT_END_NAMESPACE
DataModelIterator
Definition
messagemodel.h:141
DataModelIterator::isValid
bool isValid() const
Definition
messagemodel.cpp:629
DataModelIterator::DataModelIterator
DataModelIterator(TranslationType type, const DataModel *model=0, int groupNo=0, int messageNo=0)
Definition
messagemodel.cpp:623
DataModelIterator::current
MessageItem * current() const
Definition
messagemodel.cpp:647
DataModelIterator::operator++
void operator++()
Definition
messagemodel.cpp:636
MessageItem
Definition
messagemodel.h:23
MultiDataModel::model
DataModel * model(int i)
Definition
messagemodel.h:422
QmlFormPreviewView
Definition
qmlformpreviewview.h:16
QmlFormPreviewView::setSourceContext
bool setSourceContext(int model, MessageItem *messageItem)
Definition
qmlformpreviewview.cpp:58
IDBASED
@ IDBASED
Definition
messagemodel.h:67
TEXTBASED
@ TEXTBASED
Definition
messagemodel.h:67
qttools
src
linguist
linguist
qmlformpreviewview.cpp
Generated on
for Qt by
1.18.0