Skip to content

Commit

Permalink
shader editor: save prompt on closing modified document
Browse files Browse the repository at this point in the history
  • Loading branch information
Garux committed Aug 21, 2023
1 parent 4209416 commit c9d8265
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion radiant/gtkdlgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2208,7 +2208,7 @@ class QPlainTextEdit_Shader : public QPlainTextEdit
}
};

class TextEditor
class TextEditor : public QObject
{
QWidget *m_window = 0;
QPlainTextEdit *m_textView; // slave, text widget from the gtk editor
Expand All @@ -2218,6 +2218,7 @@ class TextEditor
void construct(){
m_window = new QWidget( MainFrame_getWindow(), Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint );
g_guiSettings.addWindow( m_window, "ShaderEditor/geometry" );
m_window->installEventFilter( this );

auto *vbox = new QVBoxLayout( m_window );
vbox->setContentsMargins( 0, 0, 0, 0 );
Expand Down Expand Up @@ -2312,12 +2313,32 @@ class TextEditor

m_textView->document()->setModified( false );
}
// returns true, if document modifications got saved or user decided to discard them
bool ensure_saved(){
if( m_textView->document()->isModified() ) {
const auto ret = qt_MessageBox( m_window, "Document has been modified.\nSave it?", "Save", EMessageBoxType::Question,
EMessageBoxReturn::eIDYES | EMessageBoxReturn::eIDNO | EMessageBoxReturn::eIDCANCEL );
if( ret == EMessageBoxReturn::eIDYES ){
editor_save();
}
if( ret == EMessageBoxReturn::eIDNO ){ // discard changes
m_textView->clear(); // unset isModified flag this way to avoid messagebox on next opening
}
else if( ret == EMessageBoxReturn::eIDCANCEL ){
return false;
}
}
return true;
}
public:
void DoGtkTextEditor( const char* text, const char* shaderName, const char* filename, const bool editable ){
if ( !m_window ) {
construct(); // build it the first time we need it
}

if( !ensure_saved() )
return;

m_filename = filename;
m_textView->setReadOnly( !editable );
m_textView->setPlainText( text );
Expand All @@ -2342,6 +2363,16 @@ class TextEditor
}
}
}
protected:
bool eventFilter( QObject *obj, QEvent *event ) override {
if( event->type() == QEvent::Close ) {
if( !ensure_saved() ){ // keep editor opened
event->ignore();
return true;
}
}
return QObject::eventFilter( obj, event ); // standard event processing
}
};

static TextEditor g_textEditor;
Expand Down

0 comments on commit c9d8265

Please sign in to comment.