• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • widgets
thumbimage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  thumbimage.cpp - description
3  -------------------
4  begin : Fri 09 Dec 2005
5  copyright : (C) 2005 by Jason Harris and Jasem Mutlaq
6  email : kstars@30doradus.org
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 "thumbimage.h"
19 
20 #include <QMouseEvent>
21 #include <QPainter>
22 #include <QPen>
23 
24 QPixmap ThumbImage::croppedImage() {
25  return Image->copy( *CropRect );
26 }
27 
28 ThumbImage::ThumbImage( QWidget *parent, const char *name ) : QLabel( parent )
29 {
30  //FIXME: Deprecated? Maybe we don't need this, since double-buffering is now built in
31  // setBackgroundMode( Qt::WA_NoBackground );
32 
33  setObjectName( name );
34  bMouseButtonDown = false;
35  bTopLeftGrab = false;
36  bTopRightGrab = false;
37  bBottomLeftGrab = false;
38  bBottomRightGrab = false;
39  HandleSize = 10;
40 
41  CropRect = new QRect();
42  Anchor = new QPoint();
43  Image = new QPixmap();
44 }
45 
46 ThumbImage::~ThumbImage()
47 {}
48 
49 void ThumbImage::paintEvent( QPaintEvent* ) {
50  QPainter p;
51  p.begin( this );
52 
53  p.drawPixmap( 0, 0, *Image );
54 
55  p.setPen( QPen( QColor( "Grey" ), 2 ) );
56  p.drawRect( *CropRect );
57 
58  p.setPen( QPen( QColor( "Grey" ), 0 ) );
59  p.drawRect( QRect( CropRect->left(), CropRect->top(),
60  HandleSize, HandleSize ) );
61  p.drawRect( QRect( CropRect->right() - HandleSize, CropRect->top(),
62  HandleSize, HandleSize ) );
63  p.drawRect( QRect( CropRect->left(), CropRect->bottom() - HandleSize,
64  HandleSize, HandleSize ) );
65  p.drawRect( QRect( CropRect->right() - HandleSize, CropRect->bottom() - HandleSize,
66  HandleSize, HandleSize ) );
67 
68  if ( CropRect->x() > 0 )
69  p.fillRect( 0, 0, CropRect->x(), height(),
70  QBrush( QColor("white"), Qt::Dense3Pattern ) );
71  if ( CropRect->right() < width() )
72  p.fillRect( CropRect->right(), 0, (width() - CropRect->right()), height(),
73  QBrush( QColor("white"), Qt::Dense3Pattern ) );
74  if ( CropRect->y() > 0 )
75  p.fillRect( 0, 0, width(), CropRect->y(),
76  QBrush( QColor("white"), Qt::Dense3Pattern ) );
77  if ( CropRect->bottom() < height() )
78  p.fillRect( 0, CropRect->bottom(), width(), (height() - CropRect->bottom()),
79  QBrush( QColor("white"), Qt::Dense3Pattern ) );
80 
81  p.end();
82 }
83 
84 void ThumbImage::mousePressEvent( QMouseEvent *e ) {
85  if ( e->button() == Qt::LeftButton && CropRect->contains( e->pos() ) ) {
86  bMouseButtonDown = true;
87 
88  //The Anchor tells how far from the CropRect corner we clicked
89  Anchor->setX( e->x() - CropRect->left() );
90  Anchor->setY( e->y() - CropRect->top() );
91 
92  if ( e->x() <= CropRect->left() + HandleSize && e->y() <= CropRect->top() + HandleSize ) {
93  bTopLeftGrab = true;
94  }
95  if ( e->x() <= CropRect->left() + HandleSize && e->y() >= CropRect->bottom() - HandleSize ) {
96  bBottomLeftGrab = true;
97  Anchor->setY( e->y() - CropRect->bottom() );
98  }
99  if ( e->x() >= CropRect->right() - HandleSize && e->y() <= CropRect->top() + HandleSize ) {
100  bTopRightGrab = true;
101  Anchor->setX( e->x() - CropRect->right() );
102  }
103  if ( e->x() >= CropRect->right() - HandleSize && e->y() >= CropRect->bottom() - HandleSize ) {
104  bBottomRightGrab = true;
105  Anchor->setX( e->x() - CropRect->right() );
106  Anchor->setY( e->y() - CropRect->bottom() );
107  }
108  }
109 }
110 
111 void ThumbImage::mouseReleaseEvent( QMouseEvent * ) {
112  if ( bMouseButtonDown ) bMouseButtonDown = false;
113  if ( bTopLeftGrab ) bTopLeftGrab = false;
114  if ( bTopRightGrab ) bTopRightGrab = false;
115  if ( bBottomLeftGrab ) bBottomLeftGrab = false;
116  if ( bBottomRightGrab ) bBottomRightGrab = false;
117 }
118 
119 void ThumbImage::mouseMoveEvent( QMouseEvent *e ) {
120  if ( bMouseButtonDown ) {
121 
122  //If a corner was grabbed, we are resizing the box
123  if ( bTopLeftGrab ) {
124  if ( e->x() >= 0 && e->x() <= width() ) CropRect->setLeft( e->x() - Anchor->x() );
125  if ( e->y() >= 0 && e->y() <= height() ) CropRect->setTop( e->y() - Anchor->y() );
126  if ( CropRect->left() < 0 ) CropRect->setLeft( 0 );
127  if ( CropRect->top() < 0 ) CropRect->setTop( 0 );
128  if ( CropRect->width() < 200 ) CropRect->setLeft( CropRect->left() - 200 + CropRect->width() );
129  if ( CropRect->height() < 200 ) CropRect->setTop( CropRect->top() - 200 + CropRect->height() );
130  } else if ( bTopRightGrab ) {
131  if ( e->x() >= 0 && e->x() <= width() ) CropRect->setRight( e->x() - Anchor->x() );
132  if ( e->y() >= 0 && e->y() <= height() ) CropRect->setTop( e->y() - Anchor->y() );
133  if ( CropRect->right() > width() ) CropRect->setRight( width() );
134  if ( CropRect->top() < 0 ) CropRect->setTop( 0 );
135  if ( CropRect->width() < 200 ) CropRect->setRight( CropRect->right() + 200 - CropRect->width() );
136  if ( CropRect->height() < 200 ) CropRect->setTop( CropRect->top() - 200 + CropRect->height() );
137  } else if ( bBottomLeftGrab ) {
138  if ( e->x() >= 0 && e->x() <= width() ) CropRect->setLeft( e->x() - Anchor->x() );
139  if ( e->y() >= 0 && e->y() <= height() ) CropRect->setBottom( e->y() - Anchor->y() );
140  if ( CropRect->left() < 0 ) CropRect->setLeft( 0 );
141  if ( CropRect->bottom() > height() ) CropRect->setBottom( height() );
142  if ( CropRect->width() < 200 ) CropRect->setLeft( CropRect->left() - 200 + CropRect->width() );
143  if ( CropRect->height() < 200 ) CropRect->setBottom( CropRect->bottom() + 200 - CropRect->height() );
144  } else if ( bBottomRightGrab ) {
145  if ( e->x() >= 0 && e->x() <= width() ) CropRect->setRight( e->x() - Anchor->x() );
146  if ( e->y() >= 0 && e->y() <= height() ) CropRect->setBottom( e->y() - Anchor->y() );
147  if ( CropRect->right() > width() ) CropRect->setRight( width() );
148  if ( CropRect->bottom() > height() ) CropRect->setBottom( height() );
149  if ( CropRect->width() < 200 ) CropRect->setRight( CropRect->right() + 200 - CropRect->width() );
150  if ( CropRect->height() < 200 ) CropRect->setBottom( CropRect->bottom() + 200 - CropRect->height() );
151  } else { //no corner grabbed; move croprect
152  CropRect->moveTopLeft( QPoint( e->x() - Anchor->x(), e->y() - Anchor->y() ) );
153  if ( CropRect->left() < 0 ) CropRect->moveLeft( 0 );
154  if ( CropRect->right() > width() ) CropRect->moveRight( width() );
155  if ( CropRect->top() < 0 ) CropRect->moveTop( 0 );
156  if ( CropRect->bottom() > height() ) CropRect->moveBottom( height() );
157  }
158 
159  emit cropRegionModified();
160  update();
161  }
162 }
163 
164 #include "thumbimage.moc"
QWidget
ThumbImage::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *e)
Definition: thumbimage.cpp:111
ThumbImage::cropRegionModified
void cropRegionModified()
ThumbImage::croppedImage
QPixmap croppedImage()
Definition: thumbimage.cpp:24
ThumbImage::ThumbImage
ThumbImage(QWidget *parent, const char *name=0)
Definition: thumbimage.cpp:28
thumbimage.h
ThumbImage::mousePressEvent
void mousePressEvent(QMouseEvent *e)
Definition: thumbimage.cpp:84
ThumbImage::paintEvent
void paintEvent(QPaintEvent *)
Definition: thumbimage.cpp:49
QLabel
ThumbImage::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e)
Definition: thumbimage.cpp:119
ThumbImage::~ThumbImage
~ThumbImage()
Definition: thumbimage.cpp:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal