1 /* This file is part of the KDE project
2    Copyright (C) 2006 Laurent Montel <montel@kde.org>
3    Copyright (C) 2007, 2009 Thomas Zander <zander@kde.org>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "KoGridData.h"
22 #include "KoViewConverter.h"
23 
24 #include <KoUnit.h>
25 #include <KoOasisSettings.h>
26 #include <KoXmlWriter.h>
27 
28 #include <KoIcon.h>
29 
30 #include <ktoggleaction.h>
31 #include <klocalizedstring.h>
32 #include <QPainter>
33 #include <QRectF>
34 #include <FlakeDebug.h>
35 
36 #define DEFAULT_GRID_SIZE_MM 5.0
37 
38 class Q_DECL_HIDDEN KoGridData::Private
39 {
40 public:
Private()41     Private()
42         : snapToGrid(false),
43         showGrid(false),
44         paintGridInBackground(false),
45         gridX(MM_TO_POINT(DEFAULT_GRID_SIZE_MM)),
46         gridY(MM_TO_POINT(DEFAULT_GRID_SIZE_MM)),
47         gridColor(Qt::lightGray),
48         toggleGridAction(0)
49     {
50     }
51 
~Private()52     ~Private()
53     {
54         delete toggleGridAction;
55     }
56 
57     bool snapToGrid;
58     bool showGrid;
59     bool paintGridInBackground;
60     qreal gridX, gridY;
61     QColor gridColor;
62     KToggleAction *toggleGridAction;
63 };
64 
KoGridData()65 KoGridData::KoGridData()
66         : d(new Private())
67 {
68 }
69 
~KoGridData()70 KoGridData::~KoGridData()
71 {
72     delete d;
73 }
74 
gridX() const75 qreal KoGridData::gridX() const
76 {
77     return d->gridX;
78 }
79 
gridY() const80 qreal KoGridData::gridY() const
81 {
82     return d->gridY;
83 }
84 
setGrid(qreal x,qreal y)85 void KoGridData::setGrid(qreal x, qreal y)
86 {
87     d->gridX = x;
88     d->gridY = y;
89 }
90 
snapToGrid() const91 bool KoGridData::snapToGrid() const
92 {
93     return d->snapToGrid;
94 }
95 
setSnapToGrid(bool on)96 void KoGridData::setSnapToGrid(bool on)
97 {
98     d->snapToGrid = on;
99 }
100 
gridColor() const101 QColor KoGridData::gridColor() const
102 {
103     return d->gridColor;
104 }
105 
setGridColor(const QColor & color)106 void KoGridData::setGridColor(const QColor & color)
107 {
108     d->gridColor = color;
109 }
110 
showGrid() const111 bool KoGridData::showGrid() const
112 {
113     if (d->toggleGridAction)
114         return d->toggleGridAction->isChecked();
115     return d->showGrid;
116 }
117 
setShowGrid(bool showGrid)118 void KoGridData::setShowGrid(bool showGrid)
119 {
120     if (d->toggleGridAction)
121         d->toggleGridAction->setChecked(showGrid);
122     d->showGrid = showGrid;
123 }
124 
paintGridInBackground() const125 bool KoGridData::paintGridInBackground() const
126 {
127     return d->paintGridInBackground;
128 }
129 
setPaintGridInBackground(bool inBackground)130 void KoGridData::setPaintGridInBackground(bool inBackground)
131 {
132     d->paintGridInBackground = inBackground;
133 }
134 
paintGrid(QPainter & painter,const KoViewConverter & converter,const QRectF & area) const135 void KoGridData::paintGrid(QPainter &painter, const KoViewConverter &converter, const QRectF &area) const
136 {
137     if (! showGrid())
138         return;
139 
140     painter.setPen(QPen(gridColor(), 0));
141 
142     qreal x = 0.0;
143     do {
144         painter.drawLine(converter.documentToView(QPointF(x, area.top())),
145                          converter.documentToView(QPointF(x, area.bottom())));
146         x += gridX();
147     } while (x <= area.right());
148 
149     x = - gridX();
150     while (x >= area.left()) {
151         painter.drawLine(converter.documentToView(QPointF(x, area.top())),
152                          converter.documentToView(QPointF(x, area.bottom())));
153         x -= gridX();
154     };
155 
156     qreal y = 0.0;
157     do {
158         painter.drawLine(converter.documentToView(QPointF(area.left(), y)),
159                          converter.documentToView(QPointF(area.right(), y)));
160         y += gridY();
161     } while (y <= area.bottom());
162 
163     y = - gridY();
164     while (y >= area.top()) {
165         painter.drawLine(converter.documentToView(QPointF(area.left(), y)),
166                          converter.documentToView(QPointF(area.right(), y)));
167         y -= gridY();
168     };
169 }
170 
loadOdfSettings(const KoXmlDocument & settingsDoc)171 bool KoGridData::loadOdfSettings(const KoXmlDocument & settingsDoc)
172 {
173     KoOasisSettings settings(settingsDoc);
174     KoOasisSettings::Items viewSettings = settings.itemSet("ooo:view-settings");
175     if (viewSettings.isNull())
176         return false;
177 
178     KoOasisSettings::IndexedMap viewMap = viewSettings.indexedMap("Views");
179     if (viewMap.isNull())
180         return false;
181 
182     KoOasisSettings::Items firstView = viewMap.entry(0);
183     if (firstView.isNull())
184         return false;
185 
186     qreal gridX = firstView.parseConfigItemInt("GridFineWidth", DEFAULT_GRID_SIZE_MM);
187     qreal gridY = firstView.parseConfigItemInt("GridFineHeight", DEFAULT_GRID_SIZE_MM);
188     d->gridX = MM_TO_POINT(gridX / 100.0);
189     d->gridY = MM_TO_POINT(gridY / 100.0);
190     d->snapToGrid = firstView.parseConfigItemBool("IsSnapToGrid");
191 
192     return true;
193 }
194 
saveOdfSettings(KoXmlWriter & settingsWriter)195 void KoGridData::saveOdfSettings(KoXmlWriter &settingsWriter)
196 {
197     settingsWriter.startElement("config:config-item");
198     settingsWriter.addAttribute("config:name", "IsSnapToGrid");
199     settingsWriter.addAttribute("config:type", "boolean");
200     settingsWriter.addTextNode(snapToGrid() ? "true" : "false");
201     settingsWriter.endElement();
202 
203     if (d->gridX >= 0.0) {
204         settingsWriter.startElement("config:config-item");
205         settingsWriter.addAttribute("config:name", "GridFineWidth");
206         settingsWriter.addAttribute("config:type", "int");
207         settingsWriter.addTextNode(QString::number(static_cast<int>(POINT_TO_MM(d->gridX * 100.0))));
208         settingsWriter.endElement();
209     }
210 
211     if (d->gridY >= 0.0) {
212         settingsWriter.startElement("config:config-item");
213         settingsWriter.addAttribute("config:name", "GridFineHeight");
214         settingsWriter.addAttribute("config:type", "int");
215         settingsWriter.addTextNode(QString::number(static_cast<int>(POINT_TO_MM(d->gridY * 100.0))));
216         settingsWriter.endElement();
217     }
218 }
219 
gridToggleAction(QWidget * canvas)220 KToggleAction *KoGridData::gridToggleAction(QWidget* canvas)
221 {
222     if (! d->toggleGridAction) {
223         d->toggleGridAction = new KToggleAction(koIcon("view-grid"), i18n("Show Grid"), 0);
224         d->toggleGridAction->setToolTip(i18n("Shows or hides grid"));
225         d->toggleGridAction->setChecked(d->showGrid);
226     }
227     if (canvas)
228         QObject::connect(d->toggleGridAction, SIGNAL(toggled(bool)), canvas, SLOT(update()));
229     return d->toggleGridAction;
230 }
231