Listing 2. editor.cpp 1 #include "editor.h" 2 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 11 /* 12 * Constructs an Editor which is a child of 13 * 'parent', with the 14 * name 'name' and widget flags set to 'f'. 15 */ 16 Editor::Editor( QWidget* parent, 17 const char* name, WFlags fl ) 18 : ljeditor( parent, name, fl ) 19 { 20 editField = ljeditor::TextEdit; 21 fileName = ""; 22 } 23 24 /* 25 * Destroys the object and frees any allocated 26 * resources. 27 */ 28 Editor::~Editor() 29 { 30 // no need to delete child widgets, 31 // Qt does it all for us 32 } 33 34 /* 35 * Opens a new editor window. 36 */ 37 void Editor::fileNew() 38 { 39 Editor * editor = new Editor; 40 editor->show(); 41 } 42 43 /* 44 * Opens a file in this editor window. 45 */ 46 void Editor::fileOpen() 47 { 48 if ( ! saveAndContinue( tr( "Opening file [nlb] aborted" ) ) ) 49 return; 50 51 editField->setText( "" ); 52 setCaption( tr( "ljedit" ) ); 53 54 55 fileName = QFileDialog::getOpenFileName( ".", 56 QString::null, this, 57 "opendialog", 58 tr( "Open File ..." ) ); 59 60 if ( fileName.isEmpty() ){ 61 statusBar()->message( tr( "Opening file[nlb] aborted" ), 62 2000 ); 63 return; 64 } 65 66 QFile file( fileName ); 67 if ( ! file.open( IO_ReadOnly ) ){ 68 statusBar()->message( QString( tr( [nlb] "Can't read %1" ) ).arg( fileName ), 69 2000 ); 70 return; 71 } 72 73 QTextStream t( &file ); 74 QString text = t.read(); 75 if ( QStyleSheet::mightBeRichText( text ) ) 76 editField->setTextFormat( RichText ); 77 else 78 editField->setTextFormat( PlainText ); 79 80 editField->setText( text ); 81 setCaption( fileName ); 82 statusBar()->message( QString( tr( "%1 [nlb] loaded" ) ).arg( fileName ), 83 2000 ); 84 } 85 86 /* 87 * Save the content of the editor in fileName. 88 */ 89 void Editor::fileSave() 90 { 91 if ( fileName.isEmpty() ){ 92 fileSaveAs(); 93 return; 94 } 95 96 QFile file( fileName ); 97 if ( ! file.open( IO_WriteOnly ) ) { 98 statusBar()->message( QString( tr( "Could[nlb] not write to %1" ) ).arg(fileName), 99 2000 ); 100 return; 101 } 102 QTextStream t( &file ); 103 t << editField->text(); 104 file.close(); 105 106 setCaption( fileName ); 107 108 statusBar()->message( QString( tr( "File %1 [nlb] saved" ) ).arg( fileName ), 109 2000 ); 110 } 111 112 /* 113 * Get the filename to save the file. 114 * Uses fileSave() for actually saving the file. 115 * Stores information whether HTML or plain text 116 * should be written. 117 */ 118 void Editor::fileSaveAs() 119 { 120 QFileDialog * fileDialog = new QFileDialog( 121 ".", 122 QString::null, 123 this, 124 "saveasdialog", 125 TRUE ); 126 fileDialog->setMode( QFileDialog::AnyFile ); 127 128 QString htmlFilter = tr( "HTML files (*.html[nlb] *.htm)" ); 129 fileDialog->addFilter( tr( "Text files [nlb] (*.txt)" ) ); 130 fileDialog->addFilter( htmlFilter ); 131 fileDialog->setCaption( tr( "Save As" ) ); 132 fileDialog->exec(); 133 134 fileName = fileDialog->selectedFile(); 135 136 if ( fileDialog->selectedFilter() == [nlb] htmlFilter ) 137 editField->setTextFormat( RichText ); 138 else 139 editField->setTextFormat( PlainText ); 140 141 if ( ! fileName.isEmpty() ) 142 fileSave(); 143 else 144 statusBar()->message( tr( "Saving [nlb] aborted" ), 145 2000 ); 146 } 147 148 /* 149 * What happens when a window close event occurs? 150 */ 151 void Editor::closeEvent( QCloseEvent * close ) 152 { 153 if ( saveAndContinue( tr( "Closing [nlb] aborted" ) ) ) 154 close->accept(); 155 else 156 close->ignore(); 157 } 158 159 /* 160 * Ask user to save the old file or to cancel the 161 * operation. 162 */ 163 bool Editor::saveAndContinue( [nlb] QString abortMessage ) 164 { 165 if ( ! fileName.isEmpty() || 166 ! editField->text().isEmpty() ){ 167 switch( QMessageBox::information( this, 168 fileName, 169 QString( tr( "Save %1?" ) ).arg( fileName ), 170 tr( "Yes" ), 171 tr( "No" ), 172 tr( "Cancel" ) ) ){ 173 case 0: if ( ! fileName.isEmpty() ) 174 fileSave(); 175 else 176 fileSaveAs(); 177 return TRUE; 178 break; 179 case 1: statusBar()->message( QString( [nlb] tr( "%1 not saved" ) ).arg(fileName), 180 2000 ); 181 return TRUE; 182 break; 183 case 2: statusBar()->message( [nlb] abortMessage, 184 2000 ); 185 return FALSE; 186 break; 187 default: return FALSE; 188 } 189 } else { 190 return TRUE; 191 } 192 }