bool save(ColorPalette& palette, const QString& suggested_filename = QString()) { // Attempt to save with the existing file names if ( !suggested_filename.isEmpty() && attemptSave(palette, suggested_filename) ) return true; if ( attemptSave(palette, palette.fileName()) ) return true; // Set up the save directory QDir save_dir(save_path); if ( !save_dir.exists() && !QDir().mkdir(save_path) ) return false; // Attempt to save as (Name).gpl QString filename = palette.name()+".gpl"; if ( !save_dir.exists(filename) && attemptSave(palette, save_dir.absoluteFilePath(filename)) ) return true; // Get all of the files matching the pattern *.gpl save_dir.setNameFilters(QStringList() << "*.gpl"); save_dir.setFilter(QDir::Files); QStringList existing_files = save_dir.entryList(); // For all the files that match (Name)(Number).gpl, find the maximum (Number) QRegularExpression name_regex(QRegularExpression::escape(palette.name())+"([0-9]+)\\.gpl"); int max = 0; for ( const auto& existing_file : existing_files ) { QRegularExpressionMatch match = name_regex.match(existing_file); if ( match.hasMatch() ) { int num = match.captured(1).toInt(); if ( num > max ) max = num; } } return attemptSave(palette, save_dir.absoluteFilePath(QString("%1%2.gpl").arg(palette.name()).arg(max+1)) ); }
void fixUnnamed(ColorPalette& palette) { if ( palette.name().isEmpty() ) palette.setName(ColorPaletteModel::tr("Unnamed")); }