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