Kate
katestyletreewidget.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org> 00003 Copyright (C) 2002, 2003 Anders Lund <anders.lund@lund.tdcadsl.dk> 00004 Copyright (C) 2005-2006 Hamish Rodda <rodda@kde.org> 00005 Copyright (C) 2007 Mirko Stocker <me@misto.ch> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License version 2 as published by the Free Software Foundation. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "katestyletreewidget.h" 00023 00024 #include <QtGui/QPainter> 00025 #include <QtGui/QKeyEvent> 00026 #include <QtGui/QAction> 00027 #include <QtGui/QStyledItemDelegate> 00028 #include <QtGui/QHeaderView> 00029 00030 #include <klocale.h> 00031 #include <kicon.h> 00032 #include <kcolorscheme.h> 00033 #include <kmenu.h> 00034 #include <kmessagebox.h> 00035 #include <kcolordialog.h> 00036 00037 #include "kateconfig.h" 00038 #include "kateextendedattribute.h" 00039 00040 //BEGIN KateStyleTreeDelegate 00041 class KateStyleTreeDelegate : public QStyledItemDelegate 00042 { 00043 public: 00044 KateStyleTreeDelegate(KateStyleTreeWidget* widget); 00045 00046 virtual void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; 00047 00048 private: 00049 QBrush getBrushForColorColumn(const QModelIndex& index, int column) const; 00050 KateStyleTreeWidget* m_widget; 00051 }; 00052 //END 00053 00054 //BEGIN KateStyleTreeWidgetItem decl 00055 /* 00056 QListViewItem subclass to display/edit a style, bold/italic is check boxes, 00057 normal and selected colors are boxes, which will display a color chooser when 00058 activated. 00059 The context name for the style will be drawn using the editor default font and 00060 the chosen colors. 00061 This widget id designed to handle the default as well as the individual hl style 00062 lists. 00063 This widget is designed to work with the KateStyleTreeWidget class exclusively. 00064 Added by anders, jan 23 2002. 00065 */ 00066 class KateStyleTreeWidgetItem : public QTreeWidgetItem 00067 { 00068 public: 00069 KateStyleTreeWidgetItem( QTreeWidgetItem *parent, const QString& styleName, KTextEditor::Attribute::Ptr defaultstyle, KateExtendedAttribute::Ptr data = KateExtendedAttribute::Ptr() ); 00070 KateStyleTreeWidgetItem( QTreeWidget *parent, const QString& styleName, KTextEditor::Attribute::Ptr defaultstyle, KateExtendedAttribute::Ptr data = KateExtendedAttribute::Ptr() ); 00071 ~KateStyleTreeWidgetItem() {} 00072 00073 enum columns { 00074 Context = 0, 00075 Bold, 00076 Italic, 00077 Underline, 00078 StrikeOut, 00079 Foreground, 00080 SelectedForeground, 00081 Background, 00082 SelectedBackground, 00083 UseDefaultStyle, 00084 NumColumns 00085 }; 00086 00087 /* initializes the style from the default and the hldata */ 00088 void initStyle(); 00089 /* updates the hldata's style */ 00090 void updateStyle(); 00091 /* For bool fields, toggles them, for color fields, display a color chooser */ 00092 void changeProperty( int p ); 00096 void unsetColor( int c ); 00097 /* style context name */ 00098 QString contextName() const { return text(0); } 00099 /* only true for a hl mode item using it's default style */ 00100 bool defStyle() const; 00101 /* true for default styles */ 00102 bool isDefault() const; 00103 /* whichever style is active (currentStyle for hl mode styles not using 00104 the default style, defaultStyle otherwise) */ 00105 KTextEditor::Attribute::Ptr style() const { return currentStyle; } 00106 00107 virtual QVariant data( int column, int role ) const; 00108 00109 KateStyleTreeWidget* treeWidget() const; 00110 00111 private: 00112 /* private methods to change properties */ 00113 void toggleDefStyle(); 00114 void setColor( int ); 00115 /* helper function to copy the default style into the KateExtendedAttribute, 00116 when a property is changed and we are using default style. */ 00117 00118 KTextEditor::Attribute::Ptr currentStyle, // the style currently in use (was "is") 00119 defaultStyle; // default style for hl mode contexts and default styles (was "ds") 00120 KateExtendedAttribute::Ptr actualStyle; // itemdata for hl mode contexts (was "st") 00121 }; 00122 //END 00123 00124 00125 //BEGIN KateStyleTreeWidget 00126 KateStyleTreeWidget::KateStyleTreeWidget( QWidget *parent, bool showUseDefaults ) 00127 : QTreeWidget( parent ) 00128 { 00129 setItemDelegate(new KateStyleTreeDelegate(this)); 00130 00131 QStringList headers; 00132 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"); 00133 if(showUseDefaults) { 00134 headers << i18n("Use Default Style"); 00135 } 00136 00137 setHeaderLabels(headers); 00138 00139 headerItem()->setIcon(1, KIcon("format-text-bold")); 00140 headerItem()->setIcon(2, KIcon("format-text-italic")); 00141 headerItem()->setIcon(3, KIcon("format-text-underline")); 00142 headerItem()->setIcon(4, KIcon("format-text-strikethrough")); 00143 00144 // grap the bg color, selected color and default font 00145 normalcol = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color(); 00146 bgcol = KateRendererConfig::global()->backgroundColor(); 00147 selcol = KateRendererConfig::global()->selectionColor(); 00148 docfont = KateRendererConfig::global()->font(); 00149 00150 QPalette pal = viewport()->palette(); 00151 pal.setColor(QPalette::Background, bgcol); 00152 viewport()->setPalette( pal ); 00153 } 00154 00155 QIcon brushIcon(const QColor& color) 00156 { 00157 QPixmap pm(16,16); 00158 QRect all(0,0,15,15); 00159 { 00160 QPainter p(&pm); 00161 p.fillRect(all, color); 00162 p.setPen(Qt::black); 00163 p.drawRect(all); 00164 } 00165 return QIcon(pm); 00166 } 00167 00168 bool KateStyleTreeWidget::edit( const QModelIndex & index, EditTrigger trigger, QEvent * event ) 00169 { 00170 if(index.column() == KateStyleTreeWidgetItem::Context) 00171 return false; 00172 00173 KateStyleTreeWidgetItem *i = dynamic_cast<KateStyleTreeWidgetItem*>(itemFromIndex(index)); 00174 if (!i) 00175 return QTreeWidget::edit(index, trigger, event); 00176 00177 switch (trigger) { 00178 case QAbstractItemView::DoubleClicked: 00179 case QAbstractItemView::SelectedClicked: 00180 case QAbstractItemView::EditKeyPressed: 00181 i->changeProperty(index.column()); 00182 update(index); 00183 update(index.sibling(index.row(), KateStyleTreeWidgetItem::Context)); 00184 return false; 00185 default: 00186 return QTreeWidget::edit(index, trigger, event); 00187 } 00188 } 00189 00190 void KateStyleTreeWidget::resizeColumns() 00191 { 00192 for (int i = 0; i < columnCount(); ++i) 00193 resizeColumnToContents(i); 00194 } 00195 00196 void KateStyleTreeWidget::showEvent( QShowEvent * event ) 00197 { 00198 QTreeWidget::showEvent(event); 00199 00200 resizeColumns(); 00201 } 00202 00203 void KateStyleTreeWidget::contextMenuEvent( QContextMenuEvent * event ) 00204 { 00205 KateStyleTreeWidgetItem *i = dynamic_cast<KateStyleTreeWidgetItem*>(itemAt(event->pos())); 00206 if (!i) return; 00207 00208 KMenu m( this ); 00209 KTextEditor::Attribute::Ptr currentStyle = i->style(); 00210 // the title is used, because the menu obscures the context name when 00211 // displayed on behalf of spacePressed(). 00212 QPainter p; 00213 p.setPen(Qt::black); 00214 00215 QIcon cl = brushIcon( i->style()->foreground().color() ); 00216 QIcon scl = brushIcon( i->style()->selectedForeground().color() ); 00217 QIcon bgcl = brushIcon( i->style()->hasProperty(QTextFormat::BackgroundBrush) ? i->style()->background().color() : viewport()->palette().base().color() ); 00218 QIcon sbgcl = brushIcon( i->style()->hasProperty(KTextEditor::Attribute::SelectedBackground) ? i->style()->selectedBackground().color() : viewport()->palette().base().color() ); 00219 00220 m.addTitle( i->contextName() ); 00221 00222 QAction* a = m.addAction( i18n("&Bold"), this, SLOT(changeProperty()) ); 00223 a->setCheckable(true); 00224 a->setChecked( currentStyle->fontBold() ); 00225 a->setData(KateStyleTreeWidgetItem::Bold); 00226 00227 a = m.addAction( i18n("&Italic"), this, SLOT(changeProperty()) ); 00228 a->setCheckable(true); 00229 a->setChecked( currentStyle->fontItalic() ); 00230 a->setData(KateStyleTreeWidgetItem::Italic); 00231 00232 a = m.addAction( i18n("&Underline"), this, SLOT(changeProperty()) ); 00233 a->setCheckable(true); 00234 a->setChecked( currentStyle->fontUnderline() ); 00235 a->setData(KateStyleTreeWidgetItem::Underline); 00236 00237 a = m.addAction( i18n("S&trikeout"), this, SLOT(changeProperty()) ); 00238 a->setCheckable(true); 00239 a->setChecked( currentStyle->fontStrikeOut() ); 00240 a->setData(KateStyleTreeWidgetItem::StrikeOut); 00241 00242 m.addSeparator(); 00243 00244 a = m.addAction( cl, i18n("Normal &Color..."), this, SLOT(changeProperty()) ); 00245 a->setData(KateStyleTreeWidgetItem::Foreground); 00246 00247 a = m.addAction( scl, i18n("&Selected Color..."), this, SLOT(changeProperty()) ); 00248 a->setData(KateStyleTreeWidgetItem::SelectedForeground); 00249 00250 a = m.addAction( bgcl, i18n("&Background Color..."), this, SLOT(changeProperty()) ); 00251 a->setData(KateStyleTreeWidgetItem::Background); 00252 00253 a = m.addAction( sbgcl, i18n("S&elected Background Color..."), this, SLOT(changeProperty()) ); 00254 a->setData(KateStyleTreeWidgetItem::SelectedBackground); 00255 00256 // Unset [some] colors. I could show one only if that button was clicked, but that 00257 // would disable setting this with the keyboard (how many aren't doing just 00258 // that every day? ;) 00259 // ANY ideas for doing this in a nicer way will be warmly wellcomed. 00260 KTextEditor::Attribute::Ptr style = i->style(); 00261 if ( style->hasProperty( QTextFormat::BackgroundBrush) || style->hasProperty( KTextEditor::Attribute::SelectedBackground ) ) 00262 { 00263 m.addSeparator(); 00264 if ( style->hasProperty( QTextFormat::BackgroundBrush) ) { 00265 a = m.addAction( i18n("Unset Background Color"), this, SLOT(unsetColor()) ); 00266 a->setData(100); 00267 } 00268 if ( style->hasProperty( KTextEditor::Attribute::SelectedBackground ) ) { 00269 a = m.addAction( i18n("Unset Selected Background Color"), this, SLOT(unsetColor()) ); 00270 a->setData(101); 00271 } 00272 } 00273 00274 if ( ! i->isDefault() && ! i->defStyle() ) { 00275 m.addSeparator(); 00276 a = m.addAction( i18n("Use &Default Style"), this, SLOT(changeProperty()) ); 00277 a->setCheckable(true); 00278 a->setChecked( i->defStyle() ); 00279 a->setData(KateStyleTreeWidgetItem::UseDefaultStyle); 00280 } 00281 m.exec( event->globalPos() ); 00282 } 00283 00284 void KateStyleTreeWidget::changeProperty() 00285 { 00286 ((KateStyleTreeWidgetItem*)currentItem())->changeProperty( static_cast<QAction*>(sender())->data().toInt() ); 00287 } 00288 00289 void KateStyleTreeWidget::unsetColor() 00290 { 00291 ((KateStyleTreeWidgetItem*)currentItem())->unsetColor( static_cast<QAction*>(sender())->data().toInt() ); 00292 } 00293 00294 void KateStyleTreeWidget::updateGroupHeadings() 00295 { 00296 for(int i = 0; i < topLevelItemCount(); i++) { 00297 QTreeWidgetItem* currentTopLevelItem = topLevelItem(i); 00298 QTreeWidgetItem* firstChild = currentTopLevelItem->child(0); 00299 00300 if(firstChild) { 00301 QColor foregroundColor = firstChild->data(KateStyleTreeWidgetItem::Foreground, Qt::DisplayRole).value<QColor>(); 00302 QColor backgroundColor = firstChild->data(KateStyleTreeWidgetItem::Background, Qt::DisplayRole).value<QColor>(); 00303 00304 currentTopLevelItem->setForeground(KateStyleTreeWidgetItem::Context, foregroundColor); 00305 00306 if(backgroundColor.isValid()) { 00307 currentTopLevelItem->setBackground(KateStyleTreeWidgetItem::Context, backgroundColor); 00308 } 00309 } 00310 } 00311 } 00312 00313 void KateStyleTreeWidget::emitChanged( ) 00314 { 00315 updateGroupHeadings(); 00316 emit changed(); 00317 } 00318 00319 void KateStyleTreeWidget::addItem( const QString & styleName, KTextEditor::Attribute::Ptr defaultstyle, KateExtendedAttribute::Ptr data ) 00320 { 00321 new KateStyleTreeWidgetItem(this, styleName, defaultstyle, data); 00322 } 00323 00324 void KateStyleTreeWidget::addItem( QTreeWidgetItem * parent, const QString & styleName, KTextEditor::Attribute::Ptr defaultstyle, KateExtendedAttribute::Ptr data ) 00325 { 00326 new KateStyleTreeWidgetItem(parent, styleName, defaultstyle, data); 00327 updateGroupHeadings(); 00328 } 00329 //END 00330 00331 //BEGIN KateStyleTreeWidgetItem 00332 static const int BoxSize = 16; 00333 static const int ColorBtnWidth = 32; 00334 00335 KateStyleTreeDelegate::KateStyleTreeDelegate(KateStyleTreeWidget* widget) 00336 : m_widget(widget) 00337 { 00338 } 00339 00340 QBrush KateStyleTreeDelegate::getBrushForColorColumn(const QModelIndex& index, int column) const 00341 { 00342 QModelIndex colorIndex = index.sibling(index.row(), column); 00343 QVariant displayData = colorIndex.model()->data(colorIndex); 00344 return qVariantValue<QBrush>(displayData); 00345 } 00346 00347 void KateStyleTreeDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const 00348 { 00349 static QSet<int> columns; 00350 if (!columns.count()) 00351 columns << KateStyleTreeWidgetItem::Foreground << KateStyleTreeWidgetItem::SelectedForeground << KateStyleTreeWidgetItem::Background << KateStyleTreeWidgetItem::SelectedBackground; 00352 00353 if(index.column() == KateStyleTreeWidgetItem::Context) { 00354 QStyleOptionViewItem styleContextItem(option); 00355 00356 QBrush brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedBackground); 00357 if(brush != QBrush()) { 00358 styleContextItem.palette.setBrush(QPalette::Highlight, brush); 00359 } else { 00360 styleContextItem.palette.setBrush(QPalette::Highlight, QBrush(KateRendererConfig::global()->selectionColor())); 00361 } 00362 00363 brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedForeground); 00364 if(brush != QBrush()) { 00365 styleContextItem.palette.setBrush(QPalette::HighlightedText, brush); 00366 } 00367 00368 return QStyledItemDelegate::paint(painter, styleContextItem, index); 00369 } 00370 00371 if (!columns.contains(index.column())) { 00372 return QStyledItemDelegate::paint(painter, option, index); 00373 } 00374 00375 QVariant displayData = index.model()->data(index); 00376 if (displayData.type() != QVariant::Brush) 00377 return QStyledItemDelegate::paint(painter, option, index); 00378 00379 QBrush brush = qVariantValue<QBrush>(displayData); 00380 00381 QStyleOptionButton opt; 00382 opt.rect = option.rect; 00383 opt.palette = m_widget->palette(); 00384 00385 bool set = brush != QBrush(); 00386 00387 if (!set) { 00388 opt.text = i18nc("No text or background color set", "None set"); 00389 brush = Qt::white; 00390 } 00391 00392 if(index.row() == m_widget->currentIndex().row() && m_widget->currentItem()->isSelected() && m_widget->currentItem()->childCount() == 0) { 00393 painter->fillRect(opt.rect, KColorScheme(QPalette::Active, KColorScheme::Selection).background()); 00394 } 00395 00396 m_widget->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_widget); 00397 00398 if (set) 00399 painter->fillRect(m_widget->style()->subElementRect(QStyle::SE_PushButtonContents, &opt,m_widget), brush); 00400 } 00401 00402 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem( QTreeWidgetItem *parent, const QString & stylename, 00403 KTextEditor::Attribute::Ptr defaultAttribute, KateExtendedAttribute::Ptr actualAttribute ) 00404 : QTreeWidgetItem( parent ), 00405 currentStyle( 0L ), 00406 defaultStyle( defaultAttribute ), 00407 actualStyle( actualAttribute ) 00408 { 00409 initStyle(); 00410 setText(0, stylename); 00411 } 00412 00413 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem( QTreeWidget *parent, const QString & stylename, 00414 KTextEditor::Attribute::Ptr defaultAttribute, KateExtendedAttribute::Ptr actualAttribute ) 00415 : QTreeWidgetItem( parent ), 00416 currentStyle( 0L ), 00417 defaultStyle( defaultAttribute ), 00418 actualStyle( actualAttribute ) 00419 { 00420 initStyle(); 00421 setText(0, stylename); 00422 } 00423 00424 void KateStyleTreeWidgetItem::initStyle() 00425 { 00426 if (!actualStyle) 00427 { 00428 currentStyle = defaultStyle; 00429 } 00430 else 00431 { 00432 currentStyle = new KTextEditor::Attribute (*defaultStyle); 00433 00434 if (actualStyle->hasAnyProperty()) 00435 *currentStyle += *actualStyle; 00436 } 00437 00438 setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); 00439 } 00440 00441 static Qt::CheckState toCheckState(bool b) { 00442 return b ? Qt::Checked : Qt::Unchecked; 00443 } 00444 00445 QVariant KateStyleTreeWidgetItem::data( int column, int role ) const 00446 { 00447 if (column == Context) { 00448 switch (role) { 00449 case Qt::ForegroundRole: 00450 if (style()->hasProperty(QTextFormat::ForegroundBrush)) 00451 return style()->foreground().color(); 00452 break; 00453 00454 case Qt::BackgroundRole: 00455 if (style()->hasProperty(QTextFormat::BackgroundBrush)) 00456 return style()->background().color(); 00457 break; 00458 00459 case Qt::FontRole: 00460 return style()->font(); 00461 break; 00462 } 00463 } 00464 00465 if (role == Qt::CheckStateRole) { 00466 switch (column) { 00467 case Bold: 00468 return toCheckState(style()->fontBold()); 00469 case Italic: 00470 return toCheckState(style()->fontItalic()); 00471 case Underline: 00472 return toCheckState(style()->fontUnderline()); 00473 case StrikeOut: 00474 return toCheckState(style()->fontStrikeOut()); 00475 case UseDefaultStyle: 00476 /* can't compare all attributes, currentStyle has always more than defaultStyle (e.g. the item's name), 00477 * so we just compare the important ones:*/ 00478 return toCheckState( 00479 currentStyle->foreground() == defaultStyle->foreground() 00480 && currentStyle->background() == defaultStyle->background() 00481 && currentStyle->selectedForeground() == defaultStyle->selectedForeground() 00482 && currentStyle->selectedBackground() == defaultStyle->selectedBackground() 00483 && currentStyle->fontBold() == defaultStyle->fontBold() 00484 && currentStyle->fontItalic() == defaultStyle->fontItalic() 00485 && currentStyle->fontUnderline() == defaultStyle->fontUnderline() 00486 && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut()); 00487 } 00488 } 00489 00490 if (role == Qt::DisplayRole) { 00491 switch (column) { 00492 case Foreground: 00493 return style()->foreground(); 00494 case SelectedForeground: 00495 return style()->selectedForeground(); 00496 case Background: 00497 return style()->background(); 00498 case SelectedBackground: 00499 return style()->selectedBackground(); 00500 } 00501 } 00502 00503 return QTreeWidgetItem::data(column, role); 00504 } 00505 00506 void KateStyleTreeWidgetItem::updateStyle() 00507 { 00508 // nothing there, not update it, will crash 00509 if (!actualStyle) 00510 return; 00511 00512 if ( currentStyle->hasProperty(QTextFormat::FontWeight) ) 00513 { 00514 if ( currentStyle->fontWeight() != actualStyle->fontWeight()) 00515 actualStyle->setFontWeight( currentStyle->fontWeight() ); 00516 } 00517 else actualStyle->clearProperty( QTextFormat::FontWeight ); 00518 00519 if ( currentStyle->hasProperty(QTextFormat::FontItalic) ) 00520 { 00521 if ( currentStyle->fontItalic() != actualStyle->fontItalic()) 00522 actualStyle->setFontItalic( currentStyle->fontItalic() ); 00523 } 00524 else actualStyle->clearProperty( QTextFormat::FontItalic ); 00525 00526 if ( currentStyle->hasProperty(QTextFormat::FontStrikeOut) ) 00527 { 00528 if ( currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut()) 00529 actualStyle->setFontStrikeOut( currentStyle->fontStrikeOut() ); 00530 } 00531 else actualStyle->clearProperty( QTextFormat::FontStrikeOut ); 00532 00533 if ( currentStyle->hasProperty(QTextFormat::FontUnderline) ) 00534 { 00535 if ( currentStyle->fontUnderline() != actualStyle->fontUnderline()) 00536 actualStyle->setFontUnderline( currentStyle->fontUnderline() ); 00537 } 00538 else actualStyle->clearProperty( QTextFormat::FontUnderline ); 00539 00540 if ( currentStyle->hasProperty(KTextEditor::Attribute::Outline) ) 00541 { 00542 if ( currentStyle->outline() != actualStyle->outline()) 00543 actualStyle->setOutline( currentStyle->outline() ); 00544 } 00545 else actualStyle->clearProperty( KTextEditor::Attribute::Outline ); 00546 00547 if ( currentStyle->hasProperty(QTextFormat::ForegroundBrush) ) 00548 { 00549 if ( currentStyle->foreground() != actualStyle->foreground()) 00550 actualStyle->setForeground( currentStyle->foreground() ); 00551 } 00552 else actualStyle->clearProperty( QTextFormat::ForegroundBrush ); 00553 00554 if ( currentStyle->hasProperty(KTextEditor::Attribute::SelectedForeground) ) 00555 { 00556 if ( currentStyle->selectedForeground() != actualStyle->selectedForeground()) 00557 actualStyle->setSelectedForeground( currentStyle->selectedForeground() ); 00558 } 00559 else actualStyle->clearProperty( KTextEditor::Attribute::SelectedForeground ); 00560 00561 if ( currentStyle->hasProperty(QTextFormat::BackgroundBrush) ) 00562 { 00563 if ( currentStyle->background() != actualStyle->background()) 00564 actualStyle->setBackground( currentStyle->background() ); 00565 } 00566 else actualStyle->clearProperty( QTextFormat::BackgroundBrush ); 00567 00568 if ( currentStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) ) 00569 { 00570 if ( currentStyle->selectedBackground() != actualStyle->selectedBackground()) 00571 actualStyle->setSelectedBackground( currentStyle->selectedBackground() ); 00572 } 00573 else actualStyle->clearProperty( KTextEditor::Attribute::SelectedBackground ); 00574 } 00575 00576 /* only true for a hl mode item using it's default style */ 00577 bool KateStyleTreeWidgetItem::defStyle() const { return actualStyle && actualStyle->properties() != defaultStyle->properties(); } 00578 00579 /* true for default styles */ 00580 bool KateStyleTreeWidgetItem::isDefault() const { return actualStyle ? false : true; } 00581 00582 void KateStyleTreeWidgetItem::changeProperty( int p ) 00583 { 00584 if ( p == Bold ) 00585 currentStyle->setFontBold( ! currentStyle->fontBold() ); 00586 else if ( p == Italic ) 00587 currentStyle->setFontItalic( ! currentStyle->fontItalic() ); 00588 else if ( p == Underline ) 00589 currentStyle->setFontUnderline( ! currentStyle->fontUnderline() ); 00590 else if ( p == StrikeOut ) 00591 currentStyle->setFontStrikeOut( ! currentStyle->fontStrikeOut() ); 00592 else if ( p == UseDefaultStyle ) 00593 toggleDefStyle(); 00594 else 00595 setColor( p ); 00596 00597 updateStyle (); 00598 00599 treeWidget()->emitChanged(); 00600 } 00601 00602 void KateStyleTreeWidgetItem::toggleDefStyle() 00603 { 00604 if ( *currentStyle == *defaultStyle ) { 00605 KMessageBox::information( treeWidget(), 00606 i18n("\"Use Default Style\" will be automatically unset when you change any style properties."), 00607 i18n("Kate Styles"), 00608 "Kate hl config use defaults" ); 00609 } 00610 else { 00611 currentStyle = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute( *defaultStyle )); 00612 updateStyle(); 00613 00614 QModelIndex currentIndex = treeWidget()->currentIndex(); 00615 while(currentIndex.isValid()) { 00616 treeWidget()->update(currentIndex); 00617 currentIndex = currentIndex.sibling(currentIndex.row(), currentIndex.column() - 1); 00618 } 00619 } 00620 } 00621 00622 void KateStyleTreeWidgetItem::setColor( int column ) 00623 { 00624 QColor c; // use this 00625 QColor d; // default color 00626 if ( column == Foreground) 00627 { 00628 c = currentStyle->foreground().color(); 00629 d = defaultStyle->foreground().color(); 00630 } 00631 else if ( column == SelectedForeground ) 00632 { 00633 c = currentStyle->selectedForeground().color(); 00634 d = defaultStyle->selectedForeground().color(); 00635 } 00636 else if ( column == Background ) 00637 { 00638 c = currentStyle->background().color(); 00639 d = defaultStyle->background().color(); 00640 } 00641 else if ( column == SelectedBackground ) 00642 { 00643 c = currentStyle->selectedBackground().color(); 00644 d = defaultStyle->selectedBackground().color(); 00645 } 00646 00647 if ( KColorDialog::getColor( c, d, treeWidget() ) != QDialog::Accepted) return; 00648 00649 bool def = ! c.isValid(); 00650 00651 // if set default, and the attrib is set in the default style use it 00652 // else if set default, unset it 00653 // else set the selected color 00654 switch (column) 00655 { 00656 case Foreground: 00657 if ( def ) 00658 { 00659 if ( defaultStyle->hasProperty(QTextFormat::ForegroundBrush) ) 00660 currentStyle->setForeground( defaultStyle->foreground()); 00661 else 00662 currentStyle->clearProperty(QTextFormat::ForegroundBrush); 00663 } 00664 else 00665 currentStyle->setForeground( c ); 00666 break; 00667 case SelectedForeground: 00668 if ( def ) 00669 { 00670 if ( defaultStyle->hasProperty(KTextEditor::Attribute::SelectedForeground) ) 00671 currentStyle->setSelectedForeground( defaultStyle->selectedForeground()); 00672 else 00673 currentStyle->clearProperty(KTextEditor::Attribute::SelectedForeground); 00674 } 00675 else 00676 currentStyle->setSelectedForeground( c ); 00677 break; 00678 case Background: 00679 if ( def ) 00680 { 00681 if ( defaultStyle->hasProperty(QTextFormat::BackgroundBrush) ) 00682 currentStyle->setBackground( defaultStyle->background()); 00683 else 00684 currentStyle->clearProperty(QTextFormat::BackgroundBrush); 00685 } 00686 else 00687 currentStyle->setBackground( c ); 00688 break; 00689 case SelectedBackground: 00690 if ( def ) 00691 { 00692 if ( defaultStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) ) 00693 currentStyle->setSelectedBackground( defaultStyle->selectedBackground()); 00694 else 00695 currentStyle->clearProperty(KTextEditor::Attribute::SelectedBackground); 00696 } 00697 else 00698 currentStyle->setSelectedBackground( c ); 00699 break; 00700 } 00701 00702 //FIXME 00703 //repaint(); 00704 } 00705 00706 void KateStyleTreeWidgetItem::unsetColor( int c ) 00707 { 00708 if ( c == 100 && currentStyle->hasProperty(QTextFormat::BackgroundBrush) ) 00709 currentStyle->clearProperty(QTextFormat::BackgroundBrush); 00710 else if ( c == 101 && currentStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) ) 00711 currentStyle->clearProperty(KTextEditor::Attribute::SelectedBackground); 00712 updateStyle(); 00713 00714 treeWidget()->emitChanged(); 00715 } 00716 00717 KateStyleTreeWidget* KateStyleTreeWidgetItem::treeWidget() const 00718 { 00719 return static_cast<KateStyleTreeWidget*>(QTreeWidgetItem::treeWidget()); 00720 } 00721 //END 00722 00723 #include "katestyletreewidget.moc"
KDE 4.6 API Reference