author | paule <paule> | 2007-01-13 07:34:07 (UTC) |
---|---|---|
committer | paule <paule> | 2007-01-13 07:34:07 (UTC) |
commit | b1075202780c1d807c9d9f5286a2ffa7714bae51 (patch) (unidiff) | |
tree | 6d37dc6d7f7b45d39f7f379c8654fd085d63e895 /noncore | |
parent | ee43ea083b2abea078507677ee30c7af88d248c4 (diff) | |
download | opie-b1075202780c1d807c9d9f5286a2ffa7714bae51.zip opie-b1075202780c1d807c9d9f5286a2ffa7714bae51.tar.gz opie-b1075202780c1d807c9d9f5286a2ffa7714bae51.tar.bz2 |
Prompt user to save on closing a modified file; prompt user on Save As if specified file already exists; change tab to show filename when saving a new file
-rw-r--r-- | noncore/apps/tinykate/mainwindow/tinykate.cpp | 136 | ||||
-rw-r--r-- | noncore/apps/tinykate/mainwindow/tinykate.h | 5 |
2 files changed, 116 insertions, 25 deletions
diff --git a/noncore/apps/tinykate/mainwindow/tinykate.cpp b/noncore/apps/tinykate/mainwindow/tinykate.cpp index b1b88e9..e87464e 100644 --- a/noncore/apps/tinykate/mainwindow/tinykate.cpp +++ b/noncore/apps/tinykate/mainwindow/tinykate.cpp | |||
@@ -183,12 +183,6 @@ TinyKate::~TinyKate( ) | |||
183 | { | 183 | { |
184 | owarn << "TinyKate destructor\n" << oendl; | 184 | owarn << "TinyKate destructor\n" << oendl; |
185 | 185 | ||
186 | shutDown=true; | ||
187 | while (currentView!=0) | ||
188 | { | ||
189 | slotClose(); | ||
190 | } | ||
191 | |||
192 | if( KGlobal::config() != 0 ) | 186 | if( KGlobal::config() != 0 ) |
193 | { | 187 | { |
194 | owarn << "deleting KateConfig object..\n" << oendl; | 188 | owarn << "deleting KateConfig object..\n" << oendl; |
@@ -285,42 +279,95 @@ void TinyKate::slotNew( ) | |||
285 | { | 279 | { |
286 | KateDocument *kd= new KateDocument(false, false, this,0,this); | 280 | KateDocument *kd= new KateDocument(false, false, this,0,this); |
287 | KTextEditor::View *kv; | 281 | KTextEditor::View *kv; |
282 | |||
283 | kd->setDocName(tr("Unnamed %1").arg(nextUnnamed++)); | ||
284 | kd->setNewDoc(true); | ||
288 | tabwidget->addTab(kv=kd->createView(tabwidget,"BLAH"), | 285 | tabwidget->addTab(kv=kd->createView(tabwidget,"BLAH"), |
289 | "tinykate/tinykate", | 286 | "tinykate/tinykate", |
290 | tr("Unnamed %1").arg(nextUnnamed++)); | 287 | kd->docName()); |
291 | viewCount++; | 288 | viewCount++; |
292 | } | 289 | } |
293 | 290 | ||
294 | void TinyKate::slotClose( ) | 291 | bool TinyKate::checkSave() { |
292 | if (currentView==0) return true; | ||
293 | |||
294 | KateView *kv = (KateView*) currentView; | ||
295 | if(kv->isModified()) { | ||
296 | KateDocument *kd = (KateDocument*) kv->document(); | ||
297 | switch( QMessageBox::information( 0, (tr("TinyKATE")), | ||
298 | (tr("Do you want to save\n" | ||
299 | "changes to the document\n" | ||
300 | "%1?\n").arg(kd->docName())), | ||
301 | (tr("Save")), (tr("Don't Save")), (tr("&Cancel")), 2, 2 ) ) | ||
302 | { | ||
303 | case 0: | ||
304 | { | ||
305 | return saveDocument(); | ||
306 | } | ||
307 | break; | ||
308 | |||
309 | case 1: | ||
310 | { | ||
311 | return true; | ||
312 | } | ||
313 | break; | ||
314 | |||
315 | default: | ||
316 | { | ||
317 | return false; | ||
318 | } | ||
319 | break; | ||
320 | }; | ||
321 | } | ||
322 | else { | ||
323 | return true; | ||
324 | } | ||
325 | } | ||
326 | |||
327 | bool TinyKate::closeDocument() | ||
295 | { | 328 | { |
296 | if (currentView==0) return; | 329 | if (currentView==0) return true; |
297 | KTextEditor::View *dv=currentView; | 330 | KTextEditor::View *dv=currentView; |
298 | currentView=0; | 331 | if(checkSave()) { |
299 | tabwidget->removePage(dv); | 332 | currentView=0; |
300 | delete dv->document(); | 333 | tabwidget->removePage(dv); |
301 | viewCount--; | 334 | delete dv->document(); |
302 | if ((!viewCount) && (!shutDown)) slotNew(); | 335 | viewCount--; |
336 | if ((!viewCount) && (!shutDown)) slotNew(); | ||
337 | return true; | ||
338 | } | ||
339 | else | ||
340 | return false; | ||
303 | } | 341 | } |
304 | 342 | ||
305 | void TinyKate::slotSave() | 343 | void TinyKate::slotClose( ) |
344 | { | ||
345 | closeDocument(); | ||
346 | } | ||
347 | |||
348 | bool TinyKate::saveDocument() | ||
306 | { | 349 | { |
307 | // feel free to make this how you want | 350 | // feel free to make this how you want |
308 | if (currentView==0) return; | 351 | if (currentView==0) return false; |
309 | 352 | ||
310 | // KateView *kv = (KateView*) currentView; | 353 | // KateView *kv = (KateView*) currentView; |
311 | KateDocument *kd = (KateDocument*) currentView->document(); | 354 | KateDocument *kd = (KateDocument*) currentView->document(); |
312 | // odebug << "saving file "+kd->docName() << oendl; | 355 | // odebug << "saving file "+kd->docName() << oendl; |
313 | if( kd->docName().isEmpty()) | 356 | if( kd->isNewDoc()) { |
314 | slotSaveAs(); | 357 | return saveDocumentAs(); |
358 | } | ||
315 | else | 359 | else |
316 | kd->saveFile(); | 360 | return kd->saveFile(); |
361 | // FIXME check result of saveFile and show message if failed? | ||
317 | // kv->save(); | 362 | // kv->save(); |
318 | // kd->saveFile(); | 363 | // kd->saveFile(); |
319 | } | 364 | } |
320 | 365 | ||
321 | void TinyKate::slotSaveAs() | 366 | bool TinyKate::saveDocumentAs() |
322 | { | 367 | { |
323 | if (currentView==0) return; | 368 | if (currentView==0) return false; |
369 | |||
370 | bool result = false; | ||
324 | KateDocument *kd = (KateDocument*) currentView->document(); | 371 | KateDocument *kd = (KateDocument*) currentView->document(); |
325 | 372 | ||
326 | QString filename= OFileDialog::getSaveFileName(OFileSelector::EXTENDED_ALL, | 373 | QString filename= OFileDialog::getSaveFileName(OFileSelector::EXTENDED_ALL, |
@@ -329,13 +376,52 @@ void TinyKate::slotSaveAs() | |||
329 | { | 376 | { |
330 | odebug << "saving file "+filename << oendl; | 377 | odebug << "saving file "+filename << oendl; |
331 | QFileInfo fi(filename); | 378 | QFileInfo fi(filename); |
379 | if(fi.exists()) { | ||
380 | if ( QMessageBox::warning(this,tr("TinyKATE"), | ||
381 | tr("The file %1\n" | ||
382 | "already exists.\n\n" | ||
383 | "Are you sure you want to\n" | ||
384 | "overwrite it?") | ||
385 | .arg(fi.fileName()), | ||
386 | tr("Yes"),tr("No"),0,0,1) != 0) { | ||
387 | return false; | ||
388 | } | ||
389 | } | ||
332 | QString filenamed = fi.fileName(); | 390 | QString filenamed = fi.fileName(); |
333 | kd->setDocFile( filename); | 391 | kd->setDocFile( filename); |
334 | kd->setDocName( filenamed); | 392 | kd->setDocName( filenamed); |
335 | kd->saveFile(); | 393 | kd->setNewDoc(false); |
336 | // KTextEditor::View *dv = currentView; | 394 | result = kd->saveFile(); |
337 | // tabwidget->changeTab( dv, filenamed); | 395 | // FIXME check result of saveFile and show message if failed? |
338 | // need to change tab label here | 396 | tabwidget->changeTab( tabwidget->currentWidget(), "tinykate/tinykate", filenamed ); |
397 | } | ||
398 | return result; | ||
399 | } | ||
400 | |||
401 | void TinyKate::slotSave() | ||
402 | { | ||
403 | saveDocument(); | ||
404 | } | ||
405 | |||
406 | void TinyKate::slotSaveAs() | ||
407 | { | ||
408 | saveDocumentAs(); | ||
409 | } | ||
410 | |||
411 | void TinyKate::closeEvent(QCloseEvent *e) { | ||
412 | // Close all documents | ||
413 | shutDown = true; | ||
414 | while (currentView!=0) | ||
415 | { | ||
416 | if(!closeDocument()) { | ||
417 | // User cancelled | ||
418 | shutDown=false; | ||
419 | break; | ||
420 | } | ||
339 | } | 421 | } |
340 | 422 | ||
423 | if(shutDown) | ||
424 | e->accept(); | ||
425 | else | ||
426 | e->ignore(); | ||
341 | } | 427 | } |
diff --git a/noncore/apps/tinykate/mainwindow/tinykate.h b/noncore/apps/tinykate/mainwindow/tinykate.h index 6e95d87..7d0c588 100644 --- a/noncore/apps/tinykate/mainwindow/tinykate.h +++ b/noncore/apps/tinykate/mainwindow/tinykate.h | |||
@@ -48,6 +48,11 @@ protected slots: | |||
48 | void slotSaveAs(); | 48 | void slotSaveAs(); |
49 | protected: | 49 | protected: |
50 | void open(const QString&); | 50 | void open(const QString&); |
51 | bool closeDocument(); | ||
52 | bool checkSave(); | ||
53 | bool saveDocument(); | ||
54 | bool saveDocumentAs(); | ||
55 | void closeEvent(QCloseEvent *e); | ||
51 | private: | 56 | private: |
52 | QString currentFileName; | 57 | QString currentFileName; |
53 | Opie::Ui::OTabWidget *tabwidget; | 58 | Opie::Ui::OTabWidget *tabwidget; |