KTextEditor

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

KDE's Doxygen guidelines are available online.