• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

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

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal