Back to index

scribus-ng  1.3.4.dfsg+svn20071115
hruler.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                           hruler.cpp  -  description
00009                              -------------------
00010     begin                : Tue Apr 10 2001
00011     copyright            : (C) 2001 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include "hruler.h"
00025 #include "hruler.moc"
00026 #include "page.h"
00027 #include <qcursor.h>
00028 #include <qcolor.h>
00029 #include <qrect.h>
00030 #include <qpointarray.h>
00031 #include "scribusview.h"
00032 #include "scribusdoc.h"
00033 #include "scribus.h"
00034 #include "selection.h"
00035 #include "units.h"
00036 #include "prefsmanager.h"
00037 
00038 #if QT_VERSION  > 0x030102
00039        #define SPLITHC SplitVCursor
00040 #else
00041        #define SPLITHC SplitHCursor
00042 #endif
00043 
00044 extern QPixmap loadIcon(QString nam);
00045 
00046 
00047 #ifdef QT_MAC
00048     #define topline 1
00049 #else
00050     #define topline 3
00051 #endif
00052 
00053 enum ruler_code 
00054 { 
00055        rc_none = 0, 
00056        rc_leftFrameDist = 1, 
00057        rc_rightFrameDist = 2,
00058        rc_indentFirst = 3,
00059        rc_leftMargin = 4,
00060        rc_tab = 5,
00061        rc_rightMargin = 6
00062 };
00063 
00064 
00065 Hruler::Hruler(ScribusView *pa, ScribusDoc *doc) : QWidget(pa)
00066 {
00067        prefsManager=PrefsManager::instance();
00068        setEraseColor(QColor(255,255,255));
00069        currDoc = doc;
00070        currView = pa;
00071        offs = 0;
00072        Markp = 0;
00073        oldMark = 0;
00074        ActCol = 1;
00075        Mpressed = false;
00076        ItemPosValid = false;
00077        RulerCode = rc_none;
00078        setMouseTracking(true);
00079        unitChange();
00080 }
00081 
00082 void Hruler::mousePressEvent(QMouseEvent *m)
00083 {
00084        if (currDoc->isLoading())
00085               return;
00086        Mpressed = true;
00087        MouseX = m->x();
00088        if (ItemPosValid)
00089        {
00090               RulerCode = rc_none;
00091               Markp = -1;
00092               double Pos = (ItemPos-offs+Extra+lineCorr)*Scaling;
00093               if ((static_cast<int>(Pos-1) < (m->x()+currDoc->guidesSettings.grabRad))
00094                                && (static_cast<int>(Pos-1) > (m->x()-currDoc->guidesSettings.grabRad)))
00095                      RulerCode = rc_leftFrameDist;
00096               Pos = (ItemEndPos-offs-RExtra-lineCorr)*Scaling;
00097               if ((static_cast<int>(Pos+1) < (m->x()+currDoc->guidesSettings.grabRad))
00098                                && (static_cast<int>(Pos+1) > (m->x()-currDoc->guidesSettings.grabRad)))
00099                      RulerCode = rc_rightFrameDist;
00100               double ColWidth = (ItemEndPos - ItemPos - (ColGap * (Cols - 1)) - Extra - RExtra - 2*lineCorr) / Cols;
00101               QRect fpo;
00102               ActCol = 0;
00103               for (int CurrCol = 0; CurrCol < Cols; ++CurrCol)
00104               {
00105                      Pos = (ItemPos-offs+(ColWidth+ColGap)*CurrCol+Extra+lineCorr)*Scaling;
00106                      fpo = QRect(static_cast<int>(Pos), topline, static_cast<int>(ColWidth*Scaling), 12);
00107                      if (fpo.contains(m->pos()))
00108                      {
00109                             ActCol = CurrCol+1;
00110                             break;
00111                      }
00112               }
00113               if (ActCol == 0)
00114               {
00115                      ActCol = 1;
00116                      return;
00117               }
00118               Pos = (ItemPos-offs+First+Indent+(ColWidth+ColGap)*(ActCol-1)+Extra+lineCorr)*Scaling;
00119               fpo = QRect(static_cast<int>(Pos)-3, topline, 6, 6);
00120               if (fpo.contains(m->pos()))
00121               {
00122                      RulerCode = rc_indentFirst;
00123                      return;
00124               }
00125               Pos = (ItemPos-offs+Indent+(ColWidth+ColGap)*(ActCol-1)+Extra+lineCorr)*Scaling;
00126               fpo = QRect(static_cast<int>(Pos)-3, 9, 6, 6);
00127               if (fpo.contains(m->pos()))
00128               {
00129                      RulerCode = rc_leftMargin;
00130                      return;
00131               }
00132               Pos = (ItemPos-offs+RMargin+(ColWidth+ColGap)*(ActCol-1)+Extra+lineCorr)*Scaling;
00133               fpo = QRect(static_cast<int>(Pos)-5, 9, 8, 6);
00134               if (fpo.contains(m->pos()))
00135               {
00136                      RulerCode = rc_rightMargin;
00137                      return;
00138               }
00139               if (TabValues.count() != 0)
00140               {
00141                      for (int yg = 0; yg < static_cast<int>(TabValues.count()); yg++)
00142                      {
00143                             Pos = (ItemPos-offs+TabValues[yg].tabPosition+(ColWidth+ColGap)*(ActCol-1)+Extra+lineCorr)*Scaling;
00144                             fpo = QRect(static_cast<int>(Pos)-3, 7, 8, 8);
00145                             if (fpo.contains(m->pos()))
00146                             {
00147                                    RulerCode = rc_tab;
00148                                    ActTab = yg;
00149                                    break;
00150                             }
00151                      }
00152               }
00153               if ((RulerCode == rc_none) && (ActCol != 0) && (m->button() == LeftButton))
00154               {
00155                      double Pos = (ItemPos-offs+Extra+lineCorr)*Scaling;
00156                      int newY = m->x() - static_cast<int>(Pos);
00157                      ParagraphStyle::TabRecord tb;
00158                      tb.tabPosition = newY / Scaling;
00159                      tb.tabType = 0;
00160                      tb.tabFillChar = currDoc->toolSettings.tabFillChar[0];
00161                      TabValues.prepend(tb);
00162                      ActTab = 0;
00163                      RulerCode = rc_tab;
00164                      UpdateTabList();
00165                      qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00166                      emit DocChanged(false);
00167               }
00168        }
00169        else
00170        {
00171               if (prefsManager->appPrefs.guidesSettings.guidesShown)
00172               {
00173                      QPoint py = currView->viewport()->mapFromGlobal(m->globalPos());
00174                      currView->DrHY = py.y();
00175                      qApp->setOverrideCursor(QCursor(SPLITHC), true);
00176               }
00177        }
00178 }
00179 
00180 void Hruler::mouseReleaseEvent(QMouseEvent *m)
00181 {
00182        if (currDoc->isLoading())
00183        {
00184               Mpressed = false;
00185               return;
00186        }
00187        if (ItemPosValid)
00188        {
00189               if ((m->y() < height()) && (m->y() > 0))
00190               {
00191                      double ColWidth = (ItemEndPos - ItemPos - (ColGap * (Cols - 1)) - Extra - RExtra - 2*lineCorr) / Cols;
00192                      switch (RulerCode)
00193                      {
00194                             case rc_leftFrameDist:
00195                                    currDoc->m_Selection->itemAt(0)->setTextToFrameDistLeft(Extra);
00196                                    emit DocChanged(false);
00197                                    break;
00198                             case rc_rightFrameDist:
00199                                    currDoc->m_Selection->itemAt(0)->setTextToFrameDistRight(RExtra);
00200                                    emit DocChanged(false);
00201                                    break;
00202                             case rc_indentFirst:
00203                                    currItem->changeCurrentStyle().setFirstIndent(First);
00204                                    emit DocChanged(false);
00205                                    break;
00206                             case rc_leftMargin:
00207                                    currItem->changeCurrentStyle().setLeftMargin(Indent);
00208                                    currItem->changeCurrentStyle().setFirstIndent(First);
00209                                    emit DocChanged(false);
00210                                    break;
00211                             case rc_rightMargin:
00212                                    currItem->changeCurrentStyle().setRightMargin(ColWidth - RMargin);
00213                                    emit DocChanged(false);
00214                                    break;
00215                             case rc_tab:
00216                                    if (m->button() == RightButton)
00217                                    {
00218                                           TabValues[ActTab].tabType += 1;
00219                                           if (TabValues[ActTab].tabType > 4)
00220                                                  TabValues[ActTab].tabType = 0;
00221                                    }
00222                                    currItem->changeCurrentStyle().setTabValues(TabValues);
00223                                    emit DocChanged(false);
00224                                    break;
00225                             default:
00226                                    break;
00227                      }
00228               }
00229               else
00230               {
00231                      if (RulerCode == rc_tab)
00232                      {
00233                             QValueList<ParagraphStyle::TabRecord>::Iterator it;
00234                             it = TabValues.at(ActTab);
00235                             TabValues.remove(it);
00236                             ActTab = 0;
00237                             currItem->changeCurrentStyle().setTabValues(TabValues);
00238                             emit DocChanged(false);
00239                             qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00240                      }
00241               }
00242               RulerCode = rc_none;
00243               currView->DrawNew();
00244               currDoc->m_Selection->itemAt(0)->emitAllToGUI();
00245        }
00246        else
00247        {
00248               if ((Mpressed) && (m->pos().y() > height()))
00249               {
00250                      currView->DrHY = -1;
00251                      currView->SetYGuide(m, -1);
00252               }
00253               qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00254               emit DocChanged(false);
00255               currView->updateContents();
00256        }
00257        Mpressed = false;
00258 }
00259 
00260 void Hruler::mouseMoveEvent(QMouseEvent *m)
00261 {
00262        if (currDoc->isLoading())
00263               return;
00264        if (ItemPosValid)
00265        {
00266               double ColWidth = (ItemEndPos - ItemPos - (ColGap * (Cols - 1)) - Extra - RExtra - 2*lineCorr) / Cols;
00267               int ColEnd, ColStart;
00268               double oldInd;
00269               if (RulerCode == rc_leftFrameDist || RulerCode == rc_rightFrameDist)
00270               {
00271                      ColStart =static_cast<int>((ItemPos-offs+lineCorr)*Scaling);
00272                      ColEnd = static_cast<int>((ItemEndPos-offs-lineCorr)*Scaling);
00273               }
00274               else
00275               {
00276                      ColStart  = static_cast<int>((ItemPos-offs+(ColWidth+ColGap)*(ActCol-1)+Extra+lineCorr)*Scaling);
00277                      ColEnd = static_cast<int>((ItemPos-offs+(ColWidth+ColGap)*(ActCol-1)+ColWidth+Extra+lineCorr)*Scaling);
00278               }
00279               if ((Mpressed) && (m->y() < height()) && (m->y() > 0) && (m->x() > ColStart) && (m->x() < ColEnd))
00280               {
00281                      qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00282                      double toplimit = ItemEndPos-ItemPos-2*lineCorr-Extra - (ColGap * (Cols - 1))-1;
00283                      double toplimit2 = ItemEndPos-ItemPos-2*lineCorr-RExtra - (ColGap * (Cols - 1))-1;
00284                      switch (RulerCode)
00285                      {
00286                             case rc_leftFrameDist:
00287                                    Extra -= (MouseX - m->x()) / Scaling;
00288                                    if (Extra < 0)
00289                                           Extra = 0;
00290                                    if (Extra > toplimit2)
00291                                           Extra = toplimit2;
00292                                    emit MarkerMoved(Extra, 0);
00293                                    repaint();
00294                                    break;
00295                             case rc_rightFrameDist:
00296                                    RExtra += (MouseX - m->x()) / Scaling;
00297                                    if (RExtra < 0)
00298                                           RExtra = 0;
00299                                    if (RExtra > toplimit)
00300                                           RExtra = toplimit;
00301                                    emit MarkerMoved(RExtra, 0);
00302                                    repaint();
00303                                    break;
00304                             case rc_indentFirst:
00305                                    First -= (MouseX - m->x()) / Scaling;
00306                                    if ((ItemPos-offs+(ColWidth+ColGap)*ActCol+First+Indent)*Scaling < (ItemPos-offs+(ColWidth+ColGap)*ActCol)*Scaling)
00307                                           First += (MouseX - m->x()) / Scaling;
00308                                    if (First+Indent > ColWidth)
00309                                           First  = ColWidth-Indent;
00310                                    emit MarkerMoved(First, 0);
00311                                    repaint();
00312                                    break;
00313                             case rc_leftMargin:
00314                                    oldInd = Indent+First;
00315                                    Indent -= (MouseX - m->x()) / Scaling;
00316                                    if (Indent < 0)
00317                                           Indent = 0;
00318                                    if (Indent > ColWidth-1)
00319                                           Indent  = ColWidth-1;
00320                                    First = oldInd - Indent;
00321                                    emit MarkerMoved(Indent, 0);
00322                                    repaint();
00323                                    break;
00324                             case rc_rightMargin:
00325                                    RMargin -= (MouseX - m->x()) / Scaling;
00326                                    if (RMargin < 0)
00327                                           RMargin = 0;
00328                                    if (RMargin > ColWidth-1)
00329                                           RMargin  = ColWidth-1;
00330                                    emit MarkerMoved(RMargin, 0);
00331                                    repaint();
00332                                    break;
00333                             case rc_tab:
00334                                    TabValues[ActTab].tabPosition -= (MouseX - m->x()) / Scaling;
00335                                    if (TabValues[ActTab].tabPosition < 0)
00336                                           TabValues[ActTab].tabPosition = 0;
00337                                    if (TabValues[ActTab].tabPosition > ColWidth-1)
00338                                           TabValues[ActTab].tabPosition  = ColWidth-1;
00339                                    emit MarkerMoved(TabValues[ActTab].tabPosition, 0);
00340                                    UpdateTabList();
00341                                    repaint();
00342                                    break;
00343                             default:
00344                                    break;
00345                      }
00346                      MouseX = m->x();
00347                      if (RulerCode != rc_none)
00348                      {
00349                             QPoint py = currView->viewport()->mapFromGlobal(m->globalPos());
00350                             QPainter p;
00351                             p.begin(currView->viewport());
00352                             p.setRasterOp(XorROP);
00353                             p.setPen(QPen(white, 1, DotLine, FlatCap, MiterJoin));
00354                             QPoint out = currView->contentsToViewport(QPoint(0, qRound(currDoc->currentPage()->yOffset() * Scaling)));
00355                             p.drawLine(Markp, out.y(), Markp, out.y()+qRound(currDoc->currentPage()->height() * Scaling));
00356                             p.drawLine(py.x(), out.y(), py.x(), out.y()+qRound(currDoc->currentPage()->height() * Scaling));
00357                             p.end();
00358                             Markp = py.x();
00359                      }
00360                      return;
00361               }
00362               if ((!Mpressed) && (m->y() < height()) && (m->y() > 0) && (m->x() > ColStart) && (m->x() < ColEnd))
00363               {
00364                      qApp->setOverrideCursor(QCursor(loadIcon("tab.png"), 3), true);
00365                      double Pos = (ItemPos-offs+Extra+lineCorr)*Scaling;
00366                      if ((static_cast<int>(Pos) < (m->x()+currDoc->guidesSettings.grabRad)) && (static_cast<int>(Pos) > (m->x()-currDoc->guidesSettings.grabRad)))
00367                             qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00368                      Pos = (ItemEndPos-offs-RExtra-lineCorr)*Scaling;
00369                      if ((static_cast<int>(Pos) < (m->x()+currDoc->guidesSettings.grabRad)) && (static_cast<int>(Pos) > (m->x()-currDoc->guidesSettings.grabRad)))
00370                             qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00371                      QRect fpo;
00372                      double ColWidth = (ItemEndPos - ItemPos - (ColGap * (Cols - 1)) - Extra - RExtra - 2*lineCorr) / Cols;
00373 //                   if (currDoc->currentParaStyle > 4)
00374                      {
00375                             for (int CurrCol = 0; CurrCol < Cols; ++CurrCol)
00376                             {
00377                                    Pos = (ItemPos-offs+First+Indent+(ColWidth+ColGap)*CurrCol+Extra+lineCorr)*Scaling;
00378                                    fpo = QRect(static_cast<int>(Pos)-3, topline, 6, 6);
00379                                    if (fpo.contains(m->pos()))
00380                                    {
00381                                           qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00382                                           break;
00383                                    }
00384                                    Pos = (ItemPos-offs+Indent+(ColWidth+ColGap)*CurrCol+Extra+lineCorr)*Scaling;
00385                                    fpo = QRect(static_cast<int>(Pos)-3, 9, 6, 6);
00386                                    if (fpo.contains(m->pos()))
00387                                    {
00388                                           qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00389                                           break;
00390                                    }
00391                             }
00392                      }
00393                      if (TabValues.count() != 0)
00394                      {
00395                             for (int CurrCol = 0; CurrCol < Cols; ++CurrCol)
00396                             {
00397                                    for (int yg = 0; yg < static_cast<int>(TabValues.count()); yg++)
00398                                    {
00399                                           Pos = (ItemPos-offs+TabValues[yg].tabPosition+(ColWidth+ColGap)*CurrCol+Extra+lineCorr)*Scaling;
00400                                           fpo = QRect(static_cast<int>(Pos)-3, 7, 8, 8);
00401                                           if (fpo.contains(m->pos()))
00402                                           {
00403                                                  qApp->setOverrideCursor(QCursor(SizeHorCursor), true);
00404                                                  break;
00405                                           }
00406                                    }
00407                             }
00408                      }
00409                      emit MarkerMoved((m->x() - static_cast<int>((ItemPos-offs+Extra+lineCorr)*Scaling)) / Scaling, 0);
00410                      return;
00411               }
00412               if ((Mpressed) && (RulerCode == rc_tab) && ((m->y() > height()) || (m->y() < 0)))
00413               {
00414                      qApp->setOverrideCursor(QCursor(loadIcon("DelPoint.png"), 1, 1), true);
00415                      return;
00416               }
00417               qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00418        }
00419        else
00420        {
00421               if ((Mpressed) && (m->pos().y() > height()))
00422                      currView->FromHRuler(m);
00423        }
00424 }
00425 
00426 void Hruler::paintEvent(QPaintEvent *e)
00427 {
00428        if (currDoc->isLoading())
00429               return;
00430        QString tx = "";
00431        double xl, frac;
00432        double sc = currView->scale();
00433        Scaling = sc;
00434        QFont ff = font();
00435        ff.setPointSize(8);
00436        setFont(ff);
00437        QPainter p;
00438        p.begin(this);
00439        p.setClipRect(e->rect());
00440        p.drawLine(0, 16, width(), 16);
00441        p.setBrush(black);
00442        p.setPen(black);
00443        p.setFont(font());
00444        double cc = width() / sc;
00445        double firstMark = ceil(offs / iter) * iter - offs;
00446        while (firstMark < cc)
00447        {
00448               p.drawLine(qRound(firstMark * sc), 10, qRound(firstMark * sc), 16);
00449               firstMark += iter;
00450        }
00451        firstMark = ceil(offs / iter2) * iter2 - offs;
00452        int markC = static_cast<int>(ceil(offs / iter2));
00453        while (firstMark < cc)
00454        {
00455               p.drawLine(qRound(firstMark * sc), topline, qRound(firstMark * sc), 16);
00456               switch (currDoc->unitIndex())
00457               {
00458                      case 1:
00459                             tx = QString::number(markC * iter2 / (iter2 / 100) / cor);
00460                             break;
00461                      case 2:
00462                             xl = (markC * iter2 / iter2) / cor;
00463                             tx = QString::number(static_cast<int>(xl));
00464                             frac = fabs(xl - static_cast<int>(xl));
00465                             if ((static_cast<int>(xl) == 0) && (frac > 0.1))
00466                                    tx = "";
00467                             if ((frac > 0.24) && (frac < 0.26))
00468                                    tx += QChar(0xBC);
00469                             if ((frac > 0.49) && (frac < 0.51))
00470                                    tx += QChar(0xBD);
00471                             if ((frac > 0.74) && (frac < 0.76))
00472                                    tx += QChar(0xBE);
00473                             tx = tx;
00474                             break;
00475                      case 3:
00476                      case 5:
00477                             tx = QString::number(markC * iter2 / (iter2 / 10) / cor);
00478                             break;
00479                      case 4:
00480                             tx = QString::number(markC * iter2 / iter2 / cor);
00481                             break;
00482                      default:
00483                             tx = QString::number(markC * iter2);
00484                             break;
00485               }
00486               drawNumber(tx, qRound(firstMark * sc) + 2, 9, p);
00487               //p.drawText(qRound(firstMark * sc)+2, 9, tx);
00488               firstMark += iter2;
00489               markC++;
00490        }
00491        if (ItemPosValid)
00492        {
00493               double Pos = ItemPos-offs;
00494               double EndPos = ItemEndPos-offs;
00495               p.eraseRect(QRect(QPoint(qRound((Pos+Extra)*sc), 1), QPoint(qRound((EndPos-RExtra)*sc), 15)));
00496               p.drawLine(qRound((Pos+Extra)*sc), 16, qRound((EndPos-RExtra)*sc), 16);
00497               p.save();
00498               if (Revers)
00499               {
00500                      p.translate(qRound((Pos)*sc), 0);
00501                      p.scale(-1, 1);
00502                      p.translate(qRound((Pos+Extra)*sc-(EndPos-RExtra)*sc), 0);
00503                      p.translate(-qRound(Pos*sc), 0);
00504               }
00505               for (int CurrCol = 0; CurrCol < Cols; ++CurrCol)
00506               {
00507                      double ColWidth = (ItemEndPos - ItemPos - (ColGap * (Cols - 1)) - Extra - RExtra - 2*lineCorr) / Cols;
00508                      Pos = ItemPos-offs + (ColWidth + ColGap) * CurrCol+Extra + lineCorr;
00509                      EndPos = Pos+ColWidth;
00510                      p.setPen(QPen(blue, 1, SolidLine, FlatCap, MiterJoin));
00511                      for (xl = Pos; xl < EndPos; xl += iter)
00512                             p.drawLine(qRound(xl*sc), 10, qRound(xl*sc), 16);
00513                      for (xl = Pos; xl < EndPos; xl += iter2)
00514                      {
00515                             p.drawLine(qRound(xl*sc), topline, qRound(xl*sc), 16);
00516                             switch (currDoc->unitIndex())
00517                             {
00518                                    case 2:
00519                                    {
00520                                           QString tx = "";
00521                                           int num1 = static_cast<int>((xl-Pos) / iter2 / cor);
00522                                           if (num1 != 0)
00523                                                  tx = QString::number(num1);
00524                                           double frac = (xl / iter2 / cor) - num1;
00525                                           if ((frac > 0.24) && (frac < 0.26))
00526                                                  tx += QChar(0xBC);
00527                                           if ((frac > 0.49) && (frac < 0.51))
00528                                                  tx += QChar(0xBD);
00529                                           if ((frac > 0.74) && (frac < 0.76))
00530                                                  tx += QChar(0xBE);
00531                                           if (Revers)
00532                                           {
00533                                                  p.save();
00534                                                  p.translate(qRound((xl-2/sc) * sc),0);
00535                                                  p.scale(-1,1);
00536                                                  drawNumber(tx, 0, 17, p);
00537                                                  //p.drawText(0, 17, tx);
00538                                                  p.restore();
00539                                           }
00540                                           else
00541                                                  drawNumber(tx, qRound((xl+2/sc) * sc), 9, p);
00542                                                  //p.drawText(qRound((xl+2/sc) * sc), 9, tx);
00543                                           break;
00544                                    }
00545                                    case 3:
00546                                           if (Revers)
00547                                           {
00548                                                  p.save();
00549                                                  p.translate(qRound((xl-2/sc) * sc),0);
00550                                                  p.scale(-1,1);
00551                                                  drawNumber(QString::number((xl-Pos) / iter / cor), 0, 17, p);
00552                                                  //p.drawText(0, 17, QString::number((xl-Pos) / iter / cor));
00553                                                  p.restore();
00554                                           }
00555                                           else
00556                                                  drawNumber(QString::number((xl-Pos) / iter / cor), qRound((xl+2/sc) * sc), 9, p);
00557                                                  //p.drawText(qRound((xl+2/sc) * sc), 9, QString::number((xl-Pos) / iter / cor));
00558                                           break;
00559                                    case 4:
00560                                           if (Revers)
00561                                           {
00562                                                  p.save();
00563                                                  p.translate(qRound((xl-2/sc) * sc),0);
00564                                                  p.scale(-1,1);
00565                                                  drawNumber(QString::number((xl-Pos) / iter / 10 / cor), 0, 9, p);
00566                                                  //p.drawText(0, 9, QString::number((xl-Pos) / iter / 10 / cor));
00567                                                  p.restore();
00568                                           }
00569                                           else
00570                                                  drawNumber(QString::number((xl-Pos) / iter / 10 / cor), qRound((xl+2/sc) * sc), 9, p);
00571                                                  //p.drawText(qRound((xl+2/sc) * sc), 9, QString::number((xl-Pos) / iter / 10 / cor));
00572                                           break;
00573                                    case 5:
00574                                           if (Revers)
00575                                           {
00576                                                  p.save();
00577                                                  p.translate(qRound((xl-2/sc) * sc),0);
00578                                                  p.scale(-1,1);
00579                                                  drawNumber(QString::number((xl-Pos) / iter  / cor), 0, 9, p);
00580                                                  //p.drawText(0, 9, QString::number((xl-Pos) / iter  / cor));
00581                                                  p.restore();
00582                                           }
00583                                           else
00584                                                  drawNumber(QString::number((xl-Pos) / iter  / cor), qRound((xl+2/sc) * sc), 9, p);
00585                                                  //p.drawText(qRound((xl+2/sc) * sc), 9, QString::number((xl-Pos) / iter  / cor));
00586                                           break;
00587                                    default:
00588                                           if (Revers)
00589                                           {
00590                                                  p.save();
00591                                                  p.translate(qRound((xl-2/sc) * sc),0);
00592                                                  p.scale(-1,1);
00593                                                  drawNumber(QString::number((xl-Pos) / iter * 10 / cor), 0, 9, p);
00594                                                  //p.drawText(0, 9, QString::number((xl-Pos) / iter * 10 / cor));
00595                                                  p.restore();
00596                                           }
00597                                           else
00598                                                  drawNumber(QString::number((xl-Pos) / iter * 10 / cor), qRound((xl+2/sc) * sc), 9, p);
00599                                                  //p.drawText(qRound((xl+2/sc) * sc), 9, QString::number((xl-Pos) / iter * 10 / cor));
00600                                           break;
00601                             }
00602                      }
00603                      p.setPen(QPen(blue, 2, SolidLine, FlatCap, MiterJoin));
00604                      p.drawLine(qRound(Pos*sc), topline, qRound(Pos*sc), 15);
00605                      if (CurrCol == 0)
00606                      {
00607                             p.drawLine(qRound(Pos*sc), 15, qRound((Pos+4/sc)*sc), 15);
00608                             p.drawLine(qRound(Pos*sc), topline, qRound((Pos+4/sc)*sc), topline);
00609                      }
00610 //                   if (currDoc->currentParaStyle > 4)
00611                      {
00612                             p.setPen(QPen(blue, 1, SolidLine, FlatCap, MiterJoin));
00613                             double fpos = Pos+First+Indent;
00614                             QPointArray cr;
00615                             cr.setPoints(3, qRound(fpos*sc), 9, qRound((fpos+3/sc)*sc), topline, qRound((fpos-3/sc)*sc), topline);
00616                             p.drawPolygon(cr);
00617                             QPointArray cr2;
00618                             cr2.setPoints(3, qRound((Pos+Indent)*sc), 9, qRound((Pos+Indent+3/sc)*sc), 15, qRound((Pos+Indent-3/sc)*sc), 15);
00619                             p.drawPolygon(cr2);
00620                             QPointArray cr3;
00621                             cr3.setPoints(3, qRound((Pos+RMargin)*sc), topline, qRound((Pos+RMargin)*sc), 15, qRound((Pos+RMargin-3/sc)*sc), 9);
00622                             p.drawPolygon(cr3);
00623                      }
00624                      p.setPen(QPen(blue, 2, SolidLine, FlatCap, MiterJoin));
00625                      if (TabValues.count() != 0)
00626                      {
00627                             p.setPen(QPen(black, 2, SolidLine, FlatCap, MiterJoin));
00628                             for (int yg = 0; yg < static_cast<int>(TabValues.count()); yg++)
00629                             {
00630                                    if (Pos+TabValues[yg].tabPosition < EndPos)
00631                                    {
00632                                           switch (static_cast<int>(TabValues[yg].tabType))
00633                                           {
00634                                                  case 0:
00635                                                         if (Revers)
00636                                                         {
00637                                                                p.save();
00638                                                                p.translate(qRound((Pos+TabValues[yg].tabPosition)*sc),0);
00639                                                                p.scale(-1,1);
00640                                                                p.drawLine(0, 7, 0, 15);
00641                                                                p.drawLine(0, 15, 8, 15);
00642                                                                p.restore();
00643                                                         }
00644                                                         else
00645                                                         {
00646                                                                p.drawLine(qRound((Pos+TabValues[yg].tabPosition)*sc), 7, qRound((Pos+TabValues[yg].tabPosition)*sc), 15);
00647                                                                p.drawLine(qRound((Pos+TabValues[yg].tabPosition)*sc), 15, qRound((Pos+TabValues[yg].tabPosition+8/sc)*sc), 15);
00648                                                         }
00649                                                         break;
00650                                                  case 1:
00651                                                         if (Revers)
00652                                                         {
00653                                                                p.save();
00654                                                                p.translate(qRound((Pos+TabValues[yg].tabPosition)*sc),0);
00655                                                                p.scale(-1,1);
00656                                                                p.drawLine(0, 7, 0, 15);
00657                                                                p.drawLine(0, 15, -8, 15);
00658                                                                p.restore();
00659                                                         }
00660                                                         else
00661                                                         {
00662                                                                p.drawLine(qRound((Pos+TabValues[yg].tabPosition)*sc), 7, qRound((Pos+TabValues[yg].tabPosition)*sc), 15);
00663                                                                p.drawLine(qRound((Pos+TabValues[yg].tabPosition)*sc), 15, qRound((Pos+TabValues[yg].tabPosition-8/sc)*sc), 15);
00664                                                         }
00665                                                         break;
00666                                                  case 2:
00667                                                  case 3:
00668                                                         p.drawLine(qRound((Pos+TabValues[yg].tabPosition)*sc), 7, qRound((Pos+TabValues[yg].tabPosition)*sc), 15);
00669                                                         p.drawLine(qRound((Pos+TabValues[yg].tabPosition-4/sc)*sc), 15, qRound((Pos+TabValues[yg].tabPosition+4/sc)*sc), 15);
00670                                                         p.drawLine(qRound((Pos+TabValues[yg].tabPosition+3/sc)*sc), 12, qRound((Pos+TabValues[yg].tabPosition+2/sc)*sc), 12);
00671                                                         break;
00672                                                  case 4:
00673                                                         p.drawLine(qRound((Pos+TabValues[yg].tabPosition)*sc), 7, qRound((Pos+TabValues[yg].tabPosition)*sc), 15);
00674                                                         p.drawLine(qRound((Pos+TabValues[yg].tabPosition-4/sc)*sc), 15, qRound((Pos+TabValues[yg].tabPosition+4/sc)*sc), 15);
00675                                                         break;
00676                                                  default:
00677                                                         break;
00678                                           }
00679                                    }
00680                             }
00681                      }
00682                      p.setPen(QPen(blue, 2, SolidLine, FlatCap, MiterJoin));
00683                      p.drawLine(qRound(EndPos*sc), topline, qRound(EndPos*sc), 15);
00684                      if (CurrCol == Cols-1)
00685                      {
00686                             p.drawLine(qRound(EndPos*sc), 15, qRound((EndPos-4/sc)*sc), 15);
00687                             p.drawLine(qRound(EndPos*sc), topline, qRound((EndPos-4/sc)*sc), topline);
00688                      }
00689               }
00690               p.restore();
00691        }
00692        p.end();
00693 }
00694 
00695 
00696 void Hruler::drawNumber(QString txt, int x, int y0, QPainter & p)
00697 {
00698        const int y = y0 - 3 + topline;
00699 #ifndef QT_MAC
00700        p.drawText(x,y,txt);
00701 #else
00702        static const int SCALE = 16;
00703        QFontMetrics fm = p.fontMetrics();
00704        QRect bbox = fm.boundingRect(txt);
00705        static QPixmap pix;
00706        if (pix.width() < bbox.width()*SCALE || pix.height() < bbox.height()*SCALE)
00707               pix.resize(bbox.width()*SCALE, bbox.height()*SCALE);
00708        QFont fnt = p.font();
00709        QPainter p2;
00710        pix.fill();
00711        p2.begin( &pix );
00712        if (fnt.pointSize() > 0)
00713               fnt.setPointSize(SCALE*fnt.pointSize()-SCALE/2);
00714        else if (fnt.pixelSize() > 0)
00715               fnt.setPixelSize(SCALE*fnt.pixelSize()-SCALE/2);
00716        else
00717               fnt.setPixelSize(SCALE);
00718        p2.setFont(fnt);
00719        p2.drawText(-bbox.x()*SCALE, -bbox.y()*SCALE, txt);
00720        p2.end();
00721        p.scale(1.0/SCALE,1.0/SCALE);
00722        p.drawPixmap(x*SCALE, (y+bbox.top())*SCALE, pix, 0, 0, bbox.width()*SCALE, bbox.height()*SCALE);
00723        p.scale(SCALE,SCALE);
00724 #endif
00725 }
00726 
00727 void Hruler::Draw(int where)
00728 {
00729        // erase old marker
00730        int currentCoor = where - currView->contentsX();
00731        repaint(oldMark-3, 0, 7, 17);
00732        QPointArray cr;
00733        QPainter p;
00734 #ifdef OPTION_SMOOTH_MARKERS
00735        // draw new marker to pixmap
00736        static const int SCALE = 16;
00737        static const QColor BACKGROUND(255, 255, 255);
00738        static QPixmap pix( 4*SCALE, 16*SCALE );
00739        static bool initpix = true;
00740        if (initpix) {
00741               initpix = false;
00742               p.begin( &pix );
00743               p.setBrush( BACKGROUND );
00744               p.drawRect( 0, 0, 4*SCALE, 16*SCALE );
00745 
00746               p.setPen(red);
00747               p.setBrush(red);
00748               cr.setPoints(3, 2*SCALE, 16*SCALE, 4*SCALE, 0, 0, 0);
00749               p.drawPolygon(cr);
00750               p.end();
00751        }
00752        // draw pixmap
00753        p.begin(this);
00754        p.translate(-currView->contentsX(), 0);
00755        p.scale(1.0/SCALE, 1.0/(SCALE+1));
00756        p.drawPixmap((where-2)*SCALE, 1, pix);
00757        p.end();
00758        // restore marks
00759        p.begin(this);
00760        p.setBrush(black);
00761        p.setPen(black);
00762        p.setFont(font());
00763        double sc = currView->getScale();
00764        double cc = width() / sc;
00765        double firstMark = ceil(offs / iter) * iter - offs;
00766        while (firstMark < cc)
00767        {
00768               p.drawLine(qRound(firstMark * sc), 10, qRound(firstMark * sc), 16);
00769               firstMark += iter;
00770        }
00771        p.end();
00772 #else
00773        // draw slim marker
00774        p.begin(this);
00775        p.translate(-currView->contentsX(), 0);
00776        p.setPen(red);
00777        p.setBrush(red);
00778        cr.setPoints(5,  where, 5, where, 16, where, 5, where+2, 0, where-2, 0);
00779        p.drawPolygon(cr);
00780        p.end();
00781 #endif
00782        oldMark = currentCoor;
00783 }
00784 
00785 void Hruler::setItem(PageItem * item)
00786 {
00787        currItem = item;
00788        ItemPos = item->xPos();
00789        ItemEndPos = ItemPos + item->width();
00790        if (currDoc->guidesSettings.rulerMode)
00791        {
00792               ItemPos -= currDoc->currentPage()->xOffset();
00793               ItemEndPos -= currDoc->currentPage()->xOffset();
00794        }
00795 }
00796 
00797 void Hruler::UpdateTabList()
00798 {
00799        ParagraphStyle::TabRecord tb;
00800        tb.tabPosition = TabValues[ActTab].tabPosition;
00801        tb.tabType = TabValues[ActTab].tabType;
00802        tb.tabFillChar =  TabValues[ActTab].tabFillChar;
00803        QValueList<ParagraphStyle::TabRecord>::Iterator it;
00804        int gg = static_cast<int>(TabValues.count());
00805        int g = gg;
00806        it = TabValues.at(ActTab);
00807        TabValues.remove(it);
00808        for (int yg = static_cast<int>(TabValues.count()-1); yg > -1; yg--)
00809        {
00810               if (tb.tabPosition < TabValues[yg].tabPosition)
00811                      g = yg;
00812        }
00813        ActTab = g;
00814        if (gg == g)
00815        {
00816               TabValues.append(tb);
00817               ActTab = static_cast<int>(TabValues.count()-1);
00818        }
00819        else
00820        {
00821               it = TabValues.at(ActTab);
00822               TabValues.insert(it, tb);
00823        }
00824 }
00825 
00826 void Hruler::unitChange()
00827 {
00828        double sc = currView->scale();
00829        cor=1;
00830        int docUnitIndex=currDoc->unitIndex();
00831        switch (docUnitIndex)
00832        {
00833               case 0:
00834                      if (sc > 1 && sc <= 4)
00835                             cor = 2;
00836                      if (sc > 4)
00837                             cor = 10;
00838                      if (sc < 0.3)
00839                      {
00840                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 3;
00841                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 3;
00842                      }
00843                      else if (sc < 0.5)
00844                      {
00845                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 2;
00846                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 2;
00847                      }
00848                      else
00849                      {
00850                             iter = unitRulerGetIter1FromIndex(docUnitIndex) / cor;
00851                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) / cor;
00852                      }
00853                      break;
00854               case 1:
00855                      if (sc > 1)
00856                             cor = 10;
00857                      iter = unitRulerGetIter1FromIndex(docUnitIndex) / cor;
00858                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex) / cor;
00859                      break;
00860               case 2:
00861                      iter = unitRulerGetIter1FromIndex(docUnitIndex);
00862                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex);
00863                      if (sc > 1 && sc <= 4)
00864                      {
00865                             cor = 2;
00866                             iter /= cor;
00867                             iter2 /= cor;
00868                      }
00869                      if (sc > 4)
00870                      {
00871                             cor = 4;
00872                             iter /= cor;
00873                             iter2 /= cor;
00874                      }
00875                      break;
00876               case 3:
00877                      iter = unitRulerGetIter1FromIndex(docUnitIndex);
00878                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex);
00879                      if (sc > 1 && sc <= 4)
00880                      {
00881                             cor = 1;
00882                             iter = 12.0;
00883                             iter2 = 60.0;
00884                      }
00885                      if (sc > 4)
00886                      {
00887                             cor = 2;
00888                             iter = 6.0;
00889                             iter2 = 12.0;
00890                      }
00891                      break;
00892               case 4:
00893                      if (sc > 1 && sc <= 4)
00894                             cor = 1;
00895                      if (sc > 4)
00896                             cor = 10;
00897                      if (sc < 0.3)
00898                      {
00899                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 4;
00900                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 4;
00901                      }
00902                      else if (sc < 0.6)
00903                      {
00904                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 3;
00905                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 3;
00906                      }
00907                      else
00908                      {
00909                             iter = unitRulerGetIter1FromIndex(docUnitIndex) / cor;
00910                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) / cor;
00911                      }
00912                      break;
00913               case 5:
00914                      iter = unitRulerGetIter1FromIndex(docUnitIndex);
00915                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex);
00916                      if (sc > 1 && sc <= 4)
00917                      {
00918                             cor = 1;
00919                             iter = 72.0/25.4*4.512;
00920                             iter2 = 72.0/25.4*4.512*5.0;
00921                      }
00922                      if (sc > 4)
00923                      {
00924                             cor = 2;
00925                             iter = 72.0/25.4*4.512/2.0;
00926                             iter2 = 72.0/25.4*4.512;
00927                      }
00928                      break;
00929               default:
00930                      if (sc > 1 && sc <= 4)
00931                             cor = 2;
00932                      if (sc > 4)
00933                             cor = 10;
00934                      iter = unitRulerGetIter1FromIndex(0) / cor;
00935                      iter2 = unitRulerGetIter2FromIndex(0) / cor;
00936                      break;
00937        }
00938 }