File: style_edit.cpp

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (273 lines) | stat: -rw-r--r-- 9,585 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
 * @file: style_edit.cpp 
 * Widgets for style manipulation
 */
/* 
 * GUI for ShowGraph tool.
 * Copyright (c) 2009, Boris Shurygin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * 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.
 * 
 * 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.
 * 
 * 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.
 */
#include "gview_impl.h"

ColorButton::ColorButton( QWidget *parent):
    QAbstractButton( parent)
{

}

void ColorButton::paintEvent( QPaintEvent *event)
{
    QPainter painter( this);
    painter.setPen( palette().windowText().color());
    painter.setBrush( color);
    painter.drawRect( rect());
}

QSize ColorButton::sizeHint() const
{
    return QSize( 20,20);
}

StyleEdit::StyleEdit( QWidget *parent, bool show_additional)
: name_label(NULL),
  line_color_label(NULL),
  line_style_label(NULL),
  line_width_label(NULL),
  fill_color_label(NULL),
  shape_label(NULL),
  name_combo(NULL),
  fill_check(NULL),
  shape_combo(NULL),
  line_color_button(NULL),
  line_style_combo(NULL),
  line_width_spin(NULL),
  fill_color_button(NULL),
  ok(NULL),
  cancel(NULL)
{
    QColor color;
    if ( show_additional)
    {        
        shape_combo = new QComboBox( this);
        shape_combo->addItem(tr("Box"), NODE_SHAPE_BOX);
        shape_combo->addItem(tr("Rounded box"), NODE_SHAPE_ROUNDED_BOX);
        shape_combo->addItem(tr("Ellipse"), NODE_SHAPE_ELLIPSE);
        shape_combo->addItem(tr("Circle"), NODE_SHAPE_CIRCLE);
        shape_combo->addItem(tr("Diamond"), NODE_SHAPE_DIAMOND);

        shape_label = new QLabel( "Shape:", this);
        shape_label->setBuddy( shape_combo);
    } else
    {
        shape_combo = NULL;
    }

    color = QColor(palette().windowText().color());
    line_color_button = new ColorButton( this);
    line_color_button->setColor( color);
    
    line_color_label = new QLabel( "Line color:", this);
    line_color_label->setBuddy( line_color_button);

    line_style_combo = new QComboBox( this);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    line_style_combo->addItem(tr("Solid"), QPen(Qt::SolidLine));
    line_style_combo->addItem(tr("Dash"), QPen(Qt::DashLine));
    line_style_combo->addItem(tr("Dot"), QPen(Qt::DotLine));
    line_style_combo->addItem(tr("Dash Dot"), QPen(Qt::DashDotLine));
    line_style_combo->addItem(tr("Dash Dot Dot"), QPen(Qt::DashDotDotLine));
    line_style_combo->addItem(tr("None"), QPen(Qt::NoPen));
#else
    line_style_combo->addItem(tr("Solid"), Qt::SolidLine);
    line_style_combo->addItem(tr("Dash"), Qt::DashLine);
    line_style_combo->addItem(tr("Dot"), Qt::DotLine);
    line_style_combo->addItem(tr("Dash Dot"), Qt::DashDotLine);
    line_style_combo->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine);
    line_style_combo->addItem(tr("None"), Qt::NoPen);
