1 #ifndef __FONT_LIST_H__
2 #define __FONT_LIST_H__
5 * KFontInst - KDE Font Installer
7 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
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.
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>
40 #include "FontLister.h"
41 #include "JobRunner.h"
42 #include "FontFilter.h"
44 #include "DisabledFonts.h"
54 #define KFI_FONT_DRAG_MIME "kfontinst/fontlist"
73 class CFontList
: public QAbstractItemModel
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);
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;
120 void status(const QString
&str
);
127 void listingCompleted();
128 void newItems(const KFileItemList
&items
);
130 void deleteItems(const KFileItemList
&items
);
131 void renameItems(const RenameList
&items
);
135 void addItem(const KFileItem
&item
);
136 CFamilyItem
* findFamily(const QString
&familyName
, bool create
=false);
137 CFontItem
* findFont(const KUrl
&url
);
138 void touchThumbnails();
142 QList
<CFamilyItem
*> itsFamilies
;
143 QHash
<KUrl
, CFontItem
*> itsFonts
; // Use for quick searching...
144 CFontLister
*itsLister
;
148 static int theirPreviewSize
;
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;
167 CFontModelItem
*itsParent
;
171 class CFamilyItem
: public CFontModelItem
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
);
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;
207 bool usable(const CFontItem
*font
, bool root
);
213 QList
<CFontItem
*> itsFonts
;
217 CFontItem
*itsRegularFont
; // 'RegularFont' is font nearest to 'Regular' style, and used for previews.
218 CFontList
&itsParent
;
221 class CFontItem
: public CFontModelItem
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
; }
256 QPixmap
*itsPixmap
[2];
257 quint32 itsStyleInfo
;
260 CDisabledFonts::TFileList itsFiles
;
261 qulonglong itsWritingSystems
;
262 KIO::filesize_t itsSize
;
265 class CFontListSortFilterProxy
: public QSortFilterProxyModel
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
; }
298 QString
filterText() const { return CFontFilter::CRIT_FONTCONFIG
==itsFilterCriteria
299 ? (itsFcQuery
? itsFcQuery
->font() : QString()) : itsFilterText
; }
303 CGroupListItem
*itsGroup
;
304 QString itsFilterText
;
305 CFontFilter::ECriteria itsFilterCriteria
;
306 qulonglong itsFilterWs
;
308 CFcQuery
*itsFcQuery
;
311 class CFontListView
: public QTreeView
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();
339 void fontsDropped(const QSet
<KUrl
> &);
340 void itemSelected(const QModelIndex
&, bool en
, bool dis
);
346 void refreshFilter();
347 void filterText(const QString
&text
);
348 void filterCriteria(int crit
, qulonglong ws
);
352 void setSortColumn(int col
);
353 void selectionChanged(const QItemSelection
&selected
, const QItemSelection
&deselected
);
354 void itemCollapsed(const QModelIndex
&index
);
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
);
368 CFontListSortFilterProxy
*itsProxy
;
372 QAction
*itsDeleteAct
,