Back to index

scribus-ng  1.3.4.dfsg+svn20071115
stylemanager.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 #include "stylemanager.h"
00009 #include "stylemanager.moc"
00010 #include "styleitem.h"
00011 #include "scribusdoc.h"
00012 #include "scribusview.h"
00013 #include "smreplacedia.h"
00014 #include "smtextstyles.h"
00015 #include "smlinestyle.h"
00016 #include "styleitem.h"
00017 #include "selection.h"
00018 #include "prefsmanager.h"
00019 #include "prefsfile.h"
00020 #include "prefscontext.h"
00021 #include "commonstrings.h"
00022 #include "customfdialog.h"
00023 #include "fileloader.h"
00024 #include "smstyleimport.h"
00025 #include <qheader.h>
00026 #include <qlabel.h>
00027 #include <qlistview.h>
00028 #include <qtabwidget.h>
00029 #include <qtoolbutton.h>
00030 #include <qevent.h>
00031 #include <qlineedit.h>
00032 #include <qlayout.h>
00033 #include <qbuttongroup.h>
00034 #include <qradiobutton.h>
00035 #include <qpushbutton.h>
00036 #include <qsplitter.h>
00037 #include <qsize.h>
00038 #include <qfont.h>
00039 #include <qpainter.h>
00040 #include <qtooltip.h>
00041 #include <qpair.h>
00042 #include <qmessagebox.h>
00043 
00044 const QString StyleManager::SEPARATOR = "$$$$"; // dumb but it works
00045 
00046 StyleManager::StyleManager(QWidget *parent, const char *name) : SMBase(parent, name), item_(0), widget_(0), shortcutWidget_(0), currentType_(QString::null), isEditMode_(true), doc_(0)
00047 {
00048        splitter->setMinimumWidth(0);
00049        splitter->setResizeMode(leftFrame, QSplitter::KeepSize);
00050        splitter->setResizeMode(rightFrame, QSplitter::Stretch);
00051        uniqueLabel->hide();
00052        rightFrame->hide();
00053        svLayout = new QHBoxLayout(styleViewFrame);
00054        styleView = new StyleView(styleViewFrame);
00055        svLayout->addWidget(styleView);
00056        styleView->addColumn( tr("Name"));
00057        styleView->addColumn( tr("Shortcut"));
00058        styleView->setColumnWidthMode(NAME_COL, QListView::Maximum);
00059        styleView->setColumnWidthMode(SHORTCUT_COL, QListView::Maximum);
00060        styleView->setColumnWidth(NAME_COL, 0);
00061        styleView->setColumnWidth(SHORTCUT_COL, 0);
00062        styleView->header()->hide();
00063        applyButton->setEnabled(false);
00064        resetButton->setEnabled(false);
00065        layout_ = new QGridLayout(mainFrame);
00066        newPopup_ = new QPopupMenu(newButton, "newPopup_");
00067        rightClickPopup_ = new QPopupMenu(styleView, "rightClickPopup_");
00068        newButton->setPopup(newPopup_);
00069        QString pname(name);
00070        if (pname.isEmpty())
00071               pname = "styleManager";
00072        prefs_ = PrefsManager::instance()->prefsFile->getContext(pname);
00073        isEditMode_ = true;
00074        isStoryEditMode_ = false;
00075        editPosition_.setX(prefs_->getInt("eX", x()));
00076        editPosition_.setY(prefs_->getInt("eY", y()));
00077 
00078        width_ = prefs_->getInt("Width", 212);
00079        widthEm_ = prefs_->getInt("WidthEM", 500);
00080        height_ = prefs_->getInt("Height", 450);
00081        widthLeft_ = prefs_->getInt("WidthLeft", 192);
00082        widthRight_ = prefs_->getInt("WidthRight", 474);
00083 
00084        newButton->setEnabled(false);
00085        cloneButton->setEnabled(false);
00086        importButton->setEnabled(false);
00087        deleteButton->setEnabled(false);
00088        rightClickPopup_->setEnabled(false);
00089        newPopup_->setEnabled(false);
00090 
00091        connect(newPopup_, SIGNAL(activated(int)), this, SLOT(slotNewPopup(int)));
00092        connect(okButton, SIGNAL(clicked()), this, SLOT(slotOk()));
00093        connect(importButton, SIGNAL(clicked()), this, SLOT(slotImport()));
00094        connect(resetButton, SIGNAL(clicked()), this, SLOT(slotClean()));
00095        connect(applyButton, SIGNAL(clicked()), this, SLOT(slotApply()));
00096        connect(deleteButton, SIGNAL(clicked()), this, SLOT(slotDelete()));
00097        connect(cloneButton, SIGNAL(clicked()), this, SLOT(slotClone()));
00098        connect(newButton, SIGNAL(clicked()), this, SLOT(slotNew()));
00099        connect(styleView, SIGNAL(rightButtonClicked(QListViewItem*, const QPoint&, int)),
00100                      this, SLOT(slotRightClick(QListViewItem*, const QPoint&, int)));
00101        connect(styleView, SIGNAL(doubleClicked(QListViewItem*, const QPoint&, int)),
00102                      this, SLOT(slotDoubleClick(QListViewItem*, const QPoint&, int)));
00103 
00104        languageChange();
00105        slotOk();
00106 }
00107 
00108 void StyleManager::languageChange()
00109 {
00110 /***********************************/
00111 /*      Begin Tooltips             */
00112 /***********************************/
00113 // These are for general Style Manager widgets (not for c/pstyles except the name field
00114 
00115        // for the "<< Done" button when in edit mode
00116        exitEditModeOk_ = tr("Apply all changes and exit edit mode");
00117        // for the "Edit >>" button when not in edit mode
00118        enterEditModeOk_= tr("Edit styles");
00119 
00120        QToolTip::add(nameEdit,     tr("Name of the selected style"));
00121        QToolTip::add(resetButton,  tr("Reset all changes"));
00122        QToolTip::add(applyButton,  tr("Apply all changes"));
00123        QToolTip::add(newButton,    tr("Create a new style"));
00124        QToolTip::add(importButton, tr("Import styles from another document"));
00125        QToolTip::add(cloneButton,  tr("Clone selected style"));
00126        QToolTip::add(deleteButton, tr("Delete selected styles"));
00127 
00128 /***********************************/
00129 /*      End Tooltips               */
00130 /***********************************/
00131 
00132        nameLabel->setText( tr("Name:"));
00133        resetButton->setText( tr("&Reset"));
00134        applyButton->setText( tr("&Apply"));
00135        doneText= tr("&Done");
00136        editText= tr("&Edit");
00137        setOkButtonText();
00138        newButton->setText( tr("&New"));
00139        importButton->setText( tr("&Import"));
00140        cloneButton->setText( tr("&Clone"));
00141        deleteButton->setText( tr("&Delete"));
00142 
00143        if (isEditMode_)
00144               QToolTip::add(okButton, exitEditModeOk_);
00145        else
00146               QToolTip::add(okButton, enterEditModeOk_);
00147 
00148        if (shortcutWidget_)
00149               shortcutWidget_->languageChange();
00150 
00151        newPopup_->clear();
00152        QStringList popupStrings;
00153        for (uint i = 0; i < items_.count(); ++i)
00154        {
00155               popupStrings << items_.at(i)->typeNameSingular();
00156               items_.at(i)->languageChange();
00157               styleClassesPS_[items_.at(i)->typeNamePlural()] = items_.at(i)->typeNameSingular();
00158               styleClassesSP_[items_.at(i)->typeNameSingular()] = items_.at(i)->typeNamePlural();
00159        }
00160        popupStrings.sort();
00161        for (uint i = 0; i < popupStrings.count(); ++i)
00162               newPopup_->insertItem(popupStrings[i]);
00163 
00164        styleView->clear();
00165        for (uint i = 0; i < items_.count(); ++i)
00166               addNewType(items_.at(i));
00167 
00168        rightClickPopup_->clear();
00169        rcpNewId_ = rightClickPopup_->insertItem( tr("New"), newPopup_);
00170        rightClickPopup_->insertItem( tr("Import"), this, SLOT(slotImport()));
00171        rightClickPopup_->insertSeparator();
00172        rcpEditId_ = rightClickPopup_->insertItem( tr("Edit"), this, SLOT(slotEdit()));
00173        rcpCloneId_ = rightClickPopup_->insertItem( tr("Clone"), this, SLOT(slotClone()));
00174        rcpToScrapId_ = rightClickPopup_->insertItem( tr("Send to Scrapbook"), this, SLOT(slotScrap()));
00175        rightClickPopup_->insertSeparator();
00176        rcpDeleteId_ = rightClickPopup_->insertItem( tr("Delete"), this, SLOT(slotDelete()));
00177 }
00178 
00179 void StyleManager::unitChange()
00180 {
00181        if (doc_)
00182        {
00183               for (uint i = 0; i < items_.count(); ++i)
00184                      items_.at(i)->unitChange();
00185 
00186               slotSetupWidget();
00187        }
00188 }
00189 
00190 void StyleManager::setOkButtonText()
00191 {
00192        if (!isStoryEditMode_)
00193               okButton->setText(isEditMode_ ? "<< " + doneText : editText + " >>");
00194        else
00195               okButton->setText(CommonStrings::tr_OK);
00196 }
00197 
00198 void StyleManager::setDoc(ScribusDoc *doc)
00199 {
00200        if (doc)
00201        {
00202               doc_ = doc;
00203               newButton->setEnabled(true);
00204               cloneButton->setEnabled(true);
00205               importButton->setEnabled(true);
00206               deleteButton->setEnabled(true);
00207               rightClickPopup_->setEnabled(true);
00208               newPopup_->setEnabled(true);
00209               connect(doc->m_Selection, SIGNAL(selectionChanged()), this, SLOT(slotDocSelectionChanged()));
00210        }
00211        else
00212        {
00213               doc_ = 0;
00214               newButton->setEnabled(false);
00215               cloneButton->setEnabled(false);
00216               importButton->setEnabled(false);
00217               deleteButton->setEnabled(false);
00218               rightClickPopup_->setEnabled(false);
00219               newPopup_->setEnabled(false);
00220        }
00221 
00222        // clear the style list and reload from new doc
00223        styleView->clear();
00224        styleActions_.clear();
00225        for (uint i = 0; i < items_.count(); ++i)
00226        {
00227               items_.at(i)->currentDoc(doc);
00228               addNewType(items_.at(i)); // forces a reload
00229               if (doc_)
00230                      items_.at(i)->unitChange();
00231        }
00232 }
00233 
00234 void StyleManager::updateColorList()
00235 {
00236        for (uint i = 0; i < items_.count(); ++i)
00237        {
00238               items_.at(i)->currentDoc(doc_);
00239               items_.at(i)->reload();
00240        }
00241 }
00242 
00243 void StyleManager::addStyle(StyleItem *item)
00244 {
00245        items_.append(item);
00246        addNewType(item);
00247        connect(item, SIGNAL(selectionDirty()), this, SLOT(slotDirty()));
00248 }
00249 
00250 void StyleManager::slotApply()
00251 {
00252        if (applyButton->isEnabled())
00253        {
00254               for (uint i = 0; i < items_.count(); ++i)
00255                      items_.at(i)->apply();
00256        }
00257 
00258        slotClean();
00259 }
00260 
00261 void StyleManager::slotDelete()
00262 {
00263        if (!isEditMode_)
00264               slotOk(); // switch to edit mode before deleting
00265 
00266        QStringList selected;
00267 
00268        if (!rcStyle_.isNull())
00269               selected << rcStyle_;
00270        else
00271        {
00272               QListViewItemIterator it(styleView, QListViewItemIterator::Selected);
00273               while (it.current()) {
00274                      selected << it.current()->text(0);
00275                      ++it;
00276               }
00277        }
00278        if (selected.isEmpty())
00279               return; // nothing to delete
00280 
00281        QStringList tmp;
00282        QValueList<StyleName> styles = item_->styles(false); // get list from cache
00283        for (uint i = 0; i < styles.count(); ++i)
00284               tmp << styles[i].first;
00285        SMReplaceDia *dia = new SMReplaceDia(selected, tmp, this);
00286        if (dia->exec() && item_)
00287        {
00288               item_->deleteStyles(dia->items());
00289               applyButton->setEnabled(true);
00290               resetButton->setEnabled(true);
00291               reloadStyleView(false);
00292        }
00293 
00294        delete dia;
00295 }
00296 
00297 
00298 void StyleManager::slotImport()
00299 {
00300        if (!doc_)
00301               return;
00302 
00303        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00304        QString wdir = dirs->get("editformats", ".");
00305        CustomFDialog dia(this, wdir, tr("Open"), tr("documents (*.sla *.sla.gz *.scd *.scd.gz);;All Files (*)"));
00306        if (dia.exec() == QDialog::Accepted)
00307        {
00308               QString selectedFile = dia.selectedFile();
00309               dirs->set("editformats", selectedFile.left(selectedFile.findRev("/")));
00310 
00311               StyleSet<ParagraphStyle> tmpParaStyles;
00312               StyleSet<CharStyle> tmpCharStyles;
00313               QMap<QString, multiLine> tmpLineStyles;
00314 
00315               doc_->loadStylesFromFile(selectedFile, &tmpParaStyles, &tmpCharStyles, &tmpLineStyles);
00316 
00317 // FIXME Once all styles are derived from Style remove this and make a proper
00318 //       implementation
00319 // Start hack
00320 
00321               SMParagraphStyle *pstyle = 0;
00322               SMCharacterStyle *cstyle = 0;
00323               SMLineStyle      *lstyle = 0;
00324               for (uint i = 0; i < items_.count(); ++i)
00325               {
00326                      pstyle = dynamic_cast<SMParagraphStyle*>(items_.at(i));
00327                      if (pstyle)
00328                             break;
00329               }
00330               for (uint i = 0; i < items_.count(); ++i)
00331               {
00332                      cstyle = dynamic_cast<SMCharacterStyle*>(items_.at(i));
00333                      if (cstyle)
00334                             break;
00335               }
00336               for (uint i = 0; i < items_.count(); ++i)
00337               {
00338                      lstyle = dynamic_cast<SMLineStyle*>(items_.at(i));
00339                      if (lstyle)
00340                             break;
00341               }
00342 
00343               Q_ASSERT(pstyle && cstyle && lstyle);
00344 
00345               ImportDialog *dia2 = new ImportDialog(this, &tmpParaStyles, &tmpCharStyles, &tmpLineStyles);
00346 // end hack
00347 
00348               QValueList<QPair<QString, QString> > selected;
00349               if (dia2->exec())
00350               {
00351                      if (!isEditMode_)
00352                             slotOk();
00353                      QStringList neededColors;
00354                      neededColors.clear();
00355                      QMap<QCheckListItem*, QString>::Iterator it;
00356                      for (it = dia2->storedStyles.begin(); it != dia2->storedStyles.end(); ++it)
00357                      {
00358                             ParagraphStyle& sty(tmpParaStyles[tmpParaStyles.find(it.data())]);
00359                             if (it.key()->isOn())
00360                             {
00361                                    if (dia2->clashRename())
00362                                    {
00363                                           sty.setName(pstyle->getUniqueName(sty.name()));
00364                                           pstyle->tmpStyles()->create(sty);
00365                                    }
00366                                    else
00367                                    {
00368                                           if (pstyle->tmpStyles()->find(sty.name()) >= 0)
00369                                                  (*(pstyle->tmpStyles()))[pstyle->tmpStyles()->find(it.data())] = sty;
00370                                           else
00371                                                  pstyle->tmpStyles()->create(sty);
00372                                    }
00373                                    selected << QPair<QString, QString>(pstyle->typeName(), sty.name());
00374                                    if ((!doc_->PageColors.contains(sty.charStyle().strokeColor())) && (!neededColors.contains(sty.charStyle().strokeColor())))
00375                                           neededColors.append(sty.charStyle().strokeColor());
00376                                    if ((!doc_->PageColors.contains(sty.charStyle().fillColor())) && (!neededColors.contains(sty.charStyle().fillColor())))
00377                                           neededColors.append(sty.charStyle().fillColor());
00378                             }
00379                      }
00380 
00381                      for (it = dia2->storedCharStyles.begin(); it != dia2->storedCharStyles.end(); ++it)
00382                      {
00383                             CharStyle& sty(tmpCharStyles[tmpCharStyles.find(it.data())]);
00384                             if (it.key()->isOn())
00385                             {
00386                                    if (dia2->clashRename())
00387                                    {
00388                                           sty.setName(cstyle->getUniqueName(sty.name()));
00389                                           cstyle->tmpStyles()->create(sty);
00390                                    }
00391                                    else
00392                                    {
00393                                           if (cstyle->tmpStyles()->find(sty.name()) >= 0)
00394                                                  (*(cstyle->tmpStyles()))[cstyle->tmpStyles()->find(it.data())] = sty;
00395                                           else
00396                                                  cstyle->tmpStyles()->create(sty);
00397                                    }
00398                                    selected << QPair<QString, QString>(cstyle->typeName(), sty.name());
00399                                    if ((!doc_->PageColors.contains(sty.strokeColor())) && (!neededColors.contains(sty.strokeColor())))
00400                                           neededColors.append(sty.strokeColor());
00401                                    if ((!doc_->PageColors.contains(sty.fillColor())) && (!neededColors.contains(sty.fillColor())))
00402                                           neededColors.append(sty.fillColor());
00403                             }
00404                      }
00405 
00406                      for (it = dia2->storedLineStyles.begin(); it != dia2->storedLineStyles.end(); ++it)
00407                      {
00408                             multiLine &sty = tmpLineStyles[it.data()];
00409                             QString styName = it.data();
00410 
00411                             if (it.key()->isOn())
00412                             {
00413                                    if (dia2->clashRename())
00414                                           styName = lstyle->getUniqueName(styName);
00415 
00416                                    lstyle->tmpLines[styName] = sty;
00417                                    selected << QPair<QString, QString>(lstyle->typeName(), styName);
00418                                    
00419                                    for (uint i = 0; i < sty.count(); ++i)
00420                                    {
00421                                           if ((!doc_->PageColors.contains(sty[i].Color)) && (!neededColors.contains(sty[i].Color)))
00422                                                  neededColors.append(sty[i].Color);
00423                                    }
00424                             }
00425                      }
00426 
00427                      if (!neededColors.isEmpty())
00428                      {
00429                             FileLoader fl(selectedFile);
00430                             if (fl.TestFile() == -1)
00431                             { //TODO put in nice user warning
00432                                    delete dia2;
00433                                    return;
00434                             }
00435                             ColorList LColors;
00436                             if (fl.ReadColors(selectedFile, LColors))
00437                             {
00438                                    ColorList::Iterator itc;
00439                                    for (itc = LColors.begin(); itc != LColors.end(); ++itc)
00440                                    {
00441                                           if (neededColors.contains(itc.key()))
00442                                                  doc_->PageColors.insert(itc.key(), itc.data());
00443                                    }
00444                             }
00445                      }
00446               }
00447               delete dia2;
00448 // Start hack part 2
00449               pstyle->currentDoc(doc_);
00450               cstyle->currentDoc(doc_);
00451 // end hack part 2
00452               reloadStyleView(false);
00453               setSelection(selected);
00454               slotDirty();
00455               slotSetupWidget();
00456        }
00457        else
00458               return;
00459 }
00460 
00461 void StyleManager::setSelection(const QValueList<QPair<QString, QString> > &selected)
00462 {
00463        styleView->clearSelection();
00464        
00465        QListViewItemIterator it(styleView, QListViewItemIterator::Selectable);
00466        StyleViewItem *item;
00467 
00468        while (it.current())
00469        {
00470               item = dynamic_cast<StyleViewItem*>(it.current());
00471               if (item)
00472               {
00473                      for (uint i = 0; i < selected.count(); ++i)
00474                      {
00475                             if (item->rootName() == selected[i].first && item->text(NAME_COL) == selected[i].second)
00476                             {
00477                                    styleView->setCurrentItem(item);
00478                                    item->setSelected(true);
00479                                    item->repaint();
00480                             }
00481                      }
00482               }
00483               ++it;
00484        }
00485 
00486        styleView->triggerUpdate();
00487 }
00488 
00489 void StyleManager::slotEdit()
00490 {
00491        if (!isEditMode_)
00492               slotOk(); // switch to edit mode for cloning
00493 
00494        if (!rcStyle_.isNull())
00495        {
00496               QListViewItemIterator it(styleView);
00497               while (it.current())
00498               {
00499                      StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00500                      if (item && !item->isRoot())
00501                      {
00502                             if (item->rootName() == styleClassesSP_[rcType_] &&
00503                                 item->text(0) == rcStyle_)
00504                             {
00505                                    styleView->setCurrentItem(item);
00506                                    item->setSelected(true);
00507                                    item->repaint();
00508                                    break;
00509                             }
00510                      }
00511                      ++it;
00512               }
00513               rcStyle_ = QString::null;
00514               rcType_ = QString::null;
00515        }
00516 }
00517 
00518 
00519 void StyleManager::slotClone()
00520 {
00521        if (!isEditMode_)
00522               slotOk(); // switch to edit mode for cloning
00523 
00524        if (!rcStyle_.isNull())
00525        {
00526               QListViewItemIterator it(styleView);
00527               while (it.current())
00528               {
00529                      StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00530                      if (item && !item->isRoot())
00531                      {
00532                             if (item->rootName() == styleClassesSP_[rcType_] &&
00533                                 item->text(0) == rcStyle_)
00534                             {
00535                                    styleView->setCurrentItem(item);
00536                                    item->setSelected(true);
00537                                    item->repaint();
00538                                    break;
00539                             }
00540                      }
00541                      ++it;
00542               }
00543               rcStyle_ = QString::null;
00544               rcType_ = QString::null;
00545        }
00546 
00547        QListViewItemIterator it(styleView, QListViewItemIterator::Selected);
00548        QValueList<QPair<QString, QString> > names;
00549 
00550        while (it.current())
00551        { // can't create styles here cause createNewStyle() alters the selection
00552               StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00553               if (item)
00554                      names << QPair<QString, QString>(item->rootName(), item->text(NAME_COL));
00555               ++it;
00556        }
00557 
00558        for (uint i = 0; i < names.count(); ++i)
00559               createNewStyle(names[i].first, names[i].second);
00560 }
00561 
00562 void StyleManager::slotScrap()
00563 {
00564        
00565 }
00566 
00567 
00568 void StyleManager::slotNew()
00569 {
00570 //#5334: Dont open into edit mdoe until user has selected a style type
00571 //     if (!isEditMode_)
00572 //            slotOk(); // switch to edit mode for a new style
00573 
00574        // TODO maybe there's something more clever for this
00575        newPopup_->exec(newButton->mapToGlobal(QPoint(0, newButton->height() + 2)));
00576 }
00577 
00578 void StyleManager::slotNewPopup(int i)
00579 {
00580        if (!isEditMode_)
00581               slotOk(); // switch to edit mode for a new style
00582 
00583        QString typeName = rcType_;
00584        if (typeName.isNull())
00585               typeName = newPopup_->text(i);
00586        else if (typeName.isNull() && i < 0)
00587               return; // nothing to create
00588 
00589        rcType_ = QString::null;
00590        rcStyle_ = QString::null;
00591 
00592        createNewStyle(typeName);
00593 }
00594 
00595 void StyleManager::slotRightClick(QListViewItem *item, const QPoint &point, int col)
00596 {
00597        rcStyle_ = QString::null;
00598        rcType_ = QString::null;
00599 
00600        if (isEditMode_ && item) // make item the only selection if in edit mode
00601        {                        // default behaviour for right clicking is not to select the item
00602               styleView->clearSelection();
00603               styleView->setCurrentItem(item);
00604               item->setSelected(true);
00605               item->repaint();
00606        }
00607 
00608        StyleViewItem *sitem = dynamic_cast<StyleViewItem*>(item);
00609        if (sitem && !sitem->isRoot())
00610        {
00611               rightClickPopup_->removeItem(rcpNewId_);
00612               rcpNewId_ = rightClickPopup_->insertItem( tr("New %1").arg(styleClassesPS_[sitem->rootName()]),
00613                                                        this, SLOT(slotNewPopup(int)), 0, -1, 0);
00614               rightClickPopup_->setItemEnabled(rcpDeleteId_, true);
00615               rightClickPopup_->setItemEnabled(rcpEditId_, true);
00616               rightClickPopup_->setItemEnabled(rcpCloneId_, true);
00617               rightClickPopup_->setItemEnabled(rcpToScrapId_, true);
00618               rcStyle_ = sitem->text(0);
00619               rcType_ = styleClassesPS_[sitem->rootName()];
00620               loadType(styleClassesPS_[sitem->rootName()]);
00621        }
00622        else if (sitem && sitem->isRoot())
00623        {
00624               rightClickPopup_->removeItem(rcpNewId_);
00625               rcpNewId_ = rightClickPopup_->insertItem( tr("New %1").arg(styleClassesPS_[sitem->text(0)]),
00626                                                        this, SLOT(slotNewPopup(int)), 0, -1, 0);
00627               rightClickPopup_->setItemEnabled(rcpDeleteId_, false);
00628               rightClickPopup_->setItemEnabled(rcpEditId_, false);
00629               rightClickPopup_->setItemEnabled(rcpCloneId_, false);
00630               rightClickPopup_->setItemEnabled(rcpToScrapId_, false);
00631               rcType_ = styleClassesPS_[sitem->text(0)];
00632               loadType(rcType_);
00633        }
00634        else
00635        {
00636               rightClickPopup_->removeItem(rcpNewId_);
00637               rcpNewId_ = rightClickPopup_->insertItem( tr("New"), newPopup_, -1, 0);
00638               rightClickPopup_->setItemEnabled(rcpDeleteId_, false);
00639               rightClickPopup_->setItemEnabled(rcpEditId_, false);
00640               rightClickPopup_->setItemEnabled(rcpCloneId_, false);
00641               rightClickPopup_->setItemEnabled(rcpToScrapId_, false);
00642        }
00643 
00644        rightClickPopup_->exec(point);
00645 }
00646 
00647 void StyleManager::slotDoubleClick(QListViewItem *item, const QPoint &point, int col)
00648 {
00649        rcStyle_ = QString::null;
00650        rcType_ = QString::null;
00651 
00652        if (isEditMode_ && item) // make item the only selection if in edit mode
00653        {
00654               styleView->clearSelection();
00655               styleView->setCurrentItem(item);
00656               item->setSelected(true);
00657               item->repaint();
00658               return; // work done, already in edit mode
00659        }
00660 
00661        StyleViewItem *sitem = dynamic_cast<StyleViewItem*>(item);
00662        if (sitem && !sitem->isRoot())
00663        {
00664               rcType_ = sitem->rootName();
00665               rcStyle_ = sitem->text(0);
00666               slotOk(); // switch to edit mode
00667        }
00668        else if (sitem && sitem->isRoot())
00669        {
00670               rcType_ = sitem->text(0);
00671               slotNewPopup(-1);
00672        }
00673        rcStyle_ = QString::null;
00674        rcType_ = QString::null;
00675 }
00676 
00677 void StyleManager::createNewStyle(const QString &typeName, const QString &fromParent)
00678 {
00679        if (!doc_)
00680               return;
00681 
00682        loadType(typeName); // get the right style class
00683        Q_ASSERT(item_);
00684 
00685        QString newName = fromParent.isNull() ?
00686                      item_->newStyle() : item_->newStyle(fromParent);
00687 
00688        if (newName.isNull())
00689               return;
00690 
00691        QListViewItem *root = 0;
00692        QListViewItemIterator it(styleView, QListViewItemIterator::NotSelectable);
00693        while (it.current())
00694        {
00695               StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00696               if (item)
00697               {
00698                      if (item->text(NAME_COL) == item_->typeName())
00699                      {
00700                             root = item;
00701                             break;
00702                      }
00703               }
00704               ++it;
00705        }
00706        Q_ASSERT(root);
00707        styleView->clearSelection();
00708        StyleViewItem *newItem = new StyleViewItem(root, newName, item_->typeName());
00709        Q_CHECK_PTR(newItem);
00710        newItem->setDirty(true);
00711        styleView->setCurrentItem(newItem);
00712        newItem->setSelected(true);
00713        newItem->repaint();
00714        slotSetupWidget();
00715        nameEdit->setFocus();
00716        nameEdit->selectAll();
00717        applyButton->setEnabled(true);
00718        resetButton->setEnabled(true);
00719 }
00720 
00721 void StyleManager::show()
00722 {
00723        QDialog::show();
00724 }
00725 
00726 
00727 // open or close edit mode
00728 void StyleManager::slotOk()
00729 {
00730        static bool isFirst = true;
00731        if (isEditMode_)
00732        {
00733               disconnect(styleView, SIGNAL(selectionChanged()), this, SLOT(slotSetupWidget()));
00734               widthEm_ = width();
00735               prefs_->set("WidthEM", widthEm_);
00736               height_ = height();
00737               prefs_->set("Height", height_);
00738               QValueList<int> l = splitter->sizes();
00739               widthLeft_ = l[0];
00740               widthRight_ = l[1];
00741               prefs_->set("WidthLeft", widthLeft_);
00742               prefs_->set("WidthRight", widthRight_);
00743               slotApply();
00744               styleView->setSelectionMode(QListView::Multi);
00745               okButton->setText(QString("%1 >>").arg( tr("&Edit")));
00746               editFrame->hide();
00747               applyButton->hide();
00748               resetButton->hide();
00749 //            editButtonsFrame->hide();
00750               rightFrame->hide();
00751               isEditMode_ = false;
00752               for (uint i = 0; i < items_.count(); ++i)
00753               {
00754                      items_.at(i)->apply();
00755                      items_.at(i)->editMode(false);
00756               }
00757               QToolTip::add(okButton, enterEditModeOk_);
00758               slotClean();
00759               slotDocSelectionChanged();
00760               adjustSize();
00761               resize(width_, height_);
00762               if (!isFirst)
00763                      move(editPosition_);
00764               prefs_->set("isEditMode", isEditMode_);
00765               connect(styleView, SIGNAL(selectionChanged(QListViewItem*)),
00766                       this, SLOT(slotApplyStyle(QListViewItem*)));
00767               connect(styleView, SIGNAL(clicked(QListViewItem*)),
00768                             this, SLOT(slotApplyStyle(QListViewItem*)));
00769               if (isStoryEditMode_)
00770               {
00771                      isStoryEditMode_=false;
00772                      hide();
00773               }
00774        }
00775        else
00776        {
00777               disconnect(styleView, SIGNAL(selectionChanged(QListViewItem*)),
00778                          this, SLOT(slotApplyStyle(QListViewItem*)));
00779               disconnect(styleView, SIGNAL(clicked(QListViewItem*)),
00780                             this, SLOT(slotApplyStyle(QListViewItem*)));
00781               if (!isFirst)
00782               {
00783                      width_ = width();
00784                      prefs_->set("Width", width_);
00785               }
00786               height_ = height();
00787               prefs_->set("Height", height_);
00788               slotSetupWidget();
00789               styleView->setSelectionMode(QListView::Extended);
00790               editPosition_.setX(x());
00791               editPosition_.setY(y());
00792               prefs_->set("eX", x());
00793               prefs_->set("eY", y());
00794 //            okButton->setText(QString("<< %1").arg( tr("&Done")));
00795               editFrame->show();
00796               applyButton->show();
00797               resetButton->show();
00798 //            editButtonsFrame->show();
00799               rightFrame->show();
00800               isEditMode_ = true;
00801               for (uint i = 0; i < items_.count(); ++i)
00802                      items_.at(i)->editMode(true);
00803               QToolTip::add(okButton, exitEditModeOk_);
00804               slotClean();
00805               adjustSize();
00806               QValueList<int> l;
00807               l << widthLeft_ << widthRight_;
00808               splitter->setSizes(l);
00809               resize(widthEm_, height_);
00810               prefs_->set("isEditMode", isEditMode_);
00811               connect(styleView, SIGNAL(selectionChanged()), this, SLOT(slotSetupWidget()));
00812        }
00813        setOkButtonText();
00814        isFirst = false;
00815 }
00816 
00817 void StyleManager::addNewType(StyleItem *item, bool loadFromDoc)
00818 {
00819        if (item) {
00820               item_ = item;
00821 
00822               QValueList<StyleName> styles = item_->styles(loadFromDoc);
00823               StyleViewItem *rootItem = new StyleViewItem(styleView, item_->typeName());
00824               rootItem->setOpen(true);
00825               QMap<QString, StyleViewItem*> sitems;
00826 
00827               for (uint i = 0; i < styles.count(); ++i) // set the list of styles of this type
00828               {
00829                      StyleViewItem *sitem;
00830                      if (styles[i].second.isNull())
00831                      {
00832                             sitem = new StyleViewItem(rootItem, styles[i].first, item_->typeName());
00833                      }
00834                      else if (sitems.contains(styles[i].second))
00835                      {
00836                             StyleViewItem *parent = sitems[styles[i].second];
00837                             sitem = new StyleViewItem(parent, styles[i].first, item_->typeName());
00838                             parent->setOpen(true);
00839                      }
00840                      else 
00841                      {
00842                             bool postpone = false;
00843                             // search if parent is in remaing styles
00844                             for (uint j = i+1; j < styles.count(); ++j)
00845                             {
00846                                    if (styles[j].first == styles[i].second)
00847                                    {
00848                                           styles.append(styles[i]); // postpone
00849                                           postpone = true;
00850                                    }
00851                             }
00852                             if (postpone)
00853                                    continue;
00854                             else 
00855                             {
00856                                    qDebug(QString("stylemanager: unknown parent '%1' of %2 style '%3'").arg(styles[i].second).arg(item_->typeName()).arg(styles[i].first));
00857                                    sitem = new StyleViewItem(rootItem, styles[i].first, item_->typeName());
00858                             }
00859                      }
00860                      
00861                      sitems[styles[i].first] = sitem;
00862                      sitem->setText(SHORTCUT_COL, item_->shortcut(sitem->text(NAME_COL)));
00863                      
00864                      QString key = sitem->rootName() + SEPARATOR + sitem->text(NAME_COL);
00865                      if (styleActions_.contains(key))
00866                             continue;
00867 
00868                      styleActions_[key] =
00869                             new ScrAction(ScrAction::DataQString, QIconSet(), "",
00870                                                  sitem->text(SHORTCUT_COL), doc_->view(), key, 0, 0.0, key);
00871                      connect(styleActions_[key], SIGNAL(activatedData(QString)),
00872                                    this, SLOT(slotApplyStyle(QString)));
00873               }
00874        }
00875 }
00876 
00877 void StyleManager::slotDirty()
00878 {
00879        QListViewItemIterator it(styleView, QListViewItemIterator::Selected);
00880 
00881        while (it.current()) {
00882               StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00883               if (item)
00884               {
00885                      item->setDirty(true);
00886                      item->repaint();
00887                      applyButton->setEnabled(true);
00888                      resetButton->setEnabled(true);
00889               }
00890               ++it;
00891        }
00892        applyButton->setEnabled(true);
00893        resetButton->setEnabled(true);
00894 }
00895 
00896 void StyleManager::slotClean()
00897 {
00898        QListViewItemIterator it(styleView);
00899 
00900        while (it.current()) {
00901               StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00902               if (item)
00903               {
00904                      item->setDirty(false);
00905                      item->repaint();
00906               }
00907               ++it;
00908        }
00909 
00910        if (isEditMode_)
00911        {
00912               StyleItem *tmp = item_;
00913 
00914               for (uint i = 0; i < items_.count(); ++i)
00915               {
00916                      item_ = items_.at(i);
00917                      reloadStyleView();
00918               }
00919               slotSetupWidget();
00920               item_ = tmp;
00921        }
00922        applyButton->setEnabled(false);
00923        resetButton->setEnabled(false);
00924        uniqueLabel->hide();
00925 }
00926 
00927 void StyleManager::reloadStyleView(bool loadFromDoc)
00928 {
00929        QListViewItemIterator it(styleView, QListViewItemIterator::Selected);
00930        QValueList<QPair<QString, QString> > selected;
00931 
00932        while (it.current())
00933        {
00934               StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
00935               if (item)
00936                      selected << QPair<QString, QString>(item->rootName(), item->text(NAME_COL));
00937               ++it;
00938        }
00939 
00940        styleView->clear();
00941        if (item_ && loadFromDoc)
00942               item_->reload();
00943 
00944        for (uint i = 0; i < items_.count(); ++i)
00945               addNewType(items_.at(i), false);
00946 
00947        QListViewItemIterator it2(styleView, QListViewItemIterator::Selectable);
00948 
00949        while (it2.current())
00950        {
00951               StyleViewItem *item = dynamic_cast<StyleViewItem*>(it2.current());
00952               if (item)
00953               {
00954                      for (uint i = 0; i < selected.count(); ++i)
00955                      {
00956                             if (selected[i].first == item->rootName() &&
00957                                 selected[i].second == item->text(NAME_COL))
00958                             {
00959                                    item->setDirty(false);
00960                                    styleView->setCurrentItem(item);
00961                                    item->setSelected(true);
00962                                    break;
00963                             }
00964                      }
00965               }
00966               ++it2;
00967        }
00968 
00969        styleView->repaint();
00970 }
00971 
00972 void StyleManager::insertShortcutPage(QTabWidget *twidget)
00973 {
00974        if (twidget)
00975        {
00976               if (!shortcutWidget_)
00977               {
00978                      shortcutWidget_ = new ShortcutWidget(0, "shortcutWidget_");
00979                      connect(shortcutWidget_, SIGNAL(newKey(const QString&)),
00980                                    this, SLOT(slotShortcutChanged(const QString&)));
00981               }
00982               twidget->addTab(shortcutWidget_, tr("Shortcut"));
00983        }
00984 }
00985 
00986 void StyleManager::slotNameChanged(const QString& name)
00987 {
00988        if (!nameIsUnique(name))
00989        {
00990               uniqueLabel->show();
00991               okButton->setEnabled(false);
00992               applyButton->setEnabled(false);
00993               return;
00994        }
00995        else if (uniqueLabel->isVisible())
00996        {
00997               uniqueLabel->hide();
00998               okButton->setEnabled(true);
00999               applyButton->setEnabled(true);
01000        }
01001 
01002 
01003        if (item_)
01004        {
01005               item_->nameChanged(name);
01006               updateActionName(styleView->currentItem()->text(NAME_COL), name);
01007               styleView->currentItem()->setText(NAME_COL, name);
01008               applyButton->setEnabled(true);
01009               resetButton->setEnabled(true);
01010        }
01011 }
01012 
01013 
01014 void StyleManager::updateActionName(const QString &oldName, const QString &newName)
01015 {
01016        if (!item_)
01017               return;
01018        QString oldKey = item_->typeName() + SEPARATOR + oldName;
01019        QString newKey = item_->typeName() + SEPARATOR + newName;
01020 
01021        if (styleActions_.contains(oldKey))
01022        {
01023               ScrAction *a = styleActions_[oldKey];
01024               disconnect(a, SIGNAL(activatedData(QString)), this, SLOT(slotApplyStyle(QString)));
01025               ScrAction *b = new ScrAction(ScrAction::DataQString, QIconSet(), "",
01026                                     a->accel(), doc_->view(), newKey, 0, 0.0, newKey);
01027               styleActions_.remove(oldKey);
01028               delete a;
01029               styleActions_[newKey] = b;
01030               connect(b, SIGNAL(activatedData(QString)), this, SLOT(slotApplyStyle(QString)));
01031        }
01032 }
01033 
01034 void StyleManager::slotShortcutChanged(const QString& shortcut)
01035 {
01036        if (!doc_)
01037               return;
01038 
01039        StyleViewItem *sitem = dynamic_cast<StyleViewItem*>(styleView->currentItem());
01040        if (!sitem)
01041               return;
01042 
01043        if (!shortcut.isNull() && shortcutExists(shortcut))
01044        {
01045               QMessageBox::information(this, CommonStrings::trWarning,
01046                                        tr("This key sequence is already in use"),
01047                                        CommonStrings::tr_OK);
01048               shortcutWidget_->setShortcut(item_->shortcut(sitem->text(NAME_COL)));
01049               return;
01050        }
01051 
01052        sitem->setText(SHORTCUT_COL, shortcut.isNull() ? "" : shortcut);
01053        QString key = sitem->rootName() + SEPARATOR + sitem->text(NAME_COL);
01054        if (styleActions_.contains(key))
01055               styleActions_[key]->setAccel(shortcut);
01056        else
01057        {
01058               styleActions_[key] =
01059                      new ScrAction(ScrAction::DataQString, QIconSet(), "",
01060                                    shortcut, doc_->view(), key, 0, 0.0, key);
01061               connect(styleActions_[key], SIGNAL(activatedData(QString)),
01062                       this, SLOT(slotApplyStyle(QString)));
01063        }
01064 
01065        if (item_)
01066               item_->setShortcut(shortcut);
01067 }
01068 
01069 bool StyleManager::shortcutExists(const QString &keys)
01070 {
01071        QKeySequence key(keys);
01072 
01073        QMap<QString, QGuardedPtr<ScrAction> >::iterator it;
01074        for (it = styleActions_.begin(); it != styleActions_.end(); ++it)
01075        {
01076               if ((*it)->accel() == key)
01077                      return true;
01078        }
01079 
01080        ApplicationPrefs *prefsData=&(PrefsManager::instance()->appPrefs);
01081        for (QMap<QString,Keys>::Iterator it=prefsData->KeyActions.begin();
01082             it!=prefsData->KeyActions.end(); ++it)
01083        {
01084               if (key.matches(it.data().keySequence) != Qt::NoMatch)
01085                      return true;
01086        }
01087 
01088        return false;
01089 }
01090 
01091 void StyleManager::slotApplyStyle(QString keyString)
01092 {
01093        if (isEditMode_)
01094               return;
01095 
01096        QStringList slist = QStringList::split(SEPARATOR, keyString);
01097        Q_ASSERT(slist.count() == 2);
01098 
01099        loadType(slist[0]);
01100        item_->toSelection(slist[1]);
01101        slotDocSelectionChanged();
01102 }
01103 
01104 bool StyleManager::nameIsUnique(const QString &name)
01105 {
01106        QValueList<StyleName> names = item_->styles(false);
01107        for (uint i = 0; i < names.count(); ++i)
01108        {
01109               if (names[i].first == name)
01110                      return false;
01111        }
01112        return true;
01113 }
01114 
01115 // setups the selected type and edit widgets related to it
01116 void StyleManager::slotSetupWidget()
01117 {
01118        QPair<QString, QStringList> selection = namesFromSelection();
01119        QString typeName = selection.first;
01120 
01121        if (typeName.isNull() && widget_)
01122               widget_->setEnabled(false); // nothing selected or two or more different types
01123        else if (!item_ || item_->typeName() != typeName || widget_ != item_->widget())
01124               loadType(typeName); // new type selected
01125        else if (widget_ && !widget_->isEnabled())
01126               widget_->setEnabled(true);
01127 
01128        disconnect(nameEdit, SIGNAL(textChanged(const QString&)),
01129                   this, SLOT(slotNameChanged(const QString&)));
01130        if (!typeName.isNull())
01131        {
01132               item_->selected(selection.second);
01133               if (selection.second.count() > 1)
01134               {
01135                      nameEdit->setText( tr("More than one style selected"));
01136                      nameEdit->setEnabled(false);
01137                      shortcutWidget_->setEnabled(false);
01138                      shortcutWidget_->setShortcut(QString::null);
01139               }
01140               else
01141               {
01142                      nameEdit->setText(selection.second[0]);
01143                      nameEdit->setEnabled(true);
01144                      shortcutWidget_->setEnabled(true);
01145                      shortcutWidget_->setShortcut(item_->shortcut(selection.second[0]));
01146               }
01147        }
01148        else
01149        {
01150               nameEdit->setText("");
01151               nameEdit->setEnabled(false);
01152        }
01153 
01154        connect(nameEdit, SIGNAL(textChanged(const QString&)),
01155                this, SLOT(slotNameChanged(const QString&)));
01156 
01157 }
01158 
01159 void StyleManager::slotApplyStyle(QListViewItem *item)
01160 {
01161        StyleViewItem *sitem = dynamic_cast<StyleViewItem*>(item);
01162 
01163        if (isEditMode_ || !sitem || sitem->isRoot())
01164               return; // don't apply a style in edit mode or if there was no item/type selected
01165 
01166        styleView->clearSelection();
01167 
01168        if (!item_ || item_->typeName() != sitem->rootName())
01169               loadType(sitem->rootName()); // load the type where we want to apply this style
01170 
01171        Q_ASSERT(item_);
01172 
01173        item_->toSelection(sitem->text(NAME_COL)); // apply selected style to the selection
01174 
01175        slotDocSelectionChanged();
01176 }
01177 
01178 void StyleManager::slotDocSelectionChanged()
01179 {
01180        if (isEditMode_)
01181               return; // don't track changes when in edit mode
01182 
01183        disconnect(styleView, SIGNAL(currentChanged(QListViewItem*)),
01184                   this, SLOT(slotApplyStyle(QListViewItem*)));
01185 
01186        styleView->clearSelection();
01187 
01188        QValueList<QPair<QString, QString> > selected;
01189 
01190        for (uint i = 0; i < items_.count(); ++i)
01191               selected << QPair<QString, QString>(items_.at(i)->typeName(), items_.at(i)->fromSelection());
01192        
01193        QListViewItemIterator it(styleView, QListViewItemIterator::Selectable);
01194        StyleViewItem *item;
01195 
01196        while (it.current())
01197        {
01198               item = dynamic_cast<StyleViewItem*>(it.current());
01199               if (item)
01200               {
01201                      for (uint i = 0; i < selected.count(); ++i)
01202                      {
01203                             if (item->rootName() == selected[i].first && item->text(NAME_COL) == selected[i].second)
01204                             {
01205                                    styleView->setCurrentItem(item);
01206                                    item->setSelected(true);
01207                                    item->repaint();
01208                             }
01209                      }
01210               }
01211               ++it;
01212        }
01213 
01214        styleView->triggerUpdate();
01215 
01216        connect(styleView, SIGNAL(currentChanged(QListViewItem*)),
01217                this, SLOT(slotApplyStyle(QListViewItem*)));
01218 }
01219 
01220 void StyleManager::slotDocStylesChanged()
01221 {
01222        qDebug("slotDocStylesChanged()");
01223 }
01224 
01225 // QPair.first == QString::null if nothing is selected or if
01226 // there are items from more than one type in the selection
01227 // if selection is valid (only from single type) QPair.first will
01228 // include the type name and QPair.second will have all the selected
01229 // stylenames in it
01230 QPair<QString, QStringList> StyleManager::namesFromSelection()
01231 {
01232        QString typeName(QString::null);
01233        QStringList styleNames;
01234 
01235        if (rcStyle_.isNull())
01236        {
01237               QListViewItemIterator it(styleView, QListViewItemIterator::Selected);
01238               while (it.current())
01239               {
01240                      StyleViewItem *item = dynamic_cast<StyleViewItem*>(it.current());
01241                      if (!item)
01242                      {
01243                             ++it;
01244                             continue;
01245                      }
01246                      else if (typeName.isNull())
01247                             typeName = item->rootName();
01248                      else if (!typeName.isNull() && typeName != item->rootName())
01249                      {
01250                             typeName = QString::null;
01251                             break; // two different types selected returning null
01252                      }
01253        
01254                      if (!item->isRoot())
01255                             styleNames << item->text(NAME_COL);
01256        
01257                      ++it;
01258               }
01259        }
01260        else // right click selection which doesn't show in the gui
01261        {
01262               typeName = rcType_;
01263               styleNames << rcStyle_;
01264        }
01265        return QPair<QString, QStringList>(typeName, styleNames);
01266 }
01267 
01268 // sets the current type to name including item_ and the main widget
01269 // for editing styles
01270 void StyleManager::loadType(const QString &name)
01271 {
01272        item_ = 0;
01273        for (uint i = 0; i < items_.count(); ++i)
01274        {
01275               if (items_.at(i)->typeNameSingular() == name || items_.at(i)->typeName() == name)
01276               {
01277                      item_ = items_.at(i);
01278                      break;
01279               }
01280        }
01281        if (!item_)
01282               return;
01283 
01284        if (widget_)
01285        {   // remove the old style type's widget
01286               widget_->hide();
01287               layout_->remove(widget_);
01288               widget_->reparent(0,0, QPoint(0,0), false);
01289        }
01290        // show the widget for the new style type
01291        if (shortcutWidget_)
01292               widget_->removePage(shortcutWidget_);
01293        widget_ = item_->widget(); // show the widget for the style type
01294        insertShortcutPage(widget_);
01295        widget_->reparent(mainFrame, 0, QPoint(0,0), true);
01296        layout_->addWidget(widget_, 0, 0);
01297 }
01298 
01299 void StyleManager::hideEvent(QHideEvent *e)
01300 {
01301        if (isEditMode_)
01302        {
01303               QValueList<int> l = splitter->sizes();
01304               widthLeft_ = l[0];
01305               widthRight_ = l[1];
01306               widthEm_ = width();
01307        }
01308        else
01309               width_ = width();
01310 
01311        height_ = height();
01312        prefs_->set("eX", x());
01313        prefs_->set("eY", y());
01314        prefs_->set("isEditMode", isEditMode_);
01315        prefs_->set("Width", width_);
01316        prefs_->set("WidthEM", widthEm_);
01317        prefs_->set("WidthLeft", widthLeft_);
01318        prefs_->set("WidthRight", widthRight_);
01319        prefs_->set("Height", height_);
01320        prefs_->set("InitX", x());
01321        prefs_->set("InitY", y());
01322        storeVisibility(false);
01323        storePosition();
01324        SMBase::hideEvent(e);
01325        emit closed();
01326 }
01327 
01328 void StyleManager::closeEvent(QCloseEvent *e)
01329 {
01330        if (isEditMode_)
01331        {
01332               QValueList<int> l = splitter->sizes();
01333               widthLeft_ = l[0];
01334               widthRight_ = l[1];
01335               widthEm_ = width();
01336        }
01337        else
01338               width_ = width();
01339 
01340        height_ = height();
01341        prefs_->set("eX", x());
01342        prefs_->set("eY", y());
01343        prefs_->set("isEditMode", isEditMode_);
01344        prefs_->set("Width", width_);
01345        prefs_->set("WidthEM", widthEm_);
01346        prefs_->set("WidthLeft", widthLeft_);
01347        prefs_->set("WidthRight", widthRight_);
01348        prefs_->set("Height", height_);
01349        prefs_->set("InitX", x());
01350        prefs_->set("InitY", y());
01351        storeVisibility(false);
01352        storePosition();
01353        SMBase::closeEvent(e);
01354        emit closed();
01355 }
01356 
01357 void StyleManager::showEvent(QShowEvent *e)
01358 {
01359        static bool isFirst = true;
01360        if (isModal())
01361        {
01362               isStoryEditMode_=true;
01363               applyButton->setEnabled(false);
01364               slotEdit();
01365        }
01366        setOkButtonText();
01367        SMBase::showEvent(e);
01368        if (isFirst)
01369        {
01370               QPoint p(prefs_->getInt("InitX", x()), prefs_->getInt("InitY", y()));
01371               move(p);
01372               isFirst = false;
01373        }
01374 }
01375 
01376 StyleManager::~StyleManager()
01377 {
01378        if (isEditMode_)
01379        {
01380               QValueList<int> l = splitter->sizes();
01381               widthLeft_ = l[0];
01382               widthRight_ = l[1];
01383               widthEm_ = width();
01384        }
01385        else
01386               width_ = width();
01387 
01388        height_ = height();
01389        prefs_->set("eX", x());
01390        prefs_->set("eY", y());
01391        prefs_->set("isEditMode", isEditMode_);
01392        prefs_->set("Width", width_);
01393        prefs_->set("WidthEM", widthEm_);
01394        prefs_->set("WidthLeft", widthLeft_);
01395        prefs_->set("WidthRight", widthRight_);
01396        prefs_->set("InitX", x());
01397        prefs_->set("InitY", y());
01398        prefs_->set("Height", height_);
01399        storeVisibility(this->isVisible());
01400        storePosition();
01401 }
01402 
01403 
01404 
01405 /*** StyleViewItem *******************************************************************/
01406 
01407 StyleView::StyleView(QWidget *parent) : QListView(parent, "StyleView")
01408 {
01409 
01410 }
01411 
01412 StyleView::~StyleView()
01413 {
01414 
01415 }
01416 
01417 void StyleView::contentsMousePressEvent(QMouseEvent *e)
01418 {
01419        if (e->button() == Qt::RightButton)
01420               emit rightButtonClicked(itemAt(e->pos()), e->globalPos(), 0);
01421        else
01422               QListView::contentsMousePressEvent(e);
01423 }
01424 
01425 void StyleView::contentsMouseDoubleClickEvent(QMouseEvent *e)
01426 {
01427        if (e->button() == Qt::LeftButton)
01428               emit doubleClicked(itemAt(e->pos()), e->globalPos(), 0);
01429        else
01430               QListView::contentsMouseDoubleClickEvent(e);
01431 }
01432 
01433 /*** StyleViewItem *******************************************************************/
01434 
01435 StyleViewItem::StyleViewItem(QListView *view, const QString &text) : QListViewItem(view, text),
01436 isRoot_(true), isDirty_(false), parentName_(QString::null), rootName_(QString::null)
01437 {
01438        setSelectable(false);
01439 }
01440 
01441 StyleViewItem::StyleViewItem(QListViewItem *parent, const QString &text, const QString &rootName)
01442 : QListViewItem(parent, text),
01443   isRoot_(false), isDirty_(false), parentName_(parent->text(0)), rootName_(rootName)
01444 {
01445 
01446 }
01447 
01448 void StyleViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align)
01449 {
01450        if (column == 0)
01451        {
01452               QFont f(p->font());
01453               f.setBold(isDirty_);
01454               p->setFont(f);
01455        }
01456 
01457        QListViewItem::paintCell(p, cg, column, width, align);
01458 }
01459 
01460 void StyleViewItem::setDirty(bool isDirty)
01461 {
01462        isDirty_ = isDirty;
01463 }
01464 
01465 bool StyleViewItem::isDirty()
01466 {
01467        return isDirty_;
01468 }
01469 
01470 bool StyleViewItem::isRoot()
01471 {
01472        return isRoot_;
01473 }
01474 
01475 QString StyleViewItem::parentName()
01476 {
01477        return parentName_;
01478 }
01479 
01480 QString StyleViewItem::rootName()
01481 {
01482        return rootName_;
01483 }
01484 
01485 StyleViewItem::~StyleViewItem()
01486 {
01487 
01488 }
01489 
01490 /*** ShortcutWidget ******************************************************************/
01491 
01492 ShortcutWidget::ShortcutWidget(QWidget *parent, const char *name) : QWidget(parent, name)
01493 {
01494        Part0 = "";
01495        Part1 = "";
01496        Part2 = "";
01497        Part3 = "";
01498        keyCode = 0;
01499        keyManagerLayout = new QVBoxLayout( this, 0, 6); 
01500        keyManagerLayout->setAlignment( Qt::AlignHCenter );
01501 
01502        keyGroup = new QButtonGroup( this, "keyGroup" );
01503        keyGroup->setFrameStyle(0);
01504        keyGroup->setColumnLayout(0, Qt::Vertical );
01505        keyGroup->layout()->setSpacing( 0 );
01506        keyGroup->layout()->setMargin( 0 );
01507        keyGroupLayout = new QGridLayout( keyGroup->layout() );
01508        keyGroupLayout->setAlignment( Qt::AlignTop );
01509        keyGroupLayout->setSpacing( 6 );
01510        keyGroupLayout->setMargin( 11 );
01511 
01512        noKey = new QRadioButton( tr( "&No Key" ), keyGroup, "noKey" );
01513        keyGroupLayout->addMultiCellWidget( noKey, 0, 1, 0, 1 );
01514        userDef = new QRadioButton( tr( "&User Defined Key" ), keyGroup, "userDef" );
01515        keyGroupLayout->addWidget( userDef, 2, 0 );
01516 
01517        keyDisplay = new QLabel( tr( "ALT+SHIFT+T" ), keyGroup, "keyDisplay" );
01518        keyDisplay->setFrameShape( QLabel::Panel );
01519        keyDisplay->setFrameShadow( QLabel::Sunken );
01520        keyDisplay->setAlignment( static_cast<int>( QLabel::AlignCenter ) );
01521        keyDisplay->setMinimumHeight(40);
01522 
01523        keyGroupLayout->addMultiCellWidget( keyDisplay, 3, 4, 0, 2 );
01524 
01525        setKeyButton = new QPushButton( tr( "Set &Key" ), keyGroup, "setKeyButton" );
01526        setKeyButton->setToggleButton(true);
01527 
01528        languageChange();
01529 
01530        keyGroupLayout->addMultiCellWidget( setKeyButton, 0, 2, 1, 1, Qt::AlignCenter );
01531        keyManagerLayout->addWidget( keyGroup );
01532 
01533        connect(noKey, SIGNAL(clicked()), this, SLOT(setNoKey()));
01534        connect(setKeyButton, SIGNAL(clicked()), this, SLOT(setKeyText()));
01535 }
01536 
01537 void ShortcutWidget::languageChange()
01538 {
01539 /***********************************/
01540 /*      Begin Tooltips             */
01541 /***********************************/
01542 
01543        QToolTip::add(noKey,        tr("No shortcut for the style")); // set no shortcut for this style
01544        QToolTip::add(userDef,      tr("Style has user defined shortcut")); // not sure what this thing does
01545        QToolTip::add(setKeyButton, tr("Assign a shortcut for the style")); // activate shorcut assigning
01546 
01547 /***********************************/
01548 /*      End Tooltips               */
01549 /***********************************/
01550 
01551        noKey->setText( tr("&No Key"));
01552        userDef->setText( tr("&User Defined Key"));
01553        setKeyButton->setText( tr("Set &Key"));
01554 }
01555 
01556 bool ShortcutWidget::event( QEvent* ev )
01557 {
01558        bool ret = QWidget::event( ev );
01559        if ( ev->type() == QEvent::KeyPress )
01560               keyPressEvent((QKeyEvent*)ev);
01561        if ( ev->type() == QEvent::KeyRelease )
01562               keyReleaseEvent((QKeyEvent*)ev);
01563        return ret;
01564 }
01565 
01566 void ShortcutWidget::keyPressEvent(QKeyEvent *k)
01567 {
01568        if (setKeyButton->isOn())
01569        {
01570               QStringList tl;
01571               if (!keyDisplay->text().isEmpty())
01572               {
01573                      tl = tl.split("+", keyDisplay->text());
01574                      Part4 = tl[tl.count()-1];
01575                      if (Part4 == tr("Alt") || Part4 == tr("Ctrl") || Part4 == tr("Shift") || Part4 == tr("Meta"))
01576                             Part4 = "";
01577               }
01578               else
01579                      Part4 = "";
01580               switch (k->key())
01581               {
01582                      case Key_Meta:
01583                             Part0 = tr("Meta+");
01584                             keyCode |= META;
01585                             break;
01586                      case Key_Shift:
01587                             Part3 = tr("Shift+");
01588                             keyCode |= SHIFT;
01589                             break;
01590                      case Key_Alt:
01591                             Part2 = tr("Alt+");
01592                             keyCode |= ALT;
01593                             break;
01594                      case Key_Control:
01595                             Part1 = tr("Ctrl+");
01596                             keyCode |= CTRL;
01597                             break;
01598                      default:
01599                             keyCode |= k->key();
01600                             keyDisplay->setText(getKeyText(keyCode));
01601 //                          if (checkKey(keyCode))
01602 //                          {
01603 //                                 QMessageBox::information(this,
01604 //                                                                           CommonStrings::trWarning,
01605 //                                                                           tr("This key sequence is already in use"),
01606 //                                                                           CommonStrings::tr_OK);
01607 //                                 keyTable->setText(currRow, 1, "");
01608 //                                 keyDisplay->setText("");
01609 //                                 if (currentKeyMapRow!=NULL)
01610 //                                        currentKeyMapRow.data().keySequence=QKeySequence();
01611 //                                 noKey->setChecked(true);
01612 //                          }
01613 //                          else
01614 //                          {
01615 //                                 QKeySequence newKeySequence(keyCode);
01616 //                                 keyTable->setText(currRow, 1, QString(newKeySequence));
01617 //                                 if (currentKeyMapRow!=NULL)
01618 //                                        currentKeyMapRow.data().keySequence=newKeySequence;
01619 //                                 userDef->setChecked(true);
01620 //                          }
01621                             setKeyButton->setOn(false);
01622                             userDef->setChecked(true);
01623                             releaseKeyboard();
01624                             emit newKey(keyDisplay->text());
01625               }
01626        }
01627        if (setKeyButton->isOn())
01628        {
01629               keyDisplay->setText(Part0+Part1+Part2+Part3+Part4);
01630        }
01631 }
01632 
01633 void ShortcutWidget::keyReleaseEvent(QKeyEvent *k)
01634 {
01635        if (setKeyButton->isOn())
01636        {
01637               if (!keyDisplay->text().isEmpty())
01638               {
01639                      QStringList tl;
01640                      tl = tl.split("+", keyDisplay->text());
01641                      Part4 = tl[tl.count()-1];
01642                      if (Part4 == tr("Alt") || Part4 == tr("Ctrl") || Part4 == tr("Shift") || Part4 == tr("Meta"))
01643                             Part4 = "";
01644               }
01645               else
01646                      Part4 = "";
01647               if (k->key() == Key_Meta)
01648               {
01649                      Part0 = "";
01650                      keyCode &= ~META;
01651               }
01652               if (k->key() == Key_Shift)
01653               {
01654                      Part3 = "";
01655                      keyCode &= ~SHIFT;
01656               }
01657               if (k->key() == Key_Alt)
01658               {
01659                      Part2 = "";
01660                      keyCode &= ~ALT;
01661               }
01662               if (k->key() == Key_Control)
01663               {
01664                      Part1 = "";
01665                      keyCode &= ~CTRL;
01666               }
01667               keyDisplay->setText(Part0+Part1+Part2+Part3+Part4);
01668        }
01669 }
01670 
01671 QString ShortcutWidget::getKeyText(int KeyC)
01672 {
01673        if ((KeyC & ~(Qt::META | Qt::CTRL | Qt::ALT | Qt::SHIFT)) == 0)
01674               return "";
01675        // on OSX Qt translates modifiers to forsaken symbols, arrows and the like
01676        // we prefer plain English
01677        QString res;
01678        if ((KeyC & Qt::META) != 0)
01679               res += "Meta+";
01680        if ((KeyC & Qt::CTRL) != 0)
01681               res += "Ctrl+";
01682        if ((KeyC & Qt::ALT) != 0)
01683               res += "Alt+";
01684        if ((KeyC & Qt::SHIFT) != 0)
01685               res += "Shift+";
01686        return res + QString(QKeySequence(KeyC & ~(Qt::META | Qt::CTRL | Qt::ALT | Qt::SHIFT)));
01687 }
01688 
01689 void ShortcutWidget::setKeyText()
01690 {
01691        if (setKeyButton->isOn())
01692        {
01693               keyCode = 0;
01694               Part0 = "";
01695               Part1 = "";
01696               Part2 = "";
01697               Part3 = "";
01698               Part4 = "";
01699               grabKeyboard();
01700        }
01701        else
01702               releaseKeyboard();
01703 }
01704 
01705 void ShortcutWidget::setShortcut(const QString &shortcut)
01706 {
01707        disconnect(noKey, SIGNAL(clicked()), this, SLOT(setNoKey()));
01708        disconnect(setKeyButton, SIGNAL(clicked()), this, SLOT(setKeyText()));
01709 
01710        setKeyButton->setOn(false);
01711        if (shortcut.length() > 0)
01712        {
01713               userDef->setChecked(true);
01714               keyDisplay->setText(shortcut);
01715        }
01716        else
01717        {
01718               noKey->setChecked(true);
01719               keyDisplay->setText("");
01720        }
01721 
01722        connect(noKey, SIGNAL(clicked()), this, SLOT(setNoKey()));
01723        connect(setKeyButton, SIGNAL(clicked()), this, SLOT(setKeyText()));
01724 }
01725 
01726 void ShortcutWidget::setNoKey()
01727 {
01728        if (noKey->isChecked())
01729        {
01730               keyDisplay->setText("");
01731               emit newKey(QString::null);
01732        }
01733 }
01734 
01735 ShortcutWidget::~ShortcutWidget()
01736 {
01737 
01738 }