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
|
// Copyright (c) 2002-2004 INRIA Sophia-Antipolis (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Qt_widget/include/CGAL/IO/Qt_widget_get_iso_rectangle.h $
// $Id: Qt_widget_get_iso_rectangle.h 32795 2006-07-30 16:38:54Z spion $
//
//
// Author(s) : Radu Ursu
#ifndef CGAL_QT_WIDGET_GET_ISO_RECTANGLE_H
#define CGAL_QT_WIDGET_GET_ISO_RECTANGLE_H
#include <CGAL/IO/Qt_widget.h>
#include <CGAL/IO/Qt_widget_layer.h>
#include <qrect.h>
#include <qcursor.h>
#ifndef CGAL_QT_LEFT_BUTTON
#define CGAL_QT_LEFT_BUTTON Qt::LeftButton
#endif
namespace CGAL {
template <class T>
class Qt_widget_get_iso_rectangle : public Qt_widget_layer
{
private:
QCursor cursor;
QCursor oldcursor;
public:
int first_x, first_y, x2, y2;
bool widgetrepainted;
bool on_first;
QWidget::FocusPolicy oldpolicy;
typedef typename T::Iso_rectangle_2 Iso_rectangle_2;
typedef typename T::RT RT;
Qt_widget_get_iso_rectangle(const QCursor
c=QCursor(Qt::crossCursor),
QObject* parent = 0, const char* name = 0)
: Qt_widget_layer(parent, name), cursor(c), widgetrepainted(true),
on_first(false) {};
void draw(){
widgetrepainted = true;
};
protected:
bool is_pure(Qt::ButtonState s){
if((s & Qt::ControlButton) ||
(s & Qt::ShiftButton) ||
(s & Qt::AltButton))
return 0;
else
return 1;
}
void mousePressEvent(QMouseEvent *e)
{
if(e->button() == CGAL_QT_LEFT_BUTTON
&& is_pure(e->state()))
{
if (!on_first)
{
first_x = e->x();
first_y = e->y();
on_first = true;
} else {
if((e->x() != first_x) && (e->y() != first_y)) {
RT x, y, xfirst2, yfirst2;
widget->x_real(e->x(), x);
widget->y_real(e->y(), y);
widget->x_real(first_x, xfirst2);
widget->y_real(first_y, yfirst2);
RT xmin, xmax, ymin, ymax;
if(x < xfirst2) {xmin = x; xmax = xfirst2;}
else {xmin = xfirst2; xmax = x;};
if(y < yfirst2) {ymin = y; ymax = yfirst2;}
else {ymin = yfirst2; ymax = y;};
widget->new_object(
make_object(Iso_rectangle_2(xmin, ymin,
xmax, ymax)));
on_first = false;
widgetrepainted = true;
}
}
}
};
void mouseMoveEvent(QMouseEvent *e)
{
if(on_first)
{
int x = e->x();
int y = e->y();
*widget << noFill;
RasterOp old = widget->rasterOp(); //save the initial raster mode
QColor old_color=widget->color();
widget->setRasterOp(XorROP);
widget->lock();
widget->setColor(Qt::green);
if(!widgetrepainted)
widget->get_painter().drawRect(first_x, first_y,
x2 - first_x, y2 - first_y);
widget->get_painter().drawRect(first_x, first_y, x - first_x,
y - first_y);
widget->unlock();
widget->setColor(old_color);
widget->setRasterOp(old);
//save the last coordinates to redraw the screen
x2 = x;
y2 = y;
widgetrepainted = false;
}
};
void keyPressEvent(QKeyEvent *e)
{
switch ( e->key() ) {
case Key_Escape: // key_escape
if (on_first)
{
widget->lock();
*widget << noFill;
RasterOp old = widget->rasterOp(); //save the initial raster mode
QColor old_color=widget->color();
widget->setRasterOp(XorROP);
*widget << CGAL::GREEN;
if(!widgetrepainted)
widget->get_painter().drawRect(first_x, first_y,
x2 - first_x, y2 - first_y);
widget->setColor(old_color);
widget->setRasterOp(old);
widgetrepainted = true;
widget->unlock();
on_first = false;
}
break;
}//endswitch
}
void leaveEvent(QEvent *)
{
if (on_first)
{
widget->lock();
*widget << noFill;
RasterOp old = widget->rasterOp(); //save the initial raster mode
QColor old_color=widget->color();
widget->setRasterOp(XorROP);
*widget << CGAL::GREEN;
if(!widgetrepainted)
widget->get_painter().drawRect(first_x, first_y,
x2 - first_x, y2 - first_y);
widget->setColor(old_color);
widget->setRasterOp(old);
widgetrepainted = true;
widget->unlock();
}
}
void activating()
{
oldpolicy = widget->focusPolicy();
widget->setFocusPolicy(QWidget::StrongFocus);
oldcursor = widget->cursor();
widget->setCursor(cursor);
widgetrepainted = true;
};
void deactivating()
{
widget->setCursor(oldcursor);
widget->setFocusPolicy(oldpolicy);
on_first = false;
};
};//end class
} // namespace CGAL
#endif
|