Warning, file /office/calligra/libs/flake/KoGuidesData.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) 2008 Jan Hambrecht <jaham@gmx.net>
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 "KoGuidesData.h"
0022 #include "KoViewConverter.h"
0023 #include <KoUnit.h>
0024 #include <KoOasisSettings.h>
0025 #include <KoXmlWriter.h>
0026 
0027 #include <QPainter>
0028 
0029 class Q_DECL_HIDDEN KoGuidesData::Private
0030 {
0031 public:
0032     Private() : showGuideLines(true), guidesColor(Qt::lightGray) {}
0033 
0034     void parseHelpLine(const QString &text) {
0035         //<config:config-item config:name="SnapLinesDrawing" config:type="string">V7939H1139</config:config-item>
0036         QString str;
0037         int newPos = text.length() - 1; //start to element = 1
0038         for (int pos = text.length() - 1; pos >= 0;--pos) {
0039             if (text[pos] == 'P') {
0040                 //point element
0041                 str = text.mid(pos + 1, (newPos - pos));
0042                 /*
0043                 QStringList listVal = QStringList::split(",", str);
0044                 int posX = (listVal[0].toInt()/100);
0045                 int posY = (listVal[1].toInt()/100);
0046                 point.setAttribute("posX", MM_TO_POINT(posX));
0047                 point.setAttribute("posY", MM_TO_POINT(posY));
0048                 */
0049                 newPos = pos - 1;
0050             } else if (text[pos] == 'V') {
0051                 //vertical element
0052                 str = text.mid(pos + 1, (newPos - pos));
0053                 //debugFlake<<" vertical  :"<< str;
0054                 qreal posX = str.toDouble() / 100.0;
0055                 vertGuideLines.append(MM_TO_POINT(posX));
0056 
0057                 newPos = (pos - 1);
0058             } else if (text[pos] == 'H') {
0059                 //horizontal element
0060                 str = text.mid(pos + 1, (newPos - pos));
0061                 qreal posY = str.toDouble() / 100.0;
0062                 horzGuideLines.append(MM_TO_POINT(posY));
0063 
0064                 newPos = pos - 1;
0065             }
0066         }
0067     }
0068 
0069     /// list of positions of horizontal guide lines
0070     QList<qreal> horzGuideLines;
0071     /// list of positions of vertical guide lines
0072     QList<qreal> vertGuideLines;
0073     bool showGuideLines;
0074     QColor guidesColor;
0075 };
0076 
0077 KoGuidesData::KoGuidesData()
0078         : d(new Private())
0079 {
0080 }
0081 
0082 KoGuidesData::~KoGuidesData()
0083 {
0084     delete d;
0085 }
0086 
0087 void KoGuidesData::setHorizontalGuideLines(const QList<qreal> &lines)
0088 {
0089     d->horzGuideLines = lines;
0090 }
0091 
0092 void KoGuidesData::setVerticalGuideLines(const QList<qreal> &lines)
0093 {
0094     d->vertGuideLines = lines;
0095 }
0096 
0097 void KoGuidesData::addGuideLine(Qt::Orientation o, qreal pos)
0098 {
0099     if (o == Qt::Horizontal) {
0100         d->horzGuideLines.append(pos);
0101     } else {
0102         d->vertGuideLines.append(pos);
0103     }
0104 }
0105 
0106 bool KoGuidesData::showGuideLines() const
0107 {
0108     return d->showGuideLines;
0109 }
0110 
0111 void KoGuidesData::setShowGuideLines(bool show)
0112 {
0113     d->showGuideLines = show;
0114 }
0115 
0116 QList<qreal> KoGuidesData::horizontalGuideLines() const
0117 {
0118     return d->horzGuideLines;
0119 }
0120 
0121 QList<qreal> KoGuidesData::verticalGuideLines() const
0122 {
0123     return d->vertGuideLines;
0124 }
0125 
0126 void KoGuidesData::paintGuides(QPainter &painter, const KoViewConverter &converter, const QRectF &area) const
0127 {
0128     if (! showGuideLines())
0129         return;
0130 
0131     painter.setPen(QPen(d->guidesColor, 0));
0132     foreach(qreal guide, d->horzGuideLines) {
0133         if (guide < area.top() || guide > area.bottom())
0134             continue;
0135         painter.drawLine(converter.documentToView(QPointF(area.left(), guide)),
0136                          converter.documentToView(QPointF(area.right(), guide)));
0137     }
0138     foreach(qreal guide, d->vertGuideLines) {
0139         if (guide < area.left() || guide > area.right())
0140             continue;
0141         painter.drawLine(converter.documentToView(QPointF(guide, area.top())),
0142                          converter.documentToView(QPointF(guide, area.bottom())));
0143     }
0144 }
0145 
0146 void KoGuidesData::setGuidesColor(const QColor &color)
0147 {
0148     d->guidesColor = color;
0149 }
0150 
0151 QColor KoGuidesData::guidesColor() const
0152 {
0153     return d->guidesColor;
0154 }
0155 
0156 bool KoGuidesData::loadOdfSettings(const KoXmlDocument & settingsDoc)
0157 {
0158     d->vertGuideLines.clear();
0159     d->horzGuideLines.clear();
0160 
0161     KoOasisSettings settings(settingsDoc);
0162     KoOasisSettings::Items viewSettings = settings.itemSet("ooo:view-settings");
0163     if (viewSettings.isNull())
0164         return false;
0165 
0166     KoOasisSettings::IndexedMap viewMap = viewSettings.indexedMap("Views");
0167     if (viewMap.isNull())
0168         return false;
0169 
0170     KoOasisSettings::Items firstView = viewMap.entry(0);
0171     if (firstView.isNull())
0172         return false;
0173 
0174     QString str = firstView.parseConfigItemString("SnapLinesDrawing");
0175     if (!str.isEmpty())
0176         d->parseHelpLine(str);
0177 
0178     return true;
0179 }
0180 
0181 void KoGuidesData::saveOdfSettings(KoXmlWriter &settingsWriter)
0182 {
0183     settingsWriter.startElement("config:config-item");
0184     settingsWriter.addAttribute("config:name", "SnapLinesDrawing");
0185     settingsWriter.addAttribute("config:type", "string");
0186 
0187     QString lineStr;
0188 
0189     foreach(qreal h, d->horzGuideLines) {
0190         int tmpY = static_cast<int>(POINT_TO_MM(h * 100.0));
0191         lineStr += 'H' + QString::number(tmpY);
0192     }
0193     foreach(qreal v, d->vertGuideLines) {
0194         int tmpX = static_cast<int>(POINT_TO_MM(v * 100.0));
0195         lineStr += 'V' + QString::number(tmpX);
0196     }
0197 
0198     settingsWriter.addTextNode(lineStr);
0199     settingsWriter.endElement(); // config:config-item
0200 }