Back to index

scribus-ng  1.3.4.dfsg+svn20071115
polygonwidget.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "polygonwidget.h"
00008 #include "polygonwidget.moc"
00009 #include "scconfig.h"
00010 #include <qpointarray.h>
00011 #include <qpainter.h>
00012 #include <qrect.h>
00013 #include <qwmatrix.h>
00014 #include <cmath>
00015 #include "util.h"
00016 
00017 using namespace std;
00018 
00019 PolygonWidget::PolygonWidget(QWidget* parent, int polyC, int polyFd, double polyF, bool polyS, double polyR) : QWidget( parent )
00020 {
00021        Pre = new QPixmap(101, 101);
00022        Pre->fill(white);
00023        PFactor = polyF;
00024        PolygonPropsLayout = new QVBoxLayout( this, 10, 5, "PolygonPropsLayout");
00025        Layout11 = new QHBoxLayout( 0, 0, 5, "Layout11");
00026        Layout10 = new QVBoxLayout( 0, 0, 5, "Layout10");
00027        Layout2 = new QHBoxLayout( 0, 0, 5, "Layout2");
00028        Ecken = new QSpinBox( this, "Ecken" );
00029        Ecken->setMaxValue( 999 );
00030        Ecken->setMinValue( 3 );
00031        Ecken->setValue(polyC);
00032        Text1 = new QLabel( Ecken, tr("Corn&ers:"), this, "Text1" );
00033        Layout2->addWidget( Text1 );
00034        Layout2->addWidget( Ecken );
00035        Layout10->addLayout( Layout2 );
00036 
00037        Layout9_2 = new QHBoxLayout( 0, 0, 5, "Layout9_2");
00038        Layout8_2 = new QVBoxLayout( 0, 0, 5, "Layout8_2");
00039        Layout7_2 = new QHBoxLayout( 0, 0, 5, "Layout7_2");
00040        Faktor2 = new QSpinBox( this, "Faktor_2" );
00041        Faktor2->setSuffix(" ");
00042        Faktor2->setMaxValue( 180 );
00043        Faktor2->setMinValue( -180 );
00044        Faktor2->setValue(static_cast<int>(polyR));
00045        Text2_2 = new QLabel( Faktor2, tr("&Rotation:"), this, "Text2_2" );
00046        Layout7_2->addWidget( Text2_2 );
00047        Layout7_2->addWidget( Faktor2 );
00048        Layout8_2->addLayout( Layout7_2 );
00049        Slider2 = new QSlider( this, "Slider1_2" );
00050        Slider2->setMinValue( -180 );
00051        Slider2->setMaxValue( 180 );
00052        Slider2->setValue(static_cast<int>(polyR));
00053        Slider2->setOrientation( QSlider::Horizontal );
00054        Slider2->setTickmarks( QSlider::Right );
00055        Layout8_2->addWidget( Slider2 );
00056        Layout9_2->addLayout( Layout8_2 );
00057        Layout10->addLayout( Layout9_2 );
00058 
00059        Konvex = new QCheckBox( this, "Konvex" );
00060        Konvex->setText( tr( "Apply &Factor" ) );
00061        Konvex->setChecked(polyS);
00062        Layout10->addWidget( Konvex );
00063        Layout9 = new QHBoxLayout( 0, 0, 5, "Layout9");
00064        QSpacerItem* spacer = new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum );
00065        Layout9->addItem( spacer );
00066        Layout8 = new QVBoxLayout( 0, 0, 5, "Layout8");
00067        Layout7 = new QHBoxLayout( 0, 0, 5, "Layout7");
00068        Faktor = new QSpinBox( this, "Faktor" );
00069        Faktor->setSuffix( tr( " %" ) );
00070        Faktor->setMaxValue( 100 );
00071        Faktor->setMinValue( -100 );
00072        Faktor->setValue(polyFd);
00073        Text2 = new QLabel( Faktor, tr("&Factor:"), this, "Text2" );
00074        Layout7->addWidget( Text2 );
00075        Layout7->addWidget( Faktor );
00076        Layout8->addLayout( Layout7 );
00077        Slider1 = new QSlider( this, "Slider1" );
00078        Slider1->setMinValue( -100 );
00079        Slider1->setMaxValue( 100 );
00080        Slider1->setOrientation( QSlider::Horizontal );
00081        Slider1->setTickmarks( QSlider::Right );
00082        Slider1->setValue(polyFd);
00083        if (polyFd == 0)
00084               Konvex->setChecked(false);
00085        Layout8->addWidget( Slider1 );
00086        Layout9->addLayout( Layout8 );
00087        Layout10->addLayout( Layout9 );
00088        Layout11->addLayout( Layout10 );
00089        Preview = new QLabel( this, "Preview" );
00090        Preview->setMinimumSize( QSize( 106, 106 ) );
00091        Preview->setMaximumSize( QSize( 106, 106 ) );
00092        Preview->setFrameShape( QLabel::Panel );
00093        Preview->setFrameShadow( QLabel::Sunken );
00094        Preview->setLineWidth(2);
00095        Preview->setAlignment(AlignCenter);
00096        Preview->setPixmap(*Pre);
00097        Layout11->addWidget( Preview );
00098        PolygonPropsLayout->addLayout( Layout11 );
00099 
00100        UpdatePreView();
00101        //tooltips
00102        QToolTip::add( Ecken, "<qt>" + tr( "Number of corners for polygons" ) + "</qt>" );
00103        QToolTip::add( Faktor2, "<qt>" + tr( "Degrees of rotation for polygons" ) + "</qt>" );
00104        QToolTip::add( Slider2, "<qt>" + tr( "Degrees of rotation for polygons" ) + "</qt>" );
00105        QToolTip::add( Konvex, "<qt>" + tr( "Apply Convex/Concave Factor to change shape of Polygons" ) + "</qt>" );
00106        QToolTip::add( Preview, tr( "Sample Polygon" ) );
00107        QToolTip::add( Faktor, "<qt>" + tr( "A negative value will make the polygon concave (or star shaped), a positive value will make it convex" ) + "</qt>" );
00108        QToolTip::add( Slider1, tr( "A negative value will make the polygon concave (or star shaped), a positive value will make it convex" ) + "</qt>");
00109        // signals and slots connections
00110        connect(Faktor, SIGNAL(valueChanged(int)), this, SLOT(ValFromSpin(int)));
00111        connect(Faktor2, SIGNAL(valueChanged(int)), this, SLOT(ValFromSpin2(int)));
00112        connect(Slider1, SIGNAL(valueChanged(int)), Faktor, SLOT(setValue(int)));
00113        connect(Slider1, SIGNAL(valueChanged(int)), this, SLOT(UpdatePreView()));
00114        connect(Slider2, SIGNAL(valueChanged(int)), Faktor2, SLOT(setValue(int)));
00115        connect(Slider2, SIGNAL(valueChanged(int)), this, SLOT(UpdatePreView()));
00116        connect(Ecken, SIGNAL(valueChanged(int)), this, SLOT(UpdatePreView()));
00117        connect(Konvex, SIGNAL(clicked()), this, SLOT(UpdatePreView()));
00118 }
00119 
00120 void PolygonWidget::restoreDefaults(struct toolPrefs *prefsData)
00121 {
00122        PFactor = prefsData->polyS;
00123        Ecken->setValue(prefsData->polyC); 
00124        Faktor2->setValue(static_cast<int>(prefsData->polyR));
00125        Slider2->setValue(static_cast<int>(prefsData->polyR));
00126        Konvex->setChecked(prefsData->polyS);
00127        Faktor->setValue(prefsData->polyFd);
00128        Slider1->setValue(prefsData->polyFd);
00129        if (prefsData->polyFd == 0)
00130               Konvex->setChecked(false);
00131        UpdatePreView();
00132 }
00133 
00134 void PolygonWidget::getValues(int* polyC, int* polyFd, double* polyF, bool* polyS, double* polyR)
00135 {
00136        *polyC = Ecken->value();
00137        *polyF = PFactor;
00138        *polyS = Konvex->isChecked();
00139        *polyFd = Slider1->value();
00140        *polyR = Faktor2->value();
00141 }
00142 
00143 void PolygonWidget::ValFromSpin2(int a)
00144 {
00145        disconnect(Slider2, SIGNAL(valueChanged(int)), Faktor2, SLOT(setValue(int)));
00146        Slider2->setValue(a);
00147        connect(Slider2, SIGNAL(valueChanged(int)), Faktor2, SLOT(setValue(int)));
00148 }
00149 
00150 void PolygonWidget::ValFromSpin(int a)
00151 {
00152        disconnect(Slider1, SIGNAL(valueChanged(int)), Faktor, SLOT(setValue(int)));
00153        Slider1->setValue(a);
00154        connect(Slider1, SIGNAL(valueChanged(int)), Faktor, SLOT(setValue(int)));
00155 }
00156 
00157 void PolygonWidget::UpdatePreView()
00158 {
00159        if (Konvex->isChecked())
00160        {
00161               Slider1->setEnabled(true);
00162               Faktor->setEnabled(true);
00163        }
00164        else
00165        {
00166               Slider1->setEnabled(false);
00167               Faktor->setEnabled(false);
00168        }
00169        Pre->fill(white);
00170        QPainter p;
00171        p.begin(Pre);
00172        p.setBrush(NoBrush);
00173        p.setPen(black);
00174        QPointArray pp = RegularPolygon(100, 100, Ecken->value(), Konvex->isChecked(), GetFaktor(), Slider2->value());
00175        QRect br = pp.boundingRect();
00176        if (br.x() < 0)
00177               pp.translate(-br.x(), 0);
00178        if (br.y() < 0)
00179               pp.translate(0, -br.y());
00180        br = pp.boundingRect();
00181        if ((br.height() > 100) || (br.width() > 100))
00182        {
00183               QWMatrix ma;
00184               double sca = 100.0 / static_cast<double>(QMAX(br.width(), br.height()));
00185               ma.scale(sca, sca);
00186               pp = ma * pp;
00187        }
00188        p.drawPolygon(pp);
00189        p.end();
00190        Preview->setPixmap(*Pre);
00191 }
00192 
00193 double PolygonWidget::GetZeroFaktor()
00194 {
00195        return sqrt(pow(1.0,2.0)-pow(((sin((360.0/(Ecken->value()*2))/180*M_PI)* 2.0)/2.0),2.0));
00196 }
00197 
00198 double PolygonWidget::GetMaxFaktor()
00199 {
00200        double win = (360.0/(Ecken->value()*2)) / 180.0 * M_PI;
00201        double ret;
00202        if ((360.0/(Ecken->value()*2)) > 45)
00203               ret = 1/sin(win);
00204        else
00205               ret = 1/cos(win);
00206        return ret;
00207 }
00208 
00209 double PolygonWidget::GetFaktor()
00210 {
00211        int val = Slider1->value();
00212        if (val < 0)
00213               PFactor = GetZeroFaktor() * (100.0 + val) / 100.0;
00214        else
00215        {
00216               double ma = GetMaxFaktor();
00217               double mi = GetZeroFaktor();
00218               PFactor = ((ma - mi) * val / 100.0) + mi;
00219        }
00220        return PFactor;
00221 }