24 #include "gobchartstoolswidget.h"
25 #include "ui_gobchartstoolswidget.h"
27 #include <QButtonGroup>
28 #include <QColorDialog>
29 #include <QDomDocument>
30 #include <QMessageBox>
34 class GobChartsToolsWidget::LabelDetails
39 m_font = widget->ui->labelFontComboBox->currentFont();
41 m_text = widget->ui->labelLineEdit->text();
43 m_fontSize = widget->ui->labelFontSizeComboBox->currentText().toInt();
44 m_bold = widget->ui->labelBoldButton->isChecked();
45 m_italics = widget->ui->labelItalicsButton->isChecked();
46 m_underlined = widget->ui->labelUnderlineButton->isChecked();
48 m_font.setBold( m_bold );
49 m_font.setItalic( m_italics );
50 m_font.setUnderline( m_underlined );
51 m_font.setPointSize( m_fontSize );
54 Qt::Alignment m_align;
62 bool m_labelDetailsReceived;
65 m_align ( Qt::AlignCenter ),
72 m_underlined( false ),
73 m_labelDetailsReceived( false ) {}
78 struct GobChartsToolsWidget::GobChartsToolsWidgetPrivate
81 GobChartsToolsWidget::LabelDetails m_labelDetails;
90 bool m_showTotalRange;
91 bool m_rangePreferenceSet;
94 Qt::PenStyle m_penStyle;
98 bool m_horizontalGrid;
101 QMap< GobChartsType, QToolButton* > m_typeButtonMap;
106 QButtonGroup m_labelButtonGroup;
107 QButtonGroup m_chartButtonGroup;
109 GobChartsToolsWidgetPrivate() :
110 m_loadingChart ( false ),
113 m_fixedColour ( false ),
114 m_lowerBound ( 0.0 ),
115 m_upperBound ( 0.0 ),
116 m_showTotalRange ( true ),
117 m_rangePreferenceSet( false ),
118 m_penStyle ( Qt::DotLine ),
120 m_gridColourSet ( false ),
121 m_verticalGrid ( false ),
122 m_horizontalGrid ( false ),
133 m_private( new GobChartsToolsWidgetPrivate )
136 ui->chartsRangeGroupBox->setEnabled(
false );
139 ui->labelTextGroupBox->setEnabled(
false );
140 ui->labelStyleGroupBox->setEnabled(
false );
141 ui->gridGroupBox->setEnabled(
false );
142 ui->saveButton->setEnabled(
false );
143 ui->chartClearButton->setEnabled(
false );
144 ui->chartColourPickerButton->setEnabled(
false );
145 ui->chartRangeToolButton->setEnabled(
false );
147 connect( ui->saveButton, SIGNAL( clicked() ),
this, SIGNAL(
saveChart() ) );
148 connect( ui->loadButton, SIGNAL( clicked() ),
this, SIGNAL(
loadChart() ) );
150 connect( ui->chartClearButton, SIGNAL( clicked() ),
this, SLOT ( clearAndReset() ) );
151 connect( ui->chartClearButton, SIGNAL( clicked() ),
this, SIGNAL(
clearChart() ) );
152 connect( ui->chartColourPickerButton, SIGNAL( clicked() ),
this, SLOT( chartColourPreferenceChanged() ) );
155 connect( ui->chartRangeToolButton, SIGNAL( clicked() ),
this, SLOT( chartRangePreferenceChanged() ) );
156 connect( ui->chartRangeLowerBoundarySpinBox, SIGNAL( valueChanged(
double ) ),
this, SLOT( chartRangePreferenceSelected() ) );
157 connect( ui->chartRangeUpperBoundarySpinBox, SIGNAL( valueChanged(
double ) ),
this, SLOT( chartRangePreferenceSelected() ) );
160 m_private->m_chartButtonGroup.setExclusive(
true );
161 m_private->m_chartButtonGroup.addButton( ui->chartBarToolButton );
162 m_private->m_chartButtonGroup.addButton( ui->chartPieToolButton );
163 m_private->m_chartButtonGroup.addButton( ui->chartLineToolButton );
164 connect( &m_private->m_chartButtonGroup, SIGNAL( buttonClicked( QAbstractButton* ) ),
this, SLOT( chartTypeChanged( QAbstractButton* ) ) );
166 m_private->m_typeButtonMap.insert( BAR, ui->chartBarToolButton );
167 m_private->m_typeButtonMap.insert( PIE, ui->chartPieToolButton );
168 m_private->m_typeButtonMap.insert( LINE, ui->chartLineToolButton );
171 QButtonGroup *gridLineButtonGroup =
new QButtonGroup(
this );
172 gridLineButtonGroup->setExclusive(
true );
173 gridLineButtonGroup->addButton( ui->gridDashedLineToolButton );
174 gridLineButtonGroup->addButton( ui->gridDotLineToolButton );
175 gridLineButtonGroup->addButton( ui->gridSolidLineToolButton );
176 connect( gridLineButtonGroup, SIGNAL( buttonClicked( QAbstractButton* ) ),
this, SLOT( gridLinePreferenceChanged( QAbstractButton* ) ) );
178 connect( ui->gridHorizontalToolButton, SIGNAL( clicked() ),
this, SLOT( gridLinesSelected() ) );
179 connect( ui->gridVerticalToolButton, SIGNAL( clicked() ),
this, SLOT( gridLinesSelected() ) );
180 connect( ui->gridColourToolButton, SIGNAL( clicked() ),
this, SLOT( gridColourSelected() ) );
181 connect( ui->gridHorizontalSpinBox, SIGNAL( valueChanged(
int ) ),
this, SLOT( gridLineNumbersChanged() ) );
182 connect( ui->gridVerticalSpinBox, SIGNAL( valueChanged(
int ) ),
this, SLOT( gridLineNumbersChanged() ) );
185 m_private->m_labelButtonGroup.setExclusive(
true );
186 m_private->m_labelButtonGroup.addButton( ui->labelHeaderToolButton );
187 m_private->m_labelButtonGroup.addButton( ui->labelXToolButton );
188 m_private->m_labelButtonGroup.addButton( ui->labelYToolButton );
189 connect( &m_private->m_labelButtonGroup, SIGNAL( buttonClicked( QAbstractButton* ) ),
this, SLOT( generateLabelDetailRequest( QAbstractButton* ) ) );
191 QButtonGroup *labelAlignmentButtonGroup =
new QButtonGroup(
this );
192 labelAlignmentButtonGroup->setExclusive(
true );
193 labelAlignmentButtonGroup->addButton( ui->labelAlignCentreButton );
194 labelAlignmentButtonGroup->addButton( ui->labelAlignLeftButton );
195 labelAlignmentButtonGroup->addButton( ui->labelAlignRightButton );
196 connect( labelAlignmentButtonGroup, SIGNAL ( buttonClicked( QAbstractButton* ) ),
this, SLOT( labelAlignmentPreferenceChanged( QAbstractButton* ) ) );
198 connect( ui->labelFontComboBox, SIGNAL( currentIndexChanged( QString ) ),
this, SLOT( updateLabelDetails() ) );
199 connect( ui->labelFontSizeComboBox, SIGNAL( currentIndexChanged( QString ) ),
this, SLOT( updateLabelDetails() ) );
200 connect( ui->labelLineEdit, SIGNAL( textChanged( QString ) ),
this, SLOT( updateLabelDetails() ) );
201 connect( ui->labelBoldButton, SIGNAL( clicked() ),
this, SLOT( updateLabelDetails() ) );
202 connect( ui->labelItalicsButton, SIGNAL( clicked() ),
this, SLOT( updateLabelDetails() ) );
203 connect( ui->labelUnderlineButton, SIGNAL( clicked() ),
this, SLOT( updateLabelDetails() ) );
204 connect( ui->labelFontColourToolButton, SIGNAL( clicked() ),
this, SLOT( labelColourPreferenceChanged() ) );
217 void GobChartsToolsWidget::chartTypeChanged( QAbstractButton *button )
219 if( button == ui->chartBarToolButton )
221 ui->gridGroupBox->setEnabled(
true );
222 ui->labelXToolButton->setEnabled(
true );
223 ui->labelYToolButton->setEnabled(
true );
226 else if( button == ui->chartPieToolButton )
228 ui->gridGroupBox->setEnabled(
false );
229 ui->labelXToolButton->setEnabled(
false );
230 ui->labelYToolButton->setEnabled(
false );
233 else if( button == ui->chartLineToolButton )
235 ui->gridGroupBox->setEnabled(
true );
236 ui->labelXToolButton->setEnabled(
true );
237 ui->labelYToolButton->setEnabled(
true );
241 ui->labelTextGroupBox->setEnabled(
true );
242 ui->labelStyleGroupBox->setEnabled(
true );
243 ui->saveButton->setEnabled(
true );
244 ui->chartClearButton->setEnabled(
true );
245 ui->chartColourPickerButton->setEnabled(
true );
246 ui->chartRangeToolButton->setEnabled(
true );
251 void GobChartsToolsWidget::chartColourPreferenceChanged()
253 if( ui->chartColourPickerButton->isChecked() )
255 QColor colour = QColorDialog::getColor();
257 if( colour.isValid() )
260 m_private->m_chartColour = colour;
261 m_private->m_fixedColour =
true;
266 ui->chartColourPickerButton->setChecked(
false );
267 m_private->m_fixedColour =
false;
273 m_private->m_fixedColour =
false;
279 void GobChartsToolsWidget::chartRangePreferenceChanged()
281 if( ui->chartRangeToolButton->isChecked() )
283 ui->chartsRangeGroupBox->setEnabled(
true );
287 if( m_private->m_rangePreferenceSet )
289 chartRangePreferenceSelected();
294 ui->chartsRangeGroupBox->setEnabled(
false );
296 m_private->m_showTotalRange =
true;
302 void GobChartsToolsWidget::chartRangePreferenceSelected()
307 if( !m_private->m_loadingChart )
309 qreal lower( ui->chartRangeLowerBoundarySpinBox->value() );
310 qreal upper( ui->chartRangeUpperBoundarySpinBox->value() );
312 m_private->m_showTotalRange =
false;
313 m_private->m_rangePreferenceSet =
true;
314 m_private->m_lowerBound = lower;
315 m_private->m_upperBound = upper;
323 void GobChartsToolsWidget::gridLinePreferenceChanged( QAbstractButton *button )
325 if( button == ui->gridSolidLineToolButton )
327 m_private->m_penStyle = Qt::SolidLine;
330 else if( button == ui->gridDashedLineToolButton )
332 m_private->m_penStyle = Qt::DashLine;
335 else if( button == ui->gridDotLineToolButton )
337 m_private->m_penStyle = Qt::DotLine;
344 void GobChartsToolsWidget::gridLineNumbersChanged()
347 ui->gridHorizontalSpinBox->value() );
350 ui->gridVerticalSpinBox->value() );
355 void GobChartsToolsWidget::gridLinesSelected()
357 m_private->m_verticalGrid = ui->gridVerticalToolButton->isChecked();
358 m_private->m_horizontalGrid = ui->gridHorizontalToolButton->isChecked();
365 void GobChartsToolsWidget::gridColourSelected()
367 QColor colour = QColorDialog::getColor();
369 if( colour.isValid() )
372 m_private->m_gridColour = colour;
373 m_private->m_gridColourSet =
true;
379 void GobChartsToolsWidget::updateLabelDetails()
382 if( !m_private->m_labelDetails.m_labelDetailsReceived )
384 m_private->m_labelDetails.setupLabel(
this );
386 if( ui->labelHeaderToolButton->isChecked() )
389 m_private->m_labelDetails.m_text,
390 m_private->m_labelDetails.m_font,
391 m_private->m_labelDetails.m_fontColour,
392 m_private->m_labelDetails.m_align );
394 else if( ui->labelXToolButton->isChecked() )
397 m_private->m_labelDetails.m_text,
398 m_private->m_labelDetails.m_font,
399 m_private->m_labelDetails.m_fontColour,
400 m_private->m_labelDetails.m_align );
402 else if( ui->labelYToolButton->isChecked() )
405 m_private->m_labelDetails.m_text,
406 m_private->m_labelDetails.m_font,
407 m_private->m_labelDetails.m_fontColour,
408 m_private->m_labelDetails.m_align );
415 void GobChartsToolsWidget::labelColourPreferenceChanged()
417 QColor colour = QColorDialog::getColor();
419 if( colour.isValid() )
421 m_private->m_labelDetails.m_fontColour = colour;
422 updateLabelDetails();
428 void GobChartsToolsWidget::labelAlignmentPreferenceChanged( QAbstractButton *button )
430 if( button == ui->labelAlignCentreButton )
432 m_private->m_labelDetails.m_align = Qt::AlignCenter;
434 else if( button == ui->labelAlignLeftButton )
436 m_private->m_labelDetails.m_align = Qt::AlignLeft;
438 else if( button == ui->labelAlignRightButton )
440 m_private->m_labelDetails.m_align = Qt::AlignRight;
443 updateLabelDetails();
448 void GobChartsToolsWidget::generateLabelDetailRequest( QAbstractButton *button )
450 if( button == ui->labelHeaderToolButton )
454 else if( button == ui->labelYToolButton )
458 else if( button == ui->labelXToolButton )
463 ui->labelLineEdit->setFocus();
471 m_private->m_labelDetails.m_labelDetailsReceived =
true;
473 if( label == HEADER )
475 ui->labelHeaderToolButton->setChecked(
true );
477 else if( label == YLABEL )
479 ui->labelYToolButton->setChecked(
true );
481 else if( label == XLABEL )
483 ui->labelXToolButton->setChecked(
true );
486 ui->labelLineEdit->setText( text );
487 ui->labelFontComboBox->setCurrentFont( font );
488 ui->labelBoldButton->setChecked( font.bold() );
489 ui->labelItalicsButton->setChecked( font.italic() );
490 ui->labelUnderlineButton->setChecked( font.underline() );
492 m_private->m_labelDetails.m_fontColour = colour;
494 for(
int i = 0; i < ui->labelFontSizeComboBox->count(); i++ )
496 if( ui->labelFontSizeComboBox->itemText( i ) == QString(
"%1" ).arg( font.pointSize() ) )
498 ui->labelFontSizeComboBox->setCurrentIndex( i );
503 if( align == Qt::AlignCenter )
505 ui->labelAlignCentreButton->setChecked(
true );
507 else if( align == Qt::AlignLeft )
509 ui->labelAlignLeftButton->setChecked(
true );
511 else if( align == Qt::AlignRight )
513 ui->labelAlignRightButton->setChecked(
true );
516 m_private->m_labelDetails.m_labelDetailsReceived =
false;
524 QDomElement root = doc.createElement(
"ToolsWidget" );
525 doc.appendChild( root );
528 QDomElement chart = doc.createElement(
"Chart" );
530 if( m_private->m_fixedColour )
532 chart.setAttribute(
"fixed",
"true" );
534 QDomElement colour = doc.createElement(
"ChartColour" );
535 colour.setAttribute(
"red", m_private->m_chartColour.red() );
536 colour.setAttribute(
"green", m_private->m_chartColour.green() );
537 colour.setAttribute(
"blue", m_private->m_chartColour.blue() );
539 chart.appendChild( colour );
543 chart.setAttribute(
"fixed",
"false" );
546 root.appendChild( chart );
549 QDomElement range = doc.createElement(
"Range" );
551 if( m_private->m_showTotalRange )
553 range.setAttribute(
"total",
"true" );
557 range.setAttribute(
"total",
"false" );
558 range.setAttribute(
"lower", m_private->m_lowerBound );
559 range.setAttribute(
"upper", m_private->m_upperBound );
562 root.appendChild( range );
565 QDomElement grid = doc.createElement(
"Grid" );
567 if( m_private->m_gridColourSet )
569 grid.setAttribute(
"fixedColour",
"true" );
571 QDomElement colour = doc.createElement(
"GridColour" );
572 colour.setAttribute(
"red", m_private->m_gridColour.red() );
573 colour.setAttribute(
"green", m_private->m_gridColour.green() );
574 colour.setAttribute(
"blue", m_private->m_gridColour.blue() );
576 grid.appendChild( colour );
580 grid.setAttribute(
"fixedColour",
"false" );
583 if( m_private->m_verticalGrid )
585 grid.setAttribute(
"vertical",
"true" );
586 grid.setAttribute(
"vertlines", QString(
"%1" ).arg( ui->gridVerticalSpinBox->value() ) );
590 grid.setAttribute(
"vertical",
"false" );
593 if( m_private->m_horizontalGrid )
595 grid.setAttribute(
"horizontal",
"true" );
596 grid.setAttribute(
"horlines", QString(
"%1" ).arg( ui->gridHorizontalSpinBox->value() ) );
600 grid.setAttribute(
"horizontal",
"true" );
603 if( m_private->m_penStyle == Qt::SolidLine )
605 grid.setAttribute(
"style",
"solid" );
607 else if( m_private->m_penStyle == Qt::DotLine )
609 grid.setAttribute(
"style",
"dot" );
613 grid.setAttribute(
"style",
"dash" );
616 root.appendChild( grid );
620 return doc.toString( 2 );
627 if( !toolNode.isNull() )
636 if( toolNode.firstChildElement(
"Chart" ).attribute(
"fixed" ) ==
"true" )
638 m_private->m_fixedColour =
true;
639 QDomNamedNodeMap colourMap = toolNode.namedItem(
"Chart" ).namedItem(
"ChartColour" ).attributes();
642 fixedColour.setRed ( colourMap.namedItem(
"red" ).nodeValue().toInt( &ok ) );
643 fixedColour.setGreen( colourMap.namedItem(
"green" ).nodeValue().toInt( &ok ) );
644 fixedColour.setBlue ( colourMap.namedItem(
"blue" ).nodeValue().toInt( &ok ) );
646 m_private->m_chartColour = fixedColour;
647 ui->chartColourPickerButton->setChecked(
true );
651 m_private->m_fixedColour =
false;
652 ui->chartColourPickerButton->setChecked(
false );
656 QDomNamedNodeMap rangeMap = toolNode.namedItem(
"Range" ).attributes();
658 if( rangeMap.namedItem(
"total" ).nodeValue() ==
"true" )
660 m_private->m_showTotalRange =
true;
661 ui->chartRangeToolButton->setChecked(
false );
665 m_private->m_showTotalRange =
false;
666 m_private->m_rangePreferenceSet =
true;
667 m_private->m_lowerBound = rangeMap.namedItem(
"lower" ).nodeValue().toDouble( &ok );
668 m_private->m_upperBound = rangeMap.namedItem(
"upper" ).nodeValue().toDouble( &ok );
672 m_private->m_loadingChart =
true;
673 ui->chartRangeLowerBoundarySpinBox->setValue( m_private->m_lowerBound );
674 ui->chartRangeUpperBoundarySpinBox->setValue( m_private->m_upperBound );
675 m_private->m_loadingChart =
false;
676 chartRangePreferenceSelected();
678 ui->chartRangeToolButton->setChecked(
true );
682 QDomNamedNodeMap gridMap = toolNode.namedItem(
"Grid" ).attributes();
684 if( gridMap.namedItem(
"fixedColour" ).nodeValue() ==
"true" )
686 m_private->m_gridColourSet =
true;
688 QDomNamedNodeMap colourMap = toolNode.namedItem(
"Grid" ).namedItem(
"GridColour" ).attributes();
691 gridColour.setRed ( colourMap.namedItem(
"red" ).nodeValue().toInt( &ok ) );
692 gridColour.setGreen( colourMap.namedItem(
"green" ).nodeValue().toInt( &ok ) );
693 gridColour.setBlue ( colourMap.namedItem(
"blue" ).nodeValue().toInt( &ok ) );
695 m_private->m_gridColour = gridColour;
699 m_private->m_gridColourSet =
false;
702 if( gridMap.namedItem(
"vertical" ).nodeValue() ==
"true" )
704 m_private->m_verticalGrid =
true;
705 ui->gridGroupBox->setEnabled(
true );
706 ui->gridVerticalToolButton->setChecked(
true );
707 ui->gridVerticalSpinBox->setValue( gridMap.namedItem(
"vertlines" ).toAttr().value().toInt() );
711 m_private->m_verticalGrid =
false;
712 ui->gridVerticalToolButton->setChecked(
false );
715 if( gridMap.namedItem(
"horizontal" ).nodeValue() ==
"true" )
717 m_private->m_horizontalGrid =
true;
718 ui->gridGroupBox->setEnabled(
true );
719 ui->gridHorizontalToolButton->setChecked(
true );
720 ui->gridHorizontalSpinBox->setValue( gridMap.namedItem(
"horlines" ).toAttr().value().toInt() );
724 m_private->m_horizontalGrid =
false;
725 ui->gridHorizontalToolButton->setChecked(
false );
728 if( gridMap.namedItem(
"style" ).nodeValue() ==
"solid" )
730 m_private->m_penStyle = Qt::SolidLine;
731 ui->gridSolidLineToolButton->setChecked(
true );
733 else if( gridMap.namedItem(
"style" ).nodeValue() ==
"dot" )
735 m_private->m_penStyle = Qt::DotLine;
736 ui->gridDotLineToolButton->setChecked(
true );
740 m_private->m_penStyle = Qt::DashLine;
741 ui->gridDashedLineToolButton->setChecked(
true );
746 QMessageBox::critical(
this,
"Error",
"Failed to convert one or more data values to their appropriate type." );
752 m_private->m_labelDetails.m_labelDetailsReceived =
true;
754 m_private->m_labelDetails.m_labelDetailsReceived =
false;
757 ui->labelTextGroupBox->setEnabled(
true );
758 ui->labelStyleGroupBox->setEnabled(
true );
759 ui->saveButton->setEnabled(
true );
760 ui->chartClearButton->setEnabled(
true );
761 ui->chartColourPickerButton->setEnabled(
true );
762 ui->chartRangeToolButton->setEnabled(
true );
764 const_cast< QToolButton*
>( m_private->m_typeButtonMap.value( type ) )->setChecked(
true );
768 QMessageBox::critical(
this,
"Error",
"Failed to load chart." );
777 if( m_private->m_fixedColour )
783 if( m_private->m_showTotalRange )
793 if( ui->gridGroupBox->isEnabled() )
799 if( m_private->m_gridColourSet )
810 void GobChartsToolsWidget::clearAndReset()
812 ui->labelTextGroupBox->setEnabled(
false );
813 ui->labelStyleGroupBox->setEnabled(
false );
814 ui->gridGroupBox->setEnabled(
false );
815 ui->saveButton->setEnabled(
false );
816 ui->chartClearButton->setEnabled(
false );
817 ui->chartColourPickerButton->setEnabled(
false );
818 ui->chartRangeToolButton->setEnabled(
false );
820 ui->gridHorizontalToolButton->setChecked(
false );
821 ui->gridVerticalToolButton->setChecked(
false );
822 ui->gridDotLineToolButton->setChecked(
true );
824 ui->labelBoldButton->setChecked(
false );
825 ui->labelItalicsButton->setChecked(
false );
826 ui->labelUnderlineButton->setChecked(
false );
827 ui->labelAlignCentreButton->setChecked(
false );
828 ui->labelAlignLeftButton->setChecked(
false );
829 ui->labelAlignRightButton->setChecked(
false );
834 m_private->m_chartButtonGroup.setExclusive(
false );
835 ui->chartBarToolButton->setChecked(
false );
836 ui->chartPieToolButton->setChecked(
false );
837 ui->chartLineToolButton->setChecked(
false );
838 m_private->m_chartButtonGroup.setExclusive(
true );
843 m_private->m_labelButtonGroup.setExclusive(
false );
844 ui->labelHeaderToolButton->setChecked(
false );
845 ui->labelYToolButton->setChecked(
false );
846 ui->labelXToolButton->setChecked(
false );
847 m_private->m_labelButtonGroup.setExclusive(
true );
849 ui->labelLineEdit->clear();
851 ui->chartRangeLowerBoundarySpinBox->setValue( 0.0 );
852 ui->chartRangeUpperBoundarySpinBox->setValue( 0.0 );
853 ui->gridHorizontalSpinBox->setValue( 0 );
854 ui->gridVerticalSpinBox->setValue( 0 );
856 m_private->m_labelDetails.m_labelDetailsReceived =
false;
857 m_private->m_loadingChart =
false;
858 m_private->m_fixedColour =
false;
859 m_private->m_gridColourSet =
false;
860 m_private->m_verticalGrid =
false;
861 m_private->m_horizontalGrid =
false;
862 m_private->m_rangePreferenceSet =
false;
863 m_private->m_showTotalRange =
true;