Back to index

scribus-ng  1.3.4.dfsg+svn20071115
vruler.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                           vruler.cpp  -  description
00009                              -------------------
00010     begin                : Wed Apr 11 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 "vruler.h"
00025 #include "vruler.moc"
00026 #include "page.h"
00027 #include <qcursor.h>
00028 #include <qcolor.h>
00029 #include <qrect.h>
00030 #include <qpointarray.h>
00031 #include "scribus.h"
00032 #include "scribusdoc.h"
00033 #include "units.h"
00034 #include "prefsmanager.h"
00035 
00036 #if QT_VERSION  > 0x030102
00037        #define SPLITVC SplitHCursor
00038 #else
00039        #define SPLITVC SplitVCursor
00040 #endif
00041 
00042 
00043 
00044 Vruler::Vruler(ScribusView *pa, ScribusDoc *doc) : QWidget(pa)
00045 {
00046        prefsManager=PrefsManager::instance();
00047        setEraseColor(QColor(255,255,255));
00048        currDoc = doc;
00049        currView = pa;
00050        offs = 0;
00051        oldMark = 0;
00052        Mpressed = false;
00053        unitChange();
00054 }
00055 
00056 void Vruler::mousePressEvent(QMouseEvent *m)
00057 {
00058        Mpressed = true;
00059        if (prefsManager->appPrefs.guidesSettings.guidesShown)
00060        {
00061               QPoint py = currView->viewport()->mapFromGlobal(m->globalPos());
00062               currView->DrVX = py.x();
00063               qApp->setOverrideCursor(QCursor(SPLITVC), true);
00064        }
00065 }
00066 
00067 void Vruler::mouseReleaseEvent(QMouseEvent *m)
00068 {
00069        if ((Mpressed) && (m->pos().x() > width()))
00070        {
00071               currView->DrVX = -1;
00072               currView->SetXGuide(m, -1);
00073        }
00074        qApp->setOverrideCursor(QCursor(ArrowCursor), true);
00075        Mpressed = false;
00076        currView->updateContents();
00077 }
00078 
00079 void Vruler::mouseMoveEvent(QMouseEvent *m)
00080 {
00081        if ((Mpressed) && (m->pos().x() > width()))
00082               currView->FromVRuler(m);
00083 }
00084 
00085 void Vruler::paintEvent(QPaintEvent *e)
00086 {
00087        if (currDoc->isLoading())
00088               return;
00089        QString tx = "";
00090        double xl, frac;
00091        double sc = currView->scale();
00092        QFont ff = font();
00093        ff.setPointSize(8);
00094        setFont(ff);
00095        QPainter p;
00096        p.begin(this);
00097        p.setClipRect(e->rect());
00098        p.drawLine(16, 0, 16, height());
00099        p.setBrush(black);
00100        p.setPen(black);
00101        p.setFont(font());
00102        double cc = height() / sc;
00103        double firstMark = ceil(offs / iter) * iter - offs;
00104        while (firstMark < cc)
00105        {
00106               p.drawLine(10, qRound(firstMark * sc), 16, qRound(firstMark * sc));
00107               firstMark += iter;
00108        }
00109        firstMark = ceil(offs / iter2) * iter2 - offs;
00110        int markC = static_cast<int>(ceil(offs / iter2));
00111        while (firstMark < cc)
00112        {
00113               p.drawLine(3, qRound(firstMark * sc), 16, qRound(firstMark * sc));
00114               int textY = qRound(firstMark * sc)+10;
00115               switch (currDoc->unitIndex())
00116               {
00117                      case 1:
00118                             tx = QString::number(markC * iter2 / (iter2 / 100) / cor);
00119                             break;
00120                      case 2:
00121                             xl = (markC * iter2 / iter2) / cor;
00122                             tx = QString::number(static_cast<int>(xl));
00123                             frac = fabs(xl - static_cast<int>(xl));
00124                             if ((static_cast<int>(xl) == 0) && (frac > 0.1))
00125                                    tx = "";
00126                             if ((frac > 0.24) && (frac < 0.26))
00127                                    tx += QChar(0xBC);
00128                             if ((frac > 0.49) && (frac < 0.51))
00129                                    tx += QChar(0xBD);
00130                             if ((frac > 0.74) && (frac < 0.76))
00131                                    tx += QChar(0xBE);
00132                             tx = tx;
00133                             break;
00134                      case 3:
00135                      case 5:
00136                             tx = QString::number(markC * iter2 / (iter2 / 10) / cor);
00137                             break;
00138                      case 4:
00139                             tx = QString::number(markC * iter2 / iter2 / cor);
00140                             break;
00141                      default:
00142                             tx = QString::number(markC * iter2);
00143                             break;
00144               }
00145               drawNumber(tx, textY, &p);
00146               firstMark += iter2;
00147               markC++;
00148        }
00149        p.end();
00150 }
00151 
00152 void Vruler::drawNumber(QString num, int starty, QPainter *p)
00153 {
00154        int textY = starty;
00155        for (uint a = 0; a < num.length(); ++a)
00156        {
00157               QString txt = num.mid(a, 1);
00158 #ifndef QT_MAC
00159               p->drawText(1, textY, txt);
00160 #else
00161               static const int SCALE = 16;
00162               QFontMetrics fm = p->fontMetrics();
00163               QRect bbox = fm.boundingRect(txt);
00164               static QPixmap pix;
00165               if (pix.width() < bbox.width()*SCALE || pix.height() < bbox.height()*SCALE)
00166                      pix.resize(bbox.width()*SCALE, bbox.height()*SCALE);
00167               QFont fnt = p->font();
00168               QPainter p2;
00169               pix.fill();
00170               p2.begin( &pix );
00171               if (fnt.pointSize() > 0)
00172                      fnt.setPointSize(SCALE*fnt.pointSize()-SCALE/2);
00173               else if (fnt.pixelSize() > 0)
00174                      fnt.setPixelSize(SCALE*fnt.pixelSize()-SCALE/2);
00175               else
00176                      fnt.setPixelSize(SCALE);
00177               p2.setFont(fnt);
00178               p2.drawText(-bbox.x()*SCALE, -bbox.y()*SCALE, txt);
00179               p2.end();
00180               p->scale(1.0/SCALE,1.0/SCALE);
00181               p->drawPixmap(1*SCALE, (textY+bbox.top())*SCALE, pix, 0, 0, bbox.width()*SCALE, bbox.height()*SCALE);
00182               p->scale(SCALE,SCALE);
00183 #endif
00184               textY += 11;
00185        }
00186 }
00187 
00188 void Vruler::Draw(int where)
00189 {
00190        // erase old marker
00191        int currentCoor = where - currView->contentsY();
00192        repaint(0, oldMark-3, 17, 6);
00193        QPointArray cr;
00194        QPainter p;
00195 #ifdef OPTION_SMOOTH_MARKERS
00196        // draw new marker to pixmap
00197        static const int SCALE = 16;
00198        static const QColor BACKGROUND(255, 255, 255);
00199        static QPixmap pix( 16*SCALE, 4*SCALE );
00200        static bool initpix = true;
00201        if (initpix) {
00202               initpix = false;
00203               p.begin( &pix );
00204               p.setBrush( BACKGROUND );
00205               p.drawRect( 0, 0, 16*SCALE, 4*SCALE );
00206 
00207               p.setPen(red);
00208               p.setBrush(red);
00209               cr.setPoints(3, 16*SCALE, 2*SCALE, 0, 4*SCALE, 0, 0);
00210               p.drawPolygon(cr);
00211               p.end();
00212        }
00213        // draw pixmap
00214        p.begin(this);
00215        p.translate(0, -currView->contentsY());
00216        p.scale(1.0/(SCALE+1), 1.0/SCALE);
00217        p.drawPixmap(0, (where-2)*SCALE, pix);
00218        p.end();
00219        // restore marks
00220        p.begin(this);
00221        p.setBrush(black);
00222        p.setPen(black);
00223        p.setFont(font());
00224        double sc = currView->getScale();
00225        double cc = height() / sc;
00226        double firstMark = ceil(offs / iter) * iter - offs;
00227        while (firstMark < cc)
00228        {
00229               p.drawLine(10, qRound(firstMark * sc), 16, qRound(firstMark * sc));
00230               firstMark += iter;
00231        }
00232        p.end();
00233 #else
00234        // draw slim marker
00235        p.begin(this);
00236        p.translate(0, -currView->contentsY());
00237        p.setPen(red);
00238        p.setBrush(red);
00239        cr.setPoints(5,  5, where, 16, where, 5, where, 0, where+2, 0, where-2);
00240        p.drawPolygon(cr);
00241        p.end();
00242 #endif
00243 
00244        oldMark = currentCoor;
00245 }
00246 
00247 void Vruler::unitChange()
00248 {
00249        double sc = currView->scale();
00250        cor=1;
00251        int docUnitIndex=currDoc->unitIndex();
00252        switch (docUnitIndex)
00253        {
00254               case 0:
00255                      if (sc > 1 && sc <= 4)
00256                             cor = 2;
00257                      if (sc > 4)
00258                             cor = 10;
00259                      if (sc < 0.3)
00260                      {
00261                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 3;
00262                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 3;
00263                      }
00264                      else if (sc < 0.2)
00265                      {
00266                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 2;
00267                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 2;
00268                      }
00269                      else
00270                      {
00271                             iter = unitRulerGetIter1FromIndex(docUnitIndex) / cor;
00272                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) / cor;
00273                      }
00274                      break;
00275               case 1:
00276                      if (sc > 1)
00277                             cor = 10;
00278                      iter = unitRulerGetIter1FromIndex(docUnitIndex) / cor;
00279                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex) / cor;
00280                      break;
00281               case 2:
00282                      iter = unitRulerGetIter1FromIndex(docUnitIndex);
00283                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex);
00284                      if (sc > 1 && sc <= 4)
00285                      {
00286                             cor = 2;
00287                             iter /= cor;
00288                             iter2 /= cor;
00289                      }
00290                      if (sc > 4)
00291                      {
00292                             cor = 4;
00293                             iter /= cor;
00294                             iter2 /= cor;
00295                      }
00296                      break;
00297               case 3:
00298                      iter = unitRulerGetIter1FromIndex(docUnitIndex);
00299                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex);
00300                      if (sc > 1 && sc <= 4)
00301                      {
00302                             cor = 1;
00303                             iter = 12.0;
00304                             iter2 = 60.0;
00305                      }
00306                      if (sc > 4)
00307                      {
00308                             cor = 2;
00309                             iter = 6.0;
00310                             iter2 = 12.0;
00311                      }
00312                      break;
00313               case 4:
00314                      if (sc > 1 && sc <= 4)
00315                             cor = 1;
00316                      if (sc > 4)
00317                             cor = 10;
00318                      if (sc < 0.3)
00319                      {
00320                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 4;
00321                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 4;
00322                      }
00323                      else if (sc < 0.6)
00324                      {
00325                             iter = unitRulerGetIter1FromIndex(docUnitIndex) * 3;
00326                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) * 3;
00327                      }
00328                      else
00329                      {
00330                             iter = unitRulerGetIter1FromIndex(docUnitIndex) / cor;
00331                             iter2 = unitRulerGetIter2FromIndex(docUnitIndex) / cor;
00332                      }
00333                      break;
00334               case 5:
00335                      iter = unitRulerGetIter1FromIndex(docUnitIndex);
00336                      iter2 = unitRulerGetIter2FromIndex(docUnitIndex);
00337                      if (sc > 1 && sc <= 4)
00338                      {
00339                             cor = 1;
00340                             iter = 72.0/25.4*4.512;
00341                             iter2 = 72.0/25.4*4.512*5.0;
00342                      }
00343                      if (sc > 4)
00344                      {
00345                             cor = 2;
00346                             iter = 72.0/25.4*4.512/2.0;
00347                             iter2 = 72.0/25.4*4.512;
00348                      }
00349                      break;
00350               default:
00351                      if (sc > 1 && sc <= 4)
00352                             cor = 2;
00353                      if (sc > 4)
00354                             cor = 10;
00355                      iter = unitRulerGetIter1FromIndex(0) / cor;
00356                      iter2 = unitRulerGetIter2FromIndex(0) / cor;
00357                      break;
00358        }
00359 }