Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ int main(int argc, char *argv[])
}
dbusService.setSingleInstance(true);
qCDebug(dsrApp) << "DBus service set to single instance.";

// 检查是否有其他截图模式参数
bool hasOtherMode = cmdParser.isSet(delayOption) ||
cmdParser.isSet(fullscreenOption) ||
cmdParser.isSet(topWindowOption) ||
cmdParser.isSet(prohibitNotifyOption) ||
cmdParser.isSet(screenRecordFullScreenOption);

// 如果设置了 save-path 且有其他模式,只设置路径不启动截图
if (cmdParser.isSet(savePathOption) && hasOtherMode) {
window.setSavePath(cmdParser.value(savePathOption));
qCDebug(dsrApp) << "Save path set to:" << cmdParser.value(savePathOption);
}

// 处理截图模式参数
if (cmdParser.isSet(delayOption)) {
qDebug() << "cmd delay screenshot";
window.delayScreenshot(cmdParser.value(delayOption).toInt());
Expand All @@ -372,10 +387,6 @@ int main(int argc, char *argv[])
qDebug() << "cmd topWindow screenshot";
window.topWindowScreenshot();
qCDebug(dsrApp) << "Top window screenshot initiated.";
} else if (cmdParser.isSet(savePathOption)) {
qDebug() << "cmd savepath screenshot";
window.savePathScreenshot(cmdParser.value(savePathOption));
qCDebug(dsrApp) << "Save path screenshot initiated with path:" << cmdParser.value(savePathOption);
} else if (cmdParser.isSet(prohibitNotifyOption)) {
qDebug() << "screenshot no notify!";
window.noNotifyScreenshot();
Expand All @@ -384,7 +395,13 @@ int main(int argc, char *argv[])
qDebug() << "screenRecordFullScreenOption!!!!" << cmdParser.value(screenRecordFullScreenOption);
window.fullScreenRecord(cmdParser.value(screenRecordFullScreenOption));
qCDebug(dsrApp) << "Full screen record initiated with file:" << cmdParser.value(screenRecordFullScreenOption);
} else if (cmdParser.isSet(savePathOption)) {
// 只设置了 save-path,没有其他截图模式,启动保存路径截图
qDebug() << "cmd savepath screenshot";
window.savePathScreenshot(cmdParser.value(savePathOption));
qCDebug(dsrApp) << "Save path screenshot initiated with path:" << cmdParser.value(savePathOption);
} else {
// 如果没有设置任何参数,启动默认截图
window.startScreenshot();
qCDebug(dsrApp) << "Default screenshot initiated.";
}
Expand Down
210 changes: 142 additions & 68 deletions src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,26 +2381,99 @@ void MainWindow::saveTopWindow()
exitApp();
}

