Debian Codegraph @

kysdk-qtwidgets/src/ktextedit.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhenyu Wang <[email protected]>
20 *
21 */
22
23 #include "ktextedit.h"
24 #include "ktag.h"
25 #include "themeController.h"
26 #include <QDebug>
27 #include <QHBoxLayout>
28 #include <QPaintEvent>
29 #include <QPainter>
30 #include <QPainterPath>
31 #include <QProxyStyle>
32 #include <QTextCursor>
33 #include <QTimer>
34 #include "accessinfohelper.h"
35
36 namespace kdk
37 {
38
39 class KTextEditPrivate : public QObject, public ThemeController
40 {
41 Q_OBJECT
42 Q_DECLARE_PUBLIC(KTextEdit)
43 public:
44 KTextEditPrivate(KTextEdit *parent);
45
46 protected:
47 void changeTheme();
48
49 private:
50 KTextEdit *q_ptr;
51 KLabel *m_pLabel;
52 QTextBlockFormat m_blockFormat;
53 QTextCursor *m_pTextCursor;
54 };
55
56 KTextEdit::KTextEdit(QWidget *parent)
57 : QTextEdit(parent)
58 , d_ptr(new KTextEditPrivate(this))
59 {
60 Q_D(KTextEdit);
61 viewport()->setContentsMargins(0, 0, 0, 0);
62 setFrameShape(QFrame::NoFrame);
63 }
64
65 void KTextEdit::setLabelVisible(bool flag)
66 {
67 Q_D(KTextEdit);
68 d->m_pLabel->setVisible(flag);
69 }
70
71 void KTextEdit::setPixmap(const QPixmap &pixmap)
72 {
73 Q_D(KTextEdit);
74 d->m_pLabel->setPixmap(pixmap);
75 }
76
77 const QPixmap *KTextEdit::pixmap() const
78 {
79 Q_D(const KTextEdit);
80 return d->m_pLabel->pixmap();
81 }
82
83 void KTextEdit::setLabelText(const QString &text)
84 {
85 Q_D(KTextEdit);
86 d->m_pLabel->setText(text);
87 }
88
89 void KTextEdit::setReadOnly(bool ro)
90 {
91 Q_D(KTextEdit);
92 QTextEdit::setReadOnly(ro);
93 if (isReadOnly()) {
94 QPalette pale = palette();
95 pale.setColor(QPalette::Base, ThemeController::getCustomColorFromDT("highlight-active"));
96 pale.setColor(QPalette::Text, Qt::white);
97 setPalette(pale);
98 d->m_pLabel->setBackgroundColor(true, Qt::white);
99 } else {
100 QPalette pale;
101 setPalette(pale);
102 d->m_pLabel->setBackgroundColor(false);
103 }
104 }
105
106 void KTextEdit::paintEvent(QPaintEvent *event)
107 {
108 Q_D(KTextEdit);
109 QTextEdit::paintEvent(event);
110
111 if (d->m_pLabel->isVisible()) {
112 d->m_blockFormat.setTextIndent(d->m_pLabel->width() + 4);
113 d->m_blockFormat.setLineHeight(3, QTextBlockFormat::LineDistanceHeight);
114 d->m_pTextCursor->setBlockFormat(d->m_blockFormat);
115 } else {
116 d->m_blockFormat.setTextIndent(0);
117 d->m_blockFormat.setLineHeight(3, QTextBlockFormat::LineDistanceHeight);
118 d->m_pTextCursor->setBlockFormat(d->m_blockFormat);
119 }
120 }
121
122 KTextEditPrivate::KTextEditPrivate(KTextEdit *parent)
123 : q_ptr(parent)
124 {
125 Q_Q(KTextEdit);
126 m_pLabel = new KLabel(q);
127 m_pLabel->setAlignment(Qt::AlignCenter);
128 m_pLabel->setFixedHeight(30);
129 m_pLabel->setLabelType(KLabelType::DataType);
130 // 调整位置使其不遮盖边框
131 m_pLabel->move(2, 2);
132 m_pLabel->setVisible(false);
133 m_pLabel->setDataHightColor(true);
134 m_pLabel->setFixedWidth(70);
135 m_pTextCursor = new QTextCursor(q->textCursor());
136 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
137 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
138 {
139 changeTheme();
140 }
141 if(key == "systemFontSize" || key == "systemFont")
142 {
143 m_pLabel->setFixedWidth(q->fontMetrics().horizontalAdvance(m_pLabel->text()) + m_pLabel->pixmap()->width() + 12);
144 }
145 });
146
147 KDK_ALL_INFO_FORMAT(m_pLabel,"");
148 }
149
150 void KTextEditPrivate::changeTheme()
151 {
152 Q_Q(KTextEdit);
153 initThemeStyle();
154 }
155
156 }
157
158 #include "ktextedit.moc"
159 #include "moc_ktextedit.cpp"