Skip to content

Commit

Permalink
fix: mac The following screenshot shows only the desktop and no other…
Browse files Browse the repository at this point in the history
… windows

and other tests on mac machines will crash when running(eg: MacOS 11.2)

solutions:flameshot-org/flameshot#2957

One of the paths is written dead
  • Loading branch information
XMuli committed Oct 21, 2022
1 parent 76545e3 commit 1459c1e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 54 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
set(PROJECT_NAME PicShot)
project(${PROJECT_NAME} VERSION 0.4 LANGUAGES CXX)
project(${PROJECT_NAME} VERSION 0.4.5 LANGUAGES CXX)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
file(COPY config/config.ini DESTINATION ${CMAKE_BINARY_DIR}/bin)
Expand Down
4 changes: 3 additions & 1 deletion src/core/xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#define SPDLOG_WCHAR_TO_UTF8_SUPPORT // 需要定义在 spdlog 之前,以支持 wchar_t 的输出
#endif

#include <QStandardPaths>
#include <spdlog/spdlog.h>
#include "spdlog/sinks/rotating_file_sink.h"


class XLog
{
public:
Expand All @@ -34,7 +36,7 @@ class XLog

private:
XLog() {
m_logger = spdlog::rotating_logger_mt("fileLogger", "/Users/winks/Desktop/picshot_debug.log", 1024 * 1024 * 20, 10, true);
m_logger = spdlog::rotating_logger_mt("fileLogger", QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first().toStdString() + "/picshot_debug.log", 1024 * 1024 * 20, 10, true);

// 默认类型:[2022-03-19 22:15:15.885] [fileLogger] [info] [screenshot.cpp:941]
// https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
Expand Down
28 changes: 3 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,15 @@

// test
#ifdef Q_OS_WIN
//#include "./platform/wininfo_win.h"
//#include "./platform/wininfo_win.h"Windows10环境下安装Qt Creator5.9.8作为c++的IDE开发工具,学习和使用qt.md
#elif defined(Q_OS_MAC)
#elif defined(Q_OS_LINUX)
#include "./platform/wininfo_x11.h"
#endif



int main(int argc, char *argv[])
{
// TODO 2022-06-18 高分屏适配的许多尝试:
// 【方案一】 Qt 5.14+, 解决 1.5会缩放到2倍,不过显示会有问题,比如按钮之间时不时会有虚线。
// qputenv("QT_ENABLE_HIGHDPI_SCALING", "1");
// QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy);

// 【方案二】 Qt5.12 替代方案
// qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1.5");

// // 【方案三】 Qt5.12 替代方案
//#if (QT_VERSION >= QT_VERSION_CHECK(5,9,0))
// qputenv("QT_SCALE_FACTOR", "1.0"); // 全局缩放因子 https://blog.csdn.net/u014410266/article/details/107488789
// QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif

// 【方案四】 使用 windows 自带的
// exe 统计目录下使用 qt.conf 文件

// 三种方案 https://blog.csdn.net/hanjiang08/article/details/124653265
// https://blog.csdn.net/qq_18260845/article/details/103861201
// https://blog.csdn.net/feiyangqingyun/article/details/124860909

// 高分屏四种方案 https://blog.csdn.net/qq_33154343/article/details/108905279

#if(QT_VERSION > QT_VERSION_CHECK(5,6,0))
// QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // 2K、4K 2@ 倍;获取的分辨率 4K 下实际为 /2 后。 此行需在 QApplication a(argc,argv);前面
Expand Down Expand Up @@ -127,7 +105,7 @@ int main(int argc, char *argv[])
qDebug()<<val<<" ";

#ifdef Q_OS_MAC
const QString value = "Fusion"; // macintosh Windows Fusion
const QString value = "macintosh"; // macintosh Windows Fusion
QApplication::setStyle(value);
#else
#endif
Expand Down
35 changes: 11 additions & 24 deletions src/screen/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <QDir>
#include <QMouseEvent>
#include <QTimer>
#include <QWindow>
#include <QStandardPaths>
#include <QStringList>
#include <QDebug>

namespace Util {
Expand Down Expand Up @@ -460,39 +463,23 @@ void ScreenShot::onSelColor(QColor col)
// 获取虚拟屏幕截图
QPixmap* ScreenShot::getVirScrnPixmap()
{
if (!m_currPixmap) {
QDesktopWidget* desktop = qApp->desktop(); // 获取桌面的窗体对象
const QRect geom = currentScreen(QCursor::pos())->geometry();
if (!m_currPixmap) {
const QScreen* curScrn = currentScreen(QCursor::pos());
const QRect geom = curScrn->geometry();

#if defined(Q_OS_MAC)
// QPoint mousePos = QCursor:: pos();

// int screenNumber = qApp->desktop()->screenNumber(mousePos);
// QScreen* screen = qApp->screenAt(QCursor::pos());


// qDebug() << "Mouse Pos: " << mousePos.x() << ", " << mousePos.y() << ". Screen " << screenNumber << endl;

// auto t1 = mapToGlobal(mousePos);
// QPixmap m_currPixmap = screen->grabWindow(qApp->desktop()->winId(), t1.x(), t1.y(), 1000, 1000);


// auto savePath = QString( "/Users/winks/Desktop/main_%2.png").arg(QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
// // 保存图片
// qDebug() << "savePath" << savePath;
// m_currPixmap.save(savePath);
m_currPixmap = new QPixmap(m_priScrn->grabWindow(0/*desktop->winId()*/, geom.x(), geom.y(), geom.width(), geom.height()));
m_currPixmap = new QPixmap(m_priScrn->grabWindow(qApp->desktop()->winId(), geom.x(), geom.y(), geom.width(), geom.height()));
#else
#ifdef _MYDEBUG
m_currPixmap = new QPixmap(m_priScrn->grabWindow(desktop->winId(), geom.x(), geom.y(), geom.width(), geom.height()));
m_currPixmap = new QPixmap(m_priScrn->grabWindow(qApp->desktop()->winId(), geom.x(), geom.y(), geom.width(), geom.height()));
#else
// 多屏的矩形取并集
m_currPixmap = new QPixmap(m_priScrn->grabWindow(desktop->winId(), m_virGeom.x(), m_virGeom.y(), m_virGeom.width(), m_virGeom.height()));
m_currPixmap = new QPixmap(m_priScrn->grabWindow(qApp->desktop()->winId(), m_virGeom.x(), m_virGeom.y(), m_virGeom.width(), m_virGeom.height()));
#endif
#endif
}

m_currPixmap->save("/Users/winks/Desktop/123456.png");//m_currPixmap->save("123456.png");
m_currPixmap->save(QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first() + "/m_currPixmap_123456.png");
return m_currPixmap;
}

Expand Down Expand Up @@ -1550,7 +1537,7 @@ void ScreenShot::getScrnInfo()

XLOG_INFO("---------------m_screens[] Info BEGIN----------------");
for (const auto& it : m_scrns) {
qDebug() << "序号 it:" << m_scrns.indexOf(it)
qDebug() << "序号 it:" << m_scrns.indexOf(it) << " it:" << it
<< "\n可用几何 availableGeometry:" << it->availableGeometry()
<< "\n可用虚拟几何 availableVirtualSize:" << it->availableVirtualSize()
<< "\n虚拟几何 virtualGeometry:" << it->virtualGeometry()
Expand Down
4 changes: 1 addition & 3 deletions src/tool/parameterbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ void ParameterBar::creatorParaBar(QPointer<ManageBar>& manageBar, QString& path,
tb->setIcon(XHelper::instance().ChangeSVGColor(it.value(), "rect", XHelper::instance().borderColor(), QSize(ICON_WIDTH, ICON_WIDTH) * XHelper::instance().getScale()));
}

if (!tb->setProperty("path", it.value()))
XLOG_INFO("ToolButton Property [path] initialization faile.");

tb->setProperty("path", it.value());
group->addButton(tb);
manageBar->addWidget(tb);
it++;
Expand Down

0 comments on commit 1459c1e

Please sign in to comment.