From 41dfe036663869c6c99e1a878afeb8ff27f39f2c Mon Sep 17 00:00:00 2001 From: xiaoming <2014500726@smail.xtu.edu.cn> Date: Tue, 13 Aug 2024 10:49:31 +0800 Subject: [PATCH] Add custom color feature to the highlight Signed-off-by: xiaoming <2014500726@smail.xtu.edu.cn> --- CHANGELOG.md | 2 + docs/changelog.md | 1 + lib/qtermwidget/qtermwidget.cpp | 9 ++++ lib/qtermwidget/qtermwidget.h | 1 + src/mainwindow.cpp | 73 +++++++++++++++++++++++++---- src/mainwindow.h | 2 + src/sessionswindow/sessionswindow.h | 4 ++ 7 files changed, 82 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4280fb5c..67f4e82c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ en-US: - Add the feature of selecting end-of-line sequence - Add status bar log information, SSH ciphers information - Add emphasized color border to the activated session in split screen mode +- Add custom color feature to the highlight - Fix the issue that clicking the new tab button in split screen mode may not create the session correctly or be located under the wrong tab group - Fix the issue that the SSH connection cannot be reconnected by tapping the Enter key in some cases @@ -17,6 +18,7 @@ zh-CN: - 增加选择行尾序列功能 - 增加状态栏日志信息,SSH加密算法信息 - 分屏模式下激活的会话增加强调色边框 +- 高亮功能增加自定义颜色功能 - 修复分屏模式下某些情况点击新标签按钮会话未正确创建或位于错误的标签页组下 - 修复ssh连接部分情况下无法通过敲击回车键发起重连的问题 diff --git a/docs/changelog.md b/docs/changelog.md index 83fd5ca1..74f25547 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -8,6 +8,7 @@ - Add the feature of selecting end-of-line sequence - Add status bar log information, SSH ciphers information - Add emphasized color border to the activated session in split screen mode +- Add custom color feature to the highlight - Fix the issue that clicking the new tab button in split screen mode may not create the session correctly or be located under the wrong tab group - Fix the issue that the SSH connection cannot be reconnected by tapping the Enter key in some cases diff --git a/lib/qtermwidget/qtermwidget.cpp b/lib/qtermwidget/qtermwidget.cpp index 8ba64a8c..fe0152d6 100644 --- a/lib/qtermwidget/qtermwidget.cpp +++ b/lib/qtermwidget/qtermwidget.cpp @@ -814,6 +814,15 @@ void QTermWidget::addHighLightText(const QString &text, const QColor &color) m_impl->m_terminalDisplay->repaint(); } +QMap QTermWidget::getHighLightTexts(void) +{ + QMap highLightTexts; + for (int i = 0; i < m_highLightTexts.size(); i++) { + highLightTexts.insert(m_highLightTexts.at(i)->text, m_highLightTexts.at(i)->color); + } + return highLightTexts; +} + bool QTermWidget::isContainHighLightText(const QString &text) { for (int i = 0; i < m_highLightTexts.size(); i++) { diff --git a/lib/qtermwidget/qtermwidget.h b/lib/qtermwidget/qtermwidget.h index 85812f59..695b16ca 100644 --- a/lib/qtermwidget/qtermwidget.h +++ b/lib/qtermwidget/qtermwidget.h @@ -254,6 +254,7 @@ class QTermWidget : public QWidget { bool isContainHighLightText(const QString &text); void removeHighLightText(const QString &text); void clearHighLightTexts(void); + QMap getHighLightTexts(void); void setWordCharacters(const QString &wordCharacters); QString wordCharacters(void); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f45e2633..c0db0cc7 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include "filedialog.h" #include "qtftp.h" @@ -1188,23 +1189,75 @@ void CentralWidget::terminalWidgetContextMenuBase(QMenu *menu,SessionsWindow *te menu->addAction(pasteAction); menu->addAction(selectAllAction); menu->addAction(findAction); + menu->addSeparator(); QString text = term->selectedText(); + QMenu *highlightMenu = new QMenu(tr("Highlight"),this); + menu->addMenu(highlightMenu); if(!text.isEmpty()) { - menu->addSeparator(); - QAction *highlightAction = new QAction(tr("Highlight/Unhighlight"),this); - highlightAction->setStatusTip(tr("Highlight/Unhighlight selected text")); - menu->addAction(highlightAction); + QAction *highlightAction = new QAction(tr("Highlight"),this); + highlightAction->setStatusTip(tr("Highlight selected text with random color")); + highlightMenu->addAction(highlightAction); connect(highlightAction,&QAction::triggered,this,[=](){ - QString text = term->selectedText(); - if(text.isEmpty()) { + if(isContainHighLightText(text)) { return; } - if(term->isContainHighLightText(text)) { - term->removeHighLightText(text); - } else { - term->addHighLightText(text, Qt::white); + uint8_t r = QRandomGenerator::global()->bounded(256); + uint8_t g = QRandomGenerator::global()->bounded(256); + uint8_t b = QRandomGenerator::global()->bounded(256); + lastHighlightColor = QColor(r,g,b); + term->addHighLightText(text, lastHighlightColor); + }); + QAction *highlightCustomAction = new QAction(tr("Highlight (Custom)"),this); + highlightCustomAction->setStatusTip(tr("Highlight selected text with custom color")); + highlightMenu->addAction(highlightCustomAction); + connect(highlightCustomAction,&QAction::triggered,this,[=](){ + if(isContainHighLightText(text)) { + return; + } + QColor color = QColorDialog::getColor(lastHighlightColor,this); + if(color.isValid()) { + lastHighlightColor = color; + term->addHighLightText(text, color); } }); + QAction *unhighlightAction = new QAction(tr("Unhighlight"),this); + unhighlightAction->setStatusTip(tr("Unhighlight selected text")); + highlightMenu->addAction(unhighlightAction); + connect(unhighlightAction,&QAction::triggered,this,[=](){ + term->removeHighLightText(text); + }); + } + QAction *clearHighlightAction = new QAction(tr("Clear Highlights"),this); + clearHighlightAction->setStatusTip(tr("Clear all highlighted text")); + highlightMenu->addAction(clearHighlightAction); + connect(clearHighlightAction,&QAction::triggered,this,[=](){ + term->clearHighLightTexts(); + }); + QMap highLightTextList = term->getHighLightTexts(); + if(!highLightTextList.isEmpty()) { + highlightMenu->addSeparator(); + foreach(QString text, highLightTextList.keys()) { + QMenu *subMenu = new QMenu(text,highlightMenu); + subMenu->setIcon(QFontIcon::icon(QChar(0xf0c8), highLightTextList[text])); + highlightMenu->addMenu(subMenu); + QAction *action = new QAction(tr("Remove"),this); + subMenu->addAction(action); + connect(action,&QAction::triggered,this,[=](){ + term->removeHighLightText(text); + }); + QAction *colorAction = new QAction(tr("Change Color"),this); + subMenu->addAction(colorAction); + connect(colorAction,&QAction::triggered,this,[=](){ + QColor color = QColorDialog::getColor(highLightTextList[text],this); + if(color.isValid()) { + lastHighlightColor = color; + term->removeHighLightText(text); + term->addHighLightText(text, color); + } + }); + } + } + if(!text.isEmpty()) { int translateService = globalOptionsWindow->getTranslateService(); QString targetLanguage[3]; switch (language.language()) { diff --git a/src/mainwindow.h b/src/mainwindow.h index da856bf0..f0c6631c 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -435,6 +435,8 @@ private slots: int currentLayout; bool enabledSyncSplitterMoved = false; + QColor lastHighlightColor = Qt::white; + QLocale language; bool isDarkTheme; QColor themeColor; diff --git a/src/sessionswindow/sessionswindow.h b/src/sessionswindow/sessionswindow.h index d2ec12fc..d4496c3f 100644 --- a/src/sessionswindow/sessionswindow.h +++ b/src/sessionswindow/sessionswindow.h @@ -351,6 +351,10 @@ class SessionsWindow : public QObject void clearHighLightTexts(void) { if(term) term->clearHighLightTexts(); } + QMap getHighLightTexts(void) { + if(term) return term->getHighLightTexts(); + return QMap(); + } void repaintDisplay(void) { if(term) term->repaintDisplay(); }