void MainWindow::savePath(const QString &path)
// 解析路径参数,判断是目录还是完整文件路径
bool MainWindow::parsePathArgument(const QString &path, QString &outDir, QString &outFileName, QString &outFormat)
{
qCDebug(dsrApp) << "savePath";
if (!QFileInfo(path).dir().exists()) {
qCDebug(dsrApp) << "savePath QFileInfo(path).dir().exists()";
exitApp();
if (path.isEmpty()) {
qCWarning(dsrApp) << "parsePathArgument: 输入路径为空";
return false;
}

QFileInfo fileInfo(path);
QString suffix = fileInfo.suffix().toLower();

// 检查是否为支持的图片格式
bool isSupportedFormat = (suffix == "png" || suffix == "jpg" || suffix == "jpeg" || suffix == "bmp");

// 如果是支持的图片格式,且不是已存在的目录,则认为是文件路径
if (isSupportedFormat && !fileInfo.isDir()) {
// 这是一个完整的文件路径
outDir = fileInfo.absolutePath();
outFileName = fileInfo.fileName();

// 转换格式名称
if (suffix == "png") {
outFormat = "PNG";
} else if (suffix == "jpg" || suffix == "jpeg") {
outFormat = "JPEG";
} else if (suffix == "bmp") {
outFormat = "BMP";
}

qCInfo(dsrApp) << "parsePathArgument: 完整文件路径 - 目录:" << outDir
<< ", 文件名:" << outFileName << ", 格式:" << outFormat;
return true;
} else {
// 这是一个目录路径
outDir = path;
outFileName.clear();
outFormat.clear();

qCInfo(dsrApp) << "parsePathArgument: 目录路径:" << outDir;
return false;
}
}

qCDebug(dsrApp) << "path exist!";
void MainWindow::applyPathSettings(const QString &path)
{
QString dir, fileName, format;
bool isFullPath = parsePathArgument(path, dir, fileName, format);

if (isFullPath) {
// 用户指定了完整的文件路径
m_shotWithPath = true;
m_shotWithFullPath = true;
m_shotSavePath = dir;
m_shotFileName = fileName;
m_shotFileFormat = format;

qCInfo(dsrApp) << "applyPathSettings: 完整路径模式 - 目录:" << m_shotSavePath
<< ", 文件名:" << m_shotFileName << ", 格式:" << m_shotFileFormat;
} else {
// 用户指定了目录路径
m_shotWithPath = true;
m_shotWithFullPath = false;
m_shotSavePath = path;
m_shotFileName.clear();
m_shotFileFormat.clear();

qCInfo(dsrApp) << "applyPathSettings: 目录模式 - 目录:" << m_shotSavePath;
}
}

void MainWindow::savePath(const QString &path)
{
qCInfo(dsrApp) << "savePath: 接收到路径 =" << path;

applyPathSettings(path);

this->initAttributes();
this->initLaunchMode("screenShot");
this->showFullScreen();
this->initResource();

m_shotWithPath = true;
m_shotSavePath = path;

qCDebug(dsrApp) << "savePath end";
}

void MainWindow::setSavePath(const QString &path)
{
qCInfo(dsrApp) << "setSavePath: 仅设置保存路径 =" << path;

applyPathSettings(path);

qCDebug(dsrApp) << "setSavePath end (不启动截图)";
}

void MainWindow::startScreenshotFor3rd(const QString &path)
{
qCDebug(dsrApp) << "startScreenshotFor3rd";
Expand Down Expand Up @@ -4206,6 +4279,8 @@ bool MainWindow::saveAction(const QPixmap &pix)
m_saveIndex = ConfigSettings::instance()->getValue("shot", "save_op").value<SaveAction>();
if (m_shotWithPath == true) {
m_saveIndex = AutoSave;
// 命令行指定路径时,使用命令行的路径,避免ui配置的干扰流程
saveWays = SaveWays::SpecifyLocation;
}
}

Expand Down Expand Up @@ -4560,79 +4635,78 @@ bool MainWindow::saveAction(const QPixmap &pix)

return true;
} else if (m_saveIndex == AutoSave && m_saveFileName.isEmpty()) {
QString savePath;
// if (m_shotWithPath == false) {
// savePath = QStandardPaths::writableLocation(saveOption);
// }

// else {
savePath = m_shotSavePath;
// }
QString t_fileName = "";
if (savePath.contains(".png")) {
t_pictureFormat = 0;
// savePath.lastIndexOf("/");
t_fileName = savePath;
}

if (savePath.contains(".jpg")) {
t_pictureFormat = 1;
// savePath.lastIndexOf("/");
t_fileName = savePath;
}

if (savePath.contains(".bmp")) {
t_pictureFormat = 2;
// savePath.lastIndexOf("/");
t_fileName = savePath;
}

if (t_fileName == "") {
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 自动保存模式";

// 检查是否指定了完整文件路径
if (m_shotWithFullPath && !m_shotFileName.isEmpty()) {
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 使用用户指定的完整文件路径";

// 确保目录存在
QDir saveDir(m_shotSavePath);
if (!saveDir.exists()) {
bool mkdirSucc = saveDir.mkpath(".");
if (!mkdirSucc) {
qCritical() << "AutoSave: 无法创建目录:" << m_shotSavePath;
return false;
}
}

// 构建完整路径(直接覆盖同名文件)
m_saveFileName = m_shotSavePath + "/" + m_shotFileName;

qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 保存到:" << m_saveFileName
<< ", 格式:" << m_shotFileFormat;

if (!saveImg(pix, m_saveFileName, m_shotFileFormat.toLatin1().data()))
return false;
} else {
// 原有逻辑:用户只指定了目录,自动生成文件名
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 使用自动生成的文件名";

QString savePath = m_shotSavePath;

// 确保目录存在
QDir saveDir(savePath);
if (!saveDir.exists()) {
bool mkdirSucc = saveDir.mkpath(".");
if (!mkdirSucc) {
qCritical() << "Save path not exist and cannot be created:" << savePath;
qCritical() << "Fall back to temp location!";
savePath = QDir::tempPath();
qCritical() << "AutoSave: 无法创建目录:" << savePath;
return false;
}
}
}
QString t_formatStr;
QString t_formatBuffix;
switch (t_pictureFormat) {
case 0:
t_formatStr = "PNG";
t_formatBuffix = "png";
break;
case 1:
t_formatStr = "JPEG";
t_formatBuffix = "jpg";
break;
case 2:
t_formatStr = "BMP";
t_formatBuffix = "bmp";
break;
default:
t_formatStr = "PNG";
t_formatBuffix = "png";
break;
}
qCDebug(dsrApp) << "save path" << savePath;
QString t_formatStr;
QString t_formatBuffix;
switch (t_pictureFormat) {
case 0:
t_formatStr = "PNG";
t_formatBuffix = "png";
break;
case 1:
t_formatStr = "JPEG";
t_formatBuffix = "jpg";
break;
case 2:
t_formatStr = "BMP";
t_formatBuffix = "bmp";
break;
default:
t_formatStr = "PNG";
t_formatBuffix = "png";
break;
}

if (t_fileName != "") {
m_saveFileName = t_fileName;
} else {
if (selectAreaName.isEmpty()) {
m_saveFileName = QString("%1/%2_%3.%4").arg(savePath, functionTypeStr, currentTime, t_formatBuffix);
} else {
m_saveFileName =
QString("%1/%2_%3_%4.%5").arg(savePath, functionTypeStr, selectAreaName, currentTime, t_formatBuffix);
}
}

if (!saveImg(pix, m_saveFileName, t_formatStr.toLatin1().data()))
return false;
if (!saveImg(pix, m_saveFileName, t_formatStr.toLatin1().data()))
return false;
}
} else if (m_saveIndex == SaveToClipboard) {
if (selectAreaName.isEmpty()) {
tempFileName = QString("%1_%2_%3").arg(tr("Clipboard"), functionTypeStr, currentTime);
Expand Down
20 changes: 20 additions & 0 deletions src/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,19 @@ class MainWindow : public QMainWindow
*/
void saveTopWindow();
void savePath(const QString &path);
void setSavePath(const QString &path);
void startScreenshotFor3rd(const QString &path);
void noNotify();

/**
* @brief 解析路径参数,判断是目录还是完整文件路径
* @param path 输入路径
* @param outDir 输出:目录路径
* @param outFileName 输出:文件名(如果是完整路径)
* @param outFormat 输出:文件格式(PNG/JPEG/BMP)
* @return true=完整文件路径,false=目录路径
*/
bool parsePathArgument(const QString &path, QString &outDir, QString &outFileName, QString &outFormat);
void sendSavingNotify();
// 强制录屏保存退出通知,3D->2D模式
void forciblySavingNotify();
Expand Down Expand Up @@ -968,6 +979,12 @@ public slots:
void initDynamicLibPath();
QString libPath(const QString &strlib);
private:
/**
* @brief 应用路径设置到成员变量
* @param path 输入路径
*/
void applyPathSettings(const QString &path);

// QList<WindowRect> windowRects;
/**
* @brief 所有的窗口,与windowNames一一对应
Expand Down Expand Up @@ -1299,8 +1316,11 @@ public slots:
bool m_selectedCamera = false;
bool m_cameraOffFlag = false;
bool m_shotWithPath = false;
bool m_shotWithFullPath = false;
int m_screenCount;
QString m_shotSavePath;
QString m_shotFileName;
QString m_shotFileFormat;
/**
* @brief 保存截图的标志,进入保存截图时会置为1
*/
Expand Down
6 changes: 6 additions & 0 deletions src/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ void Screenshot::savePathScreenshot(const QString &path)
m_window.savePath(path);
}

void Screenshot::setSavePath(const QString &path)
{
qCDebug(dsrApp) << "setSavePath() called with path:" << path << ".";
m_window.setSavePath(path);
}

void Screenshot::startScreenshotFor3rd(const QString &path)
{
qCDebug(dsrApp) << "startScreenshotFor3rd() called with path:" << path << ".";
Expand Down
1 change: 1 addition & 0 deletions src/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public slots:
void OcrScreenshot();
void ScrollScreenshot();
void savePathScreenshot(const QString &path);
void setSavePath(const QString &path);
void startScreenshotFor3rd(const QString &path);
void initLaunchMode(const QString &launchmode);

Expand Down