PDA

View Full Version : Changing a file extension inside the code



fortyhideout12
19th February 2010, 19:20
So currently a tool I developed has the ability to open two types of files - .xml & .tormatx(.tormatxis just a unique extension I'm using to enable users to not save them as .xml; just for consistency reasons)


void Tormat_Main::open( const QString &p_fn ) {
QString fileName;
if( p_fn.isEmpty() ) { // ask user for a file

fileName = QFileDialog::getOpenFileName(this, tr("Open State"),
baseFolder, tr("TomatFiles (*.tormatx*.xml)"));

if (fileName.isEmpty()) { return; }
} else { // use passed file

Thats the code for opening a file...

My question is, how do I make it so if a user opens up a .xml extension, the program will automatically convert it to .xsps without any user interaction????

Thanks for the help guys

fortyhideout12
19th February 2010, 20:30
I guess I have a save function titled ...::Save

How do I call it within the open function?

Sorry I'm new to this qt/C++ stuff

Lykurg
19th February 2010, 20:32
I guess I have a save function titled ...::Save
You guess or you know? how to call:
Save(/*your options here*/);

fortyhideout12
19th February 2010, 20:38
yeah that was easy.

Except It brings up a save dialogue right away now when I open up a file.

I need the program to automatically change the extension...

Lykurg
19th February 2010, 20:58
yeah that was easy.

Except It brings up a save dialogue right away now when I open up a file.

I need the program to automatically change the extension...

Ehm, than change the code of the save method. Or create another function which saves a file without poping up a file dialog.

fortyhideout12
19th February 2010, 21:08
int Tormat_Main::save() {
QString file_name = QDir::toNativeSeparators( savePath ).section( QDir::separator(), -1 );
if( file_name.startsWith( "." ) ) { // the autosave starts with '.' so we want the user to save to a different location
savePath.clear();
}

if(savePath.isEmpty()) {
savePath = QFileDialog::getSaveFileName(this, tr("Save File"), baseFolder, tr("TormatFiles (*.tormat)"));
}
if (savePath.isEmpty())
return -1;
try{
QFileInfo file(savePath);
writeXmlFile(file);

QString fn = QDir::toNativeSeparators( savePath ).section( QDir::separator(), -1 );
Ui::MainWindow::info->setText( tr( "Saved state to '%1'" ).arg( fn ) );
dialog->showOutput( tr( "Saved state to '%1'" ).arg( fn ) );

QString path_to_base = QDir::toNativeSeparators( savePath ).section( QDir::separator(), 0, -2 ); // to get path of state
QString autosave_filename = QString( "%1\\.%2.autosave" ).arg( path_to_base, fn );
if( autosave_filename != _autosaver->filename() ) { // user has saved to a new state, so update the autosaver
_autosaver->deleteFile(); // delete old autosave file
_autosaver->setFilename( autosave_filename );
} else { // user has saved to same state name
_autosaver->deleteFile(); // delete obsolete autosave file
}
_state_manager->addState( savePath );
}catch(const std::exception &e) {
Logger::LogMessage("Error Writing to File: " + QString(e.what()));
QMessageBox::critical(this,"Save File Error",e.what());
return -2;
}
return 0;
}

Lykurg
19th February 2010, 21:20
And? What should I do with that code?!?