1
/* This file is part of the KDE libraries
2
Copyright (C) 2012 Dominik Haumann <dhaumann kde org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
Boston, MA 02110-1301, USA.
19
#include "katecolortreewidget.h"
21
#include "katecategorydrawer.h"
23
#include <QtGui/QStyledItemDelegate>
24
#include <QtGui/QPainter>
25
#include <QtGui/QHeaderView>
28
#include <kconfiggroup.h>
30
#include <kcolordialog.h>
31
#include <kcolorscheme.h>
32
#include <kcolorutils.h>
38
//BEGIN KateColorTreeItem
39
class KateColorTreeItem : public QTreeWidgetItem
42
KateColorTreeItem(const KateColorItem& colorItem, QTreeWidgetItem* parent = 0)
43
: QTreeWidgetItem(parent)
44
, m_colorItem(colorItem)
46
setText(0, m_colorItem.name);
47
if (!colorItem.whatsThis.isEmpty()) {
48
setData(1, Qt::WhatsThisRole, colorItem.whatsThis);
50
if (!colorItem.useDefault) {
51
setData(2, Qt::ToolTipRole, i18n("Use default color from the KDE color scheme"));
55
QColor color() const {
56
return m_colorItem.color;
59
void setColor(const QColor& c) {
60
m_colorItem.color = c;
63
QColor defaultColor() const {
64
return m_colorItem.defaultColor;
67
bool useDefaultColor() const {
68
return m_colorItem.useDefault;
71
void setUseDefaultColor(bool useDefault) {
72
m_colorItem.useDefault = useDefault;
73
QString tooltip = useDefault ? QString() : i18n("Use default color from the KDE color scheme");
74
setData(2, Qt::ToolTipRole, tooltip);
78
return m_colorItem.key;
81
KateColorItem colorItem() const {
86
KateColorItem m_colorItem;
88
//END KateColorTreeItem
91
//BEGIN KateColorTreeDelegate
92
class KateColorTreeDelegate : public QStyledItemDelegate
95
KateColorTreeDelegate(KateColorTreeWidget* widget)
96
: QStyledItemDelegate(widget)
101
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
102
QSize sh = QStyledItemDelegate::sizeHint(option, index);
103
if (!index.parent().isValid()) {
104
sh.rheight() += 2 * m_categoryDrawer.leftMargin();
106
sh.rheight() += m_categoryDrawer.leftMargin();
108
if (index.column() == 0) {
109
sh.rwidth() += m_categoryDrawer.leftMargin();
110
} else if (index.column() == 1) {
113
sh.rwidth() += m_categoryDrawer.leftMargin();
119
QRect fullCategoryRect(const QStyleOptionViewItem& option, const QModelIndex& index) const {
120
QModelIndex i = index;
121
if (i.parent().isValid()) {
125
QTreeWidgetItem* item = m_tree->itemFromIndex(i);
126
QRect r = m_tree->visualItemRect(item);
129
r.setLeft(m_categoryDrawer.leftMargin());
130
r.setWidth(m_tree->viewport()->width() - m_categoryDrawer.leftMargin() - m_categoryDrawer.rightMargin());
133
if (item->isExpanded() && item->childCount() > 0) {
134
const int childCount = item->childCount();
135
const int h = sizeHint(option, index.child(0, 0)).height();
136
r.setHeight(r.height() + childCount * h);
139
r.setTop(r.top() + m_categoryDrawer.leftMargin());
144
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
146
Q_ASSERT(index.isValid());
147
Q_ASSERT(index.column() >= 0 && index.column() <= 2);
149
//BEGIN: draw toplevel items
150
if (!index.parent().isValid()) {
151
QStyleOptionViewItem opt(option);
152
const QRegion cl = painter->clipRegion();
153
painter->setClipRect(opt.rect);
154
opt.rect = fullCategoryRect(option, index);
155
m_categoryDrawer.drawCategory(index, 0, opt, painter);
156
painter->setClipRegion(cl);
159
//END: draw toplevel items
161
//BEGIN: draw background of category for all other items
163
QStyleOptionViewItem opt(option);
164
opt.rect = fullCategoryRect(option, index);
165
const QRegion cl = painter->clipRegion();
166
QRect cr = option.rect;
167
if (index.column() == 0) {
168
if (m_tree->layoutDirection() == Qt::LeftToRight) {
171
cr.setRight(opt.rect.right());
174
painter->setClipRect(cr);
175
m_categoryDrawer.drawCategory(index, 0, opt, painter);
176
painter->setClipRegion(cl);
177
painter->setRenderHint(QPainter::Antialiasing, false);
179
//END: draw background of category for all other items
182
QStyledItemDelegate::paint(painter, option, index);
183
if (index.column() == 0) {
187
painter->setClipRect(option.rect);
188
KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(m_tree->itemFromIndex(index));
190
//BEGIN: draw color button
191
if (index.column() == 1) {
193
QColor color = item->useDefaultColor() ? item->defaultColor() : item->color();
195
QStyleOptionButton opt;
196
opt.rect = option.rect;
197
opt.palette = m_tree->palette();
199
m_tree->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_tree);
200
opt.rect = m_tree->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, m_tree);
201
opt.rect.adjust(1, 1, -1, -1);
202
painter->fillRect(opt.rect, color);
203
qDrawShadePanel(painter, opt.rect, opt.palette, true, 1, NULL);
205
//END: draw color button
207
//BEGIN: draw reset icon
208
if (index.column() == 2 && !item->useDefaultColor()) {
210
QPixmap p = SmallIcon("edit-undo");
211
QRect rect(option.rect.left() + 10, option.rect.top() + (option.rect.height() - p.height() + 1) / 2, p.width(), p.height());
213
if (option.state & QStyle::State_MouseOver || option.state & QStyle::State_HasFocus) {
214
painter->drawPixmap(rect, p);
216
painter->drawPixmap(rect, SmallIcon("edit-undo", 0, KIconLoader::DisabledState));
219
//END: draw reset icon
223
KateColorTreeWidget* m_tree;
224
KateCategoryDrawer m_categoryDrawer;
226
//END KateColorTreeDelegate
228
KateColorTreeWidget::KateColorTreeWidget(QWidget *parent)
229
: QTreeWidget(parent)
231
setItemDelegate(new KateColorTreeDelegate(this));
234
headers << QString() // i18nc("@title:column the color name", "Color Role")
235
<< QString() // i18nc("@title:column a color button", "Color")
236
<< QString();// i18nc("@title:column use default color", "Reset")
237
setHeaderLabels(headers);
238
setHeaderHidden(true);
239
setRootIsDecorated(false);
243
bool KateColorTreeWidget::edit(const QModelIndex& index, EditTrigger trigger, QEvent* event)
245
// accept edit only for color buttons in column 1 and reset in column 2
246
if (!index.parent().isValid() || index.column() < 1) {
247
return QTreeWidget::edit(index, trigger, event);
251
if (event && event->type() == QEvent::KeyPress) {
252
QKeyEvent* ke = static_cast<QKeyEvent*>(event);
253
accept = (ke->key() == Qt::Key_Space); // allow Space to edit
257
case QAbstractItemView::DoubleClicked:
258
case QAbstractItemView::SelectedClicked:
259
case QAbstractItemView::EditKeyPressed: // = F2
266
KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(itemFromIndex(index));
267
QColor color = item->useDefaultColor() ? item->defaultColor() : item->color();
269
if (index.column() == 1) {
270
if (KColorDialog::getColor(color, item->defaultColor(), this) == QDialog::Accepted) {
271
item->setUseDefaultColor(false);
272
item->setColor(color);
273
viewport()->update();
276
} else if (index.column() == 2 && !item->useDefaultColor()) {
277
item->setUseDefaultColor(true);
278
viewport()->update();
284
return QTreeWidget::edit(index, trigger, event);
287
void KateColorTreeWidget::drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const
294
void KateColorTreeWidget::selectDefaults()
296
bool somethingChanged = false;
298
// use default colors for all selected items
299
for (int a = 0; a < topLevelItemCount(); ++a) {
300
QTreeWidgetItem* top = topLevelItem(a);
301
for (int b = 0; b < top->childCount(); ++b) {
302
KateColorTreeItem* it = dynamic_cast<KateColorTreeItem*>(top->child(b));
304
if (!it->useDefaultColor()) {
305
it->setUseDefaultColor(true);
306
somethingChanged = true;
311
if (somethingChanged) {
312
viewport()->update();
317
void KateColorTreeWidget::addColorItem(const KateColorItem& colorItem)
319
QTreeWidgetItem* categoryItem = 0;
320
for (int i = 0; i < topLevelItemCount(); ++i) {
321
if (topLevelItem(i)->text(0) == colorItem.category) {
322
categoryItem = topLevelItem(i);
328
categoryItem = new QTreeWidgetItem();
329
categoryItem->setText(0, colorItem.category);
330
addTopLevelItem(categoryItem);
331
expandItem(categoryItem);
334
new KateColorTreeItem(colorItem, categoryItem);
336
resizeColumnToContents(0);
339
void KateColorTreeWidget::addColorItems(const QVector<KateColorItem>& colorItems)
341
foreach(const KateColorItem& item, colorItems)
345
QVector<KateColorItem> KateColorTreeWidget::colorItems() const
347
QVector<KateColorItem> items;
348
for (int a = 0; a < topLevelItemCount(); ++a) {
349
QTreeWidgetItem* top = topLevelItem(a);
350
for (int b = 0; b < top->childCount(); ++b) {
351
KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(top->child(b));
353
items.append(item->colorItem());
359
QColor KateColorTreeWidget::findColor(const QString& key) const
361
for (int a = 0; a < topLevelItemCount(); ++a) {
362
QTreeWidgetItem* top = topLevelItem(a);
363
for (int b = 0; b < top->childCount(); ++b) {
364
KateColorTreeItem* item = dynamic_cast<KateColorTreeItem*>(top->child(b));
365
if (item->key() == key) {
366
if (item->useDefaultColor()) {
367
return item->defaultColor();
369
return item->color();
377
// kate: indent-width 2; replace-tabs on;