24 #include "gobchartswidget.h"
25 #include "view/gobchartsfactory.h"
26 #include "view/gobchartsview.h"
27 #include "toolswidget/gobchartstoolswidget.h"
29 #include <QtCore/qmath.h>
30 #include <QVBoxLayout>
31 #include <QListWidget>
33 #include <QMessageBox>
34 #include <QFileDialog>
35 #include <QTextStream>
36 #include <QDomDocument>
43 static QIcon RoundedIcon(
const QColor& color )
45 QPixmap pixMap( QSize( 16, 16 ) );
46 pixMap.fill( QColor( 0, 0, 0, 0 ) );
49 path.addRoundedRect( QRectF( pixMap.rect() ), 15, 45, Qt::RelativeSize );
51 QPainter painter( &pixMap );
52 painter.setRenderHint( QPainter::Antialiasing );
53 painter.setPen( QPen( color, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
54 painter.setBrush( color );
55 painter.drawPath( path );
57 return QIcon( pixMap );
62 struct GobChartsWidget::GobChartsWidgetPrivate
65 GobChartsWidgetPrivate() :
66 m_gobChartsView ( NULLPOINTER ),
68 m_legend ( new QListWidget ),
69 m_horizontalSplitter( new QSplitter ),
70 m_verticalSplitter ( new QSplitter ),
71 m_frame ( new QFrame ),
72 m_model ( NULLPOINTER ),
73 m_selectionModel ( NULLPOINTER ),
76 m_legend->setAlternatingRowColors(
true );
77 m_horizontalSplitter->addWidget( m_legend );
79 QVBoxLayout *frameLayout =
new QVBoxLayout( m_frame );
80 frameLayout->addWidget( m_horizontalSplitter );
81 m_frame->setLayout( frameLayout );
82 m_frame->setFrameStyle( QFrame::Box | QFrame::Sunken );
84 m_verticalSplitter->setOrientation( Qt::Vertical );
85 m_verticalSplitter->addWidget( m_toolsWidget );
86 m_verticalSplitter->addWidget( m_frame );
87 m_verticalSplitter->setStretchFactor( 1, 1 );
90 ~GobChartsWidgetPrivate()
94 delete m_gobChartsView;
99 delete m_horizontalSplitter;
100 delete m_verticalSplitter;
112 QListWidget *m_legend;
113 QSplitter *m_horizontalSplitter;
114 QSplitter *m_verticalSplitter;
116 QAbstractItemModel *m_model;
117 QItemSelectionModel *m_selectionModel;
126 m_private( new GobChartsWidgetPrivate )
128 connect( m_private->m_toolsWidget, SIGNAL( createChart( GobChartsType ) ),
129 this, SLOT ( createChart( GobChartsType ) ) );
131 connect( m_private->m_toolsWidget, SIGNAL( clearChart() ),
132 this, SLOT ( clearChart() ) );
134 connect( m_private->m_toolsWidget, SIGNAL( saveChart() ),
135 this, SLOT ( saveChart() ) );
137 connect( m_private->m_toolsWidget, SIGNAL( loadChart() ),
138 this, SLOT ( loadChart() ) );
140 connect( m_private->m_legend, SIGNAL( itemClicked( QListWidgetItem* ) ),
141 this, SLOT ( legendItemSelected( QListWidgetItem* ) ) );
144 m_private->m_horizontalSplitter->setVisible(
false );
146 QVBoxLayout *mainLayout =
new QVBoxLayout(
this );
147 mainLayout->addWidget( m_private->m_verticalSplitter );
148 this->setLayout( mainLayout );
149 this->setFrameStyle( QFrame::Box | QFrame::Sunken );
163 if( m_private->m_gobChartsView )
165 m_private->m_gobChartsView->setDebugLoggingOn( log );
169 m_private->m_loggingOn = log;
178 m_private->m_model = model;
188 m_private->m_selectionModel = selectionModel;
194 void GobChartsWidget::graphicsItemSelected(
const QString &legendText )
196 for(
int i = 0; i < m_private->m_legend->count(); ++i )
198 QListWidgetItem *item = m_private->m_legend->item( i );
200 if( legendText == item->text() )
202 m_private->m_legend->setCurrentItem( item );
210 void GobChartsWidget::legendItemSelected( QListWidgetItem *item )
212 if( m_private->m_gobChartsView )
214 m_private->m_gobChartsView->legendItemSelected( item->text() );
220 void GobChartsWidget::createLegendItem(
const QColor &colour,
const QString &text )
222 QListWidgetItem *item =
new QListWidgetItem( RoundedIcon( colour ), text, m_private->m_legend );
228 void GobChartsWidget::clearLegend()
230 m_private->m_legend->clear();
235 void GobChartsWidget::createChart( GobChartsType type )
239 QString labels(
"" );
241 if( m_private->m_gobChartsView )
243 labels = m_private->m_gobChartsView->getStateXML(
false );
245 m_private->m_gobChartsView->close();
246 delete m_private->m_gobChartsView;
247 m_private->m_gobChartsView = NULLPOINTER;
252 if( m_private->m_gobChartsView )
254 m_private->m_gobChartsView->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
256 connect( m_private->m_gobChartsView, SIGNAL(
lastDebugLogMsg( QString ) ),
260 connect( m_private->m_toolsWidget, SIGNAL( setAllowedDataRange( qreal, qreal ) ),
261 m_private->m_gobChartsView, SLOT ( setAllowedDataRange( qreal, qreal ) ) );
263 connect( m_private->m_toolsWidget, SIGNAL( setShowTotalRange() ),
264 m_private->m_gobChartsView, SLOT ( setShowTotalRange() ) );
267 connect( m_private->m_toolsWidget, SIGNAL( setFixedColour( QColor ) ),
268 m_private->m_gobChartsView, SLOT ( setFixedColour( QColor ) ) );
270 connect( m_private->m_toolsWidget, SIGNAL( setRandomColours() ),
271 m_private->m_gobChartsView, SLOT ( setRandomColours() ) );
274 connect( m_private->m_toolsWidget, SIGNAL( setGridLineStyle( Qt::PenStyle ) ),
275 m_private->m_gobChartsView, SLOT ( setGridLineStyle( Qt::PenStyle ) ) );
277 connect( m_private->m_toolsWidget, SIGNAL( setHorizontalGridLines(
bool,
int ) ),
278 m_private->m_gobChartsView, SLOT ( setHorizontalGridLines(
bool,
int ) ) );
280 connect( m_private->m_toolsWidget, SIGNAL( setVerticalGridLines(
bool,
int ) ),
281 m_private->m_gobChartsView, SLOT ( setVerticalGridLines(
bool,
int ) ) );
283 connect( m_private->m_toolsWidget, SIGNAL( setGridColour( QColor) ),
284 m_private->m_gobChartsView, SLOT ( setGridColour( QColor ) ) );
287 connect( m_private->m_toolsWidget, SIGNAL( setLabelDetails( GobChartsLabel,QString,QFont,QColor,Qt::Alignment ) ),
288 m_private->m_gobChartsView, SLOT ( setLabelDetails( GobChartsLabel,QString,QFont,QColor,Qt::Alignment ) ) );
290 connect( m_private->m_toolsWidget, SIGNAL( requestLabelDetails( GobChartsLabel ) ),
291 m_private->m_gobChartsView, SLOT ( requestLabelDetails( GobChartsLabel ) ) );
293 connect( m_private->m_gobChartsView, SIGNAL( emitLabelDetails ( GobChartsLabel,QString,QFont,QColor,Qt::Alignment ) ),
294 m_private->m_toolsWidget, SLOT ( receiveLabelDetails( GobChartsLabel,QString,QFont,QColor,Qt::Alignment ) ) );
297 connect( m_private->m_gobChartsView, SIGNAL( highLightLegendItem( QString) ),
298 this, SLOT ( graphicsItemSelected( QString ) ) );
300 connect( m_private->m_gobChartsView, SIGNAL( createLegendItem( QColor, QString ) ),
301 this, SLOT ( createLegendItem( QColor, QString ) ) );
303 connect( m_private->m_gobChartsView, SIGNAL( clearLegend() ),
304 this, SLOT ( clearLegend() ) );
308 m_private->m_toolsWidget->emitStateSignals();
311 if( !labels.isEmpty() )
316 if( doc.setContent( labels ) )
318 m_private->m_gobChartsView->setStateXML( doc.namedItem(
"View" ), QDomNode(), false );
322 m_private->m_gobChartsView->setDebugLoggingOn( m_private->m_loggingOn );
324 if( m_private->m_model )
326 m_private->m_gobChartsView->setModel( m_private->m_model );
329 if( m_private->m_selectionModel )
331 m_private->m_gobChartsView->setSelectionModel( m_private->m_selectionModel );
335 m_private->m_horizontalSplitter->insertWidget( 0, m_private->m_gobChartsView );
336 m_private->m_horizontalSplitter->setStretchFactor( 0, 1 );
337 m_private->m_verticalSplitter->setStretchFactor( 1, 1 );
339 if( !m_private->m_horizontalSplitter->isVisible() )
341 QList< int > horList = QList< int >() << qFloor( static_cast< qreal >( rect().width() ) * 0.7 )
342 << qFloor( static_cast< qreal >( rect().width() ) * 0.3 );
344 m_private->m_horizontalSplitter->setSizes( horList );
345 m_private->m_horizontalSplitter->setVisible(
true );
348 m_private->m_gobChartsView->drawChart();
349 m_private->m_gobChartsView->show();
353 QMessageBox::critical(
this, tr(
"Error" ), tr(
"Unknown chart type selected." ) );
359 void GobChartsWidget::clearChart()
361 m_private->m_horizontalSplitter->setVisible(
false );
363 if( m_private->m_gobChartsView )
365 m_private->m_gobChartsView->close();
366 delete m_private->m_gobChartsView;
367 m_private->m_gobChartsView = NULLPOINTER;
373 void GobChartsWidget::saveChart()
375 if( m_private->m_gobChartsView )
377 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save file as" ), QDir::homePath(), tr(
"XML files (*.xml)" ) );
379 if( !fileName.isEmpty() )
381 QFile saveFile( fileName );
383 if( saveFile.open( QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate ) )
389 fileContent +=
"<GobChart>";
390 fileContent += m_private->m_gobChartsView->getStateXML();
391 fileContent += m_private->m_toolsWidget->getStateXML();
392 fileContent +=
"</GobChart>";
394 QTextStream outStream( &saveFile );
399 if( doc.setContent( fileContent ) )
401 outStream << doc.toString( 2 );
405 QMessageBox::critical(
this, tr(
"Error" ), tr(
"Failed to convert chart to required file format. XML is broken." ) );
412 QMessageBox::critical(
this, tr(
"Error" ), tr(
"Failed to open file for saving." ) );
418 QMessageBox::information(
this, tr(
"Info" ), tr(
"No active chart to save." ) );
424 void GobChartsWidget::loadChart()
426 int returnButton = QMessageBox::question(
this,
428 "This action will replace all current data. Continue?",
429 QMessageBox::Ok | QMessageBox::Cancel,
430 QMessageBox::Cancel );
432 if( returnButton == QMessageBox::Ok )
434 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load file" ), QDir::homePath(), tr(
"XML files (*.xml)" ) );
436 if( !fileName.isEmpty() )
438 QFile loadFile( fileName );
440 if( loadFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
444 if( doc.setContent( &loadFile ) )
446 QString strVal = doc.firstChildElement(
"GobChart").
447 firstChildElement (
"View" ).
448 firstChildElement (
"ChartType" ).
449 attribute (
"value" );
451 GobChartsType type =
static_cast< GobChartsType
>( strVal.toInt() );
454 if( m_private->m_gobChartsView )
456 m_private->m_gobChartsView->setStateXML( doc.namedItem(
"GobChart" ).namedItem(
"View" ),
457 doc.namedItem(
"GobChart" ).namedItem(
"Data" ) );
460 m_private->m_toolsWidget->setStateXML( type, doc.namedItem(
"GobChart" ).namedItem(
"ToolsWidget" ) );
464 QMessageBox::critical(
this, tr(
"Error" ), tr(
"Failed to load chart from file. XML is broken." ) );
471 QMessageBox::critical(
this, tr(
"Error" ), tr(
"Failed to open file for loading." ) );