Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / Plot2d / Plot2d_SetupCurveDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : Plot2d_SetupCurveDlg.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #include "Plot2d_SetupCurveDlg.h"
26
27 #include <QtxColorButton.h>
28 #include <SUIT_Tools.h>
29
30 #include <QGridLayout>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QPushButton>
34 #include <QComboBox>
35 #include <QSpinBox>
36 #include <QPainter>
37
38 const int MARGIN_SIZE     = 11;
39 const int SPACING_SIZE    = 6;
40 const int MIN_COMBO_WIDTH = 100;
41 const int MIN_SPIN_WIDTH  = 50;
42 const int MAX_LINE_WIDTH  = 10;
43 const int MSIZE           = 9;
44
45 /*!
46   \class Plot2d_SetupCurveDlg
47   \brief Dialog box for modifying 2d curve settings.
48 */
49
50 /*!
51   \brief Constructor.
52   \param parent parent widget
53 */
54 Plot2d_SetupCurveDlg::Plot2d_SetupCurveDlg( QWidget* parent )
55 : QDialog( parent )
56 {
57   setModal( true );
58   setWindowTitle( tr("TLT_SETUP_CURVE") );
59   setSizeGripEnabled( true );
60
61   // curve type
62   QLabel* aLineTypeLab = new QLabel( tr( "CURVE_LINE_TYPE_LAB" ), this );
63   myLineCombo = new QComboBox( this );
64   myLineCombo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
65   myLineCombo->setMinimumWidth( MIN_COMBO_WIDTH );
66   myLineCombo->setIconSize( QSize( 40, 16 ) );
67
68   // curve width
69   QLabel* aLineWidthLab = new QLabel( tr( "CURVE_LINE_WIDTH_LAB" ), this );
70   myLineSpin = new QSpinBox( this );
71   myLineSpin->setMinimum( 0 );
72   myLineSpin->setMaximum( MAX_LINE_WIDTH );
73   myLineSpin->setSingleStep( 1 );
74   myLineSpin->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
75   myLineSpin->setMinimumWidth( MIN_SPIN_WIDTH );
76
77   // marker type
78   QLabel* aMarkerLab = new QLabel( tr( "CURVE_MARKER_TYPE_LAB" ), this );
79   myMarkerCombo = new QComboBox( this );
80   myMarkerCombo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
81   myMarkerCombo->setMinimumWidth( MIN_COMBO_WIDTH );
82   myMarkerCombo->setIconSize( QSize( 16, 16 ) );
83
84   // curve color
85   QLabel* aColorLab = new QLabel( tr( "CURVE_COLOR_LAB" ), this );
86   myColorBtn = new QtxColorButton( this );
87
88   // preview
89   QLabel* aPreviewLab = new QLabel( tr( "CURVE_PREVIEW_LAB" ), this );
90   myPreview = new QLabel( this );
91   myPreview->setFrameStyle( QLabel::Box | QLabel::Sunken );
92   myPreview->setAlignment( Qt::AlignCenter );
93   myPreview->setScaledContents( false );
94
95   myOkBtn     = new QPushButton( tr( "BUT_OK" ),     this );
96   myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
97
98   // layouting widgets
99   QGridLayout* topLayout = new QGridLayout( this );
100   topLayout->setSpacing( SPACING_SIZE );
101   topLayout->setMargin( MARGIN_SIZE );
102
103   topLayout->addWidget( aLineTypeLab,  0, 0 );
104   topLayout->addWidget( myLineCombo,   0, 1, 1, 2 );
105   topLayout->addWidget( aLineWidthLab, 1, 0 );
106   topLayout->addWidget( myLineSpin,    1, 1, 1, 2 );
107   topLayout->addWidget( aMarkerLab,    2, 0 );
108   topLayout->addWidget( myMarkerCombo, 2, 1, 1, 2 );
109   topLayout->addWidget( aColorLab,     3, 0 );
110   topLayout->addWidget( myColorBtn,    3, 1 );
111   topLayout->addWidget( aPreviewLab,   4, 0 );
112   topLayout->addWidget( myPreview,     4, 1, 1, 2 );
113   topLayout->setColumnStretch( 2, 5 );
114
115   QHBoxLayout* btnLayout = new QHBoxLayout;
116   btnLayout->setSpacing( SPACING_SIZE );
117   btnLayout->setMargin( 0 );
118
119   btnLayout->addWidget( myOkBtn );
120   btnLayout->addSpacing( 20 );
121   btnLayout->addStretch();
122   btnLayout->addWidget( myCancelBtn );
123
124   topLayout->addLayout( btnLayout, 5, 0, 1, 3 );
125
126   // fill then combo boxes
127   myLineCombo->addItem( lineIcon( Plot2d::NoPen ),      tr( "NONE_LINE_LBL" ) );
128   myLineCombo->addItem( lineIcon( Plot2d::Solid ),      tr( "SOLID_LINE_LBL" ) );
129   myLineCombo->addItem( lineIcon( Plot2d::Dash ),       tr( "DASH_LINE_LBL" ) );
130   myLineCombo->addItem( lineIcon( Plot2d::Dot ),        tr( "DOT_LINE_LBL" ) );
131   myLineCombo->addItem( lineIcon( Plot2d::DashDot ),    tr( "DASHDOT_LINE_LBL" ) );
132   myLineCombo->addItem( lineIcon( Plot2d::DashDotDot ), tr( "DAHSDOTDOT_LINE_LBL" ) );
133
134   myMarkerCombo->addItem( markerIcon( Plot2d::None ),      tr( "NONE_MARKER_LBL" ) );
135   myMarkerCombo->addItem( markerIcon( Plot2d::Circle ),    tr( "CIRCLE_MARKER_LBL" ) );
136   myMarkerCombo->addItem( markerIcon( Plot2d::Rectangle ), tr( "RECTANGLE_MARKER_LBL" ) );
137   myMarkerCombo->addItem( markerIcon( Plot2d::Diamond ),   tr( "DIAMOND_MARKER_LBL" ) );
138   myMarkerCombo->addItem( markerIcon( Plot2d::DTriangle ), tr( "DTRIANGLE_MARKER_LBL" ) );
139   myMarkerCombo->addItem( markerIcon( Plot2d::UTriangle ), tr( "UTRIANGLE_MARKER_LBL" ) );
140   myMarkerCombo->addItem( markerIcon( Plot2d::LTriangle ), tr( "LTRIANGLE_MARKER_LBL" ) );
141   myMarkerCombo->addItem( markerIcon( Plot2d::RTriangle ), tr( "RTRIANGLE_MARKER_LBL" ) );
142   myMarkerCombo->addItem( markerIcon( Plot2d::Cross ),     tr( "CROSS_MARKER_LBL" ) );
143   myMarkerCombo->addItem( markerIcon( Plot2d::XCross ),    tr( "XCROSS_MARKER_LBL" ) );
144
145   // default settings
146   setLine( Plot2d::Solid, 0 );   // solid line, width = 0
147   setMarker( Plot2d::Circle );   // circle
148   setColor( QColor( 0, 0, 0 ) ); // black
149
150   // connections
151   connect( myLineCombo,   SIGNAL( activated( int ) ),    this, SLOT( updatePreview() ) );
152   connect( myLineSpin,    SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
153   connect( myMarkerCombo, SIGNAL( activated( int ) ),    this, SLOT( updatePreview() ) );
154   connect( myColorBtn,    SIGNAL( changed( QColor ) ),   this, SLOT( updatePreview() ) );
155   connect( myOkBtn,       SIGNAL( clicked() ),           this, SLOT( accept() ) );
156   connect( myCancelBtn,   SIGNAL( clicked() ),           this, SLOT( reject() ) );
157
158   SUIT_Tools::centerWidget( this, parent );
159   updatePreview();
160 }
161
162 /*!
163   \brief Destructor.
164 */
165 Plot2d_SetupCurveDlg::~Plot2d_SetupCurveDlg()
166 {
167 }
168
169 /*!
170   \brief Set curve line type and width.
171   \param type curve line type
172   \param width curve line width
173   \sa getLine(), getLineWidth()
174 */
175 void Plot2d_SetupCurveDlg::setLine( Plot2d::LineType type, const int width )
176 {
177   myLineCombo->setCurrentIndex( (int)type );
178   if ( width > myLineSpin->maximum() )
179     myLineSpin->setMaximum( width );
180   myLineSpin->setValue( width );
181   updatePreview();
182 }
183
184 /*!
185   \brief Get curve line type.
186   \return chosen curve line type
187   \sa setLine(), getLineWidth()
188 */
189 Plot2d::LineType Plot2d_SetupCurveDlg::getLine() const
190 {
191   return (Plot2d::LineType)myLineCombo->currentIndex();
192 }
193
194 /*!
195   \brief Get curve line width.
196   \return chosen curve line width
197   \sa setLine(), getLine()
198 */
199 int Plot2d_SetupCurveDlg::getLineWidth() const
200 {
201   return myLineSpin->value();
202 }
203
204 /*!
205   \brief Set curve marker type.
206   \param type curve marker type
207   \sa getMarker()
208 */
209 void Plot2d_SetupCurveDlg::setMarker( Plot2d::MarkerType type )
210 {
211   myMarkerCombo->setCurrentIndex( (int)type );
212   updatePreview();
213 }
214
215 /*!
216   \brief Get curve marker type.
217   \return chosen curve marker type
218   \sa setMarker()
219 */
220 Plot2d::MarkerType Plot2d_SetupCurveDlg::getMarker() const
221 {
222   return (Plot2d::MarkerType)myMarkerCombo->currentIndex();
223 }
224
225 /*!
226   \brief Set curve color.
227   \param color curve color
228   \sa getColor()
229 */
230 void Plot2d_SetupCurveDlg::setColor( const QColor& color )
231 {
232   myColorBtn->setColor( color );
233   updatePreview();
234 }
235
236 /*!
237   \brief Get curve color.
238   \return curve color
239   \sa setColor()
240 */
241 QColor Plot2d_SetupCurveDlg::getColor() const
242 {
243   return myColorBtn->color();
244 }
245
246 /*!
247   \brief Create icon pixmap according to the line type.
248   \param type line type
249   \return icon
250 */
251 QPixmap Plot2d_SetupCurveDlg::lineIcon( Plot2d::LineType type ) const
252 {
253   QSize sz = myLineCombo->iconSize();
254   QPixmap px( sz );
255   px.fill( QColor( 255, 255, 255, 0 ) );
256   QPainter p( &px );
257   Plot2d::drawLine( &p, 5, sz.height()/2, sz.width()-5, sz.height()/2, type,
258                     myLineCombo->palette().color( QPalette::Text ), 1 );
259   return px;
260 }
261
262 /*!
263   \brief Create icon pixmap according to the marker type.
264   \param type marker type
265   \return icon
266 */
267 QPixmap Plot2d_SetupCurveDlg::markerIcon( Plot2d::MarkerType type ) const
268 {
269   QSize sz = myMarkerCombo->iconSize();
270   QPixmap px( sz );
271   px.fill( QColor( 255, 255, 255, 0 ) );
272   QPainter p( &px );
273   Plot2d::drawMarker( &p, sz.width()/2, sz.height()/2, MSIZE, MSIZE, type,
274                       myMarkerCombo->palette().color( QPalette::Text ) );
275   return px;
276 }
277
278 /*
279   \brief Update preview widget.
280 */
281 void Plot2d_SetupCurveDlg::updatePreview()
282 {
283   QSize sz( 150, 20 );
284   QPixmap px( sz );
285   px.fill( palette().color( QPalette::Background ) );
286
287   QPainter p( &px );
288
289   Plot2d::drawLine( &p, 5+MSIZE/2, sz.height()/2, sz.width()-5-MSIZE/2, sz.height()/2,
290                     getLine(), getColor(), getLineWidth() );
291   Plot2d::drawMarker( &p, 5+MSIZE/2, sz.height()/2, MSIZE, MSIZE,
292                       getMarker(), getColor() );
293   Plot2d::drawMarker( &p, sz.width()-5-MSIZE/2, sz.height()/2, MSIZE, MSIZE,
294                       getMarker(), getColor() );
295
296   myPreview->setPixmap( px );
297 }