File indexing completed on 2024-04-28 11:45:26

0001 /*
0002     SPDX-FileCopyrightText: 2001-2003 Christoph Cullmann <cullmann@kde.org>
0003     SPDX-FileCopyrightText: 2002, 2003 Anders Lund <anders.lund@lund.tdcadsl.dk>
0004     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0005     SPDX-FileCopyrightText: 2007 Mirko Stocker <me@misto.ch>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "katestyletreewidget.h"
0011 #include "kateconfig.h"
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 
0016 #include <QAction>
0017 #include <QColorDialog>
0018 #include <QHeaderView>
0019 #include <QKeyEvent>
0020 #include <QMenu>
0021 #include <QPainter>
0022 #include <QStyledItemDelegate>
0023 
0024 // BEGIN KateStyleTreeDelegate
0025 class KateStyleTreeDelegate : public QStyledItemDelegate
0026 {
0027 public:
0028     KateStyleTreeDelegate(KateStyleTreeWidget *widget);
0029 
0030     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0031 
0032 private:
0033     static QBrush getBrushForColorColumn(const QModelIndex &index, int column);
0034     KateStyleTreeWidget *m_widget;
0035 };
0036 // END
0037 
0038 // BEGIN KateStyleTreeWidgetItem decl
0039 /*
0040     QListViewItem subclass to display/edit a style, bold/italic is check boxes,
0041     normal and selected colors are boxes, which will display a color chooser when
0042     activated.
0043     The context name for the style will be drawn using the editor default font and
0044     the chosen colors.
0045     This widget id designed to handle the default as well as the individual hl style
0046     lists.
0047     This widget is designed to work with the KateStyleTreeWidget class exclusively.
0048     Added by anders, jan 23 2002.
0049 */
0050 class KateStyleTreeWidgetItem : public QTreeWidgetItem
0051 {
0052 public:
0053     KateStyleTreeWidgetItem(QTreeWidgetItem *parent,
0054                             const QString &styleName,
0055                             KTextEditor::Attribute::Ptr defaultstyle,
0056                             KTextEditor::Attribute::Ptr data = KTextEditor::Attribute::Ptr());
0057     KateStyleTreeWidgetItem(QTreeWidget *parent,
0058                             const QString &styleName,
0059                             KTextEditor::Attribute::Ptr defaultstyle,
0060                             KTextEditor::Attribute::Ptr data = KTextEditor::Attribute::Ptr());
0061     ~KateStyleTreeWidgetItem() override
0062     {
0063     }
0064 
0065     enum columns {
0066         Context = 0,
0067         Bold,
0068         Italic,
0069         Underline,
0070         StrikeOut,
0071         Foreground,
0072         SelectedForeground,
0073         Background,
0074         SelectedBackground,
0075         UseDefaultStyle,
0076         NumColumns
0077     };
0078 
0079     /* initializes the style from the default and the hldata */
0080     void initStyle();
0081     /* updates the hldata's style */
0082     void updateStyle();
0083     /* For bool fields, toggles them, for color fields, display a color chooser */
0084     void changeProperty(int p);
0085     /** unset a color.
0086      * c is 100 (BGColor) or 101 (SelectedBGColor) for now.
0087      */
0088     void unsetColor(int c);
0089     /* style context name */
0090     QString contextName() const
0091     {
0092         return text(0);
0093     }
0094     /* only true for a hl mode item using its default style */
0095     bool defStyle() const;
0096     /* true for default styles */
0097     bool isDefault() const;
0098     /* whichever style is active (currentStyle for hl mode styles not using
0099        the default style, defaultStyle otherwise) */
0100     KTextEditor::Attribute::Ptr style() const
0101     {
0102         return currentStyle;
0103     }
0104 
0105     QVariant data(int column, int role) const override;
0106 
0107     KateStyleTreeWidget *treeWidget() const;
0108 
0109 private:
0110     /* private methods to change properties */
0111     void toggleDefStyle();
0112     void setColor(int);
0113     /* helper function to copy the default style into the KateExtendedAttribute,
0114        when a property is changed and we are using default style. */
0115 
0116     KTextEditor::Attribute::Ptr currentStyle, // the style currently in use (was "is")
0117         defaultStyle; // default style for hl mode contexts and default styles (was "ds")
0118     KTextEditor::Attribute::Ptr actualStyle; // itemdata for hl mode contexts (was "st")
0119 };
0120 // END
0121 
0122 // BEGIN KateStyleTreeWidget
0123 KateStyleTreeWidget::KateStyleTreeWidget(QWidget *parent, bool showUseDefaults)
0124     : QTreeWidget(parent)
0125 {
0126     setItemDelegate(new KateStyleTreeDelegate(this));
0127     setRootIsDecorated(false);
0128 
0129     QStringList headers;
0130     headers << i18nc("@title:column Meaning of text in editor", "Context") << QString() << QString() << QString() << QString()
0131             << i18nc("@title:column Text style", "Normal") << i18nc("@title:column Text style", "Selected") << i18nc("@title:column Text style", "Background")
0132             << i18nc("@title:column Text style", "Background Selected");
0133     if (showUseDefaults) {
0134         headers << i18n("Use Default Style");
0135     }
0136 
0137     setHeaderLabels(headers);
0138 
0139     headerItem()->setIcon(1, QIcon::fromTheme(QStringLiteral("format-text-bold")));
0140     headerItem()->setIcon(2, QIcon::fromTheme(QStringLiteral("format-text-italic")));
0141     headerItem()->setIcon(3, QIcon::fromTheme(QStringLiteral("format-text-underline")));
0142     headerItem()->setIcon(4, QIcon::fromTheme(QStringLiteral("format-text-strikethrough")));
0143 
0144     // grab the background color and apply it to the palette
0145     QPalette pal = viewport()->palette();
0146     pal.setColor(QPalette::Window, KateRendererConfig::global()->backgroundColor());
0147     viewport()->setPalette(pal);
0148 }
0149 
0150 QIcon brushIcon(const QColor &color)
0151 {
0152     QPixmap pm(16, 16);
0153     QRect all(0, 0, 15, 15);
0154     {
0155         QPainter p(&pm);
0156         p.fillRect(all, color);
0157         p.setPen(Qt::black);
0158         p.drawRect(all);
0159     }
0160     return QIcon(pm);
0161 }
0162 
0163 bool KateStyleTreeWidget::edit(const QModelIndex &index, EditTrigger trigger, QEvent *event)
0164 {
0165     if (m_readOnly) {
0166         return false;
0167     }
0168 
0169     if (index.column() == KateStyleTreeWidgetItem::Context) {
0170         return false;
0171     }
0172 
0173     KateStyleTreeWidgetItem *i = dynamic_cast<KateStyleTreeWidgetItem *>(itemFromIndex(index));
0174     if (!i) {
0175         return QTreeWidget::edit(index, trigger, event);
0176     }
0177 
0178     switch (trigger) {
0179     case QAbstractItemView::DoubleClicked:
0180     case QAbstractItemView::SelectedClicked:
0181     case QAbstractItemView::EditKeyPressed:
0182         i->changeProperty(index.column());
0183         update(index);
0184         update(index.sibling(index.row(), KateStyleTreeWidgetItem::Context));
0185         return false;
0186     default:
0187         return QTreeWidget::edit(index, trigger, event);
0188     }
0189 }
0190 
0191 void KateStyleTreeWidget::resizeColumns()
0192 {
0193     for (int i = 0; i < columnCount(); ++i) {
0194         resizeColumnToContents(i);
0195     }
0196 }
0197 
0198 void KateStyleTreeWidget::showEvent(QShowEvent *event)
0199 {
0200     QTreeWidget::showEvent(event);
0201 
0202     resizeColumns();
0203 }
0204 
0205 void KateStyleTreeWidget::contextMenuEvent(QContextMenuEvent *event)
0206 {
0207     if (m_readOnly) {
0208         return;
0209     }
0210 
0211     KateStyleTreeWidgetItem *i = dynamic_cast<KateStyleTreeWidgetItem *>(itemAt(event->pos()));
0212     if (!i) {
0213         return;
0214     }
0215 
0216     QMenu m(this);
0217     KTextEditor::Attribute::Ptr currentStyle = i->style();
0218     // the title is used, because the menu obscures the context name when
0219     // displayed on behalf of spacePressed().
0220     QPainter p;
0221     p.setPen(Qt::black);
0222 
0223     const QIcon emptyColorIcon = brushIcon(viewport()->palette().base().color());
0224     QIcon cl = brushIcon(i->style()->foreground().color());
0225     QIcon scl = brushIcon(i->style()->selectedForeground().color());
0226     QIcon bgcl = i->style()->hasProperty(QTextFormat::BackgroundBrush) ? brushIcon(i->style()->background().color()) : emptyColorIcon;
0227     QIcon sbgcl = i->style()->hasProperty(CustomProperties::SelectedBackground) ? brushIcon(i->style()->selectedBackground().color()) : emptyColorIcon;
0228 
0229     m.addSection(i->contextName());
0230 
0231     QAction *a = m.addAction(i18n("&Bold"), this, SLOT(changeProperty()));
0232     a->setCheckable(true);
0233     a->setChecked(currentStyle->fontBold());
0234     a->setData(KateStyleTreeWidgetItem::Bold);
0235 
0236     a = m.addAction(i18n("&Italic"), this, SLOT(changeProperty()));
0237     a->setCheckable(true);
0238     a->setChecked(currentStyle->fontItalic());
0239     a->setData(KateStyleTreeWidgetItem::Italic);
0240 
0241     a = m.addAction(i18n("&Underline"), this, SLOT(changeProperty()));
0242     a->setCheckable(true);
0243     a->setChecked(currentStyle->fontUnderline());
0244     a->setData(KateStyleTreeWidgetItem::Underline);
0245 
0246     a = m.addAction(i18n("S&trikeout"), this, SLOT(changeProperty()));
0247     a->setCheckable(true);
0248     a->setChecked(currentStyle->fontStrikeOut());
0249     a->setData(KateStyleTreeWidgetItem::StrikeOut);
0250 
0251     m.addSeparator();
0252 
0253     a = m.addAction(cl, i18n("Normal &Color..."), this, SLOT(changeProperty()));
0254     a->setData(KateStyleTreeWidgetItem::Foreground);
0255 
0256     a = m.addAction(scl, i18n("&Selected Color..."), this, SLOT(changeProperty()));
0257     a->setData(KateStyleTreeWidgetItem::SelectedForeground);
0258 
0259     a = m.addAction(bgcl, i18n("&Background Color..."), this, SLOT(changeProperty()));
0260     a->setData(KateStyleTreeWidgetItem::Background);
0261 
0262     a = m.addAction(sbgcl, i18n("S&elected Background Color..."), this, SLOT(changeProperty()));
0263     a->setData(KateStyleTreeWidgetItem::SelectedBackground);
0264 
0265     // defaulters
0266     m.addSeparator();
0267 
0268     a = m.addAction(emptyColorIcon, i18n("Unset Normal Color"), this, SLOT(unsetColor()));
0269     a->setData(1);
0270 
0271     a = m.addAction(emptyColorIcon, i18n("Unset Selected Color"), this, SLOT(unsetColor()));
0272     a->setData(2);
0273 
0274     // unsetters
0275     KTextEditor::Attribute::Ptr style = i->style();
0276     if (style->hasProperty(QTextFormat::BackgroundBrush)) {
0277         a = m.addAction(emptyColorIcon, i18n("Unset Background Color"), this, SLOT(unsetColor()));
0278         a->setData(3);
0279     }
0280 
0281     if (style->hasProperty(CustomProperties::SelectedBackground)) {
0282         a = m.addAction(emptyColorIcon, i18n("Unset Selected Background Color"), this, SLOT(unsetColor()));
0283         a->setData(4);
0284     }
0285 
0286     if (!i->isDefault() && !i->defStyle()) {
0287         m.addSeparator();
0288         a = m.addAction(i18n("Use &Default Style"), this, SLOT(changeProperty()));
0289         a->setCheckable(true);
0290         a->setChecked(i->defStyle());
0291         a->setData(KateStyleTreeWidgetItem::UseDefaultStyle);
0292     }
0293     m.exec(event->globalPos());
0294 }
0295 
0296 void KateStyleTreeWidget::changeProperty()
0297 {
0298     static_cast<KateStyleTreeWidgetItem *>(currentItem())->changeProperty(static_cast<QAction *>(sender())->data().toInt());
0299 }
0300 
0301 void KateStyleTreeWidget::unsetColor()
0302 {
0303     static_cast<KateStyleTreeWidgetItem *>(currentItem())->unsetColor(static_cast<QAction *>(sender())->data().toInt());
0304 }
0305 
0306 void KateStyleTreeWidget::updateGroupHeadings()
0307 {
0308     for (int i = 0; i < topLevelItemCount(); i++) {
0309         QTreeWidgetItem *currentTopLevelItem = topLevelItem(i);
0310         QTreeWidgetItem *firstChild = currentTopLevelItem->child(0);
0311 
0312         if (firstChild) {
0313             QColor foregroundColor = firstChild->data(KateStyleTreeWidgetItem::Foreground, Qt::DisplayRole).value<QColor>();
0314             QColor backgroundColor = firstChild->data(KateStyleTreeWidgetItem::Background, Qt::DisplayRole).value<QColor>();
0315 
0316             currentTopLevelItem->setForeground(KateStyleTreeWidgetItem::Context, foregroundColor);
0317 
0318             if (backgroundColor.isValid()) {
0319                 currentTopLevelItem->setBackground(KateStyleTreeWidgetItem::Context, backgroundColor);
0320             }
0321         }
0322     }
0323 }
0324 
0325 void KateStyleTreeWidget::emitChanged()
0326 {
0327     updateGroupHeadings();
0328     Q_EMIT changed();
0329 }
0330 
0331 void KateStyleTreeWidget::addItem(const QString &styleName, KTextEditor::Attribute::Ptr defaultstyle, KTextEditor::Attribute::Ptr data)
0332 {
0333     new KateStyleTreeWidgetItem(this, styleName, std::move(defaultstyle), std::move(data));
0334 }
0335 
0336 void KateStyleTreeWidget::addItem(QTreeWidgetItem *parent, const QString &styleName, KTextEditor::Attribute::Ptr defaultstyle, KTextEditor::Attribute::Ptr data)
0337 {
0338     new KateStyleTreeWidgetItem(parent, styleName, std::move(defaultstyle), std::move(data));
0339     updateGroupHeadings();
0340 }
0341 // END
0342 
0343 // BEGIN KateStyleTreeWidgetItem
0344 KateStyleTreeDelegate::KateStyleTreeDelegate(KateStyleTreeWidget *widget)
0345     : QStyledItemDelegate(widget)
0346     , m_widget(widget)
0347 {
0348 }
0349 
0350 QBrush KateStyleTreeDelegate::getBrushForColorColumn(const QModelIndex &index, int column)
0351 {
0352     QModelIndex colorIndex = index.sibling(index.row(), column);
0353     QVariant displayData = colorIndex.model()->data(colorIndex);
0354     return displayData.value<QBrush>();
0355 }
0356 
0357 void KateStyleTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0358 {
0359     static QSet<int> columns;
0360     if (columns.isEmpty()) {
0361         columns << KateStyleTreeWidgetItem::Foreground << KateStyleTreeWidgetItem::SelectedForeground << KateStyleTreeWidgetItem::Background
0362                 << KateStyleTreeWidgetItem::SelectedBackground;
0363     }
0364 
0365     if (index.column() == KateStyleTreeWidgetItem::Context) {
0366         QStyleOptionViewItem styleContextItem(option);
0367 
0368         QBrush brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedBackground);
0369         if (brush != QBrush()) {
0370             styleContextItem.palette.setBrush(QPalette::Highlight, brush);
0371         }
0372 
0373         brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedForeground);
0374         if (brush != QBrush()) {
0375             styleContextItem.palette.setBrush(QPalette::HighlightedText, brush);
0376         }
0377 
0378         return QStyledItemDelegate::paint(painter, styleContextItem, index);
0379     }
0380 
0381     QStyledItemDelegate::paint(painter, option, index);
0382 
0383     if (!columns.contains(index.column())) {
0384         return;
0385     }
0386 
0387     QVariant displayData = index.model()->data(index);
0388     if (displayData.type() != QVariant::Brush) {
0389         return;
0390     }
0391 
0392     QBrush brush = displayData.value<QBrush>();
0393 
0394     QStyleOptionButton opt;
0395     opt.rect = option.rect;
0396     opt.palette = m_widget->palette();
0397 
0398     bool set = brush != QBrush();
0399 
0400     if (!set) {
0401         opt.text = i18nc("No text or background color set", "None set");
0402         brush = Qt::white;
0403     }
0404 
0405     m_widget->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_widget);
0406 
0407     if (set) {
0408         painter->fillRect(m_widget->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, m_widget), brush);
0409     }
0410 }
0411 
0412 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(QTreeWidgetItem *parent,
0413                                                  const QString &stylename,
0414                                                  KTextEditor::Attribute::Ptr defaultAttribute,
0415                                                  KTextEditor::Attribute::Ptr actualAttribute)
0416     : QTreeWidgetItem(parent)
0417     , currentStyle(nullptr)
0418     , defaultStyle(std::move(defaultAttribute))
0419     , actualStyle(std::move(actualAttribute))
0420 {
0421     initStyle();
0422     setText(0, stylename);
0423 }
0424 
0425 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(QTreeWidget *parent,
0426                                                  const QString &stylename,
0427                                                  KTextEditor::Attribute::Ptr defaultAttribute,
0428                                                  KTextEditor::Attribute::Ptr actualAttribute)
0429     : QTreeWidgetItem(parent)
0430     , currentStyle(nullptr)
0431     , defaultStyle(std::move(defaultAttribute))
0432     , actualStyle(std::move(actualAttribute))
0433 {
0434     initStyle();
0435     setText(0, stylename);
0436 }
0437 
0438 void KateStyleTreeWidgetItem::initStyle()
0439 {
0440     if (!actualStyle) {
0441         currentStyle = defaultStyle;
0442     } else {
0443         currentStyle = new KTextEditor::Attribute(*defaultStyle);
0444 
0445         if (actualStyle->hasAnyProperty()) {
0446             *currentStyle += *actualStyle;
0447         }
0448     }
0449 
0450     setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
0451 }
0452 
0453 static Qt::CheckState toCheckState(bool b)
0454 {
0455     return b ? Qt::Checked : Qt::Unchecked;
0456 }
0457 
0458 QVariant KateStyleTreeWidgetItem::data(int column, int role) const
0459 {
0460     if (column == Context) {
0461         switch (role) {
0462         case Qt::ForegroundRole:
0463             if (style()->hasProperty(QTextFormat::ForegroundBrush)) {
0464                 return style()->foreground().color();
0465             }
0466             break;
0467 
0468         case Qt::BackgroundRole:
0469             if (style()->hasProperty(QTextFormat::BackgroundBrush)) {
0470                 return style()->background().color();
0471             }
0472             break;
0473 
0474         case Qt::FontRole:
0475             return style()->font();
0476             break;
0477         }
0478     }
0479 
0480     if (role == Qt::CheckStateRole) {
0481         switch (column) {
0482         case Bold:
0483             return toCheckState(style()->fontBold());
0484         case Italic:
0485             return toCheckState(style()->fontItalic());
0486         case Underline:
0487             return toCheckState(style()->fontUnderline());
0488         case StrikeOut:
0489             return toCheckState(style()->fontStrikeOut());
0490         case UseDefaultStyle:
0491             /* can't compare all attributes, currentStyle has always more than defaultStyle (e.g. the item's name),
0492              * so we just compare the important ones:*/
0493             return toCheckState(currentStyle->foreground() == defaultStyle->foreground() && currentStyle->background() == defaultStyle->background()
0494                                 && currentStyle->selectedForeground() == defaultStyle->selectedForeground()
0495                                 && currentStyle->selectedBackground() == defaultStyle->selectedBackground()
0496                                 && currentStyle->fontBold() == defaultStyle->fontBold() && currentStyle->fontItalic() == defaultStyle->fontItalic()
0497                                 && currentStyle->fontUnderline() == defaultStyle->fontUnderline()
0498                                 && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut());
0499         }
0500     }
0501 
0502     if (role == Qt::DisplayRole) {
0503         switch (column) {
0504         case Foreground:
0505             return style()->foreground();
0506         case SelectedForeground:
0507             return style()->selectedForeground();
0508         case Background:
0509             return style()->background();
0510         case SelectedBackground:
0511             return style()->selectedBackground();
0512         }
0513     }
0514 
0515     return QTreeWidgetItem::data(column, role);
0516 }
0517 
0518 void KateStyleTreeWidgetItem::updateStyle()
0519 {
0520     // nothing there, not update it, will crash
0521     if (!actualStyle) {
0522         return;
0523     }
0524 
0525     if (currentStyle->hasProperty(QTextFormat::FontWeight)) {
0526         if (currentStyle->fontWeight() != actualStyle->fontWeight()) {
0527             actualStyle->setFontWeight(currentStyle->fontWeight());
0528         }
0529     } else {
0530         actualStyle->clearProperty(QTextFormat::FontWeight);
0531     }
0532 
0533     if (currentStyle->hasProperty(QTextFormat::FontItalic)) {
0534         if (currentStyle->fontItalic() != actualStyle->fontItalic()) {
0535             actualStyle->setFontItalic(currentStyle->fontItalic());
0536         }
0537     } else {
0538         actualStyle->clearProperty(QTextFormat::FontItalic);
0539     }
0540 
0541     if (currentStyle->hasProperty(QTextFormat::FontStrikeOut)) {
0542         if (currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut()) {
0543             actualStyle->setFontStrikeOut(currentStyle->fontStrikeOut());
0544         }
0545     } else {
0546         actualStyle->clearProperty(QTextFormat::FontStrikeOut);
0547     }
0548 
0549     if (currentStyle->hasProperty(QTextFormat::TextUnderlineStyle)) {
0550         if (currentStyle->fontUnderline() != actualStyle->fontUnderline()) {
0551             actualStyle->setFontUnderline(currentStyle->fontUnderline());
0552         }
0553     } else {
0554         actualStyle->clearProperty(QTextFormat::TextUnderlineStyle);
0555     }
0556 
0557     if (currentStyle->hasProperty(CustomProperties::Outline)) {
0558         if (currentStyle->outline() != actualStyle->outline()) {
0559             actualStyle->setOutline(currentStyle->outline());
0560         }
0561     } else {
0562         actualStyle->clearProperty(CustomProperties::Outline);
0563     }
0564 
0565     if (currentStyle->hasProperty(QTextFormat::ForegroundBrush)) {
0566         if (currentStyle->foreground() != actualStyle->foreground()) {
0567             actualStyle->setForeground(currentStyle->foreground());
0568         }
0569     } else {
0570         actualStyle->clearProperty(QTextFormat::ForegroundBrush);
0571     }
0572 
0573     if (currentStyle->hasProperty(CustomProperties::SelectedForeground)) {
0574         if (currentStyle->selectedForeground() != actualStyle->selectedForeground()) {
0575             actualStyle->setSelectedForeground(currentStyle->selectedForeground());
0576         }
0577     } else {
0578         actualStyle->clearProperty(CustomProperties::SelectedForeground);
0579     }
0580 
0581     if (currentStyle->hasProperty(QTextFormat::BackgroundBrush)) {
0582         if (currentStyle->background() != actualStyle->background()) {
0583             actualStyle->setBackground(currentStyle->background());
0584         }
0585     } else {
0586         actualStyle->clearProperty(QTextFormat::BackgroundBrush);
0587     }
0588 
0589     if (currentStyle->hasProperty(CustomProperties::SelectedBackground)) {
0590         if (currentStyle->selectedBackground() != actualStyle->selectedBackground()) {
0591             actualStyle->setSelectedBackground(currentStyle->selectedBackground());
0592         }
0593     } else {
0594         actualStyle->clearProperty(CustomProperties::SelectedBackground);
0595     }
0596 }
0597 
0598 /* only true for a hl mode item using its default style */
0599 bool KateStyleTreeWidgetItem::defStyle() const
0600 {
0601     return actualStyle && actualStyle->properties() != defaultStyle->properties();
0602 }
0603 
0604 /* true for default styles */
0605 bool KateStyleTreeWidgetItem::isDefault() const
0606 {
0607     return actualStyle ? false : true;
0608 }
0609 
0610 void KateStyleTreeWidgetItem::changeProperty(int p)
0611 {
0612     if (p == Bold) {
0613         currentStyle->setFontBold(!currentStyle->fontBold());
0614     } else if (p == Italic) {
0615         currentStyle->setFontItalic(!currentStyle->fontItalic());
0616     } else if (p == Underline) {
0617         currentStyle->setFontUnderline(!currentStyle->fontUnderline());
0618     } else if (p == StrikeOut) {
0619         currentStyle->setFontStrikeOut(!currentStyle->fontStrikeOut());
0620     } else if (p == UseDefaultStyle) {
0621         toggleDefStyle();
0622     } else {
0623         setColor(p);
0624     }
0625 
0626     updateStyle();
0627 
0628     treeWidget()->emitChanged();
0629 }
0630 
0631 void KateStyleTreeWidgetItem::toggleDefStyle()
0632 {
0633     if (*currentStyle == *defaultStyle) {
0634         KMessageBox::information(treeWidget(),
0635                                  i18n("\"Use Default Style\" will be automatically unset when you change any style properties."),
0636                                  i18n("Kate Styles"),
0637                                  QStringLiteral("Kate hl config use defaults"));
0638     } else {
0639         currentStyle = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute(*defaultStyle));
0640         updateStyle();
0641 
0642         QModelIndex currentIndex = treeWidget()->currentIndex();
0643         while (currentIndex.isValid()) {
0644             treeWidget()->update(currentIndex);
0645             currentIndex = currentIndex.sibling(currentIndex.row(), currentIndex.column() - 1);
0646         }
0647     }
0648 }
0649 
0650 void KateStyleTreeWidgetItem::setColor(int column)
0651 {
0652     QColor c; // use this
0653     QColor d; // default color
0654     if (column == Foreground) {
0655         c = currentStyle->foreground().color();
0656         d = defaultStyle->foreground().color();
0657     } else if (column == SelectedForeground) {
0658         c = currentStyle->selectedForeground().color();
0659         d = defaultStyle->selectedForeground().color();
0660     } else if (column == Background) {
0661         c = currentStyle->background().color();
0662         d = defaultStyle->background().color();
0663     } else if (column == SelectedBackground) {
0664         c = currentStyle->selectedBackground().color();
0665         d = defaultStyle->selectedBackground().color();
0666     }
0667 
0668     if (!c.isValid()) {
0669         c = d;
0670     }
0671 
0672     const QColor selectedColor = QColorDialog::getColor(c, treeWidget());
0673 
0674     if (!selectedColor.isValid()) {
0675         return;
0676     }
0677 
0678     // if set default, and the attrib is set in the default style use it
0679     // else if set default, unset it
0680     // else set the selected color
0681     switch (column) {
0682     case Foreground:
0683         currentStyle->setForeground(selectedColor);
0684         break;
0685     case SelectedForeground:
0686         currentStyle->setSelectedForeground(selectedColor);
0687         break;
0688     case Background:
0689         currentStyle->setBackground(selectedColor);
0690         break;
0691     case SelectedBackground:
0692         currentStyle->setSelectedBackground(selectedColor);
0693         break;
0694     }
0695 
0696     // FIXME
0697     // repaint();
0698 }
0699 
0700 void KateStyleTreeWidgetItem::unsetColor(int colorId)
0701 {
0702     switch (colorId) {
0703     case 1:
0704         if (defaultStyle->hasProperty(QTextFormat::ForegroundBrush)) {
0705             currentStyle->setForeground(defaultStyle->foreground());
0706         } else {
0707             currentStyle->clearProperty(QTextFormat::ForegroundBrush);
0708         }
0709         break;
0710     case 2:
0711         if (defaultStyle->hasProperty(CustomProperties::SelectedForeground)) {
0712             currentStyle->setSelectedForeground(defaultStyle->selectedForeground());
0713         } else {
0714             currentStyle->clearProperty(CustomProperties::SelectedForeground);
0715         }
0716         break;
0717     case 3:
0718         if (currentStyle->hasProperty(QTextFormat::BackgroundBrush)) {
0719             currentStyle->clearProperty(QTextFormat::BackgroundBrush);
0720         }
0721         break;
0722     case 4:
0723         if (currentStyle->hasProperty(CustomProperties::SelectedBackground)) {
0724             currentStyle->clearProperty(CustomProperties::SelectedBackground);
0725         }
0726         break;
0727     }
0728 
0729     updateStyle();
0730 
0731     treeWidget()->emitChanged();
0732 }
0733 
0734 KateStyleTreeWidget *KateStyleTreeWidgetItem::treeWidget() const
0735 {
0736     return static_cast<KateStyleTreeWidget *>(QTreeWidgetItem::treeWidget());
0737 }
0738 
0739 bool KateStyleTreeWidget::readOnly() const
0740 {
0741     return m_readOnly;
0742 }
0743 
0744 void KateStyleTreeWidget::setReadOnly(bool readOnly)
0745 {
0746     m_readOnly = readOnly;
0747 }
0748 // END
0749 
0750 #include "moc_katestyletreewidget.cpp"