Skip to content

Commit 7b7ea33

Browse files
feat(main): Improve command-line save path handling with full file path support
- Reorder command-line argument processing to handle save-path before screenshot mode options - Add parsePathArgument() method to distinguish between directory paths and complete file paths - Support full file path specification with format detection (PNG, JPEG, BMP) - Add m_shotWithFullPath flag to track whether user specified a complete file path - Extract and store file name and format from full path arguments - Update saveAction() to use specified file path directly when provided via command-line - Improve logging with qCInfo for better debugging of path handling logic - Prevent default screenshot from launching when save-path option is set - Ensure directory creation before saving when using command-line specified paths - Refactor savePath() method to handle both directory and full file path modes
1 parent e1a2b28 commit 7b7ea33

5 files changed

Lines changed: 190 additions & 72 deletions

File tree

src/main.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,21 @@ int main(int argc, char *argv[])
361361
}
362362
dbusService.setSingleInstance(true);
363363
qCDebug(dsrApp) << "DBus service set to single instance.";
364+
365+
// 检查是否有其他截图模式参数
366+
bool hasOtherMode = cmdParser.isSet(delayOption) ||
367+
cmdParser.isSet(fullscreenOption) ||
368+
cmdParser.isSet(topWindowOption) ||
369+
cmdParser.isSet(prohibitNotifyOption) ||
370+
cmdParser.isSet(screenRecordFullScreenOption);
371+
372+
// 如果设置了 save-path 且有其他模式,只设置路径不启动截图
373+
if (cmdParser.isSet(savePathOption) && hasOtherMode) {
374+
window.setSavePath(cmdParser.value(savePathOption));
375+
qCDebug(dsrApp) << "Save path set to:" << cmdParser.value(savePathOption);
376+
}
377+
378+
// 处理截图模式参数
364379
if (cmdParser.isSet(delayOption)) {
365380
qDebug() << "cmd delay screenshot";
366381
window.delayScreenshot(cmdParser.value(delayOption).toInt());
@@ -372,10 +387,6 @@ int main(int argc, char *argv[])
372387
qDebug() << "cmd topWindow screenshot";
373388
window.topWindowScreenshot();
374389
qCDebug(dsrApp) << "Top window screenshot initiated.";
375-
} else if (cmdParser.isSet(savePathOption)) {
376-
qDebug() << "cmd savepath screenshot";
377-
window.savePathScreenshot(cmdParser.value(savePathOption));
378-
qCDebug(dsrApp) << "Save path screenshot initiated with path:" << cmdParser.value(savePathOption);
379390
} else if (cmdParser.isSet(prohibitNotifyOption)) {
380391
qDebug() << "screenshot no notify!";
381392
window.noNotifyScreenshot();
@@ -384,7 +395,13 @@ int main(int argc, char *argv[])
384395
qDebug() << "screenRecordFullScreenOption!!!!" << cmdParser.value(screenRecordFullScreenOption);
385396
window.fullScreenRecord(cmdParser.value(screenRecordFullScreenOption));
386397
qCDebug(dsrApp) << "Full screen record initiated with file:" << cmdParser.value(screenRecordFullScreenOption);
398+
} else if (cmdParser.isSet(savePathOption)) {
399+
// 只设置了 save-path,没有其他截图模式,启动保存路径截图
400+
qDebug() << "cmd savepath screenshot";
401+
window.savePathScreenshot(cmdParser.value(savePathOption));
402+
qCDebug(dsrApp) << "Save path screenshot initiated with path:" << cmdParser.value(savePathOption);
387403
} else {
404+
// 如果没有设置任何参数,启动默认截图
388405
window.startScreenshot();
389406
qCDebug(dsrApp) << "Default screenshot initiated.";
390407
}

src/main_window.cpp

Lines changed: 142 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,26 +2381,99 @@ void MainWindow::saveTopWindow()
23812381
exitApp();
23822382
}
23832383

