Salome HOME
bdad6713d0d3ec947a7c2af4cab958a2bfa04226
[modules/gui.git] / src / Plot2d / Plot2d_SetupCurveDlg.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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, or (at your option) any later version.
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
23 // File   : Plot2d_SetupCurveDlg.cxx
24 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 //
26 #include "Plot2d_SetupCurveDlg.h"
27
28 #include <QtxColorButton.h>
29 #ifndef NO_SUIT
30 #include <SUIT_Tools.h>
31 #endif
32
33 #include <QGridLayout>
34 #include <QHBoxLayout>
35 #include <QLabel>
36 #include <QPushButton>
37 #include <QComboBox>
38 #include <QSpinBox>
39 #include <QPainter>
40
41 const int MARGIN_SIZE     = 11;
42 const int SPACING_SIZE    = 6;
43 const int MIN_COMBO_WIDTH = 100;
44 const int MIN_SPIN_WIDTH  = 50;
45 const int MAX_LINE_WIDTH  = 10;
46 const int MSIZE           = 9;
47
48 /*!
49   \class Plot2d_SetupCurveDlg
50   \brief Dialog box for modifying 2d curve settings.
51 */
52
53 /*!
54   \brief Constructor.
55   \param parent parent widget
56 */
57 Plot2d_SetupCurveDlg::Plot2d_SetupCurveDlg( QWidget* parent )
58 : QDialog( parent )
59 {
60   setModal( true );
61   setWindowTitle( tr("TLT_SETUP_CURVE") );
62   setSizeGripEnabled( true );
63
64   // curve type
65   QLabel* aLineTypeLab = new QLabel( tr( "CURVE_LINE_TYPE_LAB" ), this );
66   myLineCombo = new QComboBox( this );
67   myLineCombo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
68   myLineCombo->setMinimumWidth( MIN_COMBO_WIDTH );
69   QSize lsz( 40, 16 );
70   myLineCombo->setIconSize( lsz );
71
72   // curve width
73   QLabel* aLineWidthLab = new QLabel( tr( "CURVE_LINE_WIDTH_LAB" ), this );
74   myLineSpin = new QSpinBox( this );
75   myLineSpin->setMinimum( 0 );
76   myLineSpin->setMaximum( MAX_LINE_WIDTH );
77   myLineSpin->setSingleStep( 1 );
78   myLineSpin->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
79   myLineSpin->setMinimumWidth( MIN_SPIN_WIDTH );
80
81   // marker type
82   QLabel* aMarkerLab = new QLabel( tr( "CURVE_MARKER_TYPE_LAB" ), this );
83   myMarkerCombo = new QComboBox( this );
84   myMarkerCombo->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
85   myMarkerCombo->setMinimumWidth( MIN_COMBO_WIDTH );
86   QSize sz(16, 16);
87   myMarkerCombo->setIconSize(sz);
88
89   // curve color
90   QLabel* aColorLab = new QLabel( tr( "CURVE_COLOR_LAB" ), this );
91   myColorBtn = new QtxColorButton( this );
92
93   // preview
94   QLabel* aPreviewLab = new QLabel( tr( "CURVE_PREVIEW_LAB" ), this );
95   myPreview = new QLabel( this );
96   myPreview->setFrameStyle( QLabel::Box | QLabel::Sunken );
97   myPreview->setAlignment( Qt::AlignCenter );
98   myPreview->setScaledContents( false );
99
100   myOkBtn     = new QPushButton( tr( "BUT_OK" ),     this );
101   myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
102
103   // layouting widgets
104   QGridLayout* topLayout = new QGridLayout( this );
105   topLayout->setSpacing( SPACING_SIZE );
106   topLayout->setMargin( MARGIN_SIZE );
107
108   topLayout->addWidget( aLineTypeLab,  0, 0 );
109   topLayout->addWidget( myLineCombo,   0, 1, 1, 2 );
110   topLayout->addWidget( aLineWidthLab, 1, 0 );
111   topLayout->addWidget( myLineSpin,    1, 1, 1, 2 );
112   topLayout->addWidget( aMarkerLab,    2, 0 );
113   topLayout->addWidget( myMarkerCombo, 2, 1, 1, 2 );
114   topLayout->addWidget( aColorLab,     3, 0 );
115   topLayout->addWidget( myColorBtn,    3, 1 );
116   topLayout->addWidget( aPreviewLab,   4, 0 );
117   topLayout->addWidget( myPreview,     4, 1, 1, 2 );
118   topLayout->setColumnStretch( 2, 5 );
119
120   QHBoxLayout* btnLayout = new QHBoxLayout;
121   btnLayout->setSpacing( SPACING_SIZE );
122   btnLayout->setMargin( 0 );
123
124   btnLayout->addWidget( myOkBtn );
125   btnLayout->addSpacing( 20 );
126   btnLayout->addStretch();
127   btnLayout->addWidget( myCancelBtn );
128
129   topLayout->addLayout( btnLayout, 5, 0, 1, 3 );
130
131   // fill then combo boxes
132   QColor cl = myLineCombo->palette().color( QPalette::Text );
133
134   myLineCombo->addItem( Plot2d::lineIcon( lsz, cl, Plot2d::NoPen ),      tr( "NONE_LINE_LBL" ) );
135   myLineCombo->addItem( Plot2d::lineIcon( lsz, cl, Plot2d::Solid ),      tr( "SOLID_LINE_LBL" ) );
136   myLineCombo->addItem( Plot2d::lineIcon( lsz, cl, Plot2d::Dash ),       tr( "DASH_LINE_LBL" ) );
137   myLineCombo->addItem( Plot2d::lineIcon( lsz, cl, Plot2d::Dot ),        tr( "DOT_LINE_LBL" ) );
138   myLineCombo->addItem( Plot2d::lineIcon( lsz, cl, Plot2d::DashDot ),    tr( "DASHDOT_LINE_LBL" ) );
139   myLineCombo->addItem( Plot2d::lineIcon( lsz, cl, Plot2d::DashDotDot ), tr( "DAHSDOTDOT_LINE_LBL" ) );
140
141   cl = myMarkerCombo->palette().color( QPalette::Text );
142
143   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::None ),      tr( "NONE_MARKER_LBL" ) );
144   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::Circle ),    tr( "CIRCLE_MARKER_LBL" ) );
145   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::Rectangle ), tr( "RECTANGLE_MARKER_LBL" ) );
146   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::Diamond ),   tr( "DIAMOND_MARKER_LBL" ) );
147   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::DTriangle ), tr( "DTRIANGLE_MARKER_LBL" ) );
148   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::UTriangle ), tr( "UTRIANGLE_MARKER_LBL" ) );
149   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::LTriangle ), tr( "LTRIANGLE_MARKER_LBL" ) );
150   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::RTriangle ), tr( "RTRIANGLE_MARKER_LBL" ) );
151   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::Cross ),     tr( "CROSS_MARKER_LBL" ) );
152   myMarkerCombo->addItem( Plot2d::markerIcon( sz, cl, Plot2d::XCross ),    tr( "XCROSS_MARKER_LBL" ) );
153
154   // default settings
155   setLine( Plot2d::Solid, 0 );   // solid line, width = 0
156   setMarker( Plot2d::Circle );   // circle
157   setColor( QColor( 0, 0, 0 ) ); // black
158
159   // connections
160   connect( myLineCombo,   SIGNAL( activated( int ) ),    this, SLOT( updatePreview() ) );
161   connect( myLineSpin,    SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
162   connect( myMarkerCombo, SIGNAL( activated( int ) ),    this, SLOT( updatePreview() ) );
163   connect( myColorBtn,    SIGNAL( changed( QColor ) ),   this, SLOT( updatePreview() ) );
164   connect( myOkBtn,       SIGNAL( clicked() ),           this, SLOT( accept() ) );
165   connect( myCancelBtn,   SIGNAL( clicked() ),           this, SLOT( reject() ) );
166
167 #ifndef NO_SUIT
168   SUIT_Tools::centerWidget( this, parent );
169 #endif
170   updatePreview();
171 }
172
173 /*!
174   \brief Destructor.
175 */
176 Plot2d_SetupCurveDlg::~Plot2d_SetupCurveDlg()
177 {
178 }
179
180 /*!
181   \brief Set curve line type and width.
182   \param type curve line type
183   \param width curve line width
184   \sa getLine(), getLineWidth()
185 */
186 void Plot2d_SetupCurveDlg::setLine( Plot2d::LineType type, const int width )
187 {
188   myLineCombo->setCurrentIndex( (int)type );
189   if ( width > myLineSpin->maximum() )
190     myLineSpin->setMaximum( width );
191   myLineSpin->setValue( width );
192   updatePreview();
193 }
194
195 /*!
196   \brief Get curve line type.
197   \return chosen curve line type
198   \sa setLine(), getLineWidth()
199 */
200 Plot2d::LineType Plot2d_SetupCurveDlg::getLine() const
201 {
202   return (Plot2d::LineType)myLineCombo->currentIndex();
203 }
204
205 /*!
206   \brief Get curve line width.
207   \return chosen curve line width
208   \sa setLine(), getLine()
209 */
210 int Plot2d_SetupCurveDlg::getLineWidth() const
211 {
212   return myLineSpin->value();
213 }
214
215 /*!
216   \brief Set curve marker type.
217   \param type curve marker type
218   \sa getMarker()
219 */
220 void Plot2d_SetupCurveDlg::setMarker( Plot2d::MarkerType type )
221 {
222   myMarkerCombo->setCurrentIndex( (int)type );
223   updatePreview();
224 }
225
226 /*!
227   \brief Get curve marker type.
228   \return chosen curve marker type
229   \sa setMarker()
230 */
231 Plot2d::MarkerType Plot2d_SetupCurveDlg::getMarker() const
232 {
233   return (Plot2d::MarkerType)myMarkerCombo->currentIndex();
234 }
235
236 /*!
237   \brief Set curve color.
238   \param color curve color
239   \sa getColor()
240 */
241 void Plot2d_SetupCurveDlg::setColor( const QColor& color )
242 {
243   myColorBtn->setColor( color );
244   updatePreview();
245 }
246
247 /*!
248   \brief Get curve color.
249   \return curve color
250   \sa setColor()
251 */
252 QColor Plot2d_SetupCurveDlg::getColor() const
253 {
254   return myColorBtn->color();
255 }
256
257 /*
258   \brief Update preview widget.
259 */
260 void Plot2d_SetupCurveDlg::updatePreview()
261 {
262   QSize sz( 150, 20 );
263   QPixmap px( sz );
264   px.fill( palette().color( QPalette::Background ) );
265
266   QPainter p( &px );
267
268   Plot2d::drawLine( &p, 5+MSIZE/2, sz.height()/2, sz.width()-5-MSIZE/2, sz.height()/2,
269                     getLine(), getColor(), getLineWidth() );
270   Plot2d::drawMarker( &p, 5+MSIZE/2, sz.height()/2, MSIZE, MSIZE,
271                       getMarker(), getColor() );
272   Plot2d::drawMarker( &p, sz.width()-5-MSIZE/2, sz.height()/2, MSIZE, MSIZE,
273                       getMarker(), getColor() );
274
275   myPreview->setPixmap( px );
276 }