Merge pull request #244 from mammonsama666/0526-frontend

feat(frontend): Complete some functions of new frontend.
This commit is contained in:
iaom 2021-05-27 21:00:22 +08:00 committed by GitHub
commit 8e440ccb02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 191 additions and 11 deletions

View File

@ -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)

View File

@ -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 *);

View File

@ -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);
}

View File

@ -47,6 +47,7 @@ Q_SIGNALS:
void startSearch(const QString &);
void stopSearch();
void currentRowChanged(const QString &, const SearchPluginIface::ResultInfo&);
void effectiveSearch();
};
}

View File

@ -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);
}

View File

@ -48,6 +48,7 @@ public:
Q_SIGNALS:
void startSearch(const QString &);
void stopSearch();
void effectiveSearch();
private:
void initWidgets();

View File

@ -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);
}
/**

View File

@ -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);
}
/********************测试用数据********************/
}

View File

@ -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 ="<<info.name;
m_item->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)
{

View File

@ -23,6 +23,8 @@
#include <QAbstractItemModel>
#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;
};
}

View File

@ -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);
}

View File

@ -3,6 +3,8 @@
#include <QTreeView>
#include <QVBoxLayout>
#include <QLabel>
#include <QMenu>
#include <QApplication>
#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();
};
}