#endif

    line_style_label = new QLabel( "Line style:", this);
    line_style_label->setBuddy( line_style_combo);
    
    line_width_spin = new QDoubleSpinBox( this);
    line_width_spin->setMaximum( 12);
    
    line_width_label = new QLabel( "Line width:", this);
    line_width_label->setBuddy( line_width_spin);

    if ( show_additional)
    {
        color = QColor(palette().base().color());
        fill_check = new QCheckBox( "Fill", this);
        fill_color_button = new ColorButton( this);
        fill_color_button->setColor( color);
        
        fill_color_label = new QLabel( "Fill color:", this);
        fill_color_label->setBuddy( fill_color_button);
    } else
    {
        fill_color_button = NULL;
        fill_color_label = NULL;
        fill_check = NULL;
    }


    ok = new QPushButton( "&OK", this);
    cancel = new QPushButton( "&Cancel", this);

    QGridLayout *mainLayout = new QGridLayout;
    //mainLayout->setColumnStretch(1, 3);
    
    //Row 0 name
    if ( show_additional)
    {
        mainLayout->addWidget( shape_label, 0, 0, Qt::AlignRight);
        mainLayout->addWidget( shape_combo, 0, 1);
    }
    //Row 1 line color
    mainLayout->addWidget( line_color_label, 1, 0, Qt::AlignRight);
    mainLayout->addWidget( line_color_button, 1, 1);
    //Row 2 line style
    mainLayout->addWidget( line_style_label, 2, 0, Qt::AlignRight);
    mainLayout->addWidget( line_style_combo, 2, 1);
     //Row 3 line width
    mainLayout->addWidget( line_width_label, 3, 0, Qt::AlignRight);
    mainLayout->addWidget( line_width_spin, 3, 1);
    //Row 4 fill color
    if ( show_additional)
    {
        mainLayout->addWidget( fill_check, 4, 0, 1, 2);
        mainLayout->addWidget( fill_color_label, 5, 0, Qt::AlignRight);
        mainLayout->addWidget( fill_color_button, 5, 1);
    }
    // Row 5 ok & cancel
    mainLayout->addWidget( ok, 6, 0);
    mainLayout->addWidget( cancel, 6, 1);
    
    setLayout( mainLayout);

    if ( show_additional)
    {
        connect( shape_combo, SIGNAL( currentIndexChanged( int)), this, SLOT( changeShape()) );
        connect( fill_color_button, SIGNAL( clicked()), this, SLOT( selectFillColor()) );
        connect( fill_check, SIGNAL( clicked()), this, SLOT( changeFillStyle()) );
    }
    connect( line_color_button, SIGNAL( clicked()), this, SLOT( selectLineColor()) );
    connect( line_style_combo, SIGNAL( currentIndexChanged( int)), this, SLOT( changeLineStyle()) );
    connect( line_width_spin, SIGNAL( valueChanged( double)), this, SLOT( changeLineWidth( double)) );
    

    connect( ok, SIGNAL( clicked()), this, SLOT( accept()));
    connect( cancel, SIGNAL( clicked()), this, SLOT( reject()));

    setWindowTitle( "Edit style");
    setWindowFlags( windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
}

/** Change name of the current style */
void StyleEdit::changeStyleName()
{

}

/** Invoke color selection for line */
void StyleEdit::selectLineColor()
{
    QColor color = QColorDialog::getColor();
    line_color_button->setColor( color);
    gstyle->setPenColor( color);
    gstyle->setState();
    emit styleChanged( gstyle);
}

/** Change line style */
void StyleEdit::changeLineStyle()
{
    gstyle->setPenStyle((Qt::PenStyle)line_style_combo->itemData( line_style_combo->currentIndex()).toInt());
    gstyle->setState();
    emit styleChanged( gstyle);
}

/** Change line style */
void StyleEdit::changeShape()
{
    emit styleChanged( gstyle);
    gstyle->setShape((NodeShape)shape_combo->itemData( shape_combo->currentIndex()).toInt());
    gstyle->setState();
    emit styleChanged( gstyle);
}

/** Change line width */
void StyleEdit::changeLineWidth( double width)
{
    gstyle->setPenWidth(line_width_spin->value());
    gstyle->setState();
    emit styleChanged( gstyle);
}
/** Set fill style */
void StyleEdit::changeFillStyle()
{
    if ( !fill_check->isChecked())
    { 
        QBrush br = gstyle->brush();
        br.setStyle( Qt::NoBrush);
        gstyle->setBrush( br);
    } else
    {
        QBrush br = gstyle->brush();
        br.setStyle( Qt::SolidPattern);
        gstyle->setBrush( br);
        gstyle->setState();
    }
    emit styleChanged( gstyle);
}
/** Invoke color selection for fill */
void StyleEdit::selectFillColor()
{
    QColor color = QColorDialog::getColor();
    QBrush brush( color);
    fill_color_button->setColor( color);
    gstyle->setBrush( brush);
    gstyle->setState();
    fill_check->setChecked( true);
    emit styleChanged( gstyle);
}

/** Set style */
void StyleEdit::setGStyle( GStyle *st)
{
    gstyle = st;
    QColor color( st->pen().color());
    line_color_button->setColor( color);
    if ( isNotNullP( shape_combo))
    {
        color =  QColor( st->brush().color());
        shape_combo->setCurrentIndex( shape_combo->findData( gstyle->shape()));
        fill_color_button->setColor( color);
        fill_check->setChecked( st->brush().style() != Qt::NoBrush);
        if ( st->brush().style() == Qt::NoBrush)
        {
            QBrush br = gstyle->brush();
            color = QColor( "White");
            br.setColor( color);
            gstyle->setBrush( br);
            fill_color_button->setColor( color);
        }
    }
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    line_style_combo->setCurrentIndex( line_style_combo->findData( QPen(gstyle->pen().style())));
#else
    line_style_combo->setCurrentIndex( line_style_combo->findData( gstyle->pen().style()));
#endif
    line_width_spin->setValue( gstyle->pen().widthF());
}