replace QFile::copy with customed one

This commit is contained in:
Roy Qu 2022-08-07 22:25:52 +08:00
parent 92d7370903
commit eda042a683
3 changed files with 33 additions and 6 deletions

View File

@ -864,9 +864,7 @@ bool Project::saveAsTemplate(const QString &templateFolder,
ini->SetValue("Template", "Description", description.toUtf8());
if (fileExists(mOptions.icon)) {
QString iconName = extractFileName(mOptions.icon);
if (dir.exists(iconName))
dir.remove(iconName);
QFile::copy(mOptions.icon, dir.absoluteFilePath(iconName));
copyFile(mOptions.icon, dir.absoluteFilePath(iconName),true);
if (dir.exists(iconName))
ini->SetValue("Template", "Icon", iconName.toUtf8());
}
@ -915,9 +913,7 @@ bool Project::saveAsTemplate(const QString &templateFolder,
const PProjectUnit& unit=mUnits[i];
QString unitName = extractFileName(unit->fileName());
QByteArray section = toByteArray(QString("Unit%1").arg(i));
if (dir.exists(unitName))
dir.remove(unitName);
if (!QFile::copy(unit->fileName(), dir.absoluteFilePath(unitName))) {
if (!copyFile(unit->fileName(), dir.absoluteFilePath(unitName),true)) {
QMessageBox::warning(nullptr,
tr("Warning"),
tr("Can't save file %1").arg(dir.absoluteFilePath(unitName)),

View File

@ -1198,3 +1198,33 @@ int countLeadingWhitespaceChars(const QString &line)
}
return n;
}
bool copyFile(const QString &fromPath, const QString &toPath, bool overwrite)
{
QFile fromFile(fromPath);
QFile toFile(toPath);
if (!fromFile.exists())
return false;
if (toFile.exists()) {
if (!overwrite)
return false;
if (!toFile.remove())
return false;
}
if (!fromFile.open(QFile::ReadOnly))
return false;
if (!toFile.open(QFile::WriteOnly | QFile::Truncate))
return false;
int bufferSize=64*1024;
char buffer[bufferSize];
while (!fromFile.atEnd()) {
int readed = fromFile.read(buffer,bufferSize);
toFile.write(buffer,readed);
}
toFile.close();
fromFile.close();
return true;
}

View File

@ -246,6 +246,7 @@ float pointToPixel(float point, float dpi);
float pixelToPoint(float pixel);
void copyFolder(const QString &fromDir, const QString& toDir);
bool copyFile(const QString &fromFile, const QString& toFile, bool overwrite);
/**