-rw-r--r-- | library/imageedit.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/library/imageedit.cpp b/library/imageedit.cpp new file mode 100644 index 0000000..7f98ebb --- a/dev/null +++ b/library/imageedit.cpp | |||
@@ -0,0 +1,97 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "imageedit.h" | ||
21 | #include <qpainter.h> | ||
22 | |||
23 | ImageEdit::ImageEdit( QWidget *parent = 0, const char *name = 0 ) | ||
24 | : QScrollView( parent, name, WNorthWestGravity | WResizeNoErase ), buffer() | ||
25 | { | ||
26 | buffer.resize( size() ); | ||
27 | buffer.fill( colorGroup().color( QColorGroup::Base ) ); | ||
28 | } | ||
29 | |||
30 | ImageEdit::~ImageEdit() | ||
31 | { | ||
32 | |||
33 | } | ||
34 | |||
35 | void ImageEdit::contentsMousePressEvent( QMouseEvent *e ) | ||
36 | { | ||
37 | lastPos = e->pos(); | ||
38 | } | ||
39 | |||
40 | void ImageEdit::contentsMouseMoveEvent( QMouseEvent *e ) | ||
41 | { | ||
42 | QPainter pw( viewport() ); | ||
43 | QPainter pb( &buffer ); | ||
44 | pb.drawLine( lastPos, e->pos() ); | ||
45 | pw.drawLine( contentsToViewport( lastPos ), | ||
46 | contentsToViewport( e->pos() ) ); | ||
47 | lastPos = e->pos(); | ||
48 | } | ||
49 | |||
50 | void ImageEdit::contentsMouseReleaseEvent( QMouseEvent * ) | ||
51 | { | ||
52 | } | ||
53 | |||
54 | void ImageEdit::viewportResizeEvent( QResizeEvent *e ) | ||
55 | { | ||
56 | enlargeBuffer(e->size()); | ||
57 | } | ||
58 | |||
59 | void ImageEdit::enlargeBuffer( const QSize& sz ) | ||
60 | { | ||
61 | QSize osz = buffer.size(); | ||
62 | QSize nsz( QMAX( osz.width(), sz.width() ), QMAX( osz.height(), sz.height() ) ); | ||
63 | buffer.resize( nsz.width(), nsz.height() ); | ||
64 | // clear new area | ||
65 | QPainter p( &buffer ); | ||
66 | if ( sz.width() > osz.width() ) | ||
67 | p.fillRect( osz.width(), 0, sz.width() - osz.width(), nsz.height(), colorGroup().color( QColorGroup::Base ) ); | ||
68 | if ( sz.height() > osz.height() ) | ||
69 | p.fillRect( 0, osz.height(), nsz.width(), sz.height() - osz.height(), colorGroup().color( QColorGroup::Base ) ); | ||
70 | p.end(); | ||
71 | } | ||
72 | |||
73 | void ImageEdit::drawContents( QPainter *p, int cx, int cy, int cw, int ch ) | ||
74 | { | ||
75 | p->drawPixmap( cx, cy, buffer, cx, cy, cw, ch ); | ||
76 | } | ||
77 | |||
78 | void ImageEdit::setPixmap( const QPixmap &pm ) | ||
79 | { | ||
80 | QSize osz = buffer.size(); | ||
81 | if ( pm.width() < osz.width() || pm.height() < osz.height() ) { | ||
82 | buffer.fill(white); | ||
83 | enlargeBuffer( pm.size() ); | ||
84 | QPainter p(&buffer); | ||
85 | p.drawPixmap(0,0,pm); | ||
86 | } else { | ||
87 | buffer = pm; | ||
88 | } | ||
89 | resizeContents( buffer.width(), buffer.height() ); | ||
90 | viewport()->repaint( FALSE ); | ||
91 | } | ||
92 | |||
93 | QPixmap ImageEdit::pixmap() const | ||
94 | { | ||
95 | return buffer; | ||
96 | } | ||
97 | |||