24 #include <QtGui/QPainter>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QAction>
27 #include <QtGui/QStyledItemDelegate>
28 #include <QtGui/QHeaderView>
46 virtual void paint( QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index )
const;
49 QBrush getBrushForColorColumn(
const QModelIndex& index,
int column)
const;
66 class KateStyleTreeWidgetItem :
public QTreeWidgetItem
71 ~KateStyleTreeWidgetItem() {}
92 void changeProperty(
int p );
96 void unsetColor(
int c );
98 QString contextName()
const {
return text(0); }
100 bool defStyle()
const;
102 bool isDefault()
const;
107 virtual QVariant data(
int column,
int role )
const;
113 void toggleDefStyle();
114 void setColor(
int );
129 setItemDelegate(
new KateStyleTreeDelegate(
this));
130 setRootIsDecorated(
false);
133 headers <<
i18nc(
"@title:column Meaning of text in editor",
"Context") <<
QString() <<
QString() <<
QString() <<
QString() <<
i18nc(
"@title:column Text style",
"Normal") <<
i18nc(
"@title:column Text style",
"Selected") <<
i18nc(
"@title:column Text style",
"Background") <<
i18nc(
"@title:column Text style",
"Background Selected");
134 if(showUseDefaults) {
135 headers <<
i18n(
"Use Default Style");
138 setHeaderLabels(headers);
140 headerItem()->setIcon(1,
KIcon(
"format-text-bold"));
141 headerItem()->setIcon(2,
KIcon(
"format-text-italic"));
142 headerItem()->setIcon(3,
KIcon(
"format-text-underline"));
143 headerItem()->setIcon(4,
KIcon(
"format-text-strikethrough"));
151 QPalette pal = viewport()->palette();
152 pal.setColor(QPalette::Background, bgcol);
153 viewport()->setPalette( pal );
159 QRect all(0,0,15,15);
162 p.fillRect(all, color);
171 if(index.column() == KateStyleTreeWidgetItem::Context)
174 KateStyleTreeWidgetItem *i =
dynamic_cast<KateStyleTreeWidgetItem*
>(itemFromIndex(index));
176 return QTreeWidget::edit(index, trigger, event);
179 case QAbstractItemView::DoubleClicked:
180 case QAbstractItemView::SelectedClicked:
181 case QAbstractItemView::EditKeyPressed:
182 i->changeProperty(index.column());
184 update(index.sibling(index.row(), KateStyleTreeWidgetItem::Context));
187 return QTreeWidget::edit(index, trigger, event);
193 for (
int i = 0; i < columnCount(); ++i)
194 resizeColumnToContents(i);
199 QTreeWidget::showEvent(event);
206 KateStyleTreeWidgetItem *i =
dynamic_cast<KateStyleTreeWidgetItem*
>(itemAt(event->pos()));
216 QIcon cl =
brushIcon( i->style()->foreground().color() );
217 QIcon scl =
brushIcon( i->style()->selectedForeground().color() );
218 QIcon bgcl =
brushIcon( i->style()->hasProperty(QTextFormat::BackgroundBrush) ? i->style()->background().color() : viewport()->palette().base().color() );
223 QAction* a = m.addAction(
i18n(
"&Bold"),
this, SLOT(changeProperty()) );
224 a->setCheckable(
true);
225 a->setChecked( currentStyle->fontBold() );
226 a->setData(KateStyleTreeWidgetItem::Bold);
228 a = m.addAction(
i18n(
"&Italic"),
this, SLOT(changeProperty()) );
229 a->setCheckable(
true);
230 a->setChecked( currentStyle->fontItalic() );
231 a->setData(KateStyleTreeWidgetItem::Italic);
233 a = m.addAction(
i18n(
"&Underline"),
this, SLOT(changeProperty()) );
234 a->setCheckable(
true);
235 a->setChecked( currentStyle->fontUnderline() );
236 a->setData(KateStyleTreeWidgetItem::Underline);
238 a = m.addAction(
i18n(
"S&trikeout"),
this, SLOT(changeProperty()) );
239 a->setCheckable(
true);
240 a->setChecked( currentStyle->fontStrikeOut() );
241 a->setData(KateStyleTreeWidgetItem::StrikeOut);
245 a = m.addAction( cl,
i18n(
"Normal &Color..."),
this, SLOT(changeProperty()) );
246 a->setData(KateStyleTreeWidgetItem::Foreground);
248 a = m.addAction( scl,
i18n(
"&Selected Color..."),
this, SLOT(changeProperty()) );
249 a->setData(KateStyleTreeWidgetItem::SelectedForeground);
251 a = m.addAction( bgcl,
i18n(
"&Background Color..."),
this, SLOT(changeProperty()) );
252 a->setData(KateStyleTreeWidgetItem::Background);
254 a = m.addAction( sbgcl,
i18n(
"S&elected Background Color..."),
this, SLOT(changeProperty()) );
255 a->setData(KateStyleTreeWidgetItem::SelectedBackground);
265 if ( style->hasProperty( QTextFormat::BackgroundBrush) ) {
266 a = m.addAction(
i18n(
"Unset Background Color"),
this, SLOT(unsetColor()) );
270 a = m.addAction(
i18n(
"Unset Selected Background Color"),
this, SLOT(unsetColor()) );
275 if ( ! i->isDefault() && ! i->defStyle() ) {
277 a = m.addAction(
i18n(
"Use &Default Style"),
this, SLOT(changeProperty()) );
278 a->setCheckable(
true);
279 a->setChecked( i->defStyle() );
280 a->setData(KateStyleTreeWidgetItem::UseDefaultStyle);
282 m.exec( event->globalPos() );
285 void KateStyleTreeWidget::changeProperty()
287 static_cast<KateStyleTreeWidgetItem*
>(currentItem())->changeProperty( static_cast<QAction*>(sender())->data().toInt() );
290 void KateStyleTreeWidget::unsetColor()
292 static_cast<KateStyleTreeWidgetItem*
>(currentItem())->unsetColor( static_cast<QAction*>(sender())->data().toInt() );
295 void KateStyleTreeWidget::updateGroupHeadings()
297 for(
int i = 0; i < topLevelItemCount(); i++) {
298 QTreeWidgetItem* currentTopLevelItem = topLevelItem(i);
299 QTreeWidgetItem* firstChild = currentTopLevelItem->child(0);
302 QColor foregroundColor = firstChild->data(KateStyleTreeWidgetItem::Foreground, Qt::DisplayRole).value<
QColor>();
303 QColor backgroundColor = firstChild->data(KateStyleTreeWidgetItem::Background, Qt::DisplayRole).value<
QColor>();
305 currentTopLevelItem->setForeground(KateStyleTreeWidgetItem::Context, foregroundColor);
307 if(backgroundColor.isValid()) {
308 currentTopLevelItem->setBackground(KateStyleTreeWidgetItem::Context, backgroundColor);
316 updateGroupHeadings();
322 new KateStyleTreeWidgetItem(
this, styleName, defaultstyle, data);
327 new KateStyleTreeWidgetItem(parent, styleName, defaultstyle, data);
328 updateGroupHeadings();
341 QBrush KateStyleTreeDelegate::getBrushForColorColumn(
const QModelIndex& index,
int column)
const
343 QModelIndex colorIndex = index.sibling(index.row(), column);
344 QVariant displayData = colorIndex.model()->data(colorIndex);
345 return qVariantValue<QBrush>(displayData);
348 void KateStyleTreeDelegate::paint( QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index )
const
351 if (!columns.count())
352 columns << KateStyleTreeWidgetItem::Foreground << KateStyleTreeWidgetItem::SelectedForeground << KateStyleTreeWidgetItem::Background << KateStyleTreeWidgetItem::SelectedBackground;
354 if(index.column() == KateStyleTreeWidgetItem::Context) {
355 QStyleOptionViewItem styleContextItem(option);
357 QBrush brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedBackground);
358 if (brush != QBrush()) {
359 styleContextItem.palette.setBrush(QPalette::Highlight, brush);
362 brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedForeground);
363 if (brush != QBrush()) {
364 styleContextItem.palette.setBrush(QPalette::HighlightedText, brush);
367 return QStyledItemDelegate::paint(painter, styleContextItem, index);
370 QStyledItemDelegate::paint(painter, option, index);
372 if (!columns.contains(index.column())) {
376 QVariant displayData = index.model()->data(index);
377 if (displayData.type() != QVariant::Brush)
380 QBrush brush = qVariantValue<QBrush>(displayData);
382 QStyleOptionButton opt;
383 opt.rect = option.rect;
384 opt.palette = m_widget->palette();
386 bool set = brush != QBrush();
389 opt.text =
i18nc(
"No text or background color set",
"None set");
393 m_widget->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_widget);
396 painter->fillRect(m_widget->style()->subElementRect(QStyle::SE_PushButtonContents, &opt,m_widget), brush);
399 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem( QTreeWidgetItem *parent,
const QString & stylename,
401 : QTreeWidgetItem( parent ),
403 defaultStyle( defaultAttribute ),
404 actualStyle( actualAttribute )
407 setText(0, stylename);
410 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(
QTreeWidget *parent,
const QString & stylename,
412 : QTreeWidgetItem( parent ),
414 defaultStyle( defaultAttribute ),
415 actualStyle( actualAttribute )
418 setText(0, stylename);
421 void KateStyleTreeWidgetItem::initStyle()
425 currentStyle = defaultStyle;
431 if (actualStyle->hasAnyProperty())
432 *currentStyle += *actualStyle;
435 setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
439 return b ? Qt::Checked : Qt::Unchecked;
442 QVariant KateStyleTreeWidgetItem::data(
int column,
int role )
const
444 if (column == Context) {
446 case Qt::ForegroundRole:
447 if (style()->hasProperty(QTextFormat::ForegroundBrush))
448 return style()->foreground().color();
451 case Qt::BackgroundRole:
452 if (style()->hasProperty(QTextFormat::BackgroundBrush))
453 return style()->background().color();
457 return style()->font();
462 if (role == Qt::CheckStateRole) {
472 case UseDefaultStyle:
476 currentStyle->foreground() == defaultStyle->foreground()
477 && currentStyle->background() == defaultStyle->background()
478 && currentStyle->selectedForeground() == defaultStyle->selectedForeground()
479 && currentStyle->selectedBackground() == defaultStyle->selectedBackground()
480 && currentStyle->fontBold() == defaultStyle->fontBold()
481 && currentStyle->fontItalic() == defaultStyle->fontItalic()
482 && currentStyle->fontUnderline() == defaultStyle->fontUnderline()
483 && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut());
487 if (role == Qt::DisplayRole) {
490 return style()->foreground();
491 case SelectedForeground:
492 return style()->selectedForeground();
494 return style()->background();
495 case SelectedBackground:
496 return style()->selectedBackground();
500 return QTreeWidgetItem::data(column, role);
503 void KateStyleTreeWidgetItem::updateStyle()
509 if ( currentStyle->hasProperty(QTextFormat::FontWeight) )
511 if ( currentStyle->fontWeight() != actualStyle->fontWeight())
512 actualStyle->setFontWeight( currentStyle->fontWeight() );
514 else actualStyle->clearProperty( QTextFormat::FontWeight );
516 if ( currentStyle->hasProperty(QTextFormat::FontItalic) )
518 if ( currentStyle->fontItalic() != actualStyle->fontItalic())
519 actualStyle->setFontItalic( currentStyle->fontItalic() );
521 else actualStyle->clearProperty( QTextFormat::FontItalic );
523 if ( currentStyle->hasProperty(QTextFormat::FontStrikeOut) )
525 if ( currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut())
526 actualStyle->setFontStrikeOut( currentStyle->fontStrikeOut() );
528 else actualStyle->clearProperty( QTextFormat::FontStrikeOut );
530 if ( currentStyle->hasProperty(QTextFormat::FontUnderline) )
532 if ( currentStyle->fontUnderline() != actualStyle->fontUnderline())
533 actualStyle->setFontUnderline( currentStyle->fontUnderline() );
535 else actualStyle->clearProperty( QTextFormat::FontUnderline );
539 if ( currentStyle->outline() != actualStyle->outline())
540 actualStyle->setOutline( currentStyle->outline() );
544 if ( currentStyle->hasProperty(QTextFormat::ForegroundBrush) )
546 if ( currentStyle->foreground() != actualStyle->foreground())
547 actualStyle->setForeground( currentStyle->foreground() );
549 else actualStyle->clearProperty( QTextFormat::ForegroundBrush );
553 if ( currentStyle->selectedForeground() != actualStyle->selectedForeground())
554 actualStyle->setSelectedForeground( currentStyle->selectedForeground() );
558 if ( currentStyle->hasProperty(QTextFormat::BackgroundBrush) )
560 if ( currentStyle->background() != actualStyle->background())
561 actualStyle->setBackground( currentStyle->background() );
563 else actualStyle->clearProperty( QTextFormat::BackgroundBrush );
567 if ( currentStyle->selectedBackground() != actualStyle->selectedBackground())
568 actualStyle->setSelectedBackground( currentStyle->selectedBackground() );
574 bool KateStyleTreeWidgetItem::defStyle()
const {
return actualStyle && actualStyle->properties() != defaultStyle->properties(); }
577 bool KateStyleTreeWidgetItem::isDefault()
const {
return actualStyle ?
false :
true; }
579 void KateStyleTreeWidgetItem::changeProperty(
int p )
582 currentStyle->setFontBold( ! currentStyle->fontBold() );
583 else if ( p == Italic )
584 currentStyle->setFontItalic( ! currentStyle->fontItalic() );
585 else if ( p == Underline )
586 currentStyle->setFontUnderline( ! currentStyle->fontUnderline() );
587 else if ( p == StrikeOut )
588 currentStyle->setFontStrikeOut( ! currentStyle->fontStrikeOut() );
589 else if ( p == UseDefaultStyle )
596 treeWidget()->emitChanged();
599 void KateStyleTreeWidgetItem::toggleDefStyle()
601 if ( *currentStyle == *defaultStyle ) {
603 i18n(
"\"Use Default Style\" will be automatically unset when you change any style properties."),
605 "Kate hl config use defaults" );
611 QModelIndex currentIndex = treeWidget()->currentIndex();
612 while(currentIndex.isValid()) {
613 treeWidget()->update(currentIndex);
614 currentIndex = currentIndex.sibling(currentIndex.row(), currentIndex.column() - 1);
619 void KateStyleTreeWidgetItem::setColor(
int column )
623 if ( column == Foreground)
625 c = currentStyle->foreground().color();
626 d = defaultStyle->foreground().color();
628 else if ( column == SelectedForeground )
630 c = currentStyle->selectedForeground().color();
631 d = defaultStyle->selectedForeground().color();
633 else if ( column == Background )
635 c = currentStyle->background().color();
636 d = defaultStyle->background().color();
638 else if ( column == SelectedBackground )
640 c = currentStyle->selectedBackground().color();
641 d = defaultStyle->selectedBackground().color();
646 bool def = ! c.isValid();
656 if ( defaultStyle->hasProperty(QTextFormat::ForegroundBrush) )
657 currentStyle->setForeground( defaultStyle->foreground());
659 currentStyle->clearProperty(QTextFormat::ForegroundBrush);
662 currentStyle->setForeground( c );
664 case SelectedForeground:
668 currentStyle->setSelectedForeground( defaultStyle->selectedForeground());
673 currentStyle->setSelectedForeground( c );
678 if ( defaultStyle->hasProperty(QTextFormat::BackgroundBrush) )
679 currentStyle->setBackground( defaultStyle->background());
681 currentStyle->clearProperty(QTextFormat::BackgroundBrush);
684 currentStyle->setBackground( c );
686 case SelectedBackground:
690 currentStyle->setSelectedBackground( defaultStyle->selectedBackground());
695 currentStyle->setSelectedBackground( c );
703 void KateStyleTreeWidgetItem::unsetColor(
int c )
705 if ( c == 100 && currentStyle->hasProperty(QTextFormat::BackgroundBrush) )
706 currentStyle->clearProperty(QTextFormat::BackgroundBrush);
711 treeWidget()->emitChanged();
720 #include "katestyletreewidget.moc"
const QColor & selectionColor() const
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
QScriptValue i18nc(QScriptContext *context, QScriptEngine *engine)
i18nc("context", "text", arguments [optional])
static void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QBrush foreground(ForegroundRole=NormalText) const
KSharedPtr< Attribute > Ptr
static KateRendererConfig * global()
const QFont & font() const
const QColor & backgroundColor() const
void setFlags(CrashFlags flags)
static int getColor(QColor &theColor, QWidget *parent=0L)