Skip to content

Commit 8d0c792

Browse files
committed
feat: Add FluPinyin, base on pinyin-pro
1 parent de5e456 commit 8d0c792

File tree

17 files changed

+50851
-14
lines changed

17 files changed

+50851
-14
lines changed

THIRD_PARTY_COPYRIGHT.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,3 +1024,26 @@ the library. If this is what you want to do, use the GNU Lesser General
10241024
Public License instead of this License. But first, please read
10251025
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
10261026

1027+
************************************************************************************
1028+
MIT License
1029+
1030+
Copyright (c) 2022-present zh-lx
1031+
1032+
Permission is hereby granted, free of charge, to any person obtaining a copy
1033+
of this software and associated documentation files (the "Software"), to deal
1034+
in the Software without restriction, including without limitation the rights
1035+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1036+
copies of the Software, and to permit persons to whom the Software is
1037+
furnished to do so, subject to the following conditions:
1038+
1039+
The above copyright notice and this permission notice shall be included in all
1040+
copies or substantial portions of the Software.
1041+
1042+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1043+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1044+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1045+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1046+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1047+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1048+
SOFTWARE.
1049+

example/example.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<file>res/image/logo_pro.png</file>
215215
<file>qml/page/T_FluentPro.qml</file>
216216
<file>qml/page/T_BubbleBox.qml</file>
217+
<file>qml/page/T_Pinyin.qml</file>
217218
</qresource>
218219
<qresource prefix="/"/>
219220
</RCC>

