File indexing completed on 2024-11-17 05:01:08

0001 /*
0002     SPDX-FileCopyrightText: 2020 Weng Xuetian <wengxt@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "kimpanel.h"
0007 
0008 Kimpanel::Kimpanel(QObject *parent)
0009     : QObject(parent)
0010     , m_panelAgent(new PanelAgent(this))
0011 {
0012     connect(m_panelAgent, &PanelAgent::updateAux, this, &Kimpanel::updateAux);
0013     connect(m_panelAgent, &PanelAgent::updatePreeditText, this, &Kimpanel::updatePreeditText);
0014     connect(m_panelAgent, &PanelAgent::updatePreeditCaret, this, &Kimpanel::updatePreeditCaret);
0015     connect(m_panelAgent, &PanelAgent::updateLookupTable, this, &Kimpanel::updateLookupTable);
0016     connect(m_panelAgent, &PanelAgent::updateSpotLocation, this, &Kimpanel::updateSpotLocation);
0017     connect(m_panelAgent, &PanelAgent::updateSpotRect, this, &Kimpanel::updateSpotRect);
0018     connect(m_panelAgent, &PanelAgent::showAux, this, &Kimpanel::showAux);
0019     connect(m_panelAgent, &PanelAgent::showPreedit, this, &Kimpanel::showPreedit);
0020     connect(m_panelAgent, &PanelAgent::showLookupTable, this, &Kimpanel::showLookupTable);
0021     connect(m_panelAgent, &PanelAgent::updateLookupTableCursor, this, &Kimpanel::updateLookupTableCursor);
0022     connect(m_panelAgent, &PanelAgent::updateLookupTableFull, this, &Kimpanel::updateLookupTableFull);
0023 
0024     connect(m_panelAgent, &PanelAgent::updateProperty, this, &Kimpanel::updateProperty);
0025     connect(m_panelAgent, &PanelAgent::registerProperties, this, &Kimpanel::registerProperties);
0026     connect(m_panelAgent, &PanelAgent::execMenu, this, &Kimpanel::execMenu);
0027     connect(m_panelAgent, &PanelAgent::execDialog, this, &Kimpanel::execDialog);
0028 }
0029 
0030 void Kimpanel::updateAux(const QString &text, const QList<TextAttribute> &attrList)
0031 {
0032     Q_UNUSED(attrList);
0033     if (m_auxText != text) {
0034         m_auxText = text;
0035         Q_EMIT auxTextChanged();
0036     }
0037 }
0038 
0039 void Kimpanel::updatePreeditText(const QString &text, const QList<TextAttribute> &attrList)
0040 {
0041     Q_UNUSED(attrList);
0042     if (m_preeditText != text) {
0043         m_preeditText = text;
0044         Q_EMIT preeditTextChanged();
0045     }
0046 }
0047 
0048 void Kimpanel::updatePreeditCaret(int pos)
0049 {
0050     if (m_caretPos != pos) {
0051         m_caretPos = pos;
0052         Q_EMIT preeditTextChanged();
0053     }
0054 }
0055 
0056 void Kimpanel::updateLookupTable(const KimpanelLookupTable &lookupTable)
0057 {
0058     m_labels.clear();
0059     m_texts.clear();
0060     for (const KimpanelLookupTable::Entry &entry : lookupTable.entries) {
0061         m_labels << entry.label;
0062         m_texts << entry.text;
0063     }
0064     m_hasPrev = lookupTable.has_prev;
0065     m_hasNext = lookupTable.has_next;
0066     Q_EMIT lookupTableChanged();
0067 }
0068 
0069 void Kimpanel::updateSpotLocation(int x, int y)
0070 {
0071     updateSpotRect(x, y, 0, 0);
0072 }
0073 
0074 void Kimpanel::updateSpotRect(int x, int y, int w, int h)
0075 {
0076     m_spotRect = QRect(x, y, w, h);
0077     Q_EMIT spotRectChanged();
0078 }
0079 
0080 void Kimpanel::showAux(bool visible)
0081 {
0082     if (m_auxVisible != visible) {
0083         m_auxVisible = visible;
0084         Q_EMIT auxTextChanged();
0085     }
0086 }
0087 
0088 void Kimpanel::showPreedit(bool visible)
0089 {
0090     if (m_preeditVisible != visible) {
0091         m_preeditVisible = visible;
0092         Q_EMIT preeditTextChanged();
0093     }
0094 }
0095 
0096 void Kimpanel::showLookupTable(bool visible)
0097 {
0098     if (m_lookupTableVisible != visible) {
0099         m_lookupTableVisible = visible;
0100         Q_EMIT lookupTableChanged();
0101     }
0102 }
0103 
0104 void Kimpanel::updateLookupTableCursor(int cursor)
0105 {
0106     if (m_lookupTableCursor != cursor) {
0107         m_lookupTableCursor = cursor;
0108         Q_EMIT lookupTableChanged();
0109     }
0110 }
0111 
0112 void Kimpanel::updateLookupTableFull(const KimpanelLookupTable &lookupTable, int cursor, int layout)
0113 {
0114     m_labels.clear();
0115     m_texts.clear();
0116     for (const KimpanelLookupTable::Entry &entry : lookupTable.entries) {
0117         m_labels << entry.label;
0118         m_texts << entry.text;
0119     }
0120     m_hasPrev = lookupTable.has_prev;
0121     m_hasNext = lookupTable.has_next;
0122     m_lookupTableCursor = cursor;
0123     m_lookupTableLayout = layout;
0124     Q_EMIT lookupTableChanged();
0125 }
0126 
0127 void Kimpanel::updateProperty(const KimpanelProperty &property)
0128 {
0129     for (auto &prop : m_props) {
0130         if (prop.toMap()[QStringLiteral("key")] == property.key) {
0131             prop = property.toMap();
0132             Q_EMIT propertiesChanged();
0133             break;
0134         }
0135     }
0136 }
0137 
0138 void Kimpanel::registerProperties(const QList<KimpanelProperty> &props)
0139 {
0140     m_props.clear();
0141     for (const auto &prop : props) {
0142         m_props << prop.toMap();
0143     }
0144     Q_EMIT propertiesChanged();
0145 }
0146 
0147 void Kimpanel::execMenu(const QList<KimpanelProperty> &props)
0148 {
0149     QVariantList menuProps;
0150     for (const auto &prop : props) {
0151         menuProps << prop.toMap();
0152     }
0153     Q_EMIT menuTriggered(menuProps);
0154 }
0155 
0156 void Kimpanel::execDialog(const KimpanelProperty &prop)
0157 {
0158     Q_UNUSED(prop)
0159 }
0160 
0161 void Kimpanel::configure()
0162 {
0163     m_panelAgent->configure();
0164 }
0165 
0166 void Kimpanel::exit()
0167 {
0168     m_panelAgent->exit();
0169 }
0170 
0171 void Kimpanel::lookupTablePageDown()
0172 {
0173     m_panelAgent->lookupTablePageDown();
0174 }
0175 
0176 void Kimpanel::lookupTablePageUp()
0177 {
0178     m_panelAgent->lookupTablePageUp();
0179 }
0180 
0181 void Kimpanel::movePreeditCaret(int position)
0182 {
0183     m_panelAgent->movePreeditCaret(position);
0184 }
0185 
0186 void Kimpanel::reloadConfig()
0187 {
0188     m_panelAgent->reloadConfig();
0189 }
0190 
0191 void Kimpanel::selectCandidate(int candidate)
0192 {
0193     m_panelAgent->selectCandidate(candidate);
0194 }
0195 
0196 void Kimpanel::triggerProperty(const QString &key)
0197 {
0198     m_panelAgent->triggerProperty(key);
0199 }