Back to index

scribus-ng  1.3.4.dfsg+svn20071115
cmdutil.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 "cmdutil.h"
00008 #include "units.h"
00009 #include "page.h"
00010 #include "scribuscore.h"
00011 #include "selection.h"
00012 
00013 ScribusMainWindow* Carrier;
00014 ScribusDoc* doc;
00015 
00017 double PointToValue(double Val)
00018 {
00019        return pts2value(Val, ScCore->primaryMainWindow()->doc->unitIndex());
00020 }
00021 
00023 double ValueToPoint(double Val)
00024 {
00025        return value2pts(Val, ScCore->primaryMainWindow()->doc->unitIndex());
00026 }
00027 
00030 double pageUnitXToDocX(double pageUnitX)
00031 {
00032        return ValueToPoint(pageUnitX) + ScCore->primaryMainWindow()->doc->currentPage()->xOffset();
00033 }
00034 
00035 // Convert doc units to page units
00036 double docUnitXToPageX(double pageUnitX)
00037 {
00038        return PointToValue(pageUnitX - ScCore->primaryMainWindow()->doc->currentPage()->xOffset());
00039 }
00040 
00045 double pageUnitYToDocY(double pageUnitY)
00046 {
00047        return ValueToPoint(pageUnitY) + ScCore->primaryMainWindow()->doc->currentPage()->yOffset();
00048 }
00049 
00050 double docUnitYToPageY(double pageUnitY)
00051 {
00052        return PointToValue(pageUnitY - ScCore->primaryMainWindow()->doc->currentPage()->yOffset());
00053 }
00054 
00055 int GetItem(QString Name)
00056 {
00057        if (!Name.isEmpty())
00058        {
00059               for (uint a = 0; a < ScCore->primaryMainWindow()->doc->Items->count(); a++)
00060               {
00061                      if (ScCore->primaryMainWindow()->doc->Items->at(a)->itemName() == Name)
00062                             return static_cast<int>(a);
00063               }
00064        }
00065        else
00066        {
00067               if (ScCore->primaryMainWindow()->doc->m_Selection->count() != 0)
00068                      return ScCore->primaryMainWindow()->doc->m_Selection->itemAt(0)->ItemNr;
00069        }
00070        return -1;
00071 }
00072 
00073 void ReplaceColor(QString col, QString rep)
00074 {
00075        QColor tmpc;
00076        for (uint c = 0; c < ScCore->primaryMainWindow()->doc->Items->count(); c++)
00077        {
00078               PageItem *ite = ScCore->primaryMainWindow()->doc->Items->at(c);
00079               if (ite->itemType() == PageItem::TextFrame)
00080               {
00081                      for (int d = 0; d < ite->itemText.length(); d++)
00082                      {
00083                             //FIXME:NLS  that should work on runs
00084                             if (col == ite->itemText.charStyle(d).fillColor())
00085                                    ite->itemText.item(d)->setFillColor(rep);
00086                             if (col == ite->itemText.charStyle(d).strokeColor())
00087                                    ite->itemText.item(d)->setStrokeColor(rep);
00088                      }
00089               }
00090               if (col == ite->fillColor())
00091                      ite->setFillColor(rep);
00092               if (col == ite->lineColor())
00093                      ite->setLineColor(rep);
00094               QPtrVector<VColorStop> cstops = ite->fill_gradient.colorStops();
00095               for (uint cst = 0; cst < ite->fill_gradient.Stops(); ++cst)
00096               {
00097                      if (col == cstops.at(cst)->name)
00098                      {
00099                             ite->SetFarbe(&tmpc, rep, cstops.at(cst)->shade);
00100                             cstops.at(cst)->color = tmpc;
00101                             cstops.at(cst)->name = rep;
00102                      }
00103               }
00104        }
00105        for (uint c = 0; c < ScCore->primaryMainWindow()->doc->MasterItems.count(); c++)
00106        {
00107               PageItem *ite = ScCore->primaryMainWindow()->doc->MasterItems.at(c);
00108               if (ite->itemType() == PageItem::TextFrame)
00109               {
00110                      for (int d = 0; d < ite->itemText.length(); d++)
00111                      {
00112                             //FIXME: NLS this should work on runs
00113                             if (col == ite->itemText.charStyle(d).fillColor())
00114                                    ite->itemText.item(d)->setFillColor(rep);
00115                             if (col == ite->itemText.charStyle(d).strokeColor())
00116                                    ite->itemText.item(d)->setStrokeColor(rep);
00117                      }
00118               }
00119               if (col == ite->fillColor())
00120                      ite->setFillColor(rep);
00121               if (col == ite->lineColor())
00122                      ite->setLineColor(rep);
00123               QPtrVector<VColorStop> cstops = ite->fill_gradient.colorStops();
00124               for (uint cst = 0; cst < ite->fill_gradient.Stops(); ++cst)
00125               {
00126                      if (col == cstops.at(cst)->name)
00127                      {
00128                             ite->SetFarbe(&tmpc, rep, cstops.at(cst)->shade);
00129                             cstops.at(cst)->color = tmpc;
00130                             cstops.at(cst)->name = rep;
00131                      }
00132               }
00133        }
00134 }
00135 
00136 /* 04/07/10 returns selection if is not name specified  pv  */
00137 PageItem* GetUniqueItem(QString name)
00138 {
00139        if (name.length()==0)
00140               if (ScCore->primaryMainWindow()->doc->m_Selection->count() != 0)
00141                      return ScCore->primaryMainWindow()->doc->m_Selection->itemAt(0);
00142               else
00143               {
00144                      PyErr_SetString(NoValidObjectError, QString("Cannot use empty string for object name when there is no selection"));
00145                      return NULL;
00146               }
00147        else
00148               return getPageItemByName(name);
00149 }
00150 
00151 PageItem* getPageItemByName(QString name)
00152 {
00153        if (name.length() == 0)
00154        {
00155               PyErr_SetString(PyExc_ValueError, QString("Cannot accept empty name for pageitem"));
00156               return NULL;
00157        }
00158        for (uint j = 0; j<ScCore->primaryMainWindow()->doc->Items->count(); j++)
00159        {
00160               if (name==ScCore->primaryMainWindow()->doc->Items->at(j)->itemName())
00161                      return ScCore->primaryMainWindow()->doc->Items->at(j);
00162        } // for items
00163        PyErr_SetString(NoValidObjectError, QString("Object not found"));
00164        return NULL;
00165 }
00166 
00167 
00173 bool ItemExists(QString name)
00174 {
00175        if (name.length() == 0)
00176               return false;
00177        for (uint j = 0; j<ScCore->primaryMainWindow()->doc->Items->count(); j++)
00178        {
00179               if (name==ScCore->primaryMainWindow()->doc->Items->at(j)->itemName())
00180                      return true;
00181        } // for items
00182        return false;
00183 }
00184 
00192 bool checkHaveDocument()
00193 {
00194     if (ScCore->primaryMainWindow()->HaveDoc)
00195         return true;
00196     // Caller is required to check for false return from this function
00197     // and return NULL.
00198     PyErr_SetString(NoDocOpenError, QString("Command does not make sense without an open document"));
00199     return false;
00200 }
00201 
00202 QStringList getSelectedItemsByName()
00203 {
00204        /*
00205        QStringList names;
00206        QPtrListIterator<PageItem> it(ScCore->primaryMainWindow()->view->SelItem);
00207        for ( ; it.current() != 0 ; ++it)
00208               names.append(it.current()->itemName());
00209        return names;
00210        */
00211        return ScCore->primaryMainWindow()->doc->m_Selection->getSelectedItemsByName();
00212 }
00213 
00214 bool setSelectedItemsByName(QStringList& itemNames)
00215 {
00216        ScCore->primaryMainWindow()->view->Deselect();
00217        // For each named item
00218        for (QStringList::Iterator it = itemNames.begin() ; it != itemNames.end() ; it++)
00219        {
00220               // Search for the named item
00221               PageItem* item = 0;
00222               for (uint j = 0; j < ScCore->primaryMainWindow()->doc->Items->count(); j++)
00223                      if (*it == ScCore->primaryMainWindow()->doc->Items->at(j)->itemName())
00224                             item = ScCore->primaryMainWindow()->doc->Items->at(j);
00225               if (!item)
00226                      return false;
00227               // and select it
00228               ScCore->primaryMainWindow()->view->SelectItemNr(item->ItemNr);
00229        }
00230        return true;
00231 }