File indexing completed on 2024-04-28 05:08:22

0001 /***************************************************************************
0002     Copyright (C) 2001-2009 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_FIELD_H
0026 #define TELLICO_FIELD_H
0027 
0028 #include "datavectors.h"
0029 #include "fieldformat.h"
0030 
0031 #include <QStringList>
0032 
0033 namespace Tellico {
0034   namespace Data {
0035 
0036 /**
0037  * The Field class encapsulates all the possible properties of a entry.
0038  *
0039  * A field can be one of eleven types. It has a name, a title, and a category,
0040  * along with some flags characterizing certain properties
0041  *
0042  * @author Robby Stephenson
0043  */
0044 class Field : public QSharedData {
0045 public:
0046   /**
0047    * The possible field types. A Line is represented by a QLineEdit,
0048    * a Para is a QMultiLineEdit encompassing multiple lines, a Choice is
0049    * limited to set values shown in a KComboBox, and a Bool is either true
0050    * or not and is thus a QCheckBox. A Number type is an integer, though it used
0051    * to be a Year. A URL is obvious, too.
0052    * A Table looks like a small spreadsheet with one column, and a Table2
0053    * type has two columns.  An Image points to a QImage. A Date contains a date.
0054    *
0055    * Table2, ReadOnly, and Dependent are deprecated
0056    *
0057    * Don't forget to change Field::typeMap().
0058    **/
0059   enum Type {
0060     Undef      = 0,
0061     Line       = 1,
0062     Para       = 2,
0063     Choice     = 3,
0064     Bool       = 4,
0065     ReadOnly   = 5, // deprecated in favor of FieldFlags::NoEdit
0066     Number     = 6,
0067     URL        = 7,
0068     Table      = 8,
0069     Table2     = 9, // deprecated in favor of property("columns")
0070     Image      = 10,
0071     Dependent  = 11, // deprecated in favor of FieldFlags::Derived
0072     Date       = 12,
0073     // Michael Zimmermann used 13 for his Keyword field, so go ahead and skip it
0074     Rating     = 14 // similar to a Choice field, but allowed values are numbers only
0075     // if you add your own field type, best to start at a high number
0076     // say 100, to ensure uniqueness
0077   };
0078   typedef QMap<Field::Type, QString> FieldMap;
0079 
0080   /**
0081    * The field flags. The properties should be bit-wise OR'd together.
0082    *
0083    * @li AllowCompletion - Include a completion object in the lineedit.
0084    * @li AllowMultiple - Multiple values are allowed in one field and are
0085    *                     separated by a semi-colon (";").
0086    * @li AllowGrouped - Entries may be grouped by this field.
0087    * @li NoDelete - The user may not delete this field.
0088    */
0089   enum FieldFlag {
0090     AllowMultiple   = 1 << 0,   // allow multiple values, separated by a semi-colon
0091     AllowGrouped    = 1 << 1,   // this field can be used to group entries
0092     AllowCompletion = 1 << 2,   // allow auto-completion
0093     NoDelete        = 1 << 3,   // don't allow user to delete this field
0094     NoEdit          = 1 << 4,   // don't allow user to edit this field
0095     Derived         = 1 << 5    // dependent value
0096   };
0097 
0098   /**
0099    * The field formatting flags.
0100    *
0101    * @li FormatTitle - The field should be formatted as a title
0102    * @li FormatName - The field should be formatted as a personal name
0103    * @li FormatDate - The field should be formatted as a date.
0104    * @li FormatPlain - The field only be formatted with capitalization.
0105    * @li FormatNone - The field should not be formatted.
0106    */
0107   enum FormatFlag {
0108     FormatPlain     = 0,   // format plain, allows capitalization
0109     FormatTitle     = 1,   // format as a title, i.e. shift articles to end
0110     FormatName      = 2,   // format as a personal full name
0111     FormatDate      = 3,   // format as a date
0112     FormatNone      = 4    // no format, i.e. no capitalization allowed
0113   };
0114 
0115   /**
0116    * The constructor for all types except Choice. The default type is Line.
0117    * By default, the field category is set to "General", and should be modified
0118    * using the @ref setCategory() method.
0119    *
0120    * @param name The field name
0121    * @param title The field title
0122    * @param type The field type
0123    */
0124   Field(const QString& name, const QString& title, Type type = Line);
0125   /**
0126    * The constructor for Choice types attributes.
0127    * By default, the field category is set to "General", and should be modified
0128    * using the @ref setCategory() method.
0129    *
0130    * @param name The field name
0131    * @param title The field title
0132    * @param allowed The allowed values of the field
0133    */
0134   Field(const QString& name, const QString& title, const QStringList& allowed);
0135   /**
0136    * The copy constructor
0137    */
0138   Field(const Field& field);
0139   /**
0140    * The assignment operator
0141    */
0142   Field& operator=(const Field& field);
0143   /**
0144    * Destructor
0145    */
0146   ~Field();
0147 
0148   /**
0149    * Returns the name of the field.
0150    *
0151    * @return The field name
0152    */
0153   const QString& name() const { return m_name; }
0154   /**
0155    * Sets the name of the field. This should only be changed before the field is added
0156    * to a collection, i.e. before any entries use it, etc.
0157    *
0158    * @param name The field name
0159    */
0160   void setName(const QString& name) { m_name = name; }
0161   /**
0162    * Returns the title of the field.
0163    *
0164    * @return The field title
0165    */
0166   const QString& title() const { return m_title; }
0167   /**
0168    * Sets the title of the field.
0169    *
0170    * @param title The field title
0171    */
0172   void setTitle(const QString& title);
0173   /**
0174    * Returns the category of the field.
0175    *
0176    * @return The field category
0177    */
0178   const QString& category() const { return m_category; }
0179   /**
0180    * Sets the category of the field.
0181    *
0182    * @param category The field category
0183    */
0184   void setCategory(const QString& category);
0185   /**
0186    * Returns the name of the field.
0187    *
0188    * @return The field name
0189    */
0190   const QStringList& allowed() const { return m_allowed; }
0191   /**
0192    * Sets the allowed values of the field.
0193    *
0194    * @param allowed The allowed values
0195    */
0196   void setAllowed(const QStringList& allowed) { m_allowed = allowed; }
0197   /**
0198    * Add a value to the allowed list
0199    *
0200    * @param value The value to allow
0201    */
0202   void addAllowed(const QString& value);
0203   /**
0204    * Returns the type of the field.
0205    *
0206    * @return The field type
0207    */
0208   Type type() const { return m_type; }
0209   /**
0210    * Sets the type of the field. Be careful with this!
0211    *
0212    * @param type The field type
0213    */
0214   void setType(Type type);
0215   /**
0216    * Returns the flags for the field.
0217    *
0218    * @return The field flags
0219    */
0220   int flags() const { return m_flags; }
0221   /**
0222    * Sets the flags of the field. The value is
0223    * set to the argument, so old flags are effectively removed.
0224    *
0225    * @param flags The field flags
0226    */
0227   void setFlags(int flags);
0228   bool hasFlag(FieldFlag flag) const;
0229   /**
0230    * Returns the formatting flag for the field.
0231    *
0232    * @return The format flag
0233    */
0234   FieldFormat::Type formatType() const { return m_formatType; }
0235   /**
0236    * Sets the formatting flag of the field.
0237    *
0238    * @param flag The field flag
0239    */
0240   void setFormatType(FieldFormat::Type flag);
0241   /**
0242    * Returns the description for the field.
0243    *
0244    * @return The field description
0245    */
0246   const QString& description() const { return m_desc; }
0247   /**
0248    * Sets the description of the field.
0249    *
0250    * @param desc The field description
0251    */
0252   void setDescription(const QString& desc) { m_desc = desc; }
0253   /**
0254    * Returns the default value for the field.
0255    *
0256    * @return The field default value
0257    */
0258   QString defaultValue() const;
0259   /**
0260    * Sets the default value of the field.
0261    *
0262    * @param value The field default value
0263    */
0264   void setDefaultValue(const QString& value);
0265   /**
0266    * Some attributes are always a category by themselves.
0267    *
0268    * @return Whether the field is th eonly member of its category
0269    */
0270   bool isSingleCategory() const;
0271   /**
0272    * Extends a field with an additional key and value property pair.
0273    *
0274    * @param key The property key
0275    * @param value The property value
0276    */
0277   void setProperty(const QString& key, const QString& value);
0278   /**
0279    * Sets all the extended properties. Any existing ones get erased.
0280    *
0281    * @param properties The property list
0282    */
0283   void setPropertyList(const StringMap& properties);
0284   /**
0285    * Return a property value.
0286    *
0287    * @param key The property key
0288    * @returnThe property value
0289    */
0290   QString property(const QString& key) const;
0291   /**
0292    * Return the list of properties.
0293    *
0294    * @return The property list
0295    */
0296   const StringMap& propertyList() const { return m_properties; }
0297 
0298   /*************************** STATIC **********************************/
0299   /**
0300    * Returns a mapping of the FieldType enum to translated titles for the types.
0301    */
0302   static FieldMap typeMap();
0303   /**
0304    * Returns a list of the titles of the field types.
0305    */
0306   static QStringList typeTitles();
0307 
0308  /**
0309   * reset if the field is a rating field used for syntax version 7 and earlier
0310   */
0311   static void convertOldRating(Data::FieldPtr field);
0312 
0313   enum DefaultField {
0314     IDField,
0315     TitleField,
0316     CreatedDateField,
0317     ModifiedDateField,
0318     IsbnField,
0319     LccnField,
0320     PegiField,
0321     ImdbField,
0322     EpisodeField,
0323     ScreenshotField,
0324     FrontCoverField
0325   };
0326 
0327   static FieldPtr createDefaultField(DefaultField field);
0328 
0329 private:
0330   QString m_name;
0331   QString m_title;
0332   QString m_category;
0333   QString m_desc;
0334   Type m_type;
0335   QStringList m_allowed;
0336   int m_flags;
0337   FieldFormat::Type m_formatType;
0338   StringMap m_properties;
0339 };
0340 
0341   } // end namespace
0342 
0343   Data::FieldList listIntersection(const Data::FieldList& list1, const Data::FieldList& list2);
0344 } // end namespace
0345 
0346 Q_DECLARE_METATYPE(Tellico::Data::FieldPtr)
0347 
0348 #endif