Skip to content

Commit

Permalink
完善布局处理,完善调试打印控制
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt1988 committed Dec 24, 2023
1 parent f48ea94 commit aa16e12
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 113 deletions.
9 changes: 7 additions & 2 deletions src/SARibbonBar/SARibbonCategory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ void SARibbonCategory::setRibbonPannelLayoutMode(SARibbonPannel::PannelLayoutMod

bool SARibbonCategory::event(QEvent* e)
{
#if SA_DEBUG_PRINT_EVENT
if (e->type() != QEvent::Paint) {
qDebug() << "SARibbonCategory event(" << e->type() << "),name=" << categoryName();
}
#endif
return QWidget::event(e);
}

Expand Down Expand Up @@ -468,10 +473,10 @@ QList< SARibbonPannel* > SARibbonCategory::pannelList() const

QSize SARibbonCategory::sizeHint() const
{
if (QLayout* lay = layout()) {
if (SARibbonCategoryLayout* lay = categoryLayout()) {
return lay->sizeHint();
}
return QSize(500, 200);
return QSize(1000, 100);
}

/**
Expand Down
120 changes: 78 additions & 42 deletions src/SARibbonBar/SARibbonCategoryLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "SARibbonCategoryLayout.h"
#include <QLayoutItem>
#include "SARibbonPannel.h"
#include "SARibbonToolButton.h"
#include "SARibbonElementManager.h"
#include "SARibbonSeparatorWidget.h"
#include <QApplication>
Expand Down Expand Up @@ -29,7 +28,8 @@ class SARibbonCategoryLayout::PrivateData
SARibbonCategoryScrollButton* mRightScrollBtn { nullptr }; ///< 在区域无法显示时显示的按钮
int mTotalWidth { 0 };
int mXBase { 0 };
QSize mSizeHint { 50, 50 };
QSize mSizeHint;
QSize mMinSizeHint;
QList< SARibbonCategoryLayoutItem* > mItemList;
SARibbonAlignment mCategoryAlignment { SARibbonAlignment::AlignLeft }; ///< 对齐方式
};
Expand All @@ -50,24 +50,49 @@ int SARibbonCategoryLayout::PrivateData::totalSizeHintWidth() const
{
int total = 0;
QMargins mag = q_ptr->contentsMargins();

#if SA_DEBUG_PRINT_SIZE_HINT
int debug_i__ = 0;
QString debug_totalSizeHintWidth__;
#endif
if (!mag.isNull()) {
total += (mag.left() + mag.right());
}
//先计算总长
for (SARibbonCategoryLayoutItem* item : qAsConst(mItemList)) {
if (item->isEmpty()) {
//如果是hide就直接跳过
//如果是hide就直接跳过
#if SA_DEBUG_PRINT_SIZE_HINT
++debug_i__;
debug_totalSizeHintWidth__ += QString(" [%1](%2)is empty skip\n")
.arg(debug_i__)
.arg(item->toPannelWidget()->pannelName());
#endif
continue;
}
//这里要使用widget()->sizeHint(),因为pannel的标题会影总体布局,此处需要修改
// TODO
QSize pannelSize = item->widget()->sizeHint();
QSize SeparatorSize(0, 0);
if (item->separatorWidget) {
SeparatorSize = item->separatorWidget->sizeHint();
}
total += pannelSize.width();
total += SeparatorSize.width();
#if SA_DEBUG_PRINT_SIZE_HINT
++debug_i__;
debug_totalSizeHintWidth__ += QString("|-[%1]pannelSize=(%2,%3),SeparatorSize=(%4,%5),name=(%6) \n")
.arg(debug_i__)
.arg(pannelSize.width())
.arg(pannelSize.height())
.arg(SeparatorSize.width())
.arg(SeparatorSize.height())
.arg(item->toPannelWidget()->pannelName());
#endif
}
#if SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "SARibbonCategoryLayout.totalSizeHintWidth=" << total;
qDebug().noquote() << debug_totalSizeHintWidth__;
#endif
return (total);
}

Expand All @@ -89,7 +114,7 @@ SARibbonCategoryLayout::SARibbonCategoryLayout(SARibbonCategory* parent)

SARibbonCategoryLayout::~SARibbonCategoryLayout()
{
while (QLayoutItem* item = takeAt(0)) {
while (auto item = takePannelItem(0)) {
delete item;
}
}
Expand Down Expand Up @@ -126,13 +151,14 @@ QLayoutItem* SARibbonCategoryLayout::itemAt(int index) const
*/
QLayoutItem* SARibbonCategoryLayout::takeAt(int index)
{
return (takePannelItem(index));
QLayoutItem* r = takePannelItem(index);
invalidate();
return r;
}

SARibbonCategoryLayoutItem* SARibbonCategoryLayout::takePannelItem(int index)
{
if ((index >= 0) && (index < d_ptr->mItemList.size())) {
invalidate();
SARibbonCategoryLayoutItem* item = d_ptr->mItemList.takeAt(index);
if (item->widget()) {
item->widget()->hide();
Expand Down Expand Up @@ -170,6 +196,7 @@ bool SARibbonCategoryLayout::takePannel(SARibbonPannel* pannel)
sp->deleteLater();
}
delete i;
invalidate();
return true;
}
return false;
Expand Down Expand Up @@ -208,12 +235,20 @@ void SARibbonCategoryLayout::insertPannel(int index, SARibbonPannel* pannel)

QSize SARibbonCategoryLayout::sizeHint() const
{
if (d_ptr->mSizeHint.isNull()) {
SARibbonCategoryLayout* that = const_cast< SARibbonCategoryLayout* >(this);
that->updateGeometryArr();
}
return (d_ptr->mSizeHint);
}

QSize SARibbonCategoryLayout::minimumSize() const
{
return (d_ptr->mSizeHint);
if (d_ptr->mMinSizeHint.isNull()) {
SARibbonCategoryLayout* that = const_cast< SARibbonCategoryLayout* >(this);
that->updateGeometryArr();
}
return (d_ptr->mMinSizeHint);
}

/**
Expand Down Expand Up @@ -251,7 +286,7 @@ QSize SARibbonCategoryLayout::categoryContentSize() const
*/
void SARibbonCategoryLayout::updateGeometryArr()
{
SARibbonCategory* category = qobject_cast< SARibbonCategory* >(parentWidget());
SARibbonCategory* category = ribbonCategory();
if (nullptr == category) {
return;
}
Expand All @@ -273,11 +308,14 @@ void SARibbonCategoryLayout::updateGeometryArr()

//如果total < categoryWidth,m_d->mXBase可以设置为0
//判断是否超过总长度
#if SARibbonCategoryLayout_DEBUG_PRINT
qDebug() << "\r\n\r\n==========================(" << category->categoryName() << ")======="
<< "\r\nSARibbonCategoryLayout::updateGeometryArr"
<< "\r\npannel name:" << category->windowTitle() << "\r\n height:" << height
<< "\r\n first total:" << total << "\r\n y:" << y << "\r\n expandWidth:" << expandWidth;
#if SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "SARibbonCategoryLayout::updateGeometryArr"
<< "\n|-category name=" << category->categoryName() //
<< "\n|-category height=" << height //
<< "\n|-totalSizeHintWidth=" << total //
<< "\n|-y=" << y //
<< "\n|-expandWidth:" << expandWidth //
<< "\n|-mag=" << mag;
#endif
if (total > categoryWidth) {
//超过总长度,需要显示滚动按钮
Expand Down Expand Up @@ -364,23 +402,12 @@ void SARibbonCategoryLayout::updateGeometryArr()
x += w;
total += w;
}
d_ptr->mTotalWidth = total;
QWidget* cp = category->parentWidget();
int parentHeight = (nullptr == cp) ? height : cp->height();
int parentWidth = (nullptr == cp) ? total : cp->width();
#if SARibbonCategoryLayout_DEBUG_PRINT
qDebug() << "\r\n mSizeHint:[ " << parentHeight << "," << parentWidth << "]";
for (int i = 0; i < d_ptr->mItemList.size(); ++i) {
qDebug() << "\r\n [ " << i << "] (pannel name=" << d_ptr->mItemList[ i ]->toPannelWidget()->pannelName()
<< "),sizeHint=" << d_ptr->mItemList[ i ]->toPannelWidget()->sizeHint()
<< "\r\n geo:" << d_ptr->mItemList[ i ]->mWillSetGeometry
<< ", Separator geo:" << d_ptr->mItemList[ i ]->mWillSetSeparatorGeometry;
}
#endif
d_ptr->mSizeHint = QSize(parentWidth, parentHeight);
d_ptr->mTotalWidth = total;
d_ptr->mSizeHint = QSize(d_ptr->mTotalWidth, height);
d_ptr->mMinSizeHint = QSize(categoryWidth, height);
#if SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "SARibbonCategory name=" << category->categoryName()
<< " SARibbonCategoryLayout updateGeometryArr,SizeHint=" << d_ptr->mSizeHint;
qDebug() << "SARibbonCategoryLayout updateGeometryArr,SizeHint=" << d_ptr->mSizeHint
<< ",Category name=" << category->categoryName();
#endif
}

Expand All @@ -392,18 +419,25 @@ void SARibbonCategoryLayout::doLayout()
if (d_ptr->mDirty) {
updateGeometryArr();
}
SARibbonCategory* category = qobject_cast< SARibbonCategory* >(parentWidget());
SARibbonCategory* category = ribbonCategory();
//两个滚动按钮的位置永远不变
d_ptr->mLeftScrollBtn->setGeometry(0, 0, 12, category->height());
d_ptr->mRightScrollBtn->setGeometry(category->width() - 12, 0, 12, category->height());
QList< QWidget* > showWidgets, hideWidgets;

#ifdef SA_DEBUG_PRINT_SIZE_HINT
int debug_i__(0);
qDebug() << "SARibbonCategoryLayout::doLayout(),name=" << category->categoryName();
#endif
for (SARibbonCategoryLayoutItem* item : qAsConst(d_ptr->mItemList)) {
if (item->isEmpty()) {
hideWidgets << item->widget();
if (item->separatorWidget) {
hideWidgets << item->separatorWidget;
}
#ifdef SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "|-[" << debug_i__ << "]pannelName(" << item->toPannelWidget()->pannelName() << ",will hide";
++debug_i__;
#endif
} else {
item->widget()->setFixedSize(item->mWillSetGeometry.size());
item->widget()->move(item->mWillSetGeometry.topLeft());
Expand All @@ -413,10 +447,11 @@ void SARibbonCategoryLayout::doLayout()
item->separatorWidget->setGeometry(item->mWillSetSeparatorGeometry);
showWidgets << item->separatorWidget;
}
#ifdef SA_RIBBON_DEBUG_HELP_DRAW
qDebug() << "SARibbonCategoryLayout::doLayout() =";
qDebug() << "\r\n pannel:" << item->widget()->windowTitle() << "\r\n pannel geo:" << item->mWillSetGeometry
<< "\r\n sep geo:" << item->mWillSetSeparatorGeometry;
#ifdef SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "|-[" << debug_i__ << "]pannelName(" << item->toPannelWidget()->pannelName()
<< "),willSetGeometry:" << item->mWillSetGeometry
<< ",WillSetSeparatorGeometry:" << item->mWillSetSeparatorGeometry;
++debug_i__;
#endif
}
}
Expand All @@ -431,14 +466,15 @@ void SARibbonCategoryLayout::doLayout()
}
// 不在上面那里进行show和hide因为这会触发SARibbonPannelLayout的重绘,导致循环绘制,非常影响效率
for (QWidget* w : qAsConst(showWidgets)) {
w->show();
if (!w->isVisible()) {
w->show();
}
}
for (QWidget* w : qAsConst(hideWidgets)) {
w->hide();
if (w->isVisible()) {
w->hide();
}
}
#if SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "SARibbonCategory name=" << category->categoryName() << " SARibbonCategoryLayout doLayout";
#endif
}

