libs/flake

KoGuidesData.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Laurent Montel <montel@kde.org>
00003    Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KoGuidesData.h"
00022 #include "KoViewConverter.h"
00023 #include <KoUnit.h>
00024 #include <KoOasisSettings.h>
00025 #include <KoXmlWriter.h>
00026 
00027 #include <QtGui/QPainter>
00028 
00029 class KoGuidesData::Private
00030 {
00031 public:
00032     Private() : showGuideLines(true), guidesColor(Qt::lightGray) {}
00033 
00034     void parseHelpLine(const QString &text) {
00035         //<config:config-item config:name="SnapLinesDrawing" config:type="string">V7939H1139</config:config-item>
00036         QString str;
00037         int newPos = text.length() - 1; //start to element = 1
00038         for (int pos = text.length() - 1; pos >= 0;--pos) {
00039             if (text[pos] == 'P') {
00040                 //point element
00041                 str = text.mid(pos + 1, (newPos - pos));
00042                 /*
00043                 QStringList listVal = QStringList::split( ",", str );
00044                 int posX = ( listVal[0].toInt()/100 );
00045                 int posY = ( listVal[1].toInt()/100 );
00046                 point.setAttribute("posX", MM_TO_POINT(  posX ));
00047                 point.setAttribute("posY", MM_TO_POINT(  posY ));
00048                 */
00049                 newPos = pos - 1;
00050             } else if (text[pos] == 'V') {
00051                 //vertical element
00052                 str = text.mid(pos + 1, (newPos - pos));
00053                 //kDebug(30518)<<" vertical  :"<< str;
00054                 qreal posX = str.toDouble() / 100.0;
00055                 vertGuideLines.append(MM_TO_POINT(posX));
00056 
00057                 newPos = (pos - 1);
00058             } else if (text[pos] == 'H') {
00059                 //horizontal element
00060                 str = text.mid(pos + 1, (newPos - pos));
00061                 qreal posY = str.toDouble() / 100.0;
00062                 horzGuideLines.append(MM_TO_POINT(posY));
00063 
00064                 newPos = pos - 1;
00065             }
00066         }
00067     }
00068 
00070     QList<qreal> horzGuideLines;
00072     QList<qreal> vertGuideLines;
00073     bool showGuideLines;
00074     QColor guidesColor;
00075 };
00076 
00077 KoGuidesData::KoGuidesData()
00078         : d(new Private())
00079 {
00080 }
00081 
00082 KoGuidesData::~KoGuidesData()
00083 {
00084     delete d;
00085 }
00086 
00087 void KoGuidesData::setHorizontalGuideLines(const QList<qreal> &lines)
00088 {
00089     d->horzGuideLines = lines;
00090 }
00091 
00092 void KoGuidesData::setVerticalGuideLines(const QList<qreal> &lines)
00093 {
00094     d->vertGuideLines = lines;
00095 }
00096 
00097 void KoGuidesData::addGuideLine(Qt::Orientation o, qreal pos)
00098 {
00099     if (o == Qt::Horizontal) {
00100         d->horzGuideLines.append(pos);
00101     } else {
00102         d->vertGuideLines.append(pos);
00103     }
00104 }
00105 
00106 bool KoGuidesData::showGuideLines() const
00107 {
00108     return d->showGuideLines;
00109 }
00110 
00111 void KoGuidesData::setShowGuideLines(bool show)
00112 {
00113     d->showGuideLines = show;
00114 }
00115 
00116 QList<qreal> KoGuidesData::horizontalGuideLines() const
00117 {
00118     return d->horzGuideLines;
00119 }
00120 
00121 QList<qreal> KoGuidesData::verticalGuideLines() const
00122 {
00123     return d->vertGuideLines;
00124 }
00125 
00126 void KoGuidesData::paintGuides(QPainter &painter, const KoViewConverter &converter, const QRectF &area) const
00127 {
00128     if (! showGuideLines())
00129         return;
00130 
00131     painter.setPen(d->guidesColor);
00132     foreach(qreal guide, d->horzGuideLines) {
00133         if (guide < area.top() || guide > area.bottom())
00134             continue;
00135         painter.drawLine(converter.documentToView(QPointF(area.left(), guide)),
00136                          converter.documentToView(QPointF(area.right(), guide)));
00137     }
00138     foreach(qreal guide, d->vertGuideLines) {
00139         if (guide < area.left() || guide > area.right())
00140             continue;
00141         painter.drawLine(converter.documentToView(QPointF(guide, area.top())),
00142                          converter.documentToView(QPointF(guide, area.bottom())));
00143     }
00144 }
00145 
00146 void KoGuidesData::setGuidesColor(const QColor &color)
00147 {
00148     d->guidesColor = color;
00149 }
00150 
00151 QColor KoGuidesData::guidesColor() const
00152 {
00153     return d->guidesColor;
00154 }
00155 
00156 bool KoGuidesData::loadOdfSettings(const KoXmlDocument & settingsDoc)
00157 {
00158     d->vertGuideLines.clear();
00159     d->horzGuideLines.clear();
00160 
00161     KoOasisSettings settings(settingsDoc);
00162     KoOasisSettings::Items viewSettings = settings.itemSet("ooo:view-settings");
00163     if (viewSettings.isNull())
00164         return false;
00165 
00166     KoOasisSettings::IndexedMap viewMap = viewSettings.indexedMap("Views");
00167     if (viewMap.isNull())
00168         return false;
00169 
00170     KoOasisSettings::Items firstView = viewMap.entry(0);
00171     if (firstView.isNull())
00172         return false;
00173 
00174     QString str = firstView.parseConfigItemString("SnapLinesDrawing");
00175     if (!str.isEmpty())
00176         d->parseHelpLine(str);
00177 
00178     return true;
00179 }
00180 
00181 void KoGuidesData::saveOdfSettings(KoXmlWriter &settingsWriter)
00182 {
00183     settingsWriter.startElement("config:config-item");
00184     settingsWriter.addAttribute("config:name", "SnapLinesDrawing");
00185     settingsWriter.addAttribute("config:type", "string");
00186 
00187     QString lineStr;
00188 
00189     foreach(qreal h, d->horzGuideLines) {
00190         int tmpY = static_cast<int>(POINT_TO_MM(h * 100.0));
00191         lineStr += 'H' + QString::number(tmpY);
00192     }
00193     foreach(qreal v, d->vertGuideLines) {
00194         int tmpX = static_cast<int>(POINT_TO_MM(v * 100.0));
00195         lineStr += 'V' + QString::number(tmpX);
00196     }
00197 
00198     settingsWriter.addTextNode(lineStr);
00199     settingsWriter.endElement(); // config:config-item
00200 }