1 /***************************************************************************
2 * Copyright (C) 2003-2006 David Saxton <david@bluehaze.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
9
10 #ifndef ITEMLIBRARY_H
11 #define ITEMLIBRARY_H
12
13 #include <KLocalizedString>
14
15 #include <QColor>
16 #include <QObject>
17 #include <QMap>
18
19 class Component;
20 class Document;
21 class Item;
22 class ItemDocument;
23 class ItemLibrary;
24 class LibraryItem;
25 inline ItemLibrary* itemLibrary();
26
27 typedef QMap< QString, QString > QStringMap;
28 typedef QMap< QString, QStringMap > QStringMapMap;
29 typedef QMap< QString, QImage > ImageMap;
30 typedef QList<LibraryItem*> LibraryItemList;
31
32 /**
33 While the program is running, only one instance of this class is created.
34 You can get it by calling itemLibrary()
35 @short Holds the list of CNItems
36 @author David Saxton
37 */
38 class ItemLibrary : public QObject
39 {
40 Q_OBJECT
41 public:
42 ~ItemLibrary() override;
43 /**
44 * Returns a QPixmap of the item icon
45 */
46 QPixmap itemIconFull( const QString &id );
47 /**
48 * Append the given item into the library
49 */
50 void addLibraryItem( LibraryItem *item );
51 /**
52 * Returns a list of items in the library
53 */
items()54 LibraryItemList* items() { return &m_items; }
55 /**
56 * @return the LibraryItem for the item with the given type (id) const.
57 */
58 LibraryItem * libraryItem( QString type ) const;
59 /**
60 * Creates a new item with the given id, and returns a pointer to it
61 */
62 Item * createItem( const QString &id, ItemDocument * itemDocument, bool newItem, const char *newId = nullptr, bool finishCreation = true );
63 /**
64 * Returns an image of the given component. As QPixmap::toImage is
65 * a slow function, this will cache the result and return that for large
66 * images.
67 * @param component A pointer to the Component.
68 * @param maxSize The maximum size (in pixels) before the image is
69 * cached.
70 */
71 QImage componentImage( Component * component, const uint maxSize = 36000 );
72 /**
73 * Does similar to that above, but will not be able to return a description
74 * if there is none saved on file (instead of the above, which falls back to
75 * calling item->description()).
76 * @param type the id of the item.
77 * @param language the language code, e.g. "es".
78 */
79 QString description( QString type, const QString & language ) const;
80 /**
81 * @return if we have a description for the item in language.
82 */
83 bool haveDescription( QString type, const QString & language ) const;
84 /**
85 * Gives the type item the description.
86 * @param type the type of item this description is for.
87 * @param language the language code, e.g. "es".
88 * @return whether the descriptions file could be saved.
89 */
90 bool setDescription( QString type, const QString & description, const QString & language );
91 /**
92 * @return the directory containing the item descriptions. By default,
93 * this is something like "/usr/share/apps/ktechlab/contexthelp/". But
94 * can be changed by calling setDescriptionsDirectory.
95 */
96 QString itemDescriptionsDirectory() const;
97 /**
98 * Stores the item descriptions directory in the users config.
99 */
100 void setItemDescriptionsDirectory( QString dir );
101 /**
102 * @return the item description file for the given language.
103 */
104 QString itemDescriptionsFile( const QString & language ) const;
105 /**
106 * @return the string used for an empty item description - something like
107 * "The help for English does not yet exist..". This is created by inserting
108 * the current language name into m_emptyItemDescription;
109 */
110 QString emptyItemDescription( const QString & language ) const;
111 /**
112 * @return the list of language-codes that have item descriptions.
113 */
descriptionLanguages()114 QStringList descriptionLanguages() const { return m_itemDescriptions.keys(); }
115
116 protected:
117 /**
118 * Saves the item descriptions to the file specified in the config.
119 * @return whether successful (e.g. if the file could be opened for
120 * writing).
121 */
122 bool saveDescriptions( const QString & language );
123 void loadItemDescriptions();
124 void addComponents();
125 void addFlowParts();
126 void addMechanics();
127 void addDrawParts();
128
129 ItemLibrary();
130
131 LibraryItemList m_items;
132 ImageMap m_imageMap;
133 QStringMapMap m_itemDescriptions; // (Language, type) <--> description
134 static KLocalizedString m_emptyItemDescription; // Description template for when a description does not yet exist
135
136 friend ItemLibrary * itemLibrary();
137 };
138
itemLibrary()139 inline ItemLibrary* itemLibrary()
140 {
141 // are we really sure we aren't calling new over and over again?
142 static ItemLibrary *_itemLibrary = new ItemLibrary();
143 return _itemLibrary;
144 }
145
146 #endif
147