ukui-search/control/search-list-view.cpp

88 lines
3.0 KiB
C++
Raw Normal View History

#include "search-list-view.h"
#include <QDebug>
#include <QFileInfo>
SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent)
{
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);
m_model = new SearchItemModel;
m_item = new SearchItem;
m_item->setSearchList(type, list);
m_model->setItem(m_item);
this->setModel(m_model);
this->setHeaderHidden(true);
this->setColumnWidth(0, 20);
this->setColumnWidth(1, 80);
this->setFixedHeight(list.count() * 47 + 2);
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setAutoFillBackground(false);
this->setStyleSheet("QWidget{background:transparent;}");
m_type = type;
connect(this->selectionModel(), &QItemSelectionModel::selectionChanged, this, [ = ]() {
Q_EMIT this->currentRowChanged(getCurrentType(), m_item->m_pathlist.at(this->currentIndex().row()));
});
}
SearchListView::~SearchListView()
{
if (m_model) {
delete m_model;
m_model = NULL;
}
if (m_item) {
delete m_item;
m_item = NULL;
}
}
//获取当前选项所属搜索类型
int SearchListView::getCurrentType() {
switch (m_type) {
case SearchItem::SearchType::Apps :
// qDebug()<<"qDebug: One row selected, its type is application.";
return ResType::App;
case SearchItem::SearchType::Files:
// qDebug()<<"qDebug: One row selected, its type is file.";
return ResType::File;
case SearchItem::SearchType::Settings:
// qDebug()<<"qDebug: One row selected, its type is setting.";
return ResType::Setting;
case SearchItem::SearchType::Dirs:
// qDebug()<<"qDebug: One row selected, its type is dir.";
return ResType::Dir;
default: //All或者Best的情况需要自己判断文件类型
return getResType(m_item->m_pathlist.at(this->currentIndex().row()));
break;
}
}
/**
* @brief SearchListView::getResType
* @param path
* @return
*/
int SearchListView::getResType(const QString& path) {
if (path.endsWith(".desktop")) {
// qDebug()<<"qDebug: One row selected, its path is "<<path<<". Its type is application.";
return SearchListView::ResType::App;
} else if (QFileInfo(path).isFile()) {
// qDebug()<<"qDebug: One row selected, its path is "<<path<<". Its type is file.";
return SearchListView::ResType::File;
} else if (QFileInfo(path).isDir()) {
// qDebug()<<"qDebug: One row selected, its path is "<<path<<". Its type is dir.";
return SearchListView::ResType::Dir;
} else {
// qDebug()<<"qDebug: One row selected, its path is "<<path<<". Its type is setting.";
return SearchListView::ResType::Setting;
}
}
/**
* @brief SearchListView::clearSelection
*/
void SearchListView::clearSelection() {
this->selectionModel()->clearSelection();
}