feat(plugin):plugin layout

This commit is contained in:
shaojun 2019-09-06 14:44:07 +08:00
parent 0277d1404f
commit c112e33bef
12 changed files with 169 additions and 69 deletions

View File

@ -65,27 +65,38 @@ MainPanelControl::~MainPanelControl()
void MainPanelControl::init()
{
m_mainPanelLayout->setMargin(0);
m_mainPanelLayout->setContentsMargins(0, 0, 0, 0);
m_fixedAreaLayout->setMargin(0);
m_fixedAreaLayout->setContentsMargins(0, 0, 0, 0);
m_pluginLayout->setMargin(0);
m_pluginLayout->setContentsMargins(10, 10, 10, 10);
m_trayAreaLayout->setMargin(0);
m_trayAreaLayout->setContentsMargins(10, 10, 10, 10);
m_appAreaSonLayout->setMargin(0);
m_appAreaSonLayout->setContentsMargins(0, 0, 0, 0);
// 主窗口
m_mainPanelLayout->addWidget(m_fixedAreaWidget);
m_fixedAreaWidget->setLayout(m_fixedAreaLayout);
m_mainPanelLayout->addWidget(m_appAreaWidget);
m_mainPanelLayout->addWidget(m_trayAreaWidget);
m_trayAreaWidget->setLayout(m_trayAreaLayout);
m_mainPanelLayout->addWidget(m_pluginAreaWidget);
m_pluginAreaWidget->setLayout(m_pluginLayout);
m_appAreaSonWidget->setLayout(m_appAreaSonLayout);
m_mainPanelLayout->setMargin(0);
m_mainPanelLayout->setContentsMargins(0, 0, 0, 0);
m_mainPanelLayout->setSpacing(0);
// 固定区域
m_fixedAreaWidget->setLayout(m_fixedAreaLayout);
m_fixedAreaLayout->setMargin(0);
m_fixedAreaLayout->setContentsMargins(0, 0, 0, 0);
m_fixedAreaLayout->setSpacing(0);
// 应用程序
m_appAreaSonWidget->setLayout(m_appAreaSonLayout);
m_appAreaSonLayout->setMargin(0);
m_appAreaSonLayout->setContentsMargins(0, 0, 0, 0);
m_appAreaSonLayout->setSpacing(0);
// 托盘
m_trayAreaWidget->setLayout(m_trayAreaLayout);
m_trayAreaLayout->setMargin(0);
m_trayAreaLayout->setContentsMargins(10, 10, 10, 10);
m_trayAreaLayout->setSpacing(0);
// 插件
m_pluginAreaWidget->setLayout(m_pluginLayout);
m_pluginLayout->setMargin(0);
m_pluginLayout->setContentsMargins(0, 10, 10, 10);
m_pluginLayout->setSpacing(10);
}
@ -111,6 +122,7 @@ void MainPanelControl::updateMainPanelLayout()
m_pluginLayout->setDirection(QBoxLayout::LeftToRight);
m_trayAreaLayout->setDirection(QBoxLayout::LeftToRight);
m_appAreaSonLayout->setDirection(QBoxLayout::LeftToRight);
m_pluginLayout->setContentsMargins(0, 10, 10, 10);
break;
case Position::Right:
case Position::Left:
@ -123,6 +135,7 @@ void MainPanelControl::updateMainPanelLayout()
m_pluginLayout->setDirection(QBoxLayout::TopToBottom);
m_trayAreaLayout->setDirection(QBoxLayout::TopToBottom);
m_appAreaSonLayout->setDirection(QBoxLayout::TopToBottom);
m_pluginLayout->setContentsMargins(10, 0, 10, 10);
break;
}
}
@ -157,6 +170,8 @@ void MainPanelControl::addTrayAreaItem(int index, QWidget *wdg)
void MainPanelControl::addPluginAreaItem(int index, QWidget *wdg)
{
m_pluginLayout->insertWidget(index, wdg);
QTimer::singleShot(50, this, [ = ] {m_pluginAreaWidget->adjustSize();});
}
void MainPanelControl::removeFixedAreaItem(QWidget *wdg)
@ -647,3 +662,8 @@ void MainPanelControl::onPositionChanged()
{
updateMainPanelLayout();
}
void MainPanelControl::itemUpdated(DockItem *item)
{
item->parentWidget()->adjustSize();
}

View File

@ -90,6 +90,7 @@ public slots:
void removeItem(DockItem *item);
void onDisplayModeChanged();
void onPositionChanged();
void itemUpdated(DockItem *item);
private:
QBoxLayout *m_mainPanelLayout;

View File

