Back to index

scribus-ng  1.3.4.dfsg+svn20071115
guidemanagercore.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 <qpen.h>
00009 #include <qtabwidget.h>
00010 #include "guidemanagercore.h"
00011 #include "scpainter.h"
00012 #include "page.h"
00013 #include "fpoint.h"
00014 #include "undomanager.h"
00015 #include "undostate.h"
00016 #include "guidemanager.h"
00017 #include "scribuscore.h"
00018 #include "pagestructs.h"
00019 #include "selection.h"
00020 
00021 
00022 GuideManagerCore::GuideManagerCore():
00023        undoManager(UndoManager::instance()),
00024        m_page(0),
00025        m_horizontalAutoGap(0.0),
00026        m_verticalAutoGap(0.0),
00027        m_horizontalAutoCount(0),
00028        m_verticalAutoCount(0),
00029        m_horizontalAutoRefer(0),
00030        m_verticalAutoRefer(0)
00031 {
00032        verticalStdG.clear();
00033        verticalAutoG.clear();
00034        horizontalStdG.clear();
00035        horizontalAutoG.clear();
00036 }
00037 
00038 GuideManagerCore::GuideManagerCore(Page *parentPage):
00039        undoManager(UndoManager::instance()),
00040        m_page(parentPage),
00041        m_horizontalAutoGap(0.0),
00042        m_verticalAutoGap(0.0),
00043        m_horizontalAutoCount(0),
00044        m_verticalAutoCount(0),
00045        m_horizontalAutoRefer(0),
00046        m_verticalAutoRefer(0)
00047 {
00048        verticalStdG.clear();
00049        verticalAutoG.clear();
00050        horizontalStdG.clear();
00051        horizontalAutoG.clear();
00052 }
00053 
00054 GuideManagerCore::~GuideManagerCore()
00055 {
00056 }
00057 
00058 void GuideManagerCore::setPage(Page *p)
00059 {
00060        m_page = p;
00061 }
00062 
00063 void GuideManagerCore::addHorizontal(double value, GuideType type)
00064 {
00065        switch (type)
00066        {
00067               case Standard:
00068                      if (!horizontalStdG.contains(value))
00069                      {
00070                             horizontalStdG.append(value);
00071                             if (UndoManager::undoEnabled())
00072                             {
00073                                    SimpleState* ss = new SimpleState(Um::AddVGuide, 0, Um::IGuides);
00074                                    ss->set("ADD_H", value);
00075                                    undoManager->action(m_page, ss);
00076                             }
00077                      }
00078                      break;
00079               case Auto:
00080                      break;
00081        }
00082 }
00083 
00084 void GuideManagerCore::addHorizontals(Guides values, GuideType type)
00085 {
00086        Guides::iterator it;
00087        switch (type)
00088        {
00089               case Standard:
00090                      for (it = values.begin(); it != values.end(); ++it)
00091                      {
00092                             if (!horizontalStdG.contains((*it)))
00093                                    horizontalStdG.append((*it));
00094                      }
00095                      break;
00096               case Auto:
00097                      horizontalAutoG.clear();
00098                      for (it = values.begin(); it != values.end(); ++it)
00099                             horizontalAutoG.append((*it));
00100                      break;
00101        }
00102 }
00103 
00104 void GuideManagerCore::addVertical(double value, GuideType type)
00105 {
00106        switch (type)
00107        {
00108               case Standard:
00109                      if (!verticalStdG.contains(value))
00110                      {
00111                             verticalStdG.append(value);
00112                             if (UndoManager::undoEnabled())
00113                             {
00114                                    SimpleState* ss = new SimpleState(Um::AddVGuide, 0, Um::IGuides);
00115                                    ss->set("ADD_V", value);
00116                                    undoManager->action(m_page, ss);
00117                             }
00118                      }
00119                      break;
00120               case Auto:
00121                      break;
00122        }
00123 }
00124 
00125 void GuideManagerCore::addVerticals(Guides values, GuideType type)
00126 {
00127        Guides::iterator it;
00128        switch (type)
00129        {
00130               case Standard:
00131                      for (it = values.begin(); it != values.end(); ++it)
00132                      {
00133                             if (!verticalStdG.contains((*it)))
00134                                    verticalStdG.append((*it));
00135                      }
00136                      break;
00137               case Auto:
00138                      verticalAutoG.clear();
00139                      for (it = values.begin(); it != values.end(); ++it)
00140                             verticalAutoG.append((*it));
00141                      break;
00142        }
00143 }
00144 
00145 void GuideManagerCore::deleteHorizontal(double value, GuideType type)
00146 {
00147        switch (type)
00148        {
00149               case Standard:
00150                      horizontalStdG.remove(horizontalStdG.find(value));
00151                      if (UndoManager::undoEnabled())
00152                      {
00153                             SimpleState* ss = new SimpleState(Um::DelVGuide, 0, Um::IGuides);
00154                             ss->set("REMOVE_H", value);
00155                             undoManager->action(m_page, ss);
00156                      }
00157                      break;
00158               case Auto:
00159                      break;
00160        }
00161 }
00162 
00163 void GuideManagerCore::deleteVertical(double value, GuideType type)
00164 {
00165        switch (type)
00166        {
00167               case Standard:
00168                      verticalStdG.remove(verticalStdG.find(value));
00169                      if (UndoManager::undoEnabled())
00170                      {
00171                             SimpleState* ss = new SimpleState(Um::DelVGuide, 0, Um::IGuides);
00172                             ss->set("REMOVE_V", value);
00173                             undoManager->action(m_page, ss);
00174                      }
00175                      break;
00176               case Auto:
00177                      break;
00178        }
00179 }
00180 
00181 Guides GuideManagerCore::horizontals(GuideType type)
00182 {
00183        switch (type)
00184        {
00185               case Standard:
00186                      return horizontalStdG;
00187                      break;
00188               case Auto:
00189                      return horizontalAutoG;
00190                      break;
00191        }
00192        // just to prevent the compiler warnings
00193        return horizontalStdG;
00194 }
00195 
00196 Guides GuideManagerCore::verticals(GuideType type)
00197 {
00198        switch (type)
00199        {
00200               case Standard:
00201                      return verticalStdG;
00202                      break;
00203               case Auto:
00204                      return verticalAutoG;
00205                      break;
00206        }
00207        return verticalStdG;
00208 }
00209 
00210 double GuideManagerCore::horizontal(uint ix, GuideType type)
00211 {
00212        switch (type)
00213        {
00214               case Standard:
00215                      return horizontalStdG[ix];
00216                      break;
00217               case Auto:
00218                      break;
00219        }
00220        return -1.0; // just for compiler warning
00221 }
00222 
00223 double GuideManagerCore::vertical(uint ix, GuideType type)
00224 {
00225        switch (type)
00226        {
00227               case Standard:
00228                      return verticalStdG[ix];
00229                      break;
00230               case Auto:
00231                      break;
00232        }
00233        return -1.0; // just for compiler warning
00234 }
00235 
00236 void GuideManagerCore::clearHorizontals(GuideType type)
00237 {
00238        switch (type)
00239        {
00240               case Standard:
00241                      if (undoManager->undoEnabled())
00242                      {
00243                             for (uint i = 0; i < horizontalStdG.count(); ++i)
00244                             {
00245                                    SimpleState* ss = new SimpleState(Um::DelVGuide, 0, Um::IGuides);
00246                                    ss->set("REMOVE_H", horizontalStdG[i]);
00247                                    undoManager->action(m_page, ss);
00248                             }
00249                      }
00250                      horizontalStdG.clear();
00251                      break;
00252               case Auto:
00253                      if (undoManager->undoEnabled())
00254                      {
00255                             SimpleState * ss = new SimpleState(Um::DelHAGuide, 0, Um::IGuides);
00256                             ss->set("REMOVE_HA_GAP", m_horizontalAutoGap);
00257                             ss->set("REMOVE_HA_COUNT", m_horizontalAutoCount);
00258                             ss->set("REMOVE_HA_REFER", m_horizontalAutoRefer);
00259                             undoManager->action(m_page, ss);
00260                      }
00261                      m_horizontalAutoGap = 0.0;
00262                      m_horizontalAutoCount= 0;
00263                      m_horizontalAutoRefer = 0;
00264                      horizontalAutoG.clear();
00265                      break;
00266        }
00267 }
00268 
00269 void GuideManagerCore::clearVerticals(GuideType type)
00270 {
00271        switch (type)
00272        {
00273               case Standard:
00274                      if (undoManager->undoEnabled())
00275                      {
00276                             for (uint i = 0; i < verticalStdG.count(); ++i)
00277                             {
00278                                    SimpleState* ss = new SimpleState(Um::DelVGuide, 0, Um::IGuides);
00279                                    ss->set("REMOVE_V", verticalStdG[i]);
00280                                    undoManager->action(m_page, ss);
00281                             }
00282                      }
00283                      verticalStdG.clear();
00284                      break;
00285               case Auto:
00286                      if (undoManager->undoEnabled())
00287                      {
00288                             SimpleState * ss = new SimpleState(Um::DelVAGuide, 0, Um::IGuides);
00289                             ss->set("REMOVE_VA_GAP", m_verticalAutoGap);
00290                             ss->set("REMOVE_VA_COUNT", m_verticalAutoCount);
00291                             ss->set("REMOVE_VA_REFER", m_verticalAutoRefer);
00292                             undoManager->action(m_page, ss);
00293                      }
00294                      m_verticalAutoGap = 0.0;
00295                      m_verticalAutoCount = 0;
00296                      m_verticalAutoRefer = 0;
00297                      verticalAutoG.clear();
00298                      break;
00299        }
00300 }
00301 void GuideManagerCore::moveHorizontal(double from, double to, GuideType type)
00302 {
00303        switch (type)
00304        {
00305               case Standard:
00306                      horizontalStdG.remove(horizontalStdG.find(from));
00307                      horizontalStdG.append(to);
00308                      if (UndoManager::undoEnabled())
00309                      {
00310                             SimpleState* ss = new SimpleState(Um::MoveVGuide, 0, Um::IGuides);
00311                             ss->set("MOVE_H_FROM", from);
00312                             ss->set("MOVE_H_TO", to);
00313                             undoManager->action(m_page, ss);
00314                      }
00315                      break;
00316               case Auto:
00317                      break;
00318        }
00319 }
00320 
00321 void GuideManagerCore::moveVertical(double from, double to, GuideType type)
00322 {
00323        switch (type)
00324        {
00325               case Standard:
00326                      verticalStdG.remove(verticalStdG.find(from));
00327                      verticalStdG.append(to);
00328                      if (UndoManager::undoEnabled())
00329                      {
00330                             SimpleState* ss = new SimpleState(Um::MoveVGuide, 0, Um::IGuides);
00331                             ss->set("MOVE_V_FROM", from);
00332                             ss->set("MOVE_V_TO", to);
00333                             undoManager->action(m_page, ss);
00334                      }
00335                      break;
00336               case Auto:
00337                      break;
00338        }
00339 }
00340 
00341 void GuideManagerCore::copy(GuideManagerCore *target)
00342 {
00343        copy(target, Standard);
00344        copy(target, Auto);
00345 }
00346 
00347 void GuideManagerCore::copy(GuideManagerCore *target, GuideType type)
00348 {
00349        switch (type)
00350        {
00351               case Standard:
00352                      target->addHorizontals(horizontalStdG, Standard);
00353                      target->addVerticals(verticalStdG, Standard);
00354                      break;
00355               case Auto:
00356                      target->setHorizontalAutoCount(m_horizontalAutoCount);
00357                      target->setVerticalAutoCount(m_verticalAutoCount);
00358                      target->setHorizontalAutoGap(m_horizontalAutoGap);
00359                      target->setVerticalAutoGap(m_verticalAutoGap);
00360                      target->setHorizontalAutoRefer(m_horizontalAutoRefer);
00361                      target->setVerticalAutoRefer(m_verticalAutoRefer);
00362                      target->addHorizontals(horizontalAutoG, Auto);
00363                      target->addVerticals(verticalAutoG, Auto);
00364                      target->gx = gx;
00365                      target->gy = gy;
00366                      target->gw = gw;
00367                      target->gh = gh;
00368                      break;
00369        }
00370 }
00371 
00372 void GuideManagerCore::drawPage(ScPainter *p, ScribusDoc *doc, double lineWidth)
00373 {
00374        Guides::iterator it;
00375        QColor color(doc->guidesSettings.guideColor);
00376 
00377        // all standard
00378        p->setPen(color, lineWidth, Qt::DashDotLine, Qt::FlatCap, Qt::MiterJoin);
00379        for (it = verticalStdG.begin(); it != verticalStdG.end(); ++it)
00380               if ((*it) >= 0 && (*it) <= m_page->width())
00381                      p->drawLine(FPoint((*it), 0), FPoint((*it), m_page->height()));
00382        for (it = horizontalStdG.begin(); it != horizontalStdG.end(); ++it)
00383               if ((*it) >= 0 && (*it) <= m_page->height())
00384                      p->drawLine(FPoint(0, (*it)), FPoint(m_page->width(), (*it)));
00385        // highlight selected standards
00386        if (ScCore->primaryMainWindow()->guidePalette->tabWidget->currentPageIndex() == 0
00387                  && m_page->pageNr() == ScCore->primaryMainWindow()->guidePalette->pageNr())
00388        {
00389               p->setPen(Qt::red, lineWidth, Qt::DashDotLine, Qt::FlatCap, Qt::MiterJoin);
00390               Guides highlight = ScCore->primaryMainWindow()->guidePalette->selectedVerticals();
00391               for (it = highlight.begin(); it != highlight.end(); ++it)
00392                      if ((*it) >= 0 && (*it) <= m_page->width())
00393                             p->drawLine(FPoint((*it), 0), FPoint((*it), m_page->height()));
00394               highlight = ScCore->primaryMainWindow()->guidePalette->selectedHorizontals();
00395               for (it = highlight.begin(); it != highlight.end(); ++it)
00396                      if ((*it) >= 0 && (*it) <= m_page->height())
00397                             p->drawLine(FPoint(0, (*it)), FPoint(m_page->width(), (*it)));
00398        }
00399        // all auto
00400        if (ScCore->primaryMainWindow()->guidePalette->tabWidget->currentPageIndex() == 1)
00401               color = Qt::red;
00402        else
00403               color = doc->guidesSettings.guideColor;
00404        p->setPen(color, lineWidth, Qt::DashDotLine, Qt::FlatCap, Qt::MiterJoin);
00405 
00406        for (it = verticalAutoG.begin(); it != verticalAutoG.end(); ++it)
00407               if ((*it) >= 0 && (*it) <= m_page->width())
00408                      p->drawLine(FPoint((*it), 0), FPoint((*it), m_page->height()));
00409        for (it = horizontalAutoG.begin(); it != horizontalAutoG.end(); ++it)
00410               if ((*it) >= 0 && (*it) <= m_page->height())
00411                      p->drawLine(FPoint(0, (*it)), FPoint(m_page->width(), (*it)));
00412 }
00413 
00414 bool GuideManagerCore::isMouseOnHorizontal(double low, double high, GuideType type)
00415 {
00416        Guides tmp;
00417        Guides::iterator it;
00418 
00419        switch (type)
00420        {
00421               case Standard:
00422                      tmp = horizontalStdG;
00423                      break;
00424               case Auto:
00425                      tmp = horizontalAutoG;
00426                      break;
00427        }
00428        for (it = tmp.begin(); it != tmp.end(); ++it)
00429        {
00430               double guideOffset = (*it) + m_page->yOffset();
00431               if (guideOffset < low && guideOffset > high)
00432                      return true;
00433        }
00434        return false;
00435 }
00436 
00437 bool GuideManagerCore::isMouseOnVertical(double low, double high, GuideType type)
00438 {
00439        Guides tmp;
00440        Guides::iterator it;
00441 
00442        switch (type)
00443        {
00444               case Standard:
00445                      tmp = verticalStdG;
00446                      break;
00447               case Auto:
00448                      tmp = horizontalAutoG;
00449                      break;
00450        }
00451        for (it = tmp.begin(); it != tmp.end(); ++it)
00452        {
00453               double guideOffset = (*it) + m_page->xOffset();
00454               if (guideOffset < low && guideOffset > high)
00455                      return true;
00456        }
00457        return false;
00458 }
00459 
00460 QPair<double, double> GuideManagerCore::topLeft(double x, double y)// const
00461 {
00462        return QPair<double, double>(closestVertLeft(x), closestHorAbove(y));
00463 }
00464 
00465 QPair<double, double> GuideManagerCore::topRight(double x, double y)// const
00466 {
00467        return QPair<double, double>(closestVertRight(x), closestHorAbove(y));
00468 }
00469 
00470 QPair<double, double> GuideManagerCore::bottomLeft(double x, double y)// const
00471 {
00472        return QPair<double, double>(closestVertLeft(x), closestHorBelow(y));
00473 }
00474 
00475 QPair<double, double> GuideManagerCore::bottomRight(double x, double y)// const
00476 {
00477        return QPair<double, double>(closestVertRight(x), closestHorBelow(y));
00478 }
00479 
00480 double GuideManagerCore::closestHorAbove(double y)// const
00481 {
00482        double closest = 0.0;
00483        for (uint i = 0; i < horizontalStdG.size(); ++i)
00484        {
00485               if (horizontalStdG[i] < y && horizontalStdG[i] > closest)
00486                      closest = horizontalStdG[i];
00487        }
00488 
00489        for (uint i = 0; i < horizontalAutoG.size(); ++i)
00490        {
00491               if (horizontalAutoG[i] < y && horizontalAutoG[i] > closest)
00492                      closest = horizontalAutoG[i];
00493        }
00494 
00495        if (m_page->Margins.Top < y && m_page->Margins.Top > closest)
00496               closest = m_page->Margins.Top;
00497        if (m_page->height() - m_page->Margins.Bottom < y && m_page->height() - m_page->Margins.Bottom > closest)
00498               closest = m_page->height() - m_page->Margins.Bottom;
00499 
00500        return closest;
00501 }
00502 
00503 double GuideManagerCore::closestHorBelow(double y)// const
00504 {
00505        double closest = m_page->height();
00506        for (uint i = 0; i < horizontalStdG.size(); ++i)
00507        {
00508               if (horizontalStdG[i] > y && horizontalStdG[i] < closest)
00509                      closest = horizontalStdG[i];
00510        }
00511 
00512        for (uint i = 0; i < horizontalAutoG.size(); ++i)
00513        {
00514               if (horizontalAutoG[i] > y && horizontalAutoG[i] < closest)
00515                      closest = horizontalAutoG[i];
00516        }
00517 
00518        if (m_page->Margins.Top > y && m_page->Margins.Top < closest)
00519               closest = m_page->Margins.Top;
00520        if (m_page->height() - m_page->Margins.Bottom > y && m_page->height() - m_page->Margins.Bottom < closest)
00521               closest = m_page->height() - m_page->Margins.Bottom;
00522 
00523        return closest;
00524 }
00525 
00526 double GuideManagerCore::closestVertLeft(double x)// const
00527 {
00528        double closest = 0.0;
00529        for (uint i = 0; i < verticalStdG.size(); ++i)
00530        {
00531               if (verticalStdG[i] < x && verticalStdG[i] > closest)
00532                      closest = verticalStdG[i];
00533        }
00534 
00535        for (uint i = 0; i < verticalAutoG.size(); ++i)
00536        {
00537               if (verticalAutoG[i] < x && verticalAutoG[i] > closest)
00538                      closest = verticalAutoG[i];
00539        }
00540 
00541        if (m_page->Margins.Left < x && m_page->Margins.Left > closest)
00542               closest = m_page->Margins.Left;
00543        if (m_page->width() - m_page->Margins.Right < x && m_page->width() - m_page->Margins.Right > closest)
00544               closest = m_page->width() - m_page->Margins.Right;
00545 
00546        return closest;
00547 }
00548 
00549 double GuideManagerCore::closestVertRight(double x)// const
00550 {
00551        double closest = m_page->width();
00552        for (uint i = 0; i < verticalStdG.size(); ++i)
00553        {
00554               if (verticalStdG[i] > x && verticalStdG[i] < closest)
00555                      closest = verticalStdG[i];
00556        }
00557 
00558        for (uint i = 0; i < verticalAutoG.size(); ++i)
00559        {
00560               if (verticalAutoG[i] > x && verticalAutoG[i] < closest)
00561                      closest = verticalAutoG[i];
00562        }
00563 
00564        if (m_page->Margins.Left > x  && m_page->Margins.Left < closest)
00565               closest = m_page->Margins.Left;
00566        if (m_page->width() - m_page->Margins.Right > x && m_page->width() - m_page->Margins.Right < closest)
00567               closest = m_page->width() - m_page->Margins.Right;
00568 
00569        return closest;
00570 }
00571 
00572 
00573 void GuideManagerIO::readVerticalGuides(const QString guideString, Page *page, GuideManagerCore::GuideType type, bool useOldGuides)
00574 {
00575        QStringList gVal(QStringList::split(' ', guideString));
00576 
00577        for (QStringList::Iterator it = gVal.begin(); it != gVal.end(); ++it )
00578               useOldGuides ?
00579                      page->guides.addHorizontal((*it).toDouble(), type) :
00580                      page->guides.addVertical((*it).toDouble(), type);
00581 }
00582 
00583 void GuideManagerIO::readHorizontalGuides(const QString guideString, Page *page, GuideManagerCore::GuideType type, bool useOldGuides)
00584 {
00585        QStringList gVal(QStringList::split(' ', guideString));
00586        for (QStringList::Iterator it = gVal.begin(); it != gVal.end(); ++it )
00587               useOldGuides ?
00588                      page->guides.addVertical((*it).toDouble(), type):
00589                      page->guides.addHorizontal((*it).toDouble(), type);
00590 }
00591 
00592 QString GuideManagerIO::writeHorizontalGuides(Page *page, GuideManagerCore::GuideType type)
00593 {
00594        Guides::iterator it;
00595        QString retval;
00596        QString tmp;
00597        Guides tmpGuides = page->guides.horizontals(type);
00598        for (it = tmpGuides.begin(); it != tmpGuides.end(); ++it)
00599        {
00600               tmp = tmp.setNum((*it));
00601               retval += tmp + " ";
00602        }
00603        return retval;
00604 }
00605 
00606 QString GuideManagerIO::writeVerticalGuides(Page *page, GuideManagerCore::GuideType type)
00607 {
00608        Guides::iterator it;
00609        QString retval;
00610        QString tmp;
00611        Guides tmpGuides = page->guides.verticals(type);
00612        for (it = tmpGuides.begin(); it != tmpGuides.end(); ++it)
00613        {
00614               tmp = tmp.setNum((*it));
00615               retval += tmp + " ";
00616        }
00617        return retval;
00618 }
00619 
00620 QString GuideManagerIO::writeSelection(Page *page)
00621 {
00622        return QString("%1 %2 %3 %4").arg(page->guides.gx).arg(page->guides.gy).arg(page->guides.gw).arg(page->guides.gh);
00623 }
00624 
00625 void GuideManagerIO::readSelection(const QString guideString, Page *page)
00626 {
00627        QStringList gVal(QStringList::split(' ', guideString));
00628        page->guides.gx = gVal[0].toDouble();
00629        page->guides.gy = gVal[1].toDouble();
00630        page->guides.gw = gVal[2].toDouble();
00631        page->guides.gh = gVal[3].toDouble();
00632 }