Merge pull request #56 from mammonsama666/0107-dev

feat(homepage): Init & refresh homepage with interface provided by ConfigFile.
This commit is contained in:
iaom 2021-01-08 13:39:28 +08:00 committed by GitHub
commit 61c64186b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 46 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");
m_qSettings->setValue("Recently",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->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;
} }
@ -207,7 +209,9 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) {
void SearchDetailView::execActions(const int& type, const int& option, const QString& path) { void SearchDetailView::execActions(const int& type, const int& option, const QString& path) {
switch (option) { switch (option) {
case OptionView::Options::Open: { case OptionView::Options::Open: {
openAction(type, path); if (openAction(type, path)) {
writeConfigFile(path);
}
break; break;
} }
case OptionView::Options::Shortcut: { case OptionView::Options::Shortcut: {
@ -266,10 +270,22 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
break; break;
} }
default: default:
return false;
break; break;
} }
} }
/**
* @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);
} }
/** /**