@ -40,7 +40,7 @@
#define SNI_WATCHER_SERVICE "org.kde.StatusNotifierWatcher"
#define SNI_WATCHER_PATH "/StatusNotifierWatcher"
#define MAINWINDOW_MAX_SIZE (100)
#define MAINWINDOW_MAX_SIZE DOCK_MAX_SIZE
#define MAINWINDOW_MIN_SIZE (40)
#define DRAG_AREA_SIZE (5)
@ -468,6 +468,7 @@ void MainWindow::initConnections()
connect(DockItemManager::instance(), &DockItemManager::itemInserted, m_mainPanel, &MainPanelControl::insertItem, Qt::DirectConnection);
connect(DockItemManager::instance(), &DockItemManager::itemRemoved, m_mainPanel, &MainPanelControl::removeItem, Qt::DirectConnection);
connect(DockItemManager::instance(), &DockItemManager::itemUpdated, m_mainPanel, &MainPanelControl::itemUpdated, Qt::DirectConnection);
connect(DockItemManager::instance(), &DockItemManager::requestRefershWindowVisible, this, &MainWindow::updatePanelVisible, Qt::QueuedConnection);
connect(DockItemManager::instance(), &DockItemManager::requestWindowAutoHide, m_settings, &DockSettings::setAutoHide);
connect(m_mainPanel, &MainPanelControl::itemMoved, DockItemManager::instance(), &DockItemManager::itemMoved, Qt::DirectConnection);

View File

@ -41,6 +41,9 @@ namespace Dock {
// 插件最小尺寸,图标采用深色
#define PLUGIN_MIN_ICON_NAME "-dark"
// dock最大尺寸
#define DOCK_MAX_SIZE 100
///
/// \brief The DisplayMode enum
/// spec dock display mode

View File

@ -46,6 +46,8 @@ DatetimeWidget::DatetimeWidget(QWidget *parent)
int dateHeight = fm_date.boundingRect("8888/88/88").height();
m_timeOffset = (timeHeight - dateHeight) / 2;
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
}
void DatetimeWidget::set24HourFormat(const bool value)
@ -65,16 +67,35 @@ void DatetimeWidget::set24HourFormat(const bool value)
QSize DatetimeWidget::sizeHint() const
{
QFontMetrics fm(qApp->font());
if (m_24HourFormat)
return fm.boundingRect("8888/88/88").size() + QSize(30, 10);
else
return fm.boundingRect("88:88 A.A.").size() + QSize(30, 20);
// 最大尺寸
QFontMetrics fm(TIME_FONT);
return fm.boundingRect("88:88 A.A.").size() + QSize(20, 20);
}
void DatetimeWidget::resizeEvent(QResizeEvent *e)
{
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
QFontMetrics fm(TIME_FONT);
QString format;
if (m_24HourFormat)
format = "hh:mm";
else
format = "hh:mm AP";
QSize timeSize = fm.boundingRect(QDateTime::currentDateTime().toString(format)).size();
QSize dateSize = QFontMetrics(DATE_FONT).boundingRect("0000/00/00").size();
if (timeSize.width() < dateSize.width())
timeSize.setWidth(dateSize.width());
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(timeSize.width());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumWidth(QWIDGETSIZE_MAX);
setMaximumHeight(timeSize.height() * 2);
}
QWidget::resizeEvent(e);
}
@ -82,9 +103,6 @@ void DatetimeWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
const auto ratio = devicePixelRatioF();
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
const QDateTime current = QDateTime::currentDateTime();
QPainter painter(this);

View File

@ -73,7 +73,7 @@ void WiredItem::paintEvent(QPaintEvent *e)
QPainter painter(this);
const auto ratio = devicePixelRatioF();
const QRectF &rf = QRectF(pos(), QSize(std::min(width(), height()), std::min(width(), height())));
const QRectF &rf = rect();
const QRectF &rfp = QRectF(m_icon.rect());
const int x = rf.center().x() - rfp.center().x() / ratio;
const int y = rf.center().y() - rfp.center().y() / ratio;
@ -82,6 +82,16 @@ void WiredItem::paintEvent(QPaintEvent *e)
void WiredItem::resizeEvent(QResizeEvent *e)
{
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}
DeviceItem::resizeEvent(e);
m_delayTimer->start();
@ -133,7 +143,7 @@ void WiredItem::reloadIcon()
const quint64 index = QDateTime::currentMSecsSinceEpoch() / 200;
const int num = (index % 5) + 1;
m_icon = QIcon(QString(":/wired/resources/wired/network-wired-symbolic-connecting%1.svg").arg(num))
.pixmap(iconSize * ratio, iconSize * ratio);
.pixmap(iconSize * ratio, iconSize * ratio);
m_icon.setDevicePixelRatio(ratio);
update();
return;

View File

