Back to index

scribus-ng  1.3.4.dfsg+svn20071115
editformats.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 "editformats.h"
00008 #include "editformats.moc"
00009 #include "edit1format.h"
00010 #include <qmessagebox.h>
00011 #include <qheader.h>
00012 
00013 #include "commonstrings.h"
00014 #include "scribusdoc.h"
00015 #include "customfdialog.h"
00016 #include "prefsmanager.h"
00017 #include "prefsfile.h"
00018 #include "fileloader.h"
00019 #include "page.h"
00020 #include "sccombobox.h"
00021 #include "util.h"
00022 
00023 extern QPixmap loadIcon(QString nam);
00024 
00025 
00026 DelStyle::DelStyle(QWidget* parent, StyleSet<ParagraphStyle>& sty, QString styleName)
00027               : QDialog( parent, "DelStyle", true, 0 )
00028 {
00029        setName( "DelStyle" );
00030        setCaption( tr( "Delete Style" ) );
00031        setIcon(loadIcon("AppIcon.png"));
00032        dialogLayout = new QVBoxLayout( this, 10, 5 );
00033        delStyleLayout = new QGridLayout;
00034        delStyleLayout->setSpacing( 5 );
00035        delStyleLayout->setMargin( 5 );
00036        deleteLabel = new QLabel( tr( "Delete Style:" ), this, "deleteLabel" );
00037        delStyleLayout->addWidget( deleteLabel, 0, 0 );
00038        styleToDelLabel = new QLabel( styleName, this, "colorToDelLabel" );
00039        delStyleLayout->addWidget( styleToDelLabel, 0, 1 );
00040        replaceLabel = new QLabel( tr( "Replace With:" ), this, "replaceLabel" );
00041        delStyleLayout->addWidget( replaceLabel, 1, 0 );
00042        replacementStyleData = new ScComboBox(false, this);
00043        replacementStyleData->insertItem( tr("No Style"));
00044 
00045        // sort the names in language specific order (PV)
00046        QStringList existingStyles;
00047        for (uint x = 0; x < sty.count(); ++x)
00048        {
00049               if (sty[x].name() != styleName)
00050                      existingStyles.append(sty[x].name());
00051        }
00052        existingStyles = sortQStringList(existingStyles);
00053        replacementStyleData->insertStringList(existingStyles);
00054 
00055        delStyleLayout->addWidget( replacementStyleData, 1, 1 );
00056        replacementStyle = replacementStyleData->text(0);
00057        dialogLayout->addLayout( delStyleLayout );
00058        okCancelLayout = new QHBoxLayout;
00059        okCancelLayout->setSpacing( 6 );
00060        okCancelLayout->setMargin( 0 );
00061        QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00062        okCancelLayout->addItem( spacer );
00063        okButton = new QPushButton( CommonStrings::tr_OK, this, "okButton" );
00064        okCancelLayout->addWidget( okButton );
00065        cancelButton = new QPushButton( CommonStrings::tr_Cancel, this, "PushButton13" );
00066        cancelButton->setDefault( true );
00067        okCancelLayout->addWidget( cancelButton );
00068        dialogLayout->addLayout( okCancelLayout );
00069        setMaximumSize(sizeHint());
00070 
00071        connect( okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
00072        connect( cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
00073        connect( replacementStyleData, SIGNAL(activated(int)), this, SLOT( ReplaceStyle(int) ) );
00074 }
00075 
00076 void DelStyle::ReplaceStyle(int id)
00077 {
00078        replacementStyle = replacementStyleData->text(id);
00079 }
00080 
00081 const QString DelStyle::getReplacementStyle()
00082 {
00083        return replacementStyle;
00084 }
00085 
00086 ChooseStyles::ChooseStyles( QWidget* parent, StyleSet<ParagraphStyle> *styleList, StyleSet<ParagraphStyle> *styleOld)
00087               : QDialog( parent, "ChooseStyles", true, 0 )
00088 {
00089        setCaption( tr( "Choose Styles" ) );
00090        setIcon(loadIcon("AppIcon.png"));
00091        ChooseStylesLayout = new QVBoxLayout( this, 10, 5, "ChooseStylesLayout");
00092        StyleView = new QListView( this, "StyleView" );
00093        StyleView->clear();
00094        StyleView->addColumn( tr( "Available Styles" ) );
00095        StyleView->header()->setClickEnabled( false, StyleView->header()->count() - 1 );
00096        StyleView->header()->setResizeEnabled( false, StyleView->header()->count() - 1 );
00097        StyleView->setSorting(-1);
00098        int counter = 0;
00099 //     bool tabEQ = false;
00100        for (uint x = 0; x < styleList->count(); ++x)
00101        {
00102               ParagraphStyle& vg ((*styleList)[x]);
00103               const ParagraphStyle* vg2 = static_cast<const ParagraphStyle*>(styleOld->resolve(vg.name()));
00104               bool found = vg2 && vg.equiv(*vg2);
00105               if (!found)
00106               {
00107                      if (vg2)
00108                             vg.setName("Copy of "+vg2->name());
00109                      QCheckListItem *item = new QCheckListItem (StyleView, vg.name(), QCheckListItem::CheckBox);
00110                      item->setOn(true);
00111                      storedStyles.insert(item, counter);
00112               }
00113               qDebug(QString("load styles: found %1 dup %2 equiv %3").arg(vg.name()).arg((ulong)vg2).arg(found));
00114               counter++;
00115        }
00116        StyleView->setSorting(0);
00117        ChooseStylesLayout->addWidget( StyleView );
00118        layout2 = new QHBoxLayout( 0, 0, 5, "layout2");
00119        QSpacerItem* spacer1 = new QSpacerItem( 71, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00120        layout2->addItem( spacer1 );
00121        OkButton = new QPushButton( CommonStrings::tr_OK, this, "OkButton" );
00122        layout2->addWidget( OkButton );
00123        CancelButton = new QPushButton( CommonStrings::tr_Cancel, this, "CancelButton" );
00124        layout2->addWidget( CancelButton );
00125        ChooseStylesLayout->addLayout( layout2 );
00126        resize(230, 280);
00127        clearWState( WState_Polished );
00128        connect(CancelButton, SIGNAL(clicked()), this, SLOT(reject()));
00129        connect(OkButton, SIGNAL(clicked()), this, SLOT(accept()));
00130 }
00131 
00132 StilFormate::StilFormate( QWidget* parent, ScribusDoc *doc) : QDialog( parent, "Formate", true, 0)
00133 {
00134        resize( 327, 260 );
00135        setCaption( tr( "Edit Styles" ) );
00136        setIcon(loadIcon("AppIcon.png"));
00137        Docu = doc;
00138        ReplaceList.clear();
00139        StilFormateLayout = new QHBoxLayout( this );
00140        StilFormateLayout->setSpacing( 5 );
00141        StilFormateLayout->setMargin( 10 );
00142 
00143        ListBox1 = new QListBox( this, "ListBox1" );
00144        ListBox1->setMinimumSize( QSize( 200, 240 ) );
00145        StilFormateLayout->addWidget( ListBox1 );
00146 
00147        Layout15 = new QVBoxLayout;
00148        Layout15->setSpacing( 6 );
00149        Layout15->setMargin( 0 );
00150 
00151        LoadS = new QPushButton( tr( "&Import" ), this, "LoadF" );
00152        Layout15->addWidget( LoadS );
00153 
00154        NewB = new QPushButton( tr( "&New" ), this, "NewB" );
00155        Layout15->addWidget( NewB );
00156 
00157        EditB = new QPushButton( tr( "&Edit" ), this, "EditB" );
00158        EditB->setDefault( true );
00159        EditB->setEnabled(false);
00160        Layout15->addWidget( EditB );
00161 
00162        DublicateB = new QPushButton( tr( "D&uplicate" ), this, "DublicateB" );
00163        DublicateB->setEnabled(false);
00164        Layout15->addWidget( DublicateB );
00165 
00166        DeleteB = new QPushButton( tr( "&Delete" ), this, "DeleteB" );
00167        DeleteB->setEnabled(false);
00168        Layout15->addWidget( DeleteB );
00169 
00170        SaveB = new QPushButton( CommonStrings::tr_Save, this, "SaveB" );
00171        Layout15->addWidget( SaveB );
00172 
00173        ExitB = new QPushButton( CommonStrings::tr_OK, this, "ExitB" );
00174        Layout15->addWidget( ExitB );
00175 
00176        CancelB = new QPushButton( CommonStrings::tr_Cancel, this, "CancelB" );
00177        Layout15->addWidget( CancelB );
00178        QSpacerItem* spacer = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
00179        Layout15->addItem( spacer );
00180        StilFormateLayout->addLayout( Layout15 );
00181 
00182        // signals and slots connections
00183        connect(CancelB, SIGNAL(clicked()), this, SLOT(reject()));
00184        connect(SaveB, SIGNAL(clicked()), this, SLOT(saveIt()));
00185        connect(ExitB, SIGNAL(clicked()), this, SLOT(accept()));
00186        connect(EditB, SIGNAL(clicked()), this, SLOT(editFormat()));
00187        connect(NewB, SIGNAL(clicked()), this, SLOT(neuesFormat()));
00188        connect(LoadS, SIGNAL(clicked()), this, SLOT(loadStyles()));
00189        connect(DublicateB, SIGNAL(clicked()), this, SLOT(dupFormat()));
00190        connect(DeleteB, SIGNAL(clicked()), this, SLOT(deleteFormat()));
00191        connect(ListBox1, SIGNAL(highlighted(QListBoxItem*)), this, SLOT(selFormat(QListBoxItem*)));
00192        connect(ListBox1, SIGNAL(selected(QListBoxItem*)), this, SLOT(selEditFormat(QListBoxItem*)));
00193        TempVorl.clear();
00194        TempVorl.redefine(doc->paragraphStyles());
00195        UpdateFList();
00196 }
00197 
00198 void StilFormate::saveIt()
00199 {
00200        emit saveStyle(this);
00201 }
00202 
00203 void StilFormate::selFormat(QListBoxItem *c)
00204 {
00205        for (uint x = 0; x < TempVorl.count(); ++x)
00206        {
00207               if (TempVorl[x].name() == c->text())
00208               {
00209                      sFnumber = x;
00210                      break;
00211               }
00212        }
00213        EditB->setEnabled(true);
00214        DublicateB->setEnabled(true);
00215        DeleteB->setEnabled(true);
00216 }
00217 
00218 void StilFormate::selEditFormat(QListBoxItem *c)
00219 {
00220        for (uint x = 0; x < TempVorl.count(); ++x)
00221        {
00222               if (TempVorl[x].name() == c->text())
00223               {
00224                      sFnumber = x;
00225                      break;
00226               }
00227        }
00228        EditB->setEnabled(true);
00229        DublicateB->setEnabled(true);
00230        DeleteB->setEnabled(true);
00231        editFormat();
00232 }
00233 
00234 void StilFormate::dupFormat()
00235 {
00236        ParagraphStyle * sty = new ParagraphStyle(TempVorl[sFnumber]);
00237        sty->setName( tr("Copy of %1").arg(TempVorl[sFnumber].name()));
00238        TempVorl.append(sty);
00239        sFnumber = TempVorl.count()-1;
00240        EditStyle* dia2 = new EditStyle(this, &TempVorl[sFnumber], TempVorl, true,
00241                                        static_cast<double>(Docu->typographicSettings.autoLineSpacing), Docu->unitIndex(), Docu);
00242        if (!dia2->exec())
00243               TempVorl.remove(sFnumber);
00244        delete dia2;
00245        UpdateFList();
00246 }
00247 
00248 void StilFormate::neuesFormat()
00249 {
00250        int selectedIndex=ListBox1->currentItem();
00251        int topIndex=ListBox1->topItem();
00252        ParagraphStyle sty;
00253        sty.setName( tr("New Style"));
00254        /*
00255        sty.setLineSpacingMode(static_cast<ParagraphStyle::LineSpacingMode>(0));
00256        sty.setLineSpacing(((Docu->toolSettings.defSize / 10.0) * 
00257                      static_cast<double>(Docu->typographicSettings.autoLineSpacing) / 100) 
00258                        + (Docu->toolSettings.defSize / 10.0));
00259        sty.setAlignment(ParagraphStyle::Leftaligned);
00260        sty.setLeftMargin(0);
00261        sty.setRightMargin(0);
00262        sty.setFirstIndent(0);
00263        sty.setGapBefore(0);
00264        sty.setGapAfter(0);
00265        sty.charStyle() = CharStyle(PrefsManager::instance()->appPrefs.AvailFonts[Docu->toolSettings.defFont],
00266                                                         Docu->toolSettings.defSize);
00267 //     sty.tabValues().clear();
00268        sty.setHasDropCap(false);
00269        sty.setDropCapLines(2);
00270        sty.setDropCapOffset(0);
00271        sty.charStyle().setFillColor(Docu->toolSettings.dPenText);
00272        sty.charStyle().setFillShade(Docu->toolSettings.dTextPenShade);
00273        sty.charStyle().setStrokeColor(Docu->toolSettings.dStrokeText);
00274        sty.charStyle().setStrokeShade(Docu->toolSettings.dTextStrokeShade);
00275        sty.setUseBaselineGrid(false);
00276        sty.charStyle().setShadowXOffset(50);
00277        sty.charStyle().setShadowYOffset(-50);
00278        sty.charStyle().setOutlineWidth(10);
00279        sty.charStyle().setUnderlineOffset(Docu->typographicSettings.valueUnderlinePos);
00280        sty.charStyle().setUnderlineWidth(Docu->typographicSettings.valueUnderlineWidth);
00281        sty.charStyle().setStrikethruOffset(Docu->typographicSettings.valueStrikeThruPos);
00282        sty.charStyle().setStrikethruWidth(Docu->typographicSettings.valueStrikeThruPos);
00283        sty.charStyle().setScaleH(1000);
00284        sty.charStyle().setScaleV(1000);
00285        sty.charStyle().setBaselineOffset(0);
00286        sty.charStyle().setTracking(0);
00287         */
00288        TempVorl.create(sty);
00289        sFnumber = TempVorl.count()-1;
00290        EditStyle* dia2 = new EditStyle(this, &TempVorl[sFnumber], TempVorl, true,  static_cast<double>(Docu->typographicSettings.autoLineSpacing), Docu->unitIndex(), Docu);
00291        if (!dia2->exec())
00292               TempVorl.remove(sFnumber);
00293        delete dia2;
00294        UpdateFList();
00295        ListBox1->setSelected(selectedIndex, true);
00296        ListBox1->setTopItem(topIndex);
00297 }
00298 
00299 void StilFormate::editFormat()
00300 {
00301        int selectedIndex=ListBox1->currentItem();
00302        int topIndex=ListBox1->topItem();
00303        EditStyle* dia = new EditStyle(this, &TempVorl[sFnumber], TempVorl, false,
00304                                       static_cast<double>(Docu->typographicSettings.autoLineSpacing), Docu->unitIndex(), Docu);
00305        dia->exec();
00306        delete dia;
00307        UpdateFList();
00308        ListBox1->setSelected(selectedIndex, true);
00309        ListBox1->setTopItem(topIndex);
00310 }
00311 
00312 void StilFormate::deleteFormat()
00313 {
00314 /*     int exit=QMessageBox::warning(this,
00315                                      CommonStrings::trWarning,
00316                                      tr("Do you really want to delete this style?"),
00317                                      tr("No"),
00318                                      tr("Yes"),
00319                                      0, 0, 0); */
00320        /* PFJ - 29.02.04 - Altered to use the correct QMessageBox value. It was 1 */
00321        /* FS - 13.03.04 the 1 is correct in this version of QMessageBox, it returns the Nr of the clicked Button either 0 or 1 or 2 */
00322 //     if (exit == 1)
00323        int selectedIndex=ListBox1->currentItem();
00324        int topIndex=ListBox1->topItem();
00325        DelStyle *dia = new DelStyle(this, TempVorl, TempVorl[sFnumber].name());
00326        if (dia->exec())
00327        {
00328               if (ReplaceList.values().contains(TempVorl[sFnumber].name()))
00329               {
00330                      QMap<QString,QString>::Iterator it;
00331                      for (it = ReplaceList.begin(); it != ReplaceList.end(); ++it)
00332                      {
00333                             if (it.data() == TempVorl[sFnumber].name())
00334                                    it.data() = dia->getReplacementStyle();
00335                      }
00336               }
00337               ReplaceList.insert(TempVorl[sFnumber].name(), dia->getReplacementStyle());
00338               ListBox1->removeItem(sFnumber);
00339               // this might be unsafe...
00340               TempVorl.remove(sFnumber);
00341               UpdateFList();
00342        }
00343        delete dia;
00344        int listBoxCount=ListBox1->count();
00345        if (listBoxCount>selectedIndex)
00346               ListBox1->setSelected(selectedIndex, true);
00347        if (listBoxCount>topIndex)
00348               ListBox1->setTopItem(topIndex);
00349 }
00350 
00351 void StilFormate::loadStyles()
00352 {
00353        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00354        QString wdir = dirs->get("editformats", ".");
00355        CustomFDialog dia(this, wdir, tr("Open"), tr("Documents (*.sla *.sla.gz *.scd *.scd.gz);;All Files (*)"));
00356        if (dia.exec() == QDialog::Accepted)
00357        {
00358               QString selectedFile = dia.selectedFile();
00359               dirs->set("editformats", selectedFile.left(selectedFile.findRev("/")));
00360               StyleSet<ParagraphStyle> TempVorl2;
00361               Docu->loadStylesFromFile(selectedFile, &TempVorl2);
00362               ChooseStyles* dia2 = new ChooseStyles(this, &TempVorl2, &TempVorl);
00363               if (dia2->exec())
00364               {
00365                      QStringList neededColors;
00366                      neededColors.clear();
00367                      QMap<QCheckListItem*, int>::Iterator it;
00368                      for (it = dia2->storedStyles.begin(); it != dia2->storedStyles.end(); ++it)
00369                      {
00370                             const ParagraphStyle& sty(TempVorl2[it.data()]);
00371                             if (it.key()->isOn())
00372                             {
00373 //                                 sty = TempVorl2[it.data()];
00374                                    qDebug(QString("load style %1").arg(sty.name()));
00375                                    TempVorl.create(sty);
00376                                    if ((!Docu->PageColors.contains(sty.charStyle().strokeColor())) && (!neededColors.contains(sty.charStyle().strokeColor())))
00377                                           neededColors.append(sty.charStyle().strokeColor());
00378                                    if ((!Docu->PageColors.contains(sty.charStyle().fillColor())) && (!neededColors.contains(sty.charStyle().fillColor())))
00379                                           neededColors.append(sty.charStyle().fillColor());
00380                             }
00381                      }
00382                      if (!neededColors.isEmpty())
00383                      {
00384                             FileLoader fl(selectedFile);
00385                             if (fl.TestFile() == -1)
00386                             //TODO put in nice user warning
00387                                    return;
00388                             ColorList LColors;
00389                             if (fl.ReadColors(selectedFile, LColors))
00390                             {
00391                                    ColorList::Iterator itc;
00392                                    for (itc = LColors.begin(); itc != LColors.end(); ++itc)
00393                                    {
00394                                           if (neededColors.contains(itc.key()))
00395                                                  Docu->PageColors.insert(itc.key(), itc.data());
00396                                    }
00397                             }
00398                      }
00399               }
00400               delete dia2;
00401 //            TempVorl.redefine(TempVorl2, false);
00402               UpdateFList();
00403        }
00404        else
00405               return;
00406 }
00407 
00408 void StilFormate::UpdateFList()
00409 {
00410        ListBox1->clear();
00411        if (TempVorl.count() == 0)
00412               return;
00413        for (uint x = 0; x < TempVorl.count(); ++x)
00414               ListBox1->insertItem(TempVorl[x].name());
00415        if (ListBox1->currentItem() == -1)
00416        {
00417               DublicateB->setEnabled(false);
00418               EditB->setEnabled(false);
00419               DeleteB->setEnabled(false);
00420        }
00421        ListBox1->sort( true );
00422        ListBox1->setSelected(ListBox1->currentItem(), false);
00423 }