Das K Desktop Environment

A.6. kscribbledoc.cpp

   1 /***************************************************************************
   2                           kscribbledoc.cpp  -  description
   3                              -------------------
   4     begin                : Mon Jan 31 11:05:05 CET 2000
   5     copyright            : (C) 2000 by Ralf Nolden
   6     email                : Ralf.Nolden@post.rwth-aachen.de
   7  ***************************************************************************/
   8 
   9 /***************************************************************************
  10  *                                                                         *
  11  *   This program is free software; you can redistribute it and/or modify  *
  12  *   it under the terms of the GNU General Public License as published by  *
  13  *   the Free Software Foundation; either version 2 of the License, or     *
  14  *   (at your option) any later version.                                   *
  15  *                                                                         *
  16  ***************************************************************************/
  17 
  18 // include files for Qt
  19 #include <qdir.h>
  20 #include <qfileinfo.h>
  21 #include <qwidget.h>
  22 
  23 // include files for KDE
  24 #include <klocale.h>
  25 #include <kmessagebox.h>
  26 #include <kfiledialog.h>
  27 
  28 // application specific includes
  29 #include "kscribbledoc.h"
  30 #include "kscribble.h"
  31 #include "kscribbleview.h"
  32 
  33 
  34 KScribbleDoc::KScribbleDoc()
  35 {
  36   pViewList = new QList<KScribbleView>;
  37   pViewList->setAutoDelete(false);
  38 }
  39 
  40 KScribbleDoc::~KScribbleDoc()
  41 {
  42   delete pViewList;
  43 }
  44 
  45 void KScribbleDoc::addView(KScribbleView *view)
  46 {
  47   pViewList->append(view);
  48   changedViewList();
  49 }
  50 
  51 void KScribbleDoc::removeView(KScribbleView *view)
  52 {
  53     pViewList->remove(view);
  54     if(!pViewList->isEmpty())
  55       changedViewList();
  56     else
  57       deleteContents();
  58 }
  59 
  60 void KScribbleDoc::changedViewList(){  
  61   
  62   KScribbleView *w;
  63   if((int)pViewList->count() == 1){
  64     w=pViewList->first();
  65     w->setCaption(m_title);
  66   }
  67   else{  
  68     int i;
  69     for( i=1,w=pViewList->first(); w!=0; i++, w=pViewList->next())
  70       w->setCaption(QString(m_title+":%1").arg(i));  
  71   }
  72 }
  73 
  74 bool KScribbleDoc::isLastView() {
  75   return ((int) pViewList->count() == 1);
  76 }
  77 
  78 
  79 void KScribbleDoc::updateAllViews(KScribbleView *sender)
  80 {
  81   KScribbleView *w;
  82   for(w=pViewList->first(); w!=0; w=pViewList->next())
  83   {
  84       w->update(sender);
  85   }
  86 
  87 }
  88 
  89 void KScribbleDoc::setPathName(const QString &&;name)
  90 {
  91   m_filename=name;
  92   m_title=QFileInfo(name).fileName();
  93 }
  94 
  95 const QString&&; KScribbleDoc::pathName() const
  96 {
  97   return m_filename;
  98 }
  99 
 100 void KScribbleDoc::setTitle(const QString &&;title)
 101 {
 102   m_title=title;
 103 }
 104 
 105 const QString &&;KScribbleDoc::title() const
 106 {
 107   return m_title;
 108 }
 109 
 110 
 111 void KScribbleDoc::closeDocument()
 112 {
 113   KScribbleView *w;
 114   if(!isLastView())
 115   {
 116     for(w=pViewList->first(); w!=0; w=pViewList->next())
 117     {
 118         if(!w->close())
 119          break;
 120     }
 121   }
 122   if(isLastView())
 123   {
 124     w=pViewList->first();
 125     w->close();
 126   }
 127 }
 128 
 129 bool KScribbleDoc::newDocument()
 130 {
 131   /////////////////////////////////////////////////
 132   // TODO: Add your document initialization code here
 133   size=QSize(300,200 );
 134   pen=QPen( Qt::black, 3 );
 135   polyline=QPointArray(3);
 136   buffer.resize(size);
 137   buffer.fill( Qt::white );
 138   /////////////////////////////////////////////////
 139   modified=false;
 140   return true;
 141 }
 142 
 143 bool KScribbleDoc::openDocument(const QString &&;filename, const char *format /*=0*/)
 144 {
 145 
 146   QFile f( filename );
 147 //  if ( !f.open( IO_ReadOnly ) )
 148 //    return false;
 149   /////////////////////////////////////////////////
 150   // TODO: Add your document opening code here
 151   if(!buffer.load( filename, format ))
 152     return false;
 153   size=buffer.size();
 154   /////////////////////////////////////////////////
 155 //  f.close();
 156   
 157   modified=false;
 158   m_filename=filename;
 159   m_title=QFileInfo(f).fileName();
 160   return true;
 161 }
 162 
 163 bool KScribbleDoc::saveDocument(const QString &&;filename, const char *format /*=0*/)
 164 {
 165   QFile f( filename );
 166 //  if ( !f.open( IO_WriteOnly ) )
 167 //    return false;
 168 
 169   /////////////////////////////////////////////////
 170   // TODO: Add your document saving code here
 171   if(!buffer.save( filename, format ))
 172     return false;
 173   /////////////////////////////////////////////////
 174 
 175 //  f.close();
 176 
 177   modified=false;
 178   m_filename=filename;
 179   m_title=QFileInfo(f).fileName();
 180   return true;
 181 }
 182 
 183 void KScribbleDoc::deleteContents()
 184 {
 185   /////////////////////////////////////////////////
 186   // TODO: Add implementation to delete the document contents
 187   buffer.fill( Qt::white );
 188   /////////////////////////////////////////////////
 189 
 190 }
 191 
 192 bool KScribbleDoc::canCloseFrame(KScribbleView* pFrame)
 193 {
 194   if(!isLastView())
 195     return true;
 196       
 197   bool ret=false;
 198   if(isModified())
 199   {
 200     QString saveName;
 201     switch(KMessageBox::warningYesNoCancel(pFrame, i18n("The current file has been modified.\n"
 202     "Do you want to save it?"),title()))
 203     {
 204     case KMessageBox::Yes:
 205       if(title().contains(i18n("Untitled")))
 206       {
 207         saveName=KFileDialog::getSaveFileName(QDir::currentDirPath(),
 208                              i18n("*|All files"), pFrame, i18n("Save as..."));
 209         if(saveName.isEmpty())
 210         return false;
 211       }
 212       else
 213        saveName=pathName();
 214           
 215       if(!saveDocument(saveName))
 216       {
 217         switch(KMessageBox::warningYesNo(pFrame,i18n("Could not save the current document !\n"
 218                           "Close anyway ?"), i18n("I/O Error !")))
 219         {
 220           case KMessageBox::Yes:
 221             ret=true;
 222           case KMessageBox::No:
 223             ret=false;
 224         }                
 225       }
 226       else
 227         ret=true;
 228         break;
 229     case KMessageBox::No:
 230       ret=true;
 231       break;
 232     case KMessageBox::Cancel:
 233     default:
 234       ret=false;         
 235       break;
 236     }
 237   }
 238   else
 239     ret=true;
 240     
 241   return ret;
 242 }
 243 
 244 void KScribbleDoc::editClearAll()
 245 {
 246   deleteContents();
 247   setModified();
 248   updateAllViews(0);
 249 }