1 /***************************************************************************
2 * *
3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 3 of the License, or *
6 * (at your option) any later version. *
7 * *
8 ***************************************************************************/
9
10 #include "CustomFontModel.h"
11 #include "WulforUtil.h"
12
13 #if QT_VERSION >= 0x050000
14 #include <QtWidgets>
15 #else
16 #include <QtGui>
17 #endif
18
19 #include <QFontDialog>
20 #include <QList>
21 #include <QStringList>
22 #include <QColor>
23
24 #include "dcpp/ShareManager.h"
25
26 using namespace dcpp;
27
28 #include <set>
29
CustomFontModel(QObject * parent)30 CustomFontModel::CustomFontModel(QObject *parent)
31 : QAbstractItemModel(parent)
32 {
33 QList<QVariant> rootData;
34 rootData << tr("Description") << tr("Font");
35
36 rootItem = new CustomFontItem(rootData, NULL);
37
38 addNewFont(WS_APP_FONT, tr("Application"));
39 addNewFont(WS_CHAT_FONT, tr("Public Chat: Chat"));
40 addNewFont(WS_CHAT_ULIST_FONT, tr("Public Chat: Userlist"));
41 addNewFont(WS_CHAT_PM_FONT, tr("Private Chat"));
42
43 connect(this, SIGNAL(fontChanged(QString,QString)), WulforSettings::getInstance(), SIGNAL(fontChanged(QString,QString)));
44 }
45
~CustomFontModel()46 CustomFontModel::~CustomFontModel()
47 {
48 if (rootItem)
49 delete rootItem;
50 }
51
columnCount(const QModelIndex & parent) const52 int CustomFontModel::columnCount(const QModelIndex &parent) const
53 {
54 if (parent.isValid())
55 return static_cast<CustomFontItem*>(parent.internalPointer())->columnCount();
56 else
57 return rootItem->columnCount();
58 }
59
data(const QModelIndex & index,int role) const60 QVariant CustomFontModel::data(const QModelIndex &index, int role) const
61 {
62 if (!index.isValid())
63 return QVariant();
64
65 CustomFontItem *item = static_cast<CustomFontItem*>(index.internalPointer());
66
67 switch(role) {
68 case Qt::DisplayRole:
69 {
70 return item->data(index.column());
71 }
72 case Qt::FontRole:
73 {
74 QFont f;
75
76 if (!item->custom_font.isEmpty())
77 f.fromString(item->custom_font);
78 else
79 f.fromString(item->data(COLUMN_CUSTOM_FONT_DISP).toString());
80
81 return f;
82 }
83 case Qt::TextAlignmentRole:
84 case Qt::ForegroundRole:
85 case Qt::BackgroundColorRole:
86 case Qt::ToolTipRole:
87 case Qt::DecorationRole:
88 default:
89 break;
90 }
91
92 return QVariant();
93 }
94
headerData(int section,Qt::Orientation orientation,int role) const95 QVariant CustomFontModel::headerData(int section, Qt::Orientation orientation,
96 int role) const
97 {
98 QList<QVariant> rootData;
99 rootData << tr("Description") << tr("Font");
100
101 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
102 return rootData.at(section);
103
104 return QVariant();
105 }
106
index(int row,int column,const QModelIndex & parent) const107 QModelIndex CustomFontModel::index(int row, int column, const QModelIndex &parent)
108 const
109 {
110 if (!hasIndex(row, column, parent))
111 return QModelIndex();
112
113 CustomFontItem *parentItem;
114
115 if (!parent.isValid())
116 parentItem = rootItem;
117 else
118 parentItem = static_cast<CustomFontItem*>(parent.internalPointer());
119
120 CustomFontItem *childItem = parentItem->child(row);
121 if (childItem)
122 return createIndex(row, column, childItem);
123 else
124 return QModelIndex();
125 }
126
parent(const QModelIndex & index) const127 QModelIndex CustomFontModel::parent(const QModelIndex &index) const
128 {
129 if (!index.isValid())
130 return QModelIndex();
131
132 CustomFontItem *childItem = static_cast<CustomFontItem*>(index.internalPointer());
133 CustomFontItem *parentItem = childItem->parent();
134
135 if (parentItem == rootItem)
136 return QModelIndex();
137
138 return createIndex(parentItem->row(), 0, parentItem);
139 }
140
rowCount(const QModelIndex & parent) const141 int CustomFontModel::rowCount(const QModelIndex &parent) const
142 {
143 CustomFontItem *parentItem;
144 if (parent.column() > 0)
145 return 0;
146
147 if (!parent.isValid())
148 parentItem = rootItem;
149 else
150 parentItem = static_cast<CustomFontItem*>(parent.internalPointer());
151
152 return parentItem->childCount();
153 }
154
addNewFont(const QString & wkey,const QString & desc)155 void CustomFontModel::addNewFont(const QString &wkey, const QString &desc){
156 if (wkey.isEmpty() || desc.isEmpty())
157 return;
158
159 QString font_desc = WSGET(wkey.toUtf8().constData());
160 QFont f;
161
162 if (font_desc.isEmpty())
163 f = qApp->font();
164 else
165 f = f.fromString(font_desc)? f : qApp->font();
166
167 CustomFontItem *item = new CustomFontItem(QList<QVariant>() << desc << f.toString(), rootItem);
168 item->key = wkey;
169 item->custom_font = "";
170
171 rootItem->appendChild(item);
172
173 emit layoutChanged();
174 }
175
itemDoubleClicked(const QModelIndex & i)176 void CustomFontModel::itemDoubleClicked(const QModelIndex &i){
177 if (!(i.isValid() && i.internalPointer()))
178 return;
179
180 CustomFontItem *item = reinterpret_cast<CustomFontItem*>(i.internalPointer());
181
182 bool ok = false;
183
184 QFont f = QFontDialog::getFont(&ok);
185
186 if (ok){
187 item->custom_font = f.toString();
188 item->updateColumn(COLUMN_CUSTOM_FONT_DISP, item->custom_font);
189
190 emit layoutChanged();
191 }
192 }
193
ok()194 void CustomFontModel::ok(){
195 for (const auto &i : rootItem->childItems){
196 if (!i->custom_font.isEmpty()){
197 WSSET(i->key.toUtf8().constData(), i->custom_font);
198
199 emit fontChanged(i->key, i->custom_font);
200 }
201 }
202 }
203
CustomFontItem(const QList<QVariant> & data,CustomFontItem * parent)204 CustomFontItem::CustomFontItem(const QList<QVariant> &data, CustomFontItem *parent) :
205 itemData(data), parentItem(parent)
206 {
207 }
208
~CustomFontItem()209 CustomFontItem::~CustomFontItem()
210 {
211 qDeleteAll(childItems);
212 }
213
appendChild(CustomFontItem * item)214 void CustomFontItem::appendChild(CustomFontItem *item) {
215 childItems.append(item);
216 }
217
child(int row)218 CustomFontItem *CustomFontItem::child(int row) {
219 return childItems.value(row);
220 }
221
childCount() const222 int CustomFontItem::childCount() const {
223 return childItems.count();
224 }
225
columnCount() const226 int CustomFontItem::columnCount() const {
227 return itemData.count();
228 }
229
data(int column) const230 QVariant CustomFontItem::data(int column) const {
231 return itemData.value(column);
232 }
233
parent()234 CustomFontItem *CustomFontItem::parent() {
235 return parentItem;
236 }
237
row() const238 int CustomFontItem::row() const {
239 if (parentItem)
240 return parentItem->childItems.indexOf(const_cast<CustomFontItem*>(this));
241
242 return 0;
243 }
244
updateColumn(int column,QVariant var)245 void CustomFontItem::updateColumn(int column, QVariant var){
246 if (column > (itemData.size()-1))
247 return;
248
249 itemData[column] = var;
250 }
251