Back to index

scribus-ng  1.3.4.dfsg+svn20071115
cmdmani.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 "cmdmani.h"
00008 #include "cmdutil.h"
00009 #include "mpalette.h" //CB argh.. noooooooooooooooooooooooooooooooooooo FIXME see other FIXME
00010 #include "selection.h"
00011 #include "scribuscore.h"
00012 
00013 PyObject *scribus_loadimage(PyObject* /* self */, PyObject* args)
00014 {
00015        char *Name = const_cast<char*>("");
00016        char *Image;
00017        if (!PyArg_ParseTuple(args, "es|es", "utf-8", &Image, "utf-8", &Name))
00018               return NULL;
00019        if(!checkHaveDocument())
00020               return NULL;
00021        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00022        if (item == NULL)
00023               return NULL;
00024        if (!item->asImageFrame())
00025        {
00026               PyErr_SetString(WrongFrameTypeError, QObject::tr("Target is not an image frame.","python error"));
00027               return NULL;
00028        }
00029        ScCore->primaryMainWindow()->doc->LoadPict(QString::fromUtf8(Image), item->ItemNr);
00030 //     Py_INCREF(Py_None);
00031 //     return Py_None;
00032        Py_RETURN_NONE;
00033 }
00034 
00035 PyObject *scribus_scaleimage(PyObject* /* self */, PyObject* args)
00036 {
00037        char *Name = const_cast<char*>("");
00038        double x, y;
00039        if (!PyArg_ParseTuple(args, "dd|es", &x, &y, "utf-8", &Name))
00040               return NULL;
00041        if(!checkHaveDocument())
00042               return NULL;
00043        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00044        if (item == NULL)
00045               return NULL;
00046        if (! item->asImageFrame())
00047        {
00048               PyErr_SetString(ScribusException, QObject::tr("Specified item not an image frame.","python error"));
00049               return NULL;
00050        }
00051        item->setImageXYScale(x, y);
00052        ScCore->primaryMainWindow()->doc->itemSelection_SetImageScale(x, y); //CB why when this is done above?
00053        ScCore->primaryMainWindow()->doc->updatePic();
00054 //     Py_INCREF(Py_None);
00055 //     return Py_None;
00056        Py_RETURN_NONE;
00057 }
00058 
00059 PyObject *scribus_moveobjrel(PyObject* /* self */, PyObject* args)
00060 {
00061        char *Name = const_cast<char*>("");
00062        double x, y;
00063        if (!PyArg_ParseTuple(args, "dd|es", &x, &y, "utf-8", &Name))
00064               return NULL;
00065        if(!checkHaveDocument())
00066               return NULL;
00067        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00068        if (item==NULL)
00069               return NULL;
00070        // Grab the old selection
00071        //QPtrList<PageItem> oldSelection = ScCore->primaryMainWindow()->view->SelItem;
00072        //int tempList=ScCore->primaryMainWindow()->doc->selection->backupToTempList(0);
00073        Selection tempSelection(*ScCore->primaryMainWindow()->doc->m_Selection);
00074        ScCore->primaryMainWindow()->doc->m_Selection->clear();
00075        // Clear the selection
00076        ScCore->primaryMainWindow()->view->Deselect();
00077        // Select the item, which will also select its group if
00078        // there is one.
00079        ScCore->primaryMainWindow()->view->SelectItemNr(item->ItemNr);
00080        // Move the item, or items
00081        if (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1)
00082               ScCore->primaryMainWindow()->view->moveGroup(ValueToPoint(x), ValueToPoint(y));
00083        else
00084               ScCore->primaryMainWindow()->doc->MoveItem(ValueToPoint(x), ValueToPoint(y), item);
00085        // Now restore the selection. We just have to go through and select
00086        // each and every item, unfortunately.
00087        //ScCore->primaryMainWindow()->doc->selection->restoreFromTempList(0, tempList);
00088        *ScCore->primaryMainWindow()->doc->m_Selection=tempSelection;
00089        //ScCore->primaryMainWindow()->view->Deselect();
00090        //for ( oldSelection.first(); oldSelection.current(); oldSelection.next() )
00091        //     ScCore->primaryMainWindow()->view->SelectItemNr(oldSelection.current()->ItemNr);
00092 //     Py_INCREF(Py_None);
00093 //     return Py_None;
00094        Py_RETURN_NONE;
00095 }
00096 
00097 PyObject *scribus_moveobjabs(PyObject* /* self */, PyObject* args)
00098 {
00099        char *Name = const_cast<char*>("");
00100        double x, y;
00101        if (!PyArg_ParseTuple(args, "dd|es", &x, &y, "utf-8", &Name))
00102               return NULL;
00103        if(!checkHaveDocument())
00104               return NULL;
00105        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00106        if (item == NULL)
00107               return NULL;
00108        // Grab the old selection
00109        //QPtrList<PageItem> oldSelection = ScCore->primaryMainWindow()->view->SelItem;
00110        //int tempList=ScCore->primaryMainWindow()->doc->selection->backupToTempList(0);
00111        Selection tempSelection(*ScCore->primaryMainWindow()->doc->m_Selection);
00112        // Clear the selection
00113        ScCore->primaryMainWindow()->view->Deselect();
00114        // Select the item, which will also select its group if
00115        // there is one.
00116        ScCore->primaryMainWindow()->view->SelectItemNr(item->ItemNr);
00117        // Move the item, or items
00118        if (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1)
00119        {
00120               double x2, y2, w, h;
00121               ScCore->primaryMainWindow()->doc->m_Selection->getGroupRect(&x2, &y2, &w, &h);
00122               ScCore->primaryMainWindow()->view->moveGroup(pageUnitXToDocX(x) - x2, pageUnitYToDocY(y) - y2);
00123        }
00124        else
00125               ScCore->primaryMainWindow()->doc->MoveItem(pageUnitXToDocX(x) - item->xPos(), pageUnitYToDocY(y) - item->yPos(), item);
00126        // Now restore the selection. We just have to go through and select
00127        // each and every item, unfortunately.
00128        //ScCore->primaryMainWindow()->doc->selection->restoreFromTempList(0, tempList);
00129        *ScCore->primaryMainWindow()->doc->m_Selection=tempSelection;
00130        //ScCore->primaryMainWindow()->view->Deselect();
00131        //for ( oldSelection.first(); oldSelection.current(); oldSelection.next() )
00132 //            ScCore->primaryMainWindow()->view->SelectItemNr(oldSelection.current()->ItemNr);
00133 //     Py_INCREF(Py_None);
00134 //     return Py_None;
00135        Py_RETURN_NONE;
00136 }
00137 
00138 PyObject *scribus_rotobjrel(PyObject* /* self */, PyObject* args)
00139 {
00140        char *Name = const_cast<char*>("");
00141        double x;
00142        if (!PyArg_ParseTuple(args, "d|es", &x, "utf-8", &Name))
00143               return NULL;
00144        if(!checkHaveDocument())
00145               return NULL;
00146        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00147        if (item == NULL)
00148               return NULL;
00149        ScCore->primaryMainWindow()->doc->RotateItem(item->rotation() - x, item->ItemNr);
00150 //     Py_INCREF(Py_None);
00151 //     return Py_None;
00152        Py_RETURN_NONE;
00153 }
00154 
00155 PyObject *scribus_rotobjabs(PyObject* /* self */, PyObject* args)
00156 {
00157        char *Name = const_cast<char*>("");
00158        double x;
00159        if (!PyArg_ParseTuple(args, "d|es", &x, "utf-8", &Name))
00160               return NULL;
00161        if(!checkHaveDocument())
00162               return NULL;
00163        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00164        if (item == NULL)
00165               return NULL;
00166        ScCore->primaryMainWindow()->doc->RotateItem(x * -1.0, item->ItemNr);
00167 //     Py_INCREF(Py_None);
00168 //     return Py_None;
00169        Py_RETURN_NONE;
00170 }
00171 
00172 PyObject *scribus_sizeobjabs(PyObject* /* self */, PyObject* args)
00173 {
00174        char *Name = const_cast<char*>("");
00175        double x, y;
00176        if (!PyArg_ParseTuple(args, "dd|es", &x, &y, "utf-8", &Name))
00177               return NULL;
00178        if(!checkHaveDocument())
00179               return NULL;
00180        PageItem *item = GetUniqueItem(QString::fromUtf8(Name));
00181        if (item == NULL)
00182               return NULL;
00183        ScCore->primaryMainWindow()->doc->SizeItem(ValueToPoint(x), ValueToPoint(y), item->ItemNr);
00184 //     Py_INCREF(Py_None);
00185 //     return Py_None;
00186        Py_RETURN_NONE;
00187 }
00188 
00189 PyObject *scribus_groupobj(PyObject* /* self */, PyObject* args)
00190 {
00191        char *Name = const_cast<char*>("");
00192        PyObject *il = 0;
00193        if (!PyArg_ParseTuple(args, "|O", &il))
00194               return NULL;
00195        if(!checkHaveDocument())
00196               return NULL;
00197        uint ap = ScCore->primaryMainWindow()->doc->currentPage()->pageNr();
00198        // If we were passed a list of items to group...
00199        if (il != 0)
00200        {
00201               int len = PyList_Size(il);
00202               if (len < 2)
00203               {
00204                      // We can't very well group only one item
00205                      PyErr_SetString(NoValidObjectError, QObject::tr("Cannot group less than two items", "python error"));
00206                      return NULL;
00207               }
00208               QStringList oldSelection = getSelectedItemsByName();
00209               ScCore->primaryMainWindow()->view->Deselect();
00210               for (int i = 0; i < len; i++)
00211               {
00212                      // FIXME: We might need to explicitly get this string as utf8
00213                      // but as sysdefaultencoding is utf8 it should be a no-op to do
00214                      // so anyway.
00215                      Name = PyString_AsString(PyList_GetItem(il, i));
00216                      PageItem *ic = GetUniqueItem(QString::fromUtf8(Name));
00217                      if (ic == NULL)
00218                             return NULL;
00219                      ScCore->primaryMainWindow()->view->SelectItemNr(ic->ItemNr);
00220               }
00221               ScCore->primaryMainWindow()->GroupObj();
00222               setSelectedItemsByName(oldSelection);
00223        }
00224        // or if no argument list was given but there is a selection...
00225        else if (ScCore->primaryMainWindow()->doc->m_Selection->count() != 0)
00226        {
00227               if (ScCore->primaryMainWindow()->doc->m_Selection->count() < 2)
00228               {
00229                      // We can't very well group only one item
00230                      PyErr_SetString(NoValidObjectError, QObject::tr("Can't group less than two items", "python error"));
00231                      return NULL;
00232               }
00233               ScCore->primaryMainWindow()->GroupObj();
00234               ScCore->primaryMainWindow()->view->GotoPage(ap);
00235        }
00236        else
00237        {
00238               PyErr_SetString(PyExc_TypeError, QObject::tr("Need selection or argument list of items to group", "python error"));
00239               return NULL;
00240        }
00241 //     Py_INCREF(Py_None);
00242 //     return Py_None;
00243        Py_RETURN_NONE;
00244 }
00245 
00246 PyObject *scribus_ungroupobj(PyObject* /* self */, PyObject* args)
00247 {
00248        char *Name = const_cast<char*>("");
00249        if (!PyArg_ParseTuple(args, "|es", "utf-8", &Name))
00250               return NULL;
00251        if(!checkHaveDocument())
00252               return NULL;
00253        PageItem *i = GetUniqueItem(QString::fromUtf8(Name));
00254        if (i == NULL)
00255               return NULL;
00256        ScCore->primaryMainWindow()->UnGroupObj();
00257 //     Py_INCREF(Py_None);
00258 //     return Py_None;
00259        Py_RETURN_NONE;
00260 }
00261 
00262 PyObject *scribus_scalegroup(PyObject* /* self */, PyObject* args)
00263 {
00264        char *Name = const_cast<char*>("");
00265        double sc;
00266        if (!PyArg_ParseTuple(args, "d|es", &sc, "utf-8", &Name))
00267               return NULL;
00268        if(!checkHaveDocument())
00269               return NULL;
00270        if (sc == 0.0)
00271        {
00272               PyErr_SetString(PyExc_ValueError, QObject::tr("Cannot scale by 0%.","python error"));
00273               return NULL;
00274        }
00275        PageItem *i = GetUniqueItem(QString::fromUtf8(Name));
00276        if (i == NULL)
00277               return NULL;
00278        ScCore->primaryMainWindow()->view->Deselect();
00279        ScCore->primaryMainWindow()->view->SelectItemNr(i->ItemNr);
00280        int h = ScCore->primaryMainWindow()->view->frameResizeHandle;
00281        ScCore->primaryMainWindow()->view->frameResizeHandle = 1;
00282        ScCore->primaryMainWindow()->view->scaleGroup(sc, sc);
00283        ScCore->primaryMainWindow()->view->frameResizeHandle = h;
00284 //     Py_INCREF(Py_None);
00285 //     return Py_None;
00286        Py_RETURN_NONE;
00287 }
00288 
00289 PyObject *scribus_getselobjnam(PyObject* /* self */, PyObject* args)
00290 {
00291        int i = 0;
00292        if (!PyArg_ParseTuple(args, "|i", &i))
00293               return NULL;
00294        if(!checkHaveDocument())
00295               return NULL;
00296        if ((i < static_cast<int>(ScCore->primaryMainWindow()->doc->m_Selection->count())) && (i > -1))
00297               return PyString_FromString(ScCore->primaryMainWindow()->doc->m_Selection->itemAt(i)->itemName().utf8());
00298        else
00299               // FIXME: Should probably return None if no selection?
00300               return PyString_FromString("");
00301 }
00302 
00303 PyObject *scribus_selcount(PyObject* /* self */)
00304 {
00305        if(!checkHaveDocument())
00306               return NULL;
00307        return PyInt_FromLong(static_cast<long>(ScCore->primaryMainWindow()->doc->m_Selection->count()));
00308 }
00309 
00310 PyObject *scribus_selectobj(PyObject* /* self */, PyObject* args)
00311 {
00312        char *Name = const_cast<char*>("");
00313        if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
00314               return NULL;
00315        if(!checkHaveDocument())
00316               return NULL;
00317        PageItem *i = GetUniqueItem(QString::fromUtf8(Name));
00318        if (i == NULL)
00319               return NULL;
00320        ScCore->primaryMainWindow()->view->SelectItemNr(i->ItemNr);
00321 //     Py_INCREF(Py_None);
00322 //     return Py_None;
00323        Py_RETURN_NONE;
00324 }
00325 
00326 PyObject *scribus_deselect(PyObject* /* self */)
00327 {
00328        if(!checkHaveDocument())
00329               return NULL;
00330        ScCore->primaryMainWindow()->view->Deselect();
00331 //     Py_INCREF(Py_None);
00332 //     return Py_None;
00333        Py_RETURN_NONE;
00334 }
00335 
00336 PyObject *scribus_lockobject(PyObject* /* self */, PyObject* args)
00337 {
00338        char *name = const_cast<char*>("");
00339        if (!PyArg_ParseTuple(args, "|es", "utf-8", &name))
00340               return NULL;
00341        if(!checkHaveDocument())
00342               return NULL;
00343        PageItem *item = GetUniqueItem(QString::fromUtf8(name));
00344        if (item == NULL)
00345               return NULL;
00346        item->toggleLock();
00347        if (item->locked())
00348               return PyInt_FromLong(1);
00349        return PyInt_FromLong(0);
00350 }
00351 
00352 PyObject *scribus_islocked(PyObject* /* self */, PyObject* args)
00353 {
00354        char *name = const_cast<char*>("");
00355        if (!PyArg_ParseTuple(args, "|es", "utf-8", &name))
00356               return NULL;
00357        // FIXME: Rather than toggling the lock, we should probably let the user set the lock state
00358        // and instead provide a different function like toggleLock()
00359        if(!checkHaveDocument())
00360               return NULL;
00361        PageItem *item = GetUniqueItem(QString::fromUtf8(name));
00362        if (item == NULL)
00363               return NULL;
00364        if (item->locked())
00365               return PyBool_FromLong(1);
00366        return PyBool_FromLong(0);
00367 }
00368 
00369 PyObject *scribus_setscaleimagetoframe(PyObject* /* self */, PyObject* args, PyObject* kw)
00370 {
00371        char *name = const_cast<char*>("");
00372        long int scaleToFrame = 0;
00373        long int proportional = 1;
00374        char* kwargs[] = {"scaletoframe", "proportional", "name", NULL};
00375        if (!PyArg_ParseTupleAndKeywords(args, kw, "i|ies", kwargs, &scaleToFrame, &proportional, "utf-8", &name))
00376               return NULL;
00377        if(!checkHaveDocument())
00378               return NULL;
00379        PageItem *item = GetUniqueItem(QString::fromUtf8(name));
00380        if (item == NULL)
00381               return NULL;
00382        if (! item->asImageFrame())
00383        {
00384               PyErr_SetString(ScribusException, QObject::tr("Specified item not an image frame.","python error"));
00385               return NULL;
00386        }
00387        // Set the item to scale if appropriate. ScaleType 1 is free
00388        // scale, 0 is scale to frame.
00389        item->ScaleType = scaleToFrame == 0;
00390        // Now, if the user has chosen to set the proportional mode,
00391        // set it. 1 is proportional, 0 is free aspect.
00392        if (proportional != -1)
00393               item->AspectRatio = proportional > 0;
00394        // Force the braindead app to notice the changes
00395        
00396        //FIXME emit or something so we dont need this
00397        ScCore->primaryMainWindow()->propertiesPalette->setLvalue(item->imageXScale(), item->imageYScale(), item->imageXOffset(), item->imageYOffset());
00398        item->AdjustPictScale();
00399        //ScCore->primaryMainWindow()->view->AdjustPictScale(item);
00400        
00401        ScCore->primaryMainWindow()->view->RefreshItem(item);
00402 //     Py_INCREF(Py_None);
00403 //     return Py_None;
00404        Py_RETURN_NONE;
00405 }
00406 
00410 void cmdmanidocwarnings()
00411 {
00412     QStringList s;
00413     s << scribus_moveobjrel__doc__ << scribus_moveobjabs__doc__ << scribus_rotobjrel__doc__ << scribus_rotobjabs__doc__ << scribus_sizeobjabs__doc__ << scribus_getselobjnam__doc__ << scribus_selcount__doc__ << scribus_selectobj__doc__ <<scribus_deselect__doc__ <<scribus_groupobj__doc__ <<scribus_ungroupobj__doc__ <<scribus_scalegroup__doc__ <<scribus_loadimage__doc__ <<scribus_scaleimage__doc__ <<scribus_lockobject__doc__ <<scribus_islocked__doc__ <<scribus_setscaleimagetoframe__doc__;
00414 }