Skip to content

Commit

Permalink
feat(macox 12.6): Compatible with mac platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
XMuli committed Oct 20, 2022
1 parent db25969 commit 8fb2739
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
Binary file modified .DS_Store
Binary file not shown.
39 changes: 18 additions & 21 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int main(int argc, char *argv[])

#if(QT_VERSION > QT_VERSION_CHECK(5,6,0))
// QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // 2K、4K 2@ 倍;获取的分辨率 4K 下实际为 /2 后。 此行需在 QApplication a(argc,argv);前面
// QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); // 控制图片缩放质量,svg 的图片不会模糊在 4K 上。 此行无需在 QApplication a(argc,argv);前面
// QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); // 控制图片缩放质量,svg 的图片不会模糊在 4K 上。 此行无需在 QApplication a(argc,argv);前面
qApp->setAttribute(Qt::AA_UseHighDpiPixmaps); // 上面一行没有生效,但是此行可以生效
#endif

Expand Down Expand Up @@ -132,29 +132,26 @@ int main(int argc, char *argv[])
#else
#endif

auto t = new TestBTStyle();
t->setWindowTitle(QString("MacOS 12.6 + Qt 5.15.2 + Style: %1").arg(value));
t->show();

auto t100 = new TestBTStyle();
t100->show();
// auto t = new TestBTStyle();
// t->setWindowTitle(QString("MacOS 12.6 + Qt 5.15.2 + Style: %1").arg(value));
// t->show();

// WinSetting* set = new WinSetting();
// set->show();
auto t0 = new QWidget();
t0->resize(800, 400);
t0->show();

SelectBar* t1 = new SelectBar(Qt::Horizontal, t0);
t1->setWindowFlags(Qt::FramelessWindowHint);
// t1 ->setBlurBackground(QPixmap("D:/projects/PicShot/src/p1.jpg"), 4);
t1->move(200, 100);
t1->show();

ParameterBar* t2 = new ParameterBar(Qt::Horizontal, t0);
// t2->setBlurBackground(QPixmap("D:/projects/PicShot/src/p2.jpg"), 5);
t2->move(200, 200);
t2->show();
// auto t0 = new QWidget();
// t0->resize(800, 400);
// t0->show();

// SelectBar* t1 = new SelectBar(Qt::Horizontal, t0);
// t1->setWindowFlags(Qt::FramelessWindowHint);
//// t1 ->setBlurBackground(QPixmap("D:/projects/PicShot/src/p1.jpg"), 4);
// t1->move(200, 100);
// t1->show();

// ParameterBar* t2 = new ParameterBar(Qt::Horizontal, t0);
//// t2->setBlurBackground(QPixmap("D:/projects/PicShot/src/p2.jpg"), 5);
// t2->move(200, 200);
// t2->show();

// ColorParaBar* t3 = new ColorParaBar(ColorParaBarMode::CPB_ParaBar, Qt::Horizontal);
//// t3->resize(400, 300);
Expand Down
66 changes: 41 additions & 25 deletions src/screen/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ScreenShot::ScreenShot(QWidget *parent)
, m_scal(XHelper::instance().getScale())
, m_scrns(qGuiApp->screens())
, m_priScrn(qGuiApp->primaryScreen())
, m_curScrn(nullptr)
, m_virGeom(m_priScrn->virtualGeometry())
, m_currPixmap(nullptr)
, m_rtCalcu(this)
Expand Down Expand Up @@ -97,6 +98,7 @@ ScreenShot::ScreenShot(QWidget *parent)
#endif

setFixedSize(m_virGeom.size());
move(m_virGeom.topLeft());
#else // Q_OS_MAC
// setWindowFlags(Qt::Window); // 不设置则 mac 下 devicePixelRatio: 1
#ifdef _MYDEBUG
Expand All @@ -106,17 +108,16 @@ ScreenShot::ScreenShot(QWidget *parent)
setFixedSize(m_virGeom.size());
#else
// showFullScreen(); // 并不是当前的屏幕大小
setWindowFlags(/*Qt::WindowStaysOnTopHint |*/ Qt::FramelessWindowHint); // 窗口置顶 + 隐藏标题栏
setFixedSize(m_virGeom.size());
auto tt = QApplication::desktop()->rect().size();
setFixedSize(QApplication::desktop()->rect().size());
// setFixedSize(QSize(1600, 800));
// setFixedSize(QGuiApplication::screenAt(QCursor::pos())->size()); // 用 resize() 的话,发现会操蛋的蛋疼

setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); // 窗口置顶 + 隐藏标题栏
m_curScrn = qApp->screenAt(QCursor::pos());
const QRect geom = m_curScrn->geometry();
setFixedSize(geom.size()); // resize() cannot get the desired effect
#endif

move(geom.topLeft());
#endif

// move(m_virGeom.topLeft());

setMouseTracking(true);
m_rtCalcu.scrnType = ScrnType::Wait;

Expand Down Expand Up @@ -1053,11 +1054,14 @@ void ScreenShot::paintEvent(QPaintEvent *event)
QRect rtSel(m_rtCalcu.getSelRect()); // 移动选中矩形
if (m_bSmartWin)
rtSel = m_rtSmartWindow;

const auto & virGeom = QRect(mapFromGlobal(m_virGeom.topLeft()), m_virGeom.size()); // 修复为相对窗口的
m_rtCalcu.limitBound(rtSel, virGeom); // 修复边界时图片被拉伸
QRect shotGeom(mapFromGlobal(m_virGeom.topLeft()), m_virGeom.size()); // 修复为相对窗口的
#ifdef Q_OS_MAC
shotGeom = QRect(mapFromGlobal(m_curScrn->geometry().topLeft()), m_curScrn->geometry().size());
#endif
m_rtCalcu.limitBound(rtSel, shotGeom); // 修复边界时图片被拉伸
if (rtSel.width() > 0 && rtSel.height() > 0) {
m_savePixmap = m_currPixmap->copy(QRect(rtSel.topLeft() * getDevicePixelRatio(), rtSel.size() * getDevicePixelRatio())); // 注意独立屏幕缩放比(eg: macox = 2)
double devPixRatio = getDevicePixelRatio(m_curScrn);
m_savePixmap = m_currPixmap->copy(QRect(rtSel.topLeft() * devPixRatio, rtSel.size() * devPixRatio)); // NOTE: devicePixelRatio(macox = 2)
pa.drawPixmap(rtSel, m_savePixmap);

// 放大镜实现
Expand Down Expand Up @@ -1091,7 +1095,7 @@ void ScreenShot::paintEvent(QPaintEvent *event)

// drawWinInfo(pa);
selectedShapeMove(pa);
drawMaskLayer(virGeom, rtSel, pa);
drawMaskLayer(shotGeom, rtSel, pa);
drawBorder(rtSel, pa);
drawCrosshair(pa);
drawToolBar();
Expand Down Expand Up @@ -1546,6 +1550,22 @@ void ScreenShot::getScrnInfo()

XLOG_INFO("---------------m_screens[] Info BEGIN----------------");
for (const auto& it : m_scrns) {
qDebug() << "序号 it:" << m_scrns.indexOf(it)
<< "\n可用几何 availableGeometry:" << it->availableGeometry()
<< "\n可用虚拟几何 availableVirtualSize:" << it->availableVirtualSize()
<< "\n虚拟几何 virtualGeometry:" << it->virtualGeometry()
<< "\n几何 geometry:" << it->geometry()
<< "\n尺寸 size:" << it->size()
<< "\n物理尺寸 physicalSize:" << it->physicalSize()
<< "\n刷新率 refreshRate:" << it->refreshRate()
<< "\n尺寸 size:" << it->size()
<< "\n虚拟尺寸 virtualSize:" << it->virtualSize()
<< "\n设备像素比 devicePixelRatio:" << it->devicePixelRatio()
<< "\n逻辑DPI logicalDotsPerInch:" << int(it->logicalDotsPerInch()) << " DPIX:" << int(it->logicalDotsPerInchX()) << " DPIY:" << int(it->logicalDotsPerInchY())
<< "\n物理DPI physicalDotsPerInch:" << int(it->physicalDotsPerInch()) << " DPIX:" << int(it->physicalDotsPerInchX()) << " DPIY:" << int(it->physicalDotsPerInchY())
<< "\n手算缩放比 getScale:" << getScale(it)
<< "\n虚拟尺寸 m_virGeom:" << m_virGeom << "\n\n";

XLOG_DEBUG("屏幕详细信息:index[{}]", m_scrns.indexOf(it));
XLOG_DEBUG("size({}, {})", it->size().width(), it->size().height());
XLOG_DEBUG("geometry({}, {}, {} * {})", it->geometry().left(), it->geometry().top(), it->geometry().width(), it->geometry().height());
Expand All @@ -1556,25 +1576,21 @@ void ScreenShot::getScrnInfo()
XLOG_INFO("序号 serialNumber[{}] 刷新率 refreshRate[{}] 模式 model[{}]", it->serialNumber().toUtf8().data(), it->refreshRate(), it->model().toUtf8().data());
XLOG_INFO("虚拟几何 virtualGeometry:({}, {}, {} * {}) 缩放比 getScale[{}]", it->availableGeometry().left(), it->availableGeometry().top(), it->availableGeometry().width(), it->availableGeometry().height(), getScale(it));
XLOG_INFO("物理几何 physicalSize:({} * {}) 大小 size({} * {})", it->physicalSize().width(), it->physicalSize().height(), it->physicalSize().width(), it->physicalSize().height());
XLOG_INFO("物理 DPI physicalDotsPerInch: {} DPIX: {} DPIY: {} ", it->physicalDotsPerInch(), it->physicalDotsPerInchX(), it->physicalDotsPerInchY());
XLOG_INFO("逻辑 DPI logicalDotsPerInch: {} DPIX: {} DPIY: {}\n", it->logicalDotsPerInch(), it->logicalDotsPerInchX(), it->logicalDotsPerInchX());
XLOG_INFO("物理 DPI physicalDotsPerInch: {} DPIX: {} DPIY: {} ", int(it->physicalDotsPerInch()), int(it->physicalDotsPerInchX()), int(it->physicalDotsPerInchY()));
XLOG_INFO("逻辑 DPI logicalDotsPerInch: {} DPIX: {} DPIY: {}\n", int(it->logicalDotsPerInch()), int(it->logicalDotsPerInchX()), int(it->logicalDotsPerInchX()));
}

XLOG_INFO("m_virtualGeometry({}, {}, {} * {})\n", m_virGeom.left(), m_virGeom.top(), m_virGeom.width(), m_virGeom.height());
XLOG_INFO("---------------m_screens[] Info END----------------");
}

