Warning, file /office/calligra/libs/flake/KoGridData.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 Laurent Montel <montel@kde.org>
0003    Copyright (C) 2007, 2009 Thomas Zander <zander@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KoGridData.h"
0022 #include "KoViewConverter.h"
0023 
0024 #include <KoUnit.h>
0025 #include <KoOasisSettings.h>
0026 #include <KoXmlWriter.h>
0027 
0028 #include <KoIcon.h>
0029 
0030 #include <ktoggleaction.h>
0031 #include <klocalizedstring.h>
0032 #include <QPainter>
0033 #include <QRectF>
0034 #include <FlakeDebug.h>
0035 
0036 #define DEFAULT_GRID_SIZE_MM 5.0
0037 
0038 class Q_DECL_HIDDEN KoGridData::Private
0039 {
0040 public:
0041     Private()
0042         : snapToGrid(false),
0043         showGrid(false),
0044         paintGridInBackground(false),
0045         gridX(MM_TO_POINT(DEFAULT_GRID_SIZE_MM)),
0046         gridY(MM_TO_POINT(DEFAULT_GRID_SIZE_MM)),
0047         gridColor(Qt::lightGray),
0048         toggleGridAction(0)
0049     {
0050     }
0051 
0052     ~Private()
0053     {
0054         delete toggleGridAction;
0055     }
0056 
0057     bool snapToGrid;
0058     bool showGrid;
0059     bool paintGridInBackground;
0060     qreal gridX, gridY;
0061     QColor gridColor;
0062     KToggleAction *toggleGridAction;
0063 };
0064 
0065 KoGridData::KoGridData()
0066         : d(new Private())
0067 {
0068 }
0069 
0070 KoGridData::~KoGridData()
0071 {
0072     delete d;
0073 }
0074 
0075 qreal KoGridData::gridX() const
0076 {
0077     return d->gridX;
0078 }
0079 
0080 qreal KoGridData::gridY() const
0081 {
0082     return d->gridY;
0083 }
0084 
0085 void KoGridData::setGrid(qreal x, qreal y)
0086 {
0087     d->gridX = x;
0088     d->gridY = y;
0089 }
0090 
0091 bool KoGridData::snapToGrid() const
0092 {
0093     return d->snapToGrid;
0094 }
0095 
0096 void KoGridData::setSnapToGrid(bool on)
0097 {
0098     d->snapToGrid = on;
0099 }
0100 
0101 QColor KoGridData::gridColor() const
0102 {
0103     return d->gridColor;
0104 }
0105 
0106 void KoGridData::setGridColor(const QColor & color)
0107 {
0108     d->gridColor = color;
0109 }
0110 
0111 bool KoGridData::showGrid() const
0112 {
0113     if (d->toggleGridAction)
0114         return d->toggleGridAction->isChecked();
0115     return d->showGrid;
0116 }
0117 
0118 void KoGridData::setShowGrid(bool showGrid)
0119 {
0120     if (d->toggleGridAction)
0121         d->toggleGridAction->setChecked(showGrid);
0122     d->showGrid = showGrid;
0123 }
0124 
0125 bool KoGridData::paintGridInBackground() const
0126 {
0127     return d->paintGridInBackground;
0128 }
0129 
0130 void KoGridData::setPaintGridInBackground(bool inBackground)
0131 {
0132     d->paintGridInBackground = inBackground;
0133 }
0134 
0135 void KoGridData::paintGrid(QPainter &painter, const KoViewConverter &converter, const QRectF &area) const
0136 {
0137     if (! showGrid())
0138         return;
0139 
0140     painter.setPen(QPen(gridColor(), 0));
0141 
0142     qreal x = 0.0;
0143     do {
0144         painter.drawLine(converter.documentToView(QPointF(x, area.top())),
0145                          converter.documentToView(QPointF(x, area.bottom())));
0146         x += gridX();
0147     } while (x <= area.right());
0148 
0149     x = - gridX();
0150     while (x >= area.left()) {
0151         painter.drawLine(converter.documentToView(QPointF(x, area.top())),
0152                          converter.documentToView(QPointF(x, area.bottom())));
0153         x -= gridX();
0154     };
0155 
0156     qreal y = 0.0;
0157     do {
0158         painter.drawLine(converter.documentToView(QPointF(area.left(), y)),
0159                          converter.documentToView(QPointF(area.right(), y)));
0160         y += gridY();
0161     } while (y <= area.bottom());
0162 
0163     y = - gridY();
0164     while (y >= area.top()) {
0165         painter.drawLine(converter.documentToView(QPointF(area.left(), y)),
0166                          converter.documentToView(QPointF(area.right(), y)));
0167         y -= gridY();
0168     };
0169 }
0170 
0171 bool KoGridData::loadOdfSettings(const KoXmlDocument & settingsDoc)
0172 {
0173     KoOasisSettings settings(settingsDoc);
0174     KoOasisSettings::Items viewSettings = settings.itemSet("ooo:view-settings");
0175     if (viewSettings.isNull())
0176         return false;
0177 
0178     KoOasisSettings::IndexedMap viewMap = viewSettings.indexedMap("Views");
0179     if (viewMap.isNull())
0180         return false;
0181 
0182     KoOasisSettings::Items firstView = viewMap.entry(0);
0183     if (firstView.isNull())
0184         return false;
0185 
0186     qreal gridX = firstView.parseConfigItemInt("GridFineWidth", DEFAULT_GRID_SIZE_MM);
0187     qreal gridY = firstView.parseConfigItemInt("GridFineHeight", DEFAULT_GRID_SIZE_MM);
0188     d->gridX = MM_TO_POINT(gridX / 100.0);
0189     d->gridY = MM_TO_POINT(gridY / 100.0);
0190     d->snapToGrid = firstView.parseConfigItemBool("IsSnapToGrid");
0191 
0192     return true;
0193 }
0194 
0195 void KoGridData::saveOdfSettings(KoXmlWriter &settingsWriter)
0196 {
0197     settingsWriter.startElement("config:config-item");
0198     settingsWriter.addAttribute("config:name", "IsSnapToGrid");
0199     settingsWriter.addAttribute("config:type", "boolean");
0200     settingsWriter.addTextNode(snapToGrid() ? "true" : "false");
0201     settingsWriter.endElement();
0202 
0203     if (d->gridX >= 0.0) {
0204         settingsWriter.startElement("config:config-item");
0205         settingsWriter.addAttribute("config:name", "GridFineWidth");
0206         settingsWriter.addAttribute("config:type", "int");
0207         settingsWriter.addTextNode(QString::number(static_cast<int>(POINT_TO_MM(d->gridX * 100.0))));
0208         settingsWriter.endElement();
0209     }
0210 
0211     if (d->gridY >= 0.0) {
0212         settingsWriter.startElement("config:config-item");
0213         settingsWriter.addAttribute("config:name", "GridFineHeight");
0214         settingsWriter.addAttribute("config:type", "int");
0215         settingsWriter.addTextNode(QString::number(static_cast<int>(POINT_TO_MM(d->gridY * 100.0))));
0216         settingsWriter.endElement();
0217     }
0218 }
0219 
0220 KToggleAction *KoGridData::gridToggleAction(QWidget* canvas)
0221 {
0222     if (! d->toggleGridAction) {
0223         d->toggleGridAction = new KToggleAction(koIcon("view-grid"), i18n("Show Grid"), 0);
0224         d->toggleGridAction->setToolTip(i18n("Shows or hides grid"));
0225         d->toggleGridAction->setChecked(d->showGrid);
0226     }
0227     if (canvas)
0228         QObject::connect(d->toggleGridAction, SIGNAL(toggled(bool)), canvas, SLOT(update()));
0229     return d->toggleGridAction;
0230 }