chalk

kis_perspective_grid_manager.cpp
1/*
2 * This file is part of Chalk
3 *
4 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "kis_perspective_grid_manager.h"
22
23#include <tdeaction.h>
24#include <tdelocale.h>
25#include <tdemessagebox.h>
26
27#include "kis_image.h"
28#include "kis_grid_drawer.h"
29#include "kis_perspective_grid.h"
30#include "kis_view.h"
31
32KisPerspectiveGridManager::KisPerspectiveGridManager(KisView * parent)
33 : TQObject()
34 , m_toggleEdition(false)
35 , m_view(parent)
36{
37
38}
39
40
41KisPerspectiveGridManager::~KisPerspectiveGridManager()
42{
43
44}
45
46void KisPerspectiveGridManager::updateGUI()
47{
48 KisImageSP image = m_view->canvasSubject()->currentImg();
49
50
51 if (image ) {
52 KisPerspectiveGrid* pGrid = image->perspectiveGrid();
53 m_toggleGrid->setEnabled( pGrid->hasSubGrids());
54 }
55}
56
57void KisPerspectiveGridManager::setup(TDEActionCollection * collection)
58{
59 kdDebug() << "KisPerspectiveGridManager::setup(TDEActionCollection * collection)" << endl;
60 m_toggleGrid = new TDEToggleAction(i18n("Show Perspective Grid"), "", this, TQ_SLOT(toggleGrid()), collection, "view_toggle_perspective_grid");
61 m_toggleGrid->setCheckedState(KGuiItem(i18n("Hide Perspective Grid")));
62 m_toggleGrid->setChecked(false);
63 m_gridClear = new TDEAction(i18n("Clear Perspective Grid"), 0, "", this, TQ_SLOT(clearPerspectiveGrid()), collection, "view_clear_perspective_grid");
64}
65
66void KisPerspectiveGridManager::setGridVisible(bool t)
67{
68 KisImageSP image = m_view->canvasSubject()->currentImg();
69
70
71 if (t && image ) {
72 KisPerspectiveGrid* pGrid = image->perspectiveGrid();
73 if( pGrid->hasSubGrids())
74 {
75 m_toggleGrid->setChecked(true);
76 }
77 } else {
78 m_toggleGrid->setChecked(false);
79 }
80 m_view->refreshKisCanvas();
81}
82
83
84void KisPerspectiveGridManager::toggleGrid()
85{
86 KisImageSP image = m_view->canvasSubject()->currentImg();
87
88
89 if (image && m_toggleGrid->isChecked()) {
90 KisPerspectiveGrid* pGrid = image->perspectiveGrid();
91
92 if(!pGrid->hasSubGrids())
93 {
94 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") );
95 m_toggleGrid->setChecked(false);
96 }
97 }
98 m_view->updateCanvas();
99}
100
101void KisPerspectiveGridManager::clearPerspectiveGrid()
102{
103 KisImageSP image = m_view->canvasSubject()->currentImg();
104 if (image ) {
105 image->perspectiveGrid()->clearSubGrids();
106 m_view->updateCanvas();
107 m_toggleGrid->setChecked(false);
108 m_toggleGrid->setEnabled(false);
109 }
110}
111
112void KisPerspectiveGridManager::startEdition()
113{
114 m_toggleEdition = true;
115 m_toggleGrid->setEnabled( false );
116 if( m_toggleGrid->isChecked() )
117 m_view->updateCanvas();
118}
119
120void KisPerspectiveGridManager::stopEdition()
121{
122 m_toggleEdition = false;
123 m_toggleGrid->setEnabled( true );
124 if( m_toggleGrid->isChecked() )
125 m_view->updateCanvas();
126}
127
128void KisPerspectiveGridManager::drawGrid(TQRect wr, TQPainter *p, bool openGL )
129{
130 KisImageSP image = m_view->canvasSubject()->currentImg();
131
132
133 if (image && m_toggleGrid->isChecked() && !m_toggleEdition) {
134 KisPerspectiveGrid* pGrid = image->perspectiveGrid();
135
136 GridDrawer *gridDrawer = 0;
137
138 if (openGL) {
139 gridDrawer = new OpenGLGridDrawer();
140 } else {
141 Q_ASSERT(p);
142
143 if (p) {
144 gridDrawer = new TQPainterGridDrawer(p);
145 }
146 }
147
148 Q_ASSERT(gridDrawer != 0);
149
150 for( TQValueList<KisSubPerspectiveGrid*>::const_iterator it = pGrid->begin(); it != pGrid->end(); ++it)
151 {
152 gridDrawer->drawPerspectiveGrid(image, wr, *it );
153 }
154 delete gridDrawer;
155 }
156}
157
158
159#include "kis_perspective_grid_manager.moc"