summaryrefslogtreecommitdiffstats
path: root/kdejava/koala/examples/kscribble/KScribbleView.java
blob: 17b80ff7ef1f1c3f3e54ba48f088f30c34586aaf (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import org.kde.qt.*;
import org.kde.koala.*;

/** The KScribbleView class provides the view widget for the KScribble instance.
 * The View instance inherits TQWidget as a base class and represents the view object of a TDEMainWindow. As KScribbleView is part of the
 * docuement-view model, it needs a reference to the document object connected with it by the KScribble class to manipulate and display
 * the document structure provided by the KScribbleDoc class.
 *
 * @author Source Framework Automatically Generated by KDevelop, (c) The KDevelop Team.
 * @version KDevelop version 0.4 code generation
 */
public class KScribbleView extends TQScrollView {

   private TQClipboard cb;
   private KScribbleDoc doc;
   private static final int IDLE = 0;
   private static final int DRAW = 1;
   private static final int SELECT = 2;
   private static final int PASTE = 3;
   private static final int DRAG = 4;

   private TQPixmap tmp = new TQPixmap();

   private int action = IDLE;
   private TQRect select = new TQRect();

   private KIconLoader k = new KIconLoader();
   private TQImageDrag qid;

   public KScribbleView(KScribbleDoc pDoc, TQWidget parent, String name, int wflags) {
      super(parent, name,wflags | WPaintClever | WNorthWestGravity | WRepaintNoErase);
      setBackgroundMode(PaletteBase);
      cb = TQApplication.clipboard();
      viewport().setAcceptDrops(true);
      setDragAutoScroll(true);
      doc=pDoc;
      action=IDLE;
      viewport().setCursor( Qt.crossCursor() );

      TQSize size=doc.docSize();
      resizeContents(size.width(), size.height());
      resize(size);

   }

   void update(KScribbleView pSender){
	if(pSender != this)
         viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
   }

   public KScribbleDoc getDocument() {
      return doc;
   }

   protected void keyPressEvent( TQKeyEvent e )  {
      switch (e.key()) {
      case Key_Right:
         scrollBy( 10, 0 );
         break;
      case Key_Left:
         scrollBy( -10,0);
         break;
      case Key_Up:
         scrollBy( 0, -10 );
         break;
      case Key_Down:
         scrollBy( 0, 10 );
         break;
      case Key_Home:
         setContentsPos(0,0);
         break;
      case Key_End:
         setContentsPos(0,viewport().height()-viewport().height());
         break;
      case Key_PageUp:
         scrollBy( 0, -viewport().height() );
         break;
      case Key_PageDown:
         scrollBy( 0, viewport().height() );
         break;
      }

   }

   /** cuts out a selection */
   void cutSelection() {
      select=select.normalize();
      TQPixmap cb_pix = new TQPixmap();
      cb_pix.resize(select.size());
      // copy selection to cb_pix and copy to clipboard
      bitBlt(cb_pix,0, 0, doc.buffer, select.x()+contentsX(), select.y()+contentsY(), cb_pix.width(),cb_pix.height());
      cb.setPixmap(cb_pix);
      // fill cb_pix with white and copy to selection area
      cb_pix.fill(Qt.white());
      bitBlt(doc.buffer, select.x()+contentsX(), select.y()+contentsY(),cb_pix, 0, 0, cb_pix.width(), cb_pix.height());
      action = IDLE;
      doc.setModified();
      doc.updateAllViews(this);
      viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
   }

   /** pastes the clipboard contents to a selection that can be inserted into the picture */
   void pasteSelection(){
      select=cb.pixmap().rect();
      action = PASTE;
      viewport().setCursor( Qt.sizeAllCursor() );
   }

   /** copies a selection to the clipboard */
   void copySelection(){
      select=select.normalize();
      TQPixmap cb_pix = new TQPixmap();
      cb_pix.resize(select.size());
      // copy selection to cb_pix and copy to clipboard
      bitBlt(cb_pix, 0, 0,doc.buffer, select.x()+contentsX(), select.y()+contentsY(), cb_pix.width(),cb_pix.height());
      cb.setPixmap(cb_pix);
      action = IDLE;
      viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
   }

   public void print(TQPrinter pPrinter) {
      if (pPrinter.setup(this)) {
         TQPainter p = new TQPainter();
         p.begin(pPrinter);

         ///////////////////////////////
         // TODO: add your printing code here
         p.drawPixmap(0,0,doc.buffer);
         ///////////////////////////////
         p.end();
      }
   }

   protected void viewportMousePressEvent( TQMouseEvent e ) {
      if ( e.button() == LeftButton && action == IDLE) {
       action=DRAW;
       doc.polyline.setPoint(0,viewportToContents(e.pos()));
       doc.polyline.setPoint(1,viewportToContents(e.pos()));
       doc.polyline.setPoint(2,viewportToContents(e.pos()));
       doc.updateAllViews(this);
      }
      else if ( e.button() == RightButton && action == IDLE) {
         action = SELECT;
         TQPoint pt=e.pos();
         int x = pt.x() > contentsWidth() ? contentsWidth() : pt.x();
         int y = pt.y() > contentsHeight() ? contentsHeight() : pt.y();
         select.setLeft(x-1);
         select.setTop(y-1);
         select.setRight(x-1);
         select.setBottom(y-1);
         }
         else if( action == SELECT ) {
            action = IDLE;

            select=select.normalize();
            // drag
            if(select.contains(e.pos(), true)) { // point inside the selection
               tmp.resize(select.size());
               bitBlt(tmp, 0, 0, doc.buffer, select.x()+contentsX(), select.y()+contentsY(), tmp.width(),tmp.height());
               TQImage img =tmp.convertToImage();
               TQDragObject d = new TQImageDrag( img, viewport(), "" );
               d.setPixmap(KDE.BarIcon("filenew"));
               d.drag();
            }
            // remove selection
            else
               viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
            }
         else if( action == PASTE ) {
            if ( e.button() == RightButton ) {
               action = IDLE;
               viewport().setCursor( Qt.crossCursor() );
            }
            TQPoint mv_pt = new TQPoint(viewport().height(), viewport().width());
            if(new TQRect(0,0,mv_pt.x(),mv_pt.y()).contains(e.pos()))
               select.moveCenter(e.pos());
            else {
               select.moveBottomRight(mv_pt);
            }
            viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
         }
   }

   protected void viewportMouseReleaseEvent( TQMouseEvent e ) {
     if ( action == DRAW )      {
        action = IDLE;
        doc.updateAllViews(this);
     }
     if ( action == SELECT) {
        TQPoint pt=e.pos();
        int x = pt.x() > 0 ? pt.x() : 0;
        int y = pt.y() > 0 ? pt.y() : 0;
        select.setRight(x);
        select.setBottom(y);
        TQSize size=doc.docSize();
        select = select.intersect(new TQRect(0,0,size.width(), size.height()));
     }
   }

   /** On paste actions inserts the pasted clipboard contents
    */
   protected void viewportMouseDoubleClickEvent(TQMouseEvent e) {
     if( action == PASTE ) {
         action = IDLE;
         select.moveCenter(e.pos());
         viewport().setCursor( Qt.crossCursor() );
         TQPixmap cb_pix = new TQPixmap();
         cb_pix.resize(cb.pixmap().size());
         cb_pix=cb.pixmap();
         bitBlt( doc.buffer,contentsX()+select.x(), contentsY()+select.y(),cb_pix, 0, 0, select.width(), select.height());
         viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
         doc.setModified();
         doc.updateAllViews(this);
      }

   }

   protected void viewportMouseMoveEvent( TQMouseEvent e ) {
      if ( action == DRAW ) {
         TQPainter painter = new TQPainter();
         painter.begin( doc.buffer );
         painter.setPen( doc.currentPen() );
         doc.polyline.setPoint(2, doc.polyline.at(1));
         doc.polyline.setPoint(1, doc.polyline.at(0));
         doc.polyline.setPoint(0, viewportToContents(e.pos()));
         painter.drawPolyline( doc.polyline );
         painter.end();

         TQRect r = doc.polyline.boundingRect();
         r = r.normalize();
         r.setLeft( r.left() - doc.penWidth() );
         r.setTop( r.top() - doc.penWidth() );
         r.setRight( r.right() + doc.penWidth() );
         r.setBottom( r.bottom() + doc.penWidth() );
         
		 bitBlt(viewport(), r.x()-contentsX(), r.y()-contentsY(), doc.buffer, r.x(), r.y(), r.width(),r.height());
         doc.setModified();
         doc.updateAllViews(this);
      }
      if ( action == SELECT ) {
         TQPoint pt=e.pos();
         select.setWidth(select.x()+pt.x());
         select.setHeight(select.y()+pt.y());
         select.setRight(pt.x());
         select.setBottom(pt.y());
         TQSize size=doc.docSize();
         select = select.intersect(new TQRect(0,0,size.width(), size.height()));
         viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
      }
      if( action == PASTE ) {
         TQPoint mv_pt = new TQPoint(viewport().height(), viewport().width());
         if(new TQRect(0,0,mv_pt.x(),mv_pt.y()).contains(e.pos()))
            select.moveCenter(e.pos());
         else {
            select.moveBottomRight(mv_pt);
         }
         TQRect pm_rect=cb.pixmap().rect();
         select.setWidth(pm_rect.width());
         select.setHeight(pm_rect.height());
         TQSize size=doc.docSize();
         select = select.intersect(new TQRect(0,0,size.width(), size.height()));
         viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
         doc.setModified();
         doc.updateAllViews(this);
      }
   }

   //void KScribbleView::viewportResizeEvent( TQResizeEvent *e )
   //{
   //}

   protected void viewportPaintEvent( TQPaintEvent e ) {
     bitBlt( viewport(),0, 0, doc.buffer, contentsX(), contentsY(), doc.buffer.width(),doc.buffer.width());

     if( action == PASTE )
     {
       tmp.resize(cb.pixmap().size());
       tmp=cb.pixmap();
     }
     if( action == PASTE || action == DRAG )
     {
       TQSize size=doc.docSize();
       select = select.intersect(new TQRect(0,0,size.width(), size.height()));
       if(select.intersects(e.rect()))
        bitBlt(viewport(), select.x(), select.y(), tmp, 0, 0, select.width(), select.height());
     }
      if( action == PASTE || action == DRAG || action == SELECT ) {
         if(select.intersects(e.rect())) {
            TQPainter paint_area = new TQPainter();
            paint_area.begin(viewport());
            paint_area.setPen(new TQPen(Qt.black(), 0, DashLine));
            paint_area.drawRect( select );
            paint_area.end();
         }
     }

      // not implemented for TQScrollView yet
//      super.viewportPaintEvent(e);
   }

   protected void  viewportDragEnterEvent ( TQDragEnterEvent e) {
      e.accept(TQImageDrag.canDecode(e));
      action = DRAG;
   }

   protected void  viewportDragMoveEvent ( TQDragMoveEvent e) {
      TQImage img = new TQImage();

      if ( TQImageDrag.canDecode(e) ){
      	 TQImageDrag.decode(e, img);
         tmp.resize(img.size());
         tmp.convertFromImage(img);
         select.setWidth(tmp.width());
         select.setHeight(tmp.height());
         select.moveCenter(e.pos());
         viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
      }
   }

   protected void  viewportDragLeaveEvent ( TQDragLeaveEvent e ) {
      action = IDLE;
      viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
   }

   protected void  viewportDropEvent ( TQDropEvent e) {
     TQImage img = new TQImage();
      if ( TQImageDrag.canDecode(e) ) {
         TQImageDrag.decode(e, img);
         tmp.resize(img.size());
         tmp.convertFromImage(img);
         select.setWidth(tmp.width());
         select.setHeight(tmp.height());
         select.moveCenter(e.pos());
         bitBlt(doc.buffer, select.x()+contentsX(), select.y()+contentsY(), tmp,0,0,tmp.width(), tmp.height());
         doc.setModified();
         doc.updateAllViews(this);
      }
      action = IDLE;
      viewport().repaint(0,0,visibleWidth(), visibleHeight(), false);
   }

}