Back to index

scribus-ng  1.3.4.dfsg+svn20071115
gradienteditor.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 /***************************************************************************
00008                           gradienteditor  -  description
00009                              -------------------
00010     begin                : Mit Mai 26 2004
00011     copyright            : (C) 2004 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include "gradienteditor.h"
00025 #include "gradienteditor.moc"
00026 #include <qpainter.h>
00027 #include <qpixmap.h>
00028 #include <qcursor.h>
00029 #include "scpainter.h"
00030 #include "fpoint.h"
00031 extern QPixmap loadIcon(QString nam);
00032 
00033 GradientPreview::GradientPreview(QWidget *pa) : QLabel(pa)
00034 {
00035        setFrameShape( QFrame::Panel );
00036        setFrameShadow( QFrame::Sunken );
00037        setLineWidth( 2 );
00038        setEraseColor(QColor(255,255,255));
00039        setMinimumSize(QSize(200, 60));
00040        setMaximumSize(QSize(3000, 60));
00041        setMouseTracking(true);
00042        Mpressed = false;
00043        outside = true;
00044        onlyselect = true;
00045        fill_gradient = VGradient(VGradient::linear);
00046        fill_gradient.clearStops();
00047 
00048        QColor color;
00049        color = QColor(255,255,255);
00050        fill_gradient.addStop( color, 0.0, 0.5, 1.0 );
00051        color = QColor(0,0,0);
00052        fill_gradient.addStop( color, 1.0, 0.5, 1.0 );
00053 
00054        QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00055        StopM.clear();
00056        ActStop = 0;
00057        for (uint a = 0; a < fill_gradient.Stops(); ++a)
00058        {
00059               int center = qRound(cstops.at(a)->rampPoint * (width()-20))+10;
00060               StopM.append(center);
00061        }
00062 } 
00063 
00064 void GradientPreview::paintEvent(QPaintEvent *e)
00065 {
00066        QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00067        StopM.clear();
00068        for (uint a = 0; a < fill_gradient.Stops(); ++a)
00069        {
00070               int center = qRound(cstops.at(a)->rampPoint * (width()-20))+10;
00071               StopM.append(center);
00072        }
00073        QPixmap pixm(width()-20, 37);
00074        ScPainter *p = new ScPainter(&pixm, width()-20, 37);
00075        p->setPen(black);
00076        p->setLineWidth(1);
00077        p->setFillMode(2);
00078        p->fill_gradient = fill_gradient;
00079        p->setGradient(VGradient::linear, FPoint(0,20), FPoint(width()-20,20));
00080        p->drawRect(0, 0, width()-20, 37);
00081        p->end();
00082        delete p;
00083        QPainter pw;
00084        pw.begin(this);
00085        pw.drawPixmap(10, 5, pixm);
00086        for (uint a = 0; a < fill_gradient.Stops(); ++a)
00087        {
00088               int center = qRound(cstops.at(a)->rampPoint * (width()-20))+10;
00089               pw.setPen(QPen(black, 1, SolidLine, FlatCap, MiterJoin));
00090               if (StopM[QMAX(ActStop,0)] == center)
00091                      pw.setBrush(red);
00092               else
00093                      pw.setBrush(blue);
00094               QPointArray cr;
00095               cr.setPoints(3, qRound(center), 43, qRound(center-4), 56, qRound(center+4), 56);
00096               pw.drawPolygon(cr);
00097        }
00098        pw.end();
00099        QLabel::paintEvent(e);
00100 }
00101 
00102 void GradientPreview::mousePressEvent(QMouseEvent *m)
00103 {
00104        QRect fpo;
00105        Mpressed = true;
00106        ActStop = -1;
00107        QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00108        for (int yg = 0; yg < static_cast<int>(StopM.count()); ++yg)
00109        {
00110               fpo = QRect(static_cast<int>(StopM[yg])-4, 43, 8, 13);
00111               if (fpo.contains(m->pos()))
00112               {
00113                      ActStop = yg;
00114                      emit selectedColor(cstops.at(ActStop)->name, cstops.at(ActStop)->shade);
00115                      emit currTrans(cstops.at(ActStop)->opacity);
00116                      emit currStep(cstops.at(ActStop)->rampPoint);
00117                      repaint();
00118                      onlyselect = true;
00119                      return;
00120               }
00121        }
00122 }
00123 
00124 void GradientPreview::mouseReleaseEvent(QMouseEvent *m)
00125 {
00126        QRect fpo;
00127        if ((Mpressed) && (ActStop != 0) && (ActStop != static_cast<int>(StopM.count()-1)) && (ActStop != -1) && (outside))
00128        {
00129               onlyselect = false;
00130               fill_gradient.removeStop(ActStop);
00131               ActStop = 0;
00132               repaint();
00133               QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00134               emit selectedColor(cstops.at(ActStop)->name, cstops.at(ActStop)->shade);
00135               emit currTrans(cstops.at(ActStop)->opacity);
00136               emit currStep(cstops.at(ActStop)->rampPoint);
00137        }
00138        if ((m->y() < height()) && (m->y() > 43) && (m->x() > 0) && (m->x() < width()) && (ActStop == -1))
00139        {
00140               QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00141               double newStop = static_cast<double>((m->x() - 10)) / (static_cast<double>(width())-20);
00142               fill_gradient.addStop(cstops.at(0)->color, newStop, 0.5, 1.0, cstops.at(0)->name, cstops.at(0)->shade);
00143               repaint();
00144               onlyselect = false;
00145               cstops = fill_gradient.colorStops();
00146               for (int yg = 0; yg < static_cast<int>(StopM.count()); ++yg)
00147               {
00148                      fpo = QRect(static_cast<int>(StopM[yg])-4, 43, 8, 13);
00149                      if (fpo.contains(m->pos()))
00150                      {
00151                             ActStop = yg;
00152                             emit selectedColor(cstops.at(ActStop)->name, cstops.at(ActStop)->shade);
00153                             emit currTrans(cstops.at(ActStop)->opacity);
00154                             emit currStep(cstops.at(ActStop)->rampPoint);
00155                             repaint();
00156                             break;
00157                      }
00158               }
00159        }
00160        Mpressed = false;
00161        if (!onlyselect)
00162               emit gradientChanged();
00163 }
00164 
00165 void GradientPreview::mouseMoveEvent(QMouseEvent *m)
00166 {
00167        QRect fpo;
00168        qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00169        if ((!Mpressed) && (m->y() < height()) && (m->y() > 43) && (m->x() > 9) && (m->x() < width()-9))
00170        {
00171               qApp->setOverrideCursor(QCursor(loadIcon("AddPoint.png"), 1, 1), true);
00172               for (int yg = 0; yg < static_cast<int>(StopM.count()); ++yg)
00173               {
00174                      fpo = QRect(static_cast<int>(StopM[yg])-4, 43, 8, 13);
00175                      if (fpo.contains(m->pos()))
00176                      {
00177                             qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00178                             return;
00179                      }
00180               }
00181        }
00182        if ((Mpressed) && (m->y() < height()) && (m->y() > 43) && (m->x() > 9) && (m->x() < width()-9) && (ActStop != -1))
00183        {
00184               qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00185               double newStop = static_cast<double>((m->x() - 10)) / (static_cast<double>(width())-20);
00186               if (ActStop > 1)
00187               {
00188                      if (StopM[ActStop-1]+2 >= m->x())
00189                             return;
00190               }
00191               if (ActStop < static_cast<int>(StopM.count()-2))
00192               {
00193                      if (StopM[ActStop+1]-2 < m->x())
00194                             return;
00195               }
00196               StopM[ActStop] = m->x();
00197               QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00198               cstops.at(ActStop)->rampPoint = newStop;
00199               emit currStep(cstops.at(ActStop)->rampPoint);
00200               cstops.sort();
00201               onlyselect = false;
00202               repaint();
00203        }
00204        if ((Mpressed) && (outside) && (ActStop != 0) && (ActStop != static_cast<int>(StopM.count()-1)) && (ActStop != -1))
00205               qApp->setOverrideCursor(QCursor(loadIcon("DelPoint.png"), 1, 1), true);
00206 }
00207 
00208 void GradientPreview::leaveEvent(QEvent*)
00209 {
00210        if ((Mpressed) && (ActStop != 0) && (ActStop != static_cast<int>(StopM.count()-1)) && (ActStop != -1))
00211               qApp->setOverrideCursor(QCursor(loadIcon("DelPoint.png"), 1, 1), true);
00212        else
00213               qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00214        outside = true;
00215 }
00216 
00217 void GradientPreview::enterEvent(QEvent*)
00218 {
00219        outside = false;
00220 }
00221 
00222 void GradientPreview::updateDisplay()
00223 {
00224        repaint();
00225        ActStop = 0;
00226        if (!fill_gradient.colorStops().isEmpty())
00227        {
00228               QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00229               emit selectedColor(cstops.at(ActStop)->name, cstops.at(ActStop)->shade);
00230               emit currTrans(cstops.at(ActStop)->opacity);
00231               emit currStep(cstops.at(ActStop)->rampPoint);
00232        }
00233 }
00234 
00235 void GradientPreview::setActColor(QColor c, QString n, int s)
00236 {
00237        if (ActStop == -1)
00238               return;
00239        QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00240        cstops.at(ActStop)->color = c;
00241        cstops.at(ActStop)->name = n;
00242        cstops.at(ActStop)->shade = s;
00243        repaint();
00244 }
00245 
00246 void GradientPreview::setActTrans(double t)
00247 {
00248        if (ActStop == -1)
00249               return;
00250        QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00251        cstops.at(ActStop)->opacity = t;
00252        repaint();
00253 }
00254 
00255 void GradientPreview::setActStep(double t)
00256 {
00257        if (ActStop == -1)
00258               return;
00259        QPtrVector<VColorStop> cstops = fill_gradient.colorStops();
00260        cstops.at(ActStop)->rampPoint = t;
00261        repaint();
00262 }
00263 
00264 GradientEditor::GradientEditor(QWidget *pa) : QLabel(pa)
00265 {
00266        Form1Layout = new QVBoxLayout( this, 0, 0, "Form1Layout");
00267        Preview = new GradientPreview(this);
00268        Form1Layout->addWidget(Preview);
00269        Layout1 = new QHBoxLayout(0,0,0,"Layout1");
00270        Layout1->setSpacing( 4 );
00271        Layout1->setMargin( 0 );
00272        Position = new QSpinBox( this, "shspin" );
00273        Position->setMinValue(0);
00274        Position->setMaxValue(100);
00275        Position->setLineStep(1);
00276        Position->setValue(0);
00277        Desc = new QLabel( this, "Desc" );
00278        languageChange();
00279        Layout1->addWidget( Desc );
00280        Layout1->addWidget(Position);
00281        Form1Layout->addLayout(Layout1);
00282        connect(Position, SIGNAL(valueChanged(int)), this, SLOT(changePos(int)));
00283        connect(Preview, SIGNAL(currStep(double)), this, SLOT(setPos(double)));
00284        resize(minimumSizeHint());
00285 }
00286 
00287 void GradientEditor::setPos(double p)
00288 {
00289        disconnect(Position, SIGNAL(valueChanged(int)), this, SLOT(changePos(int)));
00290        Position->setValue(qRound(p * 100));
00291        connect(Position, SIGNAL(valueChanged(int)), this, SLOT(changePos(int)));
00292 }
00293 
00294 void GradientEditor::changePos(int v)
00295 {
00296        Preview->setActStep(static_cast<double>(v) / 100.0);
00297        emit gradientChanged();
00298 }
00299 
00300 void GradientEditor::languageChange()
00301 {
00302        QToolTip::remove(Preview);
00303        QToolTip::add(Preview, QRect(10,43, Preview->width()-20, 13), tr( "Add, change or remove color stops here" ) );
00304        Desc->setText( tr( "Position:" ) );
00305        Position->setSuffix( tr(" %") );
00306 }