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

Kate

kateschema.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 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 //BEGIN Includes
00021 #include "kateschema.h"
00022 #include "kateschema.moc"
00023 
00024 #include "kateconfig.h"
00025 #include "katedocument.h"
00026 #include "katefactory.h"
00027 #include "kateview.h"
00028 #include "katerenderer.h"
00029 
00030 #include <klocale.h>
00031 #include <kdialogbase.h>
00032 #include <kcolorbutton.h>
00033 #include <kcombobox.h>
00034 #include <kinputdialog.h>
00035 #include <kfontdialog.h>
00036 #include <kdebug.h>
00037 #include <kiconloader.h>
00038 #include <kmessagebox.h>
00039 #include <kpopupmenu.h>
00040 #include <kcolordialog.h>
00041 #include <kapplication.h>
00042 #include <kaboutdata.h>
00043 #include <ktexteditor/markinterface.h>
00044 
00045 #include <qbuttongroup.h>
00046 #include <qcheckbox.h>
00047 #include <qptrcollection.h>
00048 #include <qdialog.h>
00049 #include <qgrid.h>
00050 #include <qgroupbox.h>
00051 #include <qlabel.h>
00052 #include <qtextcodec.h>
00053 #include <qlayout.h>
00054 #include <qlineedit.h>
00055 #include <qheader.h>
00056 #include <qlistbox.h>
00057 #include <qhbox.h>
00058 #include <qpainter.h>
00059 #include <qobjectlist.h>
00060 #include <qpixmap.h>
00061 #include <qpushbutton.h>
00062 #include <qradiobutton.h>
00063 #include <qspinbox.h>
00064 #include <qstringlist.h>
00065 #include <qtabwidget.h>
00066 #include <qvbox.h>
00067 #include <qvgroupbox.h>
00068 #include <qwhatsthis.h>
00069 //END
00070 
00071 //BEGIN KateStyleListViewItem decl
00072 /*
00073     QListViewItem subclass to display/edit a style, bold/italic is check boxes,
00074     normal and selected colors are boxes, which will display a color chooser when
00075     activated.
00076     The context name for the style will be drawn using the editor default font and
00077     the chosen colors.
00078     This widget id designed to handle the default as well as the individual hl style
00079     lists.
00080     This widget is designed to work with the KateStyleListView class exclusively.
00081     Added by anders, jan 23 2002.
00082 */
00083 class KateStyleListItem : public QListViewItem
00084 {
00085   public:
00086     KateStyleListItem( QListViewItem *parent=0, const QString & stylename=0,
00087                    class KateAttribute* defaultstyle=0, class KateHlItemData *data=0 );
00088     KateStyleListItem( QListView *parent, const QString & stylename=0,
00089                    class KateAttribute* defaultstyle=0, class KateHlItemData *data=0 );
00090     ~KateStyleListItem() { if (st) delete is; };
00091 
00092     /* mainly for readability */
00093     enum Property { ContextName, Bold, Italic, Underline, Strikeout, Color, SelColor, BgColor, SelBgColor, UseDefStyle };
00094 
00095     /* initializes the style from the default and the hldata */
00096     void initStyle();
00097     /* updates the hldata's style */
00098     void updateStyle();
00099     /* reimp */
00100     virtual int width ( const QFontMetrics & fm, const QListView * lv, int c ) const;
00101     /* calls changeProperty() if it makes sense considering pos. */
00102     void activate( int column, const QPoint &localPos );
00103     /* For bool fields, toggles them, for color fields, display a color chooser */
00104     void changeProperty( Property p );
00108     void unsetColor( int c );
00109     /* style context name */
00110     QString contextName() { return text(0); };
00111     /* only true for a hl mode item using it's default style */
00112     bool defStyle();
00113     /* true for default styles */
00114     bool isDefault();
00115     /* whichever style is active (st for hl mode styles not using
00116        the default style, ds otherwise) */
00117     class KateAttribute* style() { return is; };
00118 
00119   protected:
00120     /* reimp */
00121     void paintCell(QPainter *p, const QColorGroup& cg, int col, int width, int align);
00122 
00123   private:
00124     /* private methods to change properties */
00125     void toggleDefStyle();
00126     void setColor( int );
00127     /* helper function to copy the default style into the KateHlItemData,
00128        when a property is changed and we are using default style. */
00129 
00130     class KateAttribute *is, // the style currently in use
00131               *ds;           // default style for hl mode contexts and default styles
00132     class KateHlItemData *st;      // itemdata for hl mode contexts
00133 };
00134 //END
00135 
00136 //BEGIN KateStyleListCaption decl
00137 /*
00138     This is a simple subclass for drawing the language names in a nice treeview
00139     with the styles.  It is needed because we do not like to mess with the default
00140     palette of the containing ListView.  Only the paintCell method is overwritten
00141     to use our own palette (that is set on the viewport rather than on the listview
00142     itself).
00143 */
00144 class KateStyleListCaption : public QListViewItem
00145 {
00146   public:
00147     KateStyleListCaption( QListView *parent, const QString & name );
00148     ~KateStyleListCaption() {};
00149 
00150   protected:
00151     void paintCell(QPainter *p, const QColorGroup& cg, int col, int width, int align);
00152 };
00153 //END
00154 
00155 //BEGIN KateSchemaManager
00156 QString KateSchemaManager::normalSchema ()
00157 {
00158   return KApplication::kApplication()->aboutData()->appName () + QString (" - Normal");
00159 }
00160 
00161 QString KateSchemaManager::printingSchema ()
00162 {
00163   return KApplication::kApplication()->aboutData()->appName () + QString (" - Printing");
00164 }
00165 
00166 KateSchemaManager::KateSchemaManager ()
00167   : m_config ("kateschemarc", false, false)
00168 {
00169   update ();
00170 }
00171 
00172 KateSchemaManager::~KateSchemaManager ()
00173 {
00174 }
00175 
00176 //
00177 // read the types from config file and update the internal list
00178 //
00179 void KateSchemaManager::update (bool readfromfile)
00180 {
00181   if (readfromfile)
00182     m_config.reparseConfiguration ();
00183 
00184   m_schemas = m_config.groupList();
00185   m_schemas.sort ();
00186 
00187   m_schemas.remove (printingSchema());
00188   m_schemas.remove (normalSchema());
00189   m_schemas.prepend (printingSchema());
00190   m_schemas.prepend (normalSchema());
00191 }
00192 
00193 //
00194 // get the right group
00195 // special handling of the default schemas ;)
00196 //
00197 KConfig *KateSchemaManager::schema (uint number)
00198 {
00199   if ((number>1) && (number < m_schemas.count()))
00200     m_config.setGroup (m_schemas[number]);
00201   else if (number == 1)
00202     m_config.setGroup (printingSchema());
00203   else
00204     m_config.setGroup (normalSchema());
00205 
00206   return &m_config;
00207 }
00208 
00209 void KateSchemaManager::addSchema (const QString &t)
00210 {
00211   m_config.setGroup (t);
00212   m_config.writeEntry("Color Background", KGlobalSettings::baseColor());
00213 
00214   update (false);
00215 }
00216 
00217 void KateSchemaManager::removeSchema (uint number)
00218 {
00219   if (number >= m_schemas.count())
00220     return;
00221 
00222   if (number < 2)
00223     return;
00224 
00225   m_config.deleteGroup (name (number));
00226 
00227   update (false);
00228 }
00229 
00230 bool KateSchemaManager::validSchema (uint number)
00231 {
00232   if (number < m_schemas.count())
00233     return true;
00234 
00235   return false;
00236 }
00237 
00238 uint KateSchemaManager::number (const QString &name)
00239 {
00240   if (name == normalSchema())
00241     return 0;
00242 
00243   if (name == printingSchema())
00244     return 1;
00245 
00246   int i;
00247   if ((i = m_schemas.findIndex(name)) > -1)
00248     return i;
00249 
00250   return 0;
00251 }
00252 
00253 QString KateSchemaManager::name (uint number)
00254 {
00255   if ((number>1) && (number < m_schemas.count()))
00256     return m_schemas[number];
00257   else if (number == 1)
00258     return printingSchema();
00259 
00260   return normalSchema();
00261 }
00262 //END
00263 
00264 //
00265 // DIALOGS !!!
00266 //
00267 
00268 //BEGIN KateSchemaConfigColorTab -- 'Colors' tab
00269 KateSchemaConfigColorTab::KateSchemaConfigColorTab( QWidget *parent, const char * )
00270   : QWidget (parent)
00271 {
00272   m_schema = -1;
00273 
00274   QHBox *b;
00275   QLabel *label;
00276 
00277   QVBoxLayout *blay=new QVBoxLayout(this, 0, KDialog::spacingHint());
00278 
00279   QVGroupBox *gbTextArea = new QVGroupBox(i18n("Text Area Background"), this);
00280 
00281   b = new QHBox (gbTextArea);
00282   b->setSpacing(KDialog::spacingHint());
00283   label = new QLabel( i18n("Normal text:"), b);
00284   label->setAlignment( AlignLeft|AlignVCenter);
00285   m_back = new KColorButton(b);
00286 
00287   b = new QHBox (gbTextArea);
00288   b->setSpacing(KDialog::spacingHint());
00289   label = new QLabel( i18n("Selected text:"), b);
00290   label->setAlignment( AlignLeft|AlignVCenter);
00291   m_selected = new KColorButton(b);
00292 
00293   b = new QHBox (gbTextArea);
00294   b->setSpacing(KDialog::spacingHint());
00295   label = new QLabel( i18n("Current line:"), b);
00296   label->setAlignment( AlignLeft|AlignVCenter);
00297   m_current = new KColorButton(b);
00298 
00299   // Markers from kdelibs/interfaces/ktextinterface/markinterface.h
00300   b = new QHBox (gbTextArea);
00301   b->setSpacing(KDialog::spacingHint());
00302   m_combobox = new KComboBox(b, "color_combo_box");
00303   // add the predefined mark types as defined in markinterface.h
00304   m_combobox->insertItem(i18n("Bookmark"));            // markType01
00305   m_combobox->insertItem(i18n("Active Breakpoint"));   // markType02
00306   m_combobox->insertItem(i18n("Reached Breakpoint"));  // markType03
00307   m_combobox->insertItem(i18n("Disabled Breakpoint")); // markType04
00308   m_combobox->insertItem(i18n("Execution"));           // markType05
00309   m_combobox->insertItem(i18n("Warning"));             // markType06
00310   m_combobox->insertItem(i18n("Error"));               // markType07
00311   m_combobox->setCurrentItem(0);
00312   m_markers = new KColorButton(b, "marker_color_button");
00313   connect( m_combobox, SIGNAL( activated( int ) ), SLOT( slotComboBoxChanged( int ) ) );
00314 
00315   blay->addWidget(gbTextArea);
00316 
00317   QVGroupBox *gbBorder = new QVGroupBox(i18n("Additional Elements"), this);
00318 
00319   b = new QHBox (gbBorder);
00320   b->setSpacing(KDialog::spacingHint());
00321   label = new QLabel( i18n("Left border background:"), b);
00322   label->setAlignment( AlignLeft|AlignVCenter);
00323   m_iconborder = new KColorButton(b);
00324 
00325   b = new QHBox (gbBorder);
00326   b->setSpacing(KDialog::spacingHint());
00327   label = new QLabel( i18n("Line numbers:"), b);
00328   label->setAlignment( AlignLeft|AlignVCenter);
00329   m_linenumber = new KColorButton(b);
00330 
00331   b = new QHBox (gbBorder);
00332   b->setSpacing(KDialog::spacingHint());
00333   label = new QLabel( i18n("Bracket highlight:"), b);
00334   label->setAlignment( AlignLeft|AlignVCenter);
00335   m_bracket = new KColorButton(b);
00336 
00337   b = new QHBox (gbBorder);
00338   b->setSpacing(KDialog::spacingHint());
00339   label = new QLabel( i18n("Word wrap markers:"), b);
00340   label->setAlignment( AlignLeft|AlignVCenter);
00341   m_wwmarker = new KColorButton(b);
00342 
00343   b = new QHBox (gbBorder);
00344   b->setSpacing(KDialog::spacingHint());
00345   label = new QLabel( i18n("Tab markers:"), b);
00346   label->setAlignment( AlignLeft|AlignVCenter);
00347   m_tmarker = new KColorButton(b);
00348 
00349   blay->addWidget(gbBorder);
00350 
00351   blay->addStretch();
00352 
00353   // connect signal changed(); changed is emitted by a ColorButton change!
00354   connect( this, SIGNAL( changed() ), parent->parentWidget(), SLOT( slotChanged() ) );
00355 
00356   // QWhatsThis help
00357   QWhatsThis::add(m_back, i18n("<p>Sets the background color of the editing area.</p>"));
00358   QWhatsThis::add(m_selected, i18n("<p>Sets the background color of the selection.</p>"
00359         "<p>To set the text color for selected text, use the \"<b>Configure "
00360         "Highlighting</b>\" dialog.</p>"));
00361   QWhatsThis::add(m_markers, i18n("<p>Sets the background color of the selected "
00362         "marker type.</p><p><b>Note</b>: The marker color is displayed lightly because "
00363         "of transparency.</p>"));
00364   QWhatsThis::add(m_combobox, i18n("<p>Select the marker type you want to change.</p>"));
00365   QWhatsThis::add(m_current, i18n("<p>Sets the background color of the currently "
00366         "active line, which means the line where your cursor is positioned.</p>"));
00367   QWhatsThis::add( m_linenumber, i18n(
00368         "<p>This color will be used to draw the line numbers (if enabled) and the "
00369         "lines in the code-folding pane.</p>" ) );
00370   QWhatsThis::add(m_bracket, i18n("<p>Sets the bracket matching color. This means, "
00371         "if you place the cursor e.g. at a <b>(</b>, the matching <b>)</b> will "
00372         "be highlighted with this color.</p>"));
00373   QWhatsThis::add(m_wwmarker, i18n(
00374         "<p>Sets the color of Word Wrap-related markers:</p>"
00375         "<dl><dt>Static Word Wrap</dt><dd>A vertical line which shows the column where "
00376         "text is going to be wrapped</dd>"
00377         "<dt>Dynamic Word Wrap</dt><dd>An arrow shown to the left of "
00378         "visually-wrapped lines</dd></dl>"));
00379   QWhatsThis::add(m_tmarker, i18n(
00380         "<p>Sets the color of the tabulator marks:</p>"));
00381 }
00382 
00383 KateSchemaConfigColorTab::~KateSchemaConfigColorTab()
00384 {
00385 }
00386 
00387 void KateSchemaConfigColorTab::schemaChanged ( int newSchema )
00388 {
00389   // save curent schema
00390   if ( m_schema > -1 )
00391   {
00392     m_schemas[ m_schema ].back = m_back->color();
00393     m_schemas[ m_schema ].selected = m_selected->color();
00394     m_schemas[ m_schema ].current = m_current->color();
00395     m_schemas[ m_schema ].bracket = m_bracket->color();
00396     m_schemas[ m_schema ].wwmarker = m_wwmarker->color();
00397     m_schemas[ m_schema ].iconborder = m_iconborder->color();
00398     m_schemas[ m_schema ].tmarker = m_tmarker->color();
00399     m_schemas[ m_schema ].linenumber = m_linenumber->color();
00400   }
00401 
00402   if ( newSchema == m_schema ) return;
00403 
00404   // switch
00405   m_schema = newSchema;
00406 
00407   // first disconnect all signals otherwise setColor emits changed
00408   m_back      ->disconnect( SIGNAL( changed( const QColor & ) ) );
00409   m_selected  ->disconnect( SIGNAL( changed( const QColor & ) ) );
00410   m_current   ->disconnect( SIGNAL( changed( const QColor & ) ) );
00411   m_bracket   ->disconnect( SIGNAL( changed( const QColor & ) ) );
00412   m_wwmarker  ->disconnect( SIGNAL( changed( const QColor & ) ) );
00413   m_iconborder->disconnect( SIGNAL( changed( const QColor & ) ) );
00414   m_tmarker   ->disconnect( SIGNAL( changed( const QColor & ) ) );
00415   m_markers   ->disconnect( SIGNAL( changed( const QColor & ) ) );
00416   m_linenumber->disconnect( SIGNAL( changed( const QColor & ) ) );
00417 
00418   // If we havent this schema, read in from config file
00419   if ( ! m_schemas.contains( newSchema ) )
00420   {
00421     // fallback defaults
00422     QColor tmp0 (KGlobalSettings::baseColor());
00423     QColor tmp1 (KGlobalSettings::highlightColor());
00424     QColor tmp2 (KGlobalSettings::alternateBackgroundColor());
00425     QColor tmp3 ( "#FFFF99" );
00426     QColor tmp4 (tmp2.dark());
00427     QColor tmp5 ( KGlobalSettings::textColor() );
00428     QColor tmp6 ( "#EAE9E8" );
00429     QColor tmp7 ( "#000000" );
00430 
00431     // same std colors like in KateDocument::markColor
00432     QValueVector <QColor> mark(KTextEditor::MarkInterface::reservedMarkersCount());
00433     Q_ASSERT(mark.size() > 6);
00434     mark[0] = Qt::blue;
00435     mark[1] = Qt::red;
00436     mark[2] = Qt::yellow;
00437     mark[3] = Qt::magenta;
00438     mark[4] = Qt::gray;
00439     mark[5] = Qt::green;
00440     mark[6] = Qt::red;
00441 
00442     SchemaColors c;
00443     KConfig *config = KateFactory::self()->schemaManager()->schema(newSchema);
00444 
00445     c.back= config->readColorEntry("Color Background", &tmp0);
00446     c.selected = config->readColorEntry("Color Selection", &tmp1);
00447     c.current = config->readColorEntry("Color Highlighted Line", &tmp2);
00448     c.bracket = config->readColorEntry("Color Highlighted Bracket", &tmp3);
00449     c.wwmarker = config->readColorEntry("Color Word Wrap Marker", &tmp4);
00450     c.tmarker = config->readColorEntry("Color Tab Marker", &tmp5);
00451     c.iconborder = config->readColorEntry("Color Icon Bar", &tmp6);
00452     c.linenumber = config->readColorEntry("Color Line Number", &tmp7);
00453 
00454     for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++)
00455       c.markerColors[i] =  config->readColorEntry( QString("Color MarkType%1").arg(i+1), &mark[i] );
00456 
00457      m_schemas[ newSchema ] = c;
00458   }
00459 
00460   m_back->setColor(  m_schemas[ newSchema ].back);
00461   m_selected->setColor(  m_schemas [ newSchema ].selected );
00462   m_current->setColor(  m_schemas [ newSchema ].current );
00463   m_bracket->setColor(  m_schemas [ newSchema ].bracket );
00464   m_wwmarker->setColor(  m_schemas [ newSchema ].wwmarker );
00465   m_tmarker->setColor(  m_schemas [ newSchema ].tmarker );
00466   m_iconborder->setColor(  m_schemas [ newSchema ].iconborder );
00467   m_linenumber->setColor(  m_schemas [ newSchema ].linenumber );
00468 
00469   // map from 0..reservedMarkersCount()-1 - the same index as in markInterface
00470   for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++)
00471   {
00472     QPixmap pix(16, 16);
00473     pix.fill( m_schemas [ newSchema ].markerColors[i]);
00474     m_combobox->changeItem(pix, m_combobox->text(i), i);
00475   }
00476   m_markers->setColor(  m_schemas [ newSchema ].markerColors[ m_combobox->currentItem() ] );
00477 
00478   connect( m_back      , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00479   connect( m_selected  , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00480   connect( m_current   , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00481   connect( m_bracket   , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00482   connect( m_wwmarker  , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00483   connect( m_iconborder, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00484   connect( m_tmarker   , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00485   connect( m_linenumber, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) );
00486   connect( m_markers   , SIGNAL( changed( const QColor& ) ), SLOT( slotMarkerColorChanged( const QColor& ) ) );
00487 }
00488 
00489 void KateSchemaConfigColorTab::apply ()
00490 {
00491   schemaChanged( m_schema );
00492   QMap<int,SchemaColors>::Iterator it;
00493   for ( it =  m_schemas.begin(); it !=  m_schemas.end(); ++it )
00494   {
00495     kdDebug(13030)<<"APPLY scheme = "<<it.key()<<endl;
00496     KConfig *config = KateFactory::self()->schemaManager()->schema( it.key() );
00497     kdDebug(13030)<<"Using config group "<<config->group()<<endl;
00498     SchemaColors c = it.data();
00499 
00500     config->writeEntry("Color Background", c.back);
00501     config->writeEntry("Color Selection", c.selected);
00502     config->writeEntry("Color Highlighted Line", c.current);
00503     config->writeEntry("Color Highlighted Bracket", c.bracket);
00504     config->writeEntry("Color Word Wrap Marker", c.wwmarker);
00505     config->writeEntry("Color Tab Marker", c.tmarker);
00506     config->writeEntry("Color Icon Bar", c.iconborder);
00507     config->writeEntry("Color Line Number", c.linenumber);
00508 
00509     for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++)
00510     {
00511       config->writeEntry(QString("Color MarkType%1").arg(i + 1), c.markerColors[i]);
00512     }
00513   }
00514 }
00515 
00516 void KateSchemaConfigColorTab::slotMarkerColorChanged( const QColor& color)
00517 {
00518   int index = m_combobox->currentItem();
00519    m_schemas[ m_schema ].markerColors[ index ] = color;
00520   QPixmap pix(16, 16);
00521   pix.fill(color);
00522   m_combobox->changeItem(pix, m_combobox->text(index), index);
00523 
00524   emit changed();
00525 }
00526 
00527 void KateSchemaConfigColorTab::slotComboBoxChanged(int index)
00528 {
00529   // temporarily disconnect the changed-signal because setColor emits changed as well
00530   m_markers->disconnect( SIGNAL( changed( const QColor& ) ) );
00531   m_markers->setColor( m_schemas[m_schema].markerColors[index] );
00532   connect( m_markers, SIGNAL( changed( const QColor& ) ), SLOT( slotMarkerColorChanged( const QColor& ) ) );
00533 }
00534 
00535 //END KateSchemaConfigColorTab
00536 
00537 //BEGIN FontConfig -- 'Fonts' tab
00538 KateSchemaConfigFontTab::KateSchemaConfigFontTab( QWidget *parent, const char * )
00539   : QWidget (parent)
00540 {
00541     // sizemanagment
00542   QGridLayout *grid = new QGridLayout( this, 1, 1 );
00543 
00544   m_fontchooser = new KFontChooser ( this, 0L, false, QStringList(), false );
00545   m_fontchooser->enableColumn(KFontChooser::StyleList, false);
00546   grid->addWidget( m_fontchooser, 0, 0);
00547 
00548   connect (this, SIGNAL( changed()), parent->parentWidget(), SLOT (slotChanged()));
00549   m_schema = -1;
00550 }
00551 
00552 KateSchemaConfigFontTab::~KateSchemaConfigFontTab()
00553 {
00554 }
00555 
00556 void KateSchemaConfigFontTab::slotFontSelected( const QFont &font )
00557 {
00558   if ( m_schema > -1 )
00559   {
00560     m_fonts[m_schema] = font;
00561     emit changed();
00562   }
00563 }
00564 
00565 void KateSchemaConfigFontTab::apply()
00566 {
00567   FontMap::Iterator it;
00568   for ( it = m_fonts.begin(); it != m_fonts.end(); ++it )
00569   {
00570     KateFactory::self()->schemaManager()->schema( it.key() )->writeEntry( "Font", it.data() );
00571   }
00572 }
00573 
00574 void KateSchemaConfigFontTab::schemaChanged( int newSchema )
00575 {
00576   if ( m_schema > -1 )
00577     m_fonts[ m_schema ] = m_fontchooser->font();
00578 
00579   m_schema = newSchema;
00580 
00581   QFont f (KGlobalSettings::fixedFont());
00582 
00583   m_fontchooser->disconnect ( this );
00584   m_fontchooser->setFont ( KateFactory::self()->schemaManager()->schema( newSchema )->readFontEntry("Font", &f) );
00585   m_fonts[ newSchema ] = m_fontchooser->font();
00586   connect (m_fontchooser, SIGNAL (fontSelected( const QFont & )), this, SLOT (slotFontSelected( const QFont & )));
00587 }
00588 //END FontConfig
00589 
00590 //BEGIN FontColorConfig -- 'Normal Text Styles' tab
00591 KateSchemaConfigFontColorTab::KateSchemaConfigFontColorTab( QWidget *parent, const char * )
00592   : QWidget (parent)
00593 {
00594   m_defaultStyleLists.setAutoDelete(true);
00595 
00596   // sizemanagment
00597   QGridLayout *grid = new QGridLayout( this, 1, 1 );
00598 
00599   m_defaultStyles = new KateStyleListView( this, false );
00600   grid->addWidget( m_defaultStyles, 0, 0);
00601 
00602   connect (m_defaultStyles, SIGNAL (changed()), parent->parentWidget(), SLOT (slotChanged()));
00603 
00604   QWhatsThis::add( m_defaultStyles,  i18n(
00605       "This list displays the default styles for the current schema and "
00606       "offers the means to edit them. The style name reflects the current "
00607       "style settings."
00608       "<p>To edit the colors, click the colored squares, or select the color "
00609       "to edit from the popup menu.<p>You can unset the Background and Selected "
00610       "Background colors from the popup menu when appropriate.") );
00611 }
00612 
00613 KateSchemaConfigFontColorTab::~KateSchemaConfigFontColorTab()
00614 {
00615 }
00616 
00617 KateAttributeList *KateSchemaConfigFontColorTab::attributeList (uint schema)
00618 {
00619   if (!m_defaultStyleLists[schema])
00620   {
00621     KateAttributeList *list = new KateAttributeList ();
00622     KateHlManager::self()->getDefaults(schema, *list);
00623 
00624     m_defaultStyleLists.insert (schema, list);
00625   }
00626 
00627   return m_defaultStyleLists[schema];
00628 }
00629 
00630 void KateSchemaConfigFontColorTab::schemaChanged (uint schema)
00631 {
00632   m_defaultStyles->clear ();
00633 
00634   KateAttributeList *l = attributeList (schema);
00635 
00636   // set colors
00637   QPalette p ( m_defaultStyles->palette() );
00638   QColor _c ( KGlobalSettings::baseColor() );
00639   p.setColor( QColorGroup::Base,
00640     KateFactory::self()->schemaManager()->schema(schema)->
00641       readColorEntry( "Color Background", &_c ) );
00642   _c = KGlobalSettings::highlightColor();
00643   p.setColor( QColorGroup::Highlight,
00644     KateFactory::self()->schemaManager()->schema(schema)->
00645       readColorEntry( "Color Selection", &_c ) );
00646   _c = l->at(0)->textColor(); // not quite as much of an assumption ;)
00647   p.setColor( QColorGroup::Text, _c );
00648   m_defaultStyles->viewport()->setPalette( p );
00649 
00650   // insert the default styles backwards to get them in the right order
00651   for ( int i = KateHlManager::self()->defaultStyles() - 1; i >= 0; i-- )
00652   {
00653     new KateStyleListItem( m_defaultStyles, KateHlManager::self()->defaultStyleName(i, true), l->at( i ) );
00654   }
00655 }
00656 
00657 void KateSchemaConfigFontColorTab::reload ()
00658 {
00659   m_defaultStyles->clear ();
00660   m_defaultStyleLists.clear ();
00661 }
00662 
00663 void KateSchemaConfigFontColorTab::apply ()
00664 {
00665   for ( QIntDictIterator<KateAttributeList> it( m_defaultStyleLists ); it.current(); ++it )
00666     KateHlManager::self()->setDefaults(it.currentKey(), *(it.current()));
00667 }
00668 
00669 //END FontColorConfig
00670 
00671 //BEGIN KateSchemaConfigHighlightTab -- 'Highlighting Text Styles' tab
00672 KateSchemaConfigHighlightTab::KateSchemaConfigHighlightTab( QWidget *parent, const char *, KateSchemaConfigFontColorTab *page, uint hl )
00673   : QWidget (parent)
00674 {
00675   m_defaults = page;
00676 
00677   m_schema = 0;
00678   m_hl = 0;
00679 
00680   m_hlDict.setAutoDelete (true);
00681 
00682   QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() );
00683 
00684   // hl chooser
00685   QHBox *hbHl = new QHBox( this );
00686   layout->add (hbHl);
00687 
00688   hbHl->setSpacing( KDialog::spacingHint() );
00689   QLabel *lHl = new QLabel( i18n("H&ighlight:"), hbHl );
00690   hlCombo = new QComboBox( false, hbHl );
00691   lHl->setBuddy( hlCombo );
00692   connect( hlCombo, SIGNAL(activated(int)),
00693            this, SLOT(hlChanged(int)) );
00694 
00695   for( int i = 0; i < KateHlManager::self()->highlights(); i++) {
00696     if (KateHlManager::self()->hlSection(i).length() > 0)
00697       hlCombo->insertItem(KateHlManager::self()->hlSection(i) + QString ("/") + KateHlManager::self()->hlNameTranslated(i));
00698     else
00699       hlCombo->insertItem(KateHlManager::self()->hlNameTranslated(i));
00700   }
00701   hlCombo->setCurrentItem(0);
00702 
00703   // styles listview
00704   m_styles = new KateStyleListView( this, true );
00705   layout->addWidget (m_styles, 999);
00706 
00707   hlCombo->setCurrentItem ( hl );
00708   hlChanged ( hl );
00709 
00710   QWhatsThis::add( m_styles,  i18n(
00711     "This list displays the contexts of the current syntax highlight mode and "
00712     "offers the means to edit them. The context name reflects the current "
00713     "style settings.<p>To edit using the keyboard, press "
00714     "<strong>&lt;SPACE&gt;</strong> and choose a property from the popup menu."
00715     "<p>To edit the colors, click the colored squares, or select the color "
00716     "to edit from the popup menu.<p>You can unset the Background and Selected "
00717     "Background colors from the context menu when appropriate.") );
00718 
00719   connect (m_styles, SIGNAL (changed()), parent->parentWidget(), SLOT (slotChanged()));
00720 }
00721 
00722 KateSchemaConfigHighlightTab::~KateSchemaConfigHighlightTab()
00723 {
00724 }
00725 
00726 void KateSchemaConfigHighlightTab::hlChanged(int z)
00727 {
00728   m_hl = z;
00729 
00730   schemaChanged (m_schema);
00731 }
00732 
00733 void KateSchemaConfigHighlightTab::schemaChanged (uint schema)
00734 {
00735   m_schema = schema;
00736 
00737   kdDebug(13030) << "NEW SCHEMA: " << m_schema << " NEW HL: " << m_hl << endl;
00738 
00739   m_styles->clear ();
00740 
00741   if (!m_hlDict[m_schema])
00742   {
00743     kdDebug(13030) << "NEW SCHEMA, create dict" << endl;
00744 
00745     m_hlDict.insert (schema, new QIntDict<KateHlItemDataList>);
00746     m_hlDict[m_schema]->setAutoDelete (true);
00747   }
00748 
00749   if (!m_hlDict[m_schema]->find(m_hl))
00750   {
00751     kdDebug(13030) << "NEW HL, create list" << endl;
00752 
00753     KateHlItemDataList *list = new KateHlItemDataList ();
00754     KateHlManager::self()->getHl( m_hl )->getKateHlItemDataListCopy (m_schema, *list);
00755     m_hlDict[m_schema]->insert (m_hl, list);
00756   }
00757 
00758   KateAttributeList *l = m_defaults->attributeList (schema);
00759 
00760   // Set listview colors
00761   // We do that now, because we can now get the "normal text" color.
00762   // TODO this reads of the KConfig object, which should be changed when
00763   // the color tab is fixed.
00764   QPalette p ( m_styles->palette() );
00765   QColor _c ( KGlobalSettings::baseColor() );
00766   p.setColor( QColorGroup::Base,
00767     KateFactory::self()->schemaManager()->schema(m_schema)->
00768       readColorEntry( "Color Background", &_c ) );
00769   _c = KGlobalSettings::highlightColor();
00770   p.setColor( QColorGroup::Highlight,
00771     KateFactory::self()->schemaManager()->schema(m_schema)->
00772       readColorEntry( "Color Selection", &_c ) );
00773   _c = l->at(0)->textColor(); // not quite as much of an assumption ;)
00774   p.setColor( QColorGroup::Text, _c );
00775   m_styles->viewport()->setPalette( p );
00776 
00777   QDict<KateStyleListCaption> prefixes;
00778   for ( KateHlItemData *itemData = m_hlDict[m_schema]->find(m_hl)->last();
00779         itemData != 0L;
00780         itemData = m_hlDict[m_schema]->find(m_hl)->prev())
00781   {
00782     kdDebug(13030) << "insert items " << itemData->name << endl;
00783 
00784     // All stylenames have their language mode prefixed, e.g. HTML:Comment
00785     // split them and put them into nice substructures.
00786     int c = itemData->name.find(':');
00787     if ( c > 0 ) {
00788       QString prefix = itemData->name.left(c);
00789       QString name   = itemData->name.mid(c+1);
00790 
00791       KateStyleListCaption *parent = prefixes.find( prefix );
00792       if ( ! parent )
00793       {
00794         parent = new KateStyleListCaption( m_styles, prefix );
00795         parent->setOpen(true);
00796         prefixes.insert( prefix, parent );
00797       }
00798       new KateStyleListItem( parent, name, l->at(itemData->defStyleNum), itemData );
00799     } else {
00800       new KateStyleListItem( m_styles, itemData->name, l->at(itemData->defStyleNum), itemData );
00801     }
00802   }
00803 }
00804 
00805 void KateSchemaConfigHighlightTab::reload ()
00806 {
00807   m_styles->clear ();
00808   m_hlDict.clear ();
00809 
00810   hlChanged (0);
00811 }
00812 
00813 void KateSchemaConfigHighlightTab::apply ()
00814 {
00815   for ( QIntDictIterator< QIntDict<KateHlItemDataList> > it( m_hlDict ); it.current(); ++it )
00816     for ( QIntDictIterator< KateHlItemDataList > it2( *it.current() ); it2.current(); ++it2 )
00817     {
00818       KateHlManager::self()->getHl( it2.currentKey() )->setKateHlItemDataList (it.currentKey(), *(it2.current()));
00819     }
00820 }
00821 
00822 //END KateSchemaConfigHighlightTab
00823 
00824 //BEGIN KateSchemaConfigPage -- Main dialog page
00825 KateSchemaConfigPage::KateSchemaConfigPage( QWidget *parent, KateDocument *doc )
00826   : KateConfigPage( parent ),
00827     m_lastSchema (-1)
00828 {
00829   QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() );
00830 
00831   QHBox *hbHl = new QHBox( this );
00832   layout->add (hbHl);
00833   hbHl->setSpacing( KDialog::spacingHint() );
00834   QLabel *lHl = new QLabel( i18n("&Schema:"), hbHl );
00835   schemaCombo = new QComboBox( false, hbHl );
00836   lHl->setBuddy( schemaCombo );
00837   connect( schemaCombo, SIGNAL(activated(int)),
00838            this, SLOT(schemaChanged(int)) );
00839 
00840   QPushButton *btnnew = new QPushButton( i18n("&New..."), hbHl );
00841   connect( btnnew, SIGNAL(clicked()), this, SLOT(newSchema()) );
00842 
00843   btndel = new QPushButton( i18n("&Delete"), hbHl );
00844   connect( btndel, SIGNAL(clicked()), this, SLOT(deleteSchema()) );
00845 
00846   m_tabWidget = new QTabWidget ( this );
00847   m_tabWidget->setMargin (KDialog::marginHint());
00848   layout->add (m_tabWidget);
00849 
00850   connect (m_tabWidget, SIGNAL (currentChanged (QWidget *)), this, SLOT (newCurrentPage (QWidget *)));
00851 
00852   m_colorTab = new KateSchemaConfigColorTab (m_tabWidget);
00853   m_tabWidget->addTab (m_colorTab, i18n("Colors"));
00854 
00855   m_fontTab = new KateSchemaConfigFontTab (m_tabWidget);
00856   m_tabWidget->addTab (m_fontTab, i18n("Font"));
00857 
00858   m_fontColorTab = new KateSchemaConfigFontColorTab (m_tabWidget);
00859   m_tabWidget->addTab (m_fontColorTab, i18n("Normal Text Styles"));
00860 
00861   uint hl = doc ? doc->hlMode() : 0;
00862   m_highlightTab = new KateSchemaConfigHighlightTab (m_tabWidget, "", m_fontColorTab, hl );
00863   m_tabWidget->addTab (m_highlightTab, i18n("Highlighting Text Styles"));
00864 
00865   hbHl = new QHBox( this );
00866   layout->add (hbHl);
00867   hbHl->setSpacing( KDialog::spacingHint() );
00868   lHl = new QLabel( i18n("&Default schema for %1:").arg(KApplication::kApplication()->aboutData()->programName ()), hbHl );
00869   defaultSchemaCombo = new QComboBox( false, hbHl );
00870   lHl->setBuddy( defaultSchemaCombo );
00871 
00872 
00873   m_defaultSchema = (doc && doc->activeView()) ? doc->activeView()->renderer()->config()->schema() : KateRendererConfig::global()->schema();
00874 
00875   reload();
00876 
00877   connect( defaultSchemaCombo, SIGNAL(activated(int)),
00878            this, SLOT(slotChanged()) );
00879 }
00880 
00881 KateSchemaConfigPage::~KateSchemaConfigPage ()
00882 {
00883   // just reload config from disc
00884   KateFactory::self()->schemaManager()->update ();
00885 }
00886 
00887 void KateSchemaConfigPage::apply()
00888 {
00889   m_colorTab->apply();
00890   m_fontTab->apply();
00891   m_fontColorTab->apply ();
00892   m_highlightTab->apply ();
00893 
00894   // just sync the config
00895   KateFactory::self()->schemaManager()->schema (0)->sync();
00896 
00897   KateFactory::self()->schemaManager()->update ();
00898 
00899   // clear all attributes
00900   for (int i = 0; i < KateHlManager::self()->highlights(); ++i)
00901     KateHlManager::self()->getHl (i)->clearAttributeArrays ();
00902 
00903   // than reload the whole stuff
00904   KateRendererConfig::global()->setSchema (defaultSchemaCombo->currentItem());
00905   KateRendererConfig::global()->reloadSchema();
00906 
00907   // sync the hl config for real
00908   KateHlManager::self()->getKConfig()->sync ();
00909 }
00910 
00911 void KateSchemaConfigPage::reload()
00912 {
00913   // just reload the config from disc
00914   KateFactory::self()->schemaManager()->update ();
00915 
00916   // special for the highlighting stuff
00917   m_fontColorTab->reload ();
00918 
00919   update ();
00920 
00921   defaultSchemaCombo->setCurrentItem (KateRendererConfig::global()->schema());
00922 
00923   // initialize to the schema in the current document, or default schema
00924   schemaCombo->setCurrentItem( m_defaultSchema );
00925   schemaChanged( m_defaultSchema );
00926 }
00927 
00928 void KateSchemaConfigPage::reset()
00929 {
00930   reload ();
00931 }
00932 
00933 void KateSchemaConfigPage::defaults()
00934 {
00935   reload ();
00936 }
00937 
00938 void KateSchemaConfigPage::update ()
00939 {
00940   // soft update, no load from disk
00941   KateFactory::self()->schemaManager()->update (false);
00942 
00943   schemaCombo->clear ();
00944   schemaCombo->insertStringList (KateFactory::self()->schemaManager()->list ());
00945 
00946   defaultSchemaCombo->clear ();
00947   defaultSchemaCombo->insertStringList (KateFactory::self()->schemaManager()->list ());
00948 
00949   schemaCombo->setCurrentItem (0);
00950   schemaChanged (0);
00951 
00952   schemaCombo->setEnabled (schemaCombo->count() > 0);
00953 }
00954 
00955 void KateSchemaConfigPage::deleteSchema ()
00956 {
00957   int t = schemaCombo->currentItem ();
00958 
00959   KateFactory::self()->schemaManager()->removeSchema (t);
00960 
00961   update ();
00962 }
00963 
00964 void KateSchemaConfigPage::newSchema ()
00965 {
00966   QString t = KInputDialog::getText (i18n("Name for New Schema"), i18n ("Name:"), i18n("New Schema"), 0, this);
00967 
00968   KateFactory::self()->schemaManager()->addSchema (t);
00969 
00970   // soft update, no load from disk
00971   KateFactory::self()->schemaManager()->update (false);
00972   int i = KateFactory::self()->schemaManager()->list ().findIndex (t);
00973 
00974   update ();
00975   if (i > -1)
00976   {
00977     schemaCombo->setCurrentItem (i);
00978     schemaChanged (i);
00979   }
00980 }
00981 
00982 void KateSchemaConfigPage::schemaChanged (int schema)
00983 {
00984   btndel->setEnabled( schema > 1 );
00985 
00986   m_colorTab->schemaChanged( schema );
00987   m_fontTab->schemaChanged( schema );
00988   m_fontColorTab->schemaChanged (schema);
00989   m_highlightTab->schemaChanged (schema);
00990 
00991   m_lastSchema = schema;
00992 }
00993 
00994 void KateSchemaConfigPage::newCurrentPage (QWidget *w)
00995 {
00996   if (w == m_highlightTab)
00997     m_highlightTab->schemaChanged (m_lastSchema);
00998 }
00999 //END KateSchemaConfigPage
01000 
01001 //BEGIN SCHEMA ACTION -- the 'View->Schema' menu action
01002 void KateViewSchemaAction::init()
01003 {
01004   m_view = 0;
01005   last = 0;
01006 
01007   connect(popupMenu(),SIGNAL(aboutToShow()),this,SLOT(slotAboutToShow()));
01008 }
01009 
01010 void KateViewSchemaAction::updateMenu (KateView *view)
01011 {
01012   m_view = view;
01013 }
01014 
01015 void KateViewSchemaAction::slotAboutToShow()
01016 {
01017   KateView *view=m_view;
01018   int count = KateFactory::self()->schemaManager()->list().count();
01019 
01020   for (int z=0; z<count; z++)
01021   {
01022     QString hlName = KateFactory::self()->schemaManager()->list().operator[](z);
01023 
01024     if (names.contains(hlName) < 1)
01025     {
01026       names << hlName;
01027       popupMenu()->insertItem ( hlName, this, SLOT(setSchema(int)), 0,  z+1);
01028     }
01029   }
01030 
01031   if (!view) return;
01032 
01033   popupMenu()->setItemChecked (last, false);
01034   popupMenu()->setItemChecked (view->renderer()->config()->schema()+1, true);
01035 
01036   last = view->renderer()->config()->schema()+1;
01037 }
01038 
01039 void KateViewSchemaAction::setSchema (int mode)
01040 {
01041   KateView *view=m_view;
01042 
01043   if (view)
01044     view->renderer()->config()->setSchema (mode-1);
01045 }
01046 //END SCHEMA ACTION
01047 
01048 //BEGIN KateStyleListView
01049 KateStyleListView::KateStyleListView( QWidget *parent, bool showUseDefaults )
01050     : QListView( parent )
01051 {
01052   setSorting( -1 ); // disable sorting, let the styles appear in their defined order
01053   addColumn( i18n("Context") );
01054   addColumn( SmallIconSet("text_bold"), QString::null );
01055   addColumn( SmallIconSet("text_italic"), QString::null );
01056   addColumn( SmallIconSet("text_under"), QString::null );
01057   addColumn( SmallIconSet("text_strike"), QString::null );
01058   addColumn( i18n("Normal") );
01059   addColumn( i18n("Selected") );
01060   addColumn( i18n("Background") );
01061   addColumn( i18n("Background Selected") );
01062   if ( showUseDefaults )
01063     addColumn( i18n("Use Default Style") );
01064   connect( this, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)),
01065            this, SLOT(slotMousePressed(int, QListViewItem*, const QPoint&, int)) );
01066   connect( this, SIGNAL(contextMenuRequested(QListViewItem*,const QPoint&, int)),
01067            this, SLOT(showPopupMenu(QListViewItem*, const QPoint&)) );
01068   // grap the bg color, selected color and default font
01069   normalcol = KGlobalSettings::textColor();
01070   bgcol = KateRendererConfig::global()->backgroundColor();
01071   selcol = KateRendererConfig::global()->selectionColor();
01072   docfont = *KateRendererConfig::global()->font();
01073 
01074   viewport()->setPaletteBackgroundColor( bgcol );
01075 }
01076 
01077 void KateStyleListView::showPopupMenu( KateStyleListItem *i, const QPoint &globalPos, bool showtitle )
01078 {
01079   if ( !dynamic_cast<KateStyleListItem*>(i) ) return;
01080 
01081   KPopupMenu m( this );
01082   KateAttribute *is = i->style();
01083   int id;
01084   // the title is used, because the menu obscures the context name when
01085   // displayed on behalf of spacePressed().
01086   QPixmap cl(16,16);
01087   cl.fill( i->style()->textColor() );
01088   QPixmap scl(16,16);
01089   scl.fill( i->style()->selectedTextColor() );
01090   QPixmap bgcl(16,16);
01091   bgcl.fill( i->style()->itemSet(KateAttribute::BGColor) ? i->style()->bgColor() : viewport()->colorGroup().base() );
01092   QPixmap sbgcl(16,16);
01093   sbgcl.fill( i->style()->itemSet(KateAttribute::SelectedBGColor) ? i->style()->selectedBGColor() : viewport()->colorGroup().base() );
01094 
01095   if ( showtitle )
01096     m.insertTitle( i->contextName(), KateStyleListItem::ContextName );
01097   id = m.insertItem( i18n("&Bold"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Bold );
01098   m.setItemChecked( id, is->bold() );
01099   id = m.insertItem( i18n("&Italic"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Italic );
01100   m.setItemChecked( id, is->italic() );
01101   id = m.insertItem( i18n("&Underline"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Underline );
01102   m.setItemChecked( id, is->underline() );
01103   id = m.insertItem( i18n("S&trikeout"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Strikeout );
01104   m.setItemChecked( id, is->strikeOut() );
01105 
01106   m.insertSeparator();
01107 
01108   m.insertItem( QIconSet(cl), i18n("Normal &Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Color );
01109   m.insertItem( QIconSet(scl), i18n("&Selected Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::SelColor );
01110   m.insertItem( QIconSet(bgcl), i18n("&Background Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::BgColor );
01111   m.insertItem( QIconSet(sbgcl), i18n("S&elected Background Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::SelBgColor );
01112 
01113   // Unset [some] colors. I could show one only if that button was clicked, but that
01114   // would disable setting this with the keyboard (how many aren't doing just
01115   // that every day? ;)
01116   // ANY ideas for doing this in a nicer way will be warmly wellcomed.
01117   KateAttribute *style = i->style();
01118   if ( style->itemSet( KateAttribute::BGColor) || style->itemSet( KateAttribute::SelectedBGColor ) )
01119   {
01120     m.insertSeparator();
01121     if ( style->itemSet( KateAttribute::BGColor) )
01122       m.insertItem( i18n("Unset Background Color"), this, SLOT(unsetColor(int)), 0, 100 );
01123     if ( style->itemSet( KateAttribute::SelectedBGColor ) )
01124       m.insertItem( i18n("Unset Selected Background Color"), this, SLOT(unsetColor(int)), 0, 101 );
01125   }
01126 
01127   if ( ! i->isDefault() && ! i->defStyle() ) {
01128     m.insertSeparator();
01129     id = m.insertItem( i18n("Use &Default Style"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::UseDefStyle );
01130     m.setItemChecked( id, i->defStyle() );
01131   }
01132   m.exec( globalPos );
01133 }
01134 
01135 void KateStyleListView::showPopupMenu( QListViewItem *i, const QPoint &pos )
01136 {
01137   if ( dynamic_cast<KateStyleListItem*>(i) )
01138     showPopupMenu( (KateStyleListItem*)i, pos, true );
01139 }
01140 
01141 void KateStyleListView::mSlotPopupHandler( int z )
01142 {
01143   ((KateStyleListItem*)currentItem())->changeProperty( (KateStyleListItem::Property)z );
01144 }
01145 
01146 void KateStyleListView::unsetColor( int c )
01147 {
01148   ((KateStyleListItem*)currentItem())->unsetColor( c );
01149   emitChanged();
01150 }
01151 
01152 // Because QListViewItem::activatePos() is going to become deprecated,
01153 // and also because this attempt offers more control, I connect mousePressed to this.
01154 void KateStyleListView::slotMousePressed(int btn, QListViewItem* i, const QPoint& pos, int c)
01155 {
01156   if ( dynamic_cast<KateStyleListItem*>(i) ) {
01157      if ( btn == Qt::LeftButton && c > 0 ) {
01158       // map pos to item/column and call KateStyleListItem::activate(col, pos)
01159       ((KateStyleListItem*)i)->activate( c, viewport()->mapFromGlobal( pos ) - QPoint( 0, itemRect(i).top() ) );
01160     }
01161   }
01162 }
01163 
01164 //END
01165 
01166 //BEGIN KateStyleListItem
01167 static const int BoxSize = 16;
01168 static const int ColorBtnWidth = 32;
01169 
01170 KateStyleListItem::KateStyleListItem( QListViewItem *parent, const QString & stylename,
01171                               KateAttribute *style, KateHlItemData *data )
01172         : QListViewItem( parent, stylename ),
01173           ds( style ),
01174           st( data )
01175 {
01176   initStyle();
01177 }
01178 
01179 KateStyleListItem::KateStyleListItem( QListView *parent, const QString & stylename,
01180                               KateAttribute *style, KateHlItemData *data )
01181         : QListViewItem( parent, stylename ),
01182           ds( style ),
01183           st( data )
01184 {
01185   initStyle();
01186 }
01187 
01188 void KateStyleListItem::initStyle()
01189 {
01190   if (!st)
01191     is = ds;
01192   else
01193   {
01194     is = new KateAttribute (*ds);
01195 
01196     if (st->isSomethingSet())
01197       *is += *st;
01198   }
01199 }
01200 
01201 void KateStyleListItem::updateStyle()
01202 {
01203   // nothing there, not update it, will crash
01204   if (!st)
01205     return;
01206 
01207   if ( is->itemSet(KateAttribute::Weight) )
01208   {
01209     if ( is->weight() != st->weight())
01210       st->setWeight( is->weight() );
01211   }
01212   else st->clearAttribute( KateAttribute::Weight );
01213 
01214   if ( is->itemSet(KateAttribute::Italic) )
01215   {
01216     if ( is->italic() != st->italic())
01217       st->setItalic( is->italic() );
01218   }
01219   else st->clearAttribute( KateAttribute::Italic );
01220 
01221   if ( is->itemSet(KateAttribute::StrikeOut) )
01222   {
01223     if ( is->strikeOut() != st->strikeOut())
01224 
01225       st->setStrikeOut( is->strikeOut() );
01226   }
01227   else st->clearAttribute( KateAttribute::StrikeOut );
01228 
01229   if ( is->itemSet(KateAttribute::Underline) )
01230   {
01231     if ( is->underline() != st->underline())
01232       st->setUnderline( is->underline() );
01233   }
01234   else st->clearAttribute( KateAttribute::Underline );
01235 
01236   if ( is->itemSet(KateAttribute::Outline) )
01237   {
01238     if ( is->outline() != st->outline())
01239       st->setOutline( is->outline() );
01240   }
01241   else st->clearAttribute( KateAttribute::Outline );
01242 
01243   if ( is->itemSet(KateAttribute::TextColor) )
01244   {
01245     if ( is->textColor() != st->textColor())
01246       st->setTextColor( is->textColor() );
01247   }
01248   else st->clearAttribute( KateAttribute::TextColor );
01249 
01250   if ( is->itemSet(KateAttribute::SelectedTextColor) )
01251   {
01252     if ( is->selectedTextColor() != st->selectedTextColor())
01253       st->setSelectedTextColor( is->selectedTextColor() );
01254   }
01255   else st->clearAttribute( KateAttribute::SelectedTextColor);
01256 
01257   if ( is->itemSet(KateAttribute::BGColor) )
01258   {
01259     if ( is->bgColor() != st->bgColor())
01260       st->setBGColor( is->bgColor() );
01261   }
01262   else st->clearAttribute( KateAttribute::BGColor );
01263 
01264   if ( is->itemSet(KateAttribute::SelectedBGColor) )
01265   {
01266     if ( is->selectedBGColor() != st->selectedBGColor())
01267       st->setSelectedBGColor( is->selectedBGColor() );
01268   }
01269   else st->clearAttribute( KateAttribute::SelectedBGColor );
01270 }
01271 
01272 /* only true for a hl mode item using it's default style */
01273 bool KateStyleListItem::defStyle() { return st && st->itemsSet() != ds->itemsSet(); }
01274 
01275 /* true for default styles */
01276 bool KateStyleListItem::isDefault() { return st ? false : true; }
01277 
01278 int KateStyleListItem::width( const QFontMetrics & /*fm*/, const QListView * lv, int col ) const
01279 {
01280   int m = lv->itemMargin() * 2;
01281   switch ( col ) {
01282     case ContextName:
01283       // FIXME: width for name column should reflect bold/italic
01284       // (relevant for non-fixed fonts only - nessecary?)
01285       return QListViewItem::width( QFontMetrics( ((KateStyleListView*)lv)->docfont), lv, col);
01286     case Bold:
01287     case Italic:
01288     case UseDefStyle:
01289       return BoxSize + m;
01290     case Color:
01291     case SelColor:
01292     case BgColor:
01293     case SelBgColor:
01294       return ColorBtnWidth +m;
01295     default:
01296       return 0;
01297   }
01298 }
01299 
01300 void KateStyleListItem::activate( int column, const QPoint &localPos )
01301 {
01302   QListView *lv = listView();
01303   int x = 0;
01304   for( int c = 0; c < column-1; c++ )
01305     x += lv->columnWidth( c );
01306   int w;
01307   switch( column ) {
01308     case Bold:
01309     case Italic:
01310     case Underline:
01311     case Strikeout:
01312     case UseDefStyle:
01313       w = BoxSize;
01314       break;
01315     case Color:
01316     case SelColor:
01317     case BgColor:
01318     case SelBgColor:
01319       w = ColorBtnWidth;
01320       break;
01321     default:
01322       return;
01323   }
01324   if ( !QRect( x, 0, w, BoxSize ).contains( localPos ) )
01325   changeProperty( (Property)column );
01326 }
01327 
01328 void KateStyleListItem::changeProperty( Property p )
01329 {
01330   if ( p == Bold )
01331     is->setBold( ! is->bold() );
01332   else if ( p == Italic )
01333     is->setItalic( ! is->italic() );
01334   else if ( p == Underline )
01335     is->setUnderline( ! is->underline() );
01336   else if ( p == Strikeout )
01337     is->setStrikeOut( ! is->strikeOut() );
01338   else if ( p == UseDefStyle )
01339     toggleDefStyle();
01340   else
01341     setColor( p );
01342 
01343   updateStyle ();
01344 
01345   ((KateStyleListView*)listView())->emitChanged();
01346 }
01347 
01348 void KateStyleListItem::toggleDefStyle()
01349 {
01350   if ( *is == *ds ) {
01351     KMessageBox::information( listView(),
01352          i18n("\"Use Default Style\" will be automatically unset when you change any style properties."),
01353          i18n("Kate Styles"),
01354          "Kate hl config use defaults" );
01355   }
01356   else {
01357     delete is;
01358     is = new KateAttribute( *ds );
01359     updateStyle();
01360     repaint();
01361   }
01362 }
01363 
01364 void KateStyleListItem::setColor( int column )
01365 {
01366   QColor c; // use this
01367   QColor d; // default color
01368   if ( column == Color)
01369   {
01370     c = is->textColor();
01371     d = ds->textColor();
01372   }
01373   else if ( column == SelColor )
01374   {
01375     c = is->selectedTextColor();
01376     d = is->selectedTextColor();
01377   }
01378   else if ( column == BgColor )
01379   {
01380     c = is->bgColor();
01381     d = ds->bgColor();
01382   }
01383   else if ( column == SelBgColor )
01384   {
01385     c = is->selectedBGColor();
01386     d = ds->selectedBGColor();
01387   }
01388 
01389   if ( KColorDialog::getColor( c, d, listView() ) != QDialog::Accepted) return;
01390 
01391   bool def = ! c.isValid();
01392 
01393   // if set default, and the attrib is set in the default style use it
01394   // else if set default, unset it
01395   // else set the selected color
01396   switch (column)
01397   {
01398     case Color:
01399       if ( def )
01400       {
01401         if ( ds->itemSet(KateAttribute::TextColor) )
01402           is->setTextColor( ds->textColor());
01403         else
01404           is->clearAttribute(KateAttribute::TextColor);
01405       }
01406       else
01407         is->setTextColor( c );
01408     break;
01409     case SelColor:
01410       if ( def )
01411       {
01412         if ( ds->itemSet(KateAttribute::SelectedTextColor) )
01413           is->setSelectedTextColor( ds->selectedTextColor());
01414         else
01415           is->clearAttribute(KateAttribute::SelectedTextColor);
01416       }
01417       else
01418         is->setSelectedTextColor( c );
01419     break;
01420     case BgColor:
01421       if ( def )
01422       {
01423         if ( ds->itemSet(KateAttribute::BGColor) )
01424           is->setBGColor( ds->bgColor());
01425         else
01426           is->clearAttribute(KateAttribute::BGColor);
01427       }
01428       else
01429         is->setBGColor( c );
01430     break;
01431     case SelBgColor:
01432       if ( def )
01433       {
01434         if ( ds->itemSet(KateAttribute::SelectedBGColor) )
01435           is->setSelectedBGColor( ds->selectedBGColor());
01436         else
01437           is->clearAttribute(KateAttribute::SelectedBGColor);
01438       }
01439       else
01440         is->setSelectedBGColor( c );
01441     break;
01442   }
01443 
01444   repaint();
01445 }
01446 
01447 void KateStyleListItem::unsetColor( int c )
01448 {
01449   if ( c == 100 && is->itemSet(KateAttribute::BGColor) )
01450     is->clearAttribute(KateAttribute::BGColor);
01451   else if ( c == 101 && is->itemSet(KateAttribute::SelectedBGColor) )
01452     is->clearAttribute(KateAttribute::SelectedBGColor);
01453   updateStyle();
01454 }
01455 
01456 void KateStyleListItem::paintCell( QPainter *p, const QColorGroup& /*cg*/, int col, int width, int align )
01457 {
01458 
01459   if ( !p )
01460     return;
01461 
01462   QListView *lv = listView();
01463   if ( !lv )
01464     return;
01465   Q_ASSERT( lv ); //###
01466 
01467   // use a private color group and set the text/highlighted text colors
01468   QColorGroup mcg = lv->viewport()->colorGroup();
01469 
01470   if ( col ) // col 0 is drawn by the superclass method
01471     p->fillRect( 0, 0, width, height(), QBrush( mcg.base() ) );
01472 
01473   int marg = lv->itemMargin();
01474 
01475   QColor c;
01476 
01477   switch ( col )
01478   {
01479     case ContextName:
01480     {
01481       mcg.setColor(QColorGroup::Text, is->textColor());
01482       mcg.setColor(QColorGroup::HighlightedText, is->selectedTextColor());
01483       // text background color
01484       c = is->bgColor();
01485       if ( c.isValid() && is->itemSet(KateAttribute::BGColor) )
01486         mcg.setColor( QColorGroup::Base, c );
01487       if ( isSelected() && is->itemSet(KateAttribute::SelectedBGColor) )
01488       {
01489         c = is->selectedBGColor();
01490         if ( c.isValid() )
01491           mcg.setColor( QColorGroup::Highlight, c );
01492       }
01493       QFont f ( ((KateStyleListView*)lv)->docfont );
01494       p->setFont( is->font(f) );
01495       // FIXME - repainting when text is cropped, and the column is enlarged is buggy.
01496       // Maybe I need painting the string myself :(
01497       // (wilbert) it depends on the font used
01498       QListViewItem::paintCell( p, mcg, col, width, align );
01499     }
01500     break;
01501     case Bold:
01502     case Italic:
01503     case Underline:
01504     case Strikeout:
01505     case UseDefStyle:
01506     {
01507       // Bold/Italic/use default checkboxes
01508       // code allmost identical to QCheckListItem
01509       int x = 0;
01510       int y = (height() - BoxSize) / 2;
01511 
01512       if ( isEnabled() )
01513         p->setPen( QPen( mcg.text(), 2 ) );
01514       else
01515         p->setPen( QPen( lv->palette().color( QPalette::Disabled, QColorGroup::Text ), 2 ) );
01516 
01517       p->drawRect( x+marg, y+2, BoxSize-4, BoxSize-4 );
01518       x++;
01519       y++;
01520       if ( (col == Bold && is->bold()) ||
01521           (col == Italic && is->italic()) ||
01522           (col == Underline && is->underline()) ||
01523           (col == Strikeout && is->strikeOut()) ||
01524           (col == UseDefStyle && *is == *ds ) )
01525       {
01526         QPointArray a( 7*2 );
01527         int i, xx, yy;
01528         xx = x+1+marg;
01529         yy = y+5;
01530         for ( i=0; i<3; i++ ) {
01531           a.setPoint( 2*i,   xx, yy );
01532           a.setPoint( 2*i+1, xx, yy+2 );
01533           xx++; yy++;
01534         }
01535         yy -= 2;
01536         for ( i=3; i<7; i++ ) {
01537           a.setPoint( 2*i,   xx, yy );
01538           a.setPoint( 2*i+1, xx, yy+2 );
01539           xx++; yy--;
01540         }
01541         p->drawLineSegments( a );
01542       }
01543     }
01544     break;
01545     case Color:
01546     case SelColor:
01547     case BgColor:
01548     case SelBgColor:
01549     {
01550       bool set( false );
01551       if ( col == Color)
01552       {
01553         c = is->textColor();
01554         set = is->itemSet(KateAttribute::TextColor);
01555       }
01556       else if ( col == SelColor )
01557       {
01558         c = is->selectedTextColor();
01559         set = is->itemSet( KateAttribute::SelectedTextColor);
01560       }
01561       else if ( col == BgColor )
01562       {
01563         set = is->itemSet(KateAttribute::BGColor);
01564         c = set ? is->bgColor() : mcg.base();
01565       }
01566       else if ( col == SelBgColor )
01567       {
01568         set = is->itemSet(KateAttribute::SelectedBGColor);
01569         c = set ? is->selectedBGColor(): mcg.base();
01570       }
01571 
01572       // color "buttons"
01573       int x = 0;
01574       int y = (height() - BoxSize) / 2;
01575       if ( isEnabled() )
01576         p->setPen( QPen( mcg.text(), 2 ) );
01577       else
01578         p->setPen( QPen( lv->palette().color( QPalette::Disabled, QColorGroup::Text ), 2 ) );
01579 
01580       p->drawRect( x+marg, y+2, ColorBtnWidth-4, BoxSize-4 );
01581       p->fillRect( x+marg+1,y+3,ColorBtnWidth-7,BoxSize-7,QBrush( c ) );
01582       // if this item is unset, draw a diagonal line over the button
01583       if ( ! set )
01584         p->drawLine( x+marg-1, BoxSize-3, ColorBtnWidth-4, y+1 );
01585     }
01586     //case default: // no warning...
01587   }
01588 }
01589 //END
01590 
01591 //BEGIN KateStyleListCaption
01592 KateStyleListCaption::KateStyleListCaption( QListView *parent, const QString & name )
01593       :  QListViewItem( parent, name )
01594 {
01595 }
01596 
01597 void KateStyleListCaption::paintCell( QPainter *p, const QColorGroup& /*cg*/, int col, int width, int align )
01598 {
01599   QListView *lv = listView();
01600   if ( !lv )
01601     return;
01602   Q_ASSERT( lv ); //###
01603 
01604   // use the same colorgroup as the other items in the viewport
01605   QColorGroup mcg = lv->viewport()->colorGroup();
01606 
01607   QListViewItem::paintCell( p, mcg, col, width, align );
01608 }
01609 //END
01610 
01611 // kate: space-indent on; indent-width 2; replace-tabs on;

Kate

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
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