1 /*
2  * SPDX-FileCopyrightText: 2021~2021 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef _PLATFORMINPUTCONTEXT_FCITXCANDIDATEWINDOW_H_
9 #define _PLATFORMINPUTCONTEXT_FCITXCANDIDATEWINDOW_H_
10 
11 #include "fcitxflags.h"
12 #include "fcitxqtdbustypes.h"
13 #include <QBackingStore>
14 #include <QGuiApplication>
15 #include <QPainter>
16 #include <QPointer>
17 #include <QTextLayout>
18 #include <QWindow>
19 
20 namespace fcitx {
21 
22 struct FcitxQtICData;
23 class FcitxTheme;
24 class MultilineText;
25 
26 class FcitxCandidateWindow : public QWindow {
27     Q_OBJECT
28 public:
29     explicit FcitxCandidateWindow(FcitxQtICData *data, FcitxTheme *theme);
30     ~FcitxCandidateWindow();
31 
32     void render(QPainter *painter);
33 
34 public Q_SLOTS:
35     void renderLater();
36     void renderNow();
37     void updateClientSideUI(const FcitxQtFormattedPreeditList &preedit,
38                             int cursorpos,
39                             const FcitxQtFormattedPreeditList &auxUp,
40                             const FcitxQtFormattedPreeditList &auxDown,
41                             const FcitxQtStringKeyValueList &candidates,
42                             int candidateIndex, int layoutHint, bool hasPrev,
43                             bool hasNext);
44 
45     QSize sizeHint();
46 
47 Q_SIGNALS:
48     void candidateSelected(int i);
49     void prevClicked();
50     void nextClicked();
51 
52 protected:
53     bool event(QEvent *event) override;
54 
55     void resizeEvent(QResizeEvent *event) override;
56     void exposeEvent(QExposeEvent *event) override;
57     void mouseMoveEvent(QMouseEvent *) override;
58     void mouseReleaseEvent(QMouseEvent *) override;
59     void wheelEvent(QWheelEvent *) override;
60 
highlight()61     int highlight() const {
62         int highlightIndex = (hoverIndex_ >= 0) ? hoverIndex_ : highlight_;
63         return highlightIndex;
64     }
65 
66 private:
67     const bool isWayland_ =
68         QGuiApplication::platformName().startsWith("wayland");
69     QSize actualSize_;
70     QPointer<FcitxTheme> theme_;
71     QBackingStore *backingStore_;
72     QTextLayout upperLayout_;
73     QTextLayout lowerLayout_;
74     std::vector<std::unique_ptr<MultilineText>> candidateLayouts_;
75     std::vector<std::unique_ptr<MultilineText>> labelLayouts_;
76     int cursor_ = -1;
77     int highlight_ = -1;
78     int hoverIndex_ = -1;
79     int accAngle_ = 0;
80     bool prevHovered_ = false;
81     bool nextHovered_ = false;
82     bool hasPrev_ = false;
83     bool hasNext_ = false;
84     FcitxCandidateLayoutHint layoutHint_ = FcitxCandidateLayoutHint::NotSet;
85     int candidatesHeight_ = 0;
86     QRect prevRegion_;
87     QRect nextRegion_;
88     std::vector<QRect> candidateRegions_;
89     QPointer<QWindow> parent_;
90     QPointer<QWindow> dummyParent_;
91 };
92 
93 } // namespace fcitx
94 
95 #endif // _PLATFORMINPUTCONTEXT_FCITXCANDIDATEWINDOW_H_
96