00001
00042 #include "aiview.hpp"
00043 #include <iostream>
00044
00045 using namespace AiFractals;
00046
00047 AiView::AiView(QWidget * parent)
00048 : QWidget( parent) ,m_image(0,0,QImage::Format_RGB32),
00049 m_scaleX(0), m_scaleY(0), m_scale(false), m_retation(0),m_isZooming(false),
00050 m_startX(0), m_startY(0), m_endX(0), m_endY(0), m_fill2Win(false)
00051 {
00052
00053 this->stack = new QUndoStack( this );
00054 }
00055
00056 void AiView::paintEvent ( QPaintEvent * event )
00057 {
00058 if ( !this->m_image.isNull() )
00059 {
00060 QPainter painter( this );
00061 QMatrix matrix;
00062
00063
00064 if( m_scaleX != .0 && m_scaleY != .0 )
00065 {
00066 static qreal scaleX = .0;
00067 static qreal scaleY = .0;
00068
00069 scaleX = m_scale ? m_clearScale ? m_scaleX : scaleX + m_scaleX : scaleX;
00070 scaleY = m_scale ? m_clearScale ? m_scaleY : scaleY + m_scaleY : scaleY;
00071
00072 matrix.scale(scaleX, scaleY);
00073 m_scale = m_clearScale = false;
00074 }
00075
00076 if( m_retation != .0 && m_retation <= 360. )
00077 {
00078
00079 matrix.rotate( m_retation );
00080 }
00081
00082
00083 else if( m_canRetate && m_retation == 361. )
00084 {
00085
00086 m_image = m_image.mirrored(true,false);
00087 m_canRetate = false ;
00088 }
00089 else if( m_canRetate && m_retation == 362. )
00090 {
00091
00092 m_image = m_image.mirrored(false,true);
00093 m_canRetate = false ;
00094 }
00095
00096 QImage tmp = this->m_image.transformed( matrix) ;
00097
00098 if( m_fill2Win )
00099 {
00100 painter.drawPixmap(0, 0, this->width(), this->height(), QPixmap::fromImage(tmp) ) ;
00101 }
00102 else
00103 {
00104 int x = ( this->width() / 2 ) - ( tmp.width() / 2 );
00105 int y = ( this->height() / 2 ) - ( tmp.height() / 2 );
00106 painter.drawPixmap(x, y, QPixmap::fromImage(tmp) ) ;
00107 }
00108
00109
00110 if( m_isZooming )
00111 {
00112 int x = ( this->width() / 2 ) - ( tmp.width() / 2 );
00113 int y = ( this->height() / 2 ) - ( tmp.height() / 2 );
00114
00115 if( m_startX >= x && m_startY >= y && m_endX <= x + tmp.width() && m_endY <= y + tmp.height() && m_endX > m_startX && m_endY > m_startY )
00116 {
00117 painter.setPen(QColor(255,255,255));
00118
00119 painter.drawRect(m_startX, m_startY, m_endX - m_startX , m_endY - m_startY );
00120 painter.drawPixmap(m_endX , m_endY , QPixmap(":/png/zoom-in"));
00121 }
00122 }
00123
00124
00125 painter.end();
00126 }
00127 }
00128
00129 void AiView::mouseMoveEvent ( QMouseEvent * event )
00130 {
00131 if( m_isZooming )
00132 {
00133 m_endX = event->x();
00134 m_endY = event->y();
00135 }
00136 this->update();
00137 }
00138
00139 void AiView::mousePressEvent ( QMouseEvent * event )
00140 {
00141 if( m_isZooming )
00142 {
00143 m_startX = event->x();
00144 m_startY = event->y();
00145 }
00146 }
00147
00148 void AiView::mouseReleaseEvent ( QMouseEvent * event )
00149 {
00150 if( m_isZooming )
00151 {
00152 int x = ( this->width() / 2 ) - ( m_image.width() / 2 );
00153 int y = ( this->height() / 2 ) - ( m_image.height() / 2 );
00154 m_endX = event->x();
00155 m_endY = event->y();
00156
00157 if( m_startX >= x && m_startY >= y && m_endX <= x + m_image.width() && m_endY <= y + m_image.height() && m_endX > m_startX && m_endY > m_startY )
00158 {
00159 emit zoneZooming( m_startX - x , m_startY - y, m_endX - x, m_endY - y );
00160 m_startX = m_endX = 0;
00161 }
00162 }
00163 }
00164
00165 void AiView::makeUpdate()
00166 {
00167 this->resize( widthView(m_image.width()) , heightView(m_image.height()) );
00168 this->update();
00169 }
00170
00171 void AiView::makeUpdate( QImage & image, bool undo )
00172 {
00173 m_image = image;
00174 this->resize( widthView(m_image.width()) , heightView(m_image.height()) );
00175 this->update();
00176
00177 if ( undo && !m_image.isNull() )
00178 {
00179 AiCommand * cmd = new AiCommand(m_image, this);
00180 this->stack->push( cmd );
00181 cmd->id( stack->count() - 1 );
00182 }
00183 }
00184
00185 void AiView::makeUpdate( QString & fileName )
00186 {
00187 m_image = QImage( fileName );
00188 this->resize( widthView(m_image.width()) , heightView(m_image.height()) );
00189 this->update();
00190
00191 if ( !m_image.isNull() )
00192 {
00193 AiCommand * cmd = new AiCommand(m_image, this);
00194 this->stack->push( cmd );
00195 cmd->id( stack->count() - 1 );
00196 }
00197 }
00198
00199 void AiView::makeUndoUpdate( QImage & image )
00200 {
00201 m_image = image;
00202 this->resize( widthView(m_image.width()) , heightView(m_image.height()) );
00203 this->update();
00204 emit enabledChilds( !m_image.isNull() );
00205 }
00206
00207 QImage & AiView::image()
00208 {
00209 return m_image;
00210 }
00211
00212 const bool AiView::emptyScreen()
00213 {
00214 return m_image.isNull();
00215 }
00216
00217 const qreal AiView::getScaleX() const
00218 {
00219 return m_scaleX;
00220 }
00221
00222 const qreal AiView::getScaleY() const
00223 {
00224 return m_scaleY;
00225 }
00226
00227 void AiView::clearScale()
00228 {
00229 if( !m_image.isNull() )
00230 {
00231 m_clearScale = true;
00232 }
00233 }
00234
00235 const qreal AiView::getRetation() const
00236 {
00237 return m_retation;
00238 }
00239
00240 const int AiView::getImageWidth()
00241 {
00242 return m_image.width();
00243 }
00244
00245 const int AiView::getImageHeight()
00246 {
00247 return m_image.height();
00248 }
00249
00250 void AiView::retate(qreal r)
00251 {
00252 if( !m_image.isNull() )
00253 {
00254 m_retation = r;
00255 m_canRetate = true;
00256 this->update();
00257 }
00258 }
00259
00260 void AiView::scale(qreal sx, qreal sy)
00261 {
00262 if( !m_image.isNull() )
00263 {
00264 m_scaleX = sx;
00265 m_scaleY = sy;
00266 m_scale = true;
00267 this->resize( widthView(m_image.width()) , heightView(m_image.height()) );
00268 this->update();
00269 }
00270 }
00271
00272 void AiView::saveAs( QString & fileName )
00273 {
00274 this->m_image.save( fileName );
00275 }
00276
00277 void AiView::print()
00278 {
00279 QPrintDialog printDialog( this );
00280 if( printDialog.exec() == QDialog::Accepted )
00281 {
00282 QPainter painter( &printDialog );
00283 QRect rect = painter.viewport();
00284 QSize size = m_image.size();
00285 size.scale( rect.size() ,Qt::KeepAspectRatio);
00286 painter.setViewport( rect.x(), rect.y(), size.width(), size.height() );
00287 painter.setWindow( m_image.rect() );
00288 painter.drawImage( 0, 0, m_image );
00289 }
00290 }
00291
00292 void AiView::clear()
00293 {
00294 m_image = QImage();
00295 this->update();
00296 }
00297
00298 int AiView::widthView(int w)
00299 {
00300 QWidget * wd = qobject_cast<QWidget *>( this->parent() );
00301 return w > wd->width() ? w : wd->width();
00302 }
00303
00304 int AiView::heightView(int h)
00305 {
00306 QWidget * wd = qobject_cast<QWidget *>( this->parent() );
00307 return h > wd->height() ? h : wd->height();
00308 }
00309
00310 void AiView::zooming(bool enable)
00311 {
00312 m_isZooming = enable;
00313 }
00314
00315 void AiView::fillToWindow()
00316 {
00317 m_fill2Win = true;
00318 this->update();
00319 }
00320
00321 void AiView::fillInWindow()
00322 {
00323 m_fill2Win = false;
00324 this->update();
00325 }
00326
00327 AiView::~AiView()
00328 {
00329 }