Skip to content

Commit

Permalink
feat: Custom Task Turn to Main Line: dde-file-manager Clears Search H…
Browse files Browse the repository at this point in the history
…istory

Custom Task Turn to Main Line: dde-file-manager Clears Search History

Log: Custom Task Turn to Main Line: dde-file-manager Clears Search History
Task: https://pms.uniontech.com/bug-view-252515.html
  • Loading branch information
liyigang1 authored and deepin-bot[bot] committed May 8, 2024
1 parent 2569149 commit a01e9f9
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 183 deletions.
11 changes: 11 additions & 0 deletions assets/configs/org.deepin.dde.file-manager.search.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
"description":"Used to determine whether to enable full-text search",
"permissions":"readwrite",
"visibility":"private"
},
"displaySearchHistory": {
"value":true,
"serial":0,
"flags":[],
"name":"Display search history",
"name[zh_CN]":"显示搜索记录",
"description[zh_CN]":"用于判断是否显示搜索记录",
"description":"Used to determine whether to enable display search history",
"permissions":"readwrite",
"visibility":"private"
}
}
}
Expand Down
129 changes: 118 additions & 11 deletions src/plugins/filemanager/core/dfmplugin-titlebar/views/addressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#include <dfm-base/widgets/filemanagerwindowsmanager.h>
#include <dfm-base/base/schemefactory.h>
#include <dfm-base/utils/fileutils.h>
#include <dfm-base/base/configs/dconfig/dconfigmanager.h>

#include <dfm-framework/event/event.h>

#include <dtkwidget_global.h>
#ifdef DTKWIDGET_CLASS_DSizeMode
# include <DSizeMode>
#endif
#include <DDialog>

#include <QCompleter>
#include <QFontMetrics>
Expand Down Expand Up @@ -115,6 +117,8 @@ void AddressBarPrivate::initConnect()
});

connect(pauseButton, &DIconButton::clicked, q, &AddressBar::pauseButtonClicked);
connect(DConfigManager::instance(), &DConfigManager::valueChanged,
this, &AddressBarPrivate::onDConfigValueChanged);

