|
47 | 47 | #include <private/qguiapplication_p.h> |
48 | 48 | #include <qpa/qplatformtheme.h> |
49 | 49 | #include <QtSvg/qsvgrenderer.h> |
| 50 | +#include <QDBusMessage> |
| 51 | +#include <QDBusConnection> |
| 52 | +#include <QDBusReply> |
| 53 | +#include <QJsonDocument> |
| 54 | +#include <QJsonArray> |
| 55 | +#include <QJsonObject> |
50 | 56 |
|
51 | 57 | TextEdit::TextEdit(QWidget *parent) |
52 | 58 | : DPlainTextEdit(parent), |
@@ -126,6 +132,12 @@ TextEdit::TextEdit(QWidget *parent) |
126 | 132 | this, SLOT(fingerZoom(QString, QString, int))); |
127 | 133 | break; |
128 | 134 | } |
| 135 | + |
| 136 | + // 连接音频设备状态变化信号 |
| 137 | + dbus.sessionBus().connect("com.deepin.daemon.Audio", |
| 138 | + "/com/deepin/daemon/Audio", "com.deepin.daemon.Audio", |
| 139 | + "PortEnabledChanged", |
| 140 | + this, SLOT(onAudioPortEnabledChanged(quint32, QString, bool))); |
129 | 141 |
|
130 | 142 | //初始化右键菜单 |
131 | 143 | initRightClickedMenu(); |
@@ -525,81 +537,79 @@ void TextEdit::popRightMenu(QPoint pos) |
525 | 537 | (DSysInfo::uosEditionType() == DSysInfo::UosEdition::UosHome) || |
526 | 538 | (DSysInfo::uosEditionType() == DSysInfo::UosEdition::UosEducation)) {*/ |
527 | 539 |
|
528 | | - if (m_wrapper->window()->getIsRegistIflytekAiassistant()) { |
529 | | - bool stopReadingState = false; |
530 | | - QDBusMessage stopReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
531 | | - "/aiassistant/tts", |
532 | | - "com.iflytek.aiassistant.tts", |
533 | | - "isTTSInWorking"); |
| 540 | + bool stopReadingState = false; |
| 541 | + QDBusMessage stopReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 542 | + "/aiassistant/tts", |
| 543 | + "com.iflytek.aiassistant.tts", |
| 544 | + "isTTSInWorking"); |
534 | 545 |
|
535 | | - QDBusReply<bool> stopReadingStateRet = QDBusConnection::sessionBus().asyncCall(stopReadingMsg, 100); |
536 | | - if (stopReadingStateRet.isValid()) { |
537 | | - stopReadingState = stopReadingStateRet.value(); |
538 | | - } |
539 | | - if (!stopReadingState) { |
540 | | - m_rightMenu->addAction(m_voiceReadingAction); |
541 | | - m_voiceReadingAction->setEnabled(false); |
542 | | - } else { |
543 | | - m_rightMenu->removeAction(m_voiceReadingAction); |
544 | | - m_rightMenu->addAction(m_stopReadingAction); |
545 | | - } |
546 | | - bool voiceReadingState = false; |
547 | | - QDBusMessage voiceReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
548 | | - "/aiassistant/tts", |
549 | | - "com.iflytek.aiassistant.tts", |
550 | | - "getTTSEnable"); |
551 | | - |
552 | | - QDBusReply<bool> voiceReadingStateRet = QDBusConnection::sessionBus().asyncCall(voiceReadingMsg, 100); |
553 | | - //没有收到返回就加载配置文件数据 |
554 | | - if (voiceReadingStateRet.isValid()) { |
555 | | - voiceReadingState = voiceReadingStateRet.value(); |
556 | | - } else { |
557 | | - voiceReadingState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-tts"); |
558 | | - } |
559 | | - if ((textCursor().hasSelection() && voiceReadingState) || m_hasColumnSelection) { |
560 | | - m_voiceReadingAction->setEnabled(true); |
561 | | - } |
| 546 | + QDBusReply<bool> stopReadingStateRet = QDBusConnection::sessionBus().asyncCall(stopReadingMsg, 100); |
| 547 | + if (stopReadingStateRet.isValid()) { |
| 548 | + stopReadingState = stopReadingStateRet.value(); |
| 549 | + } |
| 550 | + if (!stopReadingState) { |
| 551 | + m_rightMenu->addAction(m_voiceReadingAction); |
| 552 | + m_voiceReadingAction->setEnabled(false); |
| 553 | + } else { |
| 554 | + m_rightMenu->removeAction(m_voiceReadingAction); |
| 555 | + m_rightMenu->addAction(m_stopReadingAction); |
| 556 | + } |
| 557 | + bool voiceReadingState = false; |
| 558 | + QDBusMessage voiceReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 559 | + "/aiassistant/tts", |
| 560 | + "com.iflytek.aiassistant.tts", |
| 561 | + "getTTSEnable"); |
| 562 | + |
| 563 | + QDBusReply<bool> voiceReadingStateRet = QDBusConnection::sessionBus().asyncCall(voiceReadingMsg, 100); |
| 564 | + //没有收到返回就加载配置文件数据 |
| 565 | + if (voiceReadingStateRet.isValid()) { |
| 566 | + voiceReadingState = voiceReadingStateRet.value(); |
| 567 | + } else { |
| 568 | + voiceReadingState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-tts"); |
| 569 | + } |
| 570 | + if ((textCursor().hasSelection()) || m_hasColumnSelection) { |
| 571 | + m_voiceReadingAction->setEnabled(true); |
| 572 | + } |
562 | 573 |
|
563 | | - m_rightMenu->addAction(m_dictationAction); |
564 | | - bool dictationState = false; |
565 | | - QDBusMessage dictationMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
566 | | - "/aiassistant/iat", |
567 | | - "com.iflytek.aiassistant.iat", |
568 | | - "getIatEnable"); |
| 574 | + m_rightMenu->addAction(m_dictationAction); |
| 575 | + bool dictationState = false; |
| 576 | + QDBusMessage dictationMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 577 | + "/aiassistant/iat", |
| 578 | + "com.iflytek.aiassistant.iat", |
| 579 | + "getIatEnable"); |
569 | 580 |
|
570 | | - QDBusReply<bool> dictationStateRet = QDBusConnection::sessionBus().asyncCall(dictationMsg, 100); |
571 | | - //没有收到返回就加载配置文件数据 |
572 | | - if (dictationStateRet.isValid()) { |
573 | | - dictationState = dictationStateRet.value(); |
574 | | - } else { |
575 | | - dictationState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-iat"); |
576 | | - } |
577 | | - m_dictationAction->setEnabled(dictationState); |
578 | | - if (m_bReadOnlyPermission || m_readOnlyMode) { |
579 | | - m_dictationAction->setEnabled(false); |
580 | | - } |
| 581 | + QDBusReply<bool> dictationStateRet = QDBusConnection::sessionBus().asyncCall(dictationMsg, 100); |
| 582 | + //没有收到返回就加载配置文件数据 |
| 583 | + if (dictationStateRet.isValid()) { |
| 584 | + dictationState = dictationStateRet.value(); |
| 585 | + } else { |
| 586 | + dictationState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-iat"); |
| 587 | + } |
581 | 588 |
|
582 | | - m_rightMenu->addAction(m_translateAction); |
583 | | - m_translateAction->setEnabled(false); |
584 | | - bool translateState = false; |
585 | | - QDBusMessage translateReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
586 | | - "/aiassistant/trans", |
587 | | - "com.iflytek.aiassistant.trans", |
588 | | - "getTransEnable"); |
589 | | - |
590 | | - QDBusReply<bool> translateStateRet = QDBusConnection::sessionBus().asyncCall(translateReadingMsg, 100); |
591 | | - //没有收到返回就加载配置文件数据 |
592 | | - if (translateStateRet.isValid()) { |
593 | | - translateState = translateStateRet.value(); |
594 | | - } else { |
595 | | - translateState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-trans"); |
596 | | - } |
597 | | - if ((textCursor().hasSelection() && translateState) || m_hasColumnSelection) { |
598 | | - m_translateAction->setEnabled(translateState); |
599 | | - } |
| 589 | + if (m_bReadOnlyPermission || m_readOnlyMode) { |
| 590 | + m_dictationAction->setEnabled(false); |
600 | 591 | } |
601 | 592 |
|
602 | 593 |
|
| 594 | + m_translateAction->setEnabled(true); |
| 595 | + bool translateState = false; |
| 596 | + QDBusMessage translateReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 597 | + "/aiassistant/trans", |
| 598 | + "com.iflytek.aiassistant.trans", |
| 599 | + "getTransEnable"); |
| 600 | + |
| 601 | + QDBusReply<bool> translateStateRet = QDBusConnection::sessionBus().asyncCall(translateReadingMsg, 100); |
| 602 | + //没有收到返回就加载配置文件数据 |
| 603 | + if (translateStateRet.isValid()) { |
| 604 | + translateState = translateStateRet.value(); |
| 605 | + } else { |
| 606 | + m_rightMenu->addAction(m_translateAction); |
| 607 | + translateState = m_wrapper->window()->getIflytekaiassistantConfig("aiassistant-trans"); |
| 608 | + } |
| 609 | + if ((textCursor().hasSelection()) || m_hasColumnSelection) { |
| 610 | + m_translateAction->setEnabled((textCursor().hasSelection()) || m_hasColumnSelection); |
| 611 | + } |
| 612 | + |
603 | 613 | if (!this->document()->isEmpty()) { |
604 | 614 |
|
605 | 615 | m_colorMarkMenu->clear(); |
@@ -2807,19 +2817,100 @@ void TextEdit::slotCancelComment(bool checked) |
2807 | 2817 | void TextEdit::slotVoiceReadingAction(bool checked) |
2808 | 2818 | { |
2809 | 2819 | Q_UNUSED(checked); |
| 2820 | + |
| 2821 | + bool voiceReadingState = false; |
| 2822 | + QDBusMessage voiceReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 2823 | + "/aiassistant/tts", |
| 2824 | + "com.iflytek.aiassistant.tts", |
| 2825 | + "getTTSEnable"); |
| 2826 | + |
| 2827 | + QDBusReply<bool> voiceReadingStateRet = QDBusConnection::sessionBus().asyncCall(voiceReadingMsg, 100); |
| 2828 | + //没有收到返回就加载配置文件数据 |
| 2829 | + if (voiceReadingStateRet.isValid()) { |
| 2830 | + voiceReadingState = voiceReadingStateRet.value(); |
| 2831 | + } |
| 2832 | + if (!voiceReadingState) { |
| 2833 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 2834 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("Please install UOS AI from the app store first.")); |
| 2835 | +#else |
| 2836 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("Please install UOS AI from the app store first.")); |
| 2837 | +#endif |
| 2838 | + return; |
| 2839 | + } |
| 2840 | + |
| 2841 | + // 检查音频输出设备 |
| 2842 | + if (!checkAudioOutputDevice()) { |
| 2843 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 2844 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("No audio output device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 2845 | +#else |
| 2846 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("No audio output device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 2847 | +#endif |
| 2848 | + return; |
| 2849 | + } |
| 2850 | + |
2810 | 2851 | QProcess::startDetached("dbus-send --print-reply --dest=com.iflytek.aiassistant /aiassistant/deepinmain com.iflytek.aiassistant.mainWindow.TextToSpeech"); |
2811 | 2852 | emit signal_readingPath(); |
2812 | 2853 | } |
2813 | 2854 |
|
2814 | 2855 | bool TextEdit::slotStopReadingAction(bool checked) |
2815 | 2856 | { |
2816 | 2857 | Q_UNUSED(checked); |
| 2858 | + bool voiceReadingState = false; |
| 2859 | + QDBusMessage voiceReadingMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 2860 | + "/aiassistant/tts", |
| 2861 | + "com.iflytek.aiassistant.tts", |
| 2862 | + "getTTSEnable"); |
| 2863 | + |
| 2864 | + QDBusReply<bool> voiceReadingStateRet = QDBusConnection::sessionBus().asyncCall(voiceReadingMsg, 100); |
| 2865 | + //没有收到返回就加载配置文件数据 |
| 2866 | + if (voiceReadingStateRet.isValid()) { |
| 2867 | + voiceReadingState = voiceReadingStateRet.value(); |
| 2868 | + } |
| 2869 | + if (!voiceReadingState) { |
| 2870 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 2871 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("Please install UOS AI from the app store first.")); |
| 2872 | +#else |
| 2873 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("Please install UOS AI from the app store first.")); |
| 2874 | +#endif |
| 2875 | + return false; |
| 2876 | + } |
| 2877 | + |
2817 | 2878 | return QProcess::startDetached("dbus-send --print-reply --dest=com.iflytek.aiassistant /aiassistant/tts com.iflytek.aiassistant.tts.stopTTSDirectly"); |
2818 | 2879 | } |
2819 | 2880 |
|
2820 | 2881 | void TextEdit::slotdictationAction(bool checked) |
2821 | 2882 | { |
2822 | 2883 | Q_UNUSED(checked); |
| 2884 | + |
| 2885 | + bool dictationState = false; |
| 2886 | + QDBusMessage dictationMsg = QDBusMessage::createMethodCall("com.iflytek.aiassistant", |
| 2887 | + "/aiassistant/iat", |
| 2888 | + "com.iflytek.aiassistant.iat", |
| 2889 | + "getIatEnable"); |
| 2890 | + |
| 2891 | + QDBusReply<bool> dictationStateRet = QDBusConnection::sessionBus().asyncCall(dictationMsg, 100); |
| 2892 | + if (dictationStateRet.isValid()) { |
| 2893 | + dictationState = dictationStateRet.value(); |
| 2894 | + } |
| 2895 | + if (!dictationState) { |
| 2896 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 2897 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("Please install UOS AI from the app store first.")); |
| 2898 | +#else |
| 2899 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("Please install UOS AI from the app store first.")); |
| 2900 | +#endif |
| 2901 | + return; |
| 2902 | + } |
| 2903 | + |
| 2904 | + // 检查音频输入设备 |
| 2905 | + if (!checkAudioInputDevice()) { |
| 2906 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 2907 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("No audio input device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 2908 | +#else |
| 2909 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("No audio input device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 2910 | +#endif |
| 2911 | + return; |
| 2912 | + } |
| 2913 | + |
2823 | 2914 | QProcess::startDetached("dbus-send --print-reply --dest=com.iflytek.aiassistant /aiassistant/deepinmain com.iflytek.aiassistant.mainWindow.SpeechToText"); |
2824 | 2915 | } |
2825 | 2916 |
|
@@ -8044,3 +8135,95 @@ void TextEdit::onTextContentChanged(int from, int charsRemoved, int charsAdded) |
8044 | 8135 | m_MidButtonPatse = false; |
8045 | 8136 | } |
8046 | 8137 | } |
| 8138 | + |
| 8139 | +bool TextEdit::checkAudioOutputDevice() |
| 8140 | +{ |
| 8141 | + QDBusMessage msg = QDBusMessage::createMethodCall("com.deepin.daemon.Audio", |
| 8142 | + "/com/deepin/daemon/Audio", |
| 8143 | + "org.freedesktop.DBus.Properties", |
| 8144 | + "Get"); |
| 8145 | + msg << QString("com.deepin.daemon.Audio") << QString("CardsWithoutUnavailable"); |
| 8146 | + |
| 8147 | + QDBusReply<QVariant> reply = QDBusConnection::sessionBus().call(msg); |
| 8148 | + if (reply.isValid()) { |
| 8149 | + QJsonDocument doc = QJsonDocument::fromJson(reply.value().toByteArray()); |
| 8150 | + QJsonArray cards = doc.array(); |
| 8151 | + |
| 8152 | + // 检查是否有启用的输出设备 (Direction=1) |
| 8153 | + for (const QJsonValue &cardValue : cards) { |
| 8154 | + QJsonObject card = cardValue.toObject(); |
| 8155 | + QJsonArray ports = card["Ports"].toArray(); |
| 8156 | + |
| 8157 | + for (const QJsonValue &portValue : ports) { |
| 8158 | + QJsonObject port = portValue.toObject(); |
| 8159 | + if (port["Direction"].toInt() == 1 && port["Enabled"].toBool()) { |
| 8160 | + return true; // 找到启用的输出设备 |
| 8161 | + } |
| 8162 | + } |
| 8163 | + } |
| 8164 | + } |
| 8165 | + |
| 8166 | + return false; |
| 8167 | +} |
| 8168 | + |
| 8169 | +bool TextEdit::checkAudioInputDevice() |
| 8170 | +{ |
| 8171 | + QDBusMessage msg = QDBusMessage::createMethodCall("com.deepin.daemon.Audio", |
| 8172 | + "/com/deepin/daemon/Audio", |
| 8173 | + "org.freedesktop.DBus.Properties", |
| 8174 | + "Get"); |
| 8175 | + msg << QString("com.deepin.daemon.Audio") << QString("CardsWithoutUnavailable"); |
| 8176 | + |
| 8177 | + QDBusReply<QVariant> reply = QDBusConnection::sessionBus().call(msg); |
| 8178 | + if (reply.isValid()) { |
| 8179 | + QJsonDocument doc = QJsonDocument::fromJson(reply.value().toByteArray()); |
| 8180 | + QJsonArray cards = doc.array(); |
| 8181 | + |
| 8182 | + // 检查是否有启用的输出设备 (Direction=2) |
| 8183 | + for (const QJsonValue &cardValue : cards) { |
| 8184 | + QJsonObject card = cardValue.toObject(); |
| 8185 | + QJsonArray ports = card["Ports"].toArray(); |
| 8186 | + |
| 8187 | + for (const QJsonValue &portValue : ports) { |
| 8188 | + QJsonObject port = portValue.toObject(); |
| 8189 | + if (port["Direction"].toInt() == 2 && port["Enabled"].toBool()) { |
| 8190 | + return true; // 找到启用的输入设备 |
| 8191 | + } |
| 8192 | + } |
| 8193 | + } |
| 8194 | + } |
| 8195 | + |
| 8196 | + return false; |
| 8197 | +} |
| 8198 | + |
| 8199 | +void TextEdit::onAudioPortEnabledChanged(quint32 cardId, const QString &portName, bool enabled) |
| 8200 | +{ |
| 8201 | + Q_UNUSED(cardId) |
| 8202 | + Q_UNUSED(portName) |
| 8203 | + |
| 8204 | + // 只处理设备被禁用的情况 |
| 8205 | + if (!enabled) { |
| 8206 | + // 检查是否还有可用的输出设备和输入设备 |
| 8207 | + bool hasOutputDevice = checkAudioOutputDevice(); |
| 8208 | + bool hasInputDevice = checkAudioInputDevice(); |
| 8209 | + |
| 8210 | + // 如果所有输出设备都被禁用,显示输出设备提示 |
| 8211 | + if (!hasOutputDevice) { |
| 8212 | + slotStopReadingAction(); |
| 8213 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 8214 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("No audio output device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 8215 | +#else |
| 8216 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("No audio output device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 8217 | +#endif |
| 8218 | + } |
| 8219 | + |
| 8220 | + // 如果所有输入设备都被禁用,显示输入设备提示 |
| 8221 | + if (!hasInputDevice) { |
| 8222 | +#ifdef DTKWIDGET_CLASS_DSizeMode |
| 8223 | + Utils::sendFloatMessageFixedFont(this, QIcon(":/images/warning.svg"), tr("No audio input device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 8224 | +#else |
| 8225 | + DMessageManager::instance()->sendMessage(this, QIcon(":/images/warning.svg"), tr("No audio input device was detected. Please ensure your speakers or headphones are properly connected and try again.")); |
| 8226 | +#endif |
| 8227 | + } |
| 8228 | + } |
| 8229 | +} |
0 commit comments