• Skip to content
  • Skip to link menu
KDE 4.3 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 static Qt::CheckState toCheckState(bool b) {
00444     return b ? Qt::Checked : Qt::Unchecked;
00445 }
00446 
00447 QVariant KateStyleTreeWidgetItem::data( int column, int role ) const
00448 {
00449   if (column == Context) {
00450     switch (role) {
00451       case Qt::ForegroundRole:
00452         if (style()->hasProperty(QTextFormat::ForegroundBrush))
00453           return style()->foreground().color();
00454         break;
00455 
00456       case Qt::BackgroundRole:
00457         if (style()->hasProperty(QTextFormat::BackgroundBrush))
00458           return style()->background().color();
00459         break;
00460 
00461       case Qt::FontRole:
00462         return style()->font();
00463         break;
00464     }
00465   }
00466 
00467   if (role == Qt::CheckStateRole) {
00468     switch (column) {
00469       case Bold:
00470         return toCheckState(style()->fontBold());
00471       case Italic:
00472         return toCheckState(style()->fontItalic());
00473       case Underline:
00474         return toCheckState(style()->fontUnderline());
00475       case StrikeOut:
00476         return toCheckState(style()->fontStrikeOut());
00477       case UseDefaultStyle:
00478         /* can't compare all attributes, currentStyle has always more than defaultStyle (e.g. the item's name),
00479          * so we just compare the important ones:*/
00480         return toCheckState(
00481                currentStyle->foreground() == defaultStyle->foreground()
00482             && currentStyle->background() == defaultStyle->background()
00483             && currentStyle->selectedForeground() == defaultStyle->selectedForeground()
00484             && currentStyle->selectedBackground() == defaultStyle->selectedBackground()
00485             && currentStyle->fontBold() == defaultStyle->fontBold()
00486             && currentStyle->fontItalic() == defaultStyle->fontItalic()
00487             && currentStyle->fontUnderline() == defaultStyle->fontUnderline()
00488             && currentStyle->fontStrikeOut() == defaultStyle->fontStrikeOut());
00489     }
00490   }
00491 
00492   if (role == Qt::DisplayRole) {
00493     switch (column) {
00494       case Foreground:
00495         return style()->foreground();
00496       case SelectedForeground:
00497         return style()->selectedForeground();
00498       case Background:
00499         return style()->background();
00500       case SelectedBackground:
00501         return style()->selectedBackground();
00502     }
00503   }
00504 
00505   return QTreeWidgetItem::data(column, role);
00506 }
00507 
00508 void KateStyleTreeWidgetItem::updateStyle()
00509 {
00510   // nothing there, not update it, will crash
00511   if (!actualStyle)
00512     return;
00513 
00514   if ( currentStyle->hasProperty(QTextFormat::FontWeight) )
00515   {
00516     if ( currentStyle->fontWeight() != actualStyle->fontWeight())
00517       actualStyle->setFontWeight( currentStyle->fontWeight() );
00518   }
00519   else actualStyle->clearProperty( QTextFormat::FontWeight );
00520 
00521   if ( currentStyle->hasProperty(QTextFormat::FontItalic) )
00522   {
00523     if ( currentStyle->fontItalic() != actualStyle->fontItalic())
00524       actualStyle->setFontItalic( currentStyle->fontItalic() );
00525   }
00526   else actualStyle->clearProperty( QTextFormat::FontItalic );
00527 
00528   if ( currentStyle->hasProperty(QTextFormat::FontStrikeOut) )
00529   {
00530     if ( currentStyle->fontStrikeOut() != actualStyle->fontStrikeOut())
00531       actualStyle->setFontStrikeOut( currentStyle->fontStrikeOut() );
00532   }
00533   else actualStyle->clearProperty( QTextFormat::FontStrikeOut );
00534 
00535   if ( currentStyle->hasProperty(QTextFormat::FontUnderline) )
00536   {
00537     if ( currentStyle->fontUnderline() != actualStyle->fontUnderline())
00538       actualStyle->setFontUnderline( currentStyle->fontUnderline() );
00539   }
00540   else actualStyle->clearProperty( QTextFormat::FontUnderline );
00541 
00542   if ( currentStyle->hasProperty(KTextEditor::Attribute::Outline) )
00543   {
00544     if ( currentStyle->outline() != actualStyle->outline())
00545       actualStyle->setOutline( currentStyle->outline() );
00546   }
00547   else actualStyle->clearProperty( KTextEditor::Attribute::Outline );
00548 
00549   if ( currentStyle->hasProperty(QTextFormat::ForegroundBrush) )
00550   {
00551     if ( currentStyle->foreground() != actualStyle->foreground())
00552       actualStyle->setForeground( currentStyle->foreground() );
00553   }
00554   else actualStyle->clearProperty( QTextFormat::ForegroundBrush );
00555 
00556   if ( currentStyle->hasProperty(KTextEditor::Attribute::SelectedForeground) )
00557   {
00558     if ( currentStyle->selectedForeground() != actualStyle->selectedForeground())
00559       actualStyle->setSelectedForeground( currentStyle->selectedForeground() );
00560   }
00561   else actualStyle->clearProperty( KTextEditor::Attribute::SelectedForeground );
00562 
00563   if ( currentStyle->hasProperty(QTextFormat::BackgroundBrush) )
00564   {
00565     if ( currentStyle->background() != actualStyle->background())
00566       actualStyle->setBackground( currentStyle->background() );
00567   }
00568   else actualStyle->clearProperty( QTextFormat::BackgroundBrush );
00569 
00570   if ( currentStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) )
00571   {
00572     if ( currentStyle->selectedBackground() != actualStyle->selectedBackground())
00573       actualStyle->setSelectedBackground( currentStyle->selectedBackground() );
00574   }
00575   else actualStyle->clearProperty( KTextEditor::Attribute::SelectedBackground );
00576 }
00577 
00578 /* only true for a hl mode item using it's default style */
00579 bool KateStyleTreeWidgetItem::defStyle() const { return actualStyle && actualStyle->properties() != defaultStyle->properties(); }
00580 
00581 /* true for default styles */
00582 bool KateStyleTreeWidgetItem::isDefault() const { return actualStyle ? false : true; }
00583 
00584 void KateStyleTreeWidgetItem::changeProperty( int p )
00585 {
00586   if ( p == Bold )
00587     currentStyle->setFontBold( ! currentStyle->fontBold() );
00588   else if ( p == Italic )
00589     currentStyle->setFontItalic( ! currentStyle->fontItalic() );
00590   else if ( p == Underline )
00591     currentStyle->setFontUnderline( ! currentStyle->fontUnderline() );
00592   else if ( p == StrikeOut )
00593     currentStyle->setFontStrikeOut( ! currentStyle->fontStrikeOut() );
00594   else if ( p == UseDefaultStyle )
00595     toggleDefStyle();
00596   else
00597     setColor( p );
00598 
00599   updateStyle ();
00600 
00601   treeWidget()->emitChanged();
00602 }
00603 
00604 void KateStyleTreeWidgetItem::toggleDefStyle()
00605 {
00606   if ( *currentStyle == *defaultStyle ) {
00607     KMessageBox::information( treeWidget(),
00608          i18n("\"Use Default Style\" will be automatically unset when you change any style properties."),
00609          i18n("Kate Styles"),
00610          "Kate hl config use defaults" );
00611   }
00612   else {
00613     currentStyle = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute( *defaultStyle ));
00614     updateStyle();
00615     
00616     QModelIndex currentIndex = treeWidget()->currentIndex();
00617     while(currentIndex.isValid()) {
00618         treeWidget()->update(currentIndex);
00619         currentIndex = currentIndex.sibling(currentIndex.row(), currentIndex.column() - 1);
00620     }
00621   }
00622 }
00623 
00624 void KateStyleTreeWidgetItem::setColor( int column )
00625 {
00626   QColor c; // use this
00627   QColor d; // default color
00628   if ( column == Foreground)
00629   {
00630     c = currentStyle->foreground().color();
00631     d = defaultStyle->foreground().color();
00632   }
00633   else if ( column == SelectedForeground )
00634   {
00635     c = currentStyle->selectedForeground().color();
00636     d = currentStyle->selectedForeground().color();
00637   }
00638   else if ( column == Background )
00639   {
00640     c = currentStyle->background().color();
00641     d = defaultStyle->background().color();
00642   }
00643   else if ( column == SelectedBackground )
00644   {
00645     c = currentStyle->selectedBackground().color();
00646     d = defaultStyle->selectedBackground().color();
00647   }
00648 
00649   if ( KColorDialog::getColor( c, d, treeWidget() ) != QDialog::Accepted) return;
00650 
00651   bool def = ! c.isValid();
00652 
00653   // if set default, and the attrib is set in the default style use it
00654   // else if set default, unset it
00655   // else set the selected color
00656   switch (column)
00657   {
00658     case Foreground:
00659       if ( def )
00660       {
00661         if ( defaultStyle->hasProperty(QTextFormat::ForegroundBrush) )
00662           currentStyle->setForeground( defaultStyle->foreground());
00663         else
00664           currentStyle->clearProperty(QTextFormat::ForegroundBrush);
00665       }
00666       else
00667         currentStyle->setForeground( c );
00668     break;
00669     case SelectedForeground:
00670       if ( def )
00671       {
00672         if ( defaultStyle->hasProperty(KTextEditor::Attribute::SelectedForeground) )
00673           currentStyle->setSelectedForeground( defaultStyle->selectedForeground());
00674         else
00675           currentStyle->clearProperty(KTextEditor::Attribute::SelectedForeground);
00676       }
00677       else
00678         currentStyle->setSelectedForeground( c );
00679     break;
00680     case Background:
00681       if ( def )
00682       {
00683         if ( defaultStyle->hasProperty(QTextFormat::BackgroundBrush) )
00684           currentStyle->setBackground( defaultStyle->background());
00685         else
00686           currentStyle->clearProperty(QTextFormat::BackgroundBrush);
00687       }
00688       else
00689         currentStyle->setBackground( c );
00690     break;
00691     case SelectedBackground:
00692       if ( def )
00693       {
00694         if ( defaultStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) )
00695           currentStyle->setSelectedBackground( defaultStyle->selectedBackground());
00696         else
00697           currentStyle->clearProperty(KTextEditor::Attribute::SelectedBackground);
00698       }
00699       else
00700         currentStyle->setSelectedBackground( c );
00701     break;
00702   }
00703 
00704   //FIXME
00705   //repaint();
00706 }
00707 
00708 void KateStyleTreeWidgetItem::unsetColor( int c )
00709 {
00710   if ( c == 100 && currentStyle->hasProperty(QTextFormat::BackgroundBrush) )
00711     currentStyle->clearProperty(QTextFormat::BackgroundBrush);
00712   else if ( c == 101 && currentStyle->hasProperty(KTextEditor::Attribute::SelectedBackground) )
00713     currentStyle->clearProperty(KTextEditor::Attribute::SelectedBackground);
00714   updateStyle();
00715 
00716   treeWidget()->emitChanged();
00717 }
00718 
00719 KateStyleTreeWidget* KateStyleTreeWidgetItem::treeWidget() const
00720 {
00721   return static_cast<KateStyleTreeWidget*>(QTreeWidgetItem::treeWidget());
00722 }
00723 //END
00724 
00725 #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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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