Back to index

scribus-ng  1.3.4.dfsg+svn20071115
selection.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        copyright            : (C) 2005 by Craig Bradney
00009        email                : cbradney@zip.com.au
00010 ***************************************************************************/
00011 
00012 /***************************************************************************
00013 *                                                                         *
00014 *   This program is free software; you can redistribute it and/or modify  *
00015 *   it under the terms of the GNU General Public License as published by  *
00016 *   the Free Software Foundation; either version 2 of the License, or     *
00017 *   (at your option) any later version.                                   *
00018 *                                                                         *
00019 ***************************************************************************/
00020 #include "selection.h"
00021 #include "selection.moc"
00022 
00023 #include "scribusdoc.h"
00024 
00025 Selection::Selection(QObject* parent)
00026        : QObject(parent),
00027        m_hasGroupSelection(false),
00028        m_isGUISelection(false)
00029 {
00030 }
00031 
00032 Selection::Selection(QObject* parent, bool guiSelection) 
00033        : QObject(parent),
00034        m_hasGroupSelection(false),
00035        m_isGUISelection(guiSelection)
00036 {
00037 }
00038 
00039 Selection::Selection(const Selection& other) :
00040        QObject(other.parent()),
00041        m_SelList(other.m_SelList),
00042        m_hasGroupSelection(other.m_hasGroupSelection),
00043        m_isGUISelection(other.m_isGUISelection)
00044 {
00045        if (m_isGUISelection && !m_SelList.isEmpty())
00046        {
00047               m_SelList[0]->connectToGUI();
00048               m_SelList[0]->emitAllToGUI();
00049               m_SelList[0]->setSelected(true);
00050               emit selectionIsMultiple(m_hasGroupSelection);
00051        }
00052 }
00053 
00054 Selection& Selection::operator=( const Selection &other )
00055 {
00056        if (&other==this)
00057               return *this;
00058        m_SelList=other.m_SelList;
00059        m_hasGroupSelection=other.m_hasGroupSelection;
00060        m_isGUISelection=other.m_isGUISelection;
00061        if (m_isGUISelection && !m_SelList.isEmpty())
00062        {
00063               m_SelList[0]->connectToGUI();
00064               m_SelList[0]->emitAllToGUI();
00065               m_SelList[0]->setSelected(true);
00066               emit selectionIsMultiple(m_hasGroupSelection);
00067        }
00068        return *this;
00069 }
00070 
00071 void Selection::copy(Selection& other, bool copyGUISelection, bool emptyOther)
00072 {
00073        if (&other==this)
00074               return;
00075        m_SelList=other.m_SelList;
00076        m_hasGroupSelection=other.m_hasGroupSelection;
00077        if (copyGUISelection)
00078               m_isGUISelection=other.m_isGUISelection;
00079        if (m_isGUISelection && !m_SelList.isEmpty())
00080        {
00081               m_SelList[0]->connectToGUI();
00082               m_SelList[0]->emitAllToGUI();
00083               m_SelList[0]->setSelected(true);
00084               emit selectionIsMultiple(m_hasGroupSelection);
00085        }
00086        if (emptyOther)
00087               other.clear();
00088 }
00089 
00090 Selection::~Selection()
00091 {
00092 }
00093 
00094 bool Selection::clear()
00095 {
00096        if (!m_SelList.isEmpty())
00097        {
00098               SelectionList::Iterator itend=m_SelList.end();
00099               SelectionList::Iterator it=m_SelList.begin();
00100               while (it!=itend)
00101               {
00102                      (*it)->setSelected(false);
00103                      (*it)->isSingleSel=false;
00104                      if (m_isGUISelection)
00105                             (*it)->disconnectFromGUI();
00106                      ++it;
00107               }
00108        }
00109        m_SelList.clear();
00110        m_hasGroupSelection=false;
00111 //     if (m_isGUISelection)
00112 //            emit empty();
00113        if (m_isGUISelection)
00114               emit selectionChanged();
00115        return true;
00116 }
00117 
00118 bool Selection::connectItemToGUI()
00119 {
00120        bool ret = false;
00121        if (!m_isGUISelection || m_SelList.isEmpty())
00122               return ret;
00123        if (m_hasGroupSelection==false)
00124        {
00125               QGuardedPtr<PageItem> pi=m_SelList.first();
00126               //Quick check to see if the pointer is NULL, if its NULL, we should remove it from the list now
00127               if (pi.isNull())
00128               {
00129                      m_SelList.remove(pi);
00130                      return ret;
00131               }
00132               ret = pi->connectToGUI();
00133               pi->emitAllToGUI();
00134               emit selectionChanged();
00135        }
00136        else
00137        {
00138               ret = m_SelList.first()->connectToGUI();
00139               m_SelList.first()->emitAllToGUI();
00140               emit selectionChanged();
00141               emit selectionIsMultiple(m_hasGroupSelection);
00142        }
00143        return ret;
00144 }
00145 
00146 bool Selection::disconnectAllItemsFromGUI()
00147 {
00148        if (!m_isGUISelection || m_SelList.isEmpty())
00149               return false;
00150        SelectionList::Iterator it2end=m_SelList.end();
00151        SelectionList::Iterator it2=m_SelList.begin();
00152        while (it2!=it2end)
00153        {
00154               (*it2)->disconnectFromGUI();
00155               ++it2;
00156        }
00157        return true;
00158 }
00159 
00160 bool Selection::addItem(PageItem *item, bool ignoreGUI)
00161 {
00162        if (item==NULL)
00163               return false;
00164        bool listWasEmpty=m_SelList.isEmpty();
00165        if (listWasEmpty || !m_SelList.contains(item))
00166        {
00167               m_SelList.append(item);
00168               item->setSelected(true);
00169               if (m_isGUISelection && !ignoreGUI)
00170               {
00171                      emit selectionChanged();
00172               }
00173 
00174               m_hasGroupSelection=(m_SelList.count()>1);
00175               if (m_isGUISelection && !ignoreGUI)
00176               {
00177                      if (listWasEmpty)
00178                      {
00179                             item->connectToGUI();
00180                             item->emitAllToGUI();
00181                      }
00182                      emit selectionIsMultiple(m_hasGroupSelection);
00183               }
00184               return true;
00185        }
00186        return false;
00187 }
00188 
00189 bool Selection::prependItem(PageItem *item, bool doEmit)
00190 {
00191        if (item==NULL)
00192               return false;
00193        if (!m_SelList.contains(item))
00194        {
00195               if (m_isGUISelection && !m_SelList.isEmpty())
00196                      m_SelList[0]->disconnectFromGUI();
00197               m_SelList.prepend(item);
00198               if (m_isGUISelection)
00199               {
00200                      item->setSelected(true);
00201                      emit selectionChanged();
00202               }
00203 
00204               m_hasGroupSelection=(m_SelList.count()>1);
00205               if (m_isGUISelection)
00206               {
00207                      item->connectToGUI();
00208                      if (doEmit)
00209                             item->emitAllToGUI();
00210                      emit selectionIsMultiple(m_hasGroupSelection);
00211               }
00212               return true;
00213        }
00214        return false;
00215 }
00216 
00217 PageItem *Selection::itemAt_(int index)
00218 {
00219        if (!m_SelList.isEmpty() && static_cast<uint>(index)<m_SelList.count())
00220        {
00221               QGuardedPtr<PageItem> pi=m_SelList[index];
00222               //If not NULL return it, otherwise remove from the list and return NULL
00223               if (!pi.isNull())
00224                      return pi;
00225               SelectionList::Iterator it=m_SelList.at(index);
00226               m_SelList.remove(it);
00227        }
00228        return NULL;
00229 }
00230 
00231 bool Selection::removeFirst()
00232 {
00233        if (!m_SelList.isEmpty())
00234        {
00235               removeItem(m_SelList.first());
00236               if (m_SelList.isEmpty())
00237                      return true;
00238               if (m_isGUISelection)
00239               {
00240                      m_SelList.first()->connectToGUI();
00241                      emit selectionChanged();
00242               }
00243        }
00244        return false;
00245 }
00246 
00247 bool Selection::removeItem(PageItem *item)
00248 {
00249        if (!m_SelList.isEmpty() && m_SelList.contains(item))
00250        {
00251               bool removeOk=m_SelList.remove(item);
00252               if (removeOk)
00253               {
00254                      item->setSelected(false);
00255                      item->isSingleSel = false;
00256               }
00257               if (m_SelList.isEmpty())
00258               {
00259                      m_hasGroupSelection=false;
00260 //                   if (m_isGUISelection)
00261 //                          emit empty();
00262               }
00263               else if (m_isGUISelection)
00264               {
00265                      m_SelList.first()->connectToGUI();
00266                      emit selectionChanged();
00267               }
00268               return removeOk;
00269        }
00270        return false;
00271 }
00272 
00273 PageItem* Selection::takeItem(uint itemIndex)
00274 {
00275        if (!m_SelList.isEmpty() && itemIndex<m_SelList.count())
00276        {
00277               PageItem *item=m_SelList[itemIndex];
00278               bool removeOk=m_SelList.remove(item);
00279               if (removeOk)
00280               {
00281                      item->setSelected(false);
00282                      item->isSingleSel = false;
00283                      if (itemIndex==0)
00284                      {
00285                             if (m_isGUISelection)
00286                             {
00287                                    item->disconnectFromGUI();
00288                                    if (m_SelList.count()>0)
00289                                           m_SelList[0]->connectToGUI();
00290                             }
00291                      }
00292                      if (m_SelList.isEmpty())
00293                      {
00294                             m_hasGroupSelection=false;
00295 //                          if (m_isGUISelection)
00296 //                                 emit empty();
00297                      }
00298                      if (m_isGUISelection)
00299                             emit selectionChanged();
00300                      return item;
00301               }
00302        }
00303        if (m_isGUISelection)
00304               emit selectionChanged();
00305        return NULL;
00306 }
00307 
00308 QStringList Selection::getSelectedItemsByName() const
00309 {
00310        QStringList names;
00311        SelectionList::ConstIterator it=m_SelList.begin();
00312        SelectionList::ConstIterator itend=m_SelList.end();
00313        for ( ; it!=itend ; ++it)
00314               names.append((*it)->itemName());
00315        return names;
00316 }
00317 
00318 double Selection::width() const
00319 {
00320        if (m_SelList.isEmpty())
00321               return 0.0;
00322        double minX=9999999.9;
00323        double maxX=-9999999.9;
00324        SelectionList::ConstIterator it=m_SelList.begin();
00325        SelectionList::ConstIterator itend=m_SelList.end();
00326        double x1=0.0,x2=0.0,y1=0.0,y2=0.0;
00327        for ( ; it!=itend ; ++it)
00328        {
00329               (*it)->getBoundingRect(&x1, &y1, &x2, &y2);
00330               if (x1<minX)
00331                      minX=x1;
00332               if (x2>maxX)
00333                      maxX=x2;
00334        }
00335        return maxX-minX;
00336 }
00337 
00338 double Selection::height() const
00339 {
00340        if (m_SelList.isEmpty())
00341               return 0.0;
00342        double minY=9999999.9;
00343        double maxY=-9999999.9;
00344        SelectionList::ConstIterator it=m_SelList.begin();
00345        SelectionList::ConstIterator itend=m_SelList.end();
00346        double x1=0.0,x2=0.0,y1=0.0,y2=0.0;
00347        for ( ; it!=itend ; ++it)
00348        {
00349               (*it)->getBoundingRect(&x1, &y1, &x2, &y2);
00350               if (y1<minY)
00351                      minY=y1;
00352               if (y2>maxY)
00353                      maxY=y2;
00354        }
00355        return maxY-minY;
00356 }
00357 
00358 void Selection::setGroupRect()
00359 {
00360        PageItem *currItem;
00361        double minx = 99999.9;
00362        double miny = 99999.9;
00363        double maxx = -99999.9;
00364        double maxy = -99999.9;
00365        uint selectedItemCount=count();
00366        for (uint gc = 0; gc < selectedItemCount; ++gc)
00367        {
00368               currItem = itemAt(gc);
00369               if (currItem->rotation() != 0)
00370               {
00371                      FPointArray pb(4);
00372                      pb.setPoint(0, FPoint(currItem->xPos(), currItem->yPos()));
00373                      pb.setPoint(1, FPoint(currItem->width(), 0.0, currItem->xPos(), currItem->yPos(), currItem->rotation(), 1.0, 1.0));
00374                      pb.setPoint(2, FPoint(currItem->width(), currItem->height(), currItem->xPos(), currItem->yPos(), currItem->rotation(), 1.0, 1.0));
00375                      pb.setPoint(3, FPoint(0.0, currItem->height(), currItem->xPos(), currItem->yPos(), currItem->rotation(), 1.0, 1.0));
00376                      for (uint pc = 0; pc < 4; ++pc)
00377                      {
00378                             minx = QMIN(minx, pb.point(pc).x());
00379                             miny = QMIN(miny, pb.point(pc).y());
00380                             maxx = QMAX(maxx, pb.point(pc).x());
00381                             maxy = QMAX(maxy, pb.point(pc).y());
00382                      }
00383               }
00384               else
00385               {
00386                      minx = QMIN(minx, currItem->xPos());
00387                      miny = QMIN(miny, currItem->yPos());
00388                      maxx = QMAX(maxx, currItem->xPos() + currItem->width());
00389                      maxy = QMAX(maxy, currItem->yPos() + currItem->height());
00390               }
00391        }
00392        groupX = minx;
00393        groupY = miny;
00394        groupW = maxx - minx;
00395        groupH = maxy - miny;
00396 }
00397 
00398 void Selection::getGroupRect(double *x, double *y, double *w, double *h)
00399 {
00400        *x = groupX;
00401        *y = groupY;
00402        *w = groupW;
00403        *h = groupH;
00404 }
00405 
00406 bool Selection::itemsAreSameType() const
00407 {
00408        //CB Putting count=1 before isempty test as its probably the most likely, given our view code.
00409        if (m_SelList.count()==1)
00410               return true;
00411        if (m_SelList.isEmpty())
00412               return false;
00413        SelectionList::ConstIterator it=m_SelList.begin();
00414        SelectionList::ConstIterator itend=m_SelList.end();
00415        PageItem::ItemType itemType = (*it)->itemType();
00416        for ( ; it!=itend ; ++it)
00417        {
00418               if ((*it)->isGroupControl)         // ignore GroupControl items
00419                      continue;
00420               if ((*it)->itemType() != itemType)
00421                      return false;
00422        }
00423        return true;
00424 }