@@ -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+
24042477void 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);
0 commit comments