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 committed May 6, 2024
1 parent 3a4b50c commit 54c0ab7
Show file tree
Hide file tree
Showing 18 changed files with 2,791 additions and 487 deletions.
4 changes: 3 additions & 1 deletion src/dfm-base/base/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ bool Application::syncAppAttribute()
return appSetting()->sync();
}

QVariant Application::genericAttribute(Application::GenericAttribute ga)
QVariant Application::genericAttribute(Application::GenericAttribute ga, const QVariant &value)
{
const QString group(QT_STRINGIFY(GenericAttribute));
const QMetaEnum &me = QMetaEnum::fromType<GenericAttribute>();
const QString key = QString::fromLatin1(me.valueToKey(ga)).remove(0, 1);
if (value.isValid())
return genericSetting()->value(group, key, value);

return genericSetting()->value(group, key);
}
Expand Down
3 changes: 2 additions & 1 deletion src/dfm-base/base/application/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Application : public QObject
kShowDeleteConfirmDialog, // display the delete confirmation dialog
kHideLoopPartitions, // hide loop partitions
kShowThunmbnailInRemote, // show file thumbnail in remote dir
KDisplaySearchHistory, // display search history
};

Q_ENUM(GenericAttribute)
Expand All @@ -78,7 +79,7 @@ class Application : public QObject
static void setAppAttribute(ApplicationAttribute aa, const QVariant &value);
static bool syncAppAttribute();

static QVariant genericAttribute(GenericAttribute ga);
static QVariant genericAttribute(GenericAttribute ga, const QVariant &value = QVariant());
static void setGenericAttribute(GenericAttribute ga, const QVariant &value);
static bool syncGenericAttribute();

Expand Down
6 changes: 6 additions & 0 deletions src/dfm-base/base/configs/settingbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace dfmbase;
#define LV2_GROUP_OPEN_ACTION "00_base.00_open_action"
#define LV2_GROUP_NEW_TAB_WINDOWS "00_base.01_new_tab_windows"
#define LV2_GROUP_FILES_AND_FOLDERS "00_base.02_files_and_folders"
#define LV2_GROUP_OTHERS "00_base.03_others"

#define TOP_GROUP_WORKSPACE "02_workspace"
#define LV2_GROUP_VIEW "02_workspace.00_view"
Expand Down Expand Up @@ -57,6 +58,7 @@ BidirectionHash<QString, Application::GenericAttribute> SettingBackendPrivate::k
{ LV2_GROUP_COMPUTER_VIEW ".01_hide_builtin_partition", Application::kHiddenSystemPartition },
{ LV2_GROUP_COMPUTER_VIEW ".02_hide_loop_partitions", Application::kHideLoopPartitions },
{ LV2_GROUP_COMPUTER_VIEW ".04_show_filesystemtag_on_diskicon", Application::kShowFileSystemTagOnDiskIcon },
{ LV2_GROUP_OTHERS ".00_display_search_history", Application::KDisplaySearchHistory },
};

SettingBackend::SettingBackend(QObject *parent)
Expand Down Expand Up @@ -288,6 +290,10 @@ void SettingBackend::initBasicSettingConfig()
ins->addCheckBoxConfig(LV2_GROUP_FILES_AND_FOLDERS ".02_mixed_sort",
tr("Mix sorting of files and folders"),
false);
ins->addGroup(LV2_GROUP_OTHERS, tr("Others"));
ins->addCheckBoxConfig(LV2_GROUP_OTHERS ".00_display_search_history",
tr("Display search history"),
true);
}

void SettingBackend::initWorkspaceSettingConfig()
Expand Down
23 changes: 23 additions & 0 deletions src/dfm-base/utils/dialogmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,29 @@ int DialogManager::showUnableToVistDir(const QString &dir)
return code;
}

int DialogManager::showClearSearchHistories()
{
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;
}

DFMBASE_NAMESPACE::GlobalEventType DialogManager::showBreakSymlinkDialog(const QString &targetName, const QUrl &linkfile)
{
DDialog d(qApp->activeWindow());
Expand Down
1 change: 1 addition & 0 deletions src/dfm-base/utils/dialogmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class DialogManager : public QObject
void showRenameBusyErrDialog();
int showRenameNameDotBeginDialog();
int showUnableToVistDir(const QString &dir);
int showClearSearchHistories();

// link file not exist
DFMBASE_NAMESPACE::GlobalEventType showBreakSymlinkDialog(const QString &targetName, const QUrl &linkfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <dfm-base/widgets/filemanagerwindowsmanager.h>
#include <dfm-base/base/schemefactory.h>
#include <dfm-base/utils/fileutils.h>
#include <dfm-base/utils/dialogmanager.h>

#include <dtkwidget_global.h>
#ifdef DTKWIDGET_CLASS_DSizeMode
Expand Down Expand Up @@ -113,6 +114,8 @@ void AddressBarPrivate::initConnect()
});

connect(pauseButton, &DIconButton::clicked, q, &AddressBar::pauseButtonClicked);
connect(Application::instance(), &Application::genericAttributeChanged,
this, &AddressBarPrivate::onGenericAttributeChanged);

