/tellico-2.3.5/src/field.h

# · C Header · 333 lines · 91 code · 17 blank · 225 comment · 0 complexity · 63af6e8f4a6310caa900d19978c72209 MD5 · raw file

  1. /***************************************************************************
  2. Copyright (C) 2001-2009 Robby Stephenson <robby@periapsis.org>
  3. ***************************************************************************/
  4. /***************************************************************************
  5. * *
  6. * This program is free software; you can redistribute it and/or *
  7. * modify it under the terms of the GNU General Public License as *
  8. * published by the Free Software Foundation; either version 2 of *
  9. * the License or (at your option) version 3 or any later version *
  10. * accepted by the membership of KDE e.V. (or its successor approved *
  11. * by the membership of KDE e.V.), which shall act as a proxy *
  12. * defined in Section 14 of version 3 of the license. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  21. * *
  22. ***************************************************************************/
  23. #ifndef TELLICO_FIELD_H
  24. #define TELLICO_FIELD_H
  25. #include "datavectors.h"
  26. #include "fieldformat.h"
  27. #include <QStringList>
  28. #include <QRegExp>
  29. namespace Tellico {
  30. namespace Data {
  31. /**
  32. * The Field class encapsulates all the possible properties of a entry.
  33. *
  34. * A field can be one of eleven types. It has a name, a title, and a category,
  35. * along with some flags characterizing certain properties
  36. *
  37. * @author Robby Stephenson
  38. */
  39. class Field : public QSharedData {
  40. public:
  41. /**
  42. * The possible field types. A Line is represented by a KLineEdit,
  43. * a Para is a QMultiLineEdit encompassing multiple lines, a Choice is
  44. * limited to set values shown in a KComboBox, and a Bool is either true
  45. * or not and is thus a QCheckBox. A Number type is an integer, though it used
  46. * to be a Year. A URL is obvious, too.
  47. * A Table looks like a small spreadsheet with one column, and a Table2
  48. * type has two columns. An Image points to a QImage. A Date contains a date.
  49. *
  50. * Table2, ReadOnly, and Dependent are deprecated
  51. *
  52. * Don't forget to change Field::typeMap().
  53. **/
  54. enum Type {
  55. Undef = 0,
  56. Line = 1,
  57. Para = 2,
  58. Choice = 3,
  59. Bool = 4,
  60. ReadOnly = 5, // deprecated in favor of FieldFlags::NoEdit
  61. Number = 6,
  62. URL = 7,
  63. Table = 8,
  64. Table2 = 9, // deprecated in favor of property("columns")
  65. Image = 10,
  66. Dependent = 11, // deprecated in favor of FieldFlags::Derived
  67. Date = 12,
  68. // Michael Zimmermann used 13 for his Keyword field, so go ahead and skip it
  69. Rating = 14 // similar to a Choice field, but allowed values are numbers only
  70. // if you add your own field type, best to start at a high number
  71. // say 100, to ensure uniqueness
  72. };
  73. typedef QMap<Field::Type, QString> FieldMap;
  74. /**
  75. * The field flags. The properties should be bit-wise OR'd together.
  76. *
  77. * @li AllowCompletion - Include a completion object in the lineedit.
  78. * @li AllowMultiple - Multiple values are allowed in one field and are
  79. * separated by a semi-colon (";").
  80. * @li AllowGrouped - Entries may be grouped by this field.
  81. * @li NoDelete - The user may not delete this field.
  82. */
  83. enum FieldFlag {
  84. AllowMultiple = 1 << 0, // allow multiple values, separated by a semi-colon
  85. AllowGrouped = 1 << 1, // this field can be used to group entries
  86. AllowCompletion = 1 << 2, // allow auto-completion
  87. NoDelete = 1 << 3, // don't allow user to delete this field
  88. NoEdit = 1 << 4, // don't allow user to edit this field
  89. Derived = 1 << 5 // dependent value
  90. };
  91. /**
  92. * The field formatting flags.
  93. *
  94. * @li FormatTitle - The field should be formatted as a title
  95. * @li FormatName - The field should be formatted as a personal name
  96. * @li FormatDate - The field should be formatted as a date.
  97. * @li FormatPlain - The field only be formatted with capitalization.
  98. * @li FormatNone - The field should not be formatted.
  99. */
  100. enum FormatFlag {
  101. FormatPlain = 0, // format plain, allows capitalization
  102. FormatTitle = 1, // format as a title, i.e. shift articles to end
  103. FormatName = 2, // format as a personal full name
  104. FormatDate = 3, // format as a date
  105. FormatNone = 4 // no format, i.e. no capitalization allowed
  106. };
  107. /**
  108. * The constructor for all types except Choice. The default type is Line.
  109. * By default, the field category is set to "General", and should be modified
  110. * using the @ref setCategory() method.
  111. *
  112. * @param name The field name
  113. * @param title The field title
  114. * @param type The field type
  115. */
  116. Field(const QString& name, const QString& title, Type type = Line);
  117. /**
  118. * The constructor for Choice types attributes.
  119. * By default, the field category is set to "General", and should be modified
  120. * using the @ref setCategory() method.
  121. *
  122. * @param name The field name
  123. * @param title The field title
  124. * @param allowed The allowed values of the field
  125. */
  126. Field(const QString& name, const QString& title, const QStringList& allowed);
  127. /**
  128. * The copy constructor
  129. */
  130. Field(const Field& field);
  131. /**
  132. * The assignment operator
  133. */
  134. Field& operator=(const Field& field);
  135. /**
  136. * Destructor
  137. */
  138. ~Field();
  139. /**
  140. * Returns the name of the field.
  141. *
  142. * @return The field name
  143. */
  144. const QString& name() const { return m_name; }
  145. /**
  146. * Sets the name of the field. This should only be changed before the field is added
  147. * to a collection, i.e. before any entries use it, etc.
  148. *
  149. * @param name The field name
  150. */
  151. void setName(const QString& name) { m_name = name; }
  152. /**
  153. * Returns the title of the field.
  154. *
  155. * @return The field title
  156. */
  157. const QString& title() const { return m_title; }
  158. /**
  159. * Sets the title of the field.
  160. *
  161. * @param title The field title
  162. */
  163. void setTitle(const QString& title);
  164. /**
  165. * Returns the category of the field.
  166. *
  167. * @return The field category
  168. */
  169. const QString& category() const { return m_category; }
  170. /**
  171. * Sets the category of the field.
  172. *
  173. * @param category The field category
  174. */
  175. void setCategory(const QString& category);
  176. /**
  177. * Returns the name of the field.
  178. *
  179. * @return The field name
  180. */
  181. const QStringList& allowed() const { return m_allowed; }
  182. /**
  183. * Sets the allowed values of the field.
  184. *
  185. * @param allowed The allowed values
  186. */
  187. void setAllowed(const QStringList& allowed) { m_allowed = allowed; }
  188. /**
  189. * Add a value to the allowed list
  190. *
  191. * @param value The value to allow
  192. */
  193. void addAllowed(const QString& value);
  194. /**
  195. * Returns the type of the field.
  196. *
  197. * @return The field type
  198. */
  199. Type type() const { return m_type; }
  200. /**
  201. * Sets the type of the field. Be careful with this!
  202. *
  203. * @param type The field type
  204. */
  205. void setType(Type type);
  206. /**
  207. * Returns the flags for the field.
  208. *
  209. * @return The field flags
  210. */
  211. int flags() const { return m_flags; }
  212. /**
  213. * Sets the flags of the field. The value is
  214. * set to the argument, so old flags are effectively removed.
  215. *
  216. * @param flags The field flags
  217. */
  218. void setFlags(int flags);
  219. bool hasFlag(FieldFlag flag) const;
  220. /**
  221. * Returns the formatting flag for the field.
  222. *
  223. * @return The format flag
  224. */
  225. FieldFormat::Type formatType() const { return m_formatType; }
  226. /**
  227. * Sets the formatting flag of the field.
  228. *
  229. * @param flag The field flag
  230. */
  231. void setFormatType(FieldFormat::Type flag);
  232. /**
  233. * Returns the description for the field.
  234. *
  235. * @return The field description
  236. */
  237. const QString& description() const { return m_desc; }
  238. /**
  239. * Sets the description of the field.
  240. *
  241. * @param desc The field description
  242. */
  243. void setDescription(const QString& desc) { m_desc = desc; }
  244. /**
  245. * Returns the default value for the field.
  246. *
  247. * @return The field default value
  248. */
  249. QString defaultValue() const;
  250. /**
  251. * Sets the default value of the field.
  252. *
  253. * @param value The field default value
  254. */
  255. void setDefaultValue(const QString& value);
  256. /**
  257. * Some attributes are always a category by themselves.
  258. *
  259. * @return Whether the field is th eonly member of its category
  260. */
  261. bool isSingleCategory() const;
  262. /**
  263. * Extends a field with an additional key and value property pair.
  264. *
  265. * @param key The property key
  266. * @param value The property value
  267. */
  268. void setProperty(const QString& key, const QString& value);
  269. /**
  270. * Sets all the extended properties. Any existing ones get erased.
  271. *
  272. * @param properties The property list
  273. */
  274. void setPropertyList(const StringMap& properties);
  275. /**
  276. * Return a property value.
  277. *
  278. * @param key The property key
  279. * @returnThe property value
  280. */
  281. QString property(const QString& key) const;
  282. /**
  283. * Return the list of properties.
  284. *
  285. * @return The property list
  286. */
  287. const StringMap& propertyList() const { return m_properties; }
  288. /*************************** STATIC **********************************/
  289. /**
  290. * Returns a mapping of the FieldType enum to translated titles for the types.
  291. */
  292. static FieldMap typeMap();
  293. /**
  294. * Returns a list of the titles of the field types.
  295. */
  296. static QStringList typeTitles();
  297. /**
  298. * reset if the field is a rating field used for syntax version 7 and earlier
  299. */
  300. static void convertOldRating(Data::FieldPtr field);
  301. private:
  302. static QRegExp s_delimiter;
  303. QString m_name;
  304. QString m_title;
  305. QString m_category;
  306. QString m_desc;
  307. Type m_type;
  308. QStringList m_allowed;
  309. int m_flags;
  310. FieldFormat::Type m_formatType;
  311. StringMap m_properties;
  312. };
  313. } // end namespace
  314. } // end namespace
  315. Q_DECLARE_METATYPE(Tellico::Data::FieldPtr)
  316. #endif