From abd521f3abea01b94c5b4c12812c2f546fb4733d Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Sat, 26 Dec 2020 19:33:26 +0800 Subject: [PATCH 1/2] feat(homePage): Add homePage ui MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 添加初始界面UI Log: 添加初始界面UI --- control/control.pri | 2 + control/home-page-item.cpp | 67 ++++++++++++++++++++++++++++++++++ control/home-page-item.h | 37 +++++++++++++++++++ control/search-detail-view.cpp | 4 +- control/search-list-view.h | 2 +- src/content-widget.cpp | 51 ++++++++++++++++++++++++-- src/content-widget.h | 7 +++- src/mainwindow.cpp | 16 ++++++++ 8 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 control/home-page-item.cpp create mode 100644 control/home-page-item.h diff --git a/control/control.pri b/control/control.pri index 016e0c7..5f7bb1a 100644 --- a/control/control.pri +++ b/control/control.pri @@ -4,8 +4,10 @@ HEADERS += \ $$PWD/search-list-view.h \ $$PWD/search-detail-view.h \ $$PWD/option-view.h \ + $$PWD/home-page-item.h \ SOURCES += \ $$PWD/search-list-view.cpp \ $$PWD/search-detail-view.cpp \ $$PWD/option-view.cpp \ + $$PWD/home-page-item.cpp \ diff --git a/control/home-page-item.cpp b/control/home-page-item.cpp new file mode 100644 index 0000000..59a3903 --- /dev/null +++ b/control/home-page-item.cpp @@ -0,0 +1,67 @@ +#include "home-page-item.h" + +HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path) : QWidget(parent) +{ + setupUi(type, path); +} + +HomePageItem::~HomePageItem() +{ +} + +/** + * @brief HomePageItem::setupUi 根据不同的分栏创建item + * @param type 所属分栏 + * @param path 路径 + */ +void HomePageItem::setupUi(const int& type, const QString& path) { + m_widget = new QWidget(this); + m_widget->setObjectName("MainWidget"); + m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.1); border-radius: 4px;}"); + m_iconlabel = new QLabel(m_widget); + m_namelabel = new QLabel(m_widget); + if (type == ItemType::Recent) { + m_widget->setFixedSize(265, 48); + QIcon icon; + switch (SearchListView::getResType(path)) { //可能出现文件应用等,需要根据路径判断类型 + case SearchListView::ResType::App : { + icon = FileUtils::getAppIcon(path); + m_namelabel->setText(FileUtils::getAppName(path)); + break; + } + case SearchListView::ResType::File : { + icon = FileUtils::getFileIcon(QString("file://%1").arg(path)); + m_namelabel->setText(FileUtils::getFileName(path)); + break; + } + case SearchListView::ResType::Setting : { + icon = FileUtils::getSettingIcon(path, true); + m_namelabel->setText(FileUtils::getSettingName(path)); + break; + } + case SearchListView::ResType::Dir : { + break; + } + default : + break; + } + m_iconlabel->setPixmap(icon.pixmap(icon.actualSize(QSize(24, 24)))); + m_hlayout = new QHBoxLayout(m_widget); + m_iconlabel->setAlignment(Qt::AlignCenter); + m_namelabel->setAlignment(Qt::AlignCenter); + m_hlayout->addWidget(m_iconlabel); + m_hlayout->addWidget(m_namelabel); + m_hlayout->addStretch(); + return; + } + m_widget->setFixedSize(120, 120); + m_vlayout = new QVBoxLayout(m_widget); + m_vlayout->setContentsMargins(0,16,0,12); + m_iconlabel->setAlignment(Qt::AlignCenter); + m_namelabel->setAlignment(Qt::AlignCenter); + m_vlayout->addWidget(m_iconlabel); + m_vlayout->addWidget(m_namelabel); + QIcon icon = FileUtils::getAppIcon(path); + m_iconlabel->setPixmap(icon.pixmap(icon.actualSize(QSize(48, 48)))); + m_namelabel->setText(FileUtils::getAppName(path)); +} diff --git a/control/home-page-item.h b/control/home-page-item.h new file mode 100644 index 0000000..234fb6b --- /dev/null +++ b/control/home-page-item.h @@ -0,0 +1,37 @@ +#ifndef HOMEPAGEITEM_H +#define HOMEPAGEITEM_H + +#include +#include +#include +#include +#include "file-utils.h" +#include "search-list-view.h" + +class HomePageItem : public QWidget +{ + Q_OBJECT +public: + explicit HomePageItem(QWidget *, const int&, const QString&); + ~HomePageItem(); + + enum ItemType { //homepage中item的类型,包括常用应用、最近打开、快捷打开 + Common, + Recent, + Quick + }; + +private: + void setupUi(const int&, const QString&); + + QWidget * m_widget = nullptr; + QHBoxLayout * m_hlayout = nullptr; + QVBoxLayout * m_vlayout = nullptr; + QLabel * m_iconlabel = nullptr; + QLabel * m_namelabel = nullptr; + +Q_SIGNALS: + +}; + +#endif // HOMEPAGEITEM_H diff --git a/control/search-detail-view.cpp b/control/search-detail-view.cpp index de3cf8c..ececd62 100644 --- a/control/search-detail-view.cpp +++ b/control/search-detail-view.cpp @@ -98,14 +98,14 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) { } case SearchListView::ResType::File : { QIcon icon = FileUtils::getFileIcon(QString("file://%1").arg(path)); - iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(100, 100)))); + iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); nameLabel->setText(FileUtils::getFileName(path)); typeLabel->setText(tr("Document")); break; } case SearchListView::ResType::Setting : { QIcon icon = FileUtils::getSettingIcon(path, true); - iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(100, 100)))); + 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)); diff --git a/control/search-list-view.h b/control/search-list-view.h index 43e44f5..665ef48 100644 --- a/control/search-list-view.h +++ b/control/search-list-view.h @@ -21,7 +21,7 @@ public: }; int getCurrentType(); - int getResType(const QString&); + static int getResType(const QString&); private: SearchItemModel * m_model = nullptr; diff --git a/src/content-widget.cpp b/src/content-widget.cpp index d3d2579..de6ad2b 100644 --- a/src/content-widget.cpp +++ b/src/content-widget.cpp @@ -25,6 +25,7 @@ ContentWidget::~ContentWidget() void ContentWidget::initUI() { m_homePage = new QWidget; m_homePageLyt = new QVBoxLayout(m_homePage); + m_homePageLyt->setSpacing(0); m_homePage->setLayout(m_homePageLyt); m_resultPage = new QWidget; @@ -56,20 +57,59 @@ void ContentWidget::initUI() { m_detailView = new SearchDetailView(m_resultDetailArea); m_resultDetailArea->setWidget(m_detailView); m_resultDetailArea->setWidgetResizable(true); - m_homePage->setStyleSheet("QWidget{background:pink;}"); m_resultListArea->setStyleSheet("QScrollArea{background:transparent;}"); m_resultDetailArea->setStyleSheet("QScrollArea{background: rgba(0,0,0,0.05); border-radius: 4px;}"); this->addWidget(m_homePage); this->addWidget(m_resultPage); - setPageType(SearchItem::SearchType::All);//初始化按“全部”加载 + setPage(SearchItem::SearchType::All);//初始化按“全部”加载 +} + +/** + * @brief ContentWidget::initHomePage 向homepage填充内容 + * @param lists 三个列表:常用,最近,快捷 + */ +void ContentWidget::initHomePage(const QVector& lists) { + for (int i = 0; i < lists.count(); i++) { + QWidget * listWidget = new QWidget(m_homePage); + QVBoxLayout * itemWidgetLyt = new QVBoxLayout(listWidget); + QLabel * titleLabel = new QLabel(listWidget); + QWidget * itemWidget = new QWidget(listWidget); + if (i == 1) { + titleLabel->setText(tr("Recently Opened")); + QGridLayout * layout = new QGridLayout(itemWidget); + layout->setSpacing(8); + layout->setContentsMargins(0, 0, 0, 0); + itemWidget->setLayout(layout); + for (int j = 0; j < lists.at(i).count(); j++) { + HomePageItem * item = new HomePageItem(itemWidget, i, lists.at(i).at(j)); + layout->addWidget(item, j / 2, j % 2); + } + } else { + if (i) titleLabel->setText(tr("Commonly Used")); + else titleLabel->setText(tr("Open Quickly")); + QHBoxLayout * layout = new QHBoxLayout(itemWidget); + layout->setSpacing(8); + layout->setContentsMargins(0, 0, 0, 0); + itemWidget->setLayout(layout); + Q_FOREACH(QString path, lists.at(i)){ + HomePageItem * item = new HomePageItem(itemWidget, i, path); + layout->addWidget(item); + } + } + itemWidgetLyt->setSpacing(6); + titleLabel->setFixedHeight(24); + itemWidgetLyt->addWidget(titleLabel); + itemWidgetLyt->addWidget(itemWidget); + m_homePageLyt->addWidget(listWidget); + } } /** * @brief setPageType 预留的接口,为指定类别搜索调整界面内容 * @param type */ -void ContentWidget::setPageType(const int& type){ +void ContentWidget::setPage(const int& type){ m_currentType = type; } @@ -77,7 +117,7 @@ void ContentWidget::setPageType(const int& type){ * @brief ContentWidget::currentType 返回当前内容页(home或searchresult) * @return */ -int ContentWidget::currentType() { +int ContentWidget::currentPage() { return m_currentType; } @@ -91,6 +131,9 @@ void ContentWidget::refreshSearchList(const QVector& types, const QVectorsetContentsMargins(8, 0, 0, 0); diff --git a/src/content-widget.h b/src/content-widget.h index 90a8abd..1edb645 100644 --- a/src/content-widget.h +++ b/src/content-widget.h @@ -4,7 +4,9 @@ #include #include #include +#include #include "control/search-detail-view.h" +#include "home-page-item.h" class ContentWidget : public QStackedWidget { @@ -13,9 +15,10 @@ public: ContentWidget(QWidget *); ~ContentWidget(); - void setPageType(const int&); - int currentType(); + void setPage(const int&); + int currentPage(); void refreshSearchList(const QVector&, const QVector&); + void initHomePage(const QVector&); private: void initUI(); QWidget * m_homePage = nullptr; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 43dabc8..e5782a2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -146,6 +146,22 @@ void MainWindow::initUi() searchContent(text); } }); + + //初始化homepage + QVector lists; + + //测试用数据 + QStringList list; + list<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"/usr/share/applications/ukui-clock.desktop"<<"/usr/share/applications/wps-office-pdf.desktop"; + QStringList list2; + list2<<"/home/zjp/下载/搜索结果.png"<<"/home/zjp/下载/显示不全.mp4"<<"/home/zjp/下载/dmesg.log"<<"/home/zjp/下载/WiFi_AP选择.docx"; + + lists.append(list); + lists.append(list2); + lists.append(list); + + //将搜索结果加入列表 + m_contentFrame->initHomePage(lists); } /** From 5d74a805f65110d305643a9d2512d168afc546f3 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Sat, 26 Dec 2020 19:50:11 +0800 Subject: [PATCH 2/2] feat(searchList): Set height for listview. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 设置搜索结果列表的高度(动态改变) Log: 设置搜索结果列表的高度 --- control/search-list-view.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/control/search-list-view.cpp b/control/search-list-view.cpp index b48aa52..b213efa 100644 --- a/control/search-list-view.cpp +++ b/control/search-list-view.cpp @@ -14,7 +14,8 @@ SearchListView::SearchListView(QWidget * parent, const QStringList& list, const this->setHeaderHidden(true); this->setColumnWidth(0, 20); this->setColumnWidth(1, 80); - this->setFixedHeight(list.count() * 47 + 2); + int rowHeight = this->rowHeight(this->model()->index(0,1, QModelIndex())) + 1; + this->setFixedHeight(list.count() * rowHeight + 2); this->setAttribute(Qt::WA_TranslucentBackground, true); this->setAutoFillBackground(false); this->setStyleSheet("QWidget{background:transparent;}");