Salome HOME
refs #1278: by default objects in study are not expanded
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ColorDlg.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ColorDlg.h"
20
21 #include "HYDROGUI_ColorWidget.h"
22 #include "HYDROGUI_Tool.h"
23
24 #include <QGroupBox>
25 #include <QLabel>
26 #include <QLayout>
27 #include <QRadioButton>
28 #include <QPushButton>
29
30 HYDROGUI_ColorDlg::HYDROGUI_ColorDlg( QWidget* theParent, bool theIsOneColor )
31 : QDialog( theParent ),
32   myIsOneColor( theIsOneColor )
33 {
34   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
35   aMainLayout->setMargin( 5 );
36   aMainLayout->setSpacing( 5 );
37
38   if ( !theIsOneColor )
39   {
40     // Filling color
41     QFrame* aFillingFrame = new QFrame( this );
42     QLabel* aFillingLabel = new QLabel( tr( "FILLING_COLOR" ), aFillingFrame );
43     myFillingTransparent = new QRadioButton( tr( "TRANSPARENT" ), aFillingFrame );
44     myFillingTransparent->setChecked( true );
45     myFillingColor = new QRadioButton( tr( "COLOR" ), aFillingFrame );
46     myFillingColorBox = new HYDROGUI_ColorWidget( aFillingFrame );
47
48     QGridLayout* aFillingLayout = new QGridLayout( aFillingFrame );
49     aFillingLayout->setMargin( 5 );
50     aFillingLayout->setSpacing( 5 );
51     aFillingLayout->addWidget( aFillingLabel, 0, 0, 2, 1 );
52     aFillingLayout->addWidget( myFillingTransparent, 0, 1 );
53     aFillingLayout->addWidget( myFillingColor,       1, 1 );
54     aFillingLayout->addWidget( myFillingColorBox,    1, 2 );
55
56     // Border color
57     myBorderColorGroup = new QGroupBox( tr( "BORDER_COLOR" ), this );
58     myBorderColorGroup->setCheckable( true );
59
60     myBorderColorBox = new HYDROGUI_ColorWidget( myBorderColorGroup );
61
62     QBoxLayout* aBorderColorLayout = new QHBoxLayout( myBorderColorGroup );
63     aBorderColorLayout->setMargin( 5 );
64     aBorderColorLayout->setSpacing( 5 );
65     aBorderColorLayout->addWidget( new QLabel( tr( "COLOR" ), myBorderColorGroup ) );
66     aBorderColorLayout->addWidget( myBorderColorBox );
67
68     aMainLayout->addWidget( aFillingFrame );
69     aMainLayout->addWidget( myBorderColorGroup );
70
71     connect( myFillingTransparent, SIGNAL( toggled( bool ) ), 
72              myFillingColorBox, SLOT( setDisabled( bool ) ) );
73   }
74   else
75   {
76     QFrame* aColorFrame = new QFrame( this );
77     QLabel* aColorLabel = new QLabel( tr( "OBJECT_COLOR" ), aColorFrame );
78     myColorBox = new HYDROGUI_ColorWidget( aColorFrame );
79     myColorBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); 
80     myColorBox->setFixedHeight( 20 );
81
82     QBoxLayout* aColorLayout = new QHBoxLayout( aColorFrame );
83     aColorLayout->setMargin( 10 );
84     aColorLayout->setSpacing( 5 );
85     aColorLayout->addWidget( aColorLabel );
86     aColorLayout->addWidget( myColorBox );
87
88     aMainLayout->addWidget( aColorFrame );
89   }
90
91   // Buttons
92   QPushButton* anOkButton = new QPushButton( tr( "OK" ), this );
93   anOkButton->setDefault( true ); 
94   QPushButton* aCancelButton = new QPushButton( tr( "CANCEL" ), this );
95   aCancelButton->setAutoDefault( true );
96
97   QHBoxLayout* aButtonsLayout = new QHBoxLayout;
98   aButtonsLayout->setMargin( 5 );
99   aButtonsLayout->setSpacing( 5 );
100   aButtonsLayout->addWidget( anOkButton );
101   aButtonsLayout->addStretch();
102   aButtonsLayout->addWidget( aCancelButton );
103
104   // Common
105   aMainLayout->addStretch();
106   aMainLayout->addLayout( aButtonsLayout );
107
108   setLayout( aMainLayout );  
109
110   // Connect signals and slots
111   connect( anOkButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
112   connect( aCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
113
114   setFixedSize( 300, theIsOneColor ? 90 : 150 );
115 }
116
117 HYDROGUI_ColorDlg::~HYDROGUI_ColorDlg()
118 {
119 }
120
121 void HYDROGUI_ColorDlg::setFirstColor( const QColor& theColor )
122 {
123   if ( !myIsOneColor )
124   {
125     if( theColor.alpha() == 0 ) // transparent
126       myFillingTransparent->setChecked( true );
127     else
128       myFillingColor->setChecked( true );
129
130     myFillingColorBox->setColor( theColor );
131   }
132   else
133   {
134     myColorBox->setColor( theColor );
135   }
136 }
137
138 QColor HYDROGUI_ColorDlg::getFirstColor() const
139 {
140   QColor aColor;
141   if ( !myIsOneColor )
142   {
143     aColor = QColor( 255, 255, 255, 0 ); // transparent
144     if( myFillingColor->isChecked() )
145       aColor = myFillingColorBox->color();
146   }
147   else
148   {
149     aColor = myColorBox->color();
150   }
151
152   return aColor;
153 }
154
155 void HYDROGUI_ColorDlg::setSecondColor( const QColor& theColor )
156 {
157   if ( myIsOneColor )
158     return;
159
160   bool isTransparent = theColor.alpha() == 0;
161   myBorderColorGroup->setChecked( !isTransparent );
162   myBorderColorBox->setColor( !isTransparent ? theColor : QColor( Qt::black ) );
163 }
164
165 QColor HYDROGUI_ColorDlg::getSecondColor() const
166 {
167   QColor aColor( Qt::transparent ); // transparent
168  
169   if ( myIsOneColor )
170     return aColor;
171
172   if( myBorderColorGroup->isChecked() )
173     aColor = myBorderColorBox->color();
174   
175   return aColor;
176 }
177
178