- Timestamp:
- 01/22/10 19:44:34 (2 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
punch-card-project/trunk/punch-card-editor/src/app/mainwindow.cc
r58 r59 3 3 #include "qpunchcard/widget.h" 4 4 #include "deckviewer/navigatormodel.h" 5 6 #include "qpunchcard/jones.h" 5 7 6 8 #include <QApplication> … … 56 58 57 59 void MainWindow::createGraphicalEditor() { 58 graphical_editor = new CardEditor(this); // thistakes ownership nevertheless60 graphical_editor = new CardEditor(this); // ŽthisŽ takes ownership nevertheless 59 61 setCentralWidget(graphical_editor); 60 62 // signale und so: … … 94 96 // this method will close it when everything went good 95 97 QFile file(filename); 96 const FileFormat*format = FileFormatFactory::createFormat(98 FileFormat format = FileFormatFactory::createFormat( 97 99 FileFormatFactory::autoDetectFormat(file) 98 100 ); 99 Deck* new_deck = new Deck (format);101 Deck* new_deck = new Deck; 100 102 // Leser anschmeissen 101 if( new_deck->read(f ile) ) {103 if( new_deck->read(format, file) ) { 102 104 statusBar()->showMessage(QString(tr("Deck read in successfully")), 4000); 103 } else 104 qDebug() << "Errors while reading in the Deck."; 105 } else { 106 QMessageBox::critical(this, tr("Could not open file"), 107 tr("Could not open file %1 using format %2 for reading. Reason: %3") 108 .arg(filename).arg(format.getName()).arg(file.errorString()) 109 ); 110 return; 111 } 105 112 // Alte Datei rausschmeissen 106 113 closeDeck(); 107 114 // Neue setzen 108 115 deck = new_deck; 109 this->file.setFileName( file.fileName() );110 116 notifyFileOpened(); 111 117 } … … 120 126 } 121 127 } else 122 qDebug("notifyFileOpened is supposed to be called when a file was *opened* ");128 qDebug("notifyFileOpened is supposed to be called when a file was *opened*, not closed"); 123 129 } 124 130 … … 141 147 void MainWindow::openFile() { 142 148 if(maybeSave()) { 149 Q_ASSERT(deck); 143 150 // Oeffnen-Dialog anzeigen. 144 QString filename = QFileDialog::getOpenFileName(this,151 QString new_filename = QFileDialog::getOpenFileName(this, 145 152 tr("Open Card Deck File..."), 146 QFileInfo( file).absolutePath(),153 QFileInfo(deck->getFileName()).absolutePath(), 147 154 tr("Jones Emulated Card Decks (*);;Card Editor XML-Files (*.xml *.cml)")); 148 if(! filename.isEmpty()) {149 loadDeck( filename);155 if(!new_filename.isEmpty()) { 156 loadDeck(new_filename); 150 157 } 151 158 } … … 153 160 154 161 bool MainWindow::saveFile() { 155 if(!file.exists() || !deck->canSave()) 156 return saveFileAs(); 157 else 158 return deck->save(file); 162 return deck->canSave() ? deck->save() : saveFileAs(); 159 163 } 160 164 161 165 bool MainWindow::saveFileAs() { 166 Q_ASSERT(deck); 162 167 // GUI anzeigen zum Speichern 163 168 QString filename = QFileDialog::getSaveFileName(this, 164 169 tr("Save Card Deck File..."), 165 QFileInfo( file).absolutePath(),170 QFileInfo(deck->getFileName()).absolutePath(), 166 171 tr("Jones Emulator Card Decks (*);;Card Editor XML-Files (*.xml *.cml)")); 167 172 if(filename.isEmpty()) 168 173 return false; 169 // Eigentlich jetzt: Nach schauen, welches Dateiformat zum Benutzen und so.174 // Eigentlich jetzt: Nachfragen, welches Dateiformat zum Benutzen und so. 170 175 // stattdessen jetzt mal billig: 171 file.setFileName(filename); 172 if(deck->canSave()) { 173 // Nutze das bekannte Dateiformat 174 return deck->save(file); 176 if(!deck->hasFormat()) { 177 // Es fehlt noch ein Format, nutze einfach mal das erstbeste 178 deck->setFormat( FileFormatFactory::createFormat(FileFormatFactory::availableFormats()[0]) ); 179 } 180 181 // Eigene Datei benutzen, statt einfach nur deck->save() aufzurufen, um ggf. Fehler und so 182 // zurueckzukriegen. 183 QFile file(filename); 184 if(deck->save(deck->getFormat(), file)) { 185 statusBar()->showMessage(QString(tr("Deck written successfully to %1").arg(filename)), 4000); 186 return true; 175 187 } else { 176 // Nutze einfach mal 177 deck->setFormat( new JonesFileFormat ); 178 if(deck->save(file)) { 179 statusBar()->showMessage(QString(tr("Deck written successfully to %1").arg(filename)), 4000); 180 return true; 181 } else { 182 // todo: das hier als qmessagewindow oder so 183 statusBar()->showMessage(QString(tr("Error while writing the deck")), 4000); 184 return false; 185 } 188 QMessageBox::critical(this, tr("Saving File failed"), 189 tr("Could not write card deck to file %1, using format %2. Reason: %3") 190 .arg(filename).arg(deck->getFormat().getName()).arg(file.errorString()) 191 ); 192 return false; 186 193 } 187 194 } … … 193 200 // Dann abspeichern oder so... hier waere natuerlich auch interessant, 194 201 // auf bestehende Dateinamen zurueckgreifen zu koennen... 202 if(this->text_editors.count() == 0) { 203 // no text editor present => ask user if he wants to open one 204 if(QMessageBox::question(this, tr("No Text Editor started"), 205 tr("You have not started any Text Editor component yet. " 206 "Each text editor uses his own codec to translate a punch card. " 207 "Hence you need a text editor to export a text using a codec. " 208 "Do you want to open a new text editor?"), 209 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) { 210 this->newTextEditor(); 211 } 212 } else if(this->text_editors.count() == 1) { 213 // exactly *one* text editor present 214 Q_ASSERT(text_editors[0]); 215 text_editors[0]->exportText(); 216 } else { 217 // multiple editors present 218 // urhm... there *should* have been some other toolbar by now... but there is not... so 219 // display some sort of "selection" dialog 220 qDebug() << "Select text editor..."; 221 } 195 222 } 196 223
Powered by
Expect where otherwise noted, content on this site is licensed under a