not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kfontinst / kcmfontinst / FontList.h
blob913485afa0ff011c109b4375941bb47f7fa91735
1 #ifndef __FONT_LIST_H__
2 #define __FONT_LIST_H__
4 /*
5 * KFontInst - KDE Font Installer
7 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
9 * ----
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
27 #include <KDE/KUrl>
28 #include <KDE/KFileItem>
29 #include <KDE/KIO/Job>
30 #include <QtCore/QList>
31 #include <QtCore/QSet>
32 #include <QtCore/QHash>
33 #include <QtGui/QTreeView>
34 #include <QtCore/QAbstractItemModel>
35 #include <QtCore/QModelIndex>
36 #include <QtCore/QVariant>
37 #include <QtGui/QSortFilterProxyModel>
38 #include <QtGui/QFontDatabase>
39 #include "Misc.h"
40 #include "FontLister.h"
41 #include "JobRunner.h"
42 #include "FontFilter.h"
43 #include "FcQuery.h"
44 #include "DisabledFonts.h"
46 class KConfigGroup;
47 class KFileItem;
48 class KFileItemList;
49 class QMenu;
50 class QPixmap;
51 class QMimeData;
52 class QTimer;
54 #define KFI_FONT_DRAG_MIME "kfontinst/fontlist"
56 namespace KFI
59 class CFontItem;
60 class CFamilyItem;
61 class CGroupListItem;
62 class CFcEngine;
64 enum EColumns
66 COL_FONT,
67 COL_STATUS,
68 COL_PREVIEW,
70 NUM_COLS
73 class CFontList : public QAbstractItemModel
75 Q_OBJECT
77 public:
79 static const int constDefaultPreviewSize=25;
81 static QStringList compact(const QStringList &fonts);
82 static void setPreviewSize(int s) { theirPreviewSize=s; }
83 static int previewSize() { return theirPreviewSize; }
85 CFontList(CFcEngine *eng, QWidget *parent=0);
86 ~CFontList();
88 QVariant data(const QModelIndex &index, int role) const;
89 Qt::ItemFlags flags(const QModelIndex &index) const;
90 Qt::DropActions supportedDropActions() const;
91 QMimeData * mimeData(const QModelIndexList &indexes) const;
92 QStringList mimeTypes() const;
93 QVariant headerData(int section, Qt::Orientation orientation,
94 int role = Qt::DisplayRole) const;
95 QModelIndex index(int row, int column,
96 const QModelIndex &parent = QModelIndex()) const;
97 QModelIndex parent(const QModelIndex &index) const;
98 int rowCount(const QModelIndex &parent = QModelIndex()) const;
99 int columnCount(const QModelIndex &parent = QModelIndex()) const;
100 void scan() { itsLister->scan(); }
101 void setAutoUpdate(bool on) { itsLister->setAutoUpdate(on); }
102 bool active() const { return !itsLister->busy(); }
103 int row(const CFamilyItem *fam) const { return itsFamilies.indexOf((CFamilyItem *)fam); }
104 void forceNewPreviews();
105 const QList<CFamilyItem *> & families() const { return itsFamilies; }
106 QModelIndex createIndex(int row, int column, void *data = 0) const
107 { return QAbstractItemModel::createIndex(row, column, data); }
108 bool hasFamily(const QString &family) { return NULL!=findFamily(family, false); }
109 void refresh(bool allowSys, bool allowUser);
110 void setAllowDisabled(bool on);
111 bool allowSys() const { return itsAllowSys; }
112 bool allowUser() const { return itsAllowUser; }
113 bool allowDisabled() const { return itsAllowDisabled; }
114 void getFamilyStats(QSet<QString> &enabled, QSet<QString> &disabled, QSet<QString> &partial);
115 void getFoundries(QSet<QString> &foundries) const;
116 QString whatsThis() const;
118 Q_SIGNALS:
120 void status(const QString &str);
121 void percent(int p);
122 void started();
123 void finished();
125 private Q_SLOTS:
127 void listingCompleted();
128 void newItems(const KFileItemList &items);
129 void clearItems();
130 void deleteItems(const KFileItemList &items);
131 void renameItems(const RenameList &items);
133 private:
135 void addItem(const KFileItem &item);
136 CFamilyItem * findFamily(const QString &familyName, bool create=false);
137 CFontItem * findFont(const KUrl &url);
138 void touchThumbnails();
140 private:
142 QList<CFamilyItem *> itsFamilies;
143 QHash<KUrl, CFontItem *> itsFonts; // Use for quick searching...
144 CFontLister *itsLister;
145 bool itsAllowSys,
146 itsAllowUser,
147 itsAllowDisabled;
148 static int theirPreviewSize;
151 class CFontModelItem
153 public:
155 CFontModelItem(CFontModelItem *p) : itsParent(p), itsIsSystem(false) { }
156 virtual ~CFontModelItem() { }
158 CFontModelItem * parent() const { return itsParent; }
159 bool isFamily() const { return NULL==itsParent; }
160 bool isFont() const { return NULL!=itsParent; }
161 bool isSystem() const { return itsIsSystem; }
162 void setIsSystem(bool sys) { itsIsSystem=sys; }
163 virtual int rowNumber() const = 0;
165 protected:
167 CFontModelItem *itsParent;
168 bool itsIsSystem;
171 class CFamilyItem : public CFontModelItem
173 public:
175 enum EStatus
177 ENABLED,
178 PARTIAL,
179 DISABLED
182 CFamilyItem(CFontList &p, const QString &n);
183 virtual ~CFamilyItem();
185 bool operator==(const CFamilyItem &other) const { return itsName==other.itsName; }
187 void touchThumbnail();
188 const QString & name() const { return itsName; }
189 const QString & icon() const { return itsIcon; }
190 const QList<CFontItem *> & fonts() const { return itsFonts; }
191 void addFont(CFontItem *font);
192 void removeFont(CFontItem *font);
193 void refresh();
194 bool updateStatus();
195 bool updateRegularFont(CFontItem *font);
196 CFontItem * findFont(const KFileItem &i);
197 int rowNumber() const { return itsParent.row(this); }
198 int row(const CFontItem *font) const { return itsFonts.indexOf((CFontItem *)font); }
199 EStatus status() const { return itsStatus; }
200 EStatus realStatus() const { return itsRealStatus; }
201 CFontItem * regularFont() { return itsRegularFont; }
202 int fontCount() const { return itsFontCount; }
203 void getFoundries(QSet<QString> &foundries) const;
205 private:
207 bool usable(const CFontItem *font, bool root);
209 private:
211 QString itsName,
212 itsIcon;
213 QList<CFontItem *> itsFonts;
214 int itsFontCount;
215 EStatus itsStatus,
216 itsRealStatus;
217 CFontItem *itsRegularFont; // 'RegularFont' is font nearest to 'Regular' style, and used for previews.
218 CFontList &itsParent;
221 class CFontItem : public CFontModelItem
223 public:
225 CFontItem(CFontModelItem *p, const KFileItem &item, const QString &style=QString());
226 virtual ~CFontItem() { }
228 void touchThumbnail();
229 const QString & name() const { return itsName; }
230 QString mimetype() const { return itsMimeType; }
231 bool isEnabled() const { return itsEnabled; }
232 bool isHidden() const { return !itsEnabled; }
233 void setUrl(const KUrl &url);
234 KUrl url() const { return itsUrl; }
235 bool isBitmap() const { return itsBitmap; }
236 const QString & fileName() const { return itsFileName; }
237 const QString & style() const { return itsStyle; }
238 quint32 styleInfo() const { return itsStyleInfo; }
239 int index() const { return itsIndex; }
240 const QString & family() const { return (static_cast<CFamilyItem *>(parent()))->name(); }
241 const QPixmap * pixmap(bool selected, bool force=false);
242 void clearPixmap() { itsPixmap[0]=itsPixmap[1]=NULL; }
243 int rowNumber() const { return (static_cast<CFamilyItem *>(parent()))->row(this); }
244 const CDisabledFonts::TFileList & files() const { return itsFiles; }
245 KIO::filesize_t size() const { return itsSize; }
246 qulonglong writingSystems() const { return itsWritingSystems; }
248 private:
250 KUrl itsUrl;
251 QString itsName,
252 itsFileName,
253 itsStyle,
254 itsMimeType;
255 int itsIndex;
256 QPixmap *itsPixmap[2];
257 quint32 itsStyleInfo;
258 bool itsBitmap,
259 itsEnabled;
260 CDisabledFonts::TFileList itsFiles;
261 qulonglong itsWritingSystems;
262 KIO::filesize_t itsSize;
265 class CFontListSortFilterProxy : public QSortFilterProxyModel
267 Q_OBJECT
269 public:
271 CFontListSortFilterProxy(QObject *parent, QAbstractItemModel *model);
272 virtual ~CFontListSortFilterProxy() { }
274 QVariant data(const QModelIndex &idx, int role) const;
275 bool acceptFont(CFontItem *fnt, bool checkFontText) const;
276 bool acceptFamily(CFamilyItem *fam) const;
277 bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
278 bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
279 void setFilterGroup(CGroupListItem *grp);
280 CGroupListItem * filterGroup() { return itsGroup; }
282 void setFilterText(const QString &text);
283 void setFilterCriteria(CFontFilter::ECriteria crit, qulonglong ws);
284 void setMgtMode(bool on);
285 bool mgtMode() const { return itsMgtMode; }
287 private Q_SLOTS:
289 void timeout();
290 void fcResults();
292 Q_SIGNALS:
294 void refresh();
296 private:
298 QString filterText() const { return CFontFilter::CRIT_FONTCONFIG==itsFilterCriteria
299 ? (itsFcQuery ? itsFcQuery->font() : QString()) : itsFilterText; }
300 private:
302 bool itsMgtMode;
303 CGroupListItem *itsGroup;
304 QString itsFilterText;
305 CFontFilter::ECriteria itsFilterCriteria;
306 qulonglong itsFilterWs;
307 QTimer *itsTimer;
308 CFcQuery *itsFcQuery;
311 class CFontListView : public QTreeView
313 Q_OBJECT
315 public:
317 CFontListView(QWidget *parent, CFontList *model);
318 virtual ~CFontListView() { }
320 void readConfig(KConfigGroup &cg);
321 void writeConfig(KConfigGroup &cg);
322 QModelIndexList selectedItems() const { return selectedIndexes(); }
323 void getFonts(CJobRunner::ItemList &urls, QStringList &fontNames, QSet<Misc::TFont> *fonts,
324 bool *hasSys, bool selected, bool getEnabled=true, bool getDisabled=true);
325 void getPrintableFonts(QSet<Misc::TFont> &items, bool selected);
326 void setFilterGroup(CGroupListItem *grp);
327 void stats(int &enabled, int &disabled, int &partial);
328 void selectedStatus(bool &enabled, bool &disabled);
329 QModelIndexList allFonts();
330 void setMgtMode(bool on);
331 void selectFirstFont();
333 Q_SIGNALS:
335 void del();
336 void print();
337 void enable();
338 void disable();
339 void fontsDropped(const QSet<KUrl> &);
340 void itemSelected(const QModelIndex &, bool en, bool dis);
341 void refresh();
342 void reload();
344 public Q_SLOTS:
346 void refreshFilter();
347 void filterText(const QString &text);
348 void filterCriteria(int crit, qulonglong ws);
350 private Q_SLOTS:
352 void setSortColumn(int col);
353 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
354 void itemCollapsed(const QModelIndex &index);
355 void view();
357 private:
359 QModelIndexList allIndexes();
360 void startDrag(Qt::DropActions supportedActions);
361 void dragEnterEvent(QDragEnterEvent *event);
362 void dropEvent(QDropEvent *event);
363 void contextMenuEvent(QContextMenuEvent *ev);
364 virtual bool viewportEvent(QEvent *event);
366 private:
368 CFontListSortFilterProxy *itsProxy;
369 CFontList *itsModel;
370 QMenu *itsStdMenu,
371 *itsMgtMenu;
372 QAction *itsDeleteAct,
373 *itsEnableAct,
374 *itsDisableAct,
375 *itsPrintAct,
376 *itsViewAct;
377 bool itsAllowDrops;
382 #endif