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 <QLabel>
#include <QTimer>
#include "config-file.h"
ContentWidget::ContentWidget(QWidget * parent):QStackedWidget(parent)
{
@ -56,6 +57,10 @@ void ContentWidget::initUI() {
m_resultListArea->setWidget(m_resultList);
m_resultListArea->setWidgetResizable(true);
m_detailView = new SearchDetailView(m_resultDetailArea);
connect(m_detailView, &SearchDetailView::configFileChanged, this, [ = ]() {
clearLayout(m_homePageLyt);
initHomePage();
});
m_resultDetailArea->setWidget(m_detailView);
m_resultDetailArea->setWidgetResizable(true);
m_resultListArea->setStyleSheet("QScrollArea{background:transparent;}");
@ -70,13 +75,29 @@ void ContentWidget::initUI() {
* @brief ContentWidget::initHomePage homepage填充内容
* @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++) {
if (lists.at(i).isEmpty())
continue;
QWidget * listWidget = new QWidget(m_homePage);
QVBoxLayout * itemWidgetLyt = new QVBoxLayout(listWidget);
QLabel * titleLabel = new QLabel(listWidget);
QWidget * itemWidget = new QWidget(listWidget);
if (i == 1) {
if (lists.at(i).length() <= 2) itemWidget->setFixedHeight(48);
else itemWidget->setFixedHeight(112);
titleLabel->setText(tr("Recently Opened"));
QGridLayout * layout = new QGridLayout(itemWidget);
layout->setSpacing(8);
@ -87,6 +108,7 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
layout->addWidget(item, j / 2, j % 2);
}
} else {
itemWidget->setFixedHeight(136);
QHBoxLayout * layout = new QHBoxLayout(itemWidget);
layout->setSpacing(8);
layout->setContentsMargins(0, 0, 0, 0);
@ -95,12 +117,12 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
HomePageItem * item = new HomePageItem(itemWidget, i, path);
layout->addWidget(item);
}
if (i) {
titleLabel->setText(tr("Open Quickly"));
for (int j = 0; j < 4 - lists.at(i).length(); j++) {
QWidget * emptyItem = new QWidget(itemWidget);
emptyItem->setFixedSize(136, 136); //占位用widget,若后续快捷打开有扩展项可删除此widget
emptyItem->setFixedSize(136, 136); //占位用widget,少于4项会补全后方占位
layout->addWidget(emptyItem);
}
if (i) titleLabel->setText(tr("Open Quickly"));
else titleLabel->setText(tr("Commonly Used"));
}
itemWidgetLyt->setSpacing(6);
@ -109,13 +131,14 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
itemWidgetLyt->addWidget(itemWidget);
m_homePageLyt->addWidget(listWidget);
}
m_homePageLyt->addStretch();
}
/**
* @brief setPageType
* @param type
*/
void ContentWidget::setPage(const int& type){
void ContentWidget::setPage(const int& 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) {
if (!m_listLyt->isEmpty()) {
clearSearchList();
clearLayout(m_listLyt);
m_resultList->setFixedHeight(0);
}
bool isEmpty = true;
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;
while ((child = m_listLyt->takeAt(0)) != 0) {
while ((child = layout->takeAt(0)) != 0) {
if(child->widget())
{
child->widget()->setParent(NULL); //防止删除后窗口看上去没有消失
@ -241,7 +267,6 @@ void ContentWidget::clearSearchList() {
delete child;
}
child = NULL;
m_resultList->setFixedHeight(0);
}

View File

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

View File

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

View File

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

View File

@ -13,6 +13,7 @@
#include <QApplication>
#include <QFileInfo>
#include <QDateTime>
#include "config-file.h"
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.replace("\n", "<br />");//替换换行符
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) {
switch (option) {
case OptionView::Options::Open: {
openAction(type, path);
if (openAction(type, path)) {
writeConfigFile(path);
}
break;
}
case OptionView::Options::Shortcut: {
@ -266,10 +270,22 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
break;
}
default:
return false;
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
* @return

View File

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

View File

@ -181,22 +181,7 @@ void MainWindow::initUi()
});
//初始化homepage
QVector<QStringList> lists;
//测试用数据
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);
m_contentFrame->initHomePage();
}
/**