File indexing completed on 2024-04-28 05:08:22
0001 /*************************************************************************** 0002 Copyright (C) 2009-2020 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #ifndef TELLICO_FIELDFORMAT_H 0026 #define TELLICO_FIELDFORMAT_H 0027 0028 #include <QString> 0029 #include <QStringList> 0030 #include <QRegularExpression> 0031 0032 namespace Tellico { 0033 0034 class FieldFormat { 0035 public: 0036 /** 0037 * The field formatting flags. 0038 * 0039 * @li FormatTitle - The field should be formatted as a title 0040 * @li FormatName - The field should be formatted as a personal name 0041 * @li FormatDate - The field should be formatted as a date. 0042 * @li FormatPlain - The field only be formatted with capitalization. 0043 * @li FormatNone - The field should not be formatted. 0044 */ 0045 enum Type { 0046 FormatPlain = 0, // format plain, allows capitalization 0047 FormatTitle = 1, // format as a title, i.e. shift articles to end 0048 FormatName = 2, // format as a personal full name 0049 FormatDate = 3, // format as a date 0050 FormatNone = 4 // no format, i.e. no capitalization allowed 0051 }; 0052 enum Request { 0053 AsIsFormat, 0054 DefaultFormat, 0055 ForceFormat 0056 }; 0057 enum Option { 0058 FormatCapitalize = 1 << 0, 0059 FormatAuto = 1 << 1, 0060 SplitMultiple = 1 << 2 0061 }; 0062 Q_DECLARE_FLAGS(Options, Option) 0063 0064 /** 0065 * Splits a string into multiple values; 0066 * 0067 * @param string The string to be split 0068 */ 0069 enum SplitParsing { StringSplit, RegExpSplit, CommaRegExpSplit }; 0070 static QStringList splitValue(const QString& string, 0071 SplitParsing parsing = RegExpSplit); 0072 static QStringList splitRow(const QString& string); 0073 static QStringList splitTable(const QString& string); 0074 /** 0075 * Returns the delimiter used to split field values 0076 * 0077 * @return The delimiter string 0078 */ 0079 static QString delimiterString(); 0080 /** 0081 * Returns the delimiter used to split field values 0082 * 0083 * @return The delimiter regexp 0084 */ 0085 static QRegularExpression delimiterRegularExpression(); 0086 static QRegularExpression commaSplitRegularExpression(); 0087 static QString columnDelimiterString(); 0088 static QString rowDelimiterString(); 0089 static QString matchValueRegularExpression(const QString& value); 0090 0091 static QString fixupValue(const QString& value); 0092 /** 0093 * Return the key to be used for sorting titles 0094 */ 0095 static QString sortKeyTitle(const QString& title); 0096 0097 static void stripArticles(QString& value); 0098 0099 static QString format(const QString& value, Type type, Request req); 0100 0101 /** 0102 * A convenience function to format a string as a title. 0103 * At the moment, this means that some articles such as "the" are placed 0104 * at the end of the title. If autoCapitalize() is true, the title is capitalized. 0105 * 0106 * @param title The string to be formatted 0107 */ 0108 static QString title(const QString& title, Options options); 0109 /** 0110 * A convenience function to format a string as a personal name. 0111 * At the moment, this means that the name is split at the last white space, 0112 * and the last name is moved in front. If multiple=true, then the string 0113 * is split using a semi-colon (";"), and each string is formatted and then 0114 * joined back together. If autoCapitalize() is true, the names are capitalized. 0115 * 0116 * @param name The string to be formatted 0117 * @param multiple A boolean indicating if the string can contain multiple values 0118 */ 0119 static QString name(const QString& name, Options options); 0120 /** 0121 * A convenience function to format a string as a date. 0122 * 0123 * @param date The string to be formatted 0124 */ 0125 static QString date(const QString& date); 0126 /** 0127 * Helper method to fix capitalization. 0128 * 0129 * @param str String to fix 0130 */ 0131 static QString capitalize(QString str); 0132 }; 0133 0134 Q_DECLARE_OPERATORS_FOR_FLAGS(FieldFormat::Options) 0135 0136 } // namespace 0137 0138 #endif