ukui-search/src/content-widget.cpp

281 lines
11 KiB
C++
Raw Normal View History

#include "content-widget.h"
#include <QDebug>
#include <QLabel>
#include <QTimer>
#include "config-file.h"
ContentWidget::ContentWidget(QWidget * parent):QStackedWidget(parent)
{
initUI();
}
ContentWidget::~ContentWidget()
{
if (m_homePage) {
delete m_homePage;
m_homePage = nullptr;
}
if (m_resultPage) {
delete m_resultPage;
m_resultPage = nullptr;
}
}
/**
* @brief initUI homepage和resultpage
*/
void ContentWidget::initUI() {
m_homePage = new QWidget;
m_homePageLyt = new QVBoxLayout(m_homePage);
m_homePageLyt->setSpacing(0);
m_homePage->setLayout(m_homePageLyt);
m_resultPage = new QWidget;
m_resultPageLyt = new QHBoxLayout(m_resultPage);
m_resultPageLyt->setSpacing(0);
m_resultPageLyt->setContentsMargins(0, 0, 0, 0);
m_resultListArea = new QScrollArea(m_resultPage);
m_resultListArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_resultListArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_resultDetailArea = new QScrollArea(m_resultPage);
m_resultDetailArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_resultDetailArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_resultListArea->setFixedWidth(244);
m_resultPageLyt->addWidget(m_resultListArea);
m_resultPageLyt->addWidget(m_resultDetailArea);
m_resultPage->setLayout(m_resultPageLyt);
m_resultList = new QWidget(m_resultDetailArea);
m_resultDetail = new QWidget(m_resultDetailArea);
m_listLyt = new QVBoxLayout(m_resultList);
m_detailLyt = new QVBoxLayout(m_resultDetail);
m_resultList->setFixedWidth(240);
m_resultList->setFixedHeight(0);
m_resultList->setStyleSheet("QWidget{background:transparent;}");
m_listLyt->setContentsMargins(0, 0, 15, 0);
m_listLyt->setSpacing(0);
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;}");
m_resultDetailArea->setStyleSheet("QScrollArea{background: rgba(0,0,0,0.05); border-radius: 4px;}");
this->addWidget(m_homePage);
this->addWidget(m_resultPage);
setPage(SearchItem::SearchType::All);//初始化按“全部”加载
}
/**
* @brief ContentWidget::initHomePage homepage填充内容
* @param 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);
layout->setContentsMargins(0, 0, 0, 0);
itemWidget->setLayout(layout);
for (int j = 0; j < lists.at(i).count(); j++) {
HomePageItem * item = new HomePageItem(itemWidget, i, lists.at(i).at(j));
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);
itemWidget->setLayout(layout);
Q_FOREACH(QString path, lists.at(i)){
HomePageItem * item = new HomePageItem(itemWidget, i, path);
layout->addWidget(item);
}
for (int j = 0; j < 4 - lists.at(i).length(); j++) {
QWidget * emptyItem = new QWidget(itemWidget);
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);
titleLabel->setFixedHeight(24);
itemWidgetLyt->addWidget(titleLabel);
itemWidgetLyt->addWidget(itemWidget);
m_homePageLyt->addWidget(listWidget);
}
m_homePageLyt->addStretch();
}
/**
* @brief setPageType
* @param type
*/
void ContentWidget::setPage(const int& type) {
m_currentType = type;
}
/**
* @brief ContentWidget::currentType home或searchresult
* @return
*/
int ContentWidget::currentPage() {
return m_currentType;
}
/**
* @brief ContentWidget::refreshSearchList /
* @param types listview
* @param lists
* @param keyword
*/
void ContentWidget::refreshSearchList(const QVector<int>& types, const QVector<QStringList>& lists, const QString& keyword) {
if (!m_listLyt->isEmpty()) {
clearLayout(m_listLyt);
m_resultList->setFixedHeight(0);
}
bool isEmpty = true;
QStringList bestList;
for (int i = 0; i < types.count(); i ++) {
if (lists.at(i).isEmpty()) {
continue;
}
bestList << lists.at(i).at(0);
isEmpty = false;
SearchListView * searchList = new SearchListView(m_resultList, lists.at(i), types.at(i), keyword); //Treeview
QLabel * titleLabel = new QLabel(m_resultList); //表头
titleLabel->setContentsMargins(8, 0, 0, 0);
titleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}");
titleLabel->setText(getTitleName(types.at(i)));
m_listLyt->addWidget(titleLabel);
m_listLyt->addWidget(searchList);
m_resultList->setFixedHeight(m_resultList->height() + searchList->height() + titleLabel->height());
// if (i == 0) {
// searchList->setCurrentIndex(searchList->model()->index(0,1, QModelIndex()));
// m_detailView->setupWidget(searchList->getCurrentType(), lists.at(0).at(0));
// }
connect(searchList, &SearchListView::currentRowChanged, this, [ = ](const int& type, const QString& path) {
if(type == SearchListView::ResType::Content && !m_contentList.isEmpty()) {
m_detailView->setContent(m_contentList.at(searchList->currentIndex().row()), keyword);
}
m_detailView->setupWidget(type, path);
searchList->is_current_list = true;
Q_EMIT this->currentItemChanged();
searchList->is_current_list = false;
});
connect(this, &ContentWidget::currentItemChanged, searchList, [ = ]() {
if (! searchList->is_current_list) {
searchList->blockSignals(true);
searchList->clearSelection();
searchList->blockSignals(false);
}
});
}
if (isEmpty) {
m_detailView->clearLayout(); //没有搜到结果,清空详情页
return;
}
SearchListView * searchList = new SearchListView(m_resultList, bestList, SearchItem::SearchType::Best, keyword); //Treeview
QLabel * titleLabel = new QLabel(m_resultList); //表头
titleLabel->setContentsMargins(8, 0, 0, 0);
titleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}");
titleLabel->setText(getTitleName(SearchItem::SearchType::Best));
m_listLyt->insertWidget(0, searchList);
m_listLyt->insertWidget(0, titleLabel);
m_resultList->setFixedHeight(m_resultList->height() + searchList->height() + titleLabel->height());
searchList->setCurrentIndex(searchList->model()->index(0, 0, QModelIndex()));
m_detailView->setupWidget(searchList->getCurrentType(), bestList.at(0));
connect(searchList, &SearchListView::currentRowChanged, this, [ = ](const int& type, const QString& path) {
m_detailView->setupWidget(type, path);
searchList->is_current_list = true;
Q_EMIT this->currentItemChanged();
searchList->is_current_list = false;
});
connect(this, &ContentWidget::currentItemChanged, searchList, [ = ]() {
if (! searchList->is_current_list) {
searchList->blockSignals(true);
searchList->clearSelection();
searchList->blockSignals(false);
}
});
}
/**
* @brief ContentWidget::getTitleName
* @param type
* @return
*/
QString ContentWidget::getTitleName(const int& type) {
switch (type) {
case SearchItem::SearchType::Apps :
return tr("Apps");
case SearchItem::SearchType::Settings :
return tr("Settings");
case SearchItem::SearchType::Files :
return tr("Files");
case SearchItem::SearchType::Dirs :
return tr("Dirs");
case SearchItem::SearchType::Contents :
return tr("File Contents");
case SearchItem::SearchType::Best :
return tr("Best Matches");
default :
return tr("Unknown");
}
}
/**
2021-01-08 10:35:26 +08:00
* @brief ContentWidget::clearLayout
* @param layout
*/
void ContentWidget::clearLayout(QLayout * layout) {
if (! layout) return;
QLayoutItem * child;
while ((child = layout->takeAt(0)) != 0) {
if(child->widget())
{
child->widget()->setParent(NULL); //防止删除后窗口看上去没有消失
}
delete child;
}
child = NULL;
}
/**
* @brief ContentWidget::setContentList
* @param list
*/
void ContentWidget::setContentList(const QStringList& list) {
m_contentList.clear();
m_contentList = list;
}