1 /***************************************************************************
2     qgsgrassregion.h  -  Edit region
3                              -------------------
4     begin                : August, 2004
5     copyright            : (C) 2004 by Radim Blazek
6     email                : blazek@itc.it
7  ***************************************************************************/
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 #ifndef QGSGRASSREGION_H
17 #define QGSGRASSREGION_H
18 
19 #include "ui_qgsgrassregionbase.h"
20 #include "qgscoordinatereferencesystem.h"
21 #include "qgscoordinatetransform.h"
22 #include "qgsmaptool.h"
23 #include "qgsrubberband.h"
24 #include "qgspointxy.h"
25 
26 class QgsGrassPlugin;
27 class QgsGrassRegionEdit;
28 
29 class QgisInterface;
30 class QgsMapCanvas;
31 class QgsRectangle;
32 
33 class QButtonGroup;
34 
35 extern "C"
36 {
37 #include <grass/gis.h>
38 }
39 
40 /**
41  * \class QgsGrassRegion
42  *  \brief GRASS attributes.
43  *
44  */
45 class QgsGrassRegion: public QWidget, private Ui::QgsGrassRegionBase
46 {
47     Q_OBJECT
48 
49   public:
50     //! Constructor
51     QgsGrassRegion( QgisInterface *iface,
52                     QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags() );
53 
54 
55     ~QgsGrassRegion() override;
56 
57   public slots:
58     void buttonClicked( QAbstractButton *button );
59 
60     void mapsetChanged();
61 
62     void reloadRegion();
63 
64     //! Mouse event receiver
65     //void mouseEventReceiverMove ( QgsPointXY & );
66 
67     //! Mouse event receiver
68     //void mouseEventReceiverClick ( QgsPointXY & );
69 
70     //! Calculate region, called if any value is changed
71     void adjust( void );
72 
73     //! Value in GUI was changed
74     void northChanged();
75     void southChanged();
76     void eastChanged();
77     void westChanged();
78     void NSResChanged();
79     void EWResChanged();
80     void rowsChanged();
81     void colsChanged();
82 
83     void radioChanged( void );
84 
85     //! Called when the capture finished to refresh the mWindow values
86     void onCaptureFinished();
87 
88     void mDrawButton_clicked();
89 
90     void canvasMapToolSet( QgsMapTool *tool );
91   private:
92     //! Pointer to plugin
93     //QgsGrassPlugin *mPlugin;
94 
95     //! Pointer to QGIS interface
96     QgisInterface *mInterface = nullptr;
97 
98     //! Pointer to canvas
99     QgsMapCanvas *mCanvas = nullptr;
100 
101     QButtonGroup *mRadioGroup = nullptr;
102 
103     //! Current new region
104     struct Cell_head mWindow;
105 
106     QgsCoordinateReferenceSystem mCrs;
107 
108     //! Display current state of new region in XOR mode
109     void displayRegion( void );
110 
111     void readRegion();
112 
113 
114     // Set region values in GUI from mWindow
115     void refreshGui();
116 
117     //! First corner coordinates
118     double mX;
119     double mY;
120 
121     //! Currently updating GUI, don't run *Changed methods
122     bool mUpdatingGui;
123 
124     // Format N, S, E, W value
125     QString formatExtent( double v );
126     QString formatResolution( double v );
127 
128     QgsGrassRegionEdit *mRegionEdit = nullptr;
129 };
130 
131 //! Map tool which uses rubber band for changing grass region
132 class QgsGrassRegionEdit : public QgsMapTool
133 {
134     Q_OBJECT
135 
136   public:
137     explicit QgsGrassRegionEdit( QgsMapCanvas * );
138 
139     ~QgsGrassRegionEdit() override;
140 
141     //! mouse pressed in map canvas
142     void canvasPressEvent( QgsMapMouseEvent * ) override;
143 
144     //! mouse movement in map canvas
145     void canvasMoveEvent( QgsMapMouseEvent * ) override;
146 
147     //! mouse released
148     void canvasReleaseEvent( QgsMapMouseEvent * ) override;
149 
150 
151     //! called when map tool is about to get inactive
152     void deactivate() override;
153 
154     //! Gets the rectangle
155     QgsRectangle getRegion();
156 
157     //! refresh the rectangle displayed in canvas
158     void setRegion( const QgsPointXY &, const QgsPointXY & );
159     void setSrcRegion( const QgsRectangle &rect );
160 
161     static void drawRegion( QgsMapCanvas *canvas, QgsRubberBand *rubberBand, const QgsRectangle &rect, const QgsCoordinateTransform &coordinateTransform = QgsCoordinateTransform(), bool isPolygon = false );
162     void calcSrcRegion();
163     static void transform( QgsMapCanvas *canvas, QVector<QgsPointXY> &points, const QgsCoordinateTransform &coordinateTransform, Qgis::TransformDirection direction = Qgis::TransformDirection::Forward );
164 
165   signals:
166     void captureStarted();
167     void captureEnded();
168 
169   public slots:
170     void setTransform();
171 
172   private:
173     //! Rubber band for selecting grass region
174     QgsRubberBand *mRubberBand = nullptr;
175     QgsRubberBand *mSrcRubberBand = nullptr;
176 
177     //! Status of input from canvas
178     bool mDraw;
179 
180     //! First rectangle point
181     QgsPointXY mStartPoint;
182     //! Last rectangle point
183     QgsPointXY mEndPoint;
184 
185     //! Region rectangle in source CRS
186     QgsRectangle mSrcRectangle;
187 
188     QgsCoordinateReferenceSystem mCrs;
189     QgsCoordinateTransform mCoordinateTransform;
190 };
191 
192 #endif // QGSGRASSREGION_H
193