double ScreenShot::getDevicePixelRatio()
{
//#ifdef Q_OS_MAC
// return 2; // TODO 2022.03.11 无奈之举;使用 CMake MACOSX_BUNDLE 则返回的缩放比不正常, return 1 ???
//#endif
return m_priScrn->devicePixelRatio();
}

double ScreenShot::getDevicePixelRatio(QScreen * screen)
{
return screen ? screen->devicePixelRatio() : 1.0;
#ifdef Q_OS_MAC
return screen ? screen->devicePixelRatio() : 2; // TODO 2022.03.11 无奈之举;使用 CMake MACOSX_BUNDLE 则返回的缩放比不正常, return 1 ???
#else
return screen ? screen->devicePixelRatio() : 1;
#endif
}

// 随着光标移动,更新获取桌面所有窗口信息
Expand Down Expand Up @@ -1610,7 +1626,7 @@ double ScreenShot::getScale(QScreen * screen)
return 2;
else if (2.25 <= scale && scale < 2.5)
return 2.25;
else if (2.5 <= scale && scale < 3)
else if (2.5 <= scale && scale < 3)get
return 2.5;
else if (3 <= scale && scale < 3.5)
return 3;
Expand Down
4 changes: 2 additions & 2 deletions src/screen/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class ScreenShot : public QWidget
const QScreen *currentScreen(const QPoint &pos = QCursor::pos());

void getScrnInfo();
double getDevicePixelRatio();
double getDevicePixelRatio(QScreen *screen);
double getDevicePixelRatio(QScreen *screen = nullptr);

void updateGetWindowsInfo();
void whichShape();
Expand Down Expand Up @@ -129,6 +128,7 @@ public slots:
double m_scal;
QList<QScreen *> m_scrns; // 所有屏幕
QScreen* m_priScrn; // 主屏幕
QScreen* m_curScrn; // 截图时光标所在的屏幕
QRect m_virGeom; // 截图时刻的虚拟桌面的大小
QPixmap* m_currPixmap; // 当前屏幕截图
QPixmap m_savePixmap; // 当前屏幕截图 + 遮罩 无构造初始化
Expand Down
2 changes: 0 additions & 2 deletions src/tool/testbtstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ TestBTStyle::TestBTStyle(QWidget *parent)


auto layout = new QHBoxLayout(this);


int i = 0;

QStringList listName;
Expand Down

0 comments on commit 8fb2739

Please sign in to comment.