Debian Codegraph @

kysdk-qtwidgets/src/ktexteditor.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2024, 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 "ktexteditor.h"
24 #include "themeController.h"
25
26 #include <QBoxLayout>
27 #include <QDebug>
28 #include <QEvent>
29 #include <QPainter>
30 #include <QScrollArea>
31 #include <QScrollBar>
32 #include <QTextEdit>
33
34 namespace kdk
35 {
36
37 class KTextEditorPrivate : public QObject, public ThemeController
38 {
39 Q_OBJECT
40 Q_DECLARE_PUBLIC(KTextEditor)
41 public:
42 KTextEditorPrivate(KTextEditor *parent);
43 virtual ~KTextEditorPrivate();
44
45 private:
46 KTextEditor *q_ptr;
47 QVBoxLayout *m_pVBoxLayout;
48 QScrollArea *m_pScrollArea;
49 QTextEdit *m_pTextEdit;
50 bool m_focus;
51 };
52
53 KTextEditor::KTextEditor(QWidget *parent)
54 : QWidget(parent)
55 , d_ptr(new KTextEditorPrivate(this))
56 {
57 Q_D(KTextEditor);
58 this->setFocusPolicy(Qt::ClickFocus);
59 installEventFilter(this);
60 d->m_pTextEdit->installEventFilter(this);
61 d->m_pScrollArea->installEventFilter(this);
62 }
63
64 QTextEdit *KTextEditor::textEdit()
65 {
66 Q_D(KTextEditor);
67 return d->m_pTextEdit;
68 }
69
70 void KTextEditor::paintEvent(QPaintEvent *event)
71 {
72 Q_D(KTextEditor);
73 QPainter painter(this);
74 painter.setRenderHint(QPainter::Antialiasing);
75 painter.fillRect(this->rect(), ThemeController::getCustomColorFromDT("window-active"));
76 QPen pen;
77 pen.setWidth(2);
78 if (d->m_focus) {
79 pen.setColor(ThemeController::getCustomColorFromDT("highlight-active"));
80 painter.setPen(pen);
81 } else {
82 pen.setColor(ThemeController::getCustomColorFromDT("placeholdertext-active"));
83 painter.setPen(pen);
84 }
85
86 painter.setBrush(ThemeController::getCustomColorFromDT("base-active"));
87 QRect rect = this->rect().adjusted(1, 1, -1, -1);
88 int radius = ThemeController::getRadiusFromDT("kradius-menu");
89 if (radius == -1)
90 radius = 8;
91 painter.drawRoundedRect(rect, radius, radius);
92 }
93
94 bool KTextEditor::eventFilter(QObject *watched, QEvent *event)
95 {
96 Q_D(KTextEditor);
97 if (watched == this) {
98 switch (event->type()) {
99 case QEvent::FocusIn:
100 d->m_focus = true;
101 update();
102 break;
103 case QEvent::FocusOut:
104 d->m_focus = false;
105 update();
106 break;
107 default:
108 break;
109 }
110 return QWidget::eventFilter(watched, event);
111 } else {
112 switch (event->type()) {
113 case QEvent::FocusIn:
114 d->m_focus = true;
115 update();
116 break;
117 case QEvent::FocusOut:
118 d->m_focus = false;
119 update();
120 break;
121 default:
122 break;
123 }
124 return false;
125 }
126 }
127
128 KTextEditorPrivate::KTextEditorPrivate(KTextEditor *parent)
129 : q_ptr(parent)
130 {
131 Q_Q(KTextEditor);
132
133 m_pVBoxLayout = new QVBoxLayout();
134 m_pScrollArea = new QScrollArea();
135 m_pTextEdit = new QTextEdit();
136 m_focus = false;
137
138 q->setContentsMargins(8, 12, 0, 13); // 上下左右边距为20 ,因控件自带边距调整
139 m_pScrollArea->setWidget(m_pTextEdit);
140 m_pScrollArea->horizontalScrollBar()->setVisible(false);
141 m_pScrollArea->setWidgetResizable(true);
142 m_pScrollArea->setFocusPolicy(Qt::ClickFocus);
143
144 m_pTextEdit->setFrameShape(QFrame::NoFrame);
145 m_pScrollArea->setFrameShape(QFrame::NoFrame);
146
147 m_pVBoxLayout->addWidget(m_pScrollArea);
148
149 q->setLayout(m_pVBoxLayout);
150 }
151
152 KTextEditorPrivate::~KTextEditorPrivate()
153 {
154 }
155
156 }
157
158 #include "ktexteditor.moc"
159 #include "moc_ktexteditor.cpp"