2384-
void MainWindow::savePath(const QString &path)
2384+
// 解析路径参数,判断是目录还是完整文件路径
2385+
bool MainWindow::parsePathArgument(const QString &path, QString &outDir, QString &outFileName, QString &outFormat)
23852386
{
2386-
qCDebug(dsrApp) << "savePath";
2387-
if (!QFileInfo(path).dir().exists()) {
2388-
qCDebug(dsrApp) << "savePath QFileInfo(path).dir().exists()";
2389-
exitApp();
2387+
if (path.isEmpty()) {
2388+
qCWarning(dsrApp) << "parsePathArgument: 输入路径为空";
2389+
return false;
23902390
}
2391+
2392+
QFileInfo fileInfo(path);
2393+
QString suffix = fileInfo.suffix().toLower();
2394+
2395+
// 检查是否为支持的图片格式
2396+
bool isSupportedFormat = (suffix == "png" || suffix == "jpg" || suffix == "jpeg" || suffix == "bmp");
2397+
2398+
// 如果是支持的图片格式,且不是已存在的目录,则认为是文件路径
2399+
if (isSupportedFormat && !fileInfo.isDir()) {
2400+
// 这是一个完整的文件路径
2401+
outDir = fileInfo.absolutePath();
2402+
outFileName = fileInfo.fileName();
2403+
2404+
// 转换格式名称
2405+
if (suffix == "png") {
2406+
outFormat = "PNG";
2407+
} else if (suffix == "jpg" || suffix == "jpeg") {
2408+
outFormat = "JPEG";
2409+
} else if (suffix == "bmp") {
2410+
outFormat = "BMP";
2411+
}
2412+
2413+
qCInfo(dsrApp) << "parsePathArgument: 完整文件路径 - 目录:" << outDir
2414+
<< ", 文件名:" << outFileName << ", 格式:" << outFormat;
2415+
return true;
2416+
} else {
2417+
// 这是一个目录路径
2418+
outDir = path;
2419+
outFileName.clear();
2420+
outFormat.clear();
2421+
2422+
qCInfo(dsrApp) << "parsePathArgument: 目录路径:" << outDir;
2423+
return false;
2424+
}
2425+
}
23912426

2392-
qCDebug(dsrApp) << "path exist!";
2427+
void MainWindow::applyPathSettings(const QString &path)
2428+
{
2429+
QString dir, fileName, format;
2430+
bool isFullPath = parsePathArgument(path, dir, fileName, format);
2431+
2432+
if (isFullPath) {
2433+
// 用户指定了完整的文件路径
2434+
m_shotWithPath = true;
2435+
m_shotWithFullPath = true;
2436+
m_shotSavePath = dir;
2437+
m_shotFileName = fileName;
2438+
m_shotFileFormat = format;
2439+
2440+
qCInfo(dsrApp) << "applyPathSettings: 完整路径模式 - 目录:" << m_shotSavePath
2441+
<< ", 文件名:" << m_shotFileName << ", 格式:" << m_shotFileFormat;
2442+
} else {
2443+
// 用户指定了目录路径
2444+
m_shotWithPath = true;
2445+
m_shotWithFullPath = false;
2446+
m_shotSavePath = path;
2447+
m_shotFileName.clear();
2448+
m_shotFileFormat.clear();
2449+
2450+
qCInfo(dsrApp) << "applyPathSettings: 目录模式 - 目录:" << m_shotSavePath;
2451+
}
2452+
}
2453+
2454+
void MainWindow::savePath(const QString &path)
2455+
{
2456+
qCInfo(dsrApp) << "savePath: 接收到路径 =" << path;
2457+
2458+
applyPathSettings(path);
23932459

23942460
this->initAttributes();
23952461
this->initLaunchMode("screenShot");
23962462
this->showFullScreen();
23972463
this->initResource();
2398-
2399-
m_shotWithPath = true;
2400-
m_shotSavePath = path;
2464+
24012465
qCDebug(dsrApp) << "savePath end";
24022466
}
24032467

2468+
void MainWindow::setSavePath(const QString &path)
2469+
{
2470+
qCInfo(dsrApp) << "setSavePath: 仅设置保存路径 =" << path;
2471+
2472+
applyPathSettings(path);
2473+
2474+
qCDebug(dsrApp) << "setSavePath end (不启动截图)";
2475+
}
2476+
24042477
void MainWindow::startScreenshotFor3rd(const QString &path)
24052478
{
24062479
qCDebug(dsrApp) << "startScreenshotFor3rd";
@@ -4206,6 +4279,8 @@ bool MainWindow::saveAction(const QPixmap &pix)
42064279
m_saveIndex = ConfigSettings::instance()->getValue("shot", "save_op").value<SaveAction>();
42074280
if (m_shotWithPath == true) {
42084281
m_saveIndex = AutoSave;
4282+
// 命令行指定路径时,使用命令行的路径,避免ui配置的干扰流程
4283+
saveWays = SaveWays::SpecifyLocation;
42094284
}
42104285
}
42114286

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

45614636
return true;
45624637
} else if (m_saveIndex == AutoSave && m_saveFileName.isEmpty()) {
4563-
QString savePath;
4564-
// if (m_shotWithPath == false) {
4565-
// savePath = QStandardPaths::writableLocation(saveOption);
4566-
// }
4567-
4568-
// else {
4569-
savePath = m_shotSavePath;
4570-
// }
4571-
QString t_fileName = "";
4572-
if (savePath.contains(".png")) {
4573-
t_pictureFormat = 0;
4574-
// savePath.lastIndexOf("/");
4575-
t_fileName = savePath;
4576-
}
4577-
4578-
if (savePath.contains(".jpg")) {
4579-
t_pictureFormat = 1;
4580-
// savePath.lastIndexOf("/");
4581-
t_fileName = savePath;
4582-
}
4583-
4584-
if (savePath.contains(".bmp")) {
4585-
t_pictureFormat = 2;
4586-
// savePath.lastIndexOf("/");
4587-
t_fileName = savePath;
4588-
}
4589-
4590-
if (t_fileName == "") {
4638+
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 自动保存模式";
4639+
4640+
// 检查是否指定了完整文件路径
4641+
if (m_shotWithFullPath && !m_shotFileName.isEmpty()) {
4642+
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 使用用户指定的完整文件路径";
4643+
4644+
// 确保目录存在
4645+
QDir saveDir(m_shotSavePath);
4646+
if (!saveDir.exists()) {
4647+
bool mkdirSucc = saveDir.mkpath(".");
4648+
if (!mkdirSucc) {
4649+
qCritical() << "AutoSave: 无法创建目录:" << m_shotSavePath;
4650+
return false;
4651+
}
4652+
}
4653+
4654+
// 构建完整路径(直接覆盖同名文件)
4655+
m_saveFileName = m_shotSavePath + "/" + m_shotFileName;
4656+
4657+
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 保存到:" << m_saveFileName
4658+
<< ", 格式:" << m_shotFileFormat;
4659+
4660+
if (!saveImg(pix, m_saveFileName, m_shotFileFormat.toLatin1().data()))
4661+
return false;
4662+
} else {
4663+
// 原有逻辑:用户只指定了目录,自动生成文件名
4664+
qCInfo(dsrApp) << __FUNCTION__ << __LINE__ << "AutoSave: 使用自动生成的文件名";
4665+
4666+
QString savePath = m_shotSavePath;
4667+
4668+
// 确保目录存在
45914669
QDir saveDir(savePath);
45924670
if (!saveDir.exists()) {
45934671
bool mkdirSucc = saveDir.mkpath(".");
45944672
if (!mkdirSucc) {
4595-
qCritical() << "Save path not exist and cannot be created:" << savePath;
4596-
qCritical() << "Fall back to temp location!";
4597-
savePath = QDir::tempPath();
4673+
qCritical() << "AutoSave: 无法创建目录:" << savePath;
4674+
return false;
45984675
}
45994676
}
4600-
}
4601-
QString t_formatStr;
4602-
QString t_formatBuffix;
4603-
switch (t_pictureFormat) {
4604-
case 0:
4605-
t_formatStr = "PNG";
4606-
t_formatBuffix = "png";
4607-
break;
4608-
case 1:
4609-
t_formatStr = "JPEG";
4610-
t_formatBuffix = "jpg";
4611-
break;
4612-
case 2:
4613-
t_formatStr = "BMP";
4614-
t_formatBuffix = "bmp";
4615-
break;
4616-
default:
4617-
t_formatStr = "PNG";
4618-
t_formatBuffix = "png";
4619-
break;
4620-
}
4621-
qCDebug(dsrApp) << "save path" << savePath;
4677+
4678+
QString t_formatStr;
4679+
QString t_formatBuffix;
4680+
4681+
switch (t_pictureFormat) {
4682+
case 0:
4683+
t_formatStr = "PNG";
4684+
t_formatBuffix = "png";
4685+
break;
4686+
case 1:
4687+
t_formatStr = "JPEG";
4688+
t_formatBuffix = "jpg";
4689+
break;
4690+
case 2:
4691+
t_formatStr = "BMP";
4692+
t_formatBuffix = "bmp";
4693+
break;
4694+
default:
4695+
t_formatStr = "PNG";
4696+
t_formatBuffix = "png";
4697+
break;
4698+
}
46224699

4623-
if (t_fileName != "") {
4624-
m_saveFileName = t_fileName;
4625-
} else {
46264700
if (selectAreaName.isEmpty()) {
46274701
m_saveFileName = QString("%1/%2_%3.%4").arg(savePath, functionTypeStr, currentTime, t_formatBuffix);
46284702
} else {
46294703
m_saveFileName =
46304704
QString("%1/%2_%3_%4.%5").arg(savePath, functionTypeStr, selectAreaName, currentTime, t_formatBuffix);
46314705
}
4632-
}
46334706

4634-
if (!saveImg(pix, m_saveFileName, t_formatStr.toLatin1().data()))
4635-
return false;
4707+
if (!saveImg(pix, m_saveFileName, t_formatStr.toLatin1().data()))
4708+
return false;
4709+
}
46364710
} else if (m_saveIndex == SaveToClipboard) {
46374711
if (selectAreaName.isEmpty()) {
46384712
tempFileName = QString("%1_%2_%3").arg(tr("Clipboard"), functionTypeStr, currentTime);

src/main_window.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,19 @@ class MainWindow : public QMainWindow
376376
*/
377377
void saveTopWindow();
378378
void savePath(const QString &path);
379+
void setSavePath(const QString &path);
379380
void startScreenshotFor3rd(const QString &path);
380381
void noNotify();
382+
383+
/**
384+
* @brief 解析路径参数,判断是目录还是完整文件路径
385+
* @param path 输入路径
386+
* @param outDir 输出:目录路径
387+
* @param outFileName 输出:文件名(如果是完整路径)
388+
* @param outFormat 输出:文件格式(PNG/JPEG/BMP)
389+
* @return true=完整文件路径,false=目录路径
390+
*/
391+
bool parsePathArgument(const QString &path, QString &outDir, QString &outFileName, QString &outFormat);
381392
void sendSavingNotify();
382393
// 强制录屏保存退出通知,3D->2D模式
383394
void forciblySavingNotify();
@@ -968,6 +979,12 @@ public slots:
968979
void initDynamicLibPath();
969980
QString libPath(const QString &strlib);
970981
private:
982+
/**
983+
* @brief 应用路径设置到成员变量
984+
* @param path 输入路径
985+
*/
986+
void applyPathSettings(const QString &path);
987+
971988
// QList<WindowRect> windowRects;
972989
/**
973990
* @brief 所有的窗口,与windowNames一一对应
@@ -1299,8 +1316,11 @@ public slots:
12991316
bool m_selectedCamera = false;
13001317
bool m_cameraOffFlag = false;
13011318
bool m_shotWithPath = false;
1319+
bool m_shotWithFullPath = false;
13021320
int m_screenCount;
13031321
QString m_shotSavePath;
1322+
QString m_shotFileName;
1323+
QString m_shotFileFormat;
13041324
/**
13051325
* @brief 保存截图的标志,进入保存截图时会置为1
13061326
*/

src/screenshot.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ void Screenshot::savePathScreenshot(const QString &path)
177177
m_window.savePath(path);
178178
}
179179

180+
void Screenshot::setSavePath(const QString &path)
181+
{
182+
qCDebug(dsrApp) << "setSavePath() called with path:" << path << ".";
183+
m_window.setSavePath(path);
184+
}
185+
180186
void Screenshot::startScreenshotFor3rd(const QString &path)
181187
{
182188
qCDebug(dsrApp) << "startScreenshotFor3rd() called with path:" << path << ".";

src/screenshot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public slots:
3030
void OcrScreenshot();
3131
void ScrollScreenshot();
3232
void savePathScreenshot(const QString &path);
33+
void setSavePath(const QString &path);
3334
void startScreenshotFor3rd(const QString &path);
3435
void initLaunchMode(const QString &launchmode);
3536

0 commit comments

Comments
 (0)