feat(homepage): Add MousePressEvent to homepage items.

Description: 向首页选项添加点击响应事件

Log: 向首页选项添加点击事件响应
This commit is contained in:
zhangjiaping 2020-12-28 15:06:48 +08:00
parent 29fe4bbaf3
commit 65021e829c
8 changed files with 163 additions and 32 deletions

View File

@ -1,4 +1,8 @@
#include "home-page-item.h" #include "home-page-item.h"
#include <QEvent>
#include <QProcess>
#include <QDebug>
#include <gio/gdesktopappinfo.h>
HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path) : QWidget(parent) HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path) : QWidget(parent)
{ {
@ -18,6 +22,37 @@ void HomePageItem::setupUi(const int& type, const QString& path) {
m_widget = new QWidget(this); m_widget = new QWidget(this);
m_widget->setObjectName("MainWidget"); m_widget->setObjectName("MainWidget");
m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.1); border-radius: 4px;}"); m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.1); border-radius: 4px;}");
m_widget->installEventFilter(this);
connect(this, &HomePageItem::onItemClicked, this, [ = ]() {
switch (SearchListView::getResType(path)) {
case SearchListView::ResType::App: {
GDesktopAppInfo * desktopAppInfo = g_desktop_app_info_new_from_filename(path.toLocal8Bit().data());
g_app_info_launch(G_APP_INFO(desktopAppInfo),nullptr, nullptr, nullptr);
g_object_unref(desktopAppInfo);
break;
}
case SearchListView::ResType::Dir:
case SearchListView::ResType::File: {
QProcess * process = new QProcess;
process->start(QString("xdg-open %1").arg(path));
connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
process->deleteLater();
});
break;
}
case SearchListView::ResType::Setting: {
//打开控制面板对应页面
QProcess * process = new QProcess;
process->start(QString("ukui-control-center --%1").arg(path.left(path.indexOf("/")).toLower()));
connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
process->deleteLater();
});
break;
}
default:
break;
}
});
m_iconlabel = new QLabel(m_widget); m_iconlabel = new QLabel(m_widget);
m_namelabel = new QLabel(m_widget); m_namelabel = new QLabel(m_widget);
if (type == ItemType::Recent) { if (type == ItemType::Recent) {
@ -53,6 +88,20 @@ void HomePageItem::setupUi(const int& type, const QString& path) {
m_hlayout->addWidget(m_namelabel); m_hlayout->addWidget(m_namelabel);
m_hlayout->addStretch(); m_hlayout->addStretch();
return; return;
} else if (type == ItemType::Quick) {
if (SearchListView::getResType(path) == SearchListView::ResType::Setting) {
QIcon icon = FileUtils::getSettingIcon(path, true);
m_iconlabel->setPixmap(icon.pixmap(icon.actualSize(QSize(48, 48))));
m_namelabel->setText(FileUtils::getSettingName(path));
} else {
QIcon icon = FileUtils::getAppIcon(path);
m_iconlabel->setPixmap(icon.pixmap(icon.actualSize(QSize(48, 48))));
m_namelabel->setText(FileUtils::getAppName(path));
}
} else {
QIcon icon = FileUtils::getAppIcon(path);
m_iconlabel->setPixmap(icon.pixmap(icon.actualSize(QSize(48, 48))));
m_namelabel->setText(FileUtils::getAppName(path));
} }
m_widget->setFixedSize(120, 120); m_widget->setFixedSize(120, 120);
m_vlayout = new QVBoxLayout(m_widget); m_vlayout = new QVBoxLayout(m_widget);
@ -61,7 +110,19 @@ void HomePageItem::setupUi(const int& type, const QString& path) {
m_namelabel->setAlignment(Qt::AlignCenter); m_namelabel->setAlignment(Qt::AlignCenter);
m_vlayout->addWidget(m_iconlabel); m_vlayout->addWidget(m_iconlabel);
m_vlayout->addWidget(m_namelabel); 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)); bool HomePageItem::eventFilter(QObject *watched, QEvent *event){
if (watched == m_widget){
if (event->type() == QEvent::MouseButtonPress) {
Q_EMIT this->onItemClicked();
m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.2); border-radius: 4px;}");
return true;
} else if (event->type() == QEvent::MouseButtonRelease) {
m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.1); border-radius: 4px;}");
return true;
}
}
return QObject::eventFilter(watched, event);
} }

View File

@ -21,6 +21,9 @@ public:
Quick Quick
}; };
protected:
bool eventFilter(QObject *, QEvent *);
private: private:
void setupUi(const int&, const QString&); void setupUi(const int&, const QString&);
@ -31,7 +34,7 @@ private:
QLabel * m_namelabel = nullptr; QLabel * m_namelabel = nullptr;
Q_SIGNALS: Q_SIGNALS:
void onItemClicked();
}; };
#endif // HOMEPAGEITEM_H #endif // HOMEPAGEITEM_H

