diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index 9144617..7d3c6d4 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -35,10 +35,25 @@ GlobalSettings *GlobalSettings::getInstance() GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) { - m_settings = new QSettings("org.ukui/ukui-search", "ukui-search", this); + m_settings = new QSettings(MAIN_SETTINGS, QSettings::IniFormat, this); // m_settings->setAtomicSyncRequired(false); - m_block_dirs_settings = new QSettings("org.ukui/ukui-search","ukui-search-block-dirs",this); + m_block_dirs_settings = new QSettings(BLOCK_DIRS,QSettings::IniFormat, this); m_block_dirs_settings->setIniCodec(QTextCodec::codecForName("UTF-8")); + + m_search_record_settings = new QSettings(SEARCH_HISTORY, QSettings::IniFormat , this); + m_search_record_settings->setIniCodec(QTextCodec::codecForName("UTF-8")); + for(QString i:m_search_record_settings->allKeys()) + { + m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit())); + } + if(!QDBusConnection::sessionBus().connect("org.kylinssoclient.dbus", + "/org/kylinssoclient/path", + "org.freedesktop.kylinssoclient.interface", + "keyChanged", + this, SLOT(updateSearchHistory(QString)))) + + qWarning()<<"Kylinssoclient Dbus connect fail!"; + this->forceSync(); //the default number of transparency in mainwindow is 0.7 //if someone changes the num in mainwindow, here should be modified too @@ -141,84 +156,88 @@ QStringList GlobalSettings::getBlockDirs() return m_block_dirs_settings->allKeys(); } -void GlobalSettings::appendCloudData(const QString &key, const QString &value) -{ - QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); - m_qSettings->beginGroup(key); - QStringList values = m_qSettings->value(key).toStringList(); - m_qSettings->endGroup(); - if (values.contains(value)) { - values.removeOne(value); - } - values.insert(0,value); +//void GlobalSettings::appendCloudData(const QString &key, const QString &value) +//{ +// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); +// m_qSettings->beginGroup(key); +// QStringList values = m_qSettings->value(key).toStringList(); +// m_qSettings->endGroup(); +// if (values.contains(value)) { +// values.removeOne(value); +// } +// values.insert(0,value); - m_qSettings->beginGroup(key); - m_qSettings->setValue(key, values); - m_qSettings->endGroup(); - if (m_qSettings) { - delete m_qSettings; - m_qSettings = NULL; - } +// m_qSettings->beginGroup(key); +// m_qSettings->setValue(key, values); +// m_qSettings->endGroup(); +// if (m_qSettings) { +// delete m_qSettings; +// m_qSettings = NULL; +// } +//} + +void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) +{ + QStringList keys = m_search_record_settings->allKeys(); + if(keys.contains(QString(QUrl::toPercentEncoding(word)))) + m_history.removeOne(word); + m_search_record_settings->setValue(QString(QUrl::toPercentEncoding(word)), time.toString("yyyy-MM-dd hh:mm:ss")); + if(keys.size() >= 20) + m_search_record_settings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst()))); + m_history.append(word); } -void GlobalSettings::setCloudData(const QString &key, const QStringList &values) +QStringList GlobalSettings::getSearchRecord() { - QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); - m_qSettings->beginGroup(key); - m_qSettings->setValue(key, values); - m_qSettings->endGroup(); - if (m_qSettings) { - delete m_qSettings; - m_qSettings = NULL; - } + return m_history; } -bool GlobalSettings::removeOneCloudData(const QString &key, const QString &value) -{ - if (!QFileInfo(CLOUD_FILE).isFile()) return false; - QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); - m_qSettings->beginGroup(key); - QStringList values = m_qSettings->value(key).toStringList(); - m_qSettings->endGroup(); - if (values.contains(value)) { - values.removeOne(value); - } else return false; - m_qSettings->beginGroup(key); - m_qSettings->setValue(key, values); - m_qSettings->endGroup(); - if (m_qSettings) { - delete m_qSettings; - m_qSettings = NULL; - } - return true; -} +//bool GlobalSettings::removeOneCloudData(const QString &key, const QString &value) +//{ +// if (!QFileInfo(CLOUD_FILE).isFile()) return false; +// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); +// m_qSettings->beginGroup(key); +// QStringList values = m_qSettings->value(key).toStringList(); +// m_qSettings->endGroup(); +// if (values.contains(value)) { +// values.removeOne(value); +// } else return false; +// m_qSettings->beginGroup(key); +// m_qSettings->setValue(key, values); +// m_qSettings->endGroup(); +// if (m_qSettings) { +// delete m_qSettings; +// m_qSettings = NULL; +// } +// return true; +//} -bool GlobalSettings::removeAllCloudData(const QString &key) -{ - if (!QFileInfo(CLOUD_FILE).isFile()) return false; - QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); - m_qSettings->beginGroup(key); - m_qSettings->beginGroup(key); - m_qSettings->setValue(key, QStringList()); - m_qSettings->endGroup(); - if (m_qSettings) { - delete m_qSettings; - m_qSettings = NULL; - } - return true; -} +//bool GlobalSettings::removeAllCloudData(const QString &key) +//{ +// if (!QFileInfo(CLOUD_FILE).isFile()) return false; +// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); +// m_qSettings->beginGroup(key); +// m_qSettings->beginGroup(key); +// m_qSettings->setValue(key, QStringList()); +// m_qSettings->endGroup(); +// if (m_qSettings) { +// delete m_qSettings; +// m_qSettings = NULL; +// } +// return true; +//} -QStringList GlobalSettings::getCloudData(const QString &key) -{ - if (!QFileInfo(CLOUD_FILE).isFile()) return QStringList(); - QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); - m_qSettings->beginGroup(key); - QStringList values = m_qSettings->value(key).toStringList(); - m_qSettings->endGroup(); - if(m_qSettings) - delete m_qSettings; - return values; -} +//QStringList GlobalSettings::getCloudData(const QString &key) +//{ +// if (!QFileInfo(CLOUD_FILE).isFile()) return QStringList(); +// QSettings * m_qSettings = new QSettings(CLOUD_FILE, QSettings::IniFormat); +// m_qSettings->beginGroup(key); +// QStringList values = m_qSettings->value(key).toStringList(); +// m_qSettings->endGroup(); +// if(m_qSettings) +// delete m_qSettings; +// return values; +//} //here should be override //MouseZhangZh @@ -253,3 +272,16 @@ void GlobalSettings::forceSync(const QString &key) m_cache.insert(key, m_settings->value(key)); } } + +void GlobalSettings::updateSearchHistory(QString key) +{ + if(key == "search") + { + m_search_record_settings->sync(); + m_history.clear(); + for(QString i:m_search_record_settings->allKeys()) + { + m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit())); + } + } +} diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index c9aa011..3fe4250 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -31,6 +31,8 @@ //My demo can build access yet. //MouseZhangZh #include +#include +#include #include "libsearch_global.h" #define CONTROL_CENTER_PERSONALISE_GSETTINGS_ID "org.ukui.control-center.personalise" @@ -44,9 +46,11 @@ #define PATH_NOT_IN_HOME 2; #define PATH_PARENT_BLOCKED 3; -#define CLOUD_FILE QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-cloud.conf" -#define CLOUD_HISTORY "history" -#define CLOUD_APPLICATIONS "applications" +#define MAIN_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search.conf" +#define BLOCK_DIRS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-block-dirs.conf" +#define SEARCH_HISTORY QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-history.conf" +//#define CLOUD_HISTORY "history" +//#define CLOUD_APPLICATIONS "applications" class LIBSEARCH_EXPORT GlobalSettings : public QObject { @@ -74,13 +78,15 @@ public Q_SLOTS: */ bool setBlockDirs(const QString& path, int &returnCode,bool remove = false); QStringList getBlockDirs(); - void appendCloudData(const QString& key, const QString& value); - void setCloudData(const QString& key, const QStringList& values); - bool removeOneCloudData(const QString& key, const QString& value); - bool removeAllCloudData(const QString& key); - QStringList getCloudData(const QString& key); +// void appendCloudData(const QString& key, const QString& value); + void setSearchRecord(const QString &word, const QDateTime &time); + QStringList getSearchRecord(); +// bool removeOneCloudData(const QString& key, const QString& value); +// bool removeAllCloudData(const QString& key); +// QStringList getCloudData(const QString& key); void forceSync(const QString& = nullptr); + void updateSearchHistory(QString key); private: explicit GlobalSettings(QObject *parent = nullptr); @@ -89,7 +95,9 @@ private: QSettings* m_settings; QGSettings* m_gsettings; QSettings *m_block_dirs_settings; + QSettings *m_search_record_settings; QMap m_cache; + QStringList m_history; QMutex m_mutex; diff --git a/src/content-widget.cpp b/src/content-widget.cpp index 5e39292..c60ff71 100644 --- a/src/content-widget.cpp +++ b/src/content-widget.cpp @@ -303,6 +303,7 @@ void ContentWidget::setupConnect(SearchListView * listview) { connect(listview,&SearchListView::currentSelectPos,[=](QPoint pos){ m_resultListArea->ensureVisible(pos.x(),pos.y()); }); + connect(listview,&SearchListView::mousePressed,this,&ContentWidget::mousePressed); } /** diff --git a/src/content-widget.h b/src/content-widget.h index fa125c8..143873a 100644 --- a/src/content-widget.h +++ b/src/content-widget.h @@ -109,6 +109,7 @@ private: Q_SIGNALS: void currentItemChanged(); + void mousePressed(); private Q_SLOTS: void clearLayout(QLayout *); diff --git a/src/control/search-list-view.cpp b/src/control/search-list-view.cpp index c66c2ca..530ee39 100644 --- a/src/control/search-list-view.cpp +++ b/src/control/search-list-view.cpp @@ -152,6 +152,15 @@ int SearchListView::getLength() return m_item->getCurrentSize(); } +void SearchListView::mousePressEvent(QMouseEvent *event) +{ + if(event->button() == Qt::LeftButton) + { + Q_EMIT mousePressed(); + } + QTreeView::mousePressEvent(event); +} + //获取当前选项所属搜索类型 int SearchListView::getCurrentType() { switch (m_type) { diff --git a/src/control/search-list-view.h b/src/control/search-list-view.h index b343425..86902db 100644 --- a/src/control/search-list-view.h +++ b/src/control/search-list-view.h @@ -23,6 +23,7 @@ #include #include +#include #include "model/search-item-model.h" #include "model/search-item.h" #include "highlight-item-delegate.h" @@ -60,6 +61,8 @@ public: int getType(); int getLength(); bool isHidden = false; +protected: + void mousePressEvent(QMouseEvent *event) override; private: SearchItemModel * m_model = nullptr; SearchItem * m_item = nullptr; @@ -72,6 +75,7 @@ private: Q_SIGNALS: void currentRowChanged(const int&, const QString&); void currentSelectPos(QPoint pos); + void mousePressed(); public Q_SLOTS: void clearSelection(); diff --git a/src/input-box.cpp b/src/input-box.cpp index c971744..cf2c5e8 100644 --- a/src/input-box.cpp +++ b/src/input-box.cpp @@ -23,7 +23,7 @@ /** * @brief ukui-search顶部搜索界面 */ -SeachBarWidget::SeachBarWidget() +SeachBarWidget::SeachBarWidget(QWidget *parent):QWidget(parent) { } @@ -54,7 +54,7 @@ SeachBar::~SeachBar() /** * @brief 顶部搜索框所在界面的布局 */ -SearchBarHLayout::SearchBarHLayout() +SearchBarHLayout::SearchBarHLayout(QWidget *parent):QHBoxLayout(parent) { initUI(); @@ -86,6 +86,11 @@ SearchBarHLayout::~SearchBarHLayout() delete m_timer; m_timer = NULL; } + if(m_queryLineEdit) + { + delete m_queryLineEdit; + m_queryLineEdit = nullptr; + } } /** @@ -93,12 +98,13 @@ SearchBarHLayout::~SearchBarHLayout() */ void SearchBarHLayout::initUI() { - m_queryLineEdit = new SearchLineEdit; + m_queryLineEdit = new SearchLineEdit(); m_queryLineEdit->installEventFilter(this); m_queryLineEdit->setTextMargins(30,1,0,1); this->setContentsMargins(0,0,0,0); this->setAlignment(m_queryLineEdit,Qt::AlignCenter); this->addWidget(m_queryLineEdit); + m_queryWidget = new QWidget(m_queryLineEdit); m_queryWidget->setFocusPolicy(Qt::NoFocus); m_queryWidget->setStyleSheet("border:0px;background:transparent"); @@ -139,6 +145,11 @@ void SearchBarHLayout::initUI() }); } +void SearchBarHLayout::effectiveSearchRecord() +{ + m_queryLineEdit->record(); +} + void SearchBarHLayout::focusIn() { m_queryLineEdit->setFocus(); } @@ -201,6 +212,14 @@ SearchLineEdit::SearchLineEdit() // this->setContextMenuPolicy(Qt::NoContextMenu); this->setMaxLength(100); + m_completer = new QCompleter(this); + m_model = new QStringListModel(this); + m_model->setStringList(GlobalSettings::getInstance()->getSearchRecord()); + m_completer->setModel(m_model); + m_completer->setCompletionMode(QCompleter::InlineCompletion); + m_completer->setCaseSensitivity(Qt::CaseInsensitive); + + setCompleter(m_completer); //这是搜索框图标,要改 // QAction *searchAction = new QAction(this); @@ -213,6 +232,18 @@ SearchLineEdit::SearchLineEdit() QDBusConnection::sessionBus().registerObject("/lineEdit/textChanged", this,QDBusConnection :: ExportAllSlots | QDBusConnection :: ExportAllSignals); connect(this, &QLineEdit::textChanged, this, &SearchLineEdit::lineEditTextChanged); + connect(this, &QLineEdit::textChanged, this, [=](){ + m_isRecorded = false; + }); +} + +void SearchLineEdit::record() +{ + if(m_isRecorded == true||text().size() <= 1||text().isEmpty()) + return; + GlobalSettings::getInstance()->setSearchRecord(text(),QDateTime::currentDateTime()); + m_isRecorded = true; + m_model->setStringList(GlobalSettings::getInstance()->getSearchRecord()); } SearchLineEdit::~SearchLineEdit() diff --git a/src/input-box.h b/src/input-box.h index 73a7c32..ab60339 100644 --- a/src/input-box.h +++ b/src/input-box.h @@ -26,13 +26,18 @@ #include #include #include +#include +#include +#include +#include +#include "global-settings.h" class SearchLineEdit; class SeachBarWidget:public QWidget { public: - SeachBarWidget(); + SeachBarWidget(QWidget *parent = nullptr); ~SeachBarWidget(); }; @@ -50,7 +55,7 @@ class SearchBarHLayout : public QHBoxLayout { Q_OBJECT public: - SearchBarHLayout(); + SearchBarHLayout(QWidget *parent = nullptr); ~SearchBarHLayout(); void clearText(); QString text(); @@ -74,6 +79,8 @@ private: Q_SIGNALS: void textChanged(QString text); +public Q_SLOTS: + void effectiveSearchRecord(); }; class SearchBarWidgetLayout : public QHBoxLayout @@ -97,8 +104,13 @@ class SearchLineEdit : public QLineEdit Q_CLASSINFO("D-Bus Interface", "org.ukui.search.inputbox") public: SearchLineEdit(); + void record(); ~SearchLineEdit(); private Q_SLOTS: void lineEditTextChanged(QString arg); +private: + QStringListModel *m_model = nullptr; + QCompleter *m_completer= nullptr; + bool m_isRecorded = false; }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1df0d8d..a0c9298 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -224,14 +224,15 @@ void MainWindow::initUi() m_contentFrame = new ContentWidget(m_frame);//内容栏 - m_searchWidget = new SeachBarWidget; - m_searchLayout = new SearchBarHLayout; + m_searchWidget = new SeachBarWidget(this); + m_searchLayout = new SearchBarHLayout(this); m_searchWidget->setLayout(m_searchLayout); m_searchWidget->setFixedHeight(44); mainlayout->addWidget(m_titleFrame); mainlayout->addWidget(m_contentFrame); mainlayout->addWidget(m_searchWidget); + connect(m_contentFrame,&ContentWidget::mousePressed,m_searchLayout,&SearchBarHLayout::effectiveSearchRecord); connect(QApplication::primaryScreen(), &QScreen::geometryChanged, this, &MainWindow::monitorResolutionChange);