diff --git a/examples/DrawIntoPainter/mainwindow.cpp b/examples/DrawIntoPainter/mainwindow.cpp index de5bf09..670913c 100644 --- a/examples/DrawIntoPainter/mainwindow.cpp +++ b/examples/DrawIntoPainter/mainwindow.cpp @@ -1,411 +1,426 @@ /** * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. * * This file is part of the KD Chart library. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "mainwindow.h" #include "framewidget.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include using namespace KChart; static QPixmap drawIntoPixmap( const QSize& size, KChart::Chart* chart ) { QPixmap pix( size ); pix.fill( Qt::white ); QPainter painter( &pix ); chart->paint( &painter, QRect( 0, 0, size.width(), size.height() ) ); return pix; } // When set, this example uses FrameWidget which uses Chart::paint to paint itself. // When not set, this example uses a Chart widget directly. #define USE_FRAME_WIDGET 1 MainWindow::MainWindow( QWidget* parent ) : QWidget( parent ) { setupUi( this ); QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame ); #ifdef USE_FRAME_WIDGET FrameWidget* chartFrameWidget = new FrameWidget(); chartLayout->addWidget( chartFrameWidget ); #endif hSBar->setVisible( false ); vSBar->setVisible( false ); m_model.loadFromCSV( ":/data" ); // Set up the diagram m_lines = new LineDiagram(); m_lines->setModel( &m_model ); CartesianAxis *xAxis = new CartesianAxis( m_lines ); CartesianAxis *yAxis = new CartesianAxis( m_lines ); CartesianAxis *axisTop = new CartesianAxis( m_lines ); CartesianAxis *axisRight = new CartesianAxis( m_lines ); xAxis->setPosition( KChart::CartesianAxis::Bottom ); yAxis->setPosition( KChart::CartesianAxis::Left ); axisTop->setPosition( KChart::CartesianAxis::Top ); axisRight->setPosition( KChart::CartesianAxis::Right ); m_lines->addAxis( xAxis ); m_lines->addAxis( yAxis ); m_lines->addAxis( axisTop ); m_lines->addAxis( axisRight ); m_chart = new Chart(); //m_chart->setGlobalLeading(10,10,10,10); // by default there is no leading #ifdef USE_FRAME_WIDGET chartFrameWidget->setChart( m_chart ); // make sure, we re-draw after changing one of the chart's properties connect( m_chart, SIGNAL(propertiesChanged()), chartFrameWidget, SLOT(update()) ) ; #else chartLayout->addWidget( m_chart ); #endif m_chart->coordinatePlane()->replaceDiagram( m_lines ); for ( int iColumn = 0; iColumn < m_lines->model()->columnCount(); ++iColumn ) { QPen pen(m_lines->pen( iColumn )); pen.setWidth(4); m_lines->setPen( iColumn, pen ); } FrameAttributes faChart( m_chart->frameAttributes() ); faChart.setVisible( true ); faChart.setPen( QPen(QColor( 0x60, 0x60, 0xb0 ), 8 ) ); m_chart->setFrameAttributes( faChart ); BackgroundAttributes baChart( m_chart->backgroundAttributes() ); baChart.setVisible( true ); baChart.setBrush( QColor( 0xd0, 0xd0, 0xff ) ); m_chart->setBackgroundAttributes( baChart ); // Set up the legend m_legend = new Legend( m_lines, m_chart ); m_legend->setPosition( Position::South ); m_legend->setAlignment( Qt::AlignRight ); m_legend->setShowLines( false ); m_legend->setTitleText( tr( "Legend" ) ); m_legend->setOrientation( Qt::Horizontal ); // setting the legend frame and background to the same color: const QColor legendColor( 0xff, 0xe0, 0x80 ); FrameAttributes faLegend( m_legend->frameAttributes() ); faLegend.setVisible( true ); faLegend.setPen( QPen( legendColor, 1 ) ); m_legend->setFrameAttributes( faLegend ); BackgroundAttributes baLegend( m_legend->backgroundAttributes() ); baLegend.setVisible( true ); baLegend.setBrush( legendColor ); m_legend->setBackgroundAttributes( baLegend ); m_chart->addLegend( m_legend ); // for illustration we paint the same chart at different sizes: QSize size1 = QSize( 200, 200 ); QSize size2 = QSize( 800, 800 ); m_pix1 = drawIntoPixmap( size1, m_chart ); m_pix2 = drawIntoPixmap( size2, m_chart ); m_pix2 = m_pix2.scaled( size1 ); m_smallChart1 = new QLabel( this ); m_smallChart1->setWindowTitle( "200x200" ); m_smallChart1->setPixmap( m_pix1 ); m_smallChart1->setFixedSize( m_pix1.size() ); m_smallChart1->move( width() - m_pix1.width() * 2, height() / 2 - m_pix1.height() - 5 ); m_smallChart1->show(); m_smallChart2 = new QLabel( this ); m_smallChart2->setWindowTitle( "800x800 scaled down" ); m_smallChart2->setPixmap( m_pix2 ); m_smallChart2->setFixedSize( m_pix2.size() ); m_smallChart2->move( width() - m_pix2.width() * 2, height() / 2 + 5 ); m_smallChart2->show(); faChart.setPen( QPen( QColor( 0xb0, 0xb0, 0xff ), 8 ) ); m_chart->setFrameAttributes( faChart ); // initialize attributes; this is necessary because we need to enable data value attributes before // any of them (e.g. only markers) can be displayed. but if we enable data value attributs, a default // data value text is included, even if we only wanted to set markers. so we enable DVA and then // individually disable the parts we don't want. on_paintValuesCB_toggled( false ); on_paintMarkersCB_toggled( false ); } +void MainWindow::slotPaintRequested(QPrinter *printer) +{ + QPainter painter(printer); + m_chart->paint(&painter, painter.window()); +} + +void MainWindow::on_printButton_clicked() +{ + static QPrinter printer; + QPrintPreviewDialog dialog(&printer); + connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); + dialog.exec(); +} + void MainWindow::on_lineTypeCB_currentIndexChanged( const QString & text ) { if ( text == "Normal" ) m_lines->setType( LineDiagram::Normal ); else if ( text == "Stacked" ) m_lines->setType( LineDiagram::Stacked ); else if ( text == "Percent" ) m_lines->setType( LineDiagram::Percent ); else qWarning (" Does not match any type"); } void MainWindow::on_paintLegendCB_toggled( bool checked ) { KChart::Legend* legend = m_chart->legend(); if ( checked != ( legend != nullptr ) ) { if ( checked ) m_chart->addLegend( m_legend ); else m_chart->takeLegend( legend ); } } void MainWindow::on_paintValuesCB_toggled( bool checked ) { const int colCount = m_lines->model()->columnCount(); for ( int iColumn = 0; iColumn < colCount; ++iColumn ) { DataValueAttributes a = m_lines->dataValueAttributes( iColumn ); a.setVisible( true ); TextAttributes ta = a.textAttributes(); ta.setRotation( 0 ); ta.setFont( QFont( "Comic", 10 ) ); ta.setPen( m_lines->brush( iColumn ).color() ); ta.setVisible( checked ); a.setTextAttributes( ta ); m_lines->setDataValueAttributes( iColumn, a ); } } void MainWindow::on_paintMarkersCB_toggled( bool checked ) { paintMarkers( checked, QSize() ); } void MainWindow::on_markersStyleCB_currentIndexChanged( const QString & text ) { Q_UNUSED( text ); on_paintMarkersCB_toggled( paintMarkersCB->isChecked() ); } void MainWindow::on_markersWidthSB_valueChanged( int i ) { Q_UNUSED( i ); markersHeightSB->setValue( markersWidthSB->value() ); on_paintMarkersCB_toggled( paintMarkersCB->isChecked() ); } void MainWindow::on_markersHeightSB_valueChanged( int i ) { Q_UNUSED( i ); markersWidthSB->setValue( markersHeightSB->value() ); on_paintMarkersCB_toggled( paintMarkersCB->isChecked() ); } void MainWindow::on_displayAreasCB_toggled( bool checked ) { const int colCount = m_lines->model()->columnCount(); for ( int iColumn = 0; iColumn < colCount; ++iColumn ) { LineAttributes la( m_lines->lineAttributes( iColumn ) ); la.setDisplayArea( checked ); if ( checked ) la.setTransparency( transparencySB->value() ); m_lines->setLineAttributes( iColumn, la ); } } void MainWindow::on_transparencySB_valueChanged( int alpha ) { Q_UNUSED( alpha ); if ( !displayAreasCB->isChecked() ) displayAreasCB->setChecked( true ); else on_displayAreasCB_toggled( true ); } void MainWindow::on_zoomFactorSB_valueChanged( qreal factor ) { const bool isZoomedIn = factor > 1.0f; hSBar->setVisible( isZoomedIn ); vSBar->setVisible( isZoomedIn ); if ( !isZoomedIn ) { hSBar->setValue( 500 ); vSBar->setValue( 500 ); } m_chart->coordinatePlane()->setZoomFactorX( factor ); m_chart->coordinatePlane()->setZoomFactorY( factor ); } void MainWindow::on_hSBar_valueChanged( int hPos ) { m_chart->coordinatePlane()->setZoomCenter( QPointF( hPos / 1000.0, vSBar->value() / 1000.0 ) ); m_chart->update(); } void MainWindow::on_vSBar_valueChanged( int vPos ) { m_chart->coordinatePlane()->setZoomCenter( QPointF( hSBar->value() / 1000.0, vPos / 1000.0 ) ); } // since DataValue markers have no relative sizing mode we need to scale them for printing void MainWindow::paintMarkers( bool checked, const QSize& printSize ) { MarkerAttributes::MarkerStylesMap map; map.insert( 0, MarkerAttributes::MarkerSquare ); map.insert( 1, MarkerAttributes::MarkerCircle ); map.insert( 2, MarkerAttributes::MarkerRing ); map.insert( 3, MarkerAttributes::MarkerCross ); map.insert( 4, MarkerAttributes::MarkerDiamond ); // next: Specify column- / cell-specific attributes! const int colCount = m_lines->model()->columnCount(); for ( int iColumn = 0; iColumn < colCount; ++iColumn ) { DataValueAttributes dva = m_lines->dataValueAttributes( iColumn ); dva.setVisible( true ); MarkerAttributes ma( dva.markerAttributes() ); switch ( markersStyleCB->currentIndex() ) { case 0: ma.setMarkerStyle( MarkerAttributes::MarkerSquare ); break; case 1: // Column-specific attributes ma.setMarkerStyle( map.value( iColumn ) ); break; case 2: ma.setMarkerStyle( MarkerAttributes::MarkerCircle ); break; case 3: ma.setMarkerStyle( MarkerAttributes::MarkerDiamond ); break; case 4: ma.setMarkerStyle( MarkerAttributes::Marker1Pixel ); break; case 5: ma.setMarkerStyle( MarkerAttributes::Marker4Pixels ); break; case 6: ma.setMarkerStyle( MarkerAttributes::MarkerRing ); break; case 7: ma.setMarkerStyle( MarkerAttributes::MarkerCross ); break; case 8: ma.setMarkerStyle( MarkerAttributes::MarkerFastCross ); break; default: Q_ASSERT( false ); } ma.setVisible( checked ); qreal factorWidth = printSize.isValid() ? ( printSize.width() / m_chart->rect().width() ) : 1.0f; qreal factorHeight = printSize.isValid() ? ( printSize.height() / m_chart->rect().height() ) : 1.0f; ma.setMarkerSize( QSize( markersWidthSB->value() * factorWidth, markersHeightSB->value() * factorHeight ) ); dva.setMarkerAttributes( ma ); m_lines->setDataValueAttributes( iColumn, dva ); // make a special one for certain values DataValueAttributes yellowAttributes( dva ); MarkerAttributes yellowMarker( yellowAttributes.markerAttributes() ); yellowMarker.setMarkerColor( Qt::yellow ); yellowAttributes.setMarkerAttributes( yellowMarker ); const int rowCount = m_lines->model()->rowCount(); for ( int j = 0; j < rowCount; ++j ) { QModelIndex index = m_lines->model()->index( j, iColumn, QModelIndex() ); QBrush brush = m_lines->model()->headerData( iColumn, Qt::Vertical, DatasetBrushRole ).value(); qreal value = m_lines->model()->data( index ).toReal(); /* Set a specific color - marker for a specific value */ if ( value == 13 ) { m_lines->setDataValueAttributes( index, yellowAttributes ); } } } } void MainWindow::on_savePB_clicked() { const QString file = QFileDialog::getSaveFileName( this, tr( "Choose PNG File..." ) ); if ( file.isEmpty() ) return; qDebug() << "Painting into PNG"; QPixmap qpix( 600, 600 ); QPainter painter( &qpix ); painter.setBrush( Qt::white ); painter.fillRect( 0, 0, 600, 600, Qt::white); m_chart->paint( &painter, QRect( 100, 100, 400, 400 ) ); painter.end(); qpix.save( file, "PNG" ); qDebug() << "Painting into PNG - done"; } void MainWindow::on_savePDF_clicked() { const QString file = QFileDialog::getSaveFileName( this, tr( "Choose PDF File..." ) ); if ( file.isEmpty() ) return; qDebug() << "Painting into PDF"; QPrinter printer( QPrinter::HighResolution ); printer.setOrientation( QPrinter::Landscape ); printer.setOutputFormat( QPrinter::PdfFormat ); printer.setOutputFileName( file ); paintMarkers( paintMarkersCB->isChecked(), printer.pageRect().size() ); QPainter painter; painter.begin( &printer ); m_chart->paint( &painter, printer.pageRect() ); painter.end(); paintMarkers( paintMarkersCB->isChecked(), m_chart->geometry().size() ); qDebug() << "Painting into PDF - done"; } void MainWindow::resizeEvent( QResizeEvent * ) { m_smallChart1->move( width() - m_pix1.width() * 2, height() / 2 - m_pix1.height() - 5 ); m_smallChart2->move( width() - m_pix2.width() * 2, height() / 2 + 5 ); } diff --git a/examples/DrawIntoPainter/mainwindow.h b/examples/DrawIntoPainter/mainwindow.h index 03a34f3..6ba23d8 100644 --- a/examples/DrawIntoPainter/mainwindow.h +++ b/examples/DrawIntoPainter/mainwindow.h @@ -1,83 +1,85 @@ /** * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. * * This file is part of the KD Chart library. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include "ui_mainwindow.h" #include #include namespace KChart { class Chart; class DatasetProxyModel; class LineDiagram; class Legend; } QT_BEGIN_NAMESPACE class QLabel; +class QPrinter; QT_END_NAMESPACE class MainWindow : public QWidget, private Ui::MainWindow { Q_OBJECT public: MainWindow( QWidget* parent = nullptr ); protected: /** * Adjusts the two small charts when the window is resized. */ /* reimp */ void resizeEvent ( QResizeEvent * ) override; private slots: void on_lineTypeCB_currentIndexChanged( const QString & text ); void on_paintLegendCB_toggled( bool checked ); void on_paintValuesCB_toggled( bool checked ); void on_paintMarkersCB_toggled( bool checked ); void on_markersStyleCB_currentIndexChanged( const QString & text ); void on_markersWidthSB_valueChanged( int i ); void on_markersHeightSB_valueChanged( int i); void on_displayAreasCB_toggled( bool checked ); void on_transparencySB_valueChanged( int i ); void on_zoomFactorSB_valueChanged( qreal factor ); void on_hSBar_valueChanged( int value ); void on_vSBar_valueChanged( int value ); void on_savePB_clicked(); void on_savePDF_clicked(); - + void slotPaintRequested(QPrinter *printer); + void on_printButton_clicked(); private: void paintMarkers( bool checked, const QSize& printSize ); private: KChart::Chart* m_chart; TableModel m_model; KChart::DatasetProxyModel* m_datasetProxy; KChart::LineDiagram* m_lines; KChart::Legend* m_legend; QPixmap m_pix1, m_pix2; QLabel* m_smallChart1; QLabel* m_smallChart2; }; #endif /* MAINWINDOW_H */ diff --git a/examples/DrawIntoPainter/mainwindow.ui b/examples/DrawIntoPainter/mainwindow.ui index 500a13c..be8db20 100644 --- a/examples/DrawIntoPainter/mainwindow.ui +++ b/examples/DrawIntoPainter/mainwindow.ui @@ -1,477 +1,484 @@ MainWindow 0 0 700 618 KD Chart drawing into a foreign QPainter Display Data Values Qt::Horizontal 6 0 Line Chart Type Normal Stacked Percent Qt::Horizontal 6 0 Markers Paint Markers Markers Style Square Map Circle Diamond 1 Pixel 4 Pixels Ring Cross Fast Cross Qt::Horizontal 6 0 Markers Size 6 0 6 0 Width Height 6 0 10 10 Qt::Horizontal 6 0 Areas Display Areas 6 0 Opacity 255 20 Qt::Horizontal 6 0 Zoom 6 0 Zoom Factor 0.010000000000000 9.900000000000000 0.100000000000000 1.000000000000000 Qt::Horizontal Display Legend true Qt::Vertical 170 16 + + + + Print + + + Save as PNG Save as PDF Qt::Horizontal 300 400 QFrame::StyledPanel QFrame::Raised 999 10 100 500 Qt::Vertical 999 10 100 500 Qt::Horizontal diff --git a/examples/HeadersFooters/Advanced/mainwindow.cpp b/examples/HeadersFooters/Advanced/mainwindow.cpp index 0b2b3bc..30e1145 100644 --- a/examples/HeadersFooters/Advanced/mainwindow.cpp +++ b/examples/HeadersFooters/Advanced/mainwindow.cpp @@ -1,210 +1,215 @@ /** * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. * * This file is part of the KD Chart library. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "mainwindow.h" #include #include #include #include #include #include #include #include #include #include -#include +#include class HeaderItem : public QTreeWidgetItem { public: HeaderItem( KChart::HeaderFooter* header, QTreeWidget* parent ) : QTreeWidgetItem( parent ), m_header( header ) {} KChart::HeaderFooter* header() const { return m_header; } private: KChart::HeaderFooter* m_header; }; MainWindow::MainWindow( QWidget* parent ) : QWidget( parent ) { setupUi( this ); QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame ); m_chart = new KChart::Chart(); chartLayout->addWidget( m_chart ); m_model.loadFromCSV( ":/data" ); // Set up the diagram m_lines = new KChart::LineDiagram(); m_lines->setModel( &m_model ); m_chart->coordinatePlane()->replaceDiagram( m_lines ); m_chart->update(); } void MainWindow::on_defaultButton_clicked() { addHeaderFooter( tr("header west"), KChart::HeaderFooter::Header, KChart::Position::West); addHeaderFooter( tr("header north"), KChart::HeaderFooter::Header, KChart::Position::North); addHeaderFooter( tr("header south"), KChart::HeaderFooter::Header, KChart::Position::South); addHeaderFooter( tr("header east"), KChart::HeaderFooter::Header, KChart::Position::East); addHeaderFooter( tr("header center"), KChart::HeaderFooter::Header, KChart::Position::Center); addHeaderFooter( tr("header north west"), KChart::HeaderFooter::Header, KChart::Position::NorthWest); addHeaderFooter( tr("header north east"), KChart::HeaderFooter::Header, KChart::Position::NorthEast); addHeaderFooter( tr("header south west"), KChart::HeaderFooter::Header, KChart::Position::SouthWest); addHeaderFooter( tr("header south east"), KChart::HeaderFooter::Header, KChart::Position::SouthEast); addHeaderFooter( tr("footer west"), KChart::HeaderFooter::Footer, KChart::Position::West); addHeaderFooter( tr("footer north"), KChart::HeaderFooter::Footer, KChart::Position::North); addHeaderFooter( tr("footer south"), KChart::HeaderFooter::Footer, KChart::Position::South); addHeaderFooter( tr("footer east"), KChart::HeaderFooter::Footer, KChart::Position::East); addHeaderFooter( tr("footer center"), KChart::HeaderFooter::Footer, KChart::Position::Center); addHeaderFooter( tr("footer north west"), KChart::HeaderFooter::Footer, KChart::Position::NorthWest); addHeaderFooter( tr("footer north east"), KChart::HeaderFooter::Footer, KChart::Position::NorthEast); addHeaderFooter( tr("footer south west"), KChart::HeaderFooter::Footer, KChart::Position::SouthWest); addHeaderFooter( tr("footer south east"), KChart::HeaderFooter::Footer, KChart::Position::SouthEast); } void MainWindow::setupAddHeaderDialog( QDialog* dlg, Ui::AddHeaderDialog& conf ) const { conf.setupUi( dlg ); conf.textED->setFocus(); // Note: Header/Footer position can be Center but it can not be Floating const QStringList labels = KChart::Position::printableNames( KChart::Position::IncludeCenter ); const QList names = KChart::Position::names( KChart::Position::IncludeCenter ); for ( int i = 0, end = qMin( labels.size(), names.size() ); i != end ; ++i ) conf.positionCO->addItem( labels[i], names[i] ); } void MainWindow::addHeaderFooter( const QString &text, KChart::HeaderFooter::HeaderFooterType type, KChart::Position position ) const { KChart::HeaderFooter* headerFooter = new KChart::HeaderFooter( m_chart ); m_chart->addHeaderFooter( headerFooter ); headerFooter->setText( text ); KChart::TextAttributes attrs( headerFooter->textAttributes() ); attrs.setPen( QPen( Qt::red ) ); headerFooter->setTextAttributes( attrs ); headerFooter->setType( type ); headerFooter->setPosition( position ); HeaderItem* newItem = new HeaderItem( headerFooter, headersTV ); newItem->setText( 0, headerFooter->text() ); newItem->setText( 1, type == KChart::HeaderFooter::Header ? tr( "Header" ) : tr( "Footer" ) ); newItem->setText( 2, headerFooter->position().name() ); m_chart->update(); } + +void MainWindow::slotPaintRequested(QPrinter *printer) +{ + QPainter painter(printer); + m_chart->paint(&painter, painter.window()); +} + void MainWindow::on_printButton_clicked() { static QPrinter printer; - QPrintDialog dialog(&printer); - if (!dialog.exec()) - return; - QPainter painter(&printer); - m_chart->paint(&painter, painter.window()); + QPrintPreviewDialog dialog(&printer); + connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); + dialog.exec(); } void MainWindow::on_addHeaderPB_clicked() { QDialog dlg; Ui::AddHeaderDialog conf; setupAddHeaderDialog( &dlg, conf ); conf.typeCO->setCurrentIndex( 0 ); // let us start with "Header" conf.positionCO->setCurrentIndex( 0 ); if ( dlg.exec() ) { addHeaderFooter( conf.textED->text(), conf.typeCO->currentText() == "Header" ? KChart::HeaderFooter::Header : KChart::HeaderFooter::Footer, KChart::Position::fromName( conf.positionCO->itemData(conf.positionCO->currentIndex() ).toByteArray() ) ); } } void MainWindow::on_editHeaderPB_clicked() { if ( headersTV->selectedItems().size() == 0 ) return; HeaderItem* item = static_cast( headersTV->selectedItems().first() ); KChart::HeaderFooter* headerFooter = item->header(); QDialog dlg; Ui::AddHeaderDialog conf; setupAddHeaderDialog( &dlg, conf ); conf.textED->setText( headerFooter->text() ); conf.typeCO->setCurrentIndex( headerFooter->type() == KChart::HeaderFooter::Header ? 0 : 1 ); conf.positionCO->setCurrentIndex( conf.positionCO->findText(headerFooter->position().printableName())); if ( dlg.exec() ) { headerFooter->setText( conf.textED->text() ); headerFooter->setType( conf.typeCO->currentText() == "Header" ? KChart::HeaderFooter::Header : KChart::HeaderFooter::Footer ); headerFooter->setPosition( KChart::Position::fromName( conf.positionCO->itemData( conf.positionCO->currentIndex() ).toByteArray() ) ); item->setText( 0, conf.textED->text() ); item->setText( 1, headerFooter->type() == KChart::HeaderFooter::Header ? tr( "Header" ) : tr( "Footer" ) ); item->setText( 2, conf.positionCO->currentText() ); m_chart->update(); } } void MainWindow::on_removeHeaderPB_clicked() { if ( headersTV->selectedItems().size() == 0 ) return; QList items = headersTV->selectedItems(); for ( QList::iterator it = items.begin(); it != items.end(); ++it ) { KChart::HeaderFooter* headerFooter = static_cast( (*it) )->header(); #if 0 // Note: Despite it being owned by the Chart, you *can* just // delete the header: KD Chart will notice that and // it will adjust its layout ... delete headerFooter; #else // ... but the correct way is to first take it, so the Chart // is no longer owning it: m_chart->takeHeaderFooter( headerFooter ); // ... and then delete it: delete headerFooter; #endif delete (*it); } m_chart->update(); } void MainWindow::on_headersTV_itemSelectionChanged() { removeHeaderPB->setEnabled( headersTV->selectedItems().count() > 0 ); editHeaderPB->setEnabled( headersTV->selectedItems().count() == 1 ); } diff --git a/examples/HeadersFooters/Advanced/mainwindow.h b/examples/HeadersFooters/Advanced/mainwindow.h index 50686f9..c1982cb 100644 --- a/examples/HeadersFooters/Advanced/mainwindow.h +++ b/examples/HeadersFooters/Advanced/mainwindow.h @@ -1,68 +1,70 @@ /** * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. * * This file is part of the KD Chart library. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include "ui_mainwindow.h" #include "ui_addheaderdialog.h" #include #include #include +class QPrinter; + namespace KChart { class Chart; class DatasetProxyModel; class LineDiagram; } class MainWindow : public QWidget, private Ui::MainWindow { Q_OBJECT public: MainWindow( QWidget* parent = nullptr ); private slots: void on_defaultButton_clicked(); + void slotPaintRequested(QPrinter *printer); void on_printButton_clicked(); void on_addHeaderPB_clicked(); void on_editHeaderPB_clicked(); void on_removeHeaderPB_clicked(); void on_headersTV_itemSelectionChanged(); - private: void setupAddHeaderDialog( QDialog* dlg, Ui::AddHeaderDialog& conf ) const; void addHeaderFooter( const QString &text, KChart::HeaderFooter::HeaderFooterType type, KChart::Position position ) const; KChart::Chart* m_chart; TableModel m_model; KChart::DatasetProxyModel* m_datasetProxy; KChart::LineDiagram* m_lines; }; #endif /* MAINWINDOW_H */