View File

@ -11,6 +11,8 @@
#include <QProcess> #include <QProcess>
#include <QClipboard> #include <QClipboard>
#include <QApplication> #include <QApplication>
#include <QFileInfo>
#include <QDateTime>
SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent) SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent)
{ {
@ -76,14 +78,54 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) {
hLine->setFixedHeight(1); hLine->setFixedHeight(1);
hLine->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}"); hLine->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}");
m_layout->addWidget(iconLabel);
m_layout->addWidget(nameFrame);
m_layout->addWidget(hLine);
//文件和文件夹有一个额外的详情区域
if (type == SearchListView::ResType::Dir || type == SearchListView::ResType::File) {
QFrame * detailFrame = new QFrame(this);
QVBoxLayout * detailLyt = new QVBoxLayout(detailFrame);
detailLyt->setContentsMargins(0,0,0,0);
QFrame * pathFrame = new QFrame(detailFrame);
QFrame * timeFrame = new QFrame(detailFrame);
QHBoxLayout * pathLyt = new QHBoxLayout(pathFrame);
QHBoxLayout * timeLyt = new QHBoxLayout(timeFrame);
QLabel * pathLabel_1 = new QLabel(pathFrame);
QLabel * pathLabel_2 = new QLabel(pathFrame);
pathLabel_1->setText(tr("Path"));
pathLabel_2->setText(path);
pathLabel_2->setMaximumWidth(500);
pathLabel_2->setWordWrap(true);
pathLyt->addWidget(pathLabel_1);
pathLyt->addStretch();
pathLyt->addWidget(pathLabel_2);
QLabel * timeLabel_1 = new QLabel(timeFrame);
QLabel * timeLabel_2 = new QLabel(timeFrame);
timeLabel_1->setText(tr("Last time modified"));
QFileInfo fileInfo(path);
timeLabel_2->setText(fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss"));
timeLyt->addWidget(timeLabel_1);
timeLyt->addStretch();
timeLyt->addWidget(timeLabel_2);
detailLyt->addWidget(pathFrame);
detailLyt->addWidget(timeFrame);
QFrame * hLine_2 = new QFrame(this);
hLine_2->setLineWidth(0);
hLine_2->setFixedHeight(1);
hLine_2->setStyleSheet("QFrame{background: rgba(0,0,0,0.2);}");
m_layout->addWidget(detailFrame);
m_layout->addWidget(hLine_2);
}
//可执行操作区域
OptionView * optionView = new OptionView(this, type); OptionView * optionView = new OptionView(this, type);
connect(optionView, &OptionView::onOptionClicked, this, [ = ](const int& option) { connect(optionView, &OptionView::onOptionClicked, this, [ = ](const int& option) {
execActions(type, option, path); execActions(type, option, path);
}); });
m_layout->addWidget(iconLabel);
m_layout->addWidget(nameFrame);
m_layout->addWidget(hLine);
m_layout->addWidget(optionView); m_layout->addWidget(optionView);
m_layout->addStretch(); m_layout->addStretch();
@ -96,6 +138,7 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) {
typeLabel->setText(tr("Application")); typeLabel->setText(tr("Application"));
break; break;
} }
case SearchListView::ResType::Dir :
case SearchListView::ResType::File : { case SearchListView::ResType::File : {
QIcon icon = FileUtils::getFileIcon(QString("file://%1").arg(path)); QIcon icon = FileUtils::getFileIcon(QString("file://%1").arg(path));
iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96))));
@ -111,8 +154,6 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) {
typeLabel->setText(FileUtils::getSettingName(path)); typeLabel->setText(FileUtils::getSettingName(path));
break; break;
} }
case SearchListView::ResType::Dir :
break;
default: default:
break; break;
} }
@ -174,6 +215,12 @@ bool SearchDetailView::openAction(const int& type, const QString& path) {
} }
case SearchListView::ResType::Setting: { case SearchListView::ResType::Setting: {
//打开控制面板对应页面 //打开控制面板对应页面
QProcess * process = new QProcess;
process->start(QString("ukui-control-center --%1").arg(path.left(path.indexOf("/")).toLower()));
connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
process->deleteLater();
});
return true;
break; break;
} }
default: default:
@ -231,7 +278,7 @@ bool SearchDetailView::addPanelShortcut(const QString& path) {
*/ */
bool SearchDetailView::openPathAction(const QString& path) { bool SearchDetailView::openPathAction(const QString& path) {
QProcess * process = new QProcess; QProcess * process = new QProcess;
process->start(QString("xdg-open %1").arg(path.left(path.length() - path.lastIndexOf("/") + 1))); process->start(QString("xdg-open %1").arg(path.left(path.lastIndexOf("/"))));
connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() { connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
process->deleteLater(); process->deleteLater();
}); });

