Skip to content

Commit

Permalink
chore: Distinguish between debug and release versions of debugging
Browse files Browse the repository at this point in the history
1. repair painting mosaic
2. fix the brush width preview
  • Loading branch information
XMuli committed Nov 2, 2022
1 parent 4bf9a16 commit bf16c14
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 105 deletions.
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ set(SRC_RESOURCES
Resources.qrc
logo.rc)

#set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(QT_MODULES Core Gui Widgets Svg Xml)
set(QT_VERSION_MODULES)

Expand Down
57 changes: 24 additions & 33 deletions src/core/xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,62 @@
#ifndef XLOG_H
#define XLOG_H

#ifdef Q_OS_WIN
#define SPDLOG_WCHAR_TO_UTF8_SUPPORT // 需要定义在 spdlog 之前,以支持 wchar_t 的输出
#include <iostream>
#ifdef _WIN32
#define SPDLOG_WCHAR_TO_UTF8_SUPPORT // needs to be defined before spdlog to support the output of wchar_t
#endif

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


class XLog
{
public:
static XLog* instance() {
static XLog instance;
return &instance;
}

std::shared_ptr<spdlog::logger> getLogger() {
return m_logger;
}
}

std::shared_ptr<spdlog::logger> getLogger() { return m_logger; }
void setLevel(const std::string val) { m_level = val; }

private:
XLog() {
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
//m_logger->set_pattern("%Y-%m-%d %H:%M:%S %t %l [%@] [%!] %v");
m_logger->set_pattern("%Y-%m-%d %H:%M:%S %t %l [%!] %v");
#ifdef QT_DEBUG
std::string prePath = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first().toStdString();
#else
std::string prePath = qApp->applicationDirPath().toStdString();
#endif
m_logger = spdlog::rotating_logger_mt("fileLogger", prePath + "/picshot_debug.log", 1024 * 1024 * 20, 10, true);
m_logger->set_pattern("%Y-%m-%d %H:%M:%S %t %l [%!] %v"); // https://github.com/gabime/spdlog/wiki/3.-Custom-formatting


setLevel("debug");
spdlog::level::level_enum logLevel(spdlog::level::n_levels);
if (m_level == "trace") {
m_logger->set_level(spdlog::level::trace);
m_logger->flush_on(spdlog::level::trace);
logLevel = spdlog::level::trace;
} else if (m_level == "debug") {
m_logger->set_level(spdlog::level::debug);
m_logger->flush_on(spdlog::level::debug);
logLevel = spdlog::level::debug;
} else if (m_level == "info") {
m_logger->set_level(spdlog::level::info);
m_logger->flush_on(spdlog::level::info);
logLevel = spdlog::level::info;
} else if (m_level == "warn") {
m_logger->set_level(spdlog::level::warn);
m_logger->flush_on(spdlog::level::warn);
logLevel = spdlog::level::warn;
} else if (m_level == "error") {
m_logger->set_level(spdlog::level::err);
m_logger->flush_on(spdlog::level::err);
logLevel = spdlog::level::err;
} else if (m_level == "critical") {
m_logger->set_level(spdlog::level::critical);
m_logger->flush_on(spdlog::level::critical);
logLevel = spdlog::level::critical;
} else if (m_level == "off") {
m_logger->set_level(spdlog::level::off);
m_logger->flush_on(spdlog::level::off);
logLevel = spdlog::level::off;
} else {
m_logger->set_level(spdlog::level::n_levels);
}
}

~XLog() {
spdlog::drop_all(); // must do this
m_logger->set_level(logLevel);
m_logger->flush_on(logLevel);
}

~XLog() { spdlog::drop_all();} // must do this
XLog(XLog&&) = delete;
XLog(const XLog&) = delete;
void operator= (const XLog&) = delete;
Expand All @@ -93,5 +85,4 @@ class XLog
#define XLOG_CRITICAL(...) SPDLOG_LOGGER_CALL(XLog::instance()->getLogger().get(), spdlog::level::critical, __VA_ARGS__)
#define XLOG_OFF(...) SPDLOG_LOGGER_CALL(XLog::instance()->getLogger().get(), spdlog::level::off, __VA_ARGS__)


#endif // XLOG_H
30 changes: 1 addition & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,24 @@ int main(int argc, char *argv[])

// I18N
insSettings->beginGroup(INIT_GENERAL);
auto lanuage = insSettings->value("Lanuage", "en_US").toString(); // QLocale::system().name()
auto lanuage = insSettings->value("Lanuage", "en_US").toString();
insSettings->endGroup();

QTranslator trans;
QString qmPath(qApp->applicationDirPath());

#if defined(Q_OS_MAC)
qmPath += "/../../../../bin/translations/" + lanuage + ".qm";
#elif defined(Q_OS_WIN)
if (QString(_COMPILER_ID).compare("MSVC") == 0)
qmPath += "/../translations/" + lanuage + ".qm";
else
qmPath += "/translations/" + lanuage + ".qm";

