Debian Codegraph @

kysdk-qtwidgets/src/ksearchlineedit.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: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "ksearchlineedit.h"
24 #include "klineframe.h"
25 #include "kshadowhelper.h"
26 #include "ktoolbutton.h"
27 #include "parmscontroller.h"
28 #include "themeController.h"
29 #include <QAbstractItemView>
30 #include <QAction>
31 #include <QApplication>
32 #include <QBoxLayout>
33 #include <QCompleter>
34 #include <QCursor>
35 #include <QDebug>
36 #include <QEvent>
37 #include <QLabel>
38 #include <QLinearGradient>
39 #include <QListView>
40 #include <QMouseEvent>
41 #include <QPainter>
42 #include <QPainterPath>
43 #include <QPalette>
44 #include <QPixmap>
45 #include <QPropertyAnimation>
46 #include <QProxyStyle>
47 #include <QPushButton>
48 #include <QStringListModel>
49 #include <QStyle>
50 #include <QStyleOption>
51 #include <QStyledItemDelegate>
52 #include <QTimer>
53 #include <QToolTip>
54 #include "accessinfohelper.h"
55
56 namespace kdk
57 {
58
59 class MyStyle : public QProxyStyle
60 {
61 public:
62 MyStyle()
63 {
64 }
65 ~MyStyle()
66 {
67 }
68
69 void drawControl(QStyle::ControlElement element,
70 const QStyleOption *option,
71 QPainter *painter,
72 const QWidget *widget = nullptr) const override;
73
74 QRect subElementRect(QStyle::SubElement element,
75 const QStyleOption *option,
76 const QWidget *widget) const override;
77 };
78
79 class CursorProxyStyle : public QProxyStyle
80 {
81 public:
82 CursorProxyStyle()
83 {
84 }
85 ~CursorProxyStyle()
86 {
87 }
88
89 virtual int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const
90 {
91 if (metric == QStyle::PM_TextCursorWidth) {
92 return 0;
93 }
94 return QProxyStyle::pixelMetric(metric, option, widget);
95 }
96 };
97
98 class ListViewDelegate : public QStyledItemDelegate, public ThemeController
99 {
100 public:
101 ListViewDelegate(QObject *parent);
102
103 protected:
104 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
105 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
106 };
107
108 static const int iconMargin = 6;
109 static const int buttonMargin = 6;
110 static const int icontextSpacing = 5;
111
112 class KSearchLineEditPrivate : public QObject, public ThemeController
113 {
114 Q_OBJECT
115 Q_DECLARE_PUBLIC(KSearchLineEdit)
116
117 public:
118 KSearchLineEditPrivate(KSearchLineEdit *parent);
119 void adjustHolderRect();
120 void adjustLayout();
121
122 public Q_SLOTS:
123 void doAnimationFinished();
124 void doEditingFinished();
125
126 protected:
127 void changeTheme();
128
129 private:
130 KSearchLineEdit *q_ptr;
131 QHBoxLayout *m_pWidgetLayout;
132 QWidget *m_pWidget;
133 QLabel *m_pTextLabel;
134 QLabel *m_pIconLabel;
135 QPropertyAnimation *m_pAnimation;
136 bool m_isSearching;
137 QStringListModel *m_pListModel;
138 QStringList m_List;
139 QCompleter *m_pCompleter;
140
141 QString m_placeHolderText;
142 Qt::Alignment m_placeHolderAlignment;
143 bool m_hasFocus;
144 QRect m_holderRect;
145 QRect m_sideRect;
146 ListViewDelegate *m_pListViewDelegate;
147 bool m_isTranslucent;
148 bool m_isTranslucentFlag;
149 bool isTextEmpty;
150 KToolButton *m_pClearButton;
151 KToolButton *m_pCustomButton;
152 KVLineFrame *m_pLineFrame;
153 QWidget *m_pSubWidget;
154 QLinearGradient m_linearGradient;
155 bool m_isClearVisible;
156 bool m_isGradient;
157 bool m_isCustomButtonHover;
158 bool m_isClearHover;
159 bool m_showCursor;
160 bool m_isGradientFlag;
161 QTimer *m_timer;
162 void sltUpdateCursorRect();
163 void init();
164 QString m_systemName;
165 bool m_contextMenu;
166 QGSettings *m_ukuiGsetting;
167 int m_cursorBlinkTime;
168 bool m_isFoucus;
169 };
170
171 KSearchLineEdit::KSearchLineEdit(QWidget *parent)
172 : QLineEdit(parent)
173 , d_ptr(new KSearchLineEditPrivate(this))
174 {
175 Q_D(KSearchLineEdit);
176
177 setFocusPolicy(Qt::ClickFocus);
178 // 应设计师要求,1.2.0.8之后的版本默认显示右键菜单
179 // setContextMenuPolicy(Qt::NoContextMenu);
180 d->init();
181 installEventFilter(this);
182 d->m_pCustomButton->installEventFilter(this);
183 d->m_pClearButton->installEventFilter(this);
184 // d->m_pCompleter->popup()->installEventFilter(this);
185
186 connect(d->m_pAnimation, &QPropertyAnimation::finished, d, &KSearchLineEditPrivate::doAnimationFinished);
187 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
188 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName" || key == "systemFontSize")
189 {
190 d->changeTheme();
191 }
192 });
193 connect(this, &KSearchLineEdit::editingFinished, d, &KSearchLineEditPrivate::doEditingFinished);
194 connect(this, &KSearchLineEdit::returnPressed, this, &KSearchLineEdit::clearFocus);
195 connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() {
196 updateGeometry();
197 });
198
199 connect(this, &QLineEdit::textChanged, this, [=]() {
200 if (!text().isEmpty() && d->m_pClearButton->isEnabled() && hasFocus() && d->m_isClearVisible) {
201 d->m_pClearButton->show();
202 d->adjustLayout();
203 } else {
204 d->m_pClearButton->hide();
205 d->adjustLayout();
206 }
207 });
208 connect(d->m_pCustomButton, &KToolButton::clicked, this, [=] {
209 d->m_isGradient = !d->m_isGradient;
210 if (d->m_isGradientFlag && d->m_isGradient) {
211 d->m_pCustomButton->setIconColor(true, ThemeController::getCustomColorFromDT("highlight-active"));
212 }
213 update();
214 });
215 connect(d->m_pClearButton, &QPushButton::clicked, this, [=]() {
216 this->clear();
217 });
218 connect(d->m_pAnimation, &QPropertyAnimation::finished, this, [=]() {
219 d->sltUpdateCursorRect();
220 if (hasFocus())
221 d->m_timer->start();
222 else
223 d->m_timer->stop();
224 });
225
226 d->changeTheme();
227
228 CursorProxyStyle *style = new CursorProxyStyle();
229 this->setStyle(style);
230 d->m_showCursor = false;
231 d->m_timer = new QTimer(this);
232 if (QGSettings::isSchemaInstalled("org.ukui.style")) {
233 d->m_ukuiGsetting = new QGSettings("org.ukui.style");
234 if (d->m_ukuiGsetting->keys().contains("cursorBlinkTime")) {
235 d->m_cursorBlinkTime = d->m_ukuiGsetting->get("cursorBlinkTime").toInt();
236 if (d->m_cursorBlinkTime == 0) {
237 d->m_timer->stop();
238 d->sltUpdateCursorRect();
239 } else {
240 d->m_timer->setInterval(d->m_cursorBlinkTime / 2);
241 connect(d->m_timer, &QTimer::timeout, this, [=] {
242 d->sltUpdateCursorRect();
243 });
244 }
245 }
246 connect(d->m_ukuiGsetting, &QGSettings::changed, d, [=] {
247 if (d->m_ukuiGsetting->keys().contains("cursorBlinkTime")) {
248 d->m_cursorBlinkTime = d->m_ukuiGsetting->get("cursorBlinkTime").toInt();
249 d->m_timer->setInterval(d->m_cursorBlinkTime / 2);
250 }
251 });
252 }
253 }
254
255 KSearchLineEdit::~KSearchLineEdit()
256 {
257 Q_D(KSearchLineEdit);
258 }
259
260 void KSearchLineEdit::setEnabled(bool enable)
261 {
262 Q_D(KSearchLineEdit);
263 QLineEdit::setEnabled(enable);
264 d->changeTheme();
265 }
266
267 bool KSearchLineEdit::isEnabled()
268 {
269 Q_D(KSearchLineEdit);
270 return QLineEdit::isEnabled();
271 }
272
273 void KSearchLineEdit::setClearButtonEnabled(bool enable)
274 {
275 Q_D(KSearchLineEdit);
276 d->m_isClearVisible = enable;
277 QLineEdit::setClearButtonEnabled(false);
278 }
279
280 bool KSearchLineEdit::isClearButtonEnabled() const
281 {
282 Q_D(const KSearchLineEdit);
283 return d->m_pClearButton->isHidden();
284 // QLineEdit::isClearButtonEnabled();
285 }
286
287 QString KSearchLineEdit::placeholderText() const
288 {
289 Q_D(const KSearchLineEdit);
290 return d->m_placeHolderText;
291 }
292
293 void KSearchLineEdit::setPlaceholderText(const QString &text)
294 {
295 Q_D(KSearchLineEdit);
296 d->m_placeHolderText = text;
297 d->m_pTextLabel->setText(text);
298 }
299
300 Qt::Alignment KSearchLineEdit::placeholderAlignment() const
301 {
302 Q_D(const KSearchLineEdit);
303 return d->m_placeHolderAlignment;
304 }
305
306 void KSearchLineEdit::setPlaceholderAlignment(Qt::Alignment flag)
307 {
308 Q_D(KSearchLineEdit);
309 d->m_placeHolderAlignment = flag;
310 }
311
312 Qt::Alignment KSearchLineEdit::alignment() const
313 {
314 return QLineEdit::alignment();
315 }
316
317 void KSearchLineEdit::setAlignment(Qt::Alignment flag)
318 {
319 Q_D(KSearchLineEdit);
320 QLineEdit::setAlignment(flag);
321 }
322
323 void KSearchLineEdit::setTranslucent(bool flag)
324 {
325 Q_D(KSearchLineEdit);
326 d->m_isTranslucentFlag = flag;
327 d->m_isTranslucent = flag;
328 d->changeTheme();
329 setProperty("needTranslucent", flag);
330 }
331
332 bool KSearchLineEdit::isTranslucent()
333 {
334 Q_D(KSearchLineEdit);
335 return d->m_isTranslucent;
336 }
337
338 void KSearchLineEdit::reloadStyle()
339 {
340 Q_D(KSearchLineEdit);
341 QAbstractItemView *popup = completer()->popup();
342 kdk::effects::KShadowHelper::self()->setWidget(popup);
343
344 popup->setItemDelegate(d->m_pListViewDelegate);
345 MyStyle *style = new MyStyle;
346 popup->setStyle(style);
347 }
348
349 KToolButton *KSearchLineEdit::customButton()
350 {
351 Q_D(KSearchLineEdit);
352 return d->m_pCustomButton;
353 }
354
355 void KSearchLineEdit::setGradation(bool flag)
356 {
357 Q_D(KSearchLineEdit);
358 d->m_isGradientFlag = flag;
359 }
360
361 void KSearchLineEdit::setCustomButtonVisible(bool flag)
362 {
363 Q_D(KSearchLineEdit);
364 d->m_pCustomButton->setVisible(flag);
365 d->m_pLineFrame->setVisible(flag);
366 d->adjustLayout();
367 }
368
369 bool KSearchLineEdit::isCustomButtonVisible()
370 {
371 Q_D(KSearchLineEdit);
372 return d->m_pCustomButton->isVisible();
373 }
374
375 void KSearchLineEdit::clear()
376 {
377 Q_D(KSearchLineEdit);
378
379 if (this->text().isEmpty())
380 return;
381 QLineEdit::clear();
382 d->isTextEmpty = true;
383 if (this->hasFocus()) {
384 d->m_pTextLabel->setVisible(false);
385 d->m_pTextLabel->adjustSize();
386 d->m_pWidget->update();
387 } else {
388 d->m_pTextLabel->setVisible(true);
389 d->m_pTextLabel->adjustSize();
390 d->adjustHolderRect();
391 d->m_pAnimation->setStartValue(d->m_sideRect);
392 d->m_pAnimation->setEndValue(d->m_holderRect);
393 d->m_pAnimation->start();
394 }
395 }
396
397 bool KSearchLineEdit::eventFilter(QObject *watched, QEvent *event)
398 {
399 Q_D(KSearchLineEdit);
400
401 QFont font;
402 font = QApplication::font();
403 QFontMetrics fm(font);
404
405 if (watched == this) {
406 switch (event->type()) {
407 case QEvent::ContextMenu:
408 if (!d->m_contextMenu)
409 d->m_contextMenu = true;
410 break;
411 case QEvent::Show: {
412 d->adjustHolderRect();
413 if (text().isEmpty())
414 d->m_pWidget->setGeometry(d->m_holderRect);
415 else
416 d->m_pWidget->setGeometry(d->m_sideRect);
417 break;
418 }
419 case QEvent::FocusIn: {
420 d->m_isFoucus = true;
421 if (d->m_contextMenu)
422 d->m_contextMenu = false;
423 d->m_pTextLabel->setVisible(false);
424 // 文本为空时获取焦点延时显示光标
425 if (text().isEmpty()) {
426 d->m_showCursor = false;
427 }
428 d->m_isSearching = true;
429 d->m_hasFocus = false;
430 if (!this->text().isEmpty()) {
431 if (d->isTextEmpty) {
432 d->m_pTextLabel->setVisible(false);
433 d->isTextEmpty = false;
434 }
435 } else {
436 if (d->isTextEmpty) {
437 d->adjustHolderRect();
438 d->m_pAnimation->setStartValue(d->m_holderRect);
439 d->m_pAnimation->setEndValue(d->m_sideRect);
440 d->m_pAnimation->start();
441 d->isTextEmpty = false;
442 }
443 d->m_pTextLabel->setVisible(false);
444 }
445 d->adjustLayout();
446 break;
447 }
448 case QEvent::FocusOut: {
449 d->m_isFoucus = false;
450 if (!d->m_contextMenu) {
451 d->m_isSearching = false;
452 d->m_hasFocus = true;
453 if (!this->text().isEmpty()) {
454 d->m_pTextLabel->setVisible(false);
455 } else {
456 d->m_pTextLabel->setVisible(true);
457 d->adjustHolderRect();
458 d->m_pAnimation->setStartValue(d->m_sideRect);
459 d->m_pAnimation->setEndValue(d->m_holderRect);
460 d->m_pAnimation->start();
461 d->isTextEmpty = true;
462 }
463 d->adjustLayout();
464 }
465 break;
466 }
467 case QEvent::HoverEnter:
468 case QEvent::HoverMove: {
469 if (width() - icontextSpacing - d->m_pIconLabel->width() < fm.horizontalAdvance(d->m_placeHolderText)) {
470 if (d->m_hasFocus)
471 setToolTip(d->m_placeHolderText);
472 else
473 setToolTip("");
474 }
475 break;
476 }
477 default:
478 break;
479 }
480 }
481
482 if (watched == d->m_pCustomButton) {
483 if (!d->m_isGradient) {
484 switch (event->type()) {
485 case QEvent::HoverEnter:
486 case QEvent::HoverMove:
487 d->m_pCustomButton->setIconColor(true, ThemeController::getCustomColorFromDT("windowtext-active"));
488 break;
489 case QEvent::Leave:
490 d->m_pCustomButton->setIconColor(true, ThemeController::getCustomColorFromDT("mid-active"));
491 break;
492 default:
493 break;
494 }
495 }
496 }
497
498 if (watched == d->m_pClearButton) {
499 switch (event->type()) {
500 case QEvent::HoverEnter:
501 case QEvent::HoverMove:
502 d->m_pClearButton->setIconColor(true, ThemeController::getCustomColorFromDT("windowtext-active"));
503 break;
504 case QEvent::Leave:
505 d->m_pClearButton->setIconColor(true, ThemeController::getCustomColorFromDT("mid-active"));
506 break;
507 default:
508 break;
509 }
510 }
511 return QLineEdit::eventFilter(watched, event);
512 }
513
514 void KSearchLineEditPrivate::adjustHolderRect()
515 {
516 Q_Q(KSearchLineEdit);
517 m_pWidget->adjustSize();
518 auto maxVisualWidth = q->width() - icontextSpacing - m_pIconLabel->width();
519 QFont font(QApplication::font());
520 QFontMetrics fm(font);
521 auto elidedText = fm.elidedText(m_placeHolderText, Qt::ElideRight, maxVisualWidth);
522 m_pTextLabel->setText(elidedText);
523 m_pWidget->adjustSize();
524
525 if (q->hasFocus())
526 m_pWidget->setFixedHeight(m_pIconLabel->height());
527 else
528 m_pWidget->setFixedHeight(m_pTextLabel->height());
529
530 m_sideRect = m_pWidget->rect();
531 if (m_systemName == "ug_CN" || m_systemName == "kk_KZ" || m_systemName == "ky_KG") {
532 m_sideRect.moveTop((q->rect().height() - m_pWidget->height()) / 2);
533 m_sideRect.moveLeft(q->rect().width() - m_sideRect.width() - iconMargin);
534 } else {
535 m_sideRect.moveTop((q->rect().height() - m_pWidget->height()) / 2);
536 m_sideRect.moveLeft(iconMargin);
537 }
538
539 if (m_placeHolderAlignment & Qt::AlignCenter) {
540 m_holderRect = m_pWidget->rect();
541 m_holderRect.moveCenter(q->rect().center());
542 } else if (m_placeHolderAlignment & Qt::AlignLeft) {
543 m_holderRect = m_pWidget->rect();
544 m_holderRect.moveTop((q->rect().height() - m_pWidget->height()) / 2);
545 m_holderRect.moveLeft(iconMargin);
546 }
547
548 else if (m_placeHolderAlignment & Qt::AlignRight) {
549 m_holderRect = m_pWidget->rect();
550 m_holderRect.moveTop((q->height() - m_pWidget->height()) / 2);
551 m_holderRect.moveRight(q->width() - iconMargin);
552 }
553 }
554
555 void KSearchLineEditPrivate::adjustLayout()
556 {
557 Q_Q(KSearchLineEdit);
558 int spacing = 5;
559 int width = spacing * 2;
560 m_pLineFrame->setFixedHeight(m_pSubWidget->height() - 12);
561 if (!m_pClearButton->isHidden())
562 width += (spacing + m_pClearButton->iconSize().width());
563 if (!m_pLineFrame->isHidden())
564 width += (spacing + m_pLineFrame->width());
565 if (!m_pCustomButton->isHidden())
566 width += (spacing + m_pCustomButton->iconSize().width());
567 m_pSubWidget->setFixedSize(width, q->cursorRect().height());
568 if (m_systemName == "ug_CN" || m_systemName == "kk_KZ" || m_systemName == "ky_KG") {
569 m_pSubWidget->move(0, (q->rect().height() - m_pSubWidget->height()) / 2 + 1);
570 q->setTextMargins(m_pSubWidget->width(), 0, m_pWidget->width(), 0);
571 } else {
572 m_pSubWidget->move(q->width() - m_pSubWidget->width(), (q->rect().height() - m_pSubWidget->height()) / 2 + 1);
573 q->setTextMargins(m_pWidget->width(), 0, m_pSubWidget->width(), 0);
574 }
575 }
576
577 void KSearchLineEdit::resizeEvent(QResizeEvent *event)
578 {
579 Q_D(KSearchLineEdit);
580 QLineEdit::resizeEvent(event);
581 d->adjustHolderRect();
582 if (text().isEmpty() && !hasFocus())
583 d->m_pWidget->setGeometry(d->m_holderRect);
584 else
585 d->m_pWidget->setGeometry(d->m_sideRect);
586 d->adjustLayout();
587 }
588
589 void KSearchLineEdit::setVisible(bool visible)
590 {
591 Q_D(KSearchLineEdit);
592
593 QLineEdit::setVisible(visible);
594 if (!text().isEmpty()) {
595 d->m_pTextLabel->setVisible(false);
596 d->adjustHolderRect();
597 d->m_pWidget->setGeometry(d->m_sideRect);
598 } else {
599 d->m_pTextLabel->setVisible(true);
600 }
601 }
602
603 void KSearchLineEdit::paintEvent(QPaintEvent *event)
604 {
605 Q_D(KSearchLineEdit);
606 QLineEdit::paintEvent(event);
607
608 QStyleOptionFrame option;
609 initStyleOption(&option);
610
611 QPainter painter(this);
612 painter.setRenderHints(QPainter::Antialiasing);
613 painter.setRenderHints(QPainter::SmoothPixmapTransform);
614
615 style()->drawPrimitive(QStyle::PE_PanelLineEdit, &option, &painter, this);
616
617 QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &option, this);
618 int margins = textMargins().left() + textMargins().right();
619 textRect.adjust(textMargins().left() +1, 3, -textMargins().right() - 1, -3);
620
621 QString text = this->text();
622 QString elidedText = fontMetrics().elidedText(text, Qt::ElideRight, textRect.width() - margins);
623
624 if (hasSelectedText())
625 {
626 int selectStart = this->selectionStart();
627 int selectLength = selectedText().length();
628 int selectEnd = selectStart + selectLength;
629
630 QFontMetrics fm = fontMetrics();
631 int startX = fm.horizontalAdvance(this->text().left(selectStart));
632 int endX = fm.horizontalAdvance(this->text().left(selectEnd));
633 int selectWidth = endX - startX;
634
635 QRect selectRect(textRect.x() + startX, textRect.y(),selectWidth, textRect.height());
636
637 painter.fillRect(selectRect, ThemeController::getCustomColorFromDT("kbrand-normal"));
638
639 if (selectStart > 0)
640 {
641 painter.setPen(isEnabled() ? ThemeController::getCustomColorFromDT("text-active") : ThemeController::getCustomColorFromDT("placeholdertext-active"));
642 QString beforeSelect = text.left(selectStart);
643 painter.drawText(textRect, alignment(), beforeSelect);
644 }
645
646 painter.setPen(ThemeController::getCustomColorFromDT("highlightedtext-active"));
647 QString selectedPart = text.mid(selectStart, selectLength);
648
649 int selectX = textRect.x() + fm.horizontalAdvance(text.left(selectStart));
650 QRect selectTextRect(selectX, textRect.y(),fm.horizontalAdvance(selectedPart), textRect.height());
651
652 painter.drawText(selectTextRect, Qt::AlignLeft | Qt::AlignVCenter, selectedPart);
653
654 if (selectStart + selectLength < text.length())
655 {
656 painter.setPen(isEnabled() ? ThemeController::getCustomColorFromDT("text-active") : ThemeController::getCustomColorFromDT("placeholdertext-active"));
657 QString afterSelect = text.mid(selectStart + selectLength);
658
659 int afterX = selectX + fm.horizontalAdvance(selectedPart);
660 QRect afterTextRect(afterX, textRect.y(),
661 textRect.width() - afterX, textRect.height());
662
663 painter.drawText(afterTextRect, Qt::AlignLeft | Qt::AlignVCenter, afterSelect);
664 }
665 }
666 else
667 {
668 painter.setPen(isEnabled() ? ThemeController::getCustomColorFromDT("text-active") : ThemeController::getCustomColorFromDT("placeholdertext-active"));
669 painter.drawText(textRect, alignment(), elidedText);
670 }
671
672 d->m_linearGradient = QLinearGradient(QPointF(rect().left(), rect().center().y()), QPointF(rect().right(), rect().center().y()));
673
674 if (d->m_isFoucus && d->m_showCursor) {
675 QRect rect = this->cursorRect();
676 painter.fillRect(QRect(rect.center().x(), rect.y(), 1, rect.height()), ThemeController::getCustomColorFromDT("windowtext-active"));
677 }
678
679 QPen pen = painter.pen();
680 pen.setWidth(2);
681 if (isEnabled()) {
682 d->m_pClearButton->setEnabled(true);
683 d->m_pCustomButton->setEnabled(true);
684 if (d->m_isGradientFlag && d->m_isGradient) {
685 d->m_linearGradient.setColorAt(0, QColor(232, 136, 248));
686 d->m_linearGradient.setColorAt(0.4, QColor(138, 65, 255));
687 d->m_linearGradient.setColorAt(0.7, QColor(78, 117, 255));
688 d->m_linearGradient.setColorAt(1, QColor(0, 54, 245));
689 pen.setBrush(d->m_linearGradient);
690 } else {
691 pen.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
692 }
693 } else {
694 pen.setBrush(ThemeController::getCustomColorFromDT("button-disable"));
695 d->m_pClearButton->setEnabled(false);
696 d->m_pCustomButton->setEnabled(false);
697 }
698 if (!hasFocus() && !d->m_isGradient)
699 painter.setPen(Qt::NoPen);
700 else
701 painter.setPen(pen);
702 int radius = ThemeController::getRadiusFromDT("kradius-normal");
703 if (radius == -1)
704 radius = 6;
705 painter.drawRoundedRect(rect().adjusted(1, 3, -1, -3), radius, radius);
706 }
707
708 QSize KSearchLineEdit::sizeHint() const
709 {
710 auto size = QLineEdit::sizeHint();
711 size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_SearchLineEditHeight));
712 return size;
713 }
714
715 KSearchLineEditPrivate::KSearchLineEditPrivate(KSearchLineEdit *parent)
716 : q_ptr(parent)
717 , m_placeHolderText(tr("Search"))
718 , m_placeHolderAlignment(Qt::AlignCenter)
719 , m_hasFocus(true)
720 , isTextEmpty(true)
721 , m_isTranslucent(false)
722 , m_isTranslucentFlag(false)
723 , m_isClearVisible(false)
724 , m_isGradient(false)
725 , m_isCustomButtonHover(false)
726 , m_isClearHover(false)
727 , m_contextMenu(false)
728 , m_isGradientFlag(false)
729 , m_isFoucus(false)
730 {
731 Q_Q(KSearchLineEdit);
732 q->setContentsMargins(0,2,0,2);
733 m_pCompleter = new QCompleter(this);
734 m_pCompleter->setCaseSensitivity(Qt::CaseInsensitive);
735 m_pListModel = new QStringListModel(m_List, this);
736 m_pCompleter->setModel(m_pListModel);
737 q->setCompleter(m_pCompleter);
738
739 QAbstractItemView *popup = m_pCompleter->popup();
740 kdk::effects::KShadowHelper::self()->setWidget(popup);
741 m_pListViewDelegate = new ListViewDelegate(popup);
742 popup->setItemDelegate(m_pListViewDelegate);
743 MyStyle *style = new MyStyle;
744 popup->setStyle(style);
745 popup->setContentsMargins(0,1,0,1);
746 setParent(parent);
747 }
748
749 void KSearchLineEditPrivate::doAnimationFinished()
750 {
751 Q_Q(KSearchLineEdit);
752 if (m_isSearching) {
753 if (m_systemName == "ug_CN" || m_systemName == "kk_KZ" || m_systemName == "ky_KG")
754 q->setTextMargins(m_pSubWidget->width(), 0, iconMargin + m_pIconLabel->width(), 0);
755 else
756 q->setTextMargins(iconMargin + m_pIconLabel->width(), 0, m_pSubWidget->width(), 0);
757 }
758 }
759
760 void KSearchLineEditPrivate::doEditingFinished()
761 {
762 Q_Q(KSearchLineEdit);
763 QString text = q->text();
764 if (QString::compare(text.trimmed(), "") != 0) {
765 bool flag = m_List.contains(text, Qt::CaseInsensitive);
766 if (!flag) {
767 m_List.append(text);
768 m_pListModel->setStringList(m_List);
769 }
770 }
771 }
772
773 void KSearchLineEditPrivate::changeTheme()
774 {
775 Q_Q(KSearchLineEdit);
776 if (m_isTranslucentFlag) {
777 if (ThemeController::widgetTheme() == ClassicTheme) {
778 m_isTranslucent = false;
779 setProperty("needTranslucent", false);
780 } else {
781 m_isTranslucent = true;
782 setProperty("needTranslucent", true);
783 }
784 } else {
785 m_isTranslucent = false;
786 setProperty("needTranslucent", false);
787 }
788 adjustHolderRect();
789 if (q->text().isEmpty())
790 m_pWidget->setGeometry(m_holderRect);
791 else
792 m_pWidget->setGeometry(m_sideRect);
793
794 QPixmap pixmap = QIcon::fromTheme("search-symbolic").pixmap(QSize(16, 16));
795 initThemeStyle();
796
797 if (!q->isEnabled()) {
798 QPalette palette = q->palette();
799 // palette.setColor(QPalette::Text,q->palette().color(QPalette::Disabled,QPalette::ButtonText));
800 palette.setColor(QPalette::Text, ThemeController::getCustomColorFromDT("buttontext-disable"));
801 m_pTextLabel->setPalette(palette);
802 } else {
803 QPalette palette = q->palette();
804 #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
805 palette.setColor(QPalette::Text, ThemeController::getCustomColorFromDT("placeholdertext-active"));
806 #else if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
807 palette.setColor(QPalette::Text, ThemeController::getCustomColorFromDT("shadow-active"));
808 #endif
809
810 m_pTextLabel->setPalette(palette);
811 }
812
813 if (ThemeController::themeMode() == LightTheme) {
814 m_pIconLabel->setPixmap(pixmap);
815 auto palette = qApp->palette();
816 palette.setColor(QPalette::Base, Qt::transparent);
817 m_pCompleter->popup()->setPalette(palette);
818 m_pCompleter->popup()->setBackgroundRole(QPalette::Base);
819 m_pCompleter->popup()->setAutoFillBackground(true);
820
821 QPalette lineFramePalette = qApp->palette();
822 lineFramePalette.setColor(QPalette::Base, QColor(207, 207, 207));
823 m_pLineFrame->setPalette(lineFramePalette);
824 } else {
825 m_pIconLabel->setPixmap(drawSymbolicColoredPixmap(pixmap));
826
827 auto palette = qApp->palette();
828 palette.setColor(QPalette::Base, Qt::transparent);
829 m_pCompleter->popup()->setPalette(palette);
830 m_pCompleter->popup()->setBackgroundRole(QPalette::Base);
831 m_pCompleter->popup()->setAutoFillBackground(true);
832
833 QPalette pal = qApp->palette();
834 m_pTextLabel->setPalette(pal);
835
836 QPalette lineFramePalette = qApp->palette();
837 lineFramePalette.setColor(QPalette::Base, ThemeController::getCustomColorFromDT("kgray-13"));
838 m_pLineFrame->setPalette(lineFramePalette);
839 }
840 q->update();
841 }
842
843 void KSearchLineEditPrivate::sltUpdateCursorRect()
844 {
845 Q_Q(KSearchLineEdit);
846 if (m_cursorBlinkTime == 0) {
847 m_showCursor = true;
848 } else {
849 m_showCursor = !m_showCursor;
850 }
851 QRect rect = q->cursorRect();
852 q->update(rect);
853 }
854
855 void KSearchLineEditPrivate::init()
856 {
857 Q_Q(KSearchLineEdit);
858 QPalette btnPalette;
859 btnPalette.setBrush(QPalette::Active, QPalette::Button, Qt::transparent);
860 btnPalette.setBrush(QPalette::Inactive, QPalette::Button, Qt::transparent);
861 btnPalette.setBrush(QPalette::Disabled, QPalette::Button, Qt::transparent);
862
863 btnPalette.setBrush(QPalette::Active, QPalette::Highlight, Qt::transparent);
864 btnPalette.setBrush(QPalette::Inactive, QPalette::Highlight, Qt::transparent);
865 btnPalette.setBrush(QPalette::Disabled, QPalette::Highlight, Qt::transparent);
866
867 m_pClearButton = new KToolButton(q);
868 m_pClearButton->setType(KToolButtonType::Flat);
869 m_pClearButton->setAutoFillBackground(true);
870 m_pClearButton->setPalette(btnPalette);
871 m_pClearButton->setIconSize(QSize(16, 16));
872 m_pClearButton->setFixedSize(QSize(16, 16));
873 m_pClearButton->setFocusPolicy(Qt::NoFocus);
874 m_pClearButton->setCursor(Qt::ArrowCursor);
875 m_pClearButton->setIcon(QIcon::fromTheme("application-exit-symbolic"));
876 m_pClearButton->setVisible(false);
877
878 m_pLineFrame = new KVLineFrame();
879 m_pLineFrame->setFollowPalette(false);
880 m_pLineFrame->setVisible(false);
881
882 m_pCustomButton = new KToolButton(q);
883 m_pCustomButton->setCheckable(true);
884 m_pCustomButton->setType(KToolButtonType::Flat);
885 m_pCustomButton->setAutoFillBackground(true);
886 m_pCustomButton->setPalette(btnPalette);
887 m_pCustomButton->setIconSize(QSize(16, 16));
888 m_pCustomButton->setFixedSize(QSize(16, 16));
889 m_pCustomButton->setFocusPolicy(Qt::NoFocus);
890 m_pCustomButton->setCursor(Qt::ArrowCursor);
891 m_pCustomButton->setVisible(false);
892 m_pCustomButton->setIcon(QIcon());
893
894 m_pSubWidget = new QWidget(q);
895 QHBoxLayout *layout = new QHBoxLayout(m_pSubWidget);
896 layout->setContentsMargins(0, 0, 0, 0);
897 layout->addSpacing(iconMargin);
898 layout->addWidget(m_pClearButton);
899 layout->addWidget(m_pLineFrame);
900 layout->addWidget(m_pCustomButton);
901 layout->addSpacing(iconMargin);
902 m_pSubWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
903
904 m_isSearching = false;
905 m_pTextLabel = new QLabel(tr("Search"));
906 m_pIconLabel = new QLabel;
907 m_pIconLabel->setScaledContents(true);
908 QPixmap pixmap = QIcon::fromTheme("search-symbolic").pixmap(QSize(16, 16));
909 m_pIconLabel->setPixmap(pixmap);
910 m_pIconLabel->setFixedSize(QSize(16, 16));
911 m_pWidget = new QWidget(q);
912 m_pWidget->setFocusPolicy(Qt::NoFocus);
913 m_pWidgetLayout = new QHBoxLayout();
914 m_pWidgetLayout->setContentsMargins(0, 0, 0, 0);
915 m_pWidgetLayout->setSpacing(0);
916 m_pWidgetLayout->addWidget(m_pIconLabel, Qt::AlignVCenter);
917 m_pWidgetLayout->addSpacing(icontextSpacing);
918 m_pWidgetLayout->addWidget(m_pTextLabel, Qt::AlignVCenter);
919 m_pWidgetLayout->addStretch();
920 m_pWidgetLayout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
921 m_pWidget->setLayout(m_pWidgetLayout);
922 m_pAnimation = new QPropertyAnimation(m_pWidget, "geometry");
923 m_pAnimation->setEasingCurve(QEasingCurve::InOutQuad);
924 m_pAnimation->setDuration(100);
925
926 m_systemName = QLocale::system().name();
927 if (m_systemName == "ug_CN" || m_systemName == "kk_KZ" || m_systemName == "ky_KG")
928 q->setAlignment(Qt::AlignRight);
929 else
930 q->setAlignment(Qt::AlignLeft);
931
932 q->setTextMargins(iconMargin + m_pIconLabel->width(), 0, m_pSubWidget->width(), 0);
933
934 KDK_ALL_INFO_FORMAT(m_pWidget,"");
935 KDK_ALL_INFO_FORMAT(m_pTextLabel,"");
936 KDK_ALL_INFO_FORMAT(m_pIconLabel,"");
937 KDK_OBJ_INFO_FORMAT(m_pAnimation);
938 KDK_OBJ_INFO_FORMAT(m_pListModel);
939 KDK_OBJ_INFO_FORMAT(m_pCompleter);
940 KDK_OBJ_INFO_FORMAT(m_pListViewDelegate);
941 KDK_ALL_INFO_FORMAT(m_pClearButton,"");
942 KDK_ALL_INFO_FORMAT(m_pCustomButton,"");
943 KDK_ALL_INFO_FORMAT(m_pLineFrame,"");
944 KDK_ALL_INFO_FORMAT(m_pSubWidget,"");
945 }
946
947 ListViewDelegate::ListViewDelegate(QObject *parent)
948 : QStyledItemDelegate(parent)
949 {
950 }
951
952 void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
953 {
954 QRect rect;
955 rect.setX(option.rect.x());
956 rect.setY(option.rect.y() + 1);
957 rect.setWidth(option.rect.width());
958 rect.setHeight(option.rect.height());
959
960 qreal radius;
961 if (ThemeController::widgetTheme() == ClassicTheme) {
962 if (painter->pen().width() == 1)
963 painter->translate(0.5, 0.5);
964 radius = 0;
965 } else {
966 radius = ThemeController::getRadiusFromDT("kradius-normal");
967 if (radius == -1)
968 radius = 6;
969 }
970 QPainterPath path;
971 path.moveTo(rect.topRight() - QPointF(radius, 0));
972 path.lineTo(rect.topLeft() + QPointF(radius, 0));
973 path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, radius));
974 path.lineTo(rect.bottomLeft() + QPointF(0, -radius));
975 path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(radius, 0));
976 path.lineTo(rect.bottomRight() - QPointF(radius, 0));
977 path.quadTo(rect.bottomRight(), rect.bottomRight() + QPointF(0, -radius));
978 path.lineTo(rect.topRight() + QPointF(0, radius));
979 path.quadTo(rect.topRight(), rect.topRight() + QPointF(-radius, -0));
980
981 painter->setRenderHint(QPainter::Antialiasing);
982 QColor color = Qt::transparent;
983 QColor fontColor = ThemeController::getCustomColorFromDT("buttontext-active");
984
985 if (ThemeController::themeMode() == ThemeFlag::LightTheme) {
986 if (!(option.state & QStyle::State_Enabled)) {
987 color = ThemeController::getCustomColorFromDT("kcomponent-disable");
988 } else if (((option.state & QStyle::State_HasFocus) || (option.state & QStyle::State_Selected) || (option.state & QStyle::State_MouseOver))) {
989 if (option.state.testFlag(QStyle::State_HasFocus) && option.state.testFlag(QStyle::State_Selected)) // QStyle::State_Enabled
990 {
991
992 color = ThemeController::getCustomColorFromDT("highlight-active");
993 fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active");
994 } else if ((option.state & QStyle::State_MouseOver)) {
995 color = ThemeController::getCustomColorFromDT("highlight-active");
996 fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active");
997 } else {
998 color = ThemeController::getCustomColorFromDT("windowtext-active");
999 }
1000 painter->save();
1001 painter->setPen(QPen(Qt::NoPen));
1002 painter->setBrush(color);
1003 painter->drawPath(path);
1004 painter->restore();
1005 }
1006 } else {
1007
1008 if (!(option.state & QStyle::State_Enabled)) {
1009 color = ThemeController::getCustomColorFromDT("kcomponent-disable");
1010 } else if (((option.state & QStyle::State_HasFocus) || (option.state & QStyle::State_Selected) || (option.state & QStyle::State_MouseOver))) {
1011 if (option.state.testFlag(QStyle::State_HasFocus) && option.state.testFlag(QStyle::State_Selected)) // QStyle::State_Enabled
1012 {
1013 color = ThemeController::getCustomColorFromDT("highlight-active");
1014 fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active");
1015 } else if ((option.state & QStyle::State_MouseOver)) {
1016 color = ThemeController::getCustomColorFromDT("highlight-active");
1017 fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active");
1018 } else {
1019 color = ThemeController::getCustomColorFromDT("windowtext-active");
1020 }
1021 painter->save();
1022 painter->setPen(QPen(Qt::NoPen));
1023 painter->setBrush(color);
1024 painter->drawPath(path);
1025 painter->restore();
1026 }
1027 }
1028 QPen pen;
1029 pen.setWidth(1);
1030 pen.setColor(fontColor);
1031 painter->setPen(pen);
1032 QFontMetrics fm = option.fontMetrics;
1033 auto str = index.model()->data(index, Qt::DisplayRole).toString();
1034 if (fm.horizontalAdvance(str) >= rect.width() - 12) {
1035 if (option.state.testFlag(QStyle::State_MouseOver))
1036 QToolTip::showText(QCursor::pos(), str, qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
1037
1038 str = fm.elidedText(str, Qt::ElideRight, rect.width() - 12);
1039 }
1040 painter->drawText(rect.adjusted(12, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, str);
1041 }
1042
1043 QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
1044 {
1045 QSize size;
1046 size.setWidth(option.widget->width());
1047 size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_SearchLineEditItemHeight));
1048 return size;
1049 }
1050
1051 void MyStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
1052 {
1053 switch (element) {
1054 case CE_ShapedFrame: {
1055 painter->setRenderHint(QPainter::Antialiasing);
1056 QColor color;
1057 if (ThemeController::themeMode() == LightTheme) {
1058 color = ThemeController::getCustomColorFromDT("kgray-17");
1059 color.setAlphaF(0.15);
1060 painter->setBrush(ThemeController::getCustomColorFromDT("kwhite"));
1061 } else {
1062 color = ThemeController::getCustomColorFromDT("kgray-3");
1063 painter->setBrush(color);
1064 }
1065 painter->setPen(color);
1066 int radius = ThemeController::getRadiusFromDT("kradius-menu");
1067 painter->drawRoundedRect(option->rect, radius, radius);
1068 break;
1069 }
1070 default:
1071 QProxyStyle::drawControl(element, option, painter, widget);
1072 }
1073 }
1074
1075 QRect MyStyle::subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const
1076 {
1077
1078 switch (element) {
1079 case SE_ShapedFrameContents:
1080 return QProxyStyle::subElementRect(element, option, widget).adjusted(0, 4, 0, 2);
1081 default:
1082 return QProxyStyle::subElementRect(element, option, widget);
1083 }
1084 }
1085
1086 }
1087 #include "ksearchlineedit.moc"
1088 #include "moc_ksearchlineedit.cpp"