Back to index

scribus-ng  1.3.4.dfsg+svn20071115
multiline.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 "multiline.h"
00008 #include "multiline.moc"
00009 
00010 #include <qpainter.h>
00011 #include <qmessagebox.h>
00012 
00013 #include "colorlistbox.h"
00014 #include "sccombobox.h"
00015 #include "scribusdoc.h"
00016 #include "page.h"
00017 #include "colorutil.h"
00018 #include "commonstrings.h"
00019 #include "sccolorengine.h"
00020 
00021 extern QPixmap loadIcon(QString nam);
00022 
00023 MultiLine::MultiLine( QWidget* parent, ScribusDoc* doc, multiLine ml, QString nam, QMap<QString,multiLine> *Sty)
00024               : QDialog( parent, "Multiline", true, 0 )
00025 {
00026        setIcon(loadIcon("AppIcon.png"));
00027        setCaption( tr( "Edit Style" ) );
00028        Docu = doc;
00029        TempVorl = ml;
00030        CurLin = 0;
00031        TempStyles = Sty;
00032        GivenName = nam;
00033        MultiLineLayout = new QVBoxLayout( this, 5, 4, "MultiLineLayout");
00034        SName = new QLineEdit(this, "Name");
00035        SName->setText(nam);
00036        MultiLineLayout->addWidget(SName);
00037        Preview = new QLabel( this, "Preview" );
00038        Preview->setFrameShape( QLabel::Panel );
00039        Preview->setFrameShadow( QLabel::Sunken );
00040        Preview->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignHCenter ) );
00041        Preview->setScaledContents( false );
00042        MultiLineLayout->addWidget( Preview );
00043 
00044        layout2 = new QHBoxLayout( 0, 0, 4, "layout2");
00045 
00046        AddStyle = new QPushButton( this, "AddStyle" );
00047        AddStyle->setPixmap(loadIcon("penciladd.png"));
00048        AddStyle->setText( QString::null );
00049        AddStyle->setAutoDefault(false);
00050        AddStyle->setDefault(false);
00051        layout2->addWidget( AddStyle );
00052 
00053        RemoveStyle = new QPushButton( this, "RemoveStyle" );
00054        RemoveStyle->setPixmap(loadIcon("pencilsub.png"));
00055        RemoveStyle->setText( QString::null );
00056        RemoveStyle->setAutoDefault(false);
00057        RemoveStyle->setDefault(false);
00058        layout2->addWidget( RemoveStyle );
00059        QSpacerItem* spacer = new QSpacerItem( 71, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00060        layout2->addItem( spacer );
00061        MultiLineLayout->addLayout( layout2 );
00062 
00063        Styles = new QListBox( this, "Styles" );
00064        RebuildList();
00065        MultiLineLayout->addWidget( Styles );
00066 
00067        Properties = new QGroupBox( this, "Properties" );
00068        Properties->setTitle( QString::null );
00069        Properties->setFrameShape( QGroupBox::NoFrame );
00070        Properties->setFrameShadow( QGroupBox::Plain );
00071        Properties->setColumnLayout(0, Qt::Vertical );
00072        Properties->layout()->setSpacing( 4 );
00073        Properties->layout()->setMargin( 0 );
00074        PropertiesLayout = new QVBoxLayout( Properties->layout() );
00075        PropertiesLayout->setAlignment( Qt::AlignTop );
00076 
00077        Dashes = new LineCombo(Properties);
00078        PropertiesLayout->addWidget( Dashes );
00079 
00080        LineEnds = new ScComboBox( false, Properties, "LineEnds" );
00081        LineEnds->insertItem(loadIcon("ButtCap.png"), tr( "Flat Cap" ) );
00082        LineEnds->insertItem(loadIcon("SquareCap.png"), tr( "Square Cap" ) );
00083        LineEnds->insertItem(loadIcon("RoundCap.png"), tr( "Round Cap" ) );
00084        PropertiesLayout->addWidget( LineEnds );
00085 
00086        LineJoin = new ScComboBox( false, Properties, "LineJoin" );
00087        LineJoin->insertItem(loadIcon("MiterJoin.png"), tr( "Miter Join" ) );
00088        LineJoin->insertItem(loadIcon("BevelJoin.png"), tr( "Bevel Join" ) );
00089        LineJoin->insertItem(loadIcon("RoundJoin.png"), tr( "Round Join" ) );
00090        PropertiesLayout->addWidget( LineJoin );
00091 
00092        layout1 = new QHBoxLayout( 0, 0, 6, "layout1");
00093 
00094        WidthText = new QLabel( Properties, "WidthText" );
00095        WidthText->setText( tr( "Line Width:" ) );
00096        WidthText->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignLeft ) );
00097        layout1->addWidget( WidthText );
00098 
00099        LWidth = new MSpinBox( 0, 300, Properties, 1 );
00100        LWidth->setSuffix( tr( " pt" ) );
00101        layout1->addWidget( LWidth );
00102        PropertiesLayout->addLayout( layout1 );
00103 
00104        layout4 = new QHBoxLayout( 0, 0, 6, "layout4");
00105        Color = new ScComboBox( false, Properties, "Color" );
00106        ColorList::Iterator it;
00107        for (it = doc->PageColors.begin(); it != doc->PageColors.end(); ++it)
00108        {
00109               Color->listBox()->insertItem( new ColorWidePixmapItem(it.data(), doc, it.key()) );
00110        }
00111        layout4->addWidget( Color );
00112        Shade = new QSpinBox( Properties, "Shade" );
00113        Shade->setSuffix( tr( " %" ) );
00114        Shade->setMaxValue( 100 );
00115        Shade->setMinValue( 0 );
00116        layout4->addWidget( Shade );
00117        PropertiesLayout->addLayout( layout4 );
00118        MultiLineLayout->addWidget( Properties );
00119 
00120        layout3 = new QHBoxLayout( 0, 0, 6, "layout3");
00121 
00122        OK = new QPushButton( CommonStrings::tr_OK, this, "OK" );
00123        OK->setAutoDefault(false);
00124        OK->setDefault(false);
00125        layout3->addWidget( OK );
00126 
00127        Cancel = new QPushButton( CommonStrings::tr_Cancel, this, "Cancel" );
00128        Cancel->setAutoDefault(false);
00129        Cancel->setDefault(false);
00130        layout3->addWidget( Cancel );
00131        MultiLineLayout->addLayout( layout3 );
00132        resize( QSize(222, 349).expandedTo(minimumSizeHint()) );
00133        clearWState( WState_Polished );
00134 
00135        // signals and slots connections
00136        connect(Styles, SIGNAL(highlighted(int)), this, SLOT(slotEditStyle(int)));
00137        connect( Cancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
00138        connect( OK, SIGNAL( clicked() ), this, SLOT( accept() ) );
00139        connect(Dashes, SIGNAL(activated(int)), this, SLOT(NewLSty()));
00140        connect(LineJoin, SIGNAL(activated(int)), this, SLOT(NewLJoin()));
00141        connect(LineEnds, SIGNAL(activated(int)), this, SLOT(NewLEnd()));
00142        connect(LWidth, SIGNAL(valueChanged(int)), this, SLOT(NewLWidth()));
00143        connect(Color, SIGNAL(activated(int)), this, SLOT(NewLColor()));
00144        connect(AddStyle, SIGNAL(clicked()), this, SLOT(NewSubLine()));
00145        connect(RemoveStyle, SIGNAL(clicked()), this, SLOT(DelSubLine()));
00146        connect(Shade, SIGNAL(valueChanged(int)), this, SLOT(NewLShade()));
00147        connect(SName, SIGNAL(lostFocus()), this, SLOT(NewName()));
00148        slotEditStyle(0);
00149        updatePreview();
00150 }
00151 
00152 void MultiLine::updatePreview()
00153 {
00154        QPixmap pm = QPixmap(200, 37);
00155        pm.fill(white);
00156        QPainter p;
00157        p.begin(&pm);
00158        for (int it = TempVorl.size()-1; it > -1; it--)
00159        {
00160               p.setPen(QPen(calcFarbe(TempVorl[it].Color, TempVorl[it].Shade),
00161                             QMAX(static_cast<int>(TempVorl[it].Width), 1),
00162                             static_cast<PenStyle>(TempVorl[it].Dash),
00163                             static_cast<PenCapStyle>(TempVorl[it].LineEnd),
00164                             static_cast<PenJoinStyle>(TempVorl[it].LineJoin)));
00165               p.drawLine(17, 18, 183, 18);
00166        }
00167        p.end();
00168        Preview->setPixmap(pm);
00169 }
00170 
00171 QColor MultiLine::calcFarbe(const QString& name, int shade)
00172 {
00173        const ScColor& color = Docu->PageColors[name];
00174        QColor tmpf = ScColorEngine::getDisplayColor(color, Docu, shade);
00175        return tmpf;
00176 }
00177 
00178 void MultiLine::updateSList()
00179 {
00180        QString tmp, tmp2;
00181        QPixmap * pm;
00182        pm = getWidePixmap(calcFarbe(TempVorl[CurLin].Color, TempVorl[CurLin].Shade));
00183        tmp2 = " "+tmp.setNum(TempVorl[CurLin].Width) + " " + tr("pt") + " ";
00184        tmp2 += CommonStrings::translatePenStyleName(static_cast<PenStyle>(TempVorl[CurLin].Dash));
00185        tmp2 += " ";
00186        
00187 //     switch (static_cast<PenStyle>(TempVorl[CurLin].Dash))
00188 //     {
00189 //     case SolidLine:
00190 //            tmp2 += tr("Solid Line");
00191 //            break;
00192 //     case DashLine:
00193 //            tmp2 += tr("Dashed Line");
00194 //            break;
00195 //     case DotLine:
00196 //            tmp2 += tr("Dotted Line");
00197 //            break;
00198 //     case DashDotLine:
00199 //            tmp2 += tr("Dash Dot Line");
00200 //            break;
00201 //     case DashDotDotLine:
00202 //            tmp2 += tr("Dash Dot Dot Line");
00203 //            break;
00204 //     default:
00205 //            tmp2 += tr("Solid Line");
00206 //            break;
00207 //     }
00208 //     tmp2 += " ";
00209        if (Styles->count() == 1)                        // to avoid Bug in Qt-3.1.2
00210        {
00211               Styles->clear();
00212               Styles->insertItem(*pm, tmp2);
00213        }
00214        else
00215               Styles->changeItem(*pm, tmp2, CurLin);
00216 }
00217 
00218 void MultiLine::reSort()
00219 {
00220        int cc = 0;
00221        struct SingleLine sl;
00222        sl.Color = TempVorl[CurLin].Color;
00223        sl.Shade = TempVorl[CurLin].Shade;
00224        sl.Dash = TempVorl[CurLin].Dash;
00225        sl.LineEnd = TempVorl[CurLin].LineEnd;
00226        sl.LineJoin = TempVorl[CurLin].LineJoin;
00227        sl.Width = TempVorl[CurLin].Width;
00228        multiLine::iterator it3;
00229        for (it3 = TempVorl.begin(); it3 != TempVorl.end(); ++it3)
00230        {
00231               if (cc == CurLin)
00232               {
00233                      TempVorl.erase(it3);
00234                      break;
00235               }
00236               cc++;
00237        }
00238        cc = 0;
00239        bool fo = false;
00240        for (multiLine::iterator it2 = TempVorl.begin(); it2 != TempVorl.end(); ++it2)
00241        {
00242               if (sl.Width < (*it2).Width)
00243               {
00244                      TempVorl.insert(it2, sl);
00245                      fo = true;
00246                      break;
00247               }
00248               cc++;
00249        }
00250        if (!fo)
00251               TempVorl.push_back(sl);
00252        CurLin = cc;
00253        RebuildList();
00254        slotEditStyle(cc);
00255        updatePreview();
00256 }
00257 
00258 void MultiLine::RebuildList()
00259 {
00260        QString tmp, tmp2;
00261        Styles->clear();
00262        QPixmap * pm2;
00263        for (multiLine::iterator it = TempVorl.begin(); it != TempVorl.end(); ++it)
00264        {
00265               pm2 = getWidePixmap(calcFarbe((*it).Color, (*it).Shade));
00266               tmp2 = " "+tmp.setNum((*it).Width) + " " + tr("pt") + " ";
00267               tmp2 += CommonStrings::translatePenStyleName(static_cast<PenStyle>((*it).Dash));
00268               tmp2 += " ";
00269               Styles->insertItem(*pm2, tmp2);
00270        }
00271 }
00272 
00273 void MultiLine::NewName()
00274 {
00275        QString NameNew = SName->text();
00276        if (NameNew != GivenName)
00277        {
00278               if (TempStyles->contains(NameNew))
00279               {
00280                      QMessageBox::warning(this, CommonStrings::trWarning, "<qt>"+ tr("Name \"%1\" isn't unique.<br/>Please choose another.").arg(NameNew)+"</qt>", tr("OK"));
00281                      SName->setText(GivenName);
00282                      SName->setFocus();
00283               }
00284               else
00285                      GivenName = NameNew;
00286        }
00287 }
00288 
00289 void MultiLine::NewSubLine()
00290 {
00291        struct SingleLine sl;
00292        sl.Color = TempVorl[CurLin].Color;
00293        sl.Shade = TempVorl[CurLin].Shade;
00294        sl.Dash = TempVorl[CurLin].Dash;
00295        sl.LineEnd = TempVorl[CurLin].LineEnd;
00296        sl.LineJoin = TempVorl[CurLin].LineJoin;
00297        sl.Width = TempVorl[CurLin].Width;
00298        int cc = 0;
00299        bool fo = false;
00300        for (multiLine::iterator it2 = TempVorl.begin(); it2 != TempVorl.end(); ++it2)
00301        {
00302               if (sl.Width < (*it2).Width)
00303               {
00304                      TempVorl.insert(it2, sl);
00305                      fo = true;
00306                      break;
00307               }
00308               cc++;
00309        }
00310        if (!fo)
00311               TempVorl.push_back(sl);
00312        CurLin = cc;
00313        RebuildList();
00314        slotEditStyle(cc);
00315        updatePreview();
00316 }
00317 
00318 void MultiLine::DelSubLine()
00319 {
00320        if (TempVorl.size() == 1)
00321               return;
00322        int cc = 0;
00323        for (multiLine::iterator it3 = TempVorl.begin(); it3 != TempVorl.end(); ++it3)
00324        {
00325               if (cc == CurLin)
00326               {
00327                      TempVorl.erase(it3);
00328                      break;
00329               }
00330               cc++;
00331        }
00332        CurLin = 0;
00333        RebuildList();
00334        slotEditStyle(0);
00335        updatePreview();
00336 }
00337 
00338 void MultiLine::NewLJoin()
00339 {
00340        PenJoinStyle c = MiterJoin;
00341        switch (LineJoin->currentItem())
00342        {
00343        case 0:
00344               c = MiterJoin;
00345               break;
00346        case 1:
00347               c = BevelJoin;
00348               break;
00349        case 2:
00350               c = RoundJoin;
00351               break;
00352        }
00353        TempVorl[CurLin].LineJoin = static_cast<int>(c);
00354        updateSList();
00355 }
00356 
00357 void MultiLine::NewLEnd()
00358 {
00359        PenCapStyle c = FlatCap;
00360        switch (LineEnds->currentItem())
00361        {
00362        case 0:
00363               c = FlatCap;
00364               break;
00365        case 1:
00366               c = SquareCap;
00367               break;
00368        case 2:
00369               c = RoundCap;
00370               break;
00371        }
00372        TempVorl[CurLin].LineEnd = static_cast<int>(c);
00373        updateSList();
00374        updatePreview();
00375 }
00376 
00377 void MultiLine::NewLSty()
00378 {
00379        PenStyle c = SolidLine;
00380        switch (Dashes->currentItem())
00381        {
00382        case 0:
00383               c = SolidLine;
00384               break;
00385        case 1:
00386               c = DashLine;
00387               break;
00388        case 2:
00389               c = DotLine;
00390               break;
00391        case 3:
00392               c = DashDotLine;
00393               break;
00394        case 4:
00395               c = DashDotDotLine;
00396               break;
00397        }
00398        TempVorl[CurLin].Dash = static_cast<int>(c);
00399        updateSList();
00400        updatePreview();
00401 }
00402 
00403 void MultiLine::NewLColor()
00404 {
00405        TempVorl[CurLin].Color = Color->currentText();
00406        updateSList();
00407        updatePreview();
00408 }
00409 
00410 void MultiLine::NewLWidth()
00411 {
00412        TempVorl[CurLin].Width = LWidth->value();
00413        reSort();
00414        updatePreview();
00415 }
00416 
00417 void MultiLine::NewLShade()
00418 {
00419        TempVorl[CurLin].Shade = Shade->value();
00420        updateSList();
00421        updatePreview();
00422 }
00423 
00424 void MultiLine::slotEditStyle(int i)
00425 {
00426        disconnect(Styles, SIGNAL(highlighted(int)), this, SLOT(slotEditStyle(int)));
00427        disconnect(Dashes, SIGNAL(activated(int)), this, SLOT(NewLSty()));
00428        disconnect(LineJoin, SIGNAL(activated(int)), this, SLOT(NewLJoin()));
00429        disconnect(LineEnds, SIGNAL(activated(int)), this, SLOT(NewLEnd()));
00430        disconnect(LWidth, SIGNAL(valueChanged(int)), this, SLOT(NewLWidth()));
00431        disconnect(Color, SIGNAL(activated(int)), this, SLOT(NewLColor()));
00432        disconnect(Shade, SIGNAL(valueChanged(int)), this, SLOT(NewLShade()));
00433        Styles->setSelected(i, true);
00434        CurLin = i;
00435        int tvs=static_cast<int>(TempVorl.size());
00436        if (i >=0 && i < tvs)
00437        {
00438               LWidth->setValue(TempVorl[i].Width);
00439               Color->setCurrentText(TempVorl[i].Color);
00440               Shade->setValue(TempVorl[i].Shade);
00441               switch (static_cast<PenStyle>(TempVorl[i].Dash))
00442               {
00443               case SolidLine:
00444                      Dashes->setCurrentItem(0);
00445                      break;
00446               case DashLine:
00447                      Dashes->setCurrentItem(1);
00448                      break;
00449               case DotLine:
00450                      Dashes->setCurrentItem(2);
00451                      break;
00452               case DashDotLine:
00453                      Dashes->setCurrentItem(3);
00454                      break;
00455               case DashDotDotLine:
00456                      Dashes->setCurrentItem(4);
00457                      break;
00458               default:
00459                      Dashes->setCurrentItem(0);
00460                      break;
00461               }
00462               switch (static_cast<PenCapStyle>(TempVorl[i].LineEnd))
00463               {
00464               case FlatCap:
00465                      LineEnds->setCurrentItem(0);
00466                      break;
00467               case SquareCap:
00468                      LineEnds->setCurrentItem(1);
00469                      break;
00470               case RoundCap:
00471                      LineEnds->setCurrentItem(2);
00472                      break;
00473               default:
00474                      LineEnds->setCurrentItem(0);
00475                      break;
00476               }
00477               switch (static_cast<PenJoinStyle>(TempVorl[i].LineJoin))
00478               {
00479               case MiterJoin:
00480                      LineJoin->setCurrentItem(0);
00481                      break;
00482               case BevelJoin:
00483                      LineJoin->setCurrentItem(1);
00484                      break;
00485               case RoundJoin:
00486                      LineJoin->setCurrentItem(2);
00487                      break;
00488               default:
00489                      LineJoin->setCurrentItem(0);
00490                      break;
00491               }
00492        }
00493        connect(Styles, SIGNAL(highlighted(int)), this, SLOT(slotEditStyle(int)));
00494        connect(Dashes, SIGNAL(activated(int)), this, SLOT(NewLSty()));
00495        connect(LineJoin, SIGNAL(activated(int)), this, SLOT(NewLJoin()));
00496        connect(LineEnds, SIGNAL(activated(int)), this, SLOT(NewLEnd()));
00497        connect(LWidth, SIGNAL(valueChanged(int)), this, SLOT(NewLWidth()));
00498        connect(Color, SIGNAL(activated(int)), this, SLOT(NewLColor()));
00499        connect(Shade, SIGNAL(valueChanged(int)), this, SLOT(NewLShade()));
00500 }
00501