/**
Expand Down Expand Up @@ -655,7 +691,7 @@ void SARibbonCategoryLayout::setGeometry(const QRect& rect)
return;
}
#if SA_DEBUG_PRINT_SIZE_HINT
qDebug() << "SARibbonCategoryLayout " << ribbonCategory()->categoryName() << " setGeometry=" << rect;
qDebug() << "===========SARibbonCategoryLayout.setGeometry(" << rect << "(" << ribbonCategory()->categoryName() << ")=======";
#endif
QLayout::setGeometry(rect);
d_ptr->mDirty = false;
Expand Down
9 changes: 8 additions & 1 deletion src/SARibbonBar/SARibbonGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
#define SA_RIBBON_DECLARE_PUBLIC(classname) \
friend class classname; \
classname* q_ptr { nullptr }; \
PrivateData(const PrivateData&) = delete; \
PrivateData(const PrivateData&) = delete; \
PrivateData& operator=(const PrivateData&) = delete;
#endif

Expand Down Expand Up @@ -187,5 +187,12 @@ enum class SARibbonAlignment
*/
#define SA_DEBUG_PRINT_SIZE_HINT 1
#endif
#ifndef SA_DEBUG_PRINT_EVENT
/**
@def 定义此宏,将打印事件
仅用于调试
*/
#define SA_DEBUG_PRINT_EVENT 0
#endif
#endif // SARIBBONGLOBAL_H
35 changes: 22 additions & 13 deletions src/SARibbonBar/SARibbonPannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,24 @@ void SARibbonPannel::paintEvent(QPaintEvent* e)

QSize SARibbonPannel::sizeHint() const
{
QSize laySize = layout()->sizeHint();
int maxWidth = laySize.width() + 2;

if (ThreeRowMode == pannelLayoutMode()) {
// 三行模式
QFontMetrics fm = fontMetrics();
QSize titleSize = fm.size(Qt::TextShowMnemonic, pannelName());
if (d_ptr->m_optionActionButton) {
// optionActionButton的宽度需要预留
titleSize.setWidth(titleSize.width() + d_ptr->m_optionActionButton->width() + 4);
int shWidth = 500;
int shHeight = 80;
if (QLayout* lay = layout()) {
QSize laySize = layout()->sizeHint();
shWidth = laySize.width();
shHeight = laySize.height();
if (ThreeRowMode == pannelLayoutMode()) {
// 三行模式
QFontMetrics fm = fontMetrics();
QSize titleSize = fm.size(Qt::TextShowMnemonic, pannelName());
if (d_ptr->m_optionActionButton) {
// optionActionButton的宽度需要预留
titleSize.setWidth(titleSize.width() + d_ptr->m_optionActionButton->width() + 4);
}
shWidth = qMax(shWidth, titleSize.width());
}
maxWidth = qMax(maxWidth, titleSize.width());
}
return (QSize(maxWidth, laySize.height()));
return QSize(shWidth, shHeight);
}

QSize SARibbonPannel::minimumSizeHint() const
Expand Down Expand Up @@ -729,6 +733,11 @@ void SARibbonPannel::resetLargeToolButtonStyle()

bool SARibbonPannel::event(QEvent* e)
{
#if SA_DEBUG_PRINT_EVENT
if (e->type() != QEvent::Paint) {
qDebug() << "SARibbonPannel event(" << e->type() << "),name=" << pannelName();
}
#endif
// if (SARibbonPannelLayout* lay = pannelLayout()) {
// if (lay->isDirty() && e->type() == QEvent::LayoutRequest) {
// if (QWidget* parw = parentWidget()) {
Expand Down Expand Up @@ -809,6 +818,7 @@ void SARibbonPannel::actionEvent(QActionEvent* e)
case QEvent::ActionChanged: {
// 让布局重新绘制
layout()->invalidate();

// updateGeometry();
// 由于pannel的尺寸发生变化,需要让category也调整
if (QWidget* parw = parentWidget()) {
Expand All @@ -826,7 +836,6 @@ void SARibbonPannel::actionEvent(QActionEvent* e)
// //!
// //! 调用parw->updateGeometry();也没有效果,目前看使用resizeevent是最有效果的
// //!
// // parw->updateGeometry();
// QResizeEvent* ersize = new QResizeEvent(parw->size(), QSize());
// QApplication::postEvent(parw, ersize);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SARibbonBar/SARibbonPannelItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ SARibbonPannelItem::SARibbonPannelItem(QWidget* widget)

bool SARibbonPannelItem::isEmpty() const
{
return (action == 0 || !action->isVisible());
return (action == nullptr || !action->isVisible());
}
Loading

0 comments on commit aa16e12

Please sign in to comment.