krita

kis_perspective_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 #include "kis_perspective_grid_manager.h"
00022 
00023 #include <kaction.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 
00027 #include "kis_image.h"
00028 #include "kis_grid_drawer.h"
00029 #include "kis_perspective_grid.h"
00030 #include "kis_view.h"
00031 
00032 KisPerspectiveGridManager::KisPerspectiveGridManager(KisView * parent)
00033     : QObject()
00034     , m_toggleEdition(false)
00035     , m_view(parent)
00036 {
00037     
00038 }
00039 
00040 
00041 KisPerspectiveGridManager::~KisPerspectiveGridManager()
00042 {
00043     
00044 }
00045 
00046 void KisPerspectiveGridManager::updateGUI()
00047 {
00048     KisImageSP image = m_view->canvasSubject()->currentImg();
00049 
00050     
00051     if (image ) {
00052         KisPerspectiveGrid* pGrid = image->perspectiveGrid();
00053         m_toggleGrid->setEnabled( pGrid->hasSubGrids());
00054     }
00055 }
00056 
00057 void KisPerspectiveGridManager::setup(KActionCollection * collection)
00058 {
00059     kdDebug() << "KisPerspectiveGridManager::setup(KActionCollection * collection)" << endl;
00060     m_toggleGrid = new KToggleAction(i18n("Show Perspective Grid"), "", this, SLOT(toggleGrid()), collection, "view_toggle_perspective_grid");
00061     m_toggleGrid->setCheckedState(KGuiItem(i18n("Hide Perspective Grid")));
00062     m_toggleGrid->setChecked(false);
00063     m_gridClear = new KAction(i18n("Clear Perspective Grid"), 0, "", this, SLOT(clearPerspectiveGrid()), collection, "view_clear_perspective_grid");
00064 }
00065 
00066 void KisPerspectiveGridManager::setGridVisible(bool t)
00067 {
00068     KisImageSP image = m_view->canvasSubject()->currentImg();
00069 
00070     
00071     if (t && image ) {
00072         KisPerspectiveGrid* pGrid = image->perspectiveGrid();
00073         if( pGrid->hasSubGrids())
00074         {
00075             m_toggleGrid->setChecked(true);
00076         }
00077     } else {
00078         m_toggleGrid->setChecked(false);
00079     }
00080     m_view->refreshKisCanvas();
00081 }
00082 
00083 
00084 void KisPerspectiveGridManager::toggleGrid()
00085 {
00086     KisImageSP image = m_view->canvasSubject()->currentImg();
00087 
00088     
00089     if (image && m_toggleGrid->isChecked()) {
00090         KisPerspectiveGrid* pGrid = image->perspectiveGrid();
00091 
00092         if(!pGrid->hasSubGrids())
00093         {
00094             KMessageBox::error(0, i18n("Before displaying the perspective grid, you need to initialize it with the perspective grid tool"), i18n("No Perspective Grid to Display") );
00095             m_toggleGrid->setChecked(false);
00096         }
00097     }
00098     m_view->updateCanvas();
00099 }
00100 
00101 void KisPerspectiveGridManager::clearPerspectiveGrid()
00102 {
00103     KisImageSP image = m_view->canvasSubject()->currentImg();
00104     if (image ) {
00105         image->perspectiveGrid()->clearSubGrids();
00106         m_view->updateCanvas();
00107         m_toggleGrid->setChecked(false);
00108         m_toggleGrid->setEnabled(false);
00109     }
00110 }
00111 
00112 void KisPerspectiveGridManager::startEdition()
00113 {
00114     m_toggleEdition = true;
00115     m_toggleGrid->setEnabled( false );
00116     if( m_toggleGrid->isChecked() )
00117         m_view->updateCanvas();
00118 }
00119 
00120 void KisPerspectiveGridManager::stopEdition()
00121 {
00122     m_toggleEdition = false;
00123     m_toggleGrid->setEnabled( true );
00124     if( m_toggleGrid->isChecked() )
00125         m_view->updateCanvas();
00126 }
00127 
00128 void KisPerspectiveGridManager::drawGrid(QRect wr, QPainter *p, bool openGL )
00129 {
00130     KisImageSP image = m_view->canvasSubject()->currentImg();
00131 
00132     
00133     if (image && m_toggleGrid->isChecked() && !m_toggleEdition) {
00134         KisPerspectiveGrid* pGrid = image->perspectiveGrid();
00135 
00136         GridDrawer *gridDrawer = 0;
00137 
00138         if (openGL) {
00139             gridDrawer = new OpenGLGridDrawer();
00140         } else {
00141             Q_ASSERT(p);
00142 
00143             if (p) {
00144                 gridDrawer = new QPainterGridDrawer(p);
00145             }
00146         }
00147 
00148         Q_ASSERT(gridDrawer != 0);
00149 
00150         for( QValueList<KisSubPerspectiveGrid*>::const_iterator it = pGrid->begin(); it != pGrid->end(); ++it)
00151         {
00152             gridDrawer->drawPerspectiveGrid(image, wr, *it );
00153         }
00154         delete gridDrawer;
00155     }
00156 }
00157 
00158 
00159 #include "kis_perspective_grid_manager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys