From 3573c4d86ab96c933267174fec1ee2923d52f262 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Thu, 27 May 2021 10:25:15 +0800 Subject: [PATCH] feat(frontend): Complete some functions of new frontend. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 完善重构版前端部分原有功能,如字体适配、展开收起、历史记录等 Log: 完善重构版前端部分原有功能,如字体适配、展开收起、历史记录等 --- .../stack-pages/search-page-section.cpp | 15 ++- .../control/stack-pages/search-page-section.h | 4 + frontend/control/stack-pages/search-page.cpp | 1 + frontend/control/stack-pages/search-page.h | 1 + frontend/control/stacked-widget.cpp | 1 + frontend/control/stacked-widget.h | 1 + frontend/mainwindow.cpp | 1 + frontend/model/search-result-manager.cpp | 18 ++++ frontend/model/search-result-model.cpp | 38 +++++++- frontend/model/search-result-model.h | 8 ++ frontend/view/result-view.cpp | 97 +++++++++++++++++-- frontend/view/result-view.h | 17 +++- 12 files changed, 191 insertions(+), 11 deletions(-) diff --git a/frontend/control/stack-pages/search-page-section.cpp b/frontend/control/stack-pages/search-page-section.cpp index 29e34c2..aece8cb 100644 --- a/frontend/control/stack-pages/search-page-section.cpp +++ b/frontend/control/stack-pages/search-page-section.cpp @@ -48,7 +48,9 @@ void ResultArea::appendWidet(ResultWidget *widget) //NEW_TODO m_mainLyt->addWidget(widget); setupConnectionsForWidget(widget); - m_widget->setFixedHeight(m_widget->height() + widget->height()); + m_widget_list.append(widget); + int spacing_height = m_widget_list.length() > 1 ? m_mainLyt->spacing() : 0; + m_widget->setFixedHeight(m_widget->height() + widget->height() + spacing_height); } /** @@ -66,6 +68,16 @@ void ResultArea::setVisibleList(const QStringList &list) } } +void ResultArea::onWidgetSizeChanged() +{ + int whole_height = 0; + Q_FOREACH (ResultWidget *widget, m_widget_list) { + whole_height += widget->height(); + } + int spacing_height = m_widget_list.length() > 1 ? m_mainLyt->spacing() : 0; + m_widget->setFixedHeight(whole_height + spacing_height * (m_widget_list.length() - 1)); +} + void ResultArea::initUi() { QPalette pal = palette(); @@ -88,6 +100,7 @@ void ResultArea::setupConnectionsForWidget(ResultWidget *widget) { connect(this, &ResultArea::startSearch, widget, &ResultWidget::startSearch); connect(this, &ResultArea::stopSearch, widget, &ResultWidget::stopSearch); + connect(widget, &ResultWidget::sizeChanged, this, &ResultArea::onWidgetSizeChanged); } DetailArea::DetailArea(QWidget *parent) : QScrollArea(parent) diff --git a/frontend/control/stack-pages/search-page-section.h b/frontend/control/stack-pages/search-page-section.h index ec3717e..86016fa 100644 --- a/frontend/control/stack-pages/search-page-section.h +++ b/frontend/control/stack-pages/search-page-section.h @@ -36,6 +36,10 @@ public: ~ResultArea() = default; void appendWidet(ResultWidget *); void setVisibleList(const QStringList &); + +public Q_SLOTS: + void onWidgetSizeChanged(); + private: void initUi(); void setupConnectionsForWidget(ResultWidget *); diff --git a/frontend/control/stack-pages/search-page.cpp b/frontend/control/stack-pages/search-page.cpp index 3c3938e..5542b74 100644 --- a/frontend/control/stack-pages/search-page.cpp +++ b/frontend/control/stack-pages/search-page.cpp @@ -76,4 +76,5 @@ void SearchPage::setupConnectionsForWidget(ResultWidget *widget) connect(widget, &ResultWidget::currentRowChanged, m_detailArea, &DetailArea::setWidgetInfo); connect(widget, &ResultWidget::currentRowChanged, this, &SearchPage::currentRowChanged); connect(this, &SearchPage::currentRowChanged, widget, &ResultWidget::clearSelectedRow); + connect(widget, &ResultWidget::rowClicked, this, &SearchPage::effectiveSearch); } diff --git a/frontend/control/stack-pages/search-page.h b/frontend/control/stack-pages/search-page.h index 2740da5..3c81d1a 100644 --- a/frontend/control/stack-pages/search-page.h +++ b/frontend/control/stack-pages/search-page.h @@ -47,6 +47,7 @@ Q_SIGNALS: void startSearch(const QString &); void stopSearch(); void currentRowChanged(const QString &, const SearchPluginIface::ResultInfo&); + void effectiveSearch(); }; } diff --git a/frontend/control/stacked-widget.cpp b/frontend/control/stacked-widget.cpp index 2bc4040..b1a8aec 100644 --- a/frontend/control/stacked-widget.cpp +++ b/frontend/control/stacked-widget.cpp @@ -88,4 +88,5 @@ void StackedWidget::initConnections() { connect(this, &StackedWidget::startSearch, m_searchPage, &SearchPage::startSearch); connect(this, &StackedWidget::stopSearch, m_searchPage, &SearchPage::stopSearch); + connect(m_searchPage, &SearchPage::effectiveSearch, this, &StackedWidget::effectiveSearch); } diff --git a/frontend/control/stacked-widget.h b/frontend/control/stacked-widget.h index 4caf68d..4304947 100644 --- a/frontend/control/stacked-widget.h +++ b/frontend/control/stacked-widget.h @@ -48,6 +48,7 @@ public: Q_SIGNALS: void startSearch(const QString &); void stopSearch(); + void effectiveSearch(); private: void initWidgets(); diff --git a/frontend/mainwindow.cpp b/frontend/mainwindow.cpp index 25c60a4..754db57 100644 --- a/frontend/mainwindow.cpp +++ b/frontend/mainwindow.cpp @@ -201,6 +201,7 @@ void MainWindow::initConnections() m_iconLabel->setPixmap(QIcon::fromTheme("kylin-search").pixmap(QSize(WINDOW_ICON_SIZE, WINDOW_ICON_SIZE))); }); connect(m_searchLayout, &SearchBarHLayout::requestSearchKeyword, this, &MainWindow::searchKeywordSlot); + connect(m_stackedWidget, &StackedWidget::effectiveSearch, m_searchLayout, &SearchBarHLayout::effectiveSearchRecord); } /** diff --git a/frontend/model/search-result-manager.cpp b/frontend/model/search-result-manager.cpp index 09679ed..2dfcdb2 100644 --- a/frontend/model/search-result-manager.cpp +++ b/frontend/model/search-result-manager.cpp @@ -72,7 +72,25 @@ void SearchResultManager::startSearch(const QString &keyword) actions.append("复制路径"); test_info.description = desc; test_info.actionList = actions; + SearchPluginIface::ResultInfo test_info_1 = test_info; + test_info_1.name = "文件1"; + SearchPluginIface::ResultInfo test_info_2 = test_info; + test_info_2.name = "文件2"; + SearchPluginIface::ResultInfo test_info_3 = test_info; + test_info_3.name = "文件3"; + SearchPluginIface::ResultInfo test_info_4 = test_info; + test_info_4.name = "文件4"; + SearchPluginIface::ResultInfo test_info_5 = test_info; + test_info_5.name = "文件5"; + SearchPluginIface::ResultInfo test_info_6 = test_info; + test_info_6.name = "文件6"; m_result_queue->append(test_info); + m_result_queue->append(test_info_1); + m_result_queue->append(test_info_2); + m_result_queue->append(test_info_3); + m_result_queue->append(test_info_4); + m_result_queue->append(test_info_5); + m_result_queue->append(test_info_6); } /********************测试用数据********************/ } diff --git a/frontend/model/search-result-model.cpp b/frontend/model/search-result-model.cpp index 0440cff..2ee5ad8 100644 --- a/frontend/model/search-result-model.cpp +++ b/frontend/model/search-result-model.cpp @@ -44,7 +44,7 @@ QModelIndex SearchResultModel::parent(const QModelIndex &child) const int SearchResultModel::rowCount(const QModelIndex &index) const { - return index.isValid() ? 0 : m_item->m_result_info_list.length(); + return index.isValid() ? 0 : (m_isExpanded ? m_item->m_result_info_list.length() : NUM_LIMIT_SHOWN_DEFAULT); } int SearchResultModel::columnCount(const QModelIndex &index) const @@ -54,8 +54,6 @@ int SearchResultModel::columnCount(const QModelIndex &index) const QVariant SearchResultModel::data(const QModelIndex &index, int role) const { - if(!index.isValid()) - return QVariant(); switch(role) { case Qt::DecorationRole: { return m_item->m_result_info_list.at(index.row()).icon; @@ -81,6 +79,7 @@ void SearchResultModel::appendInfo(const SearchPluginIface::ResultInfo &info) this->beginResetModel(); qDebug()<<"Got a result. name ="<m_result_info_list.append(info); + Q_EMIT this->itemListChanged(m_item->m_result_info_list.length()); this->endResetModel(); } @@ -89,6 +88,7 @@ void SearchResultModel::startSearch(const QString &keyword) if (!m_item->m_result_info_list.isEmpty()) { this->beginResetModel(); m_item->m_result_info_list.clear(); + Q_EMIT this->itemListChanged(m_item->m_result_info_list.length()); this->endResetModel(); } m_search_manager->startSearch(keyword); @@ -105,6 +105,38 @@ const SearchPluginIface::ResultInfo &SearchResultModel::getInfo(const QModelInde return m_item->m_result_info_list.at(index.row()); } +void SearchResultModel::setExpanded(const bool &is_expanded) +{ + this->beginResetModel(); + m_isExpanded = is_expanded; + this->endResetModel(); + Q_EMIT this->itemListChanged(m_item->m_result_info_list.length()); +} + +const bool &SearchResultModel::isExpanded() +{ + return m_isExpanded; +} + +/** + * @brief SearchResultModel::getActions 获取操作列表 + * @param index + * @return + */ +QStringList SearchResultModel::getActions(const QModelIndex &index) +{ + if (m_item->m_result_info_list.length() > index.row() && index.row() >= 0) + return m_item->m_result_info_list.at(index.row()).actionList; + return QStringList(); +} + +QString SearchResultModel::getKey(const QModelIndex &index) +{ + if (m_item->m_result_info_list.length() > index.row() && index.row() >= 0) + return m_item->m_result_info_list.at(index.row()).key; + return NULL; +} + SearchResultItem::SearchResultItem(QObject *parent) : QObject(parent) { diff --git a/frontend/model/search-result-model.h b/frontend/model/search-result-model.h index 0c2c69c..968e40b 100644 --- a/frontend/model/search-result-model.h +++ b/frontend/model/search-result-model.h @@ -23,6 +23,8 @@ #include #include "search-result-manager.h" +#define NUM_LIMIT_SHOWN_DEFAULT 5 + namespace Zeeker { class SearchResultItem : public QObject { @@ -48,6 +50,10 @@ public: QVariant data(const QModelIndex &index, int role) const override; // bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex())override; const SearchPluginIface::ResultInfo & getInfo(const QModelIndex&); + void setExpanded(const bool&); + const bool &isExpanded(); + QStringList getActions(const QModelIndex &); + QString getKey(const QModelIndex &); public Q_SLOTS: void appendInfo(const SearchPluginIface::ResultInfo &); @@ -55,12 +61,14 @@ public Q_SLOTS: Q_SIGNALS: void stopSearch(); + void itemListChanged(const int&); private: void initConnections(); SearchResultItem * m_item = nullptr; QString m_plugin_id; SearchResultManager * m_search_manager = nullptr; + bool m_isExpanded = false; }; } diff --git a/frontend/view/result-view.cpp b/frontend/view/result-view.cpp index 331e9ce..ae83c24 100644 --- a/frontend/view/result-view.cpp +++ b/frontend/view/result-view.cpp @@ -2,7 +2,7 @@ #define MAIN_MARGINS 0,0,0,0 #define MAIN_SPACING 0 #define TITLE_HEIGHT 30 -#define UNFOLD_LABEL_HEIGHT 24 +#define UNFOLD_LABEL_HEIGHT 30 using namespace Zeeker; ResultWidget::ResultWidget(const QString &plugin_id, QWidget *parent) : QWidget(parent) @@ -27,8 +27,8 @@ void ResultWidget::setEnabled(const bool &enabled) */ void ResultWidget::expandListSlot() { -//NEW_TODO qWarning()<<"List will be expanded!"; + m_resultView->setExpanded(true); } /** @@ -36,8 +36,21 @@ void ResultWidget::expandListSlot() */ void ResultWidget::reduceListSlot() { -//NEW_TODO qWarning()<<"List will be reduced!"; + m_resultView->setExpanded(false); +} + +/** + * @brief ResultWidget::onListLengthChanged 响应列表长度改变的槽函数 + */ +void ResultWidget::onListLengthChanged(const int &length) +{ + this->setVisible(length > 0); + m_showMoreLabel->setVisible(length >= NUM_LIMIT_SHOWN_DEFAULT); + int show_more_height = m_showMoreLabel->isVisible() ? UNFOLD_LABEL_HEIGHT : 0; + int whole_height = this->isVisible() ? m_resultView->showHeight() + TITLE_HEIGHT + show_more_height : 0; + this->setFixedHeight(whole_height); + Q_EMIT this->sizeChanged(); } void ResultWidget::initUi() @@ -53,15 +66,14 @@ void ResultWidget::initUi() m_resultView = new ResultView(m_plugin_id, this); - //NEW_TODO 当列表条目大于n?时显示 m_showMoreLabel = new ShowMoreLabel(this); m_showMoreLabel->setFixedHeight(UNFOLD_LABEL_HEIGHT); -// m_showMoreLabel->hide(); + m_showMoreLabel->hide(); m_mainLyt->addWidget(m_titleLabel); m_mainLyt->addWidget(m_resultView); m_mainLyt->addWidget(m_showMoreLabel); - this->setFixedHeight(m_resultView->height() + TITLE_HEIGHT + UNFOLD_LABEL_HEIGHT); + this->setFixedHeight(m_resultView->height() + TITLE_HEIGHT); } void ResultWidget::initConnections() @@ -73,11 +85,20 @@ void ResultWidget::initConnections() connect(this, &ResultWidget::stopSearch, m_resultView, &ResultView::stopSearch); connect(this, &ResultWidget::stopSearch, this, [ = ]() { m_showMoreLabel->resetLabel(); + m_resultView->setExpanded(false); }); connect(m_resultView, &ResultView::currentRowChanged, this, &ResultWidget::currentRowChanged); connect(this, &ResultWidget::clearSelectedRow, m_resultView, &ResultView::clearSelectedRow); connect(m_showMoreLabel, &ShowMoreLabel::showMoreClicked, this, &ResultWidget::expandListSlot); connect(m_showMoreLabel, &ShowMoreLabel::retractClicked, this, &ResultWidget::reduceListSlot); + connect(m_resultView, &ResultView::listLengthChanged, this, &ResultWidget::onListLengthChanged); + connect(m_resultView, &ResultView::rowClicked, this, &ResultWidget::rowClicked); + connect(qApp, &QApplication::paletteChanged, this, [ = ]() { + int show_more_height = m_showMoreLabel->isVisible() ? UNFOLD_LABEL_HEIGHT : 0; + int whole_height = this->isVisible() ? m_resultView->showHeight() + TITLE_HEIGHT + show_more_height : 0; + this->setFixedHeight(whole_height); + Q_EMIT this->sizeChanged(); + }); } ResultView::ResultView(const QString &plugin_id, QWidget *parent) : QTreeView(parent) @@ -102,6 +123,19 @@ bool ResultView::isSelected() return m_is_selected; } +int ResultView::showHeight() +{ + int height; + int rowheight = this->rowHeight(this->model()->index(0, 0, QModelIndex())) + 1; + if (this->isExpanded()) { + height = m_count * rowheight; + } else { + int show_count = m_count > NUM_LIMIT_SHOWN_DEFAULT ? NUM_LIMIT_SHOWN_DEFAULT : m_count; + height = show_count * rowheight; + } + return height; +} + void ResultView::clearSelectedRow() { if (!m_is_selected) { @@ -152,6 +186,56 @@ void ResultView::onRowSelectedSlot(const QItemSelection &selected, const QItemSe } } +void ResultView::onItemListChanged(const int &count) +{ + m_count = count; + Q_EMIT this->listLengthChanged(count); +} + +void ResultView::setExpanded(const bool &is_expanded) +{ + m_model->setExpanded(is_expanded); +} + +const bool &ResultView::isExpanded() +{ + return m_model->isExpanded(); +} + +/** + * @brief ResultView::onMenuTriggered 点击右键菜单的槽函数 + * @param action + */ +void ResultView::onMenuTriggered(QAction *action) +{ + //NEW_TODO 接口调整后需要修改 + SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_plugin_id); + if (plugin) { + plugin->openAction(action->text(), m_model->getKey(this->currentIndex())); + } else { + qWarning()<<"Get plugin failed!"; + } +} + +void ResultView::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::RightButton) { + //加一点点延时,等待列表先被选中 + QTimer::singleShot(10, this, [ = ] { + QMenu * menu = new QMenu(this); + QStringList actions = m_model->getActions(this->currentIndex()); + Q_FOREACH (QString action, actions) { + menu->addAction(new QAction(action, this)); + } + menu->move(cursor().pos()); + menu->show(); + connect(menu, &QMenu::triggered, this, &ResultView::onMenuTriggered); + }); + } + Q_EMIT this->rowClicked(); + return QTreeView::mousePressEvent(event); +} + void ResultView::initConnections() { // connect(this, &ResultView::startSearch, m_model, &SearchResultModel::startSearch); @@ -162,4 +246,5 @@ void ResultView::initConnections() connect(this, &ResultView::stopSearch, m_model, &SearchResultModel::stopSearch); connect(this->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ResultView::onRowSelectedSlot); connect(this, &ResultView::activated, this, &ResultView::onRowDoubleClickedSlot); + connect(m_model, &SearchResultModel::itemListChanged, this, &ResultView::onItemListChanged); } diff --git a/frontend/view/result-view.h b/frontend/view/result-view.h index bf61801..190cd40 100644 --- a/frontend/view/result-view.h +++ b/frontend/view/result-view.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "search-result-model.h" #include "show-more-label.h" #include "title-label.h" @@ -17,11 +19,19 @@ public: ResultView(const QString &plugin_id, QWidget *parent = nullptr); ~ResultView() = default; bool isSelected(); + int showHeight(); public Q_SLOTS: void clearSelectedRow(); void onRowDoubleClickedSlot(const QModelIndex &); void onRowSelectedSlot(const QItemSelection &, const QItemSelection &); + void onItemListChanged(const int &); + void setExpanded(const bool &); + const bool &isExpanded(); + void onMenuTriggered(QAction *); + +protected: + void mousePressEvent(QMouseEvent *event) override; private: void initConnections(); @@ -29,12 +39,14 @@ private: QString m_plugin_id; bool m_is_selected = false; ResultViewDelegate * m_style_delegate = nullptr; + int m_count = 0; Q_SIGNALS: void startSearch(const QString &); void stopSearch(); void currentRowChanged(const QString &, const SearchPluginIface::ResultInfo&); - + void listLengthChanged(const int &); + void rowClicked(); }; class ResultWidget : public QWidget @@ -49,6 +61,7 @@ public: public Q_SLOTS: void expandListSlot(); void reduceListSlot(); + void onListLengthChanged(const int &); private: QString m_plugin_id; @@ -66,6 +79,8 @@ Q_SIGNALS: void stopSearch(); void currentRowChanged(const QString &, const SearchPluginIface::ResultInfo&); void clearSelectedRow(); + void sizeChanged(); + void rowClicked(); }; }