File indexing completed on 2024-05-12 04:35:06

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (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
0013  * GNU 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, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "Pos.h"
0021 
0022 namespace tikz {
0023 
0024 QString Pos::toString() const
0025 {
0026     // we require a valid number
0027     Q_ASSERT(isValid());
0028 
0029     return QLatin1Char('(') + m_x.toString()
0030       + QLatin1String(", ") + m_y.toString()
0031       + QLatin1Char(')');
0032 }
0033 
0034 Pos Pos::fromString(const QString & str)
0035 {
0036     // format example: (12.50cm, -13.5pt)
0037     
0038     // 1. find index of '(', ',', and ')'
0039     const int indexOfOpen = str.indexOf('(');
0040     const int indexOfComma = str.indexOf(',');
0041     const int indexOfClose = str.indexOf(')');
0042 
0043     // for now: strict sanity check
0044     // FIXME: In a releas, this should be more tolerant
0045     Q_ASSERT(indexOfOpen >= 0);
0046     Q_ASSERT(indexOfComma >= 0);
0047     Q_ASSERT(indexOfClose >= 0);
0048     Q_ASSERT(indexOfComma > indexOfOpen);
0049     Q_ASSERT(indexOfClose > indexOfOpen);
0050     Q_ASSERT(indexOfClose > indexOfComma);
0051 
0052     // 2. extract 
0053     const QString p1 = str.mid(indexOfOpen + 1, indexOfComma - (indexOfOpen + 1)).trimmed();
0054     const QString p2 = str.mid(indexOfComma + 1, indexOfClose - (indexOfComma + 1)).trimmed();
0055 
0056 //     qDebug() << "reading Pos from" << str << "results in:" << p1 << p2;
0057 
0058     return Pos(Value::fromString(p1), Value::fromString(p2));
0059 }
0060 
0061 }
0062 
0063 namespace QTest {
0064     // Value: template specialization for QTest::toString()
0065     template<>
0066     char *toString(const tikz::Pos & pos)
0067     {
0068         const QString str = "Pos[" + pos.convertTo(tikz::Unit::Point).toString() + "]";
0069         const QByteArray ba = str.toLatin1();
0070         return qstrdup(ba.data());
0071     }
0072 }
0073 
0074 // kate: indent-width 4; replace-tabs on;