23 #include <QtGui/QStyledItemDelegate>
24 #include <QtGui/QPainter>
25 #include <QtGui/QHeaderView>
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);
48 setData(1, Qt::WhatsThisRole, colorItem.
whatsThis);
51 setData(2, Qt::ToolTipRole,
i18n(
"Use default color from the KDE color scheme"));
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;
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);
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);
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);
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));
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);
208 if (index.column() == 2 && !item->useDefaultColor()) {
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);
237 setHeaderLabels(headers);
238 setHeaderHidden(
true);
239 setRootIsDecorated(
false);
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);
257 case QAbstractItemView::DoubleClicked:
258 case QAbstractItemView::SelectedClicked:
259 case QAbstractItemView::EditKeyPressed:
267 QColor color = item->useDefaultColor() ? item->defaultColor() : item->color();
269 if (index.column() == 1) {
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);
296 bool somethingChanged =
false;
299 for (
int a = 0; a < topLevelItemCount(); ++a) {
300 QTreeWidgetItem* top = topLevelItem(a);
301 for (
int b = 0; b < top->childCount(); ++b) {
304 if (!it->useDefaultColor()) {
305 it->setUseDefaultColor(
true);
306 somethingChanged =
true;
311 if (somethingChanged) {
312 viewport()->update();
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);
336 resizeColumnToContents(0);
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) {
353 items.append(item->colorItem());
361 for (
int a = 0; a < topLevelItemCount(); ++a) {
362 QTreeWidgetItem* top = topLevelItem(a);
363 for (
int b = 0; b < top->childCount(); ++b) {
365 if (item->key() == key) {
366 if (item->useDefaultColor()) {
367 return item->defaultColor();
369 return item->color();
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
QPixmap SmallIcon(const QString &name, int force_size, int state, const QStringList &overlays)
static int getColor(QColor &theColor, QWidget *parent=0L)