krita

kis_grid_manager.cpp

00001 /*
00002  * This file is part of Krita
00003  *
00004  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #ifdef HAVE_GL
00026 #include <qgl.h>
00027 #endif
00028 
00029 #include <qradiobutton.h>
00030 
00031 #include <kaction.h>
00032 #include <kdialogbase.h>
00033 #include <klocale.h>
00034 
00035 #include "kis_grid_manager.h"
00036 #include "kis_grid_manager.moc"
00037 
00038 #include "kis_config.h"
00039 #include "kis_view.h"
00040 #include "kis_image.h"
00041 
00042 KisGridManager::KisGridManager(KisView * parent)
00043     : QObject(parent), m_view(parent)
00044 {
00045 
00046 }
00047 
00048 KisGridManager::~KisGridManager()
00049 {
00050 
00051 }
00052 
00053 void KisGridManager::setup(KActionCollection * collection)
00054 {
00055     m_toggleGrid = new KToggleAction(i18n("Show Grid"), "", this, SLOT(toggleGrid()), collection, "view_toggle_grid");
00056     m_toggleGrid->setCheckedState(KGuiItem(i18n("Hide Grid")));
00057     m_toggleGrid->setChecked(false);
00058 
00059     // Fast grid config
00060     m_gridFastConfig1x1 = new KAction(i18n("1x1"), 0, "", this, SLOT(fastConfig1x1()), collection, "view_fast_grid_1x1");
00061     m_gridFastConfig2x2 = new KAction(i18n("2x2"), 0, "", this, SLOT(fastConfig2x2()), collection, "view_fast_grid_2x2");
00062     m_gridFastConfig5x5 = new KAction(i18n("5x5"), 0, "", this, SLOT(fastConfig5x5()), collection, "view_fast_grid_5x5");
00063     m_gridFastConfig10x10 = new KAction(i18n("10x10"), 0, "", this, SLOT(fastConfig10x10()), collection, "view_fast_grid_10x10");
00064     m_gridFastConfig20x20 = new KAction(i18n("20x20"), 0, "", this, SLOT(fastConfig20x20()), collection, "view_fast_grid_20x20");
00065     m_gridFastConfig40x40 = new KAction(i18n("40x40"), 0, "", this, SLOT(fastConfig40x40()), collection, "view_fast_grid_40x40");
00066 }
00067 
00068 void KisGridManager::updateGUI()
00069 {
00070 
00071 }
00072 
00073 void KisGridManager::toggleGrid()
00074 {
00075     m_view->updateCanvas();
00076 }
00077 
00078 void KisGridManager::fastConfig1x1()
00079 {
00080     KisConfig cfg;
00081     cfg.setGridHSpacing(1);
00082     cfg.setGridVSpacing(1);
00083     m_view->updateCanvas();
00084 }
00085 
00086 void KisGridManager::fastConfig2x2()
00087 {
00088     KisConfig cfg;
00089     cfg.setGridHSpacing(2);
00090     cfg.setGridVSpacing(2);
00091     m_view->updateCanvas();
00092 }
00093 
00094 void KisGridManager::fastConfig5x5()
00095 {
00096     KisConfig cfg;
00097     cfg.setGridHSpacing(5);
00098     cfg.setGridVSpacing(5);
00099     m_view->updateCanvas();
00100 }
00101 
00102 void KisGridManager::fastConfig10x10()
00103 {
00104     KisConfig cfg;
00105     cfg.setGridHSpacing(10);
00106     cfg.setGridVSpacing(10);
00107     m_view->updateCanvas();
00108 }
00109 
00110 void KisGridManager::fastConfig20x20()
00111 {
00112     KisConfig cfg;
00113     cfg.setGridHSpacing(20);
00114     cfg.setGridVSpacing(20);
00115     m_view->updateCanvas();
00116 }
00117 
00118 void KisGridManager::fastConfig40x40()
00119 {
00120     KisConfig cfg;
00121     cfg.setGridHSpacing(40);
00122     cfg.setGridVSpacing(40);
00123     m_view->updateCanvas();
00124 }
00125 
00126 Qt::PenStyle KisGridManager::GridDrawer::gs2style(Q_UINT32 s)
00127 {
00128     switch(s)
00129     {
00130         case 1:
00131             return Qt::DashLine;
00132         case 2:
00133             return Qt::DotLine;
00134         case 3:
00135             return Qt::DashDotLine;
00136         case 4:
00137             return Qt::DashDotDotLine;
00138         default:
00139             return Qt::SolidLine;
00140     }
00141 }
00142 
00143 void KisGridManager::drawGrid(QRect wr, QPainter *p, bool openGL)
00144 {
00145     KisImageSP image = m_view->canvasSubject()->currentImg();
00146 
00147     if (image) {
00148         if (m_toggleGrid->isChecked())
00149         {
00150             GridDrawer *gridDrawer = 0;
00151 
00152             if (openGL) {
00153                 gridDrawer = new OpenGLGridDrawer();
00154             } else {
00155                 Q_ASSERT(p);
00156 
00157                 if (p) {
00158                     gridDrawer = new QPainterGridDrawer(p);
00159                 }
00160             }
00161 
00162             Q_ASSERT(gridDrawer != 0);
00163 
00164             if (gridDrawer) {
00165                 gridDrawer->drawGrid(image, wr);
00166                 delete gridDrawer;
00167             }
00168         }
00169     }
00170 }
00171 
00172 void KisGridManager::GridDrawer::drawGrid(KisImageSP image, const QRect& wr)
00173 {
00174     KisConfig cfg;
00175     
00176     Q_UINT32 offsetx = cfg.getGridOffsetX();
00177     Q_UINT32 offsety = cfg.getGridOffsetY();
00178     Q_UINT32 hspacing = cfg.getGridHSpacing();
00179     Q_UINT32 vspacing = cfg.getGridVSpacing();
00180     Q_UINT32 subdivision = cfg.getGridSubdivisions() - 1;
00181     //double ihspsub = hspacing / (double)subdivision;
00182     //double ivspsub = hspacing / (double)subdivision;
00183 
00184     Q_INT32 imageWidth = image->width();
00185     Q_INT32 imageHeight = image->height();
00186 
00187     // Draw vertical line
00188     QPen mainPen = QPen ( cfg.getGridMainColor(), 1, gs2style( cfg.getGridMainStyle() ) );
00189     QPen subdivisionPen = QPen ( cfg.getGridSubdivisionColor(), 1, gs2style( cfg.getGridSubdivisionStyle() ) );
00190     Q_UINT32 i = 0;
00191     for( Q_INT32 x = offsetx; x <= wr.right(); x +=hspacing)
00192     {
00193         if( i == subdivision )
00194         {
00195             setPen(mainPen);
00196             i = 0;
00197         } else {
00198             setPen(subdivisionPen);
00199             i++;
00200         }
00201         if( x >= wr.x() )
00202         {
00203             // Always draw the full line otherwise the line stippling varies
00204             // with the location of wr and we get glitchy patterns.
00205             drawLine(x, 0, x, imageHeight);
00206         }
00207     }
00208     // Draw horizontal line
00209     i = 0;
00210     for( Q_INT32 y = offsety; y <= wr.bottom(); y +=vspacing)
00211     {
00212         if( i == subdivision )
00213         {
00214             setPen(mainPen);
00215             i = 0;
00216         } else {
00217             setPen(subdivisionPen);
00218             i++;
00219         }
00220         if( y >= wr.y() )
00221         {
00222             drawLine(0, y, imageWidth, y);
00223         }
00224     }
00225 }
00226 
00227 KisGridManager::OpenGLGridDrawer::OpenGLGridDrawer()
00228 {
00229 #ifdef HAVE_GL
00230     glPushAttrib(GL_ALL_ATTRIB_BITS);
00231 #endif
00232 }
00233 
00234 KisGridManager::OpenGLGridDrawer::~OpenGLGridDrawer()
00235 {
00236 #ifdef HAVE_GL
00237     glPopAttrib();
00238 #endif
00239 }
00240 
00241 void KisGridManager::OpenGLGridDrawer::setPen(const QPen& pen)
00242 {
00243 #ifdef HAVE_GL
00244     Qt::PenStyle penStyle = pen.style();
00245 
00246     if (penStyle == Qt::SolidLine) {
00247         glDisable(GL_LINE_STIPPLE);
00248     } else {
00249         GLushort lineStipple;
00250 
00251         switch (penStyle) {
00252         case Qt::NoPen:
00253             lineStipple = 0;
00254             break;
00255         default:
00256         case Qt::SolidLine:
00257             lineStipple = 0xffff;
00258             break;
00259         case Qt::DashLine:
00260             lineStipple = 0x3fff;
00261             break;
00262         case Qt::DotLine:
00263             lineStipple = 0x3333;
00264             break;
00265         case Qt::DashDotLine:
00266             lineStipple = 0x33ff;
00267             break;
00268         case Qt::DashDotDotLine:
00269             lineStipple = 0x333f;
00270             break;
00271         }
00272 
00273         glEnable(GL_LINE_STIPPLE);
00274         glLineStipple(1, lineStipple);
00275     }
00276 
00277     QColor penColor = pen.color();
00278 
00279     glColor3ub(penColor.red(), penColor.green(), penColor.blue());
00280 #else
00281     Q_UNUSED(pen);
00282 #endif
00283 }
00284 
00285 void KisGridManager::OpenGLGridDrawer::drawLine(Q_INT32 x1, Q_INT32 y1, Q_INT32 x2, Q_INT32 y2)
00286 {
00287 #ifdef HAVE_GL
00288     glBegin(GL_LINES);
00289     glVertex2i(x1, y1);
00290     glVertex2i(x2, y2);
00291     glEnd();
00292 #else
00293     Q_UNUSED(x1);
00294     Q_UNUSED(y1);
00295     Q_UNUSED(x2);
00296     Q_UNUSED(y2);
00297 #endif
00298 }
00299 
KDE Home | KDE Accessibility Home | Description of Access Keys