00001
00007 #include <QtGui/qpainter.h>
00008 #include <QtGui/qmessagebox.h>
00009 #include <QtGui/qevent.h>
00010
00011 #include <iostream>
00012 #include "visu2d.h"
00013 #include "palette.h"
00014 #include "selection.h"
00015 #include "shape.h"
00016 #include "image.h"
00017
00018
00019
00020
00021 Visu2D::Visu2D(QWidget *parent)
00022 : QWidget(parent)
00023 {
00024 m_pImage = NULL;
00025 m_selection = NULL;
00026 m_tmpShape = NULL;
00027 m_visuScaleFactor = 2;
00028
00029 setFocusPolicy(Qt::StrongFocus);
00030 setMouseTracking(TRUE);
00031
00032 setMinimumSize(512, 512);
00033 }
00034
00035 Visu2D::~Visu2D()
00036 {
00037 if (m_selection) {
00038 delete m_selection;
00039 m_selection = 0;
00040 }
00041 if (m_tmpShape) {
00042 delete m_tmpShape;
00043 m_tmpShape = 0;
00044 }
00045 }
00046
00047 void Visu2D::paintEvent(QPaintEvent*)
00048 {
00049 QPainter p(this);
00050 p.drawPixmap(0, 0, m_pxScaled);
00051 DrawShapes(&p);
00052 }
00053
00054 void Visu2D::DrawShapes(QPainter *p)
00055 {
00056 if (m_selection) {
00057 p->setPen(Qt::red);
00058 m_selection->Draw(p, m_visuScaleFactor);
00059 }
00060 if (m_tmpShape) {
00061 p->setPen(Qt::blue);
00062 m_tmpShape->Draw(p, m_visuScaleFactor);
00063 }
00064 }
00065
00066 void Visu2D::SetShapeType(SHAPE_TYPE type)
00067 {
00068 m_shapeType = type;
00069 }
00070
00071 void Visu2D::UpdateDataAndPixmap(Image* im, Palette *pal)
00072 {
00073 m_pImage = im;
00074
00075 if (!m_pImage || !pal) {
00076 return;
00077 }
00078
00079
00080 setFixedSize(m_pImage->GetSizeX()*m_visuScaleFactor,
00081 m_pImage->GetSizeY()*m_visuScaleFactor);
00082
00083
00084 QImage *pQImage = new QImage(m_pImage->GetSizeX(),
00085 m_pImage->GetSizeY(),
00086 QImage::Format_Indexed8);
00087
00088
00089 pQImage->setNumColors(NB_COLORS);
00090 for (int i = 0; i < pQImage->numColors(); i++) {
00091 pQImage->setColor(i, pal->GetColor(i).rgb());
00092 }
00093
00094
00095 uchar *ptr;
00096 for (int y = 0; y < pQImage->height(); y++) {
00097 ptr = pQImage->scanLine(y);
00098 for (int x = 0; x < pQImage->width(); x++) {
00099 *ptr++ = pal->DataToIndex(m_pImage->GetData(x, y));
00100 }
00101 }
00102
00103
00104 QPixmap px;
00105 px = px.fromImage(*pQImage);
00106
00107 delete pQImage;
00108
00109
00110 QMatrix matrix;
00111 matrix.scale((double)m_visuScaleFactor, (double)m_visuScaleFactor);
00112
00113
00114 m_pxScaled = px.transformed(matrix);
00115
00116
00117 update();
00118 }
00119
00120 void Visu2D::mouseMoveEvent(QMouseEvent *e)
00121 {
00122 HandleMouse(MOVE, e);
00123 }
00124
00125 void Visu2D::mousePressEvent(QMouseEvent *e)
00126 {
00127 HandleMouse(PRESS, e);
00128 }
00129
00130 void Visu2D::mouseReleaseEvent(QMouseEvent *e)
00131 {
00132 HandleMouse(RELEASE, e);
00133 }
00134
00135 void Visu2D::HandleMouse(int type, QMouseEvent *e)
00136 {
00137
00138 if (!m_pImage) {
00139 return;
00140 }
00141
00142
00143
00144 int xData = e->x() / m_visuScaleFactor;
00145 int yData = e->y() / m_visuScaleFactor;
00146
00147 if (!m_pImage->IsPointInside(xData, yData)) {
00148 return;
00149 }
00150
00151 SetTextInfo(xData, yData);
00152
00153 if (e->button() & Qt::LeftButton ||
00154 e->buttons() & Qt::LeftButton) {
00155 GenerateShape(type, xData, yData);
00156 }
00157 }
00158
00159 void Visu2D::SetTextInfo(int xData, int yData)
00160 {
00161 QString txt;
00162 if (m_pImage->IsPointInside(xData, yData)) {
00163 txt.sprintf("(%d, %d) : %d",
00164 xData,
00165 yData,
00166 m_pImage->GetData(xData, yData));
00167 }
00168 emit(UpdateInfoBar(txt));
00169 }
00170
00171 void Visu2D::GenerateShape(int type, int xData, int yData)
00172 {
00173 switch (type) {
00174 case PRESS :
00175 if (!m_selection) {
00176 m_selection = new Selection;
00177 }
00178 m_tmpShape = m_selection->AllocateShape(m_shapeType);
00179 if (m_tmpShape == NULL) {
00180 QMessageBox msgBox;
00181 msgBox.setText("Visu2D::GenerateShape\n Warning : No shape allocated !");
00182 msgBox.exec();
00183 return;
00184 }
00185 m_tmpShape->Init(xData, yData);
00186 break;
00187
00188 case MOVE :
00189 if (!m_tmpShape) {
00190 return;
00191 }
00192 m_tmpShape->ModifyEndShape(xData, yData);
00193 break;
00194
00195 case RELEASE :
00196 if (!m_tmpShape) {
00197 return;
00198 }
00199 if (m_tmpShape->Validate()) {
00200 m_selection->HandleNewShape(m_tmpShape);
00201 } else {
00202 delete m_tmpShape;
00203 }
00204 m_tmpShape = NULL;
00205 emit(UpdateStats(m_selection));
00206 break;
00207 }
00208 update();
00209 }
00210
00211 void Visu2D::SaveSelection()
00212 {
00213 if (m_selection == NULL) {
00214 QMessageBox msgBox;
00215 msgBox.setText("Visu2D::SaveSelection\n Warning : No selection allocated (check the value of m_selection)");
00216 msgBox.exec();
00217 return;
00218 }
00219
00220 }
00221
00222 void Visu2D::ReadSelection()
00223 {
00224 if (m_selection == NULL) {
00225 QMessageBox msgBox;
00226 msgBox.setText("Visu2D::SaveSelection\n Warning : No selection allocated (check the value of m_selection)");
00227 msgBox.exec();
00228 return;
00229 }
00230
00231 }