Skip to content

Commit

Permalink
图片添加高分屏比例缩放
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt1988 committed Dec 14, 2023
1 parent 1c45a01 commit 1b32431
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 151 deletions.
2 changes: 0 additions & 2 deletions src/SARibbonBar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ SET(SARIBBON_HEADER_FILES
SARibbonComboBox.h
SARibbonElementFactory.h
SARibbonElementManager.h
SARibbonDrawHelper.h
SARibbonLineEdit.h
SARibbonCheckBox.h
SARibbonButtonGroupWidget.h
Expand Down Expand Up @@ -261,7 +260,6 @@ SET(SARIBBON_SOURCE_FILES
SARibbonComboBox.cpp
SARibbonElementFactory.cpp
SARibbonElementManager.cpp
SARibbonDrawHelper.cpp
SARibbonLineEdit.cpp
SARibbonCheckBox.cpp
SARibbonButtonGroupWidget.cpp
Expand Down
2 changes: 0 additions & 2 deletions src/SARibbonBar/SARibbonBar.pri
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ SOURCES += \
$$PWD/SARibbonGalleryItem.cpp \
$$PWD/SARibbonComboBox.cpp \
$$PWD/SARibbonElementManager.cpp \
$$PWD/SARibbonDrawHelper.cpp \
$$PWD/SARibbonLineEdit.cpp \
$$PWD/SARibbonCheckBox.cpp \
$$PWD/SARibbonButtonGroupWidget.cpp \
Expand Down Expand Up @@ -108,7 +107,6 @@ HEADERS += \
$$PWD/SARibbonGalleryItem.h \
$$PWD/SARibbonComboBox.h \
$$PWD/SARibbonElementManager.h \
$$PWD/SARibbonDrawHelper.h \
$$PWD/SARibbonLineEdit.h \
$$PWD/SARibbonCheckBox.h \
$$PWD/SARibbonButtonGroupWidget.h \
Expand Down
1 change: 0 additions & 1 deletion src/SARibbonBar/SARibbonButtonGroupWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ QAction* SARibbonButtonGroupWidget::addAction(QAction* a)
QAction* SARibbonButtonGroupWidget::addAction(const QString& text, const QIcon& icon, QToolButton::ToolButtonPopupMode popMode)
{
QAction* a = new QAction(icon, text, this);

addAction(a);
ButtonTyle* btn = qobject_cast< ButtonTyle* >(d_ptr->mItems.back().widget);
btn->setPopupMode(popMode);
Expand Down
11 changes: 9 additions & 2 deletions src/SARibbonBar/SARibbonColorToolButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ QPixmap SARibbonColorToolButton::PrivateData::createIconPixmap(const QStyleOptio
mode = QIcon::Normal;
}
QSize realIconSize = iconsize - QSize(0, c_ribbonbutton_color_height + 1);
realIconSize *= q_ptr->devicePixelRatioF();
QPixmap pixmap = opt.icon.pixmap(q_ptr->window()->windowHandle(), realIconSize, mode, state);
pixmap.setDevicePixelRatio(q_ptr->devicePixelRatioF());
QPixmap res(pixmap.size() + QSize(4, c_ribbonbutton_color_height + 4)); //宽度上,颜色块多出2px
res.setDevicePixelRatio(q_ptr->devicePixelRatioF());
res.fill(Qt::transparent);
QPainter painter(&res);
int xpixmap = (res.width() - pixmap.width()) / 2;
int ypixmap = (res.height() - c_ribbonbutton_color_height - 2 - pixmap.height()) / 2; //这里要减去2而不是1,这样奇数偶数都不会影响
QRect rpixmap = QRect(xpixmap, ypixmap, pixmap.width(), pixmap.height());
xpixmap /= q_ptr->devicePixelRatioF();
ypixmap /= q_ptr->devicePixelRatioF();
int w = pixmap.width()/q_ptr->devicePixelRatioF();
int h = pixmap.height()/q_ptr->devicePixelRatioF();
QRect rpixmap = QRect(xpixmap, ypixmap, w, h);
painter.drawPixmap(rpixmap, pixmap);
QRect colorRect = rpixmap.adjusted(0, pixmap.height() + 1, 0, c_ribbonbutton_color_height + 1);
QRect colorRect = rpixmap.adjusted(0, h + 1, 0, c_ribbonbutton_color_height + 1);
if (mColor.isValid()) {
painter.fillRect(colorRect, mColor);
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/SARibbonBar/SARibbonCtrlContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <QStylePainter>
#include <QDebug>
#include <QLabel>

#include <QApplication>
#include <QScreen>
/**
* @brief The SARibbonCtrlContainerPrivate class
*/
Expand Down Expand Up @@ -117,7 +118,8 @@ bool SARibbonCtrlContainer::hasContainerWidget() const
void SARibbonCtrlContainer::setIcon(const QIcon& i)
{
d_ptr->icon = i;
QPixmap pixmap = i.pixmap(d_ptr->iconSize);
QPixmap pixmap = i.pixmap(d_ptr->iconSize * devicePixelRatioF());
pixmap.setDevicePixelRatio(devicePixelRatioF());
d_ptr->labelPixmap->setPixmap(pixmap);
}

Expand Down
101 changes: 0 additions & 101 deletions src/SARibbonBar/SARibbonDrawHelper.cpp

This file was deleted.

38 changes: 0 additions & 38 deletions src/SARibbonBar/SARibbonDrawHelper.h

This file was deleted.

9 changes: 9 additions & 0 deletions src/SARibbonBar/SARibbonGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,13 @@ enum class SARibbonAlignment
*/
// #define SARIBBON_USE_3RDPARTY_FRAMELESSHELPER 0

#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
#ifndef SA_FONTMETRICS_WIDTH
#define SA_FONTMETRICS_WIDTH(fm, str) fm.horizontalAdvance(str)
#endif
#else
#ifndef SA_FONTMETRICS_WIDTH
#define SA_FONTMETRICS_WIDTH(fm, str) fm.width(str)
#endif
#endif
#endif // SARIBBONGLOBAL_H
1 change: 0 additions & 1 deletion src/SARibbonBar/SARibbonMainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "SARibbonMainWindow.h"
#include "SARibbonBar.h"
#include "SARibbonDrawHelper.h"
#include "SARibbonElementManager.h"
#include "SARibbonTabBar.h"
#include <QApplication>
Expand Down
5 changes: 5 additions & 0 deletions src/SARibbonBar/SARibbonStyleOption.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "SARibbonStyleOption.h"
#include <QApplication>
#include <QDebug>
#include <QScreen>
SARibbonStyleOption::SARibbonStyleOption()
{
init();
Expand Down Expand Up @@ -73,7 +74,10 @@ int SARibbonStyleOption::calcMainbarHeight(SARibbonBar::RibbonStyle s) const
void SARibbonStyleOption::init()
{
QFontMetrics fm = QApplication::fontMetrics();
qreal dr = QApplication::primaryScreen()->devicePixelRatio();

int lineSpacing = fm.lineSpacing();
lineSpacing *= dr;

m_titleBarHeight = lineSpacing * 1.8;
m_tabBarHeight = lineSpacing * 1.5;
Expand Down Expand Up @@ -102,6 +106,7 @@ QDebug operator<<(QDebug debug, const SARibbonStyleOption& c)
QDebugStateSaver saver(debug);
Q_UNUSED(saver);
debug.nospace() << "fontMetrics.lineSpacing=" << QApplication::fontMetrics().lineSpacing()
<< "fontMetrics.height=" << QApplication::fontMetrics().height()
<< ",SARibbonStyleOption(titleBarHeight=" << c.titleBarHeight() << ",tabBarHeight=" << c.tabBarHeight()
<< "\n,ribbonBarHeight(OfficeStyle)=" << c.ribbonBarHeight(SARibbonBar::RibbonStyleLooseThreeRow)
<< "\n,ribbonBarHeight(OfficeStyleTwoRow)=" << c.ribbonBarHeight(SARibbonBar::RibbonStyleLooseTwoRow)
Expand Down
11 changes: 9 additions & 2 deletions src/SARibbonBar/SARibbonToolButton.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "SARibbonToolButton.h"
#include "SARibbonDrawHelper.h"
#include "SARibbonElementManager.h"
#include <QAction>
#include <QApplication>
Expand All @@ -11,6 +10,8 @@
#include <QStyleOptionToolButton>
#include <QStylePainter>
#include <QTextOption>
#include <QApplication>
#include <QScreen>
/**
* @def 定义文字换行时2行文本的矩形高度系数,此系数决定文字区域的高度
*
Expand Down Expand Up @@ -621,7 +622,13 @@ QPixmap SARibbonToolButton::PrivateData::createIconPixmap(const QStyleOptionTool
} else {
mode = QIcon::Normal;
}
return (opt.icon.pixmap(iconsize - QSize(2, 2), mode, state));
//添加高分屏支持
qreal pr = QApplication::primaryScreen()->devicePixelRatio();
QSize pxiampSize = iconsize - QSize(2, 2);
pxiampSize *= pr;
QPixmap px = opt.icon.pixmap(pxiampSize, mode, state);
px.setDevicePixelRatio(pr);
return px;
}

int SARibbonToolButton::PrivateData::getTextAlignment() const
Expand Down

0 comments on commit 1b32431

Please sign in to comment.