feat(homepage): Init & refresh homepage with interface provided by ConfigFile.

Description: 加入初始化和刷新Homepage的接口调用

Log: 加入初始化和刷新Homepage的接口调用
This commit is contained in:
zhangjiaping 2021-01-08 10:27:47 +08:00
parent cc54028c35
commit 443c434019
7 changed files with 84 additions and 45 deletions

View File

@ -2,6 +2,7 @@
#include <QDebug> #include <QDebug>
#include <QLabel> #include <QLabel>
#include <QTimer> #include <QTimer>
#include "config-file.h"
ContentWidget::ContentWidget(QWidget * parent):QStackedWidget(parent) ContentWidget::ContentWidget(QWidget * parent):QStackedWidget(parent)
{ {
@ -56,6 +57,10 @@ void ContentWidget::initUI() {
m_resultListArea->setWidget(m_resultList); m_resultListArea->setWidget(m_resultList);
m_resultListArea->setWidgetResizable(true); m_resultListArea->setWidgetResizable(true);
m_detailView = new SearchDetailView(m_resultDetailArea); m_detailView = new SearchDetailView(m_resultDetailArea);
connect(m_detailView, &SearchDetailView::configFileChanged, this, [ = ]() {
clearLayout(m_homePageLyt);
initHomePage();
});
m_resultDetailArea->setWidget(m_detailView); m_resultDetailArea->setWidget(m_detailView);
m_resultDetailArea->setWidgetResizable(true); m_resultDetailArea->setWidgetResizable(true);
m_resultListArea->setStyleSheet("QScrollArea{background:transparent;}"); m_resultListArea->setStyleSheet("QScrollArea{background:transparent;}");
@ -70,13 +75,29 @@ void ContentWidget::initUI() {
* @brief ContentWidget::initHomePage homepage填充内容 * @brief ContentWidget::initHomePage homepage填充内容
* @param lists * @param lists
*/ */
void ContentWidget::initHomePage(const QVector<QStringList>& lists) { void ContentWidget::initHomePage() {
QVector<QStringList> lists;
QMap<QString, QStringList> map = ConfigFile::readConfig();
QStringList commonlyList;
commonlyList = map.value("Commonly");
QStringList recentlyList;
recentlyList = map.value("Recently");
QStringList quicklyList;
quicklyList<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"Theme/主题/更改壁纸";
lists.append(commonlyList);
lists.append(recentlyList);
lists.append(quicklyList);
for (int i = 0; i < lists.count(); i++) { for (int i = 0; i < lists.count(); i++) {
if (lists.at(i).isEmpty())
continue;
QWidget * listWidget = new QWidget(m_homePage); QWidget * listWidget = new QWidget(m_homePage);
QVBoxLayout * itemWidgetLyt = new QVBoxLayout(listWidget); QVBoxLayout * itemWidgetLyt = new QVBoxLayout(listWidget);
QLabel * titleLabel = new QLabel(listWidget); QLabel * titleLabel = new QLabel(listWidget);
QWidget * itemWidget = new QWidget(listWidget); QWidget * itemWidget = new QWidget(listWidget);
if (i == 1) { if (i == 1) {
if (lists.at(i).length() <= 2) itemWidget->setFixedHeight(48);
else itemWidget->setFixedHeight(112);
titleLabel->setText(tr("Recently Opened")); titleLabel->setText(tr("Recently Opened"));
QGridLayout * layout = new QGridLayout(itemWidget); QGridLayout * layout = new QGridLayout(itemWidget);
layout->setSpacing(8); layout->setSpacing(8);
@ -87,6 +108,7 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
layout->addWidget(item, j / 2, j % 2); layout->addWidget(item, j / 2, j % 2);
} }
} else { } else {
itemWidget->setFixedHeight(136);
QHBoxLayout * layout = new QHBoxLayout(itemWidget); QHBoxLayout * layout = new QHBoxLayout(itemWidget);
layout->setSpacing(8); layout->setSpacing(8);
layout->setContentsMargins(0, 0, 0, 0); layout->setContentsMargins(0, 0, 0, 0);
@ -95,12 +117,12 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
HomePageItem * item = new HomePageItem(itemWidget, i, path); HomePageItem * item = new HomePageItem(itemWidget, i, path);
layout->addWidget(item); layout->addWidget(item);
} }
if (i) { for (int j = 0; j < 4 - lists.at(i).length(); j++) {
titleLabel->setText(tr("Open Quickly"));
QWidget * emptyItem = new QWidget(itemWidget); QWidget * emptyItem = new QWidget(itemWidget);
emptyItem->setFixedSize(136, 136); //占位用widget,若后续快捷打开有扩展项可删除此widget emptyItem->setFixedSize(136, 136); //占位用widget,少于4项会补全后方占位
layout->addWidget(emptyItem); layout->addWidget(emptyItem);
} }
if (i) titleLabel->setText(tr("Open Quickly"));
else titleLabel->setText(tr("Commonly Used")); else titleLabel->setText(tr("Commonly Used"));
} }
itemWidgetLyt->setSpacing(6); itemWidgetLyt->setSpacing(6);
@ -109,13 +131,14 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
itemWidgetLyt->addWidget(itemWidget); itemWidgetLyt->addWidget(itemWidget);
m_homePageLyt->addWidget(listWidget); m_homePageLyt->addWidget(listWidget);
} }
m_homePageLyt->addStretch();
} }
/** /**
* @brief setPageType * @brief setPageType
* @param type * @param type
*/ */
void ContentWidget::setPage(const int& type){ void ContentWidget::setPage(const int& type) {
m_currentType = type; m_currentType = type;
} }
@ -135,7 +158,8 @@ int ContentWidget::currentPage() {
*/ */
void ContentWidget::refreshSearchList(const QVector<int>& types, const QVector<QStringList>& lists, const QString& keyword) { void ContentWidget::refreshSearchList(const QVector<int>& types, const QVector<QStringList>& lists, const QString& keyword) {
if (!m_listLyt->isEmpty()) { if (!m_listLyt->isEmpty()) {
clearSearchList(); clearLayout(m_listLyt);
m_resultList->setFixedHeight(0);
} }
bool isEmpty = true; bool isEmpty = true;
QStringList bestList; QStringList bestList;
@ -229,11 +253,13 @@ QString ContentWidget::getTitleName(const int& type) {
} }
/** /**
* @brief ContentWidget::clearSearchList * @brief ContentWidget::clearLayout
* @param layout
*/ */
void ContentWidget::clearSearchList() { void ContentWidget::clearLayout(QLayout * layout) {
if (! layout) return;
QLayoutItem * child; QLayoutItem * child;
while ((child = m_listLyt->takeAt(0)) != 0) { while ((child = layout->takeAt(0)) != 0) {
if(child->widget()) if(child->widget())
{ {
child->widget()->setParent(NULL); //防止删除后窗口看上去没有消失 child->widget()->setParent(NULL); //防止删除后窗口看上去没有消失
@ -241,7 +267,6 @@ void ContentWidget::clearSearchList() {
delete child; delete child;
} }
child = NULL; child = NULL;
m_resultList->setFixedHeight(0);
} }

View File

@ -18,10 +18,11 @@ public:
void setPage(const int&); void setPage(const int&);
int currentPage(); int currentPage();
void refreshSearchList(const QVector<int>&, const QVector<QStringList>&, const QString&); void refreshSearchList(const QVector<int>&, const QVector<QStringList>&, const QString&);
void initHomePage(const QVector<QStringList>&); void initHomePage();
void setContentList(const QStringList&); void setContentList(const QStringList&);
private: private:
void initUI(); void initUI();
void clearHomepage();
QStringList m_contentList; QStringList m_contentList;
QWidget * m_homePage = nullptr; QWidget * m_homePage = nullptr;
QVBoxLayout * m_homePageLyt = nullptr; QVBoxLayout * m_homePageLyt = nullptr;
@ -44,7 +45,7 @@ Q_SIGNALS:
void currentItemChanged(); void currentItemChanged();
private Q_SLOTS: private Q_SLOTS:
void clearSearchList(); void clearLayout(QLayout *);
}; };
#endif // CONTENTWIDGET_H #endif // CONTENTWIDGET_H

View File

@ -1,19 +1,22 @@
#include "config-file.h" #include "config-file.h"
void ConfigFile::writeCommonly(QString message){ bool ConfigFile::writeCommonly(QString message){
QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat); QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
QStringList messagelist=message.split("/"); QStringList messagelist=message.split("/");
QString appname=messagelist.last(); QString appname=messagelist.last();
if(!appname.contains("desktop")) if(!appname.contains("desktop"))
return; return false;
m_qSettings->beginGroup("Commonly"); m_qSettings->beginGroup("Commonly");
QStringList quickly=m_qSettings->allKeys(); QStringList quickly=m_qSettings->allKeys();
if(quickly.contains(message)){ if(quickly.contains(message.mid(1, message.length()-1))){
m_qSettings->setValue(message,m_qSettings->value(message).toInt()+1); m_qSettings->setValue(message,m_qSettings->value(message).toInt()+1);
}else{ }else{
m_qSettings->setValue(message,1); m_qSettings->setValue(message,1);
} }
m_qSettings->endGroup(); m_qSettings->endGroup();
if(m_qSettings)
delete m_qSettings;
return true;
} }
QStringList ConfigFile::readCommonly(){ QStringList ConfigFile::readCommonly(){
@ -28,7 +31,6 @@ QStringList ConfigFile::readCommonly(){
m_qSettings->endGroup(); m_qSettings->endGroup();
QMap<QString, int>::iterator iter =quicklycount.begin(); QMap<QString, int>::iterator iter =quicklycount.begin();
QVector<QPair<QString, int>> vec; QVector<QPair<QString, int>> vec;
QString iconamePah;
while(iter !=quicklycount.end()) { while(iter !=quicklycount.end()) {
vec.push_back(qMakePair(iter.key(), iter.value())); vec.push_back(qMakePair(iter.key(), iter.value()));
iter++; iter++;
@ -37,14 +39,14 @@ QStringList ConfigFile::readCommonly(){
return (l.second > r.second); return (l.second > r.second);
}); });
for(int j=0;j<vec.size();j++){ for(int j=0;j<vec.size();j++){
returnlist.append(vec.at(j).first); returnlist.append("/" + vec.at(j).first);
} }
if(m_qSettings) if(m_qSettings)
delete m_qSettings; delete m_qSettings;
return returnlist; return returnlist.mid(0, 4);
} }
void ConfigFile::writeRecently(QString message){ bool ConfigFile::writeRecently(QString message){
QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat); QSettings *m_qSettings=new QSettings(QDir::homePath()+"/.config/org.ukui/ukui-search/ukui-search.conf",QSettings::IniFormat);
m_qSettings->beginGroup("Recently"); m_qSettings->beginGroup("Recently");
@ -54,11 +56,18 @@ void ConfigFile::writeRecently(QString message){
recently.removeOne(message); recently.removeOne(message);
} }
recently.insert(0,message); recently.insert(0,message);
m_qSettings->beginGroup("Recently"); m_qSettings->beginGroup("Recently");
qWarning()<<m_qSettings->value("Recently").toStringList().length();
if (m_qSettings->value("Recently").toStringList().length() >= 20) {
m_qSettings->setValue("Recently",QStringList(recently.mid(0, 20)));
} else {
m_qSettings->setValue("Recently",recently); m_qSettings->setValue("Recently",recently);
}
m_qSettings->endGroup(); m_qSettings->endGroup();
if(m_qSettings) if(m_qSettings)
delete m_qSettings; delete m_qSettings;
return true;
} }
QStringList ConfigFile::readRecently(){ QStringList ConfigFile::readRecently(){
@ -69,12 +78,13 @@ QStringList ConfigFile::readRecently(){
m_qSettings->endGroup(); m_qSettings->endGroup();
if(m_qSettings) if(m_qSettings)
delete m_qSettings; delete m_qSettings;
return recently; return recently.mid(0, 4);
} }
void ConfigFile::writeConfig(QString message){ bool ConfigFile::writeConfig(QString message){
writeCommonly(message); bool isWriteCommonlyDone = writeCommonly(message);
writeRecently(message); bool isWriteRecentlyDone = writeRecently(message);
return (isWriteCommonlyDone || isWriteRecentlyDone);
} }
QMap<QString,QStringList> ConfigFile::readConfig(){ QMap<QString,QStringList> ConfigFile::readConfig(){

View File

@ -10,13 +10,13 @@ class ConfigFile : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
static void writeConfig(QString message); static bool writeConfig(QString message);
static QMap<QString,QStringList> readConfig(); static QMap<QString,QStringList> readConfig();
static void receiveMessage(QString message); static void receiveMessage(QString message);
private: private:
static void writeCommonly(QString message); static bool writeCommonly(QString message);
static QStringList readCommonly(); static QStringList readCommonly();
static void writeRecently(QString message); static bool writeRecently(QString message);
static QStringList readRecently(); static QStringList readRecently();
}; };

View File

@ -13,6 +13,7 @@
#include <QApplication> #include <QApplication>
#include <QFileInfo> #include <QFileInfo>
#include <QDateTime> #include <QDateTime>
#include "config-file.h"
SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent) SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent)
{ {
@ -75,6 +76,7 @@ QString SearchDetailView::getHtmlText(const QString & text, const QString & keyw
htmlString.append(QString(text.at(i))); htmlString.append(QString(text.at(i)));
} }
} }
htmlString.replace("\n", "<br />");//替换换行符
return htmlString; return htmlString;
} }
@ -241,6 +243,7 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
GDesktopAppInfo * desktopAppInfo = g_desktop_app_info_new_from_filename(path.toLocal8Bit().data()); GDesktopAppInfo * desktopAppInfo = g_desktop_app_info_new_from_filename(path.toLocal8Bit().data());
g_app_info_launch(G_APP_INFO(desktopAppInfo),nullptr, nullptr, nullptr); g_app_info_launch(G_APP_INFO(desktopAppInfo),nullptr, nullptr, nullptr);
g_object_unref(desktopAppInfo); g_object_unref(desktopAppInfo);
writeConfigFile(path);
return true; return true;
break; break;
} }
@ -252,6 +255,7 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() { connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
process->deleteLater(); process->deleteLater();
}); });
writeConfigFile(path);
return true; return true;
break; break;
} }
@ -262,6 +266,7 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() { connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
process->deleteLater(); process->deleteLater();
}); });
writeConfigFile(path);
return true; return true;
break; break;
} }
@ -270,6 +275,17 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
} }
} }
/**
* @brief SearchDetailView::writeConfigFile Homepage的请求
* @param path
* @return
*/
bool SearchDetailView::writeConfigFile(const QString& path) {
if (ConfigFile::writeConfig(path)) {
Q_EMIT this->configFileChanged();
}
}
/** /**
* @brief SearchDetailView::addDesktopShortcut * @brief SearchDetailView::addDesktopShortcut
* @return * @return

View File

@ -25,8 +25,10 @@ private:
bool openPathAction(const QString&); bool openPathAction(const QString&);
bool copyPathAction(const QString&); bool copyPathAction(const QString&);
QString getHtmlText(const QString&, const QString&); QString getHtmlText(const QString&, const QString&);
Q_SIGNALS: bool writeConfigFile(const QString&);
Q_SIGNALS:
void configFileChanged();
private Q_SLOTS: private Q_SLOTS:
void execActions(const int&, const int&, const QString&); void execActions(const int&, const int&, const QString&);
}; };

View File

@ -181,22 +181,7 @@ void MainWindow::initUi()
}); });
//初始化homepage //初始化homepage
QVector<QStringList> lists; m_contentFrame->initHomePage();
//测试用数据
QStringList list;
list<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"/usr/share/applications/ukui-clock.desktop"<<"/usr/share/applications/wps-office-pdf.desktop";
QStringList list2;
list2<<QString("%1/下载").arg(QDir::homePath())<<"/home/zjp/ukui/NM/v10/kylin-nm/README.md"<<"Theme/主题/更改壁纸"<<"/home/zjp/下载/WiFi_AP选择.docx";
QStringList list3;
list3<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"Theme/主题/更改壁纸";
lists.append(list);
lists.append(list2);
lists.append(list3);
//将搜索结果加入列表
m_contentFrame->initHomePage(lists);
} }
/** /**