10#include "katestyletreewidget.h"
11#include "kateconfig.h"
12#include "kateextendedattribute.h"
14#include <KLocalizedString>
18#include <QColorDialog>
23#include <QStyledItemDelegate>
29 KateStyleTreeDelegate(KateStyleTreeWidget *widget);
31 void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const override;
34 static QBrush getBrushForColorColumn(
const QModelIndex &index,
int column);
35 KateStyleTreeWidget *m_widget;
55 const QString &styleName,
58 KateStyleTreeWidgetItem(QTreeWidget *
parent,
59 const QString &styleName,
62 ~KateStyleTreeWidgetItem()
override
81 Type = ItemType::UserType + 1,
89 void changeProperty(
int p);
93 void unsetColor(
int c);
95 QString contextName()
const
100 bool defStyle()
const;
102 bool isDefault()
const;
110 QVariant data(
int column,
int role)
const override;
112 KateStyleTreeWidget *treeWidget()
const;
116 void toggleDefStyle();
128KateStyleTreeWidget::KateStyleTreeWidget(
QWidget *parent,
bool showUseDefaults)
131 setItemDelegate(
new KateStyleTreeDelegate(
this));
132 setRootIsDecorated(
false);
136 <<
i18nc(
"@title:column Text style",
"Normal") <<
i18nc(
"@title:column Text style",
"Selected") <<
i18nc(
"@title:column Text style",
"Background")
137 <<
i18nc(
"@title:column Text style",
"Background Selected");
138 if (showUseDefaults) {
139 headers <<
i18n(
"Use Default Style");
142 setHeaderLabels(headers);
144 headerItem()->setIcon(1,
QIcon::fromTheme(QStringLiteral(
"format-text-bold")));
145 headerItem()->setIcon(2,
QIcon::fromTheme(QStringLiteral(
"format-text-italic")));
146 headerItem()->setIcon(3,
QIcon::fromTheme(QStringLiteral(
"format-text-underline")));
147 headerItem()->setIcon(4,
QIcon::fromTheme(QStringLiteral(
"format-text-strikethrough")));
150 QPalette pal = viewport()->palette();
152 viewport()->setPalette(pal);
158 QRect all(0, 0, 15, 15);
161 p.fillRect(all, color);
168bool KateStyleTreeWidget::edit(
const QModelIndex &index, EditTrigger trigger,
QEvent *event)
174 if (index.
column() == KateStyleTreeWidgetItem::Context) {
179 if (item->type() != KateStyleTreeWidgetItem::Type) {
182 KateStyleTreeWidgetItem *i =
static_cast<KateStyleTreeWidgetItem *
>(item);
188 i->changeProperty(index.
column());
197void KateStyleTreeWidget::resizeColumns()
204void KateStyleTreeWidget::showEvent(
QShowEvent *event)
218 if (item->type() != KateStyleTreeWidgetItem::Type) {
221 KateStyleTreeWidgetItem *i =
static_cast<KateStyleTreeWidgetItem *
>(item);
230 const QIcon emptyColorIcon = brushIcon(
viewport()->
palette().base().color());
231 QIcon cl = brushIcon(i->style()->foreground().color());
232 QIcon scl = brushIcon(i->style()->selectedForeground().color());
234 QIcon sbgcl = i->style()->hasProperty(CustomProperties::SelectedBackground) ? brushIcon(i->style()->selectedBackground().color()) : emptyColorIcon;
236 m.addSection(i->contextName());
238 QAction *a = m.addAction(
i18n(
"&Bold"),
this, SLOT(changeProperty()));
241 a->
setData(KateStyleTreeWidgetItem::Bold);
243 a = m.addAction(
i18n(
"&Italic"),
this, SLOT(changeProperty()));
246 a->
setData(KateStyleTreeWidgetItem::Italic);
248 a = m.addAction(
i18n(
"&Underline"),
this, SLOT(changeProperty()));
251 a->
setData(KateStyleTreeWidgetItem::Underline);
253 a = m.addAction(
i18n(
"S&trikeout"),
this, SLOT(changeProperty()));
256 a->
setData(KateStyleTreeWidgetItem::StrikeOut);
260 a = m.addAction(cl,
i18nc(
"@action:inmenu",
"Normal &Color…"),
this, SLOT(changeProperty()));
261 a->
setData(KateStyleTreeWidgetItem::Foreground);
263 a = m.addAction(scl,
i18nc(
"@action:inmenu",
"&Selected Color…"),
this, SLOT(changeProperty()));
264 a->
setData(KateStyleTreeWidgetItem::SelectedForeground);
266 a = m.addAction(bgcl,
i18nc(
"@action:inmenu",
"&Background Color…"),
this, SLOT(changeProperty()));
267 a->
setData(KateStyleTreeWidgetItem::Background);
269 a = m.addAction(sbgcl,
i18nc(
"@action:inmenu",
"S&elected Background Color…"),
this, SLOT(changeProperty()));
270 a->
setData(KateStyleTreeWidgetItem::SelectedBackground);
275 a = m.addAction(emptyColorIcon,
i18n(
"Unset Normal Color"),
this, SLOT(unsetColor()));
278 a = m.addAction(emptyColorIcon,
i18n(
"Unset Selected Color"),
this, SLOT(unsetColor()));
284 a = m.addAction(emptyColorIcon,
i18n(
"Unset Background Color"),
this, SLOT(unsetColor()));
288 if (
style->hasProperty(CustomProperties::SelectedBackground)) {
289 a = m.addAction(emptyColorIcon,
i18n(
"Unset Selected Background Color"),
this, SLOT(unsetColor()));
293 if (!i->isDefault() && !i->defStyle()) {
295 a = m.addAction(
i18n(
"Use &Default Style"),
this, SLOT(changeProperty()));
298 a->
setData(KateStyleTreeWidgetItem::UseDefaultStyle);
300 m.exec(
event->globalPos());
303void KateStyleTreeWidget::changeProperty()
305 static_cast<KateStyleTreeWidgetItem *
>(
currentItem())->changeProperty(
static_cast<QAction *
>(
sender())->data().toInt());
308void KateStyleTreeWidget::unsetColor()
310 static_cast<KateStyleTreeWidgetItem *
>(
currentItem())->unsetColor(
static_cast<QAction *
>(
sender())->data().toInt());
313void KateStyleTreeWidget::updateGroupHeadings()
317 QTreeWidgetItem *firstChild = currentTopLevelItem->
child(0);
323 currentTopLevelItem->
setForeground(KateStyleTreeWidgetItem::Context, foregroundColor);
325 if (backgroundColor.isValid()) {
326 currentTopLevelItem->
setBackground(KateStyleTreeWidgetItem::Context, backgroundColor);
332void KateStyleTreeWidget::emitChanged()
334 updateGroupHeadings();
340 new KateStyleTreeWidgetItem(
this, styleName, std::move(defaultstyle), std::move(data));
345 new KateStyleTreeWidgetItem(
parent, styleName, std::move(defaultstyle), std::move(data));
346 updateGroupHeadings();
357QBrush KateStyleTreeDelegate::getBrushForColorColumn(
const QModelIndex &index,
int column)
359 QModelIndex colorIndex = index.
sibling(index.
row(), column);
360 QVariant displayData = colorIndex.
model()->
data(colorIndex);
361 return displayData.
value<QBrush>();
366 static QSet<int> columns;
368 columns << KateStyleTreeWidgetItem::Foreground << KateStyleTreeWidgetItem::SelectedForeground << KateStyleTreeWidgetItem::Background
369 << KateStyleTreeWidgetItem::SelectedBackground;
372 if (index.
column() == KateStyleTreeWidgetItem::Context) {
373 QStyleOptionViewItem styleContextItem(option);
375 QBrush brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedBackground);
376 if (brush != QBrush()) {
380 brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedForeground);
381 if (brush != QBrush()) {
394 QVariant displayData = index.
model()->
data(index);
399 QBrush brush = displayData.
value<QBrush>();
401 QStyleOptionButton opt;
402 opt.rect = option.rect;
403 opt.palette = m_widget->palette();
405 bool set = brush != QBrush();
408 opt.text =
i18nc(
"No text or background color set",
"None set");
419KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(
QTreeWidgetItem *parent,
424 , currentStyle(nullptr)
425 , defaultStyle(std::
move(defaultAttribute))
426 , actualStyle(std::
move(actualAttribute))
429 setText(0, stylename);
432KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(
QTreeWidget *parent,
437 , currentStyle(nullptr)
438 , defaultStyle(std::
move(defaultAttribute))
439 , actualStyle(std::
move(actualAttribute))
442 setText(0, stylename);
445void KateStyleTreeWidgetItem::initStyle()
448 currentStyle = defaultStyle;
450 currentStyle =
new KTextEditor::Attribute(*defaultStyle);
452 if (actualStyle->hasAnyProperty()) {
453 *currentStyle += *actualStyle;
465QVariant KateStyleTreeWidgetItem::data(
int column,
int role)
const
467 if (column == Context) {
471 return style()->foreground().color();
477 return style()->background().color();
482 return style()->font();
490 return toCheckState(style()->fontBold());
492 return toCheckState(style()->fontItalic());
494 return toCheckState(style()->fontUnderline());
496 return toCheckState(style()->fontStrikeOut());
497 case UseDefaultStyle:
500 return toCheckState(currentStyle->foreground() == defaultStyle->foreground() && currentStyle->background() == defaultStyle->background()
501 && currentStyle->selectedForeground() == defaultStyle->selectedForeground()
502 && currentStyle->selectedBackground() == defaultStyle->selectedBackground()
503 && currentStyle->fontBold() == defaultStyle->fontBold() && currentStyle->fontItalic() == defaultStyle->fontItalic()
504 && currentStyle->fontUnderline() == defaultStyle->fontUnderline()
505 && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut());
512 return style()->foreground();
513 case SelectedForeground:
514 return style()->selectedForeground();
516 return style()->background();
517 case SelectedBackground:
518 return style()->selectedBackground();
525void KateStyleTreeWidgetItem::updateStyle()
533 if (currentStyle->fontWeight() != actualStyle->fontWeight()) {
534 actualStyle->setFontWeight(currentStyle->fontWeight());
541 if (currentStyle->fontItalic() != actualStyle->fontItalic()) {
542 actualStyle->setFontItalic(currentStyle->fontItalic());
549 if (currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut()) {
550 actualStyle->setFontStrikeOut(currentStyle->fontStrikeOut());
557 if (currentStyle->fontUnderline() != actualStyle->fontUnderline()) {
558 actualStyle->setFontUnderline(currentStyle->fontUnderline());
564 if (currentStyle->hasProperty(CustomProperties::Outline)) {
565 if (currentStyle->outline() != actualStyle->outline()) {
566 actualStyle->setOutline(currentStyle->outline());
569 actualStyle->clearProperty(CustomProperties::Outline);
573 if (currentStyle->foreground() != actualStyle->foreground()) {
574 actualStyle->setForeground(currentStyle->foreground());
580 if (currentStyle->hasProperty(CustomProperties::SelectedForeground)) {
581 if (currentStyle->selectedForeground() != actualStyle->selectedForeground()) {
582 actualStyle->setSelectedForeground(currentStyle->selectedForeground());
585 actualStyle->clearProperty(CustomProperties::SelectedForeground);
589 if (currentStyle->background() != actualStyle->background()) {
590 actualStyle->setBackground(currentStyle->background());
596 if (currentStyle->hasProperty(CustomProperties::SelectedBackground)) {
597 if (currentStyle->selectedBackground() != actualStyle->selectedBackground()) {
598 actualStyle->setSelectedBackground(currentStyle->selectedBackground());
601 actualStyle->clearProperty(CustomProperties::SelectedBackground);
606bool KateStyleTreeWidgetItem::defStyle()
const
608 return actualStyle && actualStyle->properties() != defaultStyle->properties();
612bool KateStyleTreeWidgetItem::isDefault()
const
614 return actualStyle ? false :
true;
617void KateStyleTreeWidgetItem::changeProperty(
int p)
620 currentStyle->setFontBold(!currentStyle->fontBold());
621 }
else if (p == Italic) {
622 currentStyle->setFontItalic(!currentStyle->fontItalic());
623 }
else if (p == Underline) {
624 currentStyle->setFontUnderline(!currentStyle->fontUnderline());
625 }
else if (p == StrikeOut) {
626 currentStyle->setFontStrikeOut(!currentStyle->fontStrikeOut());
627 }
else if (p == UseDefaultStyle) {
635 treeWidget()->emitChanged();
638void KateStyleTreeWidgetItem::toggleDefStyle()
640 if (*currentStyle == *defaultStyle) {
642 i18n(
"\"Use Default Style\" will be automatically unset when you change any style properties."),
644 QStringLiteral(
"Kate hl config use defaults"));
649 QModelIndex currentIndex = treeWidget()->currentIndex();
650 while (currentIndex.
isValid()) {
651 treeWidget()->update(currentIndex);
652 currentIndex = currentIndex.
sibling(currentIndex.
row(), currentIndex.
column() - 1);
657void KateStyleTreeWidgetItem::setColor(
int column)
661 if (column == Foreground) {
662 c = currentStyle->foreground().color();
663 d = defaultStyle->foreground().color();
664 }
else if (column == SelectedForeground) {
665 c = currentStyle->selectedForeground().color();
666 d = defaultStyle->selectedForeground().color();
667 }
else if (column == Background) {
668 c = currentStyle->background().color();
669 d = defaultStyle->background().color();
670 }
else if (column == SelectedBackground) {
671 c = currentStyle->selectedBackground().color();
672 d = defaultStyle->selectedBackground().color();
681 if (!selectedColor.
isValid()) {
690 currentStyle->setForeground(selectedColor);
692 case SelectedForeground:
693 currentStyle->setSelectedForeground(selectedColor);
696 currentStyle->setBackground(selectedColor);
698 case SelectedBackground:
699 currentStyle->setSelectedBackground(selectedColor);
707void KateStyleTreeWidgetItem::unsetColor(
int colorId)
712 currentStyle->setForeground(defaultStyle->foreground());
718 if (defaultStyle->hasProperty(CustomProperties::SelectedForeground)) {
719 currentStyle->setSelectedForeground(defaultStyle->selectedForeground());
721 currentStyle->clearProperty(CustomProperties::SelectedForeground);
730 if (currentStyle->hasProperty(CustomProperties::SelectedBackground)) {
731 currentStyle->clearProperty(CustomProperties::SelectedBackground);
738 treeWidget()->emitChanged();
746bool KateStyleTreeWidget::readOnly()
const
751void KateStyleTreeWidget::setReadOnly(
bool readOnly)
753 m_readOnly = readOnly;
757#include "moc_katestyletreewidget.cpp"
QExplicitlySharedDataPointer< Attribute > Ptr
Shared data pointer for Attribute.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event)
void setData(const QVariant &data)
bool isValid() const const
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, ColorDialogOptions options)
QIcon fromTheme(const QString &name)
bool isValid() const const
const QAbstractItemModel * model() const const
QModelIndex sibling(int row, int column) const const
virtual bool event(QEvent *e)
QObject * parent() const const
QObject * sender() const const
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void setPen(Qt::PenStyle style)
void setColor(ColorGroup group, ColorRole role, const QColor &color)
bool contains(const QSet< T > &other) const const
bool isEmpty() const const
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const const override
void resizeColumnToContents(int column)
QString text(int column) const const
int userType() const const