From 4a460f632fd6ed2ea507a802f508b9b6818f8671 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Fri, 15 Jan 2021 15:34:43 +0800 Subject: [PATCH] fix(frontend): Widget crashed when searching & open file/filepath failed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 修复搜索过快时主界面崩溃的bug Log: 修复打开文件和打开文件所在路径失败的bug Bug: -- --- src/content-widget.cpp | 451 +++++++++++++++++------------ src/content-widget.h | 20 +- src/control/option-view.cpp | 115 +++++--- src/control/option-view.h | 8 +- src/control/search-detail-view.cpp | 269 ++++++++++------- src/control/search-detail-view.h | 26 ++ src/control/search-list-view.cpp | 31 +- src/control/search-list-view.h | 7 +- src/input-box.cpp | 3 +- src/mainwindow.cpp | 25 +- src/mainwindow.h | 3 +- src/model/search-item-model.cpp | 21 +- src/model/search-item-model.h | 1 + src/model/search-item.cpp | 7 - src/model/search-item.h | 8 +- 15 files changed, 626 insertions(+), 369 deletions(-) diff --git a/src/content-widget.cpp b/src/content-widget.cpp index 7f27475..fa31f0a 100644 --- a/src/content-widget.cpp +++ b/src/content-widget.cpp @@ -7,6 +7,7 @@ ContentWidget::ContentWidget(QWidget * parent):QStackedWidget(parent) { initUI(); + initListView(); } ContentWidget::~ContentWidget() @@ -68,7 +69,142 @@ void ContentWidget::initUI() { this->addWidget(m_homePage); this->addWidget(m_resultPage); - setPage(SearchItem::SearchType::All);//初始化按“全部”加载 + setPage(SearchItem::SearchType::Best);//初始化按“最佳”加载 +} + +/** + * @brief ContentWidget::initListView 初始化所有搜索结果列表 + */ +void ContentWidget::initListView() +{ + m_fileListView = new SearchListView(m_resultList, QStringList(), SearchItem::SearchType::Files); + m_dirListView = new SearchListView(m_resultList, QStringList(), SearchItem::SearchType::Dirs); + m_contentListView = new SearchListView(m_resultList, QStringList(), SearchItem::SearchType::Contents); + m_settingListView = new SearchListView(m_resultList, QStringList(), SearchItem::SearchType::Settings); + m_appListView = new SearchListView(m_resultList, QStringList(), SearchItem::SearchType::Apps); + m_bestListView = new SearchListView(m_resultList, QStringList(), SearchItem::SearchType::Best); + setupConnect(m_fileListView); + setupConnect(m_dirListView); + setupConnect(m_contentListView); + setupConnect(m_settingListView); + setupConnect(m_appListView); + setupConnect(m_bestListView); + + m_fileTitleLabel = new QLabel(m_resultList); + m_fileTitleLabel->setContentsMargins(8, 0, 0, 0); + m_fileTitleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); + m_fileTitleLabel->setText(getTitleName(SearchItem::SearchType::Files)); + m_dirTitleLabel = new QLabel(m_resultList); + m_dirTitleLabel->setContentsMargins(8, 0, 0, 0); + m_dirTitleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); + m_dirTitleLabel->setText(getTitleName(SearchItem::SearchType::Dirs)); + m_contentTitleLabel = new QLabel(m_resultList); + m_contentTitleLabel->setContentsMargins(8, 0, 0, 0); + m_contentTitleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); + m_contentTitleLabel->setText(getTitleName(SearchItem::SearchType::Contents)); + m_appTitleLabel = new QLabel(m_resultList); + m_appTitleLabel->setContentsMargins(8, 0, 0, 0); + m_appTitleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); + m_appTitleLabel->setText(getTitleName(SearchItem::SearchType::Apps)); + m_settingTitleLabel = new QLabel(m_resultList); + m_settingTitleLabel->setContentsMargins(8, 0, 0, 0); + m_settingTitleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); + m_settingTitleLabel->setText(getTitleName(SearchItem::SearchType::Settings)); + m_bestTitleLabel = new QLabel(m_resultList); + m_bestTitleLabel->setContentsMargins(8, 0, 0, 0); + m_bestTitleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); + m_bestTitleLabel->setText(getTitleName(SearchItem::SearchType::Best)); + + m_listLyt->addWidget(m_bestTitleLabel); + m_listLyt->addWidget(m_bestListView); + m_listLyt->addWidget(m_appTitleLabel); + m_listLyt->addWidget(m_appListView); + m_listLyt->addWidget(m_settingTitleLabel); + m_listLyt->addWidget(m_settingListView); + m_listLyt->addWidget(m_dirTitleLabel); + m_listLyt->addWidget(m_dirListView); + m_listLyt->addWidget(m_fileTitleLabel); + m_listLyt->addWidget(m_fileListView); + m_listLyt->addWidget(m_contentTitleLabel); + m_listLyt->addWidget(m_contentListView); + + this->hideListView(); + m_resultList->setFixedHeight(0); +} + +/** + * @brief ContentWidget::hideListView 隐藏所有列表 + */ +void ContentWidget::hideListView() +{ + m_bestTitleLabel->hide(); + m_bestListView->hide(); + m_appTitleLabel->hide(); + m_appListView->hide(); + m_settingTitleLabel->hide(); + m_settingListView->hide(); + m_dirTitleLabel->hide(); + m_dirListView->hide(); + m_fileTitleLabel->hide(); + m_fileListView->hide(); + m_contentTitleLabel->hide(); + m_contentListView->hide(); +} + +/** + * @brief ContentWidget::setupConnect 初始化各个treeview的信号与槽 + * @param listview + */ +void ContentWidget::setupConnect(SearchListView * listview) { + connect(listview, &SearchListView::currentRowChanged, this, [ = ](const int& type, const QString& path) { + if(type == SearchItem::SearchType::Contents && !m_contentList.isEmpty()) { + m_detailView->setContent(m_contentList.at(listview->currentIndex().row()), m_keyword); + } + m_detailView->setupWidget(type, path); + listview->is_current_list = true; + Q_EMIT this->currentItemChanged(); + listview->is_current_list = false; + }); + connect(this, &ContentWidget::currentItemChanged, listview, [ = ]() { + if (! listview->is_current_list) { + listview->blockSignals(true); + listview->clearSelection(); + listview->blockSignals(false); + } + }); +} + +/** + * @brief ContentWidget::resetHeight 重新计算列表高度 + */ +void ContentWidget::resetListHeight() +{ + int height = 0; + if (! m_bestListView->isHidden) { + height += m_bestTitleLabel->height(); + height += m_bestListView->height(); + } + if (! m_appListView->isHidden) { + height += m_appTitleLabel->height(); + height += m_appListView->height(); + } + if (! m_settingListView->isHidden) { + height += m_settingTitleLabel->height(); + height += m_settingListView->height(); + } + if (! m_fileListView->isHidden) { + height += m_fileTitleLabel->height(); + height += m_fileListView->height(); + } + if (! m_dirListView->isHidden) { + height += m_dirTitleLabel->height(); + height += m_dirListView->height(); + } + if (! m_contentListView->isHidden) { + height += m_contentTitleLabel->height(); + height += m_contentListView->height(); + } + m_resultList->setFixedHeight(height); } /** @@ -152,88 +288,70 @@ int ContentWidget::currentPage() { /** * @brief ContentWidget::refreshSearchList 刷新/构建搜索结果列表 - * @param types 获取到的搜索结果类型,仅针对有搜索结果的类型构建listview - * @param lists 获取到的搜索结果列表(每个类型对应一个列表) - * @param keyword 搜索关键字 + * @param lists 获取到的应用与设置结果列表,list.at(0)是应用, list.at(1)是设置 */ -void ContentWidget::refreshSearchList(const QVector& types, const QVector& lists, const QString& keyword) { - if (!m_listLyt->isEmpty()) { - if (m_fileListView) { - m_fileListView->clear(); - } - if (m_dirListView) { - m_dirListView->clear(); - } - if (m_contentListView) { - m_contentListView->clear(); - } - clearLayout(m_listLyt); - m_resultList->setFixedHeight(0); +void ContentWidget::refreshSearchList(const QVector& lists) { + this->hideListView(); + if (m_fileListView) { + m_fileListView->hide(); + m_fileTitleLabel->hide(); + m_fileListView->isHidden = true; + m_fileListView->clear(); } - 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 == SearchItem::SearchType::Contents && !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 (m_dirListView) { + m_dirListView->hide(); + m_dirTitleLabel->hide(); + m_dirListView->isHidden = true; + m_dirListView->clear(); } - if (isEmpty) { - m_detailView->clearLayout(); //没有搜到结果,清空详情页 - return; + if (m_contentListView) { + m_contentListView->hide(); + m_contentTitleLabel->hide(); + m_contentListView->isHidden = true; + m_contentListView->clear(); } - 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); - } - }); + if (m_appListView) { + m_appListView->hide(); + m_appTitleLabel->hide(); + m_appListView->isHidden = true; + m_appListView->clear(); + } + if (m_settingListView) { + m_settingListView->hide(); + m_settingTitleLabel->hide(); + m_settingListView->isHidden = true; + m_settingListView->clear(); + } + if (m_bestListView) { + m_bestListView->hide(); + m_bestTitleLabel->hide(); + m_bestListView->isHidden = true; + m_bestListView->clear(); + } + m_resultList->setFixedHeight(0); + m_detailView->clearLayout(); + + if (!lists.at(0).isEmpty()) { + qDebug()<<"Append a best item into list: "<show(); + m_appTitleLabel->show(); + m_appListView->isHidden = false; + m_appListView->appendList(lists.at(0)); + } + if (!lists.at(1).isEmpty()) { + m_settingListView->show(); + m_settingTitleLabel->show(); + m_settingListView->isHidden = false; + m_settingListView->appendList(lists.at(1)); + } + this->resetListHeight(); } /** @@ -242,127 +360,76 @@ void ContentWidget::refreshSearchList(const QVector& types, const QVectorsetContentsMargins(8, 0, 0, 0); - titleLabel->setStyleSheet("QLabel{background: rgba(0,0,0,0.1);}"); - titleLabel->setText(getTitleName(type)); - m_listLyt->addWidget(titleLabel); - m_listLyt->addWidget(m_fileListView); - connect(m_fileListView, &SearchListView::currentRowChanged, this, [ = ](const int& type, const QString& path) { - m_detailView->setupWidget(type, path); - m_fileListView->is_current_list = true; - Q_EMIT this->currentItemChanged(); - m_fileListView->is_current_list = false; - }); - connect(this, &ContentWidget::currentItemChanged, m_fileListView, [ = ]() { - if (! m_fileListView->is_current_list) { - m_fileListView->blockSignals(true); - m_fileListView->clearSelection(); - m_fileListView->blockSignals(false); - } - }); - m_resultList->setFixedHeight(m_resultList->height() + m_fileListView->height() + titleLabel->height()); + case SearchItem::SearchType::Best: { + if (m_bestListView->isHidden) { + m_bestListView->show(); + m_bestTitleLabel->show(); + m_bestListView->isHidden = false; } - if (m_fileListView->isHidden) { - m_fileListView->isHidden = false; - 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(type)); - m_listLyt->addWidget(titleLabel); - m_listLyt->addWidget(m_fileListView); - m_resultList->setFixedHeight(m_resultList->height() + m_fileListView->height() + titleLabel->height()); + m_bestListView->appendItem(path); + if (m_detailView->isEmpty()) { + m_bestListView->setCurrentIndex(m_bestListView->model()->index(0, 0, QModelIndex())); } - m_fileListView->setKeyword(keyword); - m_fileListView->appendItem(path); - m_resultList->setFixedHeight(m_resultList->height() + m_fileListView->rowheight); - return; break; } +// case SearchItem::SearchType::Apps: { +// if (m_appListView->isHidden) { +// m_appListView->show(); +// m_appTitleLabel->show(); +// m_appListView->isHidden = false; +// if (!m_detailView->isEmpty() && m_detailView->getType() > type) { +// m_appListView->setCurrentIndex(m_appListView->model()->index(0, 0, QModelIndex())); +// } +// } +// m_appListView->appendItem(path); +// currentList = m_appListView; + +// this->resetListHeight(); +// break; +// } +// case SearchItem::SearchType::Settings: { +// if (m_settingListView->isHidden) { +// m_settingListView->show(); +// m_settingTitleLabel->show(); +// m_settingListView->isHidden = false; +// if (!m_detailView->isEmpty() && m_detailView->getType() > type) { +// m_settingListView->setCurrentIndex(m_settingListView->model()->index(0, 0, QModelIndex())); +// } +// } +// m_settingListView->appendItem(path); +// currentList = m_settingListView; +// break; +// } + case SearchItem::SearchType::Files: { + if (m_fileListView->isHidden) { + m_fileListView->show(); + m_fileTitleLabel->show(); + m_fileListView->isHidden = false; + appendSearchItem(SearchItem::SearchType::Best, path); + } + m_fileListView->appendItem(path); + break;; + } case SearchItem::SearchType::Dirs: { - if (!m_dirListView) { - m_dirListView = new SearchListView(m_resultList, QStringList(), type, keyword); - 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(type)); - m_listLyt->addWidget(titleLabel); - m_listLyt->addWidget(m_dirListView); - connect(m_dirListView, &SearchListView::currentRowChanged, this, [ = ](const int& type, const QString& path) { - m_detailView->setupWidget(type, path); - m_dirListView->is_current_list = true; - Q_EMIT this->currentItemChanged(); - m_dirListView->is_current_list = false; - }); - connect(this, &ContentWidget::currentItemChanged, m_dirListView, [ = ]() { - if (! m_dirListView->is_current_list) { - m_dirListView->blockSignals(true); - m_dirListView->clearSelection(); - m_dirListView->blockSignals(false); - } - }); - m_resultList->setFixedHeight(m_resultList->height() + m_dirListView->height() + titleLabel->height()); - } if (m_dirListView->isHidden) { + m_dirListView->show(); + m_dirTitleLabel->show(); m_dirListView->isHidden = false; - 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(type)); - m_listLyt->addWidget(titleLabel); - m_listLyt->addWidget(m_dirListView); - m_resultList->setFixedHeight(m_resultList->height() + m_dirListView->height() + titleLabel->height()); + appendSearchItem(SearchItem::SearchType::Best, path); } - m_dirListView->setKeyword(keyword); m_dirListView->appendItem(path); - m_resultList->setFixedHeight(m_resultList->height() + m_dirListView->rowheight); - return; break; } case SearchItem::SearchType::Contents: { - if (!m_contentListView) { - m_contentListView = new SearchListView(m_resultList, QStringList(), type, keyword); - 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(type)); - m_listLyt->addWidget(titleLabel); - m_listLyt->addWidget(m_contentListView); - connect(m_contentListView, &SearchListView::currentRowChanged, this, [ = ](const int& type, const QString& path) { - m_detailView->setContent(m_contentList.at(m_contentListView->currentIndex().row()), m_keyword); - m_detailView->setupWidget(type, path); - m_contentListView->is_current_list = true; - Q_EMIT this->currentItemChanged(); - m_contentListView->is_current_list = false; - }); - connect(this, &ContentWidget::currentItemChanged, m_contentListView, [ = ]() { - if (! m_contentListView->is_current_list) { - m_contentListView->blockSignals(true); - m_contentListView->clearSelection(); - m_contentListView->blockSignals(false); - } - }); - m_resultList->setFixedHeight(m_resultList->height() + m_contentListView->height() + titleLabel->height()); - } if (m_contentListView->isHidden) { + m_contentListView->show(); + m_contentTitleLabel->show(); m_contentListView->isHidden = false; - 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(type)); - m_listLyt->addWidget(titleLabel); - m_listLyt->addWidget(m_contentListView); - m_resultList->setFixedHeight(m_resultList->height() + m_contentListView->height() + titleLabel->height()); + appendSearchItem(SearchItem::SearchType::Best, path); } - m_contentListView->setKeyword(keyword); m_contentListView->appendItem(path); - m_resultList->setFixedHeight(m_resultList->height() + m_contentListView->rowheight); QString temp; for (int i = 0; i < contents.length(); i ++) { temp.append(contents.at(i)); @@ -371,12 +438,13 @@ void ContentWidget::appendSearchItem(const int& type, const QString& path, const } } m_contentList.append(temp); - return; break; } default: break; } + this->resetListHeight(); + return; } /** @@ -430,3 +498,18 @@ void ContentWidget::setContentList(const QStringList& list) { m_contentList.clear(); m_contentList = list; } + +/** + * @brief ContentWidget::setKeyword 设置关键字 + * @param keyword + */ +void ContentWidget::setKeyword(QString keyword) +{ + m_keyword = keyword; + m_fileListView->setKeyword(keyword); + m_dirListView->setKeyword(keyword); + m_contentListView->setKeyword(keyword); + m_settingListView->setKeyword(keyword); + m_appListView->setKeyword(keyword); + m_bestListView->setKeyword(keyword); +} diff --git a/src/content-widget.h b/src/content-widget.h index a1f7948..509a90e 100644 --- a/src/content-widget.h +++ b/src/content-widget.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "control/search-detail-view.h" #include "home-page-item.h" @@ -17,13 +18,18 @@ public: void setPage(const int&); int currentPage(); - void refreshSearchList(const QVector&, const QVector&, const QString&); - void appendSearchItem(const int& type, const QString& path, const QString& keyword, QStringList contents = QStringList()); + void refreshSearchList(const QVector&); + void appendSearchItem(const int& type, const QString& path, QStringList contents = QStringList()); void initHomePage(); void setContentList(const QStringList&); + void setKeyword(QString); private: void initUI(); + void initListView(); + void hideListView(); + void setupConnect(SearchListView *); void clearHomepage(); + void resetListHeight(); QString m_keyword; QStringList m_contentList; QWidget * m_homePage = nullptr; @@ -41,10 +47,20 @@ private: SearchListView * m_fileListView = nullptr; SearchListView * m_dirListView = nullptr; SearchListView * m_contentListView = nullptr; + SearchListView * m_settingListView = nullptr; + SearchListView * m_appListView = nullptr; + SearchListView * m_bestListView = nullptr; + QLabel * m_fileTitleLabel = nullptr; + QLabel * m_dirTitleLabel = nullptr; + QLabel * m_contentTitleLabel = nullptr; + QLabel * m_appTitleLabel = nullptr; + QLabel * m_settingTitleLabel = nullptr; + QLabel * m_bestTitleLabel = nullptr; int m_currentType = 0; QString getTitleName(const int&); + QMutex m_mutex; Q_SIGNALS: void currentItemChanged(); diff --git a/src/control/option-view.cpp b/src/control/option-view.cpp index 224f42e..63c37f5 100644 --- a/src/control/option-view.cpp +++ b/src/control/option-view.cpp @@ -2,13 +2,13 @@ #include #include -OptionView::OptionView(QWidget *parent, const int& type) : QWidget(parent) +OptionView::OptionView(QWidget *parent) : QWidget(parent) { m_mainLyt = new QVBoxLayout(this); this->setLayout(m_mainLyt); m_mainLyt->setContentsMargins(0,8,0,0); m_mainLyt->setSpacing(8); - initComponent(type); + initUI(); } OptionView::~OptionView() @@ -39,7 +39,8 @@ OptionView::~OptionView() * @brief OptionView::initComponent 构建可用选项表 * @param type 详情页类型 */ -void OptionView::initComponent(const int& type) { +void OptionView::setupOptions(const int& type) { + this->hideOptions(); switch (type) { case SearchListView::ResType::App : { setupAppOptions(); @@ -63,66 +64,92 @@ void OptionView::initComponent(const int& type) { } } + +void OptionView::initUI() +{ + m_optionFrame = new QFrame(this); + m_optionLyt = new QVBoxLayout(m_optionFrame); + m_optionLyt->setContentsMargins(8, 0, 0, 0); + + m_openLabel = new QLabel(m_optionFrame); + m_openLabel->setText(tr("Open")); //打开 + m_openLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); + m_openLabel->setCursor(QCursor(Qt::PointingHandCursor)); + m_openLabel->installEventFilter(this); + m_optionLyt->addWidget(m_openLabel); + + m_shortcutLabel = new QLabel(m_optionFrame); + m_shortcutLabel->setText(tr("Add Shortcut to Desktop")); //添加到桌面快捷方式 + m_shortcutLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); + m_shortcutLabel->setCursor(QCursor(Qt::PointingHandCursor)); + m_shortcutLabel->installEventFilter(this); + m_optionLyt->addWidget(m_shortcutLabel); + + m_panelLabel = new QLabel(m_optionFrame); + m_panelLabel->setText(tr("Add Shortcut to Panel")); //添加到任务栏快捷方式 + m_panelLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); + m_panelLabel->setCursor(QCursor(Qt::PointingHandCursor)); + m_panelLabel->installEventFilter(this); + m_optionLyt->addWidget(m_panelLabel); + + m_openPathLabel = new QLabel(m_optionFrame); + m_openPathLabel->setText(tr("Open path")); //打开所在路径 + m_openPathLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); + m_openPathLabel->setCursor(QCursor(Qt::PointingHandCursor)); + m_openPathLabel->installEventFilter(this); + m_optionLyt->addWidget(m_openPathLabel); + + m_copyPathLabel = new QLabel(m_optionFrame); + m_copyPathLabel->setText(tr("Copy path")); //复制所在路径 + m_copyPathLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); + m_copyPathLabel->setCursor(QCursor(Qt::PointingHandCursor)); + m_copyPathLabel->installEventFilter(this); + m_optionLyt->addWidget(m_copyPathLabel); + + m_optionLyt->addStretch(); + m_optionFrame->setLayout(m_optionLyt); + m_mainLyt->addWidget(m_optionFrame); + this->hideOptions(); +} + /** * @brief setupOptionLabel 创建每一个单独的选项 * @param opt 选项类型 */ void OptionView::setupOptionLabel(const int& opt) { - QFrame * optionFrame = new QFrame(this); - QHBoxLayout * optionLyt = new QHBoxLayout(optionFrame); - optionLyt->setContentsMargins(8, 0, 0, 0); switch (opt) { case Options::Open: { - m_openLabel = new QLabel(optionFrame); - m_openLabel->setText(tr("Open")); //打开 - m_openLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); - m_openLabel->setCursor(QCursor(Qt::PointingHandCursor)); - m_openLabel->installEventFilter(this); - optionLyt->addWidget(m_openLabel); + m_openLabel->show(); break; } case Options::Shortcut: { - m_shortcutLabel = new QLabel(optionFrame); - m_shortcutLabel->setText(tr("Add Shortcut to Desktop")); //添加到桌面快捷方式 - m_shortcutLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); - m_shortcutLabel->setCursor(QCursor(Qt::PointingHandCursor)); - m_shortcutLabel->installEventFilter(this); - optionLyt->addWidget(m_shortcutLabel); + m_shortcutLabel->show(); break; } case Options::Panel: { - m_panelLabel = new QLabel(optionFrame); - m_panelLabel->setText(tr("Add Shortcut to Panel")); //添加到任务栏快捷方式 - m_panelLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); - m_panelLabel->setCursor(QCursor(Qt::PointingHandCursor)); - m_panelLabel->installEventFilter(this); - optionLyt->addWidget(m_panelLabel); + m_panelLabel->show(); break; } case Options::OpenPath: { - m_openPathLabel = new QLabel(optionFrame); - m_openPathLabel->setText(tr("Open path")); //打开所在路径 - m_openPathLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); - m_openPathLabel->setCursor(QCursor(Qt::PointingHandCursor)); - m_openPathLabel->installEventFilter(this); - optionLyt->addWidget(m_openPathLabel); + m_openPathLabel->show(); break; } case Options::CopyPath: { - m_copyPathLabel = new QLabel(optionFrame); - m_copyPathLabel->setText(tr("Copy path")); //复制所在路径 - m_copyPathLabel->setStyleSheet("QLabel{font-size: 14px; color: #3D6BE5}"); - m_copyPathLabel->setCursor(QCursor(Qt::PointingHandCursor)); - m_copyPathLabel->installEventFilter(this); - optionLyt->addWidget(m_copyPathLabel); + m_copyPathLabel->show(); break; } default: break; } - optionLyt->addStretch(); - optionFrame->setLayout(optionLyt); - m_mainLyt->addWidget(optionFrame); +} + +void OptionView::hideOptions() +{ + m_openLabel->hide(); + m_shortcutLabel->hide(); + m_panelLabel->hide(); + m_openPathLabel->hide(); + m_copyPathLabel->hide(); } /** @@ -167,19 +194,19 @@ void OptionView::setupSettingOptions() { */ bool OptionView::eventFilter(QObject *watched, QEvent *event){ if (m_openLabel && watched == m_openLabel && event->type() == QEvent::MouseButtonPress){ - Q_EMIT onOptionClicked(Options::Open); + Q_EMIT this->onOptionClicked(Options::Open); return true; } else if (m_shortcutLabel && watched == m_shortcutLabel && event->type() == QEvent::MouseButtonPress) { - Q_EMIT onOptionClicked(Options::Shortcut); + Q_EMIT this->onOptionClicked(Options::Shortcut); return true; } else if (m_panelLabel && watched == m_panelLabel && event->type() == QEvent::MouseButtonPress) { - Q_EMIT onOptionClicked(Options::Panel); + Q_EMIT this->onOptionClicked(Options::Panel); return true; } else if (m_openPathLabel && watched == m_openPathLabel && event->type() == QEvent::MouseButtonPress) { - Q_EMIT onOptionClicked(Options::OpenPath); + Q_EMIT this->onOptionClicked(Options::OpenPath); return true; } else if (m_copyPathLabel && watched == m_copyPathLabel && event->type() == QEvent::MouseButtonPress) { - Q_EMIT onOptionClicked(Options::CopyPath); + Q_EMIT this->onOptionClicked(Options::CopyPath); return true; } return QObject::eventFilter(watched, event); diff --git a/src/control/option-view.h b/src/control/option-view.h index edbdb6c..e4ec40b 100644 --- a/src/control/option-view.h +++ b/src/control/option-view.h @@ -12,8 +12,9 @@ class OptionView : public QWidget { Q_OBJECT public: - explicit OptionView(QWidget *, const int&); + explicit OptionView(QWidget *); ~OptionView(); + void setupOptions(const int&); enum Options { Open, @@ -27,15 +28,18 @@ protected: bool eventFilter(QObject *, QEvent *); private: - void initComponent(const int&); + void initUI(); void setupAppOptions(); void setupFileOptions(); void setupDirOptions(); void setupSettingOptions(); void setupOptionLabel(const int&); + void hideOptions(); int m_type; + QFrame * m_optionFrame = nullptr; + QVBoxLayout * m_optionLyt = nullptr; QVBoxLayout * m_mainLyt = nullptr; QLabel * m_openLabel = nullptr; QLabel * m_shortcutLabel = nullptr; diff --git a/src/control/search-detail-view.cpp b/src/control/search-detail-view.cpp index 4a52c39..2e81cd6 100644 --- a/src/control/search-detail-view.cpp +++ b/src/control/search-detail-view.cpp @@ -17,12 +17,7 @@ SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent) { - m_layout = new QVBoxLayout(this); - this->setLayout(m_layout); - m_layout->setContentsMargins(16, 60, 16, 24); - this->setObjectName("detailView"); - this->setStyleSheet("QWidget#detailView{background:transparent;}"); - this->setFixedWidth(360); + initUI(); } SearchDetailView::~SearchDetailView() @@ -38,15 +33,31 @@ SearchDetailView::~SearchDetailView() * @brief SearchDetailView::clearLayout 清空布局 */ void SearchDetailView::clearLayout() { - QLayoutItem * child; - while ((child = m_layout->takeAt(0)) != 0) { - if(child->widget()) - { - child->widget()->setParent(NULL); //防止删除后窗口看上去没有消失 - } - delete child; - } - child = NULL; +// QLayoutItem * child; +// while ((child = m_layout->takeAt(0)) != 0) { +// if(child->widget()) +// { +// child->widget()->setParent(NULL); //防止删除后窗口看上去没有消失 +// } +// delete child; +// } +// child = NULL; + m_iconLabel->hide(); + m_nameFrame->hide(); + m_nameLabel->hide(); + m_typeLabel->hide(); + m_hLine->hide(); + m_detailFrame->hide(); + m_contentLabel->hide(); + m_pathFrame->hide(); + m_timeFrame->hide(); + m_pathLabel_1->hide(); + m_pathLabel_2->hide(); + m_timeLabel_1->hide(); + m_timeLabel_2->hide(); + m_hLine_2->hide(); + m_optionView->hide(); + m_isEmpty = true; } /** @@ -59,6 +70,24 @@ void SearchDetailView::setContent(const QString& text, const QString& keyword) { m_keyword = keyword; } +/** + * @brief SearchDetailView::isEmpty 返回当前详情页是否为空 + * @return + */ +bool SearchDetailView::isEmpty() +{ + return m_isEmpty; +} + +/** + * @brief SearchDetailView::getType 返回当前详情页显示的类型 + * @return + */ +int SearchDetailView::getType() +{ + return m_type; +} + QString SearchDetailView::getHtmlText(const QString & text, const QString & keyword) { QString htmlString; bool boldOpenned = false; @@ -87,119 +116,67 @@ QString SearchDetailView::getHtmlText(const QString & text, const QString & keyw * @param path 结果路径 */ void SearchDetailView::setupWidget(const int& type, const QString& path) { + m_type = type; + m_path = path; + m_isEmpty = false; clearLayout(); - //图标和名称、分割线区域 - QLabel * iconLabel = new QLabel(this); - iconLabel->setAlignment(Qt::AlignCenter); - iconLabel->setFixedHeight(120); - - QFrame * nameFrame = new QFrame(this); - QHBoxLayout * nameLayout = new QHBoxLayout(nameFrame); - QLabel * nameLabel = new QLabel(nameFrame); - QLabel * typeLabel = new QLabel(nameFrame); - nameLabel->setStyleSheet("QLabel{font-size: 18px;}"); -// typeLabel->setStyleSheet("QLabel{font-size: 14px; color: rgba(0, 0, 0, 0.43);}"); - typeLabel->setStyleSheet("QLabel{font-size: 14px; color: palette(mid);}"); - nameFrame->setFixedHeight(48); - nameLabel->setMaximumWidth(240); - nameLayout->addWidget(nameLabel); - nameLayout->addStretch(); - nameLayout->addWidget(typeLabel); - nameFrame->setLayout(nameLayout); - - QFrame * hLine = new QFrame(this); - hLine->setLineWidth(0); - hLine->setFixedHeight(1); - hLine->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); - - m_layout->addWidget(iconLabel); - m_layout->addWidget(nameFrame); - m_layout->addWidget(hLine); + m_iconLabel->show(); + m_nameFrame->show(); + m_nameLabel->show(); + m_typeLabel->show(); + m_hLine->show(); //文件和文件夹有一个额外的详情区域 if (type == SearchListView::ResType::Dir || type == SearchListView::ResType::File || type == SearchListView::ResType::Content) { - QFrame * detailFrame = new QFrame(this); - QVBoxLayout * detailLyt = new QVBoxLayout(detailFrame); - detailLyt->setContentsMargins(0,0,0,0); + m_detailFrame->show(); if (type == SearchListView::ResType::Content) { //文件内容区域 - QLabel * contentLabel = new QLabel(detailFrame); - contentLabel->setWordWrap(true); - contentLabel->setContentsMargins(9, 0, 9, 0); -// contentLabel->setText(m_contentText); - contentLabel->setText(QApplication::translate("", getHtmlText(m_contentText, m_keyword).toLocal8Bit(), nullptr)); - detailLyt->addWidget(contentLabel); + m_contentLabel->show(); + m_contentLabel->setText(QApplication::translate("", getHtmlText(m_contentText, m_keyword).toLocal8Bit(), nullptr)); } - QFrame * pathFrame = new QFrame(detailFrame); - QFrame * timeFrame = new QFrame(detailFrame); - QHBoxLayout * pathLyt = new QHBoxLayout(pathFrame); - QHBoxLayout * timeLyt = new QHBoxLayout(timeFrame); - QLabel * pathLabel_1 = new QLabel(pathFrame); - QLabel * pathLabel_2 = new QLabel(pathFrame); - pathLabel_1->setText(tr("Path")); - pathLabel_2->setFixedWidth(240); - pathLabel_2->setText(path); - pathLabel_2->setWordWrap(true); - pathLyt->addWidget(pathLabel_1); - pathLyt->addStretch(); - pathLyt->addWidget(pathLabel_2); - QLabel * timeLabel_1 = new QLabel(timeFrame); - QLabel * timeLabel_2 = new QLabel(timeFrame); - timeLabel_1->setText(tr("Last time modified")); + m_pathFrame->show(); + m_timeFrame->show(); + m_pathLabel_1->show(); + m_pathLabel_2->show(); + m_pathLabel_2->setText(path); + m_timeLabel_1->show(); + m_timeLabel_2->show(); QFileInfo fileInfo(path); - timeLabel_2->setText(fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss")); - timeLyt->addWidget(timeLabel_1); - timeLyt->addStretch(); - timeLyt->addWidget(timeLabel_2); - detailLyt->addWidget(pathFrame); - detailLyt->addWidget(timeFrame); - - QFrame * hLine_2 = new QFrame(this); - hLine_2->setLineWidth(0); - hLine_2->setFixedHeight(1); - hLine_2->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); - - m_layout->addWidget(detailFrame); - m_layout->addWidget(hLine_2); + m_timeLabel_2->setText(fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss")); + m_hLine_2->show(); } - //可执行操作区域 - OptionView * optionView = new OptionView(this, type); - connect(optionView, &OptionView::onOptionClicked, this, [ = ](const int& option) { - execActions(type, option, path); - }); - - m_layout->addWidget(optionView); - m_layout->addStretch(); + m_optionView->setupOptions(m_type); + m_optionView->show(); //根据不同类型的搜索结果切换加载图片和名称的方式 switch (type) { case SearchListView::ResType::App : { QIcon icon = FileUtils::getAppIcon(path); - iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); - QFontMetrics fontMetrics = nameLabel->fontMetrics(); + m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString name = fontMetrics.elidedText(FileUtils::getAppName(path), Qt::ElideRight, 215); //当字体长度超过215时显示为省略号 - nameLabel->setText(name); - typeLabel->setText(tr("Application")); + m_nameLabel->setText(name); + m_typeLabel->setText(tr("Application")); break; } case SearchListView::ResType::Content: case SearchListView::ResType::Dir : case SearchListView::ResType::File : { QIcon icon = FileUtils::getFileIcon(QString("file://%1").arg(path)); - iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); - QFontMetrics fontMetrics = nameLabel->fontMetrics(); + m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString name = fontMetrics.elidedText(FileUtils::getFileName(path), Qt::ElideRight, 215); - nameLabel->setText(name); - typeLabel->setText(tr("Document")); + m_nameLabel->setText(name); + m_typeLabel->setText(tr("Document")); break; } case SearchListView::ResType::Setting : { QIcon icon = FileUtils::getSettingIcon(path, true); - iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); QString settingType = path.mid(path.indexOf("/") + 1, path.lastIndexOf("/") - path.indexOf("/") - 1); //配置项所属控制面板插件名 - nameLabel->setText(settingType); - typeLabel->setText(FileUtils::getSettingName(path)); + m_nameLabel->setText(settingType); + m_typeLabel->setText(FileUtils::getSettingName(path)); break; } default: @@ -258,6 +235,7 @@ bool SearchDetailView::openAction(const int& type, const QString& path) { case SearchListView::ResType::File: { QProcess process; process.start(QString("xdg-open %1").arg(path)); + process.waitForFinished(); return true; break; } @@ -265,6 +243,7 @@ bool SearchDetailView::openAction(const int& type, const QString& path) { //打开控制面板对应页面 QProcess process; process.start(QString("ukui-control-center --%1").arg(path.left(path.indexOf("/")).toLower())); + process.waitForFinished(); return true; break; } @@ -287,6 +266,94 @@ bool SearchDetailView::writeConfigFile(const QString& path) { return false; } +/** + * @brief SearchDetailView::initUI 初始化ui + */ +void SearchDetailView::initUI() +{ + m_layout = new QVBoxLayout(this); + this->setLayout(m_layout); + m_layout->setContentsMargins(16, 60, 16, 24); + this->setObjectName("detailView"); + this->setStyleSheet("QWidget#detailView{background:transparent;}"); + this->setFixedWidth(360); + + //图标和名称、分割线区域 + m_iconLabel = new QLabel(this); + m_iconLabel->setAlignment(Qt::AlignCenter); + m_iconLabel->setFixedHeight(120); + m_nameFrame = new QFrame(this); + m_nameLayout = new QHBoxLayout(m_nameFrame); + m_nameLabel = new QLabel(m_nameFrame); + m_typeLabel = new QLabel(m_nameFrame); + m_nameLabel->setStyleSheet("QLabel{font-size: 18px;}"); + m_typeLabel->setStyleSheet("QLabel{font-size: 14px; color: palette(mid);}"); + m_nameFrame->setFixedHeight(48); + m_nameLabel->setMaximumWidth(240); + m_nameLayout->addWidget(m_nameLabel); + m_nameLayout->addStretch(); + m_nameLayout->addWidget(m_typeLabel); + m_nameFrame->setLayout(m_nameLayout); + m_hLine = new QFrame(this); + m_hLine->setLineWidth(0); + m_hLine->setFixedHeight(1); + m_hLine->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); + m_layout->addWidget(m_iconLabel); + m_layout->addWidget(m_nameFrame); + m_layout->addWidget(m_hLine); + + //文件和文件夹有一个额外的详情区域 + m_detailFrame = new QFrame(this); + m_detailLyt = new QVBoxLayout(m_detailFrame); + m_detailLyt->setContentsMargins(0,0,0,0); + + //文件内容区域 + m_contentLabel = new QLabel(m_detailFrame); + m_contentLabel->setWordWrap(true); + m_contentLabel->setContentsMargins(9, 0, 9, 0); + m_detailLyt->addWidget(m_contentLabel); + + //路径与修改时间区域 + m_pathFrame = new QFrame(m_detailFrame); + m_timeFrame = new QFrame(m_detailFrame); + m_pathLyt = new QHBoxLayout(m_pathFrame); + m_timeLyt = new QHBoxLayout(m_timeFrame); + m_pathLabel_1 = new QLabel(m_pathFrame); + m_pathLabel_2 = new QLabel(m_pathFrame); + m_pathLabel_1->setText(tr("Path")); + m_pathLabel_2->setFixedWidth(240); + m_pathLabel_2->setWordWrap(true); + m_pathLyt->addWidget(m_pathLabel_1); + m_pathLyt->addStretch(); + m_pathLyt->addWidget(m_pathLabel_2); + m_timeLabel_1 = new QLabel(m_timeFrame); + m_timeLabel_2 = new QLabel(m_timeFrame); + m_timeLabel_1->setText(tr("Last time modified")); + m_timeLyt->addWidget(m_timeLabel_1); + m_timeLyt->addStretch(); + m_timeLyt->addWidget(m_timeLabel_2); + m_detailLyt->addWidget(m_pathFrame); + m_detailLyt->addWidget(m_timeFrame); + + m_hLine_2 = new QFrame(this); + m_hLine_2->setLineWidth(0); + m_hLine_2->setFixedHeight(1); + m_hLine_2->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); + m_layout->addWidget(m_detailFrame); + m_layout->addWidget(m_hLine_2); + + //可执行操作区域 + m_optionView = new OptionView(this); + connect(m_optionView, &OptionView::onOptionClicked, this, [ = ](const int& option) { + execActions(m_type, option, m_path); + }); + m_layout->addWidget(m_optionView); + + m_layout->addStretch(); + + this->clearLayout(); //初始化时隐藏所有控件 +} + /** * @brief SearchDetailView::addDesktopShortcut 添加到桌面快捷方式 * @return @@ -302,6 +369,7 @@ bool SearchDetailView::addDesktopShortcut(const QString& path) { { QProcess process; process.start(QString("chmod a+x %1").arg(newName)); + process.waitForFinished(); return true; } return false; @@ -336,6 +404,7 @@ bool SearchDetailView::addPanelShortcut(const QString& path) { bool SearchDetailView::openPathAction(const QString& path) { QProcess process; process.start(QString("xdg-open %1").arg(path.left(path.lastIndexOf("/")))); + process.waitForFinished(); return true; } diff --git a/src/control/search-detail-view.h b/src/control/search-detail-view.h index b1b0c6b..9b853e6 100644 --- a/src/control/search-detail-view.h +++ b/src/control/search-detail-view.h @@ -14,6 +14,8 @@ public: void setupWidget(const int&, const QString&); void clearLayout(); void setContent(const QString&, const QString&); + bool isEmpty(); + int getType(); private: QVBoxLayout * m_layout = nullptr; @@ -26,6 +28,30 @@ private: bool copyPathAction(const QString&); QString getHtmlText(const QString&, const QString&); bool writeConfigFile(const QString&); + bool m_isEmpty = true; + int m_type = 0; + QString m_path = 0; + + void initUI(); + QLabel * m_iconLabel = nullptr; + QFrame * m_nameFrame = nullptr; + QHBoxLayout * m_nameLayout = nullptr; + QLabel * m_nameLabel = nullptr; + QLabel * m_typeLabel = nullptr; + QFrame * m_hLine = nullptr; + QFrame * m_detailFrame = nullptr; + QVBoxLayout * m_detailLyt = nullptr; + QLabel * m_contentLabel = nullptr; + QFrame * m_pathFrame = nullptr; + QFrame * m_timeFrame = nullptr; + QHBoxLayout * m_pathLyt = nullptr; + QHBoxLayout * m_timeLyt = nullptr; + QLabel * m_pathLabel_1 = nullptr; + QLabel * m_pathLabel_2 = nullptr; + QLabel * m_timeLabel_1 = nullptr; + QLabel * m_timeLabel_2 = nullptr; + QFrame * m_hLine_2 = nullptr; + OptionView * m_optionView = nullptr; Q_SIGNALS: void configFileChanged(); diff --git a/src/control/search-list-view.cpp b/src/control/search-list-view.cpp index 9952b69..2a23a7f 100644 --- a/src/control/search-list-view.cpp +++ b/src/control/search-list-view.cpp @@ -2,7 +2,7 @@ #include #include -SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type, const QString& keyword) : QTreeView(parent) +SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent) { setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection); @@ -14,14 +14,13 @@ SearchListView::SearchListView(QWidget * parent, const QStringList& list, const this->setHeaderHidden(true); this->setColumnWidth(0, 20); this->setColumnWidth(1, 80); - rowheight = this->rowHeight(this->model()->index(0,1, QModelIndex())) + 1; + rowheight = this->rowHeight(this->model()->index(0, 0, QModelIndex())) + 1; this->setFixedHeight(list.count() * rowheight + 2); this->setAttribute(Qt::WA_TranslucentBackground, true); this->setAutoFillBackground(false); this->setStyleSheet("QWidget{background:transparent;}"); m_styleDelegate = new HighlightItemDelegate(); // m_styleDelegate->setSearchKeyword(keyword); - setKeyword(keyword); this->setItemDelegate(m_styleDelegate); m_type = type; @@ -43,11 +42,21 @@ SearchListView::~SearchListView() } /** - * @brief SearchListView::appendItem + * @brief SearchListView::appendItem 单个添加 */ void SearchListView::appendItem(QString path) { m_model->appendItem(path); - rowheight = this->rowHeight(this->model()->index(0,1, QModelIndex())) + 1; + rowheight = this->rowHeight(this->model()->index(0, 0, QModelIndex())) + 1; + this->setFixedHeight(m_item->getCurrentSize() * rowheight + 3); +} + +/** + * @brief SearchListView::appendList 添加整个列表 + */ +void SearchListView::appendList(QStringList list) +{ + m_model->appendList(list); + rowheight = this->rowHeight(this->model()->index(0, 0, QModelIndex())) + 1; this->setFixedHeight(m_item->getCurrentSize() * rowheight + 3); } @@ -60,6 +69,9 @@ void SearchListView::removeItem(QString path) { void SearchListView::clear() { + this->blockSignals(true); + this->clearSelection(); + this->blockSignals(false); m_model->clear(); this->setFixedHeight(0); this->isHidden = true; @@ -74,6 +86,15 @@ void SearchListView::setKeyword(QString keyword) m_styleDelegate->setSearchKeyword(keyword); } +/** + * @brief SearchListView::getType 获取此列表类型 + * @return + */ +int SearchListView::getType() +{ + return m_type; +} + //获取当前选项所属搜索类型 int SearchListView::getCurrentType() { switch (m_type) { diff --git a/src/control/search-list-view.h b/src/control/search-list-view.h index e8ba42f..7c7d9b4 100644 --- a/src/control/search-list-view.h +++ b/src/control/search-list-view.h @@ -11,14 +11,15 @@ class SearchListView : public QTreeView { Q_OBJECT public: - explicit SearchListView(QWidget *, const QStringList&, const int&, const QString&); + explicit SearchListView(QWidget *, const QStringList&, const int&); ~SearchListView(); enum ResType { //搜索结果可能出现的类型:应用、文件、设置、文件夹 + Best, App, - File, Setting, Dir, + File, Content }; @@ -29,9 +30,11 @@ public: int rowheight = 0; void appendItem(QString); + void appendList(QStringList); void removeItem(QString); void clear(); void setKeyword(QString); + int getType(); bool isHidden = false; private: SearchItemModel * m_model = nullptr; diff --git a/src/input-box.cpp b/src/input-box.cpp index ac3ddb0..654ad68 100644 --- a/src/input-box.cpp +++ b/src/input-box.cpp @@ -80,8 +80,9 @@ UkuiSearchBarHLayout::UkuiSearchBarHLayout() Q_EMIT this->textChanged(m_queryLineEdit->text()); return; } +// Q_EMIT this->textChanged(m_queryLineEdit->text()); m_timer->stop(); - m_timer->start(0.2 * 1000); + m_timer->start(0.1 * 1000); } }); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9c4015b..f55e519 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -91,13 +91,16 @@ MainWindow::MainWindow(QWidget *parent) : m_search_result_thread = new SearchResult(this); // m_search_result_thread->start(); connect(m_search_result_thread, &SearchResult::searchResultFile, this, [ = ](QString path) { - m_contentFrame->appendSearchItem(SearchItem::SearchType::Files, path, m_searchLayout->text()); + qDebug()<<"Append a file into list: "<appendSearchItem(SearchItem::SearchType::Files, path); }); connect(m_search_result_thread, &SearchResult::searchResultDir, this, [ = ](QString path) { - m_contentFrame->appendSearchItem(SearchItem::SearchType::Dirs, path, m_searchLayout->text()); + qDebug()<<"Append a dir into list: "<appendSearchItem(SearchItem::SearchType::Dirs, path); }); connect(m_search_result_thread, &SearchResult::searchResultContent, this, [ = ](QPair pair) { - m_contentFrame->appendSearchItem(SearchItem::SearchType::Contents, pair.first, m_searchLayout->text(), pair.second); + qDebug()<<"Append a file content into list: "<appendSearchItem(SearchItem::SearchType::Contents, pair.first, pair.second); }); m_sys_tray_icon = new QSystemTrayIcon(this); @@ -208,12 +211,12 @@ void MainWindow::initUi() m_contentFrame->setCurrentIndex(0); } else { m_contentFrame->setCurrentIndex(1); -// QTimer::singleShot(50,this,[=](){ + QTimer::singleShot(10,this,[=](){ if (! m_search_result_thread->isRunning()) { m_search_result_thread->start(); } searchContent(text); -// }); + }); } }); @@ -272,8 +275,8 @@ void MainWindow::primaryScreenChangedSlot(QScreen *screen) * @param searchcontent */ void MainWindow::searchContent(QString searchcontent){ - m_lists.clear(); - m_types.clear(); + m_app_setting_lists.clear(); + m_contentFrame->setKeyword(searchcontent); AppMatch * appMatchor = new AppMatch(this); SettingsMatch * settingMatchor = new SettingsMatch(this); @@ -282,11 +285,9 @@ void MainWindow::searchContent(QString searchcontent){ appList = appMatchor->startMatchApp(searchcontent); QStringList settingList; settingList = settingMatchor->startMatchApp(searchcontent); - m_types.append(SearchItem::SearchType::Apps); - m_types.append(SearchItem::SearchType::Settings); - m_lists.append(appList); - m_lists.append(settingList); - m_contentFrame->refreshSearchList(m_types, m_lists, searchcontent); + m_app_setting_lists.append(appList); + m_app_setting_lists.append(settingList); + m_contentFrame->refreshSearchList(m_app_setting_lists); //文件、文件夹、内容搜索 this->m_searcher->onKeywordSearch(searchcontent, m_search_result_file, m_search_result_dir, m_search_result_content); diff --git a/src/mainwindow.h b/src/mainwindow.h index cb3519f..ff64a2a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -81,8 +81,7 @@ private: QGSettings * m_transparency_gsettings = nullptr; double getTransparentData(); - QVector m_types; - QVector m_lists; + QVector m_app_setting_lists; QStringList m_dirList; QQueue *m_search_result_file = nullptr; diff --git a/src/model/search-item-model.cpp b/src/model/search-item-model.cpp index e01f0f4..f90db7e 100644 --- a/src/model/search-item-model.cpp +++ b/src/model/search-item-model.cpp @@ -18,7 +18,7 @@ SearchItemModel::~SearchItemModel(){ */ QModelIndex SearchItemModel::index(int row, int column, const QModelIndex &parent) const { - if (row < 0 || row > m_item->m_pathlist.count()-1) + if (row < 0 || row > m_item->m_pathlist.count() - 1) return QModelIndex(); return createIndex(row, column, m_item); } @@ -122,8 +122,21 @@ void SearchItemModel::setItem(SearchItem * item) { * @brief SearchItemModel::appendItem */ void SearchItemModel::appendItem(QString path) { - m_item->appendItem(path); - this->insertRow(rowCount(QModelIndex()) - 1); + this->beginResetModel(); + m_item->m_pathlist << path; + this->endResetModel(); +// this->insertRow(rowCount(QModelIndex()) - 1); +} + +/** + * @brief SearchItemModel::appendList 直接以列表形式添加搜索结果 + * @param list + */ +void SearchItemModel::appendList(QStringList list) +{ + this->beginResetModel(); + m_item->m_pathlist = list; + this->endResetModel(); } /** @@ -135,5 +148,7 @@ void SearchItemModel::removeItem(QString path) { void SearchItemModel::clear() { + this->beginResetModel(); m_item->clear(); + this->endResetModel(); } diff --git a/src/model/search-item-model.h b/src/model/search-item-model.h index 2aa2858..b789829 100644 --- a/src/model/search-item-model.h +++ b/src/model/search-item-model.h @@ -33,6 +33,7 @@ public: void setItem(SearchItem *); void appendItem(QString); + void appendList(QStringList); void removeItem(QString); void clear(); diff --git a/src/model/search-item.cpp b/src/model/search-item.cpp index d79d7ff..7ab27f7 100644 --- a/src/model/search-item.cpp +++ b/src/model/search-item.cpp @@ -83,13 +83,6 @@ void SearchItem::setSearchList(const int& type, const QStringList& searchResult) m_pathlist = searchResult; } -/** - * @brief SearchItem::appendItem - */ -void SearchItem::appendItem(QString path) { - m_pathlist.append(path); -} - /** * @brief SearchItem::removeItem */ diff --git a/src/model/search-item.h b/src/model/search-item.h index b322e51..650ac05 100644 --- a/src/model/search-item.h +++ b/src/model/search-item.h @@ -16,17 +16,15 @@ public: ~SearchItem(); enum SearchType { - All, + Best, Apps, Settings, - Files, Dirs, - Contents, - Best + Files, + Contents }; void setSearchList(const int&, const QStringList&); - void appendItem(QString); void removeItem(QString); int getCurrentSize(); void clear();