summaryrefslogtreecommitdiffstats
path: root/tdejava/koala/examples/kscribble/KScribbleDoc.java
blob: 427db1948ec481f4de220d00657fe57d640820ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import java.util.*;

import org.trinitydesktop.qt.*;
import org.trinitydesktop.koala.*;

/**	KScribbleDoc provides a document object for a document-view model.
  *
  * The KScribbleDoc class provides a document object that can be used in conjunction with the classes JavaApiTestApp and KScribbleView
  * to create a document-view model for standard KDE applications based on TDEApplication and TDEMainWindow. Thereby, the document object
  * is created by the JavaApiTestApp instance and contains the document structure with the according methods for manipulation of the document
  * data by KScribbleView objects. Also, KScribbleDoc contains the methods for serialization of the document data from and to files.
  *
  * @author Source Framework Automatically Generated by KDevelop, (c) The KDevelop Team.
  * @version KDevelop version 1.2 code generation
  */
public class KScribbleDoc extends TQObject {

    /** the list of the views currently connected to the document */
   private ArrayList pViewList;
   private String m_title;
   private String m_filename;
   private TQSize size;
   private TQPen pen;
   public TQPointArray polyline;
   public KPixmap buffer;

	/** the modified flag of the current document */
   private boolean modified;
   private KURL doc_url;


   public KScribbleDoc() {

      pViewList = new ArrayList();
   }

   void addView(KScribbleView view) {
      pViewList.add(view);
	   changedViewList();
   }

   void removeView(KScribbleView view) {
        pViewList.remove(view);
        if(!pViewList.isEmpty())
            changedViewList();
         else
            deleteContents();
   }

   void changedViewList(){

      KScribbleView w;
      if(pViewList.size() == 1){
         w=(KScribbleView)pViewList.get(0);
         w.setCaption(m_title);
      }
      else{
         int i = 1;
         Iterator it = pViewList.iterator();
         while(it.hasNext()) {
            w = (KScribbleView)it.next();
            w.setCaption(m_title + ":"+ i++);

         }
      }
   }

   boolean isLastView() {
     return (pViewList.size() == 1);
   }


   void updateAllViews(KScribbleView sender) {
      KScribbleView w;
      Iterator it = pViewList.iterator();
      while(it.hasNext()) {
         w = (KScribbleView)it.next();
         w.update(sender);

      }

   }

   void setPathName(String name) {
      m_filename=name;
      m_title= new TQFileInfo(name).fileName();
   }

   String pathName() {
      return m_filename;
   }

   /** returns the current pen in use */
   TQPen currentPen() {
      return pen;
   }

   /** returns the pen color */
	int penWidth() {
      return pen.width();
   }

   /** returns the pen color */
   TQColor penColor(){
      return pen.color();
   }

   /** sets the pen width */
   void setPenWidth( int w ){
      pen.setWidth( w );
   }

	/** sets the pen color */
  	void setPenColor( TQColor c ){
      pen.setColor( c );
   }

   /** sets the pen style by a second toolbar */
//   void setPenStyle( PenStyle s) {
//      pen.setStyle(s);
//   }

   void setTitle(String title) {

     m_title=title;
   }

   String title()  {
     return m_title;
   }

  /** sets the pixmap contents. Used by KScribbleApp
    to create a new document by drop events */
   void setPixmap(KPixmap pix) {
      buffer=pix;
   };
   void resizeDocument(TQSize m_size) {
      size=m_size;
   };

   void closeDocument() {
      KScribbleView w;
      if(!isLastView()) {
         Iterator it = pViewList.iterator();
         while(it.hasNext()) {
            w = (KScribbleView)it.next();
            if (!w.close())
               break;

         }

      }
      if(isLastView()) {
         w= (KScribbleView)pViewList.get(0);
         w.close();
      }
   }

   boolean newDocument() {
     /////////////////////////////////////////////////
     // TODO: Add your document initialization code here
     size=new TQSize(300,200 );
     pen= new TQPen();
     pen.setColor(Qt.black());
     pen.setWidth(3);
     polyline= new TQPointArray(3);
     if (buffer == null) {
      buffer = new KPixmap();
     }
     buffer.resize(size);
     buffer.fill( Qt.white() );
     /////////////////////////////////////////////////
     modified=false;
     return true;
   }

   public boolean openDocument(String filename, String format) {

      TQFile f = new TQFile( filename );
   //	if ( !f.open( IO_ReadOnly ) )
   //		return false;
     /////////////////////////////////////////////////
     // TODO: Add your document opening code here
      if(!buffer.load( filename, format, 0))
         return false;
      size=buffer.size();
     /////////////////////////////////////////////////
   //	f.close();

     modified=false;
     m_filename=filename;
      m_title=new TQFileInfo(f).fileName();
     return true;
   }

   boolean saveDocument(String filename) {
      return saveDocument(filename,"") ;
   }

    /** returns the first view instance */
   KScribbleView firstView(){
      return (KScribbleView) pViewList.get(0);
   };

   boolean saveDocument(String filename, String format /*=0*/) {
      TQFile f = new TQFile( filename );
      //	if ( !f.open( IO_WriteOnly ) )
      //		return false;

      /////////////////////////////////////////////////
      // TODO: Add your document saving code here
      if(!buffer.save( filename, format ))
         return false;
      /////////////////////////////////////////////////

      //  f.close();

      modified=false;
      m_filename=filename;
      m_title=new TQFileInfo(f).fileName();
      return true;
   }

   void deleteContents() {
     /////////////////////////////////////////////////
     // TODO: Add implementation to delete the document contents
     buffer.fill( Qt.white() );
     /////////////////////////////////////////////////

   }

   boolean isModified() {
      return modified;
   }

   void setModified() {
      modified = true;
   }

   boolean canCloseFrame(KScribbleView pFrame) {
      if(!isLastView())
         return true;

      boolean ret=false;
      if(isModified()) {
         String saveName = new String();
         switch(KMessageBox.warningYesNoCancel(pFrame, i18n("The current file has been modified.\n" +
                             "Do you want to save it?"),title()))
         {
            case KMessageBox.Yes:
               if(title().indexOf(i18n("Untitled")) > 0) {
                  saveName= KFileDialog.getSaveFileName(TQDir.currentDirPath(),
                         i18n("*|All files"), pFrame, i18n("Save as..."));
                  if(saveName == null || saveName.length() == 0)
                     return false;
                  }
                  else
                     saveName=pathName();

                  if(!saveDocument(saveName)) {
                     switch(KMessageBox.warningYesNo(pFrame,i18n("Could not save the current document !\n" +
                                                                                          "Close anyway ?"), i18n("I/O Error !")))
                     {
                        case KMessageBox.Yes:
                           ret=true;
                        case KMessageBox.No:
                           ret=false;
                     }
               }
               else
                  ret=true;
               break;
            case KMessageBox.No:
               ret=true;
               break;
            case KMessageBox.Cancel:
            default:
               ret=false;
               break;
         }
      }
      else
         ret=true;

      return ret;
   }

    /** get the document size */
   TQSize docSize() {
      return size;
   }

   void editClearAll() {
     deleteContents();
     setModified();
     updateAllViews(null);

   }

}