2021-08-02 13:46:59 +08:00
|
|
|
#include "best-list-view.h"
|
|
|
|
#define MAIN_MARGINS 0,0,0,0
|
|
|
|
#define MAIN_SPACING 0
|
|
|
|
#define TITLE_HEIGHT 30
|
|
|
|
#define UNFOLD_LABEL_HEIGHT 30
|
2021-11-09 09:48:32 +08:00
|
|
|
#define VIEW_ICON_SIZE 24
|
2021-08-02 13:46:59 +08:00
|
|
|
|
2021-12-14 14:43:35 +08:00
|
|
|
using namespace UkuiSearch;
|
2021-08-02 13:46:59 +08:00
|
|
|
BestListView::BestListView(QWidget *parent) : QTreeView(parent)
|
|
|
|
{
|
2022-11-04 17:35:40 +08:00
|
|
|
setStyle(ResultItemStyle::getStyle());
|
2021-08-02 13:46:59 +08:00
|
|
|
this->setFrameShape(QFrame::NoFrame);
|
|
|
|
this->viewport()->setAutoFillBackground(false);
|
2021-11-09 09:48:32 +08:00
|
|
|
this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE));
|
2021-08-02 13:46:59 +08:00
|
|
|
this->setRootIsDecorated(false);
|
|
|
|
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
this->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
this->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
this->setHeaderHidden(true);
|
|
|
|
m_model = new BestListModel(this);
|
|
|
|
this->setModel(m_model);
|
|
|
|
initConnections();
|
2022-06-15 15:54:50 +08:00
|
|
|
m_styleDelegate = new ResultViewDelegate(this);
|
|
|
|
this->setItemDelegate(m_styleDelegate);
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
2021-08-05 15:37:58 +08:00
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
bool BestListView::isSelected()
|
|
|
|
{
|
|
|
|
return m_is_selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BestListView::showHeight()
|
|
|
|
{
|
2022-07-18 16:34:26 +08:00
|
|
|
int height(0);
|
|
|
|
// int rowheight = this->rowHeight(this->model()->index(0, 0, QModelIndex()));
|
|
|
|
// 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;
|
|
|
|
// }
|
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
if (this->isExpanded()) {
|
2022-07-18 16:34:26 +08:00
|
|
|
for (int i = 0; i<m_count; ++i) {
|
|
|
|
height += this->rowHeight(this->model()->index(i, 0, QModelIndex()));
|
|
|
|
}
|
2021-08-02 13:46:59 +08:00
|
|
|
} else {
|
|
|
|
int show_count = m_count > NUM_LIMIT_SHOWN_DEFAULT ? NUM_LIMIT_SHOWN_DEFAULT : m_count;
|
2022-07-18 16:34:26 +08:00
|
|
|
for (int i = 0; i<show_count; ++i) {
|
|
|
|
height += this->rowHeight(this->model()->index(i, 0, QModelIndex()));
|
|
|
|
}
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:23:14 +08:00
|
|
|
int BestListView::getResultNum()
|
|
|
|
{
|
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex BestListView::getModlIndex(int row, int column)
|
|
|
|
{
|
|
|
|
return this->m_model->index(row, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchPluginIface::ResultInfo BestListView::getIndexResultInfo(QModelIndex &index)
|
|
|
|
{
|
|
|
|
return this->m_model->getInfo(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString BestListView::getPluginInfo(const QModelIndex& index)
|
|
|
|
{
|
|
|
|
return this->m_model->getPluginInfo(index);
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:10:28 +08:00
|
|
|
int BestListView::getResultHeight()
|
|
|
|
{
|
|
|
|
return this->rowHeight(this->model()->index(0, 0, QModelIndex()));
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
void BestListView::clearSelectedRow()
|
|
|
|
{
|
|
|
|
if (!m_is_selected) {
|
|
|
|
this->blockSignals(true);
|
2021-09-22 10:29:10 +08:00
|
|
|
//this->clearSelection();
|
|
|
|
this->setCurrentIndex(QModelIndex());
|
2021-08-02 13:46:59 +08:00
|
|
|
this->blockSignals(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief BestListView::onRowDoubleClickedSlot 处理列表中的双击打开事件
|
|
|
|
* @param index 点击的条目
|
|
|
|
*/
|
|
|
|
void BestListView::onRowDoubleClickedSlot(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
const SearchPluginIface::ResultInfo &info = m_model->getInfo(index);
|
2021-08-06 10:04:36 +08:00
|
|
|
QString plugin_id = m_model->getPluginInfo(index);;
|
|
|
|
SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(plugin_id);
|
|
|
|
try {
|
|
|
|
if (plugin) {
|
|
|
|
if (!info.actionKey.isEmpty()) {
|
|
|
|
plugin->openAction(0, info.actionKey, info.type);
|
|
|
|
} else {
|
|
|
|
throw -2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw -1;
|
|
|
|
}
|
|
|
|
} catch(int e) {
|
|
|
|
qWarning()<<"Open failed, reason="<<e;
|
|
|
|
}
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief BestListView::onRowSelectedSlot 处理列表项选中事件
|
|
|
|
* @param selected
|
|
|
|
* @param deselected
|
|
|
|
*/
|
2021-08-18 14:23:14 +08:00
|
|
|
void BestListView::onRowSelectedSlot(const QModelIndex &index)
|
2021-08-02 13:46:59 +08:00
|
|
|
{
|
2021-10-20 16:30:20 +08:00
|
|
|
if (index.isValid()) {
|
|
|
|
m_is_selected = true;
|
|
|
|
this->setCurrentIndex(index);
|
|
|
|
Q_EMIT this->currentRowChanged(m_model->getPluginInfo(index), m_model->getInfo(index));
|
|
|
|
}
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BestListView::onItemListChanged(const int &count)
|
|
|
|
{
|
|
|
|
m_count = count;
|
|
|
|
Q_EMIT this->listLengthChanged(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BestListView::setExpanded(const bool &is_expanded)
|
|
|
|
{
|
2021-08-24 17:10:28 +08:00
|
|
|
QModelIndex index = this->currentIndex();
|
2021-08-02 13:46:59 +08:00
|
|
|
m_model->setExpanded(is_expanded);
|
2021-08-24 17:10:28 +08:00
|
|
|
this->setCurrentIndex(index);
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool &BestListView::isExpanded()
|
|
|
|
{
|
|
|
|
return m_model->isExpanded();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief BestListView::onMenuTriggered 点击右键菜单的槽函数
|
|
|
|
* @param action
|
|
|
|
*/
|
|
|
|
void BestListView::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 BestListView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2021-09-08 16:34:32 +08:00
|
|
|
m_tmpCurrentIndex = this->currentIndex();
|
|
|
|
m_tmpMousePressIndex = indexAt(event->pos());
|
2021-09-09 11:23:59 +08:00
|
|
|
if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) {
|
|
|
|
Q_EMIT this->clicked(m_tmpMousePressIndex);
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
return QTreeView::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2021-09-08 16:34:32 +08:00
|
|
|
void BestListView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
2021-10-20 16:30:20 +08:00
|
|
|
QModelIndex index = indexAt(event->pos());
|
|
|
|
if (index.isValid()) {
|
|
|
|
Q_EMIT this->clicked(index);
|
|
|
|
} else {
|
|
|
|
Q_EMIT this->clicked(this->currentIndex());
|
|
|
|
}
|
2021-09-08 16:34:32 +08:00
|
|
|
return QTreeView::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
2021-09-09 11:23:59 +08:00
|
|
|
void BestListView::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
2021-10-20 16:30:20 +08:00
|
|
|
m_tmpCurrentIndex = this->currentIndex();
|
|
|
|
m_tmpMousePressIndex = indexAt(event->pos());
|
|
|
|
if (m_tmpMousePressIndex.isValid() and m_tmpCurrentIndex != m_tmpMousePressIndex) {
|
|
|
|
Q_EMIT this->clicked(m_tmpMousePressIndex);
|
|
|
|
}
|
2021-09-09 11:23:59 +08:00
|
|
|
return QTreeView::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
void BestListView::initConnections()
|
|
|
|
{
|
|
|
|
connect(this, &BestListView::startSearch, [ = ](const QString &keyword) {
|
|
|
|
qDebug() << "==========start search!";
|
2022-06-15 15:54:50 +08:00
|
|
|
m_styleDelegate->setSearchKeyword(keyword);
|
2021-08-02 13:46:59 +08:00
|
|
|
m_model->startSearch(keyword);
|
|
|
|
});
|
|
|
|
connect(this, &BestListView::startSearch, m_model, &BestListModel::startSearch);
|
2021-08-18 14:23:14 +08:00
|
|
|
connect(this, &BestListView::clicked, this, &BestListView::onRowSelectedSlot);
|
2021-08-02 13:46:59 +08:00
|
|
|
connect(this, &BestListView::activated, this, &BestListView::onRowDoubleClickedSlot);
|
|
|
|
connect(m_model, &BestListModel::itemListChanged, this, &BestListView::onItemListChanged);
|
2021-08-24 17:10:28 +08:00
|
|
|
connect(this, &BestListView::sendBestListData, this, [=] (const QString &plugin, const SearchPluginIface::ResultInfo&info) {
|
|
|
|
QModelIndex index = this->currentIndex();
|
|
|
|
this->m_model->appendInfo(plugin, info);
|
|
|
|
if (index.isValid()) {
|
|
|
|
this->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
});
|
2022-11-22 10:17:36 +08:00
|
|
|
connect(SearchPluginManager::getInstance(), &SearchPluginManager::unregistered, this, [=] (const QString &plugin) {
|
|
|
|
QModelIndex index = this->currentIndex();
|
|
|
|
this->m_model->removeInfo(plugin);
|
|
|
|
if (index.isValid()) {
|
|
|
|
this->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(SearchPluginManager::getInstance(), &SearchPluginManager::changePos, this, [ = ] (const QString &pluginName, const int index){
|
|
|
|
this->m_model->moveInfo(pluginName, index);
|
|
|
|
});
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BestListWidget::BestListWidget(QWidget *parent) : QWidget(parent)
|
|
|
|
{
|
|
|
|
this->initUi();
|
|
|
|
initConnections();
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:23:14 +08:00
|
|
|
QString BestListWidget::getWidgetName()
|
|
|
|
{
|
|
|
|
return m_titleLabel->text();
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
void BestListWidget::setEnabled(const bool &enabled)
|
|
|
|
{
|
|
|
|
m_enabled = enabled;
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:23:14 +08:00
|
|
|
void BestListWidget::clearResult()
|
|
|
|
{
|
|
|
|
this->setVisible(false);
|
|
|
|
this->setFixedHeight(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int BestListWidget::getResultNum()
|
|
|
|
{
|
|
|
|
return m_bestListView->getResultNum();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BestListWidget::setResultSelection(const QModelIndex &index)
|
|
|
|
{
|
2021-08-24 17:10:28 +08:00
|
|
|
this->m_bestListView->setCurrentIndex(index);
|
2021-08-18 14:23:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BestListWidget::clearResultSelection()
|
|
|
|
{
|
2021-08-24 17:10:28 +08:00
|
|
|
//this->m_bestListView->selectionModel()->clearSelection();
|
|
|
|
this->m_bestListView->setCurrentIndex(QModelIndex());
|
2021-08-18 14:23:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex BestListWidget::getModlIndex(int row, int column)
|
|
|
|
{
|
|
|
|
return this->m_bestListView->getModlIndex(row, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BestListWidget::activateIndex()
|
|
|
|
{
|
|
|
|
this->m_bestListView->onRowDoubleClickedSlot(this->m_bestListView->currentIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex BestListWidget::getCurrentSelection()
|
|
|
|
{
|
|
|
|
return this->m_bestListView->currentIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BestListWidget::getExpandState()
|
|
|
|
{
|
|
|
|
return m_bestListView->isExpanded();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchPluginIface::ResultInfo BestListWidget::getIndexResultInfo(QModelIndex &index)
|
|
|
|
{
|
|
|
|
return m_bestListView->getIndexResultInfo(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString BestListWidget::getPluginInfo(const QModelIndex&index)
|
|
|
|
{
|
|
|
|
return this->m_bestListView->getPluginInfo(index);
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:10:28 +08:00
|
|
|
int BestListWidget::getResultHeight()
|
|
|
|
{
|
|
|
|
return this->m_bestListView->getResultHeight();
|
|
|
|
}
|
2021-08-02 13:46:59 +08:00
|
|
|
/**
|
|
|
|
* @brief BestListWidget::expandListSlot 展开列表的槽函数
|
|
|
|
*/
|
|
|
|
void BestListWidget::expandListSlot()
|
|
|
|
{
|
|
|
|
qWarning()<<"List will be expanded!";
|
|
|
|
m_bestListView->setExpanded(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief BestListWidget::reduceListSlot 收起列表的槽函数
|
|
|
|
*/
|
|
|
|
void BestListWidget::reduceListSlot()
|
|
|
|
{
|
|
|
|
qWarning()<<"List will be reduced!";
|
|
|
|
m_bestListView->setExpanded(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief BestListWidget::onListLengthChanged 响应列表长度改变的槽函数
|
|
|
|
*/
|
|
|
|
void BestListWidget::onListLengthChanged(const int &length)
|
|
|
|
{
|
|
|
|
this->setVisible(length > 0);
|
2021-08-05 15:37:58 +08:00
|
|
|
int whole_height = this->isVisible() ? (m_bestListView->showHeight() + TITLE_HEIGHT) : 0;
|
2021-08-02 13:46:59 +08:00
|
|
|
this->setFixedHeight(whole_height);
|
|
|
|
Q_EMIT this->sizeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BestListWidget::initUi()
|
|
|
|
{
|
|
|
|
m_mainLyt = new QVBoxLayout(this);
|
|
|
|
this->setLayout(m_mainLyt);
|
|
|
|
m_mainLyt->setContentsMargins(MAIN_MARGINS);
|
|
|
|
m_mainLyt->setSpacing(MAIN_SPACING);
|
|
|
|
|
|
|
|
m_titleLabel = new TitleLabel(this);
|
|
|
|
m_titleLabel->setText(tr("Best Matches"));
|
|
|
|
m_titleLabel->setFixedHeight(TITLE_HEIGHT);
|
|
|
|
|
|
|
|
m_bestListView = new BestListView(this);
|
|
|
|
|
|
|
|
m_mainLyt->addWidget(m_titleLabel);
|
|
|
|
m_mainLyt->addWidget(m_bestListView);
|
|
|
|
this->setFixedHeight(m_bestListView->height() + TITLE_HEIGHT);
|
2021-08-12 14:57:25 +08:00
|
|
|
this->setFixedWidth(656);
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BestListWidget::initConnections()
|
|
|
|
{
|
|
|
|
connect(this, &BestListWidget::startSearch, m_bestListView, &BestListView::startSearch);
|
2021-08-05 15:37:58 +08:00
|
|
|
connect(this, &BestListWidget::startSearch, m_titleLabel, &TitleLabel::startSearch);
|
2021-08-12 14:57:25 +08:00
|
|
|
connect(this, &BestListWidget::startSearch, m_titleLabel, &TitleLabel::startSearch);
|
2021-08-05 15:37:58 +08:00
|
|
|
connect(this, &BestListWidget::stopSearch, m_titleLabel, &TitleLabel::stopSearch);
|
2021-08-02 13:46:59 +08:00
|
|
|
connect(this, &BestListWidget::sendBestListData, m_bestListView, &BestListView::sendBestListData);
|
|
|
|
connect(m_bestListView, &BestListView::currentRowChanged, this, &BestListWidget::currentRowChanged);
|
|
|
|
connect(this, &BestListWidget::clearSelectedRow, m_bestListView, &BestListView::clearSelectedRow);
|
2021-08-05 15:37:58 +08:00
|
|
|
connect(m_titleLabel, &TitleLabel::showMoreClicked, this, &BestListWidget::expandListSlot);
|
|
|
|
connect(m_titleLabel, &TitleLabel::retractClicked, this, &BestListWidget::reduceListSlot);
|
2021-08-02 13:46:59 +08:00
|
|
|
connect(m_bestListView, &BestListView::listLengthChanged, this, &BestListWidget::onListLengthChanged);
|
2021-08-23 19:37:17 +08:00
|
|
|
connect(m_bestListView, &BestListView::listLengthChanged, m_titleLabel, &TitleLabel::onListLengthChanged);
|
2021-08-18 14:23:14 +08:00
|
|
|
connect(m_bestListView, &BestListView::clicked, this, &BestListWidget::rowClicked);
|
2021-08-02 13:46:59 +08:00
|
|
|
connect(qApp, &QApplication::paletteChanged, this, [ = ]() {
|
2021-08-05 15:37:58 +08:00
|
|
|
int whole_height = this->isVisible() ? m_bestListView->showHeight() + TITLE_HEIGHT : 0;
|
2021-08-02 13:46:59 +08:00
|
|
|
this->setFixedHeight(whole_height);
|
|
|
|
Q_EMIT this->sizeChanged();
|
|
|
|
});
|
|
|
|
}
|