#elif defined(Q_OS_LINUX)
#endif

trans.load(qmPath);
qApp->installTranslator(&trans);
qDebug()<<"[*.qm path]:" << qmPath << _COMPILER_ID << " "<< bool(QString(_COMPILER_ID).compare("MSVC") == 0);

qDebug()<<qApp->applicationFilePath();

// 截图、显示主界面;若点击右上角,则整程序关闭; 如同执行了 close、destroy 一类函数
Tray::instance();

//auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
Expand Down Expand Up @@ -115,16 +109,6 @@ 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();

// 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);
Expand Down Expand Up @@ -154,17 +138,5 @@ int main(int argc, char *argv[])
// PinWidget* w5 = new PinWidget(QPixmap("C:/Users/xmuli/Desktop/Snipaste_2022-07-16_02-13-39.png"), QRect(100, 100, 400, 400));
// w5->show();

// QMouseEvent* evMouse = dynamic_cast<QMouseEvent*>(ev);
// cout << "Posicion: " << evMouse->x() << ", " << evMouse->y() << endl;

// QDesktopWidget* desktop = qApp->desktop(); // 获取桌面的窗体对象

// // 获取屏幕类
// QScreen *pScreen = QGuiApplication::primaryScreen();
// // 获取第一个屏幕的图片
// QPixmap mPixmap = pScreen->grabWindow(0, 0, 0, desktop->size().width(), desktop->size().height());
// // 设置名称为当前时间


return QApplication::exec();
}
14 changes: 3 additions & 11 deletions src/screen/drawhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ const QPixmap* XHelper::SetMosaicSmooth(QPixmap* pixmap, int px)

QGraphicsBlurEffect* blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
QGraphicsPixmapItem* item =
new QGraphicsPixmapItem(*pixmap);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(*pixmap);
item->setGraphicsEffect(blur);

QGraphicsScene scene;
Expand All @@ -135,11 +134,6 @@ const QPixmap* XHelper::SetMosaicSmooth(QPixmap* pixmap, int px)
blur->setBlurRadius(12);
// multiple repeat for make blur effect stronger
scene.render(&painter, pixmap->rect(), QRectF());

//pixmap->save("hahhaha.png");

// TODO 2022-01-09: QGraphicsBlurEffect
// key: qt 截图 毛玻璃效果
return pixmap;
}

Expand All @@ -150,7 +144,6 @@ const QImage XHelper::SetMosaicPixlelated(QPixmap* pixmap, int px /*= 20*/)

const QImage& image = pixmap->toImage();
QImage* pImage = const_cast<QImage*>(&image);

const int width = image.width();
const int height = image.height();

Expand All @@ -164,8 +157,7 @@ const QImage XHelper::SetMosaicPixlelated(QPixmap* pixmap, int px /*= 20*/)

const QPoint topLeft(i, j);
const QRect rt(topLeft, size);

// qInfo()<<"--------->>i:"<< i << " j:" << j << " rt:" << rt;
//qDebug()<<"--------->>i:"<< i << " j:" << j << " rt:" << rt;
QColor color = pImage->pixelColor(rt.topLeft());
for (int x = rt.x(); x <= rt.right(); ++x) {
for (int y = rt.y(); y <= rt.bottom(); ++y)
Expand All @@ -174,7 +166,7 @@ const QImage XHelper::SetMosaicPixlelated(QPixmap* pixmap, int px /*= 20*/)
}
}

return image; // TODO 可优化: 值传递
return image;
}

const int ArrowWidth = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/screen/drawhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class XHelper : public QObject
void SetAttrRecur(QDomElement& elem, QString strtagname, QString strattr, QString strattrval);

// Mosaics draw
const QPixmap* SetMosaicSmooth(QPixmap* pixmap, int px); // 毛玻璃马赛克
const QPixmap* SetMosaicSmooth(QPixmap* pixmap, int px); // 毛玻璃马赛克
const QImage SetMosaicPixlelated(QPixmap* pixmap, int px = 20); // 像素级马赛克

// Arrow Line draw
Expand Down
36 changes: 19 additions & 17 deletions src/screen/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ ScreenShot::ScreenShot(QWidget *parent)
move(geom.topLeft());
#endif


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

// new refactor
m_selSizeTip->setVisible(false);
m_lineWidthTip->setVisible(false);
m_lineWidthTip->setFixedSize(75, 75);
m_lineWidthTip->setFixedSize(50, 50);
m_lineWidthTip->move(15, 15);
m_selBar->setVisible(false);
m_paraBar->setVisible(false);
Expand Down Expand Up @@ -166,7 +165,7 @@ ScrnType ScreenShot::updateScrnType(const QPoint pos)
} else if (cursArea == CursorArea::Border) {
return ScrnType::Stretch;
} else {
return ScrnType::Wait; // 避免警告,不会运行到此
return ScrnType::Wait;
}
}

