Salome HOME
5bc7f5fe87b0bed7f1e3239fb1b289e5c1fea505
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ColorDlg.cxx
1 // Copyright (C) 2007-2013  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
23 #include "HYDROGUI_ColorDlg.h"
24
25 #include "HYDROGUI_ColorWidget.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <QGroupBox>
29 #include <QLabel>
30 #include <QLayout>
31 #include <QRadioButton>
32 #include <QPushButton>
33
34 HYDROGUI_ColorDlg::HYDROGUI_ColorDlg( QWidget* theParent, bool theIsOneColor )
35 : QDialog( theParent ),
36   myIsOneColor( theIsOneColor )
37 {
38   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
39   aMainLayout->setMargin( 5 );
40   aMainLayout->setSpacing( 5 );
41
42   if ( !theIsOneColor )
43   {
44     // Filling color
45     QFrame* aFillingFrame = new QFrame( this );
46     QLabel* aFillingLabel = new QLabel( tr( "FILLING_COLOR" ), aFillingFrame );
47     myFillingTransparent = new QRadioButton( tr( "TRANSPARENT" ), aFillingFrame );
48     myFillingTransparent->setChecked( true );
49     myFillingColor = new QRadioButton( tr( "COLOR" ), aFillingFrame );
50     myFillingColorBox = new HYDROGUI_ColorWidget( aFillingFrame );
51
52     QGridLayout* aFillingLayout = new QGridLayout( aFillingFrame );
53     aFillingLayout->setMargin( 5 );
54     aFillingLayout->setSpacing( 5 );
55     aFillingLayout->addWidget( aFillingLabel, 0, 0, 2, 1 );
56     aFillingLayout->addWidget( myFillingTransparent, 0, 1 );
57     aFillingLayout->addWidget( myFillingColor,       1, 1 );
58     aFillingLayout->addWidget( myFillingColorBox,    1, 2 );
59
60     // Border color
61     myBorderColorGroup = new QGroupBox( tr( "BORDER_COLOR" ), this );
62     myBorderColorGroup->setCheckable( true );
63
64     myBorderColorBox = new HYDROGUI_ColorWidget( myBorderColorGroup );
65
66     QBoxLayout* aBorderColorLayout = new QHBoxLayout( myBorderColorGroup );
67     aBorderColorLayout->setMargin( 5 );
68     aBorderColorLayout->setSpacing( 5 );
69     aBorderColorLayout->addWidget( new QLabel( tr( "COLOR" ), myBorderColorGroup ) );
70     aBorderColorLayout->addWidget( myBorderColorBox );
71
72     aMainLayout->addWidget( aFillingFrame );
73     aMainLayout->addWidget( myBorderColorGroup );
74   }
75   else
76   {
77     QFrame* aColorFrame = new QFrame( this );
78     QLabel* aColorLabel = new QLabel( tr( "OBJECT_COLOR" ), aColorFrame );
79     myColorBox = new HYDROGUI_ColorWidget( aColorFrame );
80     myColorBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); 
81     myColorBox->setFixedHeight( 20 );
82
83     QBoxLayout* aColorLayout = new QHBoxLayout( aColorFrame );
84     aColorLayout->setMargin( 10 );
85     aColorLayout->setSpacing( 5 );
86     aColorLayout->addWidget( aColorLabel );
87     aColorLayout->addWidget( myColorBox );
88
89     aMainLayout->addWidget( aColorFrame );
90   }
91
92   // Buttons
93   QPushButton* anOkButton = new QPushButton( tr( "OK" ), this );
94   anOkButton->setDefault( true ); 
95   QPushButton* aCancelButton = new QPushButton( tr( "CANCEL" ), this );
96   aCancelButton->setAutoDefault( true );
97
98   QHBoxLayout* aButtonsLayout = new QHBoxLayout;
99   aButtonsLayout->setMargin( 5 );
100   aButtonsLayout->setSpacing( 5 );
101   aButtonsLayout->addWidget( anOkButton );
102   aButtonsLayout->addStretch();
103   aButtonsLayout->addWidget( aCancelButton );
104
105   // Common
106   aMainLayout->addStretch();
107   aMainLayout->addLayout( aButtonsLayout );
108
109   setLayout( aMainLayout );  
110
111   // Connect signals and slots
112   connect( anOkButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
113   connect( aCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
114
115   setFixedSize( 300, theIsOneColor ? 90 : 150 );
116 }
117
118 HYDROGUI_ColorDlg::~HYDROGUI_ColorDlg()
119 {
120 }
121
122 void HYDROGUI_ColorDlg::setFirstColor( const QColor& theColor )
123 {
124   if ( !myIsOneColor )
125   {
126     if( theColor.alpha() == 0 ) // transparent
127       myFillingTransparent->setChecked( true );
128     else
129       myFillingColor->setChecked( true );
130
131     myFillingColorBox->setColor( theColor );
132   }
133   else
134   {
135     myColorBox->setColor( theColor );
136   }
137 }
138
139 QColor HYDROGUI_ColorDlg::getFirstColor() const
140 {
141   QColor aColor;
142   if ( !myIsOneColor )
143   {
144     aColor = QColor( 255, 255, 255, 0 ); // transparent
145     if( myFillingColor->isChecked() )
146       aColor = myFillingColorBox->color();
147   }
148   else
149   {
150     aColor = myColorBox->color();
151   }
152
153   return aColor;
154 }
155
156 void HYDROGUI_ColorDlg::setSecondColor( const QColor& theColor )
157 {
158   if ( myIsOneColor )
159     return;
160
161   bool isTransparent = theColor.alpha() == 0;
162   myBorderColorGroup->setChecked( !isTransparent );
163   myBorderColorBox->setColor( !isTransparent ? theColor : QColor( Qt::black ) );
164 }
165
166 QColor HYDROGUI_ColorDlg::getSecondColor() const
167 {
168   QColor aColor( Qt::transparent ); // transparent
169  
170   if ( myIsOneColor )
171     return aColor;
172
173   if( myBorderColorGroup->isChecked() )
174     aColor = myBorderColorBox->color();
175   
176   return aColor;
177 }
178
179