1 | #include "setdataform.h"
|
---|
2 | #include "chartform.h"
|
---|
3 |
|
---|
4 | #include <qcolordialog.h>
|
---|
5 | #include <qcombobox.h>
|
---|
6 | #include <qlayout.h>
|
---|
7 | #include <qpixmap.h>
|
---|
8 | #include <qpushbutton.h>
|
---|
9 | #include <qtable.h>
|
---|
10 |
|
---|
11 | #include "images/pattern01.xpm"
|
---|
12 | #include "images/pattern02.xpm"
|
---|
13 | #include "images/pattern03.xpm"
|
---|
14 | #include "images/pattern04.xpm"
|
---|
15 | #include "images/pattern05.xpm"
|
---|
16 | #include "images/pattern06.xpm"
|
---|
17 | #include "images/pattern07.xpm"
|
---|
18 | #include "images/pattern08.xpm"
|
---|
19 | #include "images/pattern09.xpm"
|
---|
20 | #include "images/pattern10.xpm"
|
---|
21 | #include "images/pattern11.xpm"
|
---|
22 | #include "images/pattern12.xpm"
|
---|
23 | #include "images/pattern13.xpm"
|
---|
24 | #include "images/pattern14.xpm"
|
---|
25 |
|
---|
26 | const int MAX_PATTERNS = 14;
|
---|
27 |
|
---|
28 |
|
---|
29 | SetDataForm::SetDataForm( ElementVector *elements, int decimalPlaces,
|
---|
30 | QWidget* parent, const char* name,
|
---|
31 | bool modal, WFlags f )
|
---|
32 | : QDialog( parent, name, modal, f )
|
---|
33 |
|
---|
34 | {
|
---|
35 | m_elements = elements;
|
---|
36 | m_decimalPlaces = decimalPlaces;
|
---|
37 |
|
---|
38 | setCaption( "Chart -- Set Data" );
|
---|
39 | resize( 540, 440 );
|
---|
40 |
|
---|
41 | tableButtonBox = new QVBoxLayout( this, 11, 6, "table button box layout" );
|
---|
42 |
|
---|
43 | table = new QTable( this, "data table" );
|
---|
44 | table->setNumCols( 5 );
|
---|
45 | table->setNumRows( ChartForm::MAX_ELEMENTS );
|
---|
46 | table->setColumnReadOnly( 1, TRUE );
|
---|
47 | table->setColumnReadOnly( 2, TRUE );
|
---|
48 | table->setColumnReadOnly( 4, TRUE );
|
---|
49 | table->setColumnWidth( 0, 80 );
|
---|
50 | table->setColumnWidth( 1, 60 ); // Columns 1 and 4 must be equal
|
---|
51 | table->setColumnWidth( 2, 60 );
|
---|
52 | table->setColumnWidth( 3, 200 );
|
---|
53 | table->setColumnWidth( 4, 60 );
|
---|
54 | QHeader *th = table->horizontalHeader();
|
---|
55 | th->setLabel( 0, "Value" );
|
---|
56 | th->setLabel( 1, "Color" );
|
---|
57 | th->setLabel( 2, "Pattern" );
|
---|
58 | th->setLabel( 3, "Label" );
|
---|
59 | th->setLabel( 4, "Color" );
|
---|
60 | tableButtonBox->addWidget( table );
|
---|
61 |
|
---|
62 | buttonBox = new QHBoxLayout( 0, 0, 6, "button box layout" );
|
---|
63 |
|
---|
64 | colorPushButton = new QPushButton( this, "color button" );
|
---|
65 | colorPushButton->setText( "&Color..." );
|
---|
66 | colorPushButton->setEnabled( FALSE );
|
---|
67 | buttonBox->addWidget( colorPushButton );
|
---|
68 |
|
---|
69 | QSpacerItem *spacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding,
|
---|
70 | QSizePolicy::Minimum );
|
---|
71 | buttonBox->addItem( spacer );
|
---|
72 |
|
---|
73 | okPushButton = new QPushButton( this, "ok button" );
|
---|
74 | okPushButton->setText( "OK" );
|
---|
75 | okPushButton->setDefault( TRUE );
|
---|
76 | buttonBox->addWidget( okPushButton );
|
---|
77 |
|
---|
78 | cancelPushButton = new QPushButton( this, "cancel button" );
|
---|
79 | cancelPushButton->setText( "Cancel" );
|
---|
80 | cancelPushButton->setAccel( Key_Escape );
|
---|
81 | buttonBox->addWidget( cancelPushButton );
|
---|
82 |
|
---|
83 | tableButtonBox->addLayout( buttonBox );
|
---|
84 |
|
---|
85 | connect( table, SIGNAL( clicked(int,int,int,const QPoint&) ),
|
---|
86 | this, SLOT( setColor(int,int) ) );
|
---|
87 | connect( table, SIGNAL( currentChanged(int,int) ),
|
---|
88 | this, SLOT( currentChanged(int,int) ) );
|
---|
89 | connect( table, SIGNAL( valueChanged(int,int) ),
|
---|
90 | this, SLOT( valueChanged(int,int) ) );
|
---|
91 | connect( colorPushButton, SIGNAL( clicked() ), this, SLOT( setColor() ) );
|
---|
92 | connect( okPushButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
|
---|
93 | connect( cancelPushButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
|
---|
94 |
|
---|
95 | QPixmap patterns[MAX_PATTERNS];
|
---|
96 | patterns[0] = QPixmap( pattern01 );
|
---|
97 | patterns[1] = QPixmap( pattern02 );
|
---|
98 | patterns[2] = QPixmap( pattern03 );
|
---|
99 | patterns[3] = QPixmap( pattern04 );
|
---|
100 | patterns[4] = QPixmap( pattern05 );
|
---|
101 | patterns[5] = QPixmap( pattern06 );
|
---|
102 | patterns[6] = QPixmap( pattern07 );
|
---|
103 | patterns[7] = QPixmap( pattern08 );
|
---|
104 | patterns[8] = QPixmap( pattern09 );
|
---|
105 | patterns[9] = QPixmap( pattern10 );
|
---|
106 | patterns[10] = QPixmap( pattern11 );
|
---|
107 | patterns[11] = QPixmap( pattern12 );
|
---|
108 | patterns[12] = QPixmap( pattern13 );
|
---|
109 | patterns[13] = QPixmap( pattern14 );
|
---|
110 |
|
---|
111 | QRect rect = table->cellRect( 0, 1 );
|
---|
112 | QPixmap pix( rect.width(), rect.height() );
|
---|
113 |
|
---|
114 | for ( int i = 0; i < ChartForm::MAX_ELEMENTS; ++i ) {
|
---|
115 | Element element = (*m_elements)[i];
|
---|
116 |
|
---|
117 | if ( element.isValid() )
|
---|
118 | table->setText(
|
---|
119 | i, 0,
|
---|
120 | QString( "%1" ).arg( element.value(), 0, 'f',
|
---|
121 | m_decimalPlaces ) );
|
---|
122 |
|
---|
123 | QColor color = element.valueColor();
|
---|
124 | pix.fill( color );
|
---|
125 | table->setPixmap( i, 1, pix );
|
---|
126 | table->setText( i, 1, color.name() );
|
---|
127 |
|
---|
128 | QComboBox *combobox = new QComboBox;
|
---|
129 | for ( int j = 0; j < MAX_PATTERNS; ++j )
|
---|
130 | combobox->insertItem( patterns[j] );
|
---|
131 | combobox->setCurrentItem( element.valuePattern() - 1 );
|
---|
132 | table->setCellWidget( i, 2, combobox );
|
---|
133 |
|
---|
134 | table->setText( i, 3, element.label() );
|
---|
135 |
|
---|
136 | color = element.labelColor();
|
---|
137 | pix.fill( color );
|
---|
138 | table->setPixmap( i, 4, pix );
|
---|
139 | table->setText( i, 4, color.name() );
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void SetDataForm::currentChanged( int, int col )
|
---|
146 | {
|
---|
147 | colorPushButton->setEnabled( col == 1 || col == 4 );
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void SetDataForm::valueChanged( int row, int col )
|
---|
152 | {
|
---|
153 | if ( col == 0 ) {
|
---|
154 | bool ok;
|
---|
155 | double d = table->text( row, col ).toDouble( &ok );
|
---|
156 | if ( ok && d > EPSILON )
|
---|
157 | table->setText(
|
---|
158 | row, col, QString( "%1" ).arg(
|
---|
159 | d, 0, 'f', m_decimalPlaces ) );
|
---|
160 | else if ( !table->text( row, col ).isEmpty() )
|
---|
161 | table->setText( row, col, table->text( row, col ) + "?" );
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | void SetDataForm::setColor()
|
---|
167 | {
|
---|
168 | setColor( table->currentRow(), table->currentColumn() );
|
---|
169 | table->setFocus();
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | void SetDataForm::setColor( int row, int col )
|
---|
174 | {
|
---|
175 | if ( !( col == 1 || col == 4 ) )
|
---|
176 | return;
|
---|
177 |
|
---|
178 | QColor color = QColorDialog::getColor(
|
---|
179 | QColor( table->text( row, col ) ),
|
---|
180 | this, "color dialog" );
|
---|
181 | if ( color.isValid() ) {
|
---|
182 | QPixmap pix = table->pixmap( row, col );
|
---|
183 | pix.fill( color );
|
---|
184 | table->setPixmap( row, col, pix );
|
---|
185 | table->setText( row, col, color.name() );
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | void SetDataForm::accept()
|
---|
191 | {
|
---|
192 | bool ok;
|
---|
193 | for ( int i = 0; i < ChartForm::MAX_ELEMENTS; ++i ) {
|
---|
194 | Element &element = (*m_elements)[i];
|
---|
195 | double d = table->text( i, 0 ).toDouble( &ok );
|
---|
196 | if ( ok )
|
---|
197 | element.setValue( d );
|
---|
198 | else
|
---|
199 | element.setValue( Element::INVALID );
|
---|
200 | element.setValueColor( QColor( table->text( i, 1 ) ) );
|
---|
201 | element.setValuePattern(
|
---|
202 | ((QComboBox*)table->cellWidget( i, 2 ))->currentItem() + 1 );
|
---|
203 | element.setLabel( table->text( i, 3 ) );
|
---|
204 | element.setLabelColor( QColor( table->text( i, 4 ) ) );
|
---|
205 | }
|
---|
206 |
|
---|
207 | QDialog::accept();
|
---|
208 | }
|
---|