Expand Down Expand Up @@ -335,7 +334,7 @@ void ScreenShot::onPin()
return;

auto pin = new PinWidget(m_savePixmap, m_savePixmap.rect(), nullptr); // 使用 nullptr,不会泄露
pin->move(m_rtCalcu.getSelRect().topLeft());
pin->move(mapToGlobal(m_rtCalcu.getSelRect().topLeft()));
pin->show();

clearnAndClose();
Expand All @@ -357,6 +356,8 @@ void ScreenShot::onSave()
m_savePixmap.save(fileNmae, nullptr, XHelper::instance().imgQuailty()); // 绘画在 m_savePixmap 中,若在 m_savePixmap 会有 selRect 的左上角的点的偏移
QTime stopTime = QTime::currentTime();
int elapsed = startTime.msecsTo(stopTime);

#ifdef QT_DEBUG
qDebug() << "save m_savePixmap tim =" << elapsed << "ms" << m_savePixmap.size();
//m_currPixmap->save("a2.png");

Expand All @@ -365,6 +366,8 @@ void ScreenShot::onSave()
if (!path.isEmpty()) {
m_savePixmap.save(path + QDir::separator() + imageName);
}
#else
#endif
}

clearnAndClose();
Expand Down Expand Up @@ -432,7 +435,6 @@ void ScreenShot::onParaBtnId(DrawShape shape, QToolButton* tb)
} else if (bText) {
} else if (bSeriNum) {
} else {
//XLOG_INFO
}
}

Expand All @@ -443,8 +445,11 @@ void ScreenShot::onLineWidthChange(int width)
m_lineWidthTip->setText(QString::number(width));
m_lineWidthTip->setAlignment(Qt::AlignCenter);
m_lineWidthTip->setFont(font);
m_lineWidthTip->move(mapFromGlobal(currentScreen()->geometry().topLeft()));
m_lineWidthTip->raise();
m_lineWidthTip->setVisible(true);
QTimer::singleShot(5000, [&]() { m_lineWidthTip->setVisible(false); }); // TODO 2022.09.14: 怎么有时候不到 5s 便消失覆盖了

QTimer::singleShot(4000, this, [&]() { m_lineWidthTip->setVisible(false); });
}

void ScreenShot::onSelColor(QColor col)
Expand Down Expand Up @@ -475,7 +480,7 @@ QPixmap* ScreenShot::getVirScrnPixmap()
#endif
}

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

Expand Down Expand Up @@ -685,7 +690,9 @@ void ScreenShot::drawStep(QPainter& pa, XDrawStep& step, bool isUseEnvContext)
if (!m_currPixmap || step.rt.isEmpty()) // 优化,删除就很明显
return;

QPixmap mosaicPixmap = m_currPixmap->copy(QRect(mapFromGlobal(step.rt.topLeft()) * getDevicePixelRatio(), step.rt.size() * getDevicePixelRatio()));
auto rt = QRect(step.rt.topLeft() * getDevicePixelRatio(), step.rt.size() * getDevicePixelRatio());
QPixmap mosaicPixmap = m_currPixmap->copy(rt);
mosaicPixmap.save(QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).first() + "/mosaicPixmap_123456.png");
if (step.bStyele == 0) {
const QPixmap* pix = XHelper::instance().SetMosaicSmooth(&mosaicPixmap, step.mscPx);
pa.drawPixmap(step.rt, *pix);
Expand Down Expand Up @@ -833,19 +840,14 @@ const QVector<QPoint> ScreenShot::drawBarPosition(Qt::Orientation orien /*= Qt::

const QPoint ScreenShot::drawSelSizeTip(const QRect& rt)
{
//QString str = QString("rtSel(%1, %2, %3 * %4) m_savePixmap.rect:%5 * %6").arg(rt.left()).arg(rt.top()).arg(rt.width()).arg(rt.height())
// .arg(m_savePixmap.width()).arg(m_savePixmap.height());

QString str = QString("%1 x %2").arg(rt.width()).arg(rt.height());
const QPoint pos = mapToGlobal(rt.topLeft());
QString str = QString("[%1, %2, %3 * %4]").arg(pos.x()).arg(pos.y()).arg(rt.width()).arg(rt.height());
if (str.compare(m_selSizeTip->text()) != 0) {
emit m_selSizeTip->sigTextChanged(str);
m_selSizeTip->setText(str);
}

QPoint ret;
ret.setX(rt.left());
ret.setY(rt.top() - m_selSizeTip->height() - SS_SPACE_TO_SELECTRECT);
return ret;

return QPoint(rt.left(), rt.top() - m_selSizeTip->height() - SS_SPACE_TO_SELECTRECT);
}

void ScreenShot::selectedShapeMove(QPainter& pa)
Expand Down
Loading

0 comments on commit bf16c14

Please sign in to comment.