1 #include "style.h"
2 #include "conf.h"
3 
4 #include <KColorButton>
5 #include <KLocalizedString>
6 
7 
8 namespace Dict {
9 
StyleItem(const QString & name)10 StyleItem::StyleItem(const QString &name)
11 	: QTreeWidgetItem(QStringList(name), QTreeWidgetItem::UserType), type(name)
12 {}
13 
setColorWidgets()14 void StyleItem::setColorWidgets()
15 {
16 	if (!treeWidget()) return;
17 
18 	KColorButton *text = new KColorButton(treeWidget());
19 	text->setFixedWidth(70);
20 	treeWidget()->setItemWidget(this, Text, text);
21 	connect(text, SIGNAL(changed (const QColor &)),
22 		SLOT(setTextColor(const QColor &)));
23 
24 	KColorButton *back = new KColorButton(treeWidget());
25 	back->setFixedWidth(70);
26 	treeWidget()->setItemWidget(this, Back, back);
27 	connect(back, SIGNAL(changed (const QColor &)),
28 		SLOT(setBackColor(const QColor &)));
29 
30 	if (type == "Link" || type == "Description") {
31 		back->setEnabled(false);
32 		back->setToolTip("Currently disabled, as it doesn't render correctly.");
33 	}
34 }
35 
setTextColor(const QColor & color)36 void StyleItem::setTextColor(const QColor &color)
37 {
38 	setForeground(Context, color);
39 }
40 
setBackColor(const QColor & color)41 void StyleItem::setBackColor(const QColor &color)
42 {
43 	setBackground(Context, color);
44 }
45 
style() const46 int StyleItem::style() const
47 {
48 	int style = NormalFont;
49 	if (bold()) style |= BoldFont;
50 	if (italic()) style |= ItalicFont;
51 	if (underline()) style |= UnderlineFont;
52 	return style;
53 }
54 
bold() const55 bool StyleItem::bold() const
56 {
57 	return (font(Context).bold());
58 }
59 
italic() const60 bool StyleItem::italic() const
61 {
62 	return (font(Context).italic());
63 }
64 
underline() const65 bool StyleItem::underline() const
66 {
67 	return (font(Context).underline());
68 }
69 
text() const70 QString StyleItem::text() const
71 {
72 	return foreground(Context).color().name();
73 }
74 
back() const75 QString StyleItem::back() const
76 {
77 	return background(Context).color().name();
78 }
79 
setCheckBoxes()80 void StyleItem::setCheckBoxes()
81 {
82 	setCheckState(Bold, (bold()) ? Qt::Checked : Qt::Unchecked);
83 	setCheckState(Italic, (italic()) ? Qt::Checked : Qt::Unchecked);
84 	setCheckState(Underline, (underline()) ? Qt::Checked : Qt::Unchecked);
85 }
86 
set(int style,const QString & text,const QString & back)87 void StyleItem::set(int style, const QString &text, const QString &back)
88 {
89 	setFlags(Qt::ItemIsEnabled);
90 
91 	QFont newfont = font(Context);
92 	newfont.setBold(style & BoldFont);
93 	newfont.setItalic(style & ItalicFont);
94 	newfont.setUnderline(style & UnderlineFont);
95 	setFont(Context, newfont);
96 	setCheckBoxes();
97 
98 	setTextColor(QColor(text));
99 	setBackColor(QColor(back));
100 	if (treeWidget()) {
101 		((KColorButton *)treeWidget()->itemWidget(this, Text))->setColor(QColor(text));
102 		((KColorButton *)treeWidget()->itemWidget(this, Back))->setColor(QColor(back));
103 	}
104 }
105 
setFonts(int column)106 void StyleItem::setFonts(int column)
107 {
108 	QFont newfont = font(Context);
109 	switch (column) {
110 	case Bold:
111 		newfont.setBold(!newfont.bold());
112 		break;
113 	case Italic:
114 		newfont.setItalic(!newfont.italic());
115 		break;
116 	case Underline:
117 		newfont.setUnderline(!newfont.underline());
118 		break;
119 	}
120 	setFont(Context, newfont);
121 	setCheckBoxes();
122 }
123 
124 
StyleConf(QWidget * parent)125 StyleConf::StyleConf(QWidget *parent)
126 	: QTreeWidget(parent)
127 {
128 	setRootIsDecorated(false);
129 
130 	QStringList headers;
131 	headers << i18n("Role") << "" << "" << ""
132 		<< i18n("Text") << i18n("Background");
133 	setHeaderLabels(headers);
134 
135 	headerItem()->setIcon(StyleItem::Bold, QIcon::fromTheme("format-text-bold"));
136 	headerItem()->setIcon(StyleItem::Italic, QIcon::fromTheme("format-text-italic"));
137 	headerItem()->setIcon(StyleItem::Underline, QIcon::fromTheme("format-text-underline"));
138 
139 	for (int i = StyleItem::Bold; i <= StyleItem::Underline; ++i)
140 		resizeColumnToContents(i);
141 
142 	connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
143 		SLOT(setFonts(QTreeWidgetItem *, int)));
144 }
145 
setFonts(QTreeWidgetItem * item,int column)146 void StyleConf::setFonts(QTreeWidgetItem *item, int column)
147 {
148 	((StyleItem *)item)->setFonts(column);
149 }
150 
setStyle(Style * style)151 void StyleConf::setStyle(Style *style)
152 {
153 	if (!topLevelItemCount())
154 		foreach (StyleItem *item, style->itemList()) {
155 			addTopLevelItem(item);
156 			item->setColorWidgets();
157 		}
158 	resizeColumnToContents(StyleItem::Text);
159 }
160 
161 
Style()162 Style::Style()
163 {
164 	QStringList type;
165 	type << i18n("Title") << i18n("Headword") << i18n("Abbreviation")
166 		<< i18n("Pronunciation") << i18n("Description") << i18n("Link");
167 	foreach (QString style, type)
168 		stylelist << new StyleItem(style);
169 }
170 
~Style()171 Style::~Style()
172 {
173 	foreach (StyleItem *item, stylelist)
174 		if (!item->treeWidget()) delete item;
175 }
176 
setDefaults()177 void Style::setDefaults()
178 {
179 	QPalette palette;
180 	QString text = palette.color(QPalette::Text).name();
181 	QString back = palette.color(QPalette::Base).name();
182 
183 	stylelist[Title]->set(StyleItem::NormalFont, back, "#6094CF");
184 	stylelist[Headword]->set(StyleItem::NormalFont, "#BF0000", back);
185 	stylelist[Abbreviation]->set(StyleItem::NormalFont, "#00892B", back);
186 	stylelist[Pronunciation]->set(StyleItem::NormalFont, "#555555", back);
187 	stylelist[Description]->set(StyleItem::NormalFont, text, back);
188 	stylelist[Link]->set(StyleItem::NormalFont, palette.color(QPalette::Link).name(), back);
189 }
190 
read()191 void Style::read()
192 {
193 	ConfGroup conf("Style");
194 
195 	setDefaults();
196 	foreach (StyleItem *item, stylelist) {
197 		QStringList style;
198 		style = conf.readEntry(item->context(), style);
199 		if (style.size() == 3)
200 			item->set(style[0].toInt(), style[1], style[2]);
201 	}
202 	stylefont = conf.readEntry("Font", QFont("Sans Serif"));
203 }
204 
write()205 void Style::write()
206 {
207 	ConfGroup conf("Style");
208 
209 	foreach (StyleItem *item, stylelist) {
210 		QStringList style;
211 		style << QString::number(item->style())
212 			<< item->text() << item->back();
213 		conf.writeEntry(item->context(), style);
214 	}
215 	conf.writeEntry("Font", stylefont);
216 }
217 
218 QString resource(const QString &resfile);
219 
stylesheet() const220 QString Style::stylesheet() const
221 {
222 	QPalette palette;
223 	QString text = palette.color(QPalette::Text).name();
224 	QString back = palette.color(QPalette::Base).name();
225 
226 	QStringList style;
227 	foreach (StyleItem *item, stylelist) {
228 		QString itemstyle;
229 		if (item->bold()) itemstyle += "font-weight: bold; ";
230 		if (item->italic()) itemstyle += "font-style: italic; ";
231 		itemstyle += QString("text-decoration: %1; ")
232 			.arg((item->underline() ? "underline" : "none"));
233 		if (item->text() != text)
234 			itemstyle += QString("color: %1; ").arg(item->text());
235 		if (item->back() != back)
236 			itemstyle += QString("background: %1; ").arg(item->back());
237 		style << itemstyle;
238 	}
239 	return resource("dikt.css")
240 		.arg(style[Title], style[Headword], style[Abbreviation],
241 		style[Pronunciation], style[Description], style[Link]);
242 }
243 
format(Context context) const244 QTextCharFormat Style::format(Context context) const
245 {
246 	StyleItem *item = stylelist[context];
247 	QTextCharFormat format;
248 	if (item->bold()) format.setFontWeight(QFont::Bold);
249 	format.setForeground(item->foreground(StyleItem::Context));
250 	return format;
251 }
252 
253 }
254