From 052b6b29d2a3d1c41ebee2f1505223fa04442f61 Mon Sep 17 00:00:00 2001 From: haruyukilxz Date: Mon, 2 Jul 2018 16:21:43 +0800 Subject: [PATCH] feat(TipsWidget): use one qss file - Using setstylesheet once will create new objects Change-Id: I8126113766f9b1881b2b3825dc1985c6eea9b854 --- frame/CMakeLists.txt | 2 +- frame/frame.qrc | 5 +++++ frame/item/appitem.cpp | 7 +++--- frame/item/appitem.h | 25 ++++++++++----------- frame/item/components/appsnapshot.cpp | 2 +- frame/item/containeritem.cpp | 5 +---- frame/item/launcheritem.cpp | 4 +--- frame/item/launcheritem.h | 3 ++- frame/item/tipswidget.cpp | 29 +++++++++++++++++++++++++ frame/item/tipswidget.h | 21 ++++++++++++++++++ frame/panel/mainpanel.cpp | 31 ++++++++------------------- frame/qss/frame.qss | 19 ++++++++++++++++ 12 files changed, 105 insertions(+), 48 deletions(-) create mode 100644 frame/frame.qrc create mode 100644 frame/item/tipswidget.cpp create mode 100644 frame/item/tipswidget.h create mode 100644 frame/qss/frame.qss diff --git a/frame/CMakeLists.txt b/frame/CMakeLists.txt index 64594ba7c..349240696 100644 --- a/frame/CMakeLists.txt +++ b/frame/CMakeLists.txt @@ -20,7 +20,7 @@ pkg_check_modules(DFrameworkDBus REQUIRED dframeworkdbus) pkg_check_modules(QGSettings REQUIRED gsettings-qt) # driver-manager -add_executable(${BIN_NAME} ${SRCS} ${INTERFACES} item/resources.qrc) +add_executable(${BIN_NAME} ${SRCS} ${INTERFACES} item/resources.qrc frame.qrc) target_include_directories(${BIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} ${XCB_EWMH_INCLUDE_DIRS} ${DFrameworkDBus_INCLUDE_DIRS} diff --git a/frame/frame.qrc b/frame/frame.qrc new file mode 100644 index 000000000..2824e972f --- /dev/null +++ b/frame/frame.qrc @@ -0,0 +1,5 @@ + + + qss/frame.qss + + diff --git a/frame/item/appitem.cpp b/frame/item/appitem.cpp index d2c463a6f..2a62b0d9d 100644 --- a/frame/item/appitem.cpp +++ b/frame/item/appitem.cpp @@ -105,7 +105,7 @@ QPoint AppItem::MousePressPos; AppItem::AppItem(const QDBusObjectPath &entry, QWidget *parent) : DockItem(parent), - m_appNameTips(new QLabel(this)), + m_appNameTips(new TipsWidget(this)), m_appPreviewTips(new PreviewContainer(this)), m_itemEntryInter(new DockEntryInter("com.deepin.dde.daemon.Dock", entry.path(), QDBusConnection::sessionBus(), this)), @@ -149,11 +149,10 @@ AppItem::AppItem(const QDBusObjectPath &entry, QWidget *parent) m_id = m_itemEntryInter->id(); m_active = m_itemEntryInter->isActive(); - m_appNameTips->setObjectName(m_itemEntryInter->name()); + m_appNameTips->setObjectName("AppItemTips"); m_appNameTips->setAccessibleName(m_itemEntryInter->name() + "-tips"); m_appNameTips->setVisible(false); - m_appNameTips->setStyleSheet("color:white;" - "padding:0px 3px;"); + m_appNameTips->installEventFilter(this); m_updateIconGeometryTimer->setInterval(500); m_updateIconGeometryTimer->setSingleShot(true); diff --git a/frame/item/appitem.h b/frame/item/appitem.h index 5c7011082..e61e7d8c4 100644 --- a/frame/item/appitem.h +++ b/frame/item/appitem.h @@ -25,6 +25,7 @@ #include "dockitem.h" #include "components/previewcontainer.h" #include "dbus/dbusclientmanager.h" +#include "item/tipswidget.h" #include #include @@ -57,17 +58,17 @@ class AppItem : public DockItem void requestCancelPreview() const; private: - void moveEvent(QMoveEvent *e); - void paintEvent(QPaintEvent *e); - void mouseReleaseEvent(QMouseEvent *e); - void mousePressEvent(QMouseEvent *e); - void mouseMoveEvent(QMouseEvent *e); - void wheelEvent(QWheelEvent *e); - void resizeEvent(QResizeEvent *e); - void dragEnterEvent(QDragEnterEvent *e); - void dragMoveEvent(QDragMoveEvent *e); - void dropEvent(QDropEvent *e); - void leaveEvent(QEvent *e); + void moveEvent(QMoveEvent *e) override; + void paintEvent(QPaintEvent *e) override; + void mouseReleaseEvent(QMouseEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + void mouseMoveEvent(QMouseEvent *e) override; + void wheelEvent(QWheelEvent *e) override; + void resizeEvent(QResizeEvent *e) override; + void dragEnterEvent(QDragEnterEvent *e) override; + void dragMoveEvent(QDragMoveEvent *e) override; + void dropEvent(QDropEvent *e) override; + void leaveEvent(QEvent *e) override; void showEvent(QShowEvent *e) override; void showHoverTips(); @@ -89,7 +90,7 @@ private slots: void checkAttentionEffect(); private: - QLabel *m_appNameTips; + TipsWidget *m_appNameTips; PreviewContainer *m_appPreviewTips; DockEntryInter *m_itemEntryInter; diff --git a/frame/item/components/appsnapshot.cpp b/frame/item/components/appsnapshot.cpp index 4e0f5dc20..6b1970d52 100644 --- a/frame/item/components/appsnapshot.cpp +++ b/frame/item/components/appsnapshot.cpp @@ -46,7 +46,7 @@ AppSnapshot::AppSnapshot(const WId wid, QWidget *parent) m_closeBtn->setHoverPic(":/icons/resources/close_round_hover.svg"); m_closeBtn->setPressPic(":/icons/resources/close_round_press.svg"); m_closeBtn->setVisible(false); - m_title->setStyleSheet("color: white;"); + m_title->setObjectName("AppSnapshotTitle"); QHBoxLayout *centralLayout = new QHBoxLayout; centralLayout->addWidget(m_title); diff --git a/frame/item/containeritem.cpp b/frame/item/containeritem.cpp index e9afd7fd2..601f13234 100644 --- a/frame/item/containeritem.cpp +++ b/frame/item/containeritem.cpp @@ -32,10 +32,7 @@ ContainerItem::ContainerItem(QWidget *parent) m_containerWidget->setVisible(false); m_popupTips->setText(tr("Click to display hidden icon")); m_popupTips->setVisible(false); - m_popupTips->setStyleSheet("QLabel {" - "color: white;" - "margin: 2px 4px;" - "}"); + m_popupTips->setObjectName("ContainerItem"); setAcceptDrops(true); } diff --git a/frame/item/launcheritem.cpp b/frame/item/launcheritem.cpp index 6b277a687..0c406c913 100644 --- a/frame/item/launcheritem.cpp +++ b/frame/item/launcheritem.cpp @@ -33,14 +33,12 @@ DCORE_USE_NAMESPACE LauncherItem::LauncherItem(QWidget *parent) : DockItem(parent), - m_tips(new QLabel(this)) + m_tips(new TipsWidget(this)) { setAccessibleName("Launcher"); m_tips->setVisible(false); m_tips->setObjectName("launcher"); m_tips->setText(tr("Launcher")); - m_tips->setStyleSheet("color:white;" - "padding:0px 3px;"); } void LauncherItem::refershIcon() diff --git a/frame/item/launcheritem.h b/frame/item/launcheritem.h index 1d340896b..33518d588 100644 --- a/frame/item/launcheritem.h +++ b/frame/item/launcheritem.h @@ -23,6 +23,7 @@ #define LAUNCHERITEM_H #include "dockitem.h" +#include "tipswidget.h" class LauncherItem : public DockItem { @@ -46,7 +47,7 @@ class LauncherItem : public DockItem QPixmap m_smallIcon; QPixmap m_largeIcon; - QLabel *m_tips; + TipsWidget *m_tips; }; #endif // LAUNCHERITEM_H diff --git a/frame/item/tipswidget.cpp b/frame/item/tipswidget.cpp new file mode 100644 index 000000000..54a568a66 --- /dev/null +++ b/frame/item/tipswidget.cpp @@ -0,0 +1,29 @@ +#include "tipswidget.h" + +#include + +TipsWidget::TipsWidget(QWidget *parent) : QFrame(parent) +{ + +} + +void TipsWidget::setText(const QString &text) +{ + m_text = text; + + setFixedSize(fontMetrics().width(text) + 6, fontMetrics().height()); +} + +void TipsWidget::paintEvent(QPaintEvent *event) +{ + QFrame::paintEvent(event); + + QPainter painter(this); + + QPen pen(Qt::white); + painter.setPen(pen); + + QTextOption option; + option.setAlignment(Qt::AlignCenter); + painter.drawText(rect(), m_text, option); +} diff --git a/frame/item/tipswidget.h b/frame/item/tipswidget.h new file mode 100644 index 000000000..8509a2c5c --- /dev/null +++ b/frame/item/tipswidget.h @@ -0,0 +1,21 @@ +#ifndef TIPSWIDGET_H +#define TIPSWIDGET_H + +#include + +class TipsWidget : public QFrame +{ + Q_OBJECT +public: + explicit TipsWidget(QWidget *parent = nullptr); + + void setText(const QString &text); + +protected: + void paintEvent(QPaintEvent *event) override; + +private: + QString m_text; +}; + +#endif // TIPSWIDGET_H diff --git a/frame/panel/mainpanel.cpp b/frame/panel/mainpanel.cpp index 02d76e72f..3b23466b7 100644 --- a/frame/panel/mainpanel.cpp +++ b/frame/panel/mainpanel.cpp @@ -24,6 +24,7 @@ #include #include +#include DockItem *MainPanel::DraggingItem = nullptr; PlaceholderItem *MainPanel::RequestDockItem = nullptr; @@ -49,28 +50,14 @@ MainPanel::MainPanel(QWidget *parent) setAcceptDrops(true); setAccessibleName("dock-mainpanel"); setObjectName("MainPanel"); - setStyleSheet("QWidget #MainPanel {" -// "background-color:rgba(10, 10, 10, .6);" - "}" -// "QWidget #MainPanel[displayMode='1'] {" -// "border:none;" -// "}" - "QWidget #MainPanel[position='0'] {" - "padding:0 " xstr(PANEL_PADDING) "px;" - "border-top:none;" - "}" - "QWidget #MainPanel[position='1'] {" - "padding:" xstr(PANEL_PADDING) "px 0;" - "border-right:none;" - "}" - "QWidget #MainPanel[position='2'] {" - "padding:0 " xstr(PANEL_PADDING) "px;" - "border-bottom:none;" - "}" - "QWidget #MainPanel[position='3'] {" - "padding:" xstr(PANEL_PADDING) "px 0;" - "border-left:none;" - "}"); + + QFile qssFile(":/qss/frame.qss"); + + qssFile.open(QFile::ReadOnly); + if(qssFile.isOpen()) { + setStyleSheet(qssFile.readAll()); + qssFile.close(); + } connect(m_itemController, &DockItemController::itemInserted, this, &MainPanel::itemInserted, Qt::DirectConnection); connect(m_itemController, &DockItemController::itemRemoved, this, &MainPanel::itemRemoved, Qt::DirectConnection); diff --git a/frame/qss/frame.qss b/frame/qss/frame.qss new file mode 100644 index 000000000..2487f0db9 --- /dev/null +++ b/frame/qss/frame.qss @@ -0,0 +1,19 @@ +#MainPanel[position='0'] { + padding: 0 0px; + border-top: none; +} + +#MainPanel[position='1'] { + padding: 0px 0; + border-right: none; +} + +#MainPanel[position='2'] { + padding:0 0px; + border-bottom: none; +} + +#MainPanel[position='3'] { + padding: 0px 0; + border-left: none; +}