#ifdef DTKWIDGET_CLASS_DSizeMode
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::sizeModeChanged, this, [this]() {
Expand Down Expand Up @@ -151,12 +155,15 @@ void AddressBarPrivate::initData()

void AddressBarPrivate::updateHistory()
{
ipHistroyList.clear();
ipHistroyList = SearchHistroyManager::instance()->getIPHistory();

if (!DConfigManager::instance()->value(DConfigSearch::kSearchCfgPath,
DConfigSearch::kDisplaySearchHistory, true).toBool())
return;
historyList.clear();
historyList.append(SearchHistroyManager::instance()->getSearchHistroy());
isHistoryInCompleterModel = false;

ipHistroyList.clear();
ipHistroyList = SearchHistroyManager::instance()->getIPHistory();
}

/*!
Expand Down Expand Up @@ -213,6 +220,7 @@ void AddressBarPrivate::clearCompleterModel()

void AddressBarPrivate::updateCompletionState(const QString &text)
{
isClearSearch = false;
if (ipRegExp.exactMatch(text)) {
inputIsIpAddress = true;
completeIpAddress(text);
Expand Down Expand Up @@ -290,6 +298,7 @@ void AddressBarPrivate::onTravelCompletionListFinished()
if (urlCompleter->popup()->isHidden() && q->isVisible())
doComplete();
} else {
completionPrefix.clear();
completerView->hide();
q->setFocus(); // Hide will cause lost focus (weird..), so setFocus() here.
}
Expand All @@ -300,6 +309,60 @@ void AddressBarPrivate::onIndicatorTriggerd()
onReturnPressed();
}

void AddressBarPrivate::onDConfigValueChanged(const QString &config, const QString &key)
{
if (config != DConfigSearch::kSearchCfgPath || key != DConfigSearch::kDisplaySearchHistory)
return;

bool show = DConfigManager::instance()->value(config, key, false).toBool();
if (show) {
historyList.clear();
historyList.append(SearchHistroyManager::instance()->getSearchHistroy());
} else {
historyList.clear();
showHistoryList.clear();
completerModel.setStringList(showHistoryList);
}
isHistoryInCompleterModel = false;
}

void AddressBarPrivate::filterHistory(const QString &text)
{
completionPrefix = text;
showHistoryList.clear();
for (const auto &str : historyList) {
if (str.startsWith(text))
showHistoryList.push_back(str);
}
if (showHistoryList.count() > 0)
showHistoryList.append(QObject::tr("Clear search history"));
completerModel.setStringList(showHistoryList);
}

int AddressBarPrivate::showClearSearchHistory()
{
QString clearSearch = tr("Are you sure clear search histories?");
QStringList buttonTexts;
buttonTexts.append(tr("Cancel","button"));
buttonTexts.append(tr("Confirm","button"));

DDialog d;

if (!d.parentWidget()) {
d.setWindowFlags(d.windowFlags() | Qt::WindowStaysOnTopHint);
}
d.setIcon(QIcon::fromTheme("dialog-warning"));
d.setTitle(clearSearch);
d.addButton(buttonTexts[0], true, DDialog::ButtonNormal);
d.addButton(buttonTexts[1], false, DDialog::ButtonWarning);
d.setDefaultButton(1);
d.getButton(1)->setFocus();
d.moveToCenter();
int code = d.exec();
return code;
}


void AddressBarPrivate::requestCompleteByUrl(const QUrl &url)
{
if (!crumbController || !crumbController->isSupportedScheme(url.scheme())) {
Expand Down Expand Up @@ -329,7 +392,8 @@ void AddressBarPrivate::completeSearchHistory(const QString &text)
setIndicator(AddressBar::IndicatorType::Search);

// set completion prefix.
urlCompleter->setCompletionPrefix(text);
urlCompleter->setCompletionPrefix("");
filterHistory(text);

// Check if we already loaded history list in model
if (isHistoryInCompleterModel)
Expand All @@ -340,7 +404,7 @@ void AddressBarPrivate::completeSearchHistory(const QString &text)

// History completion.
isHistoryInCompleterModel = true;
completerModel.setStringList(historyList);
completerModel.setStringList(showHistoryList);
}

void AddressBarPrivate::completeIpAddress(const QString &text)
Expand Down Expand Up @@ -450,9 +514,13 @@ void AddressBarPrivate::onReturnPressed()

// add search history list
if (!dfmbase::FileUtils::isLocalFile(UrlRoute::fromUserInput(text))) {
if (!historyList.contains(text))
historyList.removeAll(text);
historyList.append(text);
if (DConfigManager::instance()->value(DConfigSearch::kSearchCfgPath,
DConfigSearch::kDisplaySearchHistory, true).toBool()) {
if (!historyList.contains(text))
historyList.removeAll(text);
historyList.append(text);
isHistoryInCompleterModel = false;
}
SearchHistroyManager::instance()->writeIntoSearchHistory(text);

if (protocolIPRegExp.exactMatch(text)) {
Expand All @@ -469,6 +537,13 @@ void AddressBarPrivate::onReturnPressed()
}

bool isSearch { false };
if (text == QObject::tr("Clear search history")) {
emit q->escKeyPressed();;
auto result = showClearSearchHistory();
if (result == DDialog::Accepted)
q->clearSearchHistory();
return;
}
TitleBarHelper::handlePressed(q, text, &isSearch);

if (isSearch) {
Expand All @@ -486,12 +561,20 @@ void AddressBarPrivate::insertCompletion(const QString &completion)
if (inputIsIpAddress) {
q->setText(completion);
} else {
if (completion == QObject::tr("Clear search history")) {
isClearSearch = true;
emit q->returnPressed();
return;
}

isClearSearch = false;
q->setText(completerBaseString + completion);
}
}

void AddressBarPrivate::onCompletionHighlighted(const QString &highlightedCompletion)
{
isClearSearch = false;
if (inputIsIpAddress) {
if (highlightedCompletion.isEmpty()) {
q->setText(completerBaseString);
Expand All @@ -502,9 +585,16 @@ void AddressBarPrivate::onCompletionHighlighted(const QString &highlightedComple
q->setText(highlightedCompletion);
q->setSelection(0, selectLength);
} else {
int completionPrefixLen = urlCompleter->completionPrefix().length();
int completionPrefixLen = indicatorType == AddressBar::IndicatorType::Search
? completionPrefix.length() : urlCompleter->completionPrefix().length();
int selectBeginPos = highlightedCompletion.length() - completionPrefixLen;
q->setText(completerBaseString + highlightedCompletion);
if (highlightedCompletion == QObject::tr("Clear search history")) {
q->setText(completerBaseString + lastEditedString);
isClearSearch = true;
} else {
q->setText(completerBaseString + highlightedCompletion);
isClearSearch = false;
}
q->setSelection(q->text().length() - selectBeginPos, q->text().length());
}
}
Expand Down Expand Up @@ -608,6 +698,20 @@ void AddressBar::showOnFocusLostOnce()
d->isKeepVisible = true;
}

QString AddressBar::text() const
{
if (d->isClearSearch && Search == d->indicatorType)
return QObject::tr("Clear search history");
return QLineEdit::text();
}

void AddressBar::clearSearchHistory()
{
d->historyList.clear();
SearchHistroyManager::instance()->clearHistory();
d->isHistoryInCompleterModel = false;
}

bool AddressBar::event(QEvent *e)
{
if (e->type() == QEvent::KeyPress) {
Expand Down Expand Up @@ -641,6 +745,7 @@ void AddressBar::focusOutEvent(QFocusEvent *e)
setFocus();
return;
}
d->completionPrefix.clear();
d->completerView->hide();
if (d->isKeepVisible) {
d->isKeepVisible = false;
Expand Down Expand Up @@ -672,7 +777,8 @@ void AddressBar::keyPressEvent(QKeyEvent *e)
if (d->isHistoryInCompleterModel && e->modifiers() == Qt::ShiftModifier && e->key() == Qt::Key_Delete) {
QString completeResult = d->completerView->currentIndex().data().toString();
bool ret = SearchHistroyManager::instance()->removeSearchHistory(completeResult);
if (ret) {
if (ret && DConfigManager::instance()->value(DConfigSearch::kSearchCfgPath,
DConfigSearch::kDisplaySearchHistory, true).toBool()) {
d->historyList.clear();
d->historyList.append(SearchHistroyManager::instance()->getSearchHistroy());
d->completerModel.setStringList(d->historyList);
Expand All @@ -688,6 +794,7 @@ void AddressBar::keyPressEvent(QKeyEvent *e)
case Qt::Key_Return:
e->accept();
d->completerView->hide();
d->completionPrefix.clear();
emit returnPressed();
return;
case Qt::Key_Tab:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AddressBar : public QLineEdit
void setCurrentUrl(const QUrl &url);
QUrl currentUrl();
void showOnFocusLostOnce();
QString text() const;
void clearSearchHistory();

protected:
bool event(QEvent *e) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
DWIDGET_USE_NAMESPACE
DFMBASE_USE_NAMESPACE

namespace DConfigSearch {
inline constexpr char kSearchCfgPath[] { "org.deepin.dde.file-manager.search" };
inline constexpr char kDisplaySearchHistory[] = "displaySearchHistory";
}

namespace dfmplugin_titlebar {
class CrumbInterface;
class AddressBarPrivate : public QObject
Expand All @@ -41,6 +46,7 @@ class AddressBarPrivate : public QObject
friend class AddressBar;
AddressBar *const q;
QStringList historyList;
QStringList showHistoryList;
QList<IPHistroyData> ipHistroyList;
QTimer timer;
DSpinner spinner;
Expand All @@ -52,22 +58,24 @@ class AddressBarPrivate : public QObject
QString completerBaseString;
QString lastEditedString;
AddressBar::IndicatorType indicatorType { AddressBar::IndicatorType::Search };
bool isHistoryInCompleterModel { false };
int lastPressedKey { Qt::Key_D }; // just an init value
int lastPreviousKey { Qt::Key_Control }; //记录上前一个按钮
bool isKeyPressed { false };
int selectPosStart { 0 };
CrumbInterface *crumbController { nullptr };
CompleterViewModel completerModel;
CompleterView *completerView { nullptr };
QCompleter *urlCompleter { nullptr };
CompleterViewDelegate *cpItemDelegate { nullptr };
// inputMethodEvent中获取不到选中的内容,故缓存光标开始位置以及选中长度
int selectPosStart { 0 };
int selectLength { 0 };
bool isKeepVisible { false };
bool isClearSearch { false };
bool isKeyPressed { false };
bool isHistoryInCompleterModel { false };
QRegExp ipRegExp; // 0.0.0.0-255.255.255.255
QRegExp protocolIPRegExp; // smb://ip, ftp://ip, sftp://ip
QString completionPrefix;
bool inputIsIpAddress { false };
bool isKeepVisible { false };

public:
explicit AddressBarPrivate(AddressBar *qq);
Expand Down Expand Up @@ -99,6 +107,9 @@ public Q_SLOTS:
void appendToCompleterModel(const QStringList &stringList);
void onTravelCompletionListFinished();
void onIndicatorTriggerd();
void onDConfigValueChanged(const QString &config, const QString &key);
void filterHistory(const QString &text);
int showClearSearchHistory();

protected:
virtual bool eventFilterResize(AddressBar *addressbar, QResizeEvent *event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void TitleBarWidget::initConnect()
connect(addressBar, &AddressBar::escKeyPressed, this, [this]() {
if (crumbBar->controller())
crumbBar->controller()->processAction(CrumbInterface::kEscKeyPressed);
addressBar->stopSpinner();
});
connect(addressBar, &AddressBar::lostFocus, this, [this]() {
if (crumbBar->controller())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ inline constexpr char kGroupSearch[] { SEARCH_SETTING_GROUP };
inline constexpr char kIndexInternal[] { SEARCH_SETTING_GROUP ".00_index_internal" };
inline constexpr char kIndexExternal[] { SEARCH_SETTING_GROUP ".01_index_external" };
inline constexpr char kFulltextSearch[] { SEARCH_SETTING_GROUP ".02_fulltext_search" };
inline constexpr char kDisplaySearchHistory[] { SEARCH_SETTING_GROUP ".03_display_search_history" };
}

namespace DConfig {
inline constexpr char kSearchCfgPath[] { "org.deepin.dde.file-manager.search" };
inline constexpr char kEnableFullTextSearch[] { "enableFullTextSearch" };
inline constexpr char kDisplaySearchHistory[] { "displaySearchHistory" };
}

DPSEARCH_END_NAMESPACE
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/filemanager/dfmplugin-search/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ void Search::regSearchSettingConfig()
SettingJsonGenerator::instance()->addCheckBoxConfig(SearchSettings::kFulltextSearch,
tr("Full-Text search"),
false);
SettingJsonGenerator::instance()->addCheckBoxConfig(SearchSettings::kDisplaySearchHistory,
tr("Display search history"),
true);
SettingBackend::instance()->addSettingAccessor(
SearchSettings::kFulltextSearch,
[]() {
Expand All @@ -153,6 +156,19 @@ void Search::regSearchSettingConfig()
DConfig::kEnableFullTextSearch,
val);
});

SettingBackend::instance()->addSettingAccessor(
SearchSettings::kDisplaySearchHistory,
[]() {
return DConfigManager::instance()->value(DConfig::kSearchCfgPath,
DConfig::kDisplaySearchHistory,
true);
},
[](const QVariant &val) {
DConfigManager::instance()->setValue(DConfig::kSearchCfgPath,
DConfig::kDisplaySearchHistory,
val);
});
}

void Search::bindEvents()
Expand Down
Loading

0 comments on commit a01e9f9

Please sign in to comment.