KTextEditor

katestyletreewidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2001-2003 Christoph Cullmann <[email protected]>
3  SPDX-FileCopyrightText: 2002, 2003 Anders Lund <[email protected]>
4  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
5  SPDX-FileCopyrightText: 2007 Mirko Stocker <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "katestyletreewidget.h"
11 #include "kateconfig.h"
12 #include "kateglobal.h"
13 
14 #include <KLocalizedString>
15 #include <KMessageBox>
16 
17 #include <QAction>
18 #include <QColorDialog>
19 #include <QHeaderView>
20 #include <QKeyEvent>
21 #include <QMenu>
22 #include <QPainter>
23 #include <QStyledItemDelegate>
24 
25 // BEGIN KateStyleTreeDelegate
26 class KateStyleTreeDelegate : public QStyledItemDelegate
27 {
28 public:
29  KateStyleTreeDelegate(KateStyleTreeWidget *widget);
30 
31  void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
32 
33 private:
34  QBrush getBrushForColorColumn(const QModelIndex &index, int column) const;
35  KateStyleTreeWidget *m_widget;
36 };
37 // END
38 
39 // BEGIN KateStyleTreeWidgetItem decl
40 /*
41  QListViewItem subclass to display/edit a style, bold/italic is check boxes,
42  normal and selected colors are boxes, which will display a color chooser when
43  activated.
44  The context name for the style will be drawn using the editor default font and
45  the chosen colors.
46  This widget id designed to handle the default as well as the individual hl style
47  lists.
48  This widget is designed to work with the KateStyleTreeWidget class exclusively.
49  Added by anders, jan 23 2002.
50 */
51 class KateStyleTreeWidgetItem : public QTreeWidgetItem
52 {
53 public:
54  KateStyleTreeWidgetItem(QTreeWidgetItem *parent, const QString &styleName, KTextEditor::Attribute::Ptr defaultstyle, KTextEditor::Attribute::Ptr data = KTextEditor::Attribute::Ptr());
55  KateStyleTreeWidgetItem(QTreeWidget *parent, const QString &styleName, KTextEditor::Attribute::Ptr defaultstyle, KTextEditor::Attribute::Ptr data = KTextEditor::Attribute::Ptr());
56  ~KateStyleTreeWidgetItem() override
57  {
58  }
59 
60  enum columns { Context = 0, Bold, Italic, Underline, StrikeOut, Foreground, SelectedForeground, Background, SelectedBackground, UseDefaultStyle, NumColumns };
61 
62  /* initializes the style from the default and the hldata */
63  void initStyle();
64  /* updates the hldata's style */
65  void updateStyle();
66  /* For bool fields, toggles them, for color fields, display a color chooser */
67  void changeProperty(int p);
71  void unsetColor(int c);
72  /* style context name */
73  QString contextName() const
74  {
75  return text(0);
76  }
77  /* only true for a hl mode item using its default style */
78  bool defStyle() const;
79  /* true for default styles */
80  bool isDefault() const;
81  /* whichever style is active (currentStyle for hl mode styles not using
82  the default style, defaultStyle otherwise) */
83  KTextEditor::Attribute::Ptr style() const
84  {
85  return currentStyle;
86  }
87 
88  QVariant data(int column, int role) const override;
89 
90  KateStyleTreeWidget *treeWidget() const;
91 
92 private:
93  /* private methods to change properties */
94  void toggleDefStyle();
95  void setColor(int);
96  /* helper function to copy the default style into the KateExtendedAttribute,
97  when a property is changed and we are using default style. */
98 
99  KTextEditor::Attribute::Ptr currentStyle, // the style currently in use (was "is")
100  defaultStyle; // default style for hl mode contexts and default styles (was "ds")
101  KTextEditor::Attribute::Ptr actualStyle; // itemdata for hl mode contexts (was "st")
102 };
103 // END
104 
105 // BEGIN KateStyleTreeWidget
106 KateStyleTreeWidget::KateStyleTreeWidget(QWidget *parent, bool showUseDefaults)
107  : QTreeWidget(parent)
108 {
109  setItemDelegate(new KateStyleTreeDelegate(this));
110  setRootIsDecorated(false);
111 
112  QStringList headers;
113  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")
114  << i18nc("@title:column Text style", "Background") << i18nc("@title:column Text style", "Background Selected");
115  if (showUseDefaults) {
116  headers << i18n("Use Default Style");
117  }
118 
119  setHeaderLabels(headers);
120 
121  headerItem()->setIcon(1, QIcon::fromTheme(QStringLiteral("format-text-bold")));
122  headerItem()->setIcon(2, QIcon::fromTheme(QStringLiteral("format-text-italic")));
123  headerItem()->setIcon(3, QIcon::fromTheme(QStringLiteral("format-text-underline")));
124  headerItem()->setIcon(4, QIcon::fromTheme(QStringLiteral("format-text-strikethrough")));
125 
126  // grap the bg color, selected color and default font
127  bgcol = KateRendererConfig::global()->backgroundColor();
128  selcol = KateRendererConfig::global()->selectionColor();
129  docfont = KateRendererConfig::global()->baseFont();
130 
131  QPalette pal = viewport()->palette();
132  pal.setColor(QPalette::Window, bgcol);
133  viewport()->setPalette(pal);
134 }
135 
136 QIcon brushIcon(const QColor &color)
137 {
138  QPixmap pm(16, 16);
139  QRect all(0, 0, 15, 15);
140  {
141  QPainter p(&pm);
142  p.fillRect(all, color);
143  p.setPen(Qt::black);
144  p.drawRect(all);
145  }
146  return QIcon(pm);
147 }
148 
149 bool KateStyleTreeWidget::edit(const QModelIndex &index, EditTrigger trigger, QEvent *event)
150 {
151  if (index.column() == KateStyleTreeWidgetItem::Context) {
152  return false;
153  }
154 
155  KateStyleTreeWidgetItem *i = dynamic_cast<KateStyleTreeWidgetItem *>(itemFromIndex(index));
156  if (!i) {
157  return QTreeWidget::edit(index, trigger, event);
158  }
159 
160  switch (trigger) {
164  i->changeProperty(index.column());
165  update(index);
166  update(index.sibling(index.row(), KateStyleTreeWidgetItem::Context));
167  return false;
168  default:
169  return QTreeWidget::edit(index, trigger, event);
170  }
171 }
172 
173 void KateStyleTreeWidget::resizeColumns()
174 {
175  for (int i = 0; i < columnCount(); ++i) {
176  resizeColumnToContents(i);
177  }
178 }
179 
180 void KateStyleTreeWidget::showEvent(QShowEvent *event)
181 {
182  QTreeWidget::showEvent(event);
183 
184  resizeColumns();
185 }
186 
187 void KateStyleTreeWidget::contextMenuEvent(QContextMenuEvent *event)
188 {
189  KateStyleTreeWidgetItem *i = dynamic_cast<KateStyleTreeWidgetItem *>(itemAt(event->pos()));
190  if (!i) {
191  return;
192  }
193 
194  QMenu m(this);
195  KTextEditor::Attribute::Ptr currentStyle = i->style();
196  // the title is used, because the menu obscures the context name when
197  // displayed on behalf of spacePressed().
198  QPainter p;
199  p.setPen(Qt::black);
200 
201  const QIcon emptyColorIcon = brushIcon(viewport()->palette().base().color());
202  QIcon cl = brushIcon(i->style()->foreground().color());
203  QIcon scl = brushIcon(i->style()->selectedForeground().color());
204  QIcon bgcl = i->style()->hasProperty(QTextFormat::BackgroundBrush) ? brushIcon(i->style()->background().color()) : emptyColorIcon;
205  QIcon sbgcl = i->style()->hasProperty(CustomProperties::SelectedBackground) ? brushIcon(i->style()->selectedBackground().color()) : emptyColorIcon;
206 
207  m.addSection(i->contextName());
208 
209  QAction *a = m.addAction(i18n("&Bold"), this, SLOT(changeProperty()));
210  a->setCheckable(true);
211  a->setChecked(currentStyle->fontBold());
212  a->setData(KateStyleTreeWidgetItem::Bold);
213 
214  a = m.addAction(i18n("&Italic"), this, SLOT(changeProperty()));
215  a->setCheckable(true);
216  a->setChecked(currentStyle->fontItalic());
217  a->setData(KateStyleTreeWidgetItem::Italic);
218 
219  a = m.addAction(i18n("&Underline"), this, SLOT(changeProperty()));
220  a->setCheckable(true);
221  a->setChecked(currentStyle->fontUnderline());
222  a->setData(KateStyleTreeWidgetItem::Underline);
223 
224  a = m.addAction(i18n("S&trikeout"), this, SLOT(changeProperty()));
225  a->setCheckable(true);
226  a->setChecked(currentStyle->fontStrikeOut());
227  a->setData(KateStyleTreeWidgetItem::StrikeOut);
228 
229  m.addSeparator();
230 
231  a = m.addAction(cl, i18n("Normal &Color..."), this, SLOT(changeProperty()));
232  a->setData(KateStyleTreeWidgetItem::Foreground);
233 
234  a = m.addAction(scl, i18n("&Selected Color..."), this, SLOT(changeProperty()));
235  a->setData(KateStyleTreeWidgetItem::SelectedForeground);
236 
237  a = m.addAction(bgcl, i18n("&Background Color..."), this, SLOT(changeProperty()));
238  a->setData(KateStyleTreeWidgetItem::Background);
239 
240  a = m.addAction(sbgcl, i18n("S&elected Background Color..."), this, SLOT(changeProperty()));
241  a->setData(KateStyleTreeWidgetItem::SelectedBackground);
242 
243  // defaulters
244  m.addSeparator();
245 
246  a = m.addAction(emptyColorIcon, i18n("Unset Normal Color"), this, SLOT(unsetColor()));
247  a->setData(1);
248 
249  a = m.addAction(emptyColorIcon, i18n("Unset Selected Color"), this, SLOT(unsetColor()));
250  a->setData(2);
251 
252  // unsetters
253  KTextEditor::Attribute::Ptr style = i->style();
254  if (style->hasProperty(QTextFormat::BackgroundBrush)) {
255  a = m.addAction(emptyColorIcon, i18n("Unset Background Color"), this, SLOT(unsetColor()));
256  a->setData(3);
257  }
258 
259  if (style->hasProperty(CustomProperties::SelectedBackground)) {
260  a = m.addAction(emptyColorIcon, i18n("Unset Selected Background Color"), this, SLOT(unsetColor()));
261  a->setData(4);
262  }
263 
264  if (!i->isDefault() && !i->defStyle()) {
265  m.addSeparator();
266  a = m.addAction(i18n("Use &Default Style"), this, SLOT(changeProperty()));
267  a->setCheckable(true);
268  a->setChecked(i->defStyle());
269  a->setData(KateStyleTreeWidgetItem::UseDefaultStyle);
270  }
271  m.exec(event->globalPos());
272 }
273 
274 void KateStyleTreeWidget::changeProperty()
275 {
276  static_cast<KateStyleTreeWidgetItem *>(currentItem())->changeProperty(static_cast<QAction *>(sender())->data().toInt());
277 }
278 
279 void KateStyleTreeWidget::unsetColor()
280 {
281  static_cast<KateStyleTreeWidgetItem *>(currentItem())->unsetColor(static_cast<QAction *>(sender())->data().toInt());
282 }
283 
284 void KateStyleTreeWidget::updateGroupHeadings()
285 {
286  for (int i = 0; i < topLevelItemCount(); i++) {
287  QTreeWidgetItem *currentTopLevelItem = topLevelItem(i);
288  QTreeWidgetItem *firstChild = currentTopLevelItem->child(0);
289 
290  if (firstChild) {
291  QColor foregroundColor = firstChild->data(KateStyleTreeWidgetItem::Foreground, Qt::DisplayRole).value<QColor>();
292  QColor backgroundColor = firstChild->data(KateStyleTreeWidgetItem::Background, Qt::DisplayRole).value<QColor>();
293 
294  currentTopLevelItem->setForeground(KateStyleTreeWidgetItem::Context, foregroundColor);
295 
296  if (backgroundColor.isValid()) {
297  currentTopLevelItem->setBackground(KateStyleTreeWidgetItem::Context, backgroundColor);
298  }
299  }
300  }
301 }
302 
303 void KateStyleTreeWidget::emitChanged()
304 {
305  updateGroupHeadings();
306  emit changed();
307 }
308 
309 void KateStyleTreeWidget::addItem(const QString &styleName, KTextEditor::Attribute::Ptr defaultstyle, KTextEditor::Attribute::Ptr data)
310 {
311  new KateStyleTreeWidgetItem(this, styleName, std::move(defaultstyle), std::move(data));
312 }
313 
314 void KateStyleTreeWidget::addItem(QTreeWidgetItem *parent, const QString &styleName, KTextEditor::Attribute::Ptr defaultstyle, KTextEditor::Attribute::Ptr data)
315 {
316  new KateStyleTreeWidgetItem(parent, styleName, std::move(defaultstyle), std::move(data));
317  updateGroupHeadings();
318 }
319 // END
320 
321 // BEGIN KateStyleTreeWidgetItem
322 KateStyleTreeDelegate::KateStyleTreeDelegate(KateStyleTreeWidget *widget)
323  : m_widget(widget)
324 {
325 }
326 
327 QBrush KateStyleTreeDelegate::getBrushForColorColumn(const QModelIndex &index, int column) const
328 {
329  QModelIndex colorIndex = index.sibling(index.row(), column);
330  QVariant displayData = colorIndex.model()->data(colorIndex);
331  return displayData.value<QBrush>();
332 }
333 
334 void KateStyleTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
335 {
336  static QSet<int> columns;
337  if (columns.isEmpty()) {
338  columns << KateStyleTreeWidgetItem::Foreground << KateStyleTreeWidgetItem::SelectedForeground << KateStyleTreeWidgetItem::Background << KateStyleTreeWidgetItem::SelectedBackground;
339  }
340 
341  if (index.column() == KateStyleTreeWidgetItem::Context) {
342  QStyleOptionViewItem styleContextItem(option);
343 
344  QBrush brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedBackground);
345  if (brush != QBrush()) {
346  styleContextItem.palette.setBrush(QPalette::Highlight, brush);
347  }
348 
349  brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedForeground);
350  if (brush != QBrush()) {
351  styleContextItem.palette.setBrush(QPalette::HighlightedText, brush);
352  }
353 
354  return QStyledItemDelegate::paint(painter, styleContextItem, index);
355  }
356 
357  QStyledItemDelegate::paint(painter, option, index);
358 
359  if (!columns.contains(index.column())) {
360  return;
361  }
362 
363  QVariant displayData = index.model()->data(index);
364  if (displayData.type() != QVariant::Brush) {
365  return;
366  }
367 
368  QBrush brush = displayData.value<QBrush>();
369 
370  QStyleOptionButton opt;
371  opt.rect = option.rect;
372  opt.palette = m_widget->palette();
373 
374  bool set = brush != QBrush();
375 
376  if (!set) {
377  opt.text = i18nc("No text or background color set", "None set");
378  brush = Qt::white;
379  }
380 
381  m_widget->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_widget);
382 
383  if (set) {
384  painter->fillRect(m_widget->style()->subElementRect(QStyle::SE_PushButtonContents, &opt, m_widget), brush);
385  }
386 }
387 
388 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(QTreeWidgetItem *parent, const QString &stylename, KTextEditor::Attribute::Ptr defaultAttribute, KTextEditor::Attribute::Ptr actualAttribute)
389  : QTreeWidgetItem(parent)
390  , currentStyle(nullptr)
391  , defaultStyle(std::move(defaultAttribute))
392  , actualStyle(std::move(actualAttribute))
393 {
394  initStyle();
395  setText(0, stylename);
396 }
397 
398 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem(QTreeWidget *parent, const QString &stylename, KTextEditor::Attribute::Ptr defaultAttribute, KTextEditor::Attribute::Ptr actualAttribute)
399  : QTreeWidgetItem(parent)
400  , currentStyle(nullptr)
401  , defaultStyle(std::move(defaultAttribute))
402  , actualStyle(std::move(actualAttribute))
403 {
404  initStyle();
405  setText(0, stylename);
406 }
407 
408 void KateStyleTreeWidgetItem::initStyle()
409 {
410  if (!actualStyle) {
411  currentStyle = defaultStyle;
412  } else {
413  currentStyle = new KTextEditor::Attribute(*defaultStyle);
414 
415  if (actualStyle->hasAnyProperty()) {
416  *currentStyle += *actualStyle;
417  }
418  }
419 
421 }
422 
423 static Qt::CheckState toCheckState(bool b)
424 {
425  return b ? Qt::Checked : Qt::Unchecked;
426 }
427 
428 QVariant KateStyleTreeWidgetItem::data(int column, int role) const
429 {
430  if (column == Context) {
431  switch (role) {
432  case Qt::ForegroundRole:
433  if (style()->hasProperty(QTextFormat::ForegroundBrush)) {
434  return style()->foreground().color();
435  }
436  break;
437 
438  case Qt::BackgroundRole:
439  if (style()->hasProperty(QTextFormat::BackgroundBrush)) {
440  return style()->background().color();
441  }
442  break;
443 
444  case Qt::FontRole:
445  return style()->font();
446  break;
447  }
448  }
449 
450  if (role == Qt::CheckStateRole) {
451  switch (column) {
452  case Bold:
453  return toCheckState(style()->fontBold());
454  case Italic:
455  return toCheckState(style()->fontItalic());
456  case Underline:
457  return toCheckState(style()->fontUnderline());
458  case StrikeOut:
459  return toCheckState(style()->fontStrikeOut());
460  case UseDefaultStyle:
461  /* can't compare all attributes, currentStyle has always more than defaultStyle (e.g. the item's name),
462  * so we just compare the important ones:*/
463  return toCheckState(currentStyle->foreground() == defaultStyle->foreground() && currentStyle->background() == defaultStyle->background() && currentStyle->selectedForeground() == defaultStyle->selectedForeground() &&
464  currentStyle->selectedBackground() == defaultStyle->selectedBackground() && currentStyle->fontBold() == defaultStyle->fontBold() && currentStyle->fontItalic() == defaultStyle->fontItalic() &&
465  currentStyle->fontUnderline() == defaultStyle->fontUnderline() && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut());
466  }
467  }
468 
469  if (role == Qt::DisplayRole) {
470  switch (column) {
471  case Foreground:
472  return style()->foreground();
473  case SelectedForeground:
474  return style()->selectedForeground();
475  case Background:
476  return style()->background();
477  case SelectedBackground:
478  return style()->selectedBackground();
479  }
480  }
481 
482  return QTreeWidgetItem::data(column, role);
483 }
484 
485 void KateStyleTreeWidgetItem::updateStyle()
486 {
487  // nothing there, not update it, will crash
488  if (!actualStyle) {
489  return;
490  }
491 
492  if (currentStyle->hasProperty(QTextFormat::FontWeight)) {
493  if (currentStyle->fontWeight() != actualStyle->fontWeight()) {
494  actualStyle->setFontWeight(currentStyle->fontWeight());
495  }
496  } else {
497  actualStyle->clearProperty(QTextFormat::FontWeight);
498  }
499 
500  if (currentStyle->hasProperty(QTextFormat::FontItalic)) {
501  if (currentStyle->fontItalic() != actualStyle->fontItalic()) {
502  actualStyle->setFontItalic(currentStyle->fontItalic());
503  }
504  } else {
505  actualStyle->clearProperty(QTextFormat::FontItalic);
506  }
507 
508  if (currentStyle->hasProperty(QTextFormat::FontStrikeOut)) {
509  if (currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut()) {
510  actualStyle->setFontStrikeOut(currentStyle->fontStrikeOut());
511  }
512  } else {
513  actualStyle->clearProperty(QTextFormat::FontStrikeOut);
514  }
515 
516  if (currentStyle->hasProperty(QTextFormat::TextUnderlineStyle)) {
517  if (currentStyle->fontUnderline() != actualStyle->fontUnderline()) {
518  actualStyle->setFontUnderline(currentStyle->fontUnderline());
519  }
520  } else {
521  actualStyle->clearProperty(QTextFormat::TextUnderlineStyle);
522  }
523 
524  if (currentStyle->hasProperty(CustomProperties::Outline)) {
525  if (currentStyle->outline() != actualStyle->outline()) {
526  actualStyle->setOutline(currentStyle->outline());
527  }
528  } else {
529  actualStyle->clearProperty(CustomProperties::Outline);
530  }
531 
532  if (currentStyle->hasProperty(QTextFormat::ForegroundBrush)) {
533  if (currentStyle->foreground() != actualStyle->foreground()) {
534  actualStyle->setForeground(currentStyle->foreground());
535  }
536  } else {
537  actualStyle->clearProperty(QTextFormat::ForegroundBrush);
538  }
539 
540  if (currentStyle->hasProperty(CustomProperties::SelectedForeground)) {
541  if (currentStyle->selectedForeground() != actualStyle->selectedForeground()) {
542  actualStyle->setSelectedForeground(currentStyle->selectedForeground());
543  }
544  } else {
545  actualStyle->clearProperty(CustomProperties::SelectedForeground);
546  }
547 
548  if (currentStyle->hasProperty(QTextFormat::BackgroundBrush)) {
549  if (currentStyle->background() != actualStyle->background()) {
550  actualStyle->setBackground(currentStyle->background());
551  }
552  } else {
553  actualStyle->clearProperty(QTextFormat::BackgroundBrush);
554  }
555 
556  if (currentStyle->hasProperty(CustomProperties::SelectedBackground)) {
557  if (currentStyle->selectedBackground() != actualStyle->selectedBackground()) {
558  actualStyle->setSelectedBackground(currentStyle->selectedBackground());
559  }
560  } else {
561  actualStyle->clearProperty(CustomProperties::SelectedBackground);
562  }
563 }
564 
565 /* only true for a hl mode item using its default style */
566 bool KateStyleTreeWidgetItem::defStyle() const
567 {
568  return actualStyle && actualStyle->properties() != defaultStyle->properties();
569 }
570 
571 /* true for default styles */
572 bool KateStyleTreeWidgetItem::isDefault() const
573 {
574  return actualStyle ? false : true;
575 }
576 
577 void KateStyleTreeWidgetItem::changeProperty(int p)
578 {
579  if (p == Bold) {
580  currentStyle->setFontBold(!currentStyle->fontBold());
581  } else if (p == Italic) {
582  currentStyle->setFontItalic(!currentStyle->fontItalic());
583  } else if (p == Underline) {
584  currentStyle->setFontUnderline(!currentStyle->fontUnderline());
585  } else if (p == StrikeOut) {
586  currentStyle->setFontStrikeOut(!currentStyle->fontStrikeOut());
587  } else if (p == UseDefaultStyle) {
588  toggleDefStyle();
589  } else {
590  setColor(p);
591  }
592 
593  updateStyle();
594 
595  treeWidget()->emitChanged();
596 }
597 
598 void KateStyleTreeWidgetItem::toggleDefStyle()
599 {
600  if (*currentStyle == *defaultStyle) {
601  KMessageBox::information(treeWidget(), i18n("\"Use Default Style\" will be automatically unset when you change any style properties."), i18n("Kate Styles"), QStringLiteral("Kate hl config use defaults"));
602  } else {
603  currentStyle = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute(*defaultStyle));
604  updateStyle();
605 
606  QModelIndex currentIndex = treeWidget()->currentIndex();
607  while (currentIndex.isValid()) {
608  treeWidget()->update(currentIndex);
609  currentIndex = currentIndex.sibling(currentIndex.row(), currentIndex.column() - 1);
610  }
611  }
612 }
613 
614 void KateStyleTreeWidgetItem::setColor(int column)
615 {
616  QColor c; // use this
617  QColor d; // default color
618  if (column == Foreground) {
619  c = currentStyle->foreground().color();
620  d = defaultStyle->foreground().color();
621  } else if (column == SelectedForeground) {
622  c = currentStyle->selectedForeground().color();
623  d = defaultStyle->selectedForeground().color();
624  } else if (column == Background) {
625  c = currentStyle->background().color();
626  d = defaultStyle->background().color();
627  } else if (column == SelectedBackground) {
628  c = currentStyle->selectedBackground().color();
629  d = defaultStyle->selectedBackground().color();
630  }
631 
632  if (!c.isValid()) {
633  c = d;
634  }
635 
636  const QColor selectedColor = QColorDialog::getColor(c, treeWidget());
637 
638  if (!selectedColor.isValid()) {
639  return;
640  }
641 
642  // if set default, and the attrib is set in the default style use it
643  // else if set default, unset it
644  // else set the selected color
645  switch (column) {
646  case Foreground:
647  currentStyle->setForeground(selectedColor);
648  break;
649  case SelectedForeground:
650  currentStyle->setSelectedForeground(selectedColor);
651  break;
652  case Background:
653  currentStyle->setBackground(selectedColor);
654  break;
655  case SelectedBackground:
656  currentStyle->setSelectedBackground(selectedColor);
657  break;
658  }
659 
660  // FIXME
661  // repaint();
662 }
663 
664 void KateStyleTreeWidgetItem::unsetColor(int colorId)
665 {
666  switch (colorId) {
667  case 1:
668  if (defaultStyle->hasProperty(QTextFormat::ForegroundBrush)) {
669  currentStyle->setForeground(defaultStyle->foreground());
670  } else {
671  currentStyle->clearProperty(QTextFormat::ForegroundBrush);
672  }
673  break;
674  case 2:
675  if (defaultStyle->hasProperty(CustomProperties::SelectedForeground)) {
676  currentStyle->setSelectedForeground(defaultStyle->selectedForeground());
677  } else {
678  currentStyle->clearProperty(CustomProperties::SelectedForeground);
679  }
680  break;
681  case 3:
682  if (currentStyle->hasProperty(QTextFormat::BackgroundBrush)) {
683  currentStyle->clearProperty(QTextFormat::BackgroundBrush);
684  }
685  break;
686  case 4:
687  if (currentStyle->hasProperty(CustomProperties::SelectedBackground)) {
688  currentStyle->clearProperty(CustomProperties::SelectedBackground);
689  }
690  break;
691  }
692 
693  updateStyle();
694 
695  treeWidget()->emitChanged();
696 }
697 
698 KateStyleTreeWidget *KateStyleTreeWidgetItem::treeWidget() const
699 {
700  return static_cast<KateStyleTreeWidget *>(QTreeWidgetItem::treeWidget());
701 }
702 // END
void setBackground(int column, const QBrush &brush)
const QPalette & palette() const const
void fillRect(const QRectF &rectangle, const QBrush &brush)
A class which provides customized text decorations.
Definition: attribute.h:142
void setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color)
QTreeWidgetItem * child(int index) const const
QObject * sender() const const
void setChecked(bool)
QStyle * style() const const
SE_PushButtonContents
T value() const const
virtual QVariant data(int column, int role) const const
const QPoint & globalPos() const const
QTreeWidget that automatically adds columns for KateStyleListItems and provides a popup menu and a sl...
void update()
KCRASH_EXPORT void setFlags(KCrash::CrashFlags flags)
bool isValid() const const
virtual void showEvent(QShowEvent *event)
QExplicitlySharedDataPointer< Attribute > Ptr
Shared data pointer for Attribute.
Definition: attribute.h:148
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setPen(const QColor &color)
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const const override
DisplayRole
int row() const const
MESSAGECORE_EXPORT KMime::Content * firstChild(const KMime::Content *node)
virtual QVariant data(const QModelIndex &index, int role) const const =0
QTreeWidget * treeWidget() const const
void setData(const QVariant &userData)
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QColorDialog::ColorDialogOptions options)
void setCheckable(bool)
bool contains(const T &value) const const
void move(int x, int y)
QString i18n(const char *text, const TYPE &arg...)
const QPoint & pos() const const
const QAbstractItemModel * model() const const
QModelIndex sibling(int row, int column) const const
virtual bool event(QEvent *e) override
int column() const const
bool isEmpty() const const
CheckState
void setText(const QString &text)
void edit(const QModelIndex &index)
QIcon fromTheme(const QString &name)
void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QVariant::Type type() const const
void setForeground(int column, const QBrush &brush)
bool isValid() const const
ItemIsSelectable
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Nov 27 2020 22:59:30 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.