File indexing completed on 2024-04-28 04:34:03

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013-2015 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 #ifndef TIKZ_H
0020 #define TIKZ_H
0021 
0022 #include "tikz_export.h"
0023 
0024 #include <QObject>
0025 #include <QString>
0026 #include <functional>
0027 #include <type_traits>
0028 
0029 namespace tikz {
0030 Q_NAMESPACE_EXPORT(TIKZKITCORE_EXPORT)
0031 
0032 /**
0033  * Template helper to convert strings to enum.
0034  *
0035  * You need to provide a template specialization as follows:
0036  * @code
0037  * template<>
0038  * Foo toEnum<Foo>(const std::string & str)
0039  * {
0040  *     // ...
0041  * }
0042  * @endcode
0043  */
0044 template <typename T>
0045 T toEnum(const QString & str)
0046 {
0047     static_assert(std::is_enum<T>::value,
0048                   "Only enums are allowed as toEnum<>() template specialization.");
0049     Q_ASSERT(false && "Missing template specialization for toEnum<>().");
0050     return T();
0051 }
0052 
0053 /**
0054  * Logging categories.
0055  * These categories are used by log() and the helper functions debug(), info()
0056  * warn() and error().
0057  */
0058 enum class LogType : int {
0059     Debug,
0060     Info,
0061     Warning,
0062     Error
0063 };
0064 
0065 /**
0066  * Set the logging function to @p logFunc.
0067  * @warning Make sure to unset the logging function with unsetLogFunction() before
0068  *          the function you set with setLogFunction() gets invalid or relies
0069  *          on invalid data.
0070  * @see unsetLogFunction()
0071  */
0072 TIKZKITCORE_EXPORT void setLogFunction(std::function<void(LogType, const QString &)> logFunc);
0073 
0074 /**
0075  * Reset the logging function to a simple call of qDebug().
0076  * @see setLogFunction()
0077  */
0078 TIKZKITCORE_EXPORT void unsetLogFunction();
0079 
0080 /**
0081  * Logging facilities.
0082  */
0083 TIKZKITCORE_EXPORT void log(LogType level, const QString & text);
0084 
0085 /**
0086  * Logging helper function that equals the call of log(LogType::Debug, text).
0087  */
0088 inline void debug(const QString & text)
0089 {
0090     log(LogType::Debug, text);
0091 }
0092 
0093 /**
0094  * Logging helper function that equals the call of log(LogType::Info, text).
0095  */
0096 inline void info(const QString & text)
0097 {
0098     log(LogType::Info, text);
0099 }
0100 
0101 /**
0102  * Logging helper function that equals the call of log(LogType::Warning, text).
0103  */
0104 inline void warn(const QString & text)
0105 {
0106     log(LogType::Warning, text);
0107 }
0108 
0109 /**
0110  * Logging helper function that equals the call of log(LogType::Error, text).
0111  */
0112 inline void error(const QString & text)
0113 {
0114     log(LogType::Error, text);
0115 }
0116 
0117 /**
0118  * Entity types.
0119  */
0120 enum class EntityType {
0121     Document  = 0,
0122     Style,
0123     Node,
0124     Path
0125 };
0126 Q_ENUM_NS(EntityType)
0127 
0128 /**
0129  * Converts the EntityType @p type to a string.
0130  */
0131 TIKZKITCORE_EXPORT QString toString(EntityType type);
0132 
0133 /**
0134  * Converts the string @p str to an EntityType.
0135  */
0136 TIKZKITCORE_EXPORT EntityType toEntityType(const QString & str);
0137 
0138 /**
0139  * Available units.
0140  */
0141 enum class Unit : int {
0142     Point,
0143     Millimeter,
0144     Centimeter,
0145     Inch
0146 };
0147 Q_ENUM_NS(Unit)
0148 
0149 /**
0150  * Convert the tikz::Unit @p unit to a QString.
0151  */
0152 TIKZKITCORE_EXPORT QString toString(tikz::Unit unit);
0153 
0154 /**
0155  * Convert the string @p unit to an enum tikz::Unit.
0156  */
0157 template<>
0158 TIKZKITCORE_EXPORT Unit toEnum<Unit>(const QString & unit);
0159 
0160 /**
0161  * TextAlignment, following the TikZ text alignment options.
0162  */
0163 enum class TextAlignment : int {
0164     NoAlign = 0,
0165     AlignLeft,
0166     AlignCenter,
0167     AlignRight,
0168     AlignJustify
0169     // TODO: add all types ?
0170 };
0171 Q_ENUM_NS(TextAlignment)
0172 
0173 /**
0174  * Convert the tikz::TextAlignment @p alignment to a QString.
0175  * @note This function creates TikZ compatible strings.
0176  */
0177 TIKZKITCORE_EXPORT QString toString(tikz::TextAlignment alignment);
0178 
0179 /**
0180  * Convert the string @p alignment to an enum tikz::TextAlignment.
0181  */
0182 template<>
0183 TIKZKITCORE_EXPORT TextAlignment toEnum<TextAlignment>(const QString & alignment);
0184 
0185 /**
0186  * Supported TikZ Shapes.
0187  */
0188 enum class Shape : int {
0189     NoShape = 0,
0190     ShapeRectangle,
0191     ShapeCircle,
0192     ShapeDiamond,
0193     ShapeEllipse
0194 };
0195 Q_ENUM_NS(Shape)
0196 
0197 /**
0198  * Convert the tikz::Shape @p shape to a QString.
0199  * @note This function creates TikZ compatible strings.
0200  */
0201 TIKZKITCORE_EXPORT QString toString(tikz::Shape shape);
0202 
0203 /**
0204  * Convert the string @p shape to an enum tikz::Shape.
0205  */
0206 template<>
0207 TIKZKITCORE_EXPORT Shape toEnum<Shape>(const QString & shape);
0208 
0209 /**
0210  * Supported TikZ pen styles.
0211  */
0212 enum class PenStyle : int {
0213     SolidLine = 0,
0214     DottedLine,
0215     DenselyDottedLine,
0216     LooselyDottedLine,
0217     DashedLine,
0218     DenselyDashedLine,
0219     LooselyDashedLine,
0220     DashDottedLine,
0221     DenselyDashDottedLine,
0222     LooselyDashDottedLine,
0223     DashDotDottedLine,
0224     DenselyDashDotDottedLine,
0225     LooselyDashDotDottedLine
0226 };
0227 Q_ENUM_NS(PenStyle)
0228 
0229 /**
0230  * Convert @p penStyle to a QString.
0231  * Examples are e.g. "solid", "densely dashed", ...
0232  * @note This function creates TikZ compatible strings.
0233  */
0234 TIKZKITCORE_EXPORT QString toString(tikz::PenStyle penStyle);
0235 
0236 /**
0237  * Convert @p penStyle to a QString.
0238  * Examples are e.g. "solid", "densely dashed", ...
0239  */
0240 template<>
0241 TIKZKITCORE_EXPORT PenStyle toEnum<PenStyle>(const QString & penStyle);
0242 
0243 enum class LineCap : int {
0244     CapUnset = 0,
0245     RoundCap,
0246     RectCap,
0247     ButtCap     // TikZ default
0248 };
0249 Q_ENUM_NS(LineCap)
0250 
0251 enum class LineJoin : int {
0252     JoinUnset = 0,
0253     RoundJoin,
0254     BevelJoin,
0255     MiterJoin    // TikZ default
0256 };
0257 Q_ENUM_NS(LineJoin)
0258 
0259 enum class Arrow : int {
0260     NoArrow = 0,
0261     /**
0262      * Default TikZ arrows.
0263      */
0264     ToArrow,
0265     ReversedToArrow,
0266     StealthArrow,
0267     ReversedStealthArrow,
0268     LatexArrow,
0269     ReversedLatexArrow,
0270     PipeArrow,
0271     /**
0272      * tikzlibrary: arrows
0273      */
0274     StealthTickArrow,
0275     ReversedStealthTickArrow,
0276     ArrowCount
0277 };
0278 Q_ENUM_NS(Arrow)
0279 
0280 /**
0281  * Convert @p arrow to a QString.
0282  * @note This function creates TikZ compatible strings.
0283  */
0284 TIKZKITCORE_EXPORT QString toString(tikz::Arrow arrow);
0285 
0286 /**
0287  * Convert the string @p arrow to an enum tikz::Arrow.
0288  */
0289 template<>
0290 TIKZKITCORE_EXPORT Arrow toEnum<Arrow>(const QString & arrow);
0291 
0292 /**
0293  * Available path types.
0294  */
0295 enum class PathType : int {
0296     Invalid = 0,
0297     Line, // (a) -- (b)
0298     HVLine, // (a) -| (b)
0299     VHLine, // (a) |- (b)
0300     BendCurve, // (a) to[bend left=20, looseness=1.2] (b)
0301     InOutCurve, // (a) to[in=20, out=30] (b)
0302     BezierCurve, // (a) .. controls (b) and (c) .. (d)
0303     Ellipse, // (a) ellipse[x radius=1cm, y radius=2cm]
0304     Rectangle, // (a) rectangle (b)
0305     Grid // (a) grid (b)
0306 };
0307 Q_ENUM_NS(PathType)
0308 
0309 /**
0310  * Convert PathType @p type to a QString.
0311  */
0312 TIKZKITCORE_EXPORT QString toString(tikz::PathType type);
0313 
0314 /**
0315  * Convert the string @p type to an enum tikz::PathType.
0316  */
0317 template<>
0318 TIKZKITCORE_EXPORT PathType toEnum<PathType>(const QString & type);
0319 
0320 }
0321 
0322 #endif // TIKZ_H
0323 
0324 // kate: indent-width 4; replace-tabs on;