example/example_en_US.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,26 +566,31 @@
566566
</message>
567567
<message>
568568
<location filename="qml/global/ItemsOriginal.qml" line="466"/>
569-
<source>Captcha</source>
569+
<source>Pinyin</source>
570570
<translation type="unfinished"></translation>
571571
</message>
572572
<message>
573573
<location filename="qml/global/ItemsOriginal.qml" line="472"/>
574+
<source>Captcha</source>
575+
<translation type="unfinished"></translation>
576+
</message>
577+
<message>
578+
<location filename="qml/global/ItemsOriginal.qml" line="478"/>
574579
<source>Network</source>
575580
<translation type="unfinished"></translation>
576581
</message>
577582
<message>
578-
<location filename="qml/global/ItemsOriginal.qml" line="479"/>
583+
<location filename="qml/global/ItemsOriginal.qml" line="485"/>
579584
<source>Remote Loader</source>
580585
<translation type="unfinished"></translation>
581586
</message>
582587
<message>
583-
<location filename="qml/global/ItemsOriginal.qml" line="493"/>
588+
<location filename="qml/global/ItemsOriginal.qml" line="499"/>
584589
<source>Hot Loader</source>
585590
<translation type="unfinished"></translation>
586591
</message>
587592
<message>
588-
<location filename="qml/global/ItemsOriginal.qml" line="499"/>
593+
<location filename="qml/global/ItemsOriginal.qml" line="505"/>
589594
<source>Test Crash</source>
590595
<translation type="unfinished"></translation>
591596
</message>
@@ -1932,6 +1937,24 @@ My only desire is to be permitted to drive out the traitors and restore the Han.
19321937
<translation type="unfinished"></translation>
19331938
</message>
19341939
</context>
1940+
<context>
1941+
<name>T_Pinyin</name>
1942+
<message>
1943+
<location filename="qml/page/T_Pinyin.qml" line="10"/>
1944+
<source>Pinyin</source>
1945+
<translation type="unfinished"></translation>
1946+
</message>
1947+
<message>
1948+
<location filename="qml/page/T_Pinyin.qml" line="32"/>
1949+
<source>[pinyin-pro document](https://pinyin-pro.cn/guide/compare.html)</source>
1950+
<translation type="unfinished"></translation>
1951+
</message>
1952+
<message>
1953+
<location filename="qml/page/T_Pinyin.qml" line="74"/>
1954+
<source>Input Chinese or Pinyin</source>
1955+
<translation type="unfinished"></translation>
1956+
</message>
1957+
</context>
19351958
<context>
19361959
<name>T_Pivot</name>
19371960
<message>

example/example_zh_CN.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,21 +566,26 @@
566566
</message>
567567
<message>
568568
<location filename="qml/global/ItemsOriginal.qml" line="466"/>
569+
<source>Pinyin</source>
570+
<translation type="unfinished">拼音</translation>
571+
</message>
572+
<message>
573+
<location filename="qml/global/ItemsOriginal.qml" line="472"/>
569574
<source>Captcha</source>
570575
<translation type="unfinished">验证码</translation>
571576
</message>
572577
<message>
573-
<location filename="qml/global/ItemsOriginal.qml" line="472"/>
578+
<location filename="qml/global/ItemsOriginal.qml" line="478"/>
574579
<source>Network</source>
575580
<translation type="unfinished">网络</translation>
576581
</message>
577582
<message>
578-
<location filename="qml/global/ItemsOriginal.qml" line="479"/>
583+
<location filename="qml/global/ItemsOriginal.qml" line="485"/>
579584
<source>Remote Loader</source>
580585
<translation type="unfinished">远程加载</translation>
581586
</message>
582587
<message>
583-
<location filename="qml/global/ItemsOriginal.qml" line="493"/>
588+
<location filename="qml/global/ItemsOriginal.qml" line="499"/>
584589
<source>Hot Loader</source>
585590
<translation type="unfinished">热加载</translation>
586591
</message>
@@ -589,7 +594,7 @@
589594
<translation type="obsolete">3D</translation>
590595
</message>
591596
<message>
592-
<location filename="qml/global/ItemsOriginal.qml" line="499"/>
597+
<location filename="qml/global/ItemsOriginal.qml" line="505"/>
593598
<source>Test Crash</source>
594599
<translation type="unfinished">测试崩溃</translation>
595600
</message>
@@ -2082,6 +2087,24 @@ My only desire is to be permitted to drive out the traitors and restore the Han.
20822087
<translation type="unfinished">饼图</translation>
20832088
</message>
20842089
</context>
2090+
<context>
2091+
<name>T_Pinyin</name>
2092+
<message>
2093+
<location filename="qml/page/T_Pinyin.qml" line="10"/>
2094+
<source>Pinyin</source>
2095+
<translation type="unfinished">拼音</translation>
2096+
</message>
2097+
<message>
2098+
<location filename="qml/page/T_Pinyin.qml" line="32"/>
2099+
<source>[pinyin-pro document](https://pinyin-pro.cn/guide/compare.html)</source>
2100+
<translation type="unfinished">[pinyin-pro文档](https://pinyin-pro.cn/guide/compare.html)</translation>
2101+
</message>
2102+
<message>
2103+
<location filename="qml/page/T_Pinyin.qml" line="74"/>
2104+
<source>Input Chinese or Pinyin</source>
2105+
<translation type="unfinished">输入汉字或拼音</translation>
2106+
</message>
2107+
</context>
20852108
<context>
20862109
<name>T_Pivot</name>
20872110
<message>

example/qml/global/ItemsOriginal.qml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ FluObject{
462462
url: "qrc:/example/qml/page/T_Timeline.qml"
463463
onTap: { navigationView.push(url) }
464464
}
465+
FluPaneItem{
466+
title: qsTr("Pinyin")
467+
menuDelegate: paneItemMenu
468+
url: "qrc:/example/qml/page/T_Pinyin.qml"
469+
onTap: { navigationView.push(url) }
470+
}
465471
FluPaneItem{
466472
title: qsTr("Captcha")
467473
menuDelegate: paneItemMenu

example/qml/page/T_Pinyin.qml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import QtQuick 2.15
2+
import QtQuick.Layouts 1.15
3+
import QtQuick.Window 2.15
4+
import QtQuick.Controls 2.15
5+
import FluentUI 1.0
6+
import "../component"
7+
8+
FluScrollablePage {
9+
10+
title: qsTr("Pinyin")
11+
12+
Component.onCompleted: {
13+
FluPinyin.buildPattern()
14+
const chineseWords = ["中国", "北京", "上海", "广州", "深圳", "杭州", "成都", "你好", "谢谢", "再见", "请问", "名字", "朋友", "学习", "天气", "今天", "明天", "昨天", "时间", "星期", "早上", "中文", "拼音", "输入法", "计算机", "手机", "电视", "音乐", "旅游", "美食", "餐厅", "医院", "学校", "工作", "工资", "重要", "快乐", "幸福", "爱情", "家庭", "孩子", "父母", "银行", "行长", "长大", "重量", "重新", "重庆", "长城", "苹果", "香蕉", "西瓜", "草莓", "咖啡", "", "啤酒", "汽车", "飞机", "地铁", "自行车", "出租车", "公交", "走路"]
15+
pinyinBox.items = chineseWords.map(text => ({
16+
[pinyinBox.textRole]: text
17+
}))
18+
}
19+
20+
FluText {
21+
textFormat: Text.MarkdownText
22+
onLinkActivated: link => {
23+
Qt.openUrlExternally(link)
24+
}
25+
onLinkHovered: link => {
26+
if (link === "") {
27+
FluTools.restoreOverrideCursor()
28+
} else {
29+
FluTools.setOverrideCursor(Qt.PointingHandCursor)
30+
}
31+
}
32+
text: qsTr('[pinyin-pro document](https://pinyin-pro.cn/guide/compare.html)')
33+
}
34+
35+
FluGroupBox {
36+
Layout.fillWidth: true
37+
padding: 10
38+
title: "pinyin"
39+
ColumnLayout {
40+
FluText {
41+
text: 'FluPinyin.pinyin("汉语拼音") --> %1'.arg(FluPinyin.pinyin("汉语拼音"))
42+
}
43+
FluText {
44+
text: 'FluPinyin.pinyin("汉语拼音", { "toneType": "none" }) --> %1'.arg(FluPinyin.pinyin("汉语拼音", { "toneType": "none" }))
45+
}
46+
FluText {
47+
text: 'FluPinyin.pinyin("汉语拼音", { "toneType": "num" }) --> %1'.arg(FluPinyin.pinyin("汉语拼音", { "toneType": "num" }))
48+
}
49+
FluText {
50+
text: 'FluPinyin.pinyin("汉语拼音", { "type": "array" }) --> %1'.arg(JSON.stringify(FluPinyin.pinyin("汉语拼音", { "type": "array" })))
51+
}
52+
}
53+
}
54+
55+
FluGroupBox {
56+
Layout.topMargin: 20
57+
Layout.fillWidth: true
58+
padding: 10
59+
title: "match"
60+
ColumnLayout {
61+
Layout.fillWidth: true
62+
FluText {
63+
text: 'FluPinyin.match("汉语拼音", "hanyupinyin") --> %1'.arg(JSON.stringify(FluPinyin.match("汉语拼音", "hanpin")))
64+
}
65+
FluText {
66+
text: 'FluPinyin.match("汉语拼音", "hanpin") --> %1'.arg(JSON.stringify(FluPinyin.match("汉语拼音", "hanpin")))
67+
}
68+
FluText {
69+
text: 'FluPinyin.match("汉语拼音", "hanpin", { "continuous": true }) --> %1'.arg(JSON.stringify(FluPinyin.match("汉语拼音", "hanpin", { "continuous": true })))
70+
}
71+
FluAutoSuggestBox {
72+
id: pinyinBox
73+
Layout.topMargin: 10
74+
placeholderText: qsTr("Input Chinese or Pinyin")
75+
showSuggestWhenPressed: true
76+
filter: function (item) {
77+
return length === 0 || FluPinyin.match(item[textRole], text, { "continuous": true })
78+
}
79+
}
80+
}
81+
}
82+
83+
FluGroupBox {
84+
Layout.topMargin: 20
85+
Layout.fillWidth: true
86+
padding: 10
87+
title: "convert"
88+
ColumnLayout {
89+
FluText {
90+
text: 'FluPinyin.convert("pin1 yin1", { "format": "numToSymbol" }) --> %1'.arg(FluPinyin.convert("pin1 yin1", { "format": "numToSymbol" }))
91+
}
92+
FluText {
93+
text: 'FluPinyin.convert("pīn yīn", { "format": "symbolToNum" }) --> %1'.arg(FluPinyin.convert("pīn yīn", { "format": "symbolToNum" }))
94+
}
95+
FluText {
96+
text: 'FluPinyin.convert("pīn yīn", { "format": "toneNone" }) --> %1'.arg(FluPinyin.convert("pīn yīn", { "format": "toneNone" }))
97+
}
98+
}
99+
}
100+
101+
FluGroupBox {
102+
Layout.topMargin: 20
103+
Layout.fillWidth: true
104+
padding: 10
105+
title: "html"
106+
ColumnLayout {
107+
FluText {
108+
text: 'FluPinyin.html("汉语拼音") --> %1'.arg(FluPinyin.html("汉语拼音"))
109+
}
110+
FluText {
111+
text: 'FluPinyin.html("汉语拼音", { "toneType": "none" }) --> %1'.arg(FluPinyin.html("汉语拼音", { "toneType": "none" }))
112+
}
113+
}
114+
}
115+
116+
FluGroupBox {
117+
Layout.topMargin: 20
118+
Layout.fillWidth: true
119+
padding: 10
120+
title: "segment"
121+
ColumnLayout {
122+
FluText {
123+
text: 'FluPinyin.segment("小明硕士毕业于中国科学院计算所,后在日本京都大学深造") --> \n%1'.arg(JSON.stringify(FluPinyin.segment("小明硕士毕业于中国科学院计算所,后在日本京都大学深造"), null, 2))
124+
}
125+
}
126+
}
127+
}

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ if (QT_VERSION VERSION_GREATER_EQUAL "6.2")
104104
endforeach (filepath)
105105

106106
#遍历所有资源文件
107-
file(GLOB_RECURSE RES_PATHS *.png *.jpg *.svg *.ico *.ttf *.webp *.js *.qm)
107+
file(GLOB_RECURSE RES_PATHS *.png *.jpg *.svg *.ico *.ttf *.webp *.js *.mjs *.qm)
108108
foreach (filepath ${RES_PATHS})
109109
if (${filepath} MATCHES "Qt${QT_VERSION_MAJOR}/")
110110
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})

src/FluentUI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ void FluentUI::registerTypes(const char *uri) {
150150
qmlRegisterType(QUrl("qrc:/qt/qml/FluentUI/Controls/FluBubbleBox.qml"), uri, major, minor, "FluBubbleBox");
151151
qmlRegisterSingletonType(QUrl("qrc:/qt/qml/FluentUI/Controls/FluRouter.qml"), uri, major, minor, "FluRouter");
152152
qmlRegisterSingletonType(QUrl("qrc:/qt/qml/FluentUI/Controls/FluEventBus.qml"), uri, major, minor, "FluEventBus");
153+
qmlRegisterSingletonType(QUrl("qrc:/qt/qml/FluentUI/Controls/FluPinyin.qml"), uri, major, minor, "FluPinyin");
153154

154155
qmlRegisterUncreatableMetaObject(FluentIcons::staticMetaObject, uri, major, minor, "FluentIcons", "Access to enums & flags only");
155156
qmlRegisterUncreatableMetaObject(FluThemeType::staticMetaObject, uri, major, minor, "FluThemeType", "Access to enums & flags only");
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
pragma Singleton
2+
3+
import QtQuick 2.15
4+
import "./../JS/PinyinPro.mjs" as Pinyin
5+
6+
QtObject {
7+
property list<QtObject> children
8+
readonly property var context: Pinyin
9+
property int patternValue: 500 // In "size" mode, number of patterns per build; in "group" mode, number of groups to divide patterns into
10+
property string patternBuildMode: "size" // size, group
11+
property alias patternBuildInterval: patternTimer.interval
12+
readonly property alias isPatternBuilt: d.isPatternBuilt
13+
readonly property var outputFormat: Pinyin.OutputFormat
14+
function buildPattern(buildAllAtOnce = true) {
15+
if (d.isPatternBuilt || patternTimer.running) {
16+
return
17+
}
18+
d.builder = context.getPatternsNormalBuilder(patternValue,
19+
patternBuildMode)
20+
if (buildAllAtOnce) {
21+
while (!d.buildNext()) {
22+
23+
}
24+
d.builder = null
25+
d.isPatternBuilt = true
26+
} else {
27+
patternTimer.start()
28+
}
29+
}
30+
function addDict(dict, options) {
31+
context.addDict(dict, options)
32+
}
33+
function clearCustomDict(dict) {
34+
context.clearCustomDict(dict)
35+
}
36+
function convert(pinyin, options) {
37+
return context.convert(pinyin, options)
38+
}
39+
function customPinyin(config = {}, options) {
40+
context.customPinyin(config, options)
41+
}
42+
function html(text, options) {
43+
return context.html(text, options)
44+
}
45+
function match(text, pinyin, options) {
46+
return context.match(text, pinyin, options)
47+
}
48+
function pinyin(word, options) {
49+
return context.pinyin(word, options)
50+
}
51+
function removeDict(dictName) {
52+
context.removeDict(dictName)
53+
}
54+
function segment(word, options) {
55+
return context.segment(word, options)
56+
}
57+
children: [
58+
QtObject {
59+
id: d
60+
property bool isPatternBuilt: false
61+
property var builder: null
62+
function buildNext() {
63+
return d.builder.next().done
64+
}
65+
},
66+
Timer {
67+
id: patternTimer
68+
interval: 1500
69+
repeat: true
70+
triggeredOnStart: true
71+
onTriggered: {
72+
if (d.buildNext()) {
73+
d.builder = null
74+
d.isPatternBuilt = true
75+
stop()
76+
}
77+
}
78+
}
79+
]
80+
}

0 commit comments

Comments
 (0)