Add function of historycal record.

This commit is contained in:
iaom 2021-03-30 09:23:57 +08:00
parent ef6c651d66
commit 20322d4cd3
9 changed files with 186 additions and 87 deletions

View File

@ -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()));
}
}
}

View File

@ -31,6 +31,8 @@
//My demo can build access yet.
//MouseZhangZh
#include <QGSettings/QGSettings>
#include <QDBusConnection>
#include <QDBusInterface>
#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<QString, QVariant> m_cache;
QStringList m_history;
QMutex m_mutex;

View File

@ -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);
}
/**

View File

@ -109,6 +109,7 @@ private:
Q_SIGNALS:
void currentItemChanged();
void mousePressed();
private Q_SLOTS:
void clearLayout(QLayout *);

View File

@ -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) {

View File

@ -23,6 +23,7 @@
#include <QObject>
#include <QTreeView>
#include <QMouseEvent>
#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();

View File

@ -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()

View File

@ -26,13 +26,18 @@
#include <QPainter>
#include <QAction>
#include <QTimer>
#include <QStringListModel>
#include <QCompleter>
#include <QAbstractItemView>
#include <QVector4D>
#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;
};

View File

@ -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);