• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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       } else {
00309         currentTopLevelItem->setBackground(KateStyleTreeWidgetItem::Context, bgcol);
00310       }
00311     }
00312   }
00313 }
00314 
00315 void KateStyleTreeWidget::emitChanged( )
00316 {
00317   updateGroupHeadings();
00318   emit changed();
00319 }
00320 
00321 void KateStyleTreeWidget::addItem( const QString & styleName, KTextEditor::Attribute::Ptr  defaultstyle, KateExtendedAttribute::Ptr  data )
00322 {
00323   new KateStyleTreeWidgetItem(this, styleName, defaultstyle, data);
00324 }
00325 
00326 void KateStyleTreeWidget::addItem( QTreeWidgetItem * parent, const QString & styleName, KTextEditor::Attribute::Ptr  defaultstyle, KateExtendedAttribute::Ptr  data )
00327 {
00328   new KateStyleTreeWidgetItem(parent, styleName, defaultstyle, data);
00329   updateGroupHeadings();
00330 }
00331 //END
00332 
00333 //BEGIN KateStyleTreeWidgetItem
00334 static const int BoxSize = 16;
00335 static const int ColorBtnWidth = 32;
00336 
00337 KateStyleTreeDelegate::KateStyleTreeDelegate(KateStyleTreeWidget* widget)
00338   : m_widget(widget)
00339 {
00340 }
00341 
00342 QBrush KateStyleTreeDelegate::getBrushForColorColumn(const QModelIndex& index, int column) const
00343 {
00344   QModelIndex colorIndex = index.sibling(index.row(), column);
00345   QVariant displayData = colorIndex.model()->data(colorIndex);
00346   return qVariantValue<QBrush>(displayData);
00347 }
00348 
00349 void KateStyleTreeDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
00350 {
00351   static QSet<int> columns;
00352   if (!columns.count())
00353     columns << KateStyleTreeWidgetItem::Foreground << KateStyleTreeWidgetItem::SelectedForeground << KateStyleTreeWidgetItem::Background << KateStyleTreeWidgetItem::SelectedBackground;
00354 
00355   if(index.column() == KateStyleTreeWidgetItem::Context) {
00356     QStyleOptionViewItem styleContextItem(option);
00357 
00358     QBrush brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedBackground);
00359     if(brush != QBrush()) {
00360       styleContextItem.palette.setBrush(QPalette::Highlight, brush);
00361     } else {
00362       styleContextItem.palette.setBrush(QPalette::Highlight, QBrush(KateRendererConfig::global()->selectionColor()));
00363     }
00364 
00365     brush = getBrushForColorColumn(index, KateStyleTreeWidgetItem::SelectedForeground);
00366     if(brush != QBrush()) {
00367       styleContextItem.palette.setBrush(QPalette::HighlightedText, brush);
00368     }
00369     
00370     return QStyledItemDelegate::paint(painter, styleContextItem, index);
00371   }
00372   
00373   if (!columns.contains(index.column())) {
00374     return QStyledItemDelegate::paint(painter, option, index);
00375   }
00376 
00377   QVariant displayData = index.model()->data(index);
00378   if (displayData.type() != QVariant::Brush)
00379     return QStyledItemDelegate::paint(painter, option, index);
00380 
00381   QBrush brush = qVariantValue<QBrush>(displayData);
00382 
00383   QStyleOptionButton opt;
00384   opt.rect = option.rect;
00385   opt.palette = m_widget->palette();
00386 
00387   bool set = brush != QBrush();
00388 
00389   if (!set) {
00390     opt.text = i18nc("No text or background colour set", "None set");
00391     brush = Qt::white;
00392   }
00393   
00394   if(index.row() == m_widget->currentIndex().row() && m_widget->currentItem()->isSelected() && m_widget->currentItem()->childCount() == 0) {
00395     painter->fillRect(opt.rect, KColorScheme(QPalette::Active, KColorScheme::Selection).background());
00396   }
00397   
00398   m_widget->style()->drawControl(QStyle::CE_PushButton, &opt, painter, m_widget);
00399 
00400   if (set)
00401     painter->fillRect(m_widget->style()->subElementRect(QStyle::SE_PushButtonContents, &opt,m_widget), brush);
00402 }
00403 
00404 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem( QTreeWidgetItem *parent, const QString & stylename,
00405                               KTextEditor::Attribute::Ptr defaultAttribute, KateExtendedAttribute::Ptr actualAttribute )
00406         : QTreeWidgetItem( parent ),
00407           currentStyle( 0L ),
00408           defaultStyle( defaultAttribute ),
00409           actualStyle( actualAttribute )
00410 {
00411   initStyle();
00412   setText(0, stylename);
00413 }
00414 
00415 KateStyleTreeWidgetItem::KateStyleTreeWidgetItem( QTreeWidget *parent, const QString & stylename,
00416                               KTextEditor::Attribute::Ptr defaultAttribute, KateExtendedAttribute::Ptr actualAttribute )
00417         : QTreeWidgetItem( parent ),
00418           currentStyle( 0L ),
00419           defaultStyle( defaultAttribute ),
00420           actualStyle( actualAttribute )
00421 {
00422   initStyle();
00423   setText(0, stylename);
00424 }
00425 
00426 void KateStyleTreeWidgetItem::initStyle()
00427 {
00428   if (!actualStyle)
00429   {
00430     currentStyle = defaultStyle;
00431   }
00432   else
00433   {
00434     currentStyle = new KTextEditor::Attribute (*defaultStyle);
00435 
00436     if (actualStyle->hasAnyProperty())
00437       *currentStyle += *actualStyle;
00438   }
00439 
00440   setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
00441 }
00442 
00443 QVariant KateStyleTreeWidgetItem::data( int column, int role ) const
00444 {
00445   if (column == Context) {
00446     switch (role) {
00447       case Qt::ForegroundRole:
00448         if (style()->hasProperty(QTextFormat::ForegroundBrush))
00449           return style()->foreground().color();
00450         break;
00451 
00452       case Qt::BackgroundRole:
00453         if (style()->hasProperty(QTextFormat::BackgroundBrush))
00454           return style()->background().color();
00455         break;
00456 
00457       case Qt::FontRole:
00458         return style()->font();
00459         break;
00460     }
00461   }
00462 
00463   if (role == Qt::CheckStateRole) {
00464     switch (column) {
00465       case Bold:
00466         return style()->fontBold();
00467       case Italic:
00468         return style()->fontItalic();
00469       case Underline:
00470         return style()->fontUnderline();
00471       case StrikeOut:
00472         return style()->fontStrikeOut();
00473       case UseDefaultStyle:
00474         /* can't compare all attributes, currentStyle has always more than defaultStyle (e.g. the item's name),
00475          * so we just compare the important ones:*/
00476         return currentStyle->foreground() == defaultStyle->foreground()
00477             && currentStyle->background() == defaultStyle->background()
00478             && currentStyle->selectedForeground() == defaultStyle->selectedForeground()
00479             && currentStyle->selectedBackground() == defaultStyle->selectedBackground()
00480             && currentStyle->fontBold() == defaultStyle->fontBold()
00481             && currentStyle->fontItalic() == defaultStyle->fontItalic()
00482             && currentStyle->fontUnderline() == defaultStyle->fontUnderline()
00483             && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut();
00484     }
00485   }
00486 
00487   if (role == Qt::DisplayRole) {
00488     switch (column) {
00489       case Foreground:
00490         return style()->foreground();
00491       case SelectedForeground:
00492         return style()->selectedForeground();
00493       case Background:
00494         return style()->background();
00495       case SelectedBackground:
00496         return style()->selectedBackground();
00497     }
00498   }
00499 
00500   return QTreeWidgetItem::data(column, role);
00501 }
00502 
00503 void KateStyleTreeWidgetItem::updateStyle()
00504 {
00505   // nothing there, not update it, will crash
00506   if (!actualStyle)
00507     return;
00508 
00509   if ( currentStyle->hasProperty(QTextFormat::FontWeight) )
00510   {
00511     if ( currentStyle->fontWeight() != actualStyle->fontWeight())
00512       actualStyle->setFontWeight( currentStyle->fontWeight() );
00513   }
00514   else actualStyle->clearProperty( QTextFormat::FontWeight );
00515 
00516   if ( currentStyle->hasProperty(QTextFormat::FontItalic) )
00517   {
00518     if ( currentStyle->fontItalic() != actualStyle->fontItalic())
00519       actualStyle->setFontItalic( currentStyle->fontItalic() );
00520   }
00521   else actualStyle->clearProperty( QTextFormat::FontItalic );
00522 
00523   if ( currentStyle->hasProperty(QTextFormat::FontStrikeOut) )
00524   {
00525     if ( currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut())
00526       actualStyle->setFontStrikeOut( currentStyle->fontStrikeOut() );
00527   }
00528   else actualStyle->clearProperty( QTextFormat::FontStrikeOut );
00529 
00530   if ( currentStyle->hasProperty(QTextFormat::FontUnderline) )
00531   {
00532     if ( currentStyle->fontUnderline() != actualStyle->fontUnderline())
00533       actualStyle->setFontUnderline( currentStyle->fontUnderline() );
00534   }
00535   else actualStyle->clearProperty( QTextFormat::FontUnderline );
00536 
00537   if ( currentStyle->hasProperty(KTextEditor::Attribute::Outline) )
00538   {
00539     if ( currentStyle->outline() != actualStyle->outline())
00540       actualStyle->setOutline( currentStyle->outline() );
00541   }
00542   else actualStyle->clearProperty( KTextEditor::Attribute::Outline );
00543 
00544   if ( currentStyle->hasProperty(QTextFormat::ForegroundBrush) )
00545   {
00546     if ( currentStyle->foreground() != actualStyle->foreground())
00547       actualStyle->setForeground( currentStyle->foreground() );
00548   }
00549   else actualStyle->clearProperty( QTextFormat::ForegroundBrush );
00550 
00551   if ( currentStyle->hasProperty(KTextEditor::Attribute::SelectedForeground) )
00552   {
00553     if ( currentStyle->selectedForeground() != actualStyle->selectedForeground())
00554       actualStyle->setSelectedForeground( currentStyle->selectedForeground() );
00555   }
00556   else actualStyle->clearProperty( KTextEditor::Attribute::SelectedForeground );
00557 
00558   if ( currentStyle->hasProperty(QTextFormat::BackgroundBrush) )
00559   {
00560     if ( currentStyle->background() != actualStyle->background())
00561       actualStyle->setBackground( currentStyle->background() );
00562   }
00563   else actualStyle->clearProperty( QTextFormat::BackgroundBrush );
00564 
00565   if ( currentStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) )
00566   {
00567     if ( currentStyle->selectedBackground() != actualStyle->selectedBackground())
00568       actualStyle->setSelectedBackground( currentStyle->selectedBackground() );
00569   }
00570   else actualStyle->clearProperty( KTextEditor::Attribute::SelectedBackground );
00571 }
00572 
00573 /* only true for a hl mode item using it's default style */
00574 bool KateStyleTreeWidgetItem::defStyle() const { return actualStyle && actualStyle->properties() != defaultStyle->properties(); }
00575 
00576 /* true for default styles */
00577 bool KateStyleTreeWidgetItem::isDefault() const { return actualStyle ? false : true; }
00578 
00579 void KateStyleTreeWidgetItem::changeProperty( int p )
00580 {
00581   if ( p == Bold )
00582     currentStyle->setFontBold( ! currentStyle->fontBold() );
00583   else if ( p == Italic )
00584     currentStyle->setFontItalic( ! currentStyle->fontItalic() );
00585   else if ( p == Underline )
00586     currentStyle->setFontUnderline( ! currentStyle->fontUnderline() );
00587   else if ( p == StrikeOut )
00588     currentStyle->setFontStrikeOut( ! currentStyle->fontStrikeOut() );
00589   else if ( p == UseDefaultStyle )
00590     toggleDefStyle();
00591   else
00592     setColor( p );
00593 
00594   updateStyle ();
00595 
00596   treeWidget()->emitChanged();
00597 }
00598 
00599 void KateStyleTreeWidgetItem::toggleDefStyle()
00600 {
00601   if ( *currentStyle == *defaultStyle ) {
00602     KMessageBox::information( treeWidget(),
00603          i18n("\"Use Default Style\" will be automatically unset when you change any style properties."),
00604          i18n("Kate Styles"),
00605          "Kate hl config use defaults" );
00606   }
00607   else {
00608     currentStyle = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute( *defaultStyle ));
00609     updateStyle();
00610     
00611     QModelIndex currentIndex = treeWidget()->currentIndex();
00612     while(currentIndex.isValid()) {
00613         treeWidget()->update(currentIndex);
00614         currentIndex = currentIndex.sibling(currentIndex.row(), currentIndex.column() - 1);
00615     }
00616   }
00617 }
00618 
00619 void KateStyleTreeWidgetItem::setColor( int column )
00620 {
00621   QColor c; // use this
00622   QColor d; // default color
00623   if ( column == Foreground)
00624   {
00625     c = currentStyle->foreground().color();
00626     d = defaultStyle->foreground().color();
00627   }
00628   else if ( column == SelectedForeground )
00629   {
00630     c = currentStyle->selectedForeground().color();
00631     d = currentStyle->selectedForeground().color();
00632   }
00633   else if ( column == Background )
00634   {
00635     c = currentStyle->background().color();
00636     d = defaultStyle->background().color();
00637   }
00638   else if ( column == SelectedBackground )
00639   {
00640     c = currentStyle->selectedBackground().color();
00641     d = defaultStyle->selectedBackground().color();
00642   }
00643 
00644   if ( KColorDialog::getColor( c, d, treeWidget() ) != QDialog::Accepted) return;
00645 
00646   bool def = ! c.isValid();
00647 
00648   // if set default, and the attrib is set in the default style use it
00649   // else if set default, unset it
00650   // else set the selected color
00651   switch (column)
00652   {
00653     case Foreground:
00654       if ( def )
00655       {
00656         if ( defaultStyle->hasProperty(QTextFormat::ForegroundBrush) )
00657           currentStyle->setForeground( defaultStyle->foreground());
00658         else
00659           currentStyle->clearProperty(QTextFormat::ForegroundBrush);
00660       }
00661       else
00662         currentStyle->setForeground( c );
00663     break;
00664     case SelectedForeground:
00665       if ( def )
00666       {
00667         if ( defaultStyle->hasProperty(KTextEditor::Attribute::SelectedForeground) )
00668           currentStyle->setSelectedForeground( defaultStyle->selectedForeground());
00669         else
00670           currentStyle->clearProperty(KTextEditor::Attribute::SelectedForeground);
00671       }
00672       else
00673         currentStyle->setSelectedForeground( c );
00674     break;
00675     case Background:
00676       if ( def )
00677       {
00678         if ( defaultStyle->hasProperty(QTextFormat::BackgroundBrush) )
00679           currentStyle->setBackground( defaultStyle->background());
00680         else
00681           currentStyle->clearProperty(QTextFormat::BackgroundBrush);
00682       }
00683       else
00684         currentStyle->setBackground( c );
00685     break;
00686     case SelectedBackground:
00687       if ( def )
00688       {
00689         if ( defaultStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) )
00690           currentStyle->setSelectedBackground( defaultStyle->selectedBackground());
00691         else
00692           currentStyle->clearProperty(KTextEditor::Attribute::SelectedBackground);
00693       }
00694       else
00695         currentStyle->setSelectedBackground( c );
00696     break;
00697   }
00698 
00699   //FIXME
00700   //repaint();
00701 }
00702 
00703 void KateStyleTreeWidgetItem::unsetColor( int c )
00704 {
00705   if ( c == 100 && currentStyle->hasProperty(QTextFormat::BackgroundBrush) )
00706     currentStyle->clearProperty(QTextFormat::BackgroundBrush);
00707   else if ( c == 101 && currentStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) )
00708     currentStyle->clearProperty(KTextEditor::Attribute::SelectedBackground);
00709   updateStyle();
00710 
00711   treeWidget()->emitChanged();
00712 }
00713 
00714 KateStyleTreeWidget* KateStyleTreeWidgetItem::treeWidget() const
00715 {
00716   return static_cast<KateStyleTreeWidget*>(QTreeWidgetItem::treeWidget());
00717 }
00718 //END
00719 
00720 #include "katestyletreewidget.moc"

Kate

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal