1 // SPDX-FileCopyrightText: 2002 Dominique Devriese <devriese@kde.org>
2 
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #include "imageexporteroptions.h"
6 
7 #include "ui_imageexporteroptionswidget.h"
8 
9 #include <QApplication>
10 #include <QCheckBox>
11 #include <QDesktopWidget>
12 #include <QLayout>
13 #include <QSize>
14 #include <QSpinBox>
15 
ImageExporterOptions(QWidget * parent)16 ImageExporterOptions::ImageExporterOptions( QWidget* parent )
17   : QWidget( parent ), minternallysettingstuff( false )
18 {
19   expwidget = new Ui_ImageExporterOptionsWidget();
20   expwidget->setupUi( this );
21 
22   msize = QSize( 1, 1 );
23 
24   // detecting the dpi resolutions
25   QDesktopWidget* dw = QApplication::desktop();
26   // and creating the Unit objects
27   mxunit = Unit( msize.width(), Unit::pixel, dw->logicalDpiX() );
28   myunit = Unit( msize.height(), Unit::pixel, dw->logicalDpiY() );
29 
30   maspectratio = (double)msize.height() / (double)msize.width();
31 
32   expwidget->keepAspectRatio->setChecked( true );
33   layout()->setContentsMargins( 0, 0, 0, 0 );
34 
35   expwidget->comboUnit->addItems( Unit::unitList() );
36 
37   connect( expwidget->WidthInput, SIGNAL(valueChanged(double)), this, SLOT(slotWidthChanged(double)) );
38   connect( expwidget->HeightInput, SIGNAL(valueChanged(double)), this, SLOT(slotHeightChanged(double)) );
39   connect( expwidget->comboUnit, SIGNAL(activated(int)), this, SLOT(slotUnitChanged(int)) );
40 }
41 
~ImageExporterOptions()42 ImageExporterOptions::~ImageExporterOptions()
43 {
44   delete expwidget;
45 }
46 
setGrid(bool grid)47 void ImageExporterOptions::setGrid( bool grid )
48 {
49   expwidget->showGridCheckBox->setChecked( grid );
50 }
51 
showGrid() const52 bool ImageExporterOptions::showGrid() const
53 {
54   return expwidget->showGridCheckBox->isChecked();
55 }
56 
setAxes(bool axes)57 void ImageExporterOptions::setAxes( bool axes )
58 {
59   expwidget->showAxesCheckBox->setChecked( axes );
60 }
61 
showAxes() const62 bool ImageExporterOptions::showAxes() const
63 {
64   return expwidget->showAxesCheckBox->isChecked();
65 }
66 
setImageSize(const QSize & size)67 void ImageExporterOptions::setImageSize( const QSize& size )
68 {
69   msize = size;
70   minternallysettingstuff = true;
71   expwidget->WidthInput->setValue( size.width() );
72   expwidget->HeightInput->setValue( size.height() );
73   mxunit.setValue( size.width() );
74   myunit.setValue( size.height() );
75   maspectratio = (double)msize.height() / (double)msize.width();
76   minternallysettingstuff = false;
77 }
78 
imageSize() const79 QSize ImageExporterOptions::imageSize() const
80 {
81   return QSize( (int)qRound( mxunit.getValue( Unit::pixel ) ),
82                 (int)qRound( myunit.getValue( Unit::pixel ) ) );
83 }
84 
slotWidthChanged(double w)85 void ImageExporterOptions::slotWidthChanged( double w )
86 {
87   if ( ! minternallysettingstuff && expwidget->keepAspectRatio->isChecked() )
88   {
89     minternallysettingstuff = true;
90     expwidget->HeightInput->setValue( w * maspectratio );
91     mxunit.setValue( w );
92     myunit.setValue( w * maspectratio );
93     minternallysettingstuff = false;
94   };
95 }
96 
slotHeightChanged(double h)97 void ImageExporterOptions::slotHeightChanged( double h )
98 {
99   if ( ! minternallysettingstuff && expwidget->keepAspectRatio->isChecked() )
100   {
101     minternallysettingstuff = true;
102     expwidget->WidthInput->setValue( h / maspectratio );
103     mxunit.setValue( h / maspectratio );
104     myunit.setValue( h );
105     minternallysettingstuff = false;
106   };
107 }
108 
slotUnitChanged(int index)109 void ImageExporterOptions::slotUnitChanged( int index )
110 {
111   minternallysettingstuff = true;
112   Unit::MetricalUnit newunit = Unit::intToUnit( index );
113   mxunit.convertTo( newunit );
114   myunit.convertTo( newunit );
115   int newprecision = Unit::precision( newunit );
116   expwidget->WidthInput->setDecimals( newprecision );
117   expwidget->WidthInput->setValue( mxunit.value() );
118   expwidget->HeightInput->setDecimals( newprecision );
119   expwidget->HeightInput->setValue( myunit.value() );
120   minternallysettingstuff = false;
121 }
122