Skip to content

Commit

Permalink
Add custom color feature to the highlight
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <2014500726@smail.xtu.edu.cn>
  • Loading branch information
QQxiaoming committed Aug 13, 2024
1 parent fe3f871 commit 41dfe03
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,6 +18,7 @@ zh-CN:
- 增加选择行尾序列功能
- 增加状态栏日志信息,SSH加密算法信息
- 分屏模式下激活的会话增加强调色边框
- 高亮功能增加自定义颜色功能
- 修复分屏模式下某些情况点击新标签按钮会话未正确创建或位于错误的标签页组下
- 修复ssh连接部分情况下无法通过敲击回车键发起重连的问题

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions lib/qtermwidget/qtermwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,15 @@ void QTermWidget::addHighLightText(const QString &text, const QColor &color)
m_impl->m_terminalDisplay->repaint();
}

QMap<QString, QColor> QTermWidget::getHighLightTexts(void)
{
QMap<QString, QColor> 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++) {
Expand Down
1 change: 1 addition & 0 deletions lib/qtermwidget/qtermwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class QTermWidget : public QWidget {
bool isContainHighLightText(const QString &text);
void removeHighLightText(const QString &text);
void clearHighLightTexts(void);
QMap<QString, QColor> getHighLightTexts(void);

void setWordCharacters(const QString &wordCharacters);
QString wordCharacters(void);
Expand Down
73 changes: 63 additions & 10 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <QPluginLoader>
#include <QMap>
#include <QColorDialog>
#include <QRandomGenerator>

#include "filedialog.h"
#include "qtftp.h"
Expand Down Expand Up @@ -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<QString, QColor> 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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ private slots:
int currentLayout;
bool enabledSyncSplitterMoved = false;

QColor lastHighlightColor = Qt::white;

QLocale language;
bool isDarkTheme;
QColor themeColor;
Expand Down
4 changes: 4 additions & 0 deletions src/sessionswindow/sessionswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ class SessionsWindow : public QObject
void clearHighLightTexts(void) {
if(term) term->clearHighLightTexts();
}
QMap<QString, QColor> getHighLightTexts(void) {
if(term) return term->getHighLightTexts();
return QMap<QString, QColor>();
}
void repaintDisplay(void) {
if(term) term->repaintDisplay();
}
Expand Down

0 comments on commit 41dfe03

Please sign in to comment.