#ifdef DTKWIDGET_CLASS_DSizeMode
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::sizeModeChanged, this, [this]() {
Expand All @@ -138,7 +141,7 @@ void AddressBarPrivate::initData()
protocolIPRegExp.setPattern(R"(((smb)|(ftp)|(sftp))(://)((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3})");
protocolIPRegExp.setCaseSensitivity(Qt::CaseInsensitive);
// 设置补全组件
urlCompleter = new QCompleter(this);
urlCompleter = new DCompleter(this);
setCompleter(urlCompleter);

// 设置补全选择组件为popup的焦点
Expand All @@ -149,12 +152,15 @@ void AddressBarPrivate::initData()

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

if (!Application::instance()->genericAttribute(Application::KDisplaySearchHistory, true).toBool())
return;
historyList.clear();
historyList.append(SearchHistroyManager::instance()->getSearchHistroy());
isHistoryInCompleterModel = false;

ipHistroyList.clear();
ipHistroyList = SearchHistroyManager::instance()->getIPHistory();
historyList.append(QObject::tr("Clear search history"));
}

/*!
Expand All @@ -173,7 +179,7 @@ void AddressBarPrivate::setIndicator(AddressBar::IndicatorType type)
updateIndicatorIcon();
}

void AddressBarPrivate::setCompleter(QCompleter *c)
void AddressBarPrivate::setCompleter(DCompleter *c)
{
if (urlCompleter) {
urlCompleter->disconnect();
Expand All @@ -193,10 +199,10 @@ void AddressBarPrivate::setCompleter(QCompleter *c)
completerView->setItemDelegate(cpItemDelegate);
completerView->setAttribute(Qt::WA_InputMethodEnabled);

connect(urlCompleter, QOverload<const QString &>::of(&QCompleter::activated),
connect(urlCompleter, QOverload<const QString &>::of(&DCompleter::activated),
this, &AddressBarPrivate::insertCompletion);

connect(urlCompleter, QOverload<const QString &>::of(&QCompleter::highlighted),
connect(urlCompleter, QOverload<const QString &>::of(&DCompleter::highlighted),
this, &AddressBarPrivate::onCompletionHighlighted);

connect(urlCompleter->completionModel(), &QAbstractItemModel::modelReset,
Expand All @@ -211,6 +217,7 @@ void AddressBarPrivate::clearCompleterModel()

void AddressBarPrivate::updateCompletionState(const QString &text)
{
urlCompleter->setFilterMode(Qt::MatchStartsWith);
if (ipRegExp.exactMatch(text)) {
inputIsIpAddress = true;
completeIpAddress(text);
Expand Down Expand Up @@ -249,7 +256,7 @@ void AddressBarPrivate::doComplete()
if (completerView->isHidden()) {
urlCompleter->complete(q->rect().adjusted(0, 5, 0, 5));
} else {
urlCompleter->metaObject()->invokeMethod(urlCompleter, "_q_autoResizePopup");
urlCompleter->metaObject()->invokeMethod(urlCompleter, "onAutoResizePopup");
}
if (urlCompleter->completionCount() == 1
&& lastPressedKey != Qt::Key_Backspace
Expand Down Expand Up @@ -298,6 +305,22 @@ void AddressBarPrivate::onIndicatorTriggerd()
onReturnPressed();
}

void AddressBarPrivate::onGenericAttributeChanged(Application::GenericAttribute ga, const QVariant &value)
{
if (ga != Application::KDisplaySearchHistory)
return;
auto show = value.toBool();
if (show) {
historyList.clear();
historyList.append(SearchHistroyManager::instance()->getSearchHistroy());
historyList.append(QObject::tr("Clear search history"));
} else {
historyList.clear();
completerModel.setStringList(historyList);
}
isHistoryInCompleterModel = false;
}

void AddressBarPrivate::requestCompleteByUrl(const QUrl &url)
{
if (!crumbController || !crumbController->isSupportedScheme(url.scheme())) {
Expand All @@ -323,6 +346,7 @@ void AddressBarPrivate::requestCompleteByUrl(const QUrl &url)

void AddressBarPrivate::completeSearchHistory(const QString &text)
{
urlCompleter->setFilterMode(Qt::MatchRegExp);
// Update Icon
setIndicator(AddressBar::IndicatorType::Search);

Expand Down Expand Up @@ -448,9 +472,15 @@ 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 (Application::instance()->genericAttribute(Application::KDisplaySearchHistory, true).toBool()) {
if (!historyList.contains(text))
historyList.removeAll(text);
historyList.append(text);
historyList.removeOne(QObject::tr("Clear search history"));
historyList.append(text);
historyList.append(QObject::tr("Clear search history"));
isHistoryInCompleterModel = false;
}
SearchHistroyManager::instance()->writeIntoSearchHistory(text);

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

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

if (isSearch) {
Expand All @@ -484,6 +521,13 @@ 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);
}
}
Expand All @@ -502,7 +546,13 @@ void AddressBarPrivate::onCompletionHighlighted(const QString &highlightedComple
} else {
int completionPrefixLen = 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 @@ -601,6 +651,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 @@ -665,9 +729,10 @@ 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 && Application::instance()->genericAttribute(Application::KDisplaySearchHistory, true).toBool()) {
d->historyList.clear();
d->historyList.append(SearchHistroyManager::instance()->getSearchHistroy());
d->historyList.append(QObject::tr("Clear search history"));
d->completerModel.setStringList(d->historyList);
}
}
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
Loading

0 comments on commit 54c0ab7

Please sign in to comment.