View File

@ -20,6 +20,7 @@ QIcon SearchItem::getIcon(int index) {
switch (m_searchtype) { switch (m_searchtype) {
case Settings : //设置项,返回控制面板对应插件的图标 case Settings : //设置项,返回控制面板对应插件的图标
return FileUtils::getSettingIcon(m_pathlist.at(index), false); return FileUtils::getSettingIcon(m_pathlist.at(index), false);
case Dirs :
case Files : //文件,返回文件图标 case Files : //文件,返回文件图标
return FileUtils::getFileIcon(QString("file://%1").arg(m_pathlist.at(index))); return FileUtils::getFileIcon(QString("file://%1").arg(m_pathlist.at(index)));
case Apps : //应用,返回应用图标 case Apps : //应用,返回应用图标
@ -42,6 +43,7 @@ QString SearchItem::getName(int index) {
switch (m_searchtype) { switch (m_searchtype) {
case Settings : //设置项,返回功能点名 case Settings : //设置项,返回功能点名
return FileUtils::getSettingName(m_pathlist.at(index)); return FileUtils::getSettingName(m_pathlist.at(index));
case Dirs :
case Files : //文件,返回文件名 case Files : //文件,返回文件名
return FileUtils::getFileName(m_pathlist.at(index)); return FileUtils::getFileName(m_pathlist.at(index));
case Apps : //应用,返回应用名 case Apps : //应用,返回应用名

View File

@ -86,8 +86,6 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
layout->addWidget(item, j / 2, j % 2); layout->addWidget(item, j / 2, j % 2);
} }
} else { } else {
if (i) titleLabel->setText(tr("Commonly Used"));
else titleLabel->setText(tr("Open Quickly"));
QHBoxLayout * layout = new QHBoxLayout(itemWidget); QHBoxLayout * layout = new QHBoxLayout(itemWidget);
layout->setSpacing(8); layout->setSpacing(8);
layout->setContentsMargins(0, 0, 0, 0); layout->setContentsMargins(0, 0, 0, 0);
@ -96,6 +94,13 @@ void ContentWidget::initHomePage(const QVector<QStringList>& lists) {
HomePageItem * item = new HomePageItem(itemWidget, i, path); HomePageItem * item = new HomePageItem(itemWidget, i, path);
layout->addWidget(item); layout->addWidget(item);
} }
if (i) {
titleLabel->setText(tr("Open Quickly"));
QWidget * emptyItem = new QWidget(itemWidget);
emptyItem->setFixedSize(136, 136); //占位用widget,若后续快捷打开有扩展项可删除此widget
layout->addWidget(emptyItem);
}
else titleLabel->setText(tr("Commonly Used"));
} }
itemWidgetLyt->setSpacing(6); itemWidgetLyt->setSpacing(6);
titleLabel->setFixedHeight(24); titleLabel->setFixedHeight(24);
@ -166,6 +171,8 @@ QString ContentWidget::getTitleName(const int& type) {
return tr("Settings"); return tr("Settings");
case SearchItem::SearchType::Files : case SearchItem::SearchType::Files :
return tr("Files"); return tr("Files");
case SearchItem::SearchType::Dirs :
return tr("Dirs");
case SearchItem::SearchType::Best : case SearchItem::SearchType::Best :
return tr("Best Matches"); return tr("Best Matches");
default : default :

View File

@ -48,12 +48,12 @@ int main(int argc, char *argv[])
//load chinese character and pinyin file to a Map //load chinese character and pinyin file to a Map
FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt"); FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt");
/*-------------InotyifyRefact Test Start---------------*/ /*-------------InotyifyRefact Test Start---------------*/
QTime t1 = QTime::currentTime(); // QTime t1 = QTime::currentTime();
InotifyManagerRefact* imr = new InotifyManagerRefact("/home"); // InotifyManagerRefact* imr = new InotifyManagerRefact("/home");
imr->start(); // imr->start();
QTime t2 = QTime::currentTime(); // QTime t2 = QTime::currentTime();
qDebug() << t1; // qDebug() << t1;
qDebug() << t2; // qDebug() << t2;
/*-------------InotyifyRefact Test End-----------------*/ /*-------------InotyifyRefact Test End-----------------*/
qRegisterMetaType<QVector<QStringList>>("QVector<QStringList>"); qRegisterMetaType<QVector<QStringList>>("QVector<QStringList>");

View File

@ -147,10 +147,12 @@ void MainWindow::initUi()
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"; 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; QStringList list2;
list2<<"/home/zjp/下载/搜索结果.png"<<"/home/zjp/下载/显示不全.mp4"<<"/home/zjp/下载/dmesg.log"<<"/home/zjp/下载/WiFi_AP选择.docx"; list2<<"/home/zjp/下载/搜索结果.png"<<"/home/zjp/下载/显示不全.mp4"<<"/home/zjp/下载/dmesg.log"<<"/home/zjp/下载/WiFi_AP选择.docx";
QStringList list3;
list3<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"Theme/主题/更改壁纸";
lists.append(list); lists.append(list);
lists.append(list2); lists.append(list2);
lists.append(list); lists.append(list3);
//将搜索结果加入列表 //将搜索结果加入列表
m_contentFrame->initHomePage(lists); m_contentFrame->initHomePage(lists);
@ -222,24 +224,29 @@ void MainWindow::primaryScreenChangedSlot(QScreen *screen)
void MainWindow::searchContent(QString searchcontent){ void MainWindow::searchContent(QString searchcontent){
// QVector<int> types; // QVector<int> types;
// QVector<QStringList> lists; // QVector<QStringList> lists;
m_lists.clear();
m_types.clear();
AppMatch * appMatchor = new AppMatch(this); AppMatch * appMatchor = new AppMatch(this);
SettingsMatch * settingMatchor = new SettingsMatch(this); SettingsMatch * settingMatchor = new SettingsMatch(this);
//测试用数据 //测试用数据
// QStringList list; QStringList list;
list = appMatchor->startMatchApp(searchcontent);
// list<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"/usr/share/applications/wps-office-pdf.desktop"; // list<<"/usr/share/applications/peony.desktop"<<"/usr/share/applications/ukui-control-center.desktop"<<"/usr/share/applications/wps-office-pdf.desktop";
// QStringList list2; // QStringList list2;
// list2<<"/home/zjp/下载/搜索结果.png"<<"/home/zjp/下载/显示不全.mp4"<<"/home/zjp/下载/dmesg.log"<<"/home/zjp/下载/WiFi_AP选择.docx"; // list2<<"/home/zjp/下载/搜索结果.png"<<"/home/zjp/下载/显示不全.mp4"<<"/home/zjp/下载/dmesg.log"<<"/home/zjp/下载/WiFi_AP选择.docx";
// QStringList list3; QStringList list3;
list3 = settingMatchor->startMatchApp(searchcontent);
// list3<<"About/关于/计算机属性"<<"Area/语言和地区/货币单位"<<"Datetime/时间和日期/手动更改时间"<<"Theme/主题/图标主题"; // list3<<"About/关于/计算机属性"<<"Area/语言和地区/货币单位"<<"Datetime/时间和日期/手动更改时间"<<"Theme/主题/图标主题";
// types.append(SearchItem::SearchType::Apps); m_types.append(SearchItem::SearchType::Apps);
// types.append(SearchItem::SearchType::Settings); m_types.append(SearchItem::SearchType::Settings);
// types.append(SearchItem::SearchType::Files); // types.append(SearchItem::SearchType::Files);
// lists.append(list); m_lists.append(list);
// lists.append(list3); m_lists.append(list3);
// lists.append(list2); // lists.append(list2);
// m_contentFrame->refreshSearchList(m_types, m_lists);
//文件搜索 //文件搜索
@ -248,14 +255,15 @@ void MainWindow::searchContent(QString searchcontent){
connect(searcher,&FileSearcher::result,[=](QVector<QStringList> resultV){ connect(searcher,&FileSearcher::result,[=](QVector<QStringList> resultV){
QStringList list1 = resultV.at(0); QStringList list1 = resultV.at(0);
// QStringList list2 = resultV.at(1); QStringList list2 = resultV.at(1);
QVector<QStringList> lists; // QVector<QStringList> lists;
lists.append(list1); m_lists.append(list1);
QVector<int> types; m_lists.append(list2);
types.append(SearchItem::SearchType::Files); // QVector<int> types;
// types.append(SearchItem::SearchType::Files); m_types.append(SearchItem::SearchType::Dirs);
m_contentFrame->refreshSearchList(types, lists); m_types.append(SearchItem::SearchType::Files);
m_contentFrame->refreshSearchList(m_types, m_lists);
}); });
searcher->onKeywordSearch(searchcontent,0,10); searcher->onKeywordSearch(searchcontent,0,10);
// QStringList res = IndexGenerator::IndexSearch(searchcontent); // QStringList res = IndexGenerator::IndexSearch(searchcontent);

View File

@ -73,6 +73,9 @@ private:
QGSettings * m_transparency_gsettings = nullptr; QGSettings * m_transparency_gsettings = nullptr;
double getTransparentData(); double getTransparentData();
QVector<int> m_types;
QVector<QStringList> m_lists;
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
void initUi(); void initUi();