@ -49,9 +49,9 @@ WirelessItem::WirelessItem(WirelessDevice *device)
m_wirelessTips->setText(tr("No Network"));
connect(m_refreshTimer, &QTimer::timeout, this, &WirelessItem::onRefreshTimeout);
connect(m_device, static_cast<void (NetworkDevice::*) (const QString &statStr) const>(&NetworkDevice::statusChanged), this, &WirelessItem::deviceStateChanged);
connect(static_cast<WirelessDevice *>(m_device.data()), &WirelessDevice::activeApInfoChanged, m_refreshTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));
connect(static_cast<WirelessDevice *>(m_device.data()), &WirelessDevice::activeWirelessConnectionInfoChanged, m_refreshTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));
connect(m_device, static_cast<void (NetworkDevice::*)(const QString &statStr) const>(&NetworkDevice::statusChanged), this, &WirelessItem::deviceStateChanged);
connect(static_cast<WirelessDevice *>(m_device.data()), &WirelessDevice::activeApInfoChanged, m_refreshTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
connect(static_cast<WirelessDevice *>(m_device.data()), &WirelessDevice::activeWirelessConnectionInfoChanged, m_refreshTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
//QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
QTimer::singleShot(0, this, &WirelessItem::refreshTips);
@ -104,8 +104,7 @@ void WirelessItem::paintEvent(QPaintEvent *e)
pixmap.setDevicePixelRatio(ratio);
QPainter painter(this);
if (displayMode == Dock::Fashion)
{
if (displayMode == Dock::Fashion) {
QPixmap pixmap = backgroundPix(iconSize * ratio);
pixmap.setDevicePixelRatio(ratio);
painter.drawPixmap(rect().center() - pixmap.rect().center() / ratio, pixmap);
@ -122,6 +121,16 @@ void WirelessItem::resizeEvent(QResizeEvent *e)
{
DeviceItem::resizeEvent(e);
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}
m_icons.clear();
}
@ -188,8 +197,8 @@ const QPixmap WirelessItem::iconPix(const Dock::DisplayMode displayMode, const i
}
QString key = QString("wireless-%1%2")
.arg(type)
.arg(displayMode == Dock::Fashion ? "" : "-symbolic");
.arg(type)
.arg(displayMode == Dock::Fashion ? "" : "-symbolic");
if (state == NetworkDevice::DeviceStatus::Activated && !NetworkPlugin::isConnectivity()) {
key = "network-wireless-offline-symbolic";
@ -238,7 +247,7 @@ void WirelessItem::init()
connect(m_APList, &WirelessList::requestWirelessScan, this, &WirelessItem::requestWirelessScan);
connect(m_APList, &WirelessList::requestSetAppletVisible, this, &WirelessItem::requestSetAppletVisible);
QTimer::singleShot(0, this, [=]() {
QTimer::singleShot(0, this, [ = ]() {
m_refreshTimer->start();
});
}

View File

@ -38,7 +38,6 @@ OnboardItem::OnboardItem(QWidget *parent)
{
setMouseTracking(true);
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
setMaximumSize(PLUGIN_BACKGROUND_MAX_SIZE, PLUGIN_BACKGROUND_MAX_SIZE);
}
QSize OnboardItem::sizeHint() const
@ -61,14 +60,6 @@ void OnboardItem::paintEvent(QPaintEvent *e)
QPainter painter(this);
if (std::min(width(), height()) > PLUGIN_BACKGROUND_MIN_SIZE) {
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setOpacity(0.5);
DStyleHelper dstyle(style());
const int radius = dstyle.pixelMetric(DStyle::PM_FrameRadius);
QPainterPath path;
path.addRoundedRect(rect(), radius, radius);
QColor color = QColor::fromRgb(40, 40, 40);;
@ -80,6 +71,19 @@ void OnboardItem::paintEvent(QPaintEvent *e)
color = QColor::fromRgb(20, 20, 20);
}
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setOpacity(0.5);
DStyleHelper dstyle(style());
const int radius = dstyle.pixelMetric(DStyle::PM_FrameRadius);
QPainterPath path;
int minSize = std::min(width(), height());
QRect rc(0, 0, minSize, minSize);
rc.moveTo(rect().center() - rc.center());
path.addRoundedRect(rc, radius, radius);
painter.fillPath(path, color);
} else {
iconName.append(PLUGIN_MIN_ICON_NAME);
@ -142,10 +146,10 @@ void OnboardItem::resizeEvent(QResizeEvent *event)
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(PLUGIN_BACKGROUND_MAX_SIZE);
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(PLUGIN_BACKGROUND_MAX_SIZE);
setMaximumWidth(QWIDGETSIZE_MAX);
}
QWidget::resizeEvent(event);

View File

@ -37,13 +37,12 @@ PluginWidget::PluginWidget(QWidget *parent)
, m_pressed(false)
{
setMouseTracking(true);
setMinimumSize(PLUGIN_ICON_MIN_SIZE, PLUGIN_ICON_MIN_SIZE);
setMaximumSize(PLUGIN_BACKGROUND_MAX_SIZE, PLUGIN_BACKGROUND_MAX_SIZE);
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
}
QSize PluginWidget::sizeHint() const
{
return QSize(PLUGIN_ICON_MIN_SIZE, PLUGIN_ICON_MIN_SIZE);
return QSize(PLUGIN_BACKGROUND_MAX_SIZE, PLUGIN_BACKGROUND_MAX_SIZE);
}
void PluginWidget::paintEvent(QPaintEvent *e)
@ -56,17 +55,18 @@ void PluginWidget::paintEvent(QPaintEvent *e)
QPainter painter(this);
QColor color = QColor::fromRgb(40, 40, 40);;
if (m_hover) {
color = QColor::fromRgb(60, 60, 60);
}
if (m_pressed) {
color = QColor::fromRgb(20, 20, 20);
}
if (rect().height() > PLUGIN_BACKGROUND_MIN_SIZE) {
QColor color = QColor::fromRgb(40, 40, 40);;
if (m_hover) {
color = QColor::fromRgb(60, 60, 60);
}
if (m_pressed) {
color = QColor::fromRgb(20, 20, 20);
}
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setOpacity(0.5);
@ -74,7 +74,12 @@ void PluginWidget::paintEvent(QPaintEvent *e)
const int radius = dstyle.pixelMetric(DStyle::PM_FrameRadius);
QPainterPath path;
path.addRoundedRect(rect(), radius, radius);
int minSize = std::min(width(), height());
QRect rc(0, 0, minSize, minSize);
rc.moveTo(rect().center() - rc.center());
path.addRoundedRect(rc, radius, radius);
painter.fillPath(path, color);
} else {
// 最小尺寸时,不画背景,采用深色图标
@ -137,11 +142,11 @@ void PluginWidget::resizeEvent(QResizeEvent *event)
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMinimumWidth(height());
setMinimumHeight(PLUGIN_ICON_MIN_SIZE);
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMinimumWidth(PLUGIN_ICON_MIN_SIZE);
setMinimumHeight(width());
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}
QWidget::resizeEvent(event);

View File

@ -47,6 +47,8 @@ TrashWidget::TrashWidget(QWidget *parent)
updateIcon();
setAcceptDrops(true);
setMinimumSize(PLUGIN_ICON_MIN_SIZE, PLUGIN_ICON_MIN_SIZE);
}
QWidget *TrashWidget::popupApplet()
@ -56,7 +58,7 @@ QWidget *TrashWidget::popupApplet()
QSize TrashWidget::sizeHint() const
{
return QSize(26, 26);
return QSize(DOCK_MAX_SIZE, DOCK_MAX_SIZE);
}
const QString TrashWidget::contextMenu() const
@ -172,6 +174,16 @@ void TrashWidget::paintEvent(QPaintEvent *e)
void TrashWidget::resizeEvent(QResizeEvent *e)
{
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}
QWidget::resizeEvent(e);
updateIcon();
@ -188,7 +200,7 @@ void TrashWidget::updateIcon()
if (displayMode == Dock::Efficient)
iconString.append("-symbolic");
const int size = displayMode == Dock::Fashion ? std::min(width(), height()) * 0.8 : 16;
const int size = std::min(width(), height()) ;
QIcon icon = QIcon::fromTheme(iconString);
m_icon = icon.pixmap(size * devicePixelRatioF(), size * devicePixelRatioF());
m_icon.setDevicePixelRatio(devicePixelRatioF());

View File

@ -19,6 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "constants.h"
#include "abstracttraywidget.h"
#include <xcb/xproto.h>
@ -26,8 +27,8 @@
#include <QDebug>
AbstractTrayWidget::AbstractTrayWidget(QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f),
m_handleMouseReleaseTimer(new QTimer(this))
: QWidget(parent, f)
, m_handleMouseReleaseTimer(new QTimer(this))
{
m_handleMouseReleaseTimer->setSingleShot(true);
m_handleMouseReleaseTimer->setInterval(100);
@ -70,8 +71,8 @@ void AbstractTrayWidget::mouseReleaseEvent(QMouseEvent *e)
}
void AbstractTrayWidget::handleMouseRelease() {
void AbstractTrayWidget::handleMouseRelease()
{
Q_ASSERT(sender() == m_handleMouseReleaseTimer);
// do not dealwith all mouse event of SystemTray, class SystemTrayItem will handle it
@ -116,3 +117,18 @@ const QRect AbstractTrayWidget::perfectIconRect() const
return iconRect;
}
void AbstractTrayWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
// 保持横纵比
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}
}

View File

@ -58,6 +58,7 @@ protected:
void handleMouseRelease();
const QRect perfectIconRect() const;
void resizeEvent(QResizeEvent *event) override;
private:
QTimer *m_handleMouseReleaseTimer;