1 /**
2  * @file: style_edit.cpp
3  * Widgets for style manipulation
4  */
5 /*
6  * GUI for ShowGraph tool.
7  * Copyright (c) 2009, Boris Shurygin
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  */
20 #include "gview_impl.h"
21 
22 #ifdef USE_QT5
23 #define QT45_FOREGROUND(x) windowText(x)
24 #else
25 #define QT45_FOREGROUND(x) foreground(x)
26 #endif
27 
ColorButton(QWidget * parent)28 ColorButton::ColorButton( QWidget *parent):
29     QAbstractButton( parent)
30 {
31 
32 }
33 
paintEvent(QPaintEvent * event)34 void ColorButton::paintEvent( QPaintEvent *event)
35 {
36     QPainter painter( this);
37     painter.setPen( palette().QT45_FOREGROUND().color());
38     painter.setBrush( color);
39     painter.drawRect( rect());
40 }
41 
sizeHint() const42 QSize ColorButton::sizeHint() const
43 {
44     return QSize( 20,20);
45 }
46 
StyleEdit(QWidget * parent,bool show_additional)47 StyleEdit::StyleEdit( QWidget *parent, bool show_additional)
48 : name_label(NULL),
49   line_color_label(NULL),
50   line_style_label(NULL),
51   line_width_label(NULL),
52   fill_color_label(NULL),
53   shape_label(NULL),
54   name_combo(NULL),
55   fill_check(NULL),
56   shape_combo(NULL),
57   line_color_button(NULL),
58   line_style_combo(NULL),
59   line_width_spin(NULL),
60   fill_color_button(NULL),
61   ok(NULL),
62   cancel(NULL)
63 {
64     QColor color;
65     if ( show_additional)
66     {
67         shape_combo = new QComboBox( this);
68         shape_combo->addItem(tr("Box"), NODE_SHAPE_BOX);
69         shape_combo->addItem(tr("Rounded box"), NODE_SHAPE_ROUNDED_BOX);
70         shape_combo->addItem(tr("Ellipse"), NODE_SHAPE_ELLIPSE);
71         shape_combo->addItem(tr("Circle"), NODE_SHAPE_CIRCLE);
72         shape_combo->addItem(tr("Diamond"), NODE_SHAPE_DIAMOND);
73 
74         shape_label = new QLabel( "Shape:", this);
75         shape_label->setBuddy( shape_combo);
76     } else
77     {
78         shape_combo = NULL;
79     }
80 
81     color = QColor(palette().QT45_FOREGROUND().color());
82     line_color_button = new ColorButton( this);
83     line_color_button->setColor( color);
84 
85     line_color_label = new QLabel( "Line color:", this);
86     line_color_label->setBuddy( line_color_button);
87 
88     line_style_combo = new QComboBox( this);
89 #ifdef USE_QT5
90     line_style_combo->addItem(tr("Solid"), QPen(Qt::SolidLine));
91     line_style_combo->addItem(tr("Dash"), QPen(Qt::DashLine));
92     line_style_combo->addItem(tr("Dot"), QPen(Qt::DotLine));
93     line_style_combo->addItem(tr("Dash Dot"), QPen(Qt::DashDotLine));
94     line_style_combo->addItem(tr("Dash Dot Dot"), QPen(Qt::DashDotDotLine));
95     line_style_combo->addItem(tr("None"), QPen(Qt::NoPen));
96 #else
97     line_style_combo->addItem(tr("Solid"), Qt::SolidLine);
98     line_style_combo->addItem(tr("Dash"), Qt::DashLine);
99     line_style_combo->addItem(tr("Dot"), Qt::DotLine);
100     line_style_combo->addItem(tr("Dash Dot"), Qt::DashDotLine);
101     line_style_combo->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine);
102     line_style_combo->addItem(tr("None"), Qt::NoPen);
103 #endif
104 
105     line_style_label = new QLabel( "Line style:", this);
106     line_style_label->setBuddy( line_style_combo);
107 
108     line_width_spin = new QDoubleSpinBox( this);
109     line_width_spin->setMaximum( 12);
110 
111     line_width_label = new QLabel( "Line width:", this);
112     line_width_label->setBuddy( line_width_spin);
113 
114     if ( show_additional)
115     {
116         color = QColor(palette().base().color());
117         fill_check = new QCheckBox( "Fill", this);
118         fill_color_button = new ColorButton( this);
119         fill_color_button->setColor( color);
120 
121         fill_color_label = new QLabel( "Fill color:", this);
122         fill_color_label->setBuddy( fill_color_button);
123     } else
124     {
125         fill_color_button = NULL;
126         fill_color_label = NULL;
127         fill_check = NULL;
128     }
129 
130 
131     ok = new QPushButton( "&OK", this);
132     cancel = new QPushButton( "&Cancel", this);
133 
134     QGridLayout *mainLayout = new QGridLayout;
135     //mainLayout->setColumnStretch(1, 3);
136 
137     //Row 0 name
138     if ( show_additional)
139     {
140         mainLayout->addWidget( shape_label, 0, 0, Qt::AlignRight);
141         mainLayout->addWidget( shape_combo, 0, 1);
142     }
143     //Row 1 line color
144     mainLayout->addWidget( line_color_label, 1, 0, Qt::AlignRight);
145     mainLayout->addWidget( line_color_button, 1, 1);
146     //Row 2 line style
147     mainLayout->addWidget( line_style_label, 2, 0, Qt::AlignRight);
148     mainLayout->addWidget( line_style_combo, 2, 1);
149      //Row 3 line width
150     mainLayout->addWidget( line_width_label, 3, 0, Qt::AlignRight);
151     mainLayout->addWidget( line_width_spin, 3, 1);
152     //Row 4 fill color
153     if ( show_additional)
154     {
155         mainLayout->addWidget( fill_check, 4, 0, 1, 2);
156         mainLayout->addWidget( fill_color_label, 5, 0, Qt::AlignRight);
157         mainLayout->addWidget( fill_color_button, 5, 1);
158     }
159     // Row 5 ok & cancel
160     mainLayout->addWidget( ok, 6, 0);
161     mainLayout->addWidget( cancel, 6, 1);
162 
163     setLayout( mainLayout);
164 
165     if ( show_additional)
166     {
167         connect( shape_combo, SIGNAL( currentIndexChanged( int)), this, SLOT( changeShape()) );
168         connect( fill_color_button, SIGNAL( clicked()), this, SLOT( selectFillColor()) );
169         connect( fill_check, SIGNAL( clicked()), this, SLOT( changeFillStyle()) );
170     }
171     connect( line_color_button, SIGNAL( clicked()), this, SLOT( selectLineColor()) );
172     connect( line_style_combo, SIGNAL( currentIndexChanged( int)), this, SLOT( changeLineStyle()) );
173     connect( line_width_spin, SIGNAL( valueChanged( double)), this, SLOT( changeLineWidth( double)) );
174 
175 
176     connect( ok, SIGNAL( clicked()), this, SLOT( accept()));
177     connect( cancel, SIGNAL( clicked()), this, SLOT( reject()));
178 
179     setWindowTitle( "Edit style");
180     setWindowFlags( windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
181 }
182 
183 /** Change name of the current style */
changeStyleName()184 void StyleEdit::changeStyleName()
185 {
186 
187 }
188 
189 /** Invoke color selection for line */
selectLineColor()190 void StyleEdit::selectLineColor()
191 {
192     QColor color = QColorDialog::getColor();
193     line_color_button->setColor( color);
194     gstyle->setPenColor( color);
195     gstyle->setState();
196     emit styleChanged( gstyle);
197 }
198 
199 /** Change line style */
changeLineStyle()200 void StyleEdit::changeLineStyle()
201 {
202     gstyle->setPenStyle((Qt::PenStyle)line_style_combo->itemData( line_style_combo->currentIndex()).toInt());
203     gstyle->setState();
204     emit styleChanged( gstyle);
205 }
206 
207 /** Change line style */
changeShape()208 void StyleEdit::changeShape()
209 {
210     emit styleChanged( gstyle);
211     gstyle->setShape((NodeShape)shape_combo->itemData( shape_combo->currentIndex()).toInt());
212     gstyle->setState();
213     emit styleChanged( gstyle);
214 }
215 
216 /** Change line width */
changeLineWidth(double width)217 void StyleEdit::changeLineWidth( double width)
218 {
219     gstyle->setPenWidth(line_width_spin->value());
220     gstyle->setState();
221     emit styleChanged( gstyle);
222 }
223 /** Set fill style */
changeFillStyle()224 void StyleEdit::changeFillStyle()
225 {
226     if ( !fill_check->isChecked())
227     {
228         QBrush br = gstyle->brush();
229         br.setStyle( Qt::NoBrush);
230         gstyle->setBrush( br);
231     } else
232     {
233         QBrush br = gstyle->brush();
234         br.setStyle( Qt::SolidPattern);
235         gstyle->setBrush( br);
236         gstyle->setState();
237     }
238     emit styleChanged( gstyle);
239 }
240 /** Invoke color selection for fill */
selectFillColor()241 void StyleEdit::selectFillColor()
242 {
243     QColor color = QColorDialog::getColor();
244     QBrush brush( color);
245     fill_color_button->setColor( color);
246     gstyle->setBrush( brush);
247     gstyle->setState();
248     fill_check->setChecked( true);
249     emit styleChanged( gstyle);
250 }
251 
252 /** Set style */
setGStyle(GStyle * st)253 void StyleEdit::setGStyle( GStyle *st)
254 {
255     gstyle = st;
256     QColor color( st->pen().color());
257     line_color_button->setColor( color);
258     if ( isNotNullP( shape_combo))
259     {
260         color =  QColor( st->brush().color());
261         shape_combo->setCurrentIndex( shape_combo->findData( gstyle->shape()));
262         fill_color_button->setColor( color);
263         fill_check->setChecked( st->brush().style() != Qt::NoBrush);
264         if ( st->brush().style() == Qt::NoBrush)
265         {
266             QBrush br = gstyle->brush();
267             color = QColor( "White");
268             br.setColor( color);
269             gstyle->setBrush( br);
270             fill_color_button->setColor( color);
271         }
272     }
273 #ifdef USE_QT5
274     line_style_combo->setCurrentIndex( line_style_combo->findData( QPen(gstyle->pen().style())));
275 #else
276     line_style_combo->setCurrentIndex( line_style_combo->findData( gstyle->pen().style()));
277 #endif
278     line_width_spin->setValue( gstyle->pen().widthF());
279 }
280