When the user starts the richedit application we want the focus
to be in the textEdit widget so we'll add one line of code to
the
init() function to achieve this. (All the code snippets
are from qt/tools/designer/examples/richedit/richedit.ui.
The code for these tasks is straightforward. When the user
clicks
we check to see if there are
unsaved changes in the existing text and give them the
opportunity to save, continue without saving or cancel the
operation. When the user opts to open an existing file or
exit the application we perform the same check and offer
them the same choices.
void EditorForm::fileNew()
{
if ( saveAndContinue( "New" ) )
textEdit->clear();
} |
The
fileNew() function clears the text and the filename.
int EditorForm::fileOpen()
{
if ( saveAndContinue( "Open" ) ) {
QString fn( QFileDialog::getOpenFileName(
QString::null,
"Rich Text Files (*.htm*)", this ) );
if ( !fn.isEmpty() ) {
fileName = fn;
QFile file( fileName );
if ( file.open( IO_ReadOnly ) ) {
QTextStream ts( &file );
textEdit->setText( ts.read() );
}
}
}
} |
The
fileOpen() function asks the user to choose a file
using
QFileDialog::getOpenFileName(). If they
choose a file we set the fileName member to its name, open
it and read its contents directly into the text edit via a
text stream.
int EditorForm::fileSave()
{
if ( fileName.isEmpty() ) {
fileSaveAs();
} else {
QFile f( fileName );
if ( f.open( IO_WriteOnly ) ) {
QTextStream ts( &f );
ts << textEdit->text();
textEdit->setModified( FALSE );
}
}
} |
If there is no current file name we call
fileSaveAs() which will prompt for a
file name and if a file name is given calls
fileSave(). If we have a file name we
open a file and write the text from the text edit into the
file via a text stream. We also set the text edit's modified
property to FALSE.
int EditorForm::fileSaveAs()
{
QString fn = QFileDialog::getSaveFileName(
"", "Rich Text Files (*.htm*)", this );
if ( !fn.isEmpty() ) {
fileName = fn;
fileSave();
}
} |
The
fileSaveAs function prompts the user for a file
name and if they give a file name, saves the text to the
file by calling
fileSave().
int EditorForm::fileExit()
{
if ( saveAndContinue( "Exit" ) )
qApp->exit();
} |
When we exit the application we must perform the same check
for unsaved changes as we've done in the preceding
functions, so we've included the
fileExit() function's
code here.
int EditorForm::saveAndContinue( const QString & action )
{
int continueAction = 1;
if ( textEdit->isModified() ) {
switch( QMessageBox::information(
this, "Rich Edit",
"The document contains unsaved changes.\n"
"Do you want to save the changes?",
"&Save", "&Don't Save", "&Cancel " + action,
0, // Enter == button 0
2 ) ) { // Escape == button 2
case 0: // Save; continue
fileSave();
break;
case 1: // Do not save; continue
break;
case 2: // Cancel
continueAction = 0;
break;
}
}
return continueAction;
} |
The
saveAndContinue() function is included for
completeness.
We've already connected the fontComboBox's
activated()
signal to the textEdit's
setFamily() slot so we just
have to populate the combobox with the font names when we
call
init().
int EditorForm::init()
{
textEdit->setFocus();
QFontDatabase fonts;
fontComboBox->insertStringList( fonts.families() );
QString font = textEdit->family();
font = font.lower();
for ( int i = 0 ; i < fontComboBox->count(); i++ ) {
if ( font == fontComboBox->text( i ) ) {
fontComboBox->setCurrentItem( i );
break;
}
}
} |
The first line sets the focus as we've already mentioned. We
then create a
QFontDatabase object and insert its
list of font families into the fontComboBox. Finally we set
the fontComboBox's current item to the textEdit's current
font.