From 55049f4ee286633b486936c7f4d19d3d426453a0 Mon Sep 17 00:00:00 2001 From: liuxinhao Date: Mon, 15 Jan 2024 16:31:01 +0800 Subject: [PATCH 14/17] fix(shortcut): add custom app icon,Error prompt for modifying input shortcut keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改录入快捷键错误提示,新增自定义应用图标显示 Closes #25199,#25198 --- plugins/keybinding/shortcut.cpp | 79 ++++++++++++---- plugins/keybinding/shortcut.h | 10 +- translations/kiran-control-panel.zh_CN.ts | 106 ++++++++++++---------- 3 files changed, 127 insertions(+), 68 deletions(-) diff --git a/plugins/keybinding/shortcut.cpp b/plugins/keybinding/shortcut.cpp index bab4ad1..84c0cba 100644 --- a/plugins/keybinding/shortcut.cpp +++ b/plugins/keybinding/shortcut.cpp @@ -31,12 +31,15 @@ #include #include -Q_DECLARE_METATYPE(QList) +#define DEFAULT_APP_ICON "application-script-blank" +#define APP_ICON_WIDTH 20 +Q_DECLARE_METATYPE(QList) using namespace Kiran; -Shortcut::Shortcut(QWidget *parent) : QWidget(parent), - ui(new Ui::Shortcut) +Shortcut::Shortcut(QWidget *parent) + : QWidget(parent), + ui(new Ui::Shortcut) { ui->setupUi(this); init(); @@ -100,16 +103,24 @@ void Shortcut::initUI() } QHBoxLayout *hLayoutCustomApp = new QHBoxLayout(ui->lineEdit_custom_app); + m_customAppIcon = new QLabel; + m_customAppIcon->setFixedSize(24, 24); + m_customAppIcon->setPixmap(QIcon::fromTheme(DEFAULT_APP_ICON).pixmap(20, 20)); + hLayoutCustomApp->addWidget(m_customAppIcon, 0, Qt::AlignVCenter); + + hLayoutCustomApp->addStretch(); + m_btnCustomApp = new QToolButton; m_btnCustomApp->setObjectName("btn_custom_app"); m_btnCustomApp->setAccessibleName("ButtonAddCustomApp"); m_btnCustomApp->setText(tr("Add")); m_btnCustomApp->setFixedSize(56, 30); m_btnCustomApp->setCursor(Qt::PointingHandCursor); - hLayoutCustomApp->addStretch(); hLayoutCustomApp->addWidget(m_btnCustomApp); - ui->lineEdit_custom_app->setTextMargins(0, 0, m_btnCustomApp->width(), 0); + + ui->lineEdit_custom_app->setTextMargins(25, 0, m_btnCustomApp->width(), 0); connect(m_btnCustomApp, &QToolButton::clicked, this, &Shortcut::openFileSys); + connect(ui->lineEdit_custom_app,&QLineEdit::textChanged,this,&Shortcut::handleCustomAppTextChanged); QHBoxLayout *hLayoutModifyApp = new QHBoxLayout(ui->lineEdit_modify_app); m_btnModifyApp = new QToolButton; @@ -354,7 +365,7 @@ bool Shortcut::isValidKeycode(QList keycodes) return !pureModifier; } -bool Shortcut::getExecFromDesktop(QString fileName, QString &exec) +bool Shortcut::extractDesktopInfo(const QString &fileName, QString &exec, QString &icon) { QSettings settings(fileName, QSettings::IniFormat); QString str = settings.value("Desktop Entry/Exec").toString(); @@ -368,6 +379,10 @@ bool Shortcut::getExecFromDesktop(QString fileName, QString &exec) str = str.replace("%u", "", Qt::CaseInsensitive); exec = str; + + str = settings.value("Desktop Entry/Icon").toString(); + icon = str.isEmpty() ? DEFAULT_APP_ICON : str; + return true; } @@ -376,23 +391,38 @@ void Shortcut::openFileSys() QToolButton *senderbtn = qobject_cast(sender()); QLineEdit *lineEdit = qobject_cast(senderbtn->parent()); - QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/usr/share/applications"); + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open File"), + "/usr/share/applications", + tr("Desktop entries(*.desktop)")); if (fileName.isNull()) return; - QString exec = fileName; - if (fileName.endsWith(".desktop")) + QString cmd; + QString icon = DEFAULT_APP_ICON; + if (!extractDesktopInfo(fileName, cmd, icon)) { - QString tmp; - if (!getExecFromDesktop(fileName, tmp)) - { - KLOG_ERROR(qLcKeybinding) << "cant't get Exec key from " << fileName; - return; - } - exec = tmp; + KLOG_ERROR(qLcKeybinding) << "cant't get Exec key from " << fileName; + KiranMessageBox::message(this, tr("Error"), + "Extracting the program to be executed from the application's desktop file failed", + KiranMessageBox::Ok); + return; + } + + // 缓存该次添加的应用以及图标信息 + // 后续启动命令修改的时候,应复位图标 + m_lastIcon = icon; + m_lastIconExec = cmd; + + QIcon appIcon = QIcon::fromTheme(icon); + if( appIcon.isNull() ) + { + m_lastIcon = DEFAULT_APP_ICON; + appIcon = QIcon::fromTheme(DEFAULT_APP_ICON); } - lineEdit->setText(exec); + m_customAppIcon->setPixmap(appIcon.pixmap(QSize(APP_ICON_WIDTH,APP_ICON_WIDTH))); + lineEdit->setText(cmd); } void Shortcut::handleSearchTimerTimeout() @@ -787,6 +817,16 @@ void Shortcut::handleResetClicked() } } +void Shortcut::handleCustomAppTextChanged(const QString& text) +{ + // 直接编辑自定义应用输入框,修改命令 + // 导致和desktop读取出来的不一致时清空图标 + if( !text.isEmpty() && text != m_lastIconExec ) + { + m_customAppIcon->setPixmap(QIcon::fromTheme(DEFAULT_APP_ICON).pixmap(20,20)); + } +} + void Shortcut::handleInputKeycode(QList keycodes) { CustomLineEdit *senderLineEdit = qobject_cast(sender()); @@ -803,8 +843,9 @@ void Shortcut::handleInputKeycode(QList keycodes) { KiranMessageBox::message(nullptr, tr("Failed"), - QString(tr("Cannot use shortcut \"%1\", Because you cannot enter with this key." - "Please try again using Ctrl, Alt, or Shift at the same time.")) + QString(tr("Cannot use shortcut \"%1\"," + "Please keep pressing the modifier keys such as Ctrl," + "Alt, and Shift before pressing the last key of the shortcut key")) .arg(keyStr), KiranMessageBox::Ok); return; diff --git a/plugins/keybinding/shortcut.h b/plugins/keybinding/shortcut.h index fc078fb..3504b1b 100644 --- a/plugins/keybinding/shortcut.h +++ b/plugins/keybinding/shortcut.h @@ -35,6 +35,8 @@ class ShortcutItem; class KeyMap; class CustomLineEdit; class KeybindingBackEndProxy; +class QLabel; + class Shortcut : public QWidget { Q_OBJECT @@ -59,7 +61,7 @@ private: ShortcutItem *createShortcutItem(QVBoxLayout *parent, ShortcutInfoPtr shortcutInfo, int type); bool isConflict(QString &originName, QString newKeyCombination); bool isValidKeycode(QList keycodes); - bool getExecFromDesktop(QString fileName, QString &exec); + bool extractDesktopInfo(const QString& fileName, QString &exec, QString &icon); void updateShorcut(ShortcutInfoPtr newShortcut); void insertShortcut(ShortcutInfoPtr shortcutInfo); void clearFilterItems(); @@ -72,6 +74,7 @@ public slots: void handledShortcutDeleted(QString result); void handleShortcutChanged(QString result); + void handleCustomAppTextChanged(const QString& text); void handleInputKeycode(QList keycodes); void handleItemDeleteClicked(QString uid); @@ -96,8 +99,13 @@ private: QList m_shortcutItem; QList m_filterItem; + //记录上次通过应用打开的desktop文件记录的Exec和Icon + QString m_lastIconExec; + QString m_lastIcon; + QToolButton *m_btnModifyApp; QToolButton *m_btnCustomApp; + QLabel *m_customAppIcon; CustomLineEdit *m_lECustomKey; CustomLineEdit *m_lEModifyKey; diff --git a/translations/kiran-control-panel.zh_CN.ts b/translations/kiran-control-panel.zh_CN.ts index 970c5a0..31fc01e 100644 --- a/translations/kiran-control-panel.zh_CN.ts +++ b/translations/kiran-control-panel.zh_CN.ts @@ -33,18 +33,18 @@ AccountWidget - + disable 禁用 - + enable 启用 - + Create new user 创建新用户 @@ -2983,22 +2983,22 @@ 请用分号分隔多个DNS - + Ipv6 DNS invalid 无效的Ipv6 DNS - + Ipv6 address can not be empty Ipv6地址不能为空 - + Ipv6 address invalid 无效的Ipv6地址 - + Ipv6 Gateway invalid 无效的Ipv6网关 @@ -4675,8 +4675,8 @@ This is line 50 of the test text - - + + Edit 编辑 @@ -4688,8 +4688,8 @@ This is line 50 of the test text - - + + Add 添加 @@ -4742,7 +4742,7 @@ This is line 50 of the test text Cancel - 取消 + 取消 @@ -4772,7 +4772,7 @@ This is line 50 of the test text Save - 保存 + 保存 @@ -4782,129 +4782,139 @@ This is line 50 of the test text return - 返回 + 返回 - + Please enter a search keyword... 请输入搜索关键字... - + Required 必填 - - + + Please press the new shortcut key 请输入新快捷键 - + Finished 完成 - + failed to load shortcut key data! 加载快捷键数据失败! - + List shortcut failed,error:%1 列出快捷键失败,错误:%1 - + + Error 错误 - + Get shortcut failed,error: 获取快捷键失败,错误: - + Open File 打开文件 - + + Desktop entries(*.desktop) + 桌面文件(*.desktop) + + + System 系统 - + Sound 声音 - - - - - - - + + + + + + + Failed 失败 - + Delete shortcut failed,error: 删除快捷键失败,错误: - - + + Warning - 警告 + 警告 - - + + Please complete the shortcut information! 请完善快捷键信息! - + Set shortcut 设置快捷键 - + Are you sure you want to disable this shortcut? 是否确定要禁用此快捷键? - + Modify system shortcut failed,error: 修改系统快捷键失败,错误: - + Modify custom shortcut failed,error: 修改自定义快捷键失败,错误: - + Add custom shortcut failed,error: 添加自定义快捷键失败,错误: - + Reset shortcut failed,error: 重置快捷键失败,错误: - + + Cannot use shortcut "%1",Please keep pressing the modifier keys such as Ctrl,Alt, and Shift before pressing the last key of the shortcut key + 无法使用快捷键"%1", 请保持按压Ctrl、Alt、Shift等修饰键后,再按压快捷键的最后一个键 + + Cannot use shortcut "%1", Because you cannot enter with this key.Please try again using Ctrl, Alt, or Shift at the same time. - 无法使用快捷键"%1",因为使用此键将无法输入,请同时使用Ctrl,Alt,Shift再试一次。 + 无法使用快捷键"%1",因为使用此键将无法输入,请同时使用Ctrl,Alt,Shift再试一次。 - + Shortcut keys %1 are already used in %2,Please try again! 快捷键%1已用于%2,请再试一次! -- 2.33.0