00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "kateschema.h"
00021 #include "kateschema.moc"
00022 
00023 #include "kateconfig.h"
00024 #include "katefactory.h"
00025 #include "kateview.h"
00026 #include "katerenderer.h"
00027 
00028 #include <klocale.h>
00029 #include <kdialog.h>
00030 #include <kcolorbutton.h>
00031 #include <kcombobox.h>
00032 #include <kinputdialog.h>
00033 #include <kfontdialog.h>
00034 #include <kdebug.h>
00035 #include <kiconloader.h>
00036 #include <kmessagebox.h>
00037 #include <kpopupmenu.h>
00038 #include <kcolordialog.h>
00039 #include <kapplication.h>
00040 #include <kaboutdata.h>
00041 
00042 #include <qbuttongroup.h>
00043 #include <qcheckbox.h>
00044 #include <qptrcollection.h>
00045 #include <qdialog.h>
00046 #include <qgrid.h>
00047 #include <qgroupbox.h>
00048 #include <qlabel.h>
00049 #include <qtextcodec.h>
00050 #include <qlayout.h>
00051 #include <qlineedit.h>
00052 #include <qheader.h>
00053 #include <qlistbox.h>
00054 #include <qhbox.h>
00055 #include <qpainter.h>
00056 #include <qobjectlist.h>
00057 #include <qpushbutton.h>
00058 #include <qradiobutton.h>
00059 #include <qspinbox.h>
00060 #include <qstringlist.h>
00061 #include <qtabwidget.h>
00062 #include <qvbox.h>
00063 #include <qvgroupbox.h>
00064 #include <qwhatsthis.h>
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 class KateStyleListItem : public QListViewItem
00078 {
00079   public:
00080     KateStyleListItem( QListView *parent=0, const QString & stylename=0,
00081                    class KateAttribute* defaultstyle=0, class ItemData *data=0 );
00082     ~KateStyleListItem() { if (st) delete is; };
00083 
00084     
00085     enum Property { ContextName, Bold, Italic, Underline, Strikeout, Color, SelColor, BgColor, SelBgColor, UseDefStyle };
00086 
00087     
00088     void updateStyle();
00089     
00090     virtual int width ( const QFontMetrics & fm, const QListView * lv, int c ) const;
00091     
00092     void activate( int column, const QPoint &localPos );
00093     
00094     void changeProperty( Property p );
00095     
00096     QString contextName() { return text(0); };
00097     
00098     bool defStyle();
00099     
00100     bool isDefault();
00101     
00102 
00103     class KateAttribute* style() { return is; };
00104   
00105   protected:
00106     
00107     void paintCell(QPainter *p, const QColorGroup& cg, int col, int width, int align);
00108   
00109   private:
00110     
00111     void toggleDefStyle();
00112     void setColor( int );
00113     
00114 
00115     void setCustStyle();
00116 
00117     class KateAttribute *is, 
00118               *ds;           
00119     class ItemData *st;      
00120 };
00121 
00122 QString KateSchemaManager::normalSchema ()
00123 {
00124   return KApplication::kApplication()->aboutData()->appName () + QString (" - Normal");
00125 }
00126 
00127 QString KateSchemaManager::printingSchema ()
00128 {
00129   return KApplication::kApplication()->aboutData()->appName () + QString (" - Printing");
00130 }
00131 
00132 KateSchemaManager::KateSchemaManager ()
00133   : m_config ("kateschemarc", false, false)
00134 {
00135   update ();
00136 }
00137 
00138 KateSchemaManager::~KateSchemaManager ()
00139 {
00140 }
00141 
00142 
00143 
00144 
00145 void KateSchemaManager::update (bool readfromfile)
00146 {
00147   if (readfromfile)
00148     m_config.reparseConfiguration ();
00149 
00150   m_schemas = m_config.groupList();
00151   m_schemas.sort ();
00152 
00153   m_schemas.remove (printingSchema());
00154   m_schemas.remove (normalSchema());
00155   m_schemas.prepend (printingSchema());
00156   m_schemas.prepend (normalSchema());
00157 }
00158 
00159 
00160 
00161 
00162 
00163 KConfig *KateSchemaManager::schema (uint number)
00164 {
00165   if ((number>1) && (number < m_schemas.count()))
00166     m_config.setGroup (m_schemas[number]);
00167   else if (number == 1)
00168     m_config.setGroup (printingSchema());
00169   else
00170     m_config.setGroup (normalSchema());
00171 
00172   return &m_config;
00173 }
00174 
00175 void KateSchemaManager::addSchema (const QString &t)
00176 {
00177   m_config.setGroup (t);
00178   m_config.writeEntry("Color Background", KGlobalSettings::baseColor());
00179 
00180   update (false);
00181 }
00182 
00183 void KateSchemaManager::removeSchema (uint number)
00184 {
00185   if (number >= m_schemas.count())
00186     return;
00187 
00188   if (number < 2)
00189     return;
00190 
00191   m_config.deleteGroup (name (number));
00192 
00193   update (false);
00194 }
00195 
00196 bool KateSchemaManager::validSchema (uint number)
00197 {
00198   if (number < m_schemas.count())
00199     return true;
00200 
00201   return false;
00202 }
00203 
00204 uint KateSchemaManager::number (const QString &name)
00205 {
00206   if (name == normalSchema())
00207     return 0;
00208 
00209   if (name == printingSchema())
00210     return 1;
00211 
00212   int i;
00213   if ((i = m_schemas.findIndex(name)) > -1)
00214     return i;
00215 
00216   return 0;
00217 }
00218 
00219 QString KateSchemaManager::name (uint number)
00220 {
00221   if ((number>1) && (number < m_schemas.count()))
00222     return m_schemas[number];
00223   else if (number == 1)
00224     return printingSchema();
00225 
00226   return normalSchema();
00227 }
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 KateSchemaConfigColorTab::KateSchemaConfigColorTab( QWidget *parent, const char * )
00238   : QWidget (parent)
00239 {
00240   QHBox *b;
00241   QLabel *label;
00242 
00243   QVBoxLayout *blay=new QVBoxLayout(this, 0, KDialog::spacingHint());
00244 
00245   QVGroupBox *gbTextArea = new QVGroupBox(i18n("Text Area Background"), this);
00246 
00247   b = new QHBox (gbTextArea);
00248   label = new QLabel( i18n("Normal text:"), b);
00249   label->setAlignment( AlignLeft|AlignVCenter);
00250   m_back = new KColorButton(b);
00251   connect( m_back, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00252 
00253   b = new QHBox (gbTextArea);
00254   label = new QLabel( i18n("Selected text:"), b);
00255   label->setAlignment( AlignLeft|AlignVCenter);
00256   m_selected = new KColorButton(b);
00257   connect( m_selected, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00258 
00259   b = new QHBox (gbTextArea);
00260   label = new QLabel( i18n("Current line:"), b);
00261   label->setAlignment( AlignLeft|AlignVCenter);
00262   m_current = new KColorButton(b);
00263   connect( m_current, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00264 
00265   blay->addWidget(gbTextArea);
00266 
00267   QVGroupBox *gbBorder = new QVGroupBox(i18n("Additional Elements"), this);
00268 
00269   b = new QHBox (gbBorder);
00270   label = new QLabel( i18n("Left border background:"), b);
00271   label->setAlignment( AlignLeft|AlignVCenter);
00272   m_iconborder = new KColorButton(b);
00273   connect( m_iconborder, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00274 
00275   b = new QHBox (gbBorder);
00276   label = new QLabel( i18n("Bracket highlight:"), b);
00277   label->setAlignment( AlignLeft|AlignVCenter);
00278   m_bracket = new KColorButton(b);
00279   connect( m_bracket, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00280 
00281   b = new QHBox (gbBorder);
00282   label = new QLabel( i18n("Word wrap markers:"), b);
00283   label->setAlignment( AlignLeft|AlignVCenter);
00284   m_wwmarker = new KColorButton(b);
00285   connect( m_wwmarker, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00286 
00287   b = new QHBox (gbBorder);
00288   label = new QLabel( i18n("Tab markers:"), b);
00289   label->setAlignment( AlignLeft|AlignVCenter);
00290   m_tmarker = new KColorButton(b);
00291   connect( m_tmarker, SIGNAL( changed( const QColor & ) ), parent->parentWidget(), SLOT( slotChanged() ) );
00292 
00293   blay->addWidget(gbBorder);
00294 
00295   blay->addStretch();
00296 
00297   
00298   QWhatsThis::add(m_back, i18n("<p>Sets the background color of the editing area.</p>"));
00299   QWhatsThis::add(m_selected, i18n("<p>Sets the background color of the selection.</p>"
00300         "<p>To set the text color for selected text, use the \"<b>Configure "
00301         "Highlighting</b>\" dialog.</p>"));
00302   QWhatsThis::add(m_current, i18n("<p>Sets the background color of the currently "
00303         "active line, which means the line where your cursor is positioned.</p>"));
00304   QWhatsThis::add(m_bracket, i18n("<p>Sets the bracket matching color. This means, "
00305         "if you place the cursor e.g. at a <b>(</b>, the matching <b>)</b> will "
00306         "be highlighted with this color.</p>"));
00307   QWhatsThis::add(m_wwmarker, i18n(
00308         "<p>Sets the color of Word Wrap-related markers:</p>"
00309         "<dl><dt>Static Word Wrap</dt><dd>A vertical line which shows the column where "
00310         "text is going to be wrapped</dd>"
00311         "<dt>Dynamic Word Wrap</dt><dd>An arrow shown to the left of "
00312         "visually-wrapped lines</dd></dl>"));
00313   QWhatsThis::add(m_tmarker, i18n(
00314         "<p>Sets the color of the tabulator marks:</p>"));
00315 }
00316 
00317 KateSchemaConfigColorTab::~KateSchemaConfigColorTab()
00318 {
00319 }
00320 
00321 void KateSchemaConfigColorTab::readConfig (KConfig *config)
00322 {
00323   QColor tmp0 (KGlobalSettings::baseColor());
00324   QColor tmp1 (KGlobalSettings::highlightColor());
00325   QColor tmp2 (KGlobalSettings::alternateBackgroundColor());
00326   QColor tmp3 ( "#FFFF99" );
00327   QColor tmp4 (tmp2.dark());
00328   QColor tmp5 ( KGlobalSettings::textColor() );
00329   QColor tmp6 ( "#EAE9E8" );
00330 
00331   m_back->setColor(config->readColorEntry("Color Background", &tmp0));
00332   m_selected->setColor(config->readColorEntry("Color Selection", &tmp1));
00333   m_current->setColor(config->readColorEntry("Color Highlighted Line", &tmp2));
00334   m_bracket->setColor(config->readColorEntry("Color Highlighted Bracket", &tmp3));
00335   m_wwmarker->setColor(config->readColorEntry("Color Word Wrap Marker", &tmp4));
00336   m_tmarker->setColor(config->readColorEntry("Color Tab Marker", &tmp5));
00337   m_iconborder->setColor(config->readColorEntry("Color Icon Bar", &tmp6));
00338 }
00339 
00340 void KateSchemaConfigColorTab::writeConfig (KConfig *config)
00341 {
00342   config->writeEntry("Color Background", m_back->color());
00343   config->writeEntry("Color Selection", m_selected->color());
00344   config->writeEntry("Color Highlighted Line", m_current->color());
00345   config->writeEntry("Color Highlighted Bracket", m_bracket->color());
00346   config->writeEntry("Color Word Wrap Marker", m_wwmarker->color());
00347   config->writeEntry("Color Tab Marker", m_tmarker->color());
00348   config->writeEntry("Color Icon Bar", m_iconborder->color());
00349 }
00350 
00351 
00352 
00353 
00354 KateSchemaConfigFontTab::KateSchemaConfigFontTab( QWidget *parent, const char * )
00355   : QWidget (parent)
00356 {
00357     
00358   QGridLayout *grid = new QGridLayout( this, 1, 1 );
00359 
00360   m_fontchooser = new KFontChooser ( this, 0L, false, QStringList(), false );
00361   m_fontchooser->enableColumn(KFontChooser::StyleList, false);
00362   grid->addWidget( m_fontchooser, 0, 0);
00363 
00364   connect (m_fontchooser, SIGNAL (fontSelected( const QFont & )), this, SLOT (slotFontSelected( const QFont & )));
00365   connect (m_fontchooser, SIGNAL (fontSelected( const QFont & )), parent->parentWidget(), SLOT (slotChanged()));
00366 }
00367 
00368 KateSchemaConfigFontTab::~KateSchemaConfigFontTab()
00369 {
00370 }
00371 
00372 void KateSchemaConfigFontTab::slotFontSelected( const QFont &font )
00373 {
00374   myFont = font;
00375 }
00376 
00377 void KateSchemaConfigFontTab::readConfig (KConfig *config)
00378 {
00379   QFont f (KGlobalSettings::fixedFont());
00380 
00381   m_fontchooser->setFont (config->readFontEntry("Font", &f));
00382 }
00383 
00384 void KateSchemaConfigFontTab::writeConfig (KConfig *config)
00385 {
00386   config->writeEntry("Font", myFont);
00387 }
00388 
00389 
00390 
00391 
00392 KateSchemaConfigFontColorTab::KateSchemaConfigFontColorTab( QWidget *parent, const char * )
00393   : QWidget (parent)
00394 {
00395   m_defaultStyleLists.setAutoDelete(true);
00396 
00397   
00398   QGridLayout *grid = new QGridLayout( this, 1, 1 );
00399 
00400   m_defaultStyles = new KateStyleListView( this, false );
00401   grid->addWidget( m_defaultStyles, 0, 0);
00402 
00403   connect (m_defaultStyles, SIGNAL (changed()), parent->parentWidget(), SLOT (slotChanged()));
00404 }
00405 
00406 KateSchemaConfigFontColorTab::~KateSchemaConfigFontColorTab()
00407 {
00408 }
00409 
00410 KateAttributeList *KateSchemaConfigFontColorTab::attributeList (uint schema)
00411 {
00412   if (!m_defaultStyleLists[schema])
00413   {
00414     KateAttributeList *list = new KateAttributeList ();
00415     HlManager::self()->getDefaults(schema, *list);
00416 
00417     m_defaultStyleLists.insert (schema, list);
00418   }
00419 
00420   return m_defaultStyleLists[schema];
00421 }
00422 
00423 void KateSchemaConfigFontColorTab::schemaChanged (uint schema)
00424 {
00425   m_defaultStyles->clear ();
00426 
00427   KateAttributeList *l = attributeList (schema);
00428 
00429   for ( uint i = 0; i < HlManager::self()->defaultStyles(); i++ )
00430   {
00431     m_defaultStyles->insertItem( new KateStyleListItem( m_defaultStyles, HlManager::self()->defaultStyleName(i),
00432                               l->at( i ) ) );
00433   }
00434 }
00435 
00436 void KateSchemaConfigFontColorTab::reload ()
00437 {
00438   m_defaultStyles->clear ();
00439   m_defaultStyleLists.clear ();
00440 }
00441 
00442 void KateSchemaConfigFontColorTab::apply ()
00443 {
00444   for ( QIntDictIterator<KateAttributeList> it( m_defaultStyleLists ); it.current(); ++it )
00445     HlManager::self()->setDefaults(it.currentKey(), *(it.current()));
00446 }
00447 
00448 
00449 
00450 
00451 KateSchemaConfigHighlightTab::KateSchemaConfigHighlightTab( QWidget *parent, const char *, KateSchemaConfigFontColorTab *page )
00452   : QWidget (parent)
00453 {
00454   m_defaults = page;
00455 
00456   m_schema = 0;
00457   m_hl = 0;
00458 
00459   m_hlDict.setAutoDelete (true);
00460 
00461   QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() );
00462 
00463   
00464   QHBox *hbHl = new QHBox( this );
00465   layout->add (hbHl);
00466 
00467   hbHl->setSpacing( KDialog::spacingHint() );
00468   QLabel *lHl = new QLabel( i18n("H&ighlight:"), hbHl );
00469   hlCombo = new QComboBox( false, hbHl );
00470   lHl->setBuddy( hlCombo );
00471   connect( hlCombo, SIGNAL(activated(int)),
00472            this, SLOT(hlChanged(int)) );
00473 
00474   for( int i = 0; i < HlManager::self()->highlights(); i++) {
00475     if (HlManager::self()->hlSection(i).length() > 0)
00476       hlCombo->insertItem(HlManager::self()->hlSection(i) + QString ("/") + HlManager::self()->hlName(i));
00477     else
00478       hlCombo->insertItem(HlManager::self()->hlName(i));
00479   }
00480   hlCombo->setCurrentItem(0);
00481 
00482   
00483   m_styles = new KateStyleListView( this, true );
00484   layout->addWidget (m_styles, 999);
00485 
00486   hlCombo->setCurrentItem ( 0 );
00487   hlChanged ( 0 );
00488 
00489   QWhatsThis::add( m_styles,  i18n("This list displays the contexts of the current syntax highlight mode and offers the means to edit them. The context name reflects the current style settings.<p>To edit using the keyboard, press <strong><SPACE></strong> and choose a property from the popup menu.<p>To edit the colors, click the colored squares, or select the color to edit from the popup menu.") );
00490 
00491   connect (m_styles, SIGNAL (changed()), parent->parentWidget(), SLOT (slotChanged()));
00492 }
00493 
00494 KateSchemaConfigHighlightTab::~KateSchemaConfigHighlightTab()
00495 {
00496 }
00497 
00498 void KateSchemaConfigHighlightTab::hlChanged(int z)
00499 {
00500   m_hl = z;
00501 
00502   schemaChanged (m_schema);
00503 }
00504 
00505 void KateSchemaConfigHighlightTab::schemaChanged (uint schema)
00506 {
00507   m_schema = schema;
00508 
00509   kdDebug () << "NEW SCHEMA: " << m_schema << " NEW HL: " << m_hl << endl;
00510 
00511   m_styles->clear ();
00512 
00513   if (!m_hlDict[m_schema])
00514   {
00515     kdDebug () << "NEW SCHEMA, create dict" << endl;
00516 
00517     m_hlDict.insert (schema, new QIntDict<ItemDataList>);
00518     m_hlDict[m_schema]->setAutoDelete (true);
00519   }
00520 
00521   if (!m_hlDict[m_schema]->find(m_hl))
00522   {
00523     kdDebug () << "NEW HL, create list" << endl;
00524 
00525     ItemDataList *list = new ItemDataList ();
00526     HlManager::self()->getHl( m_hl )->getItemDataListCopy (m_schema, *list);
00527     m_hlDict[m_schema]->insert (m_hl, list);
00528   }
00529 
00530   KateAttributeList *l = m_defaults->attributeList (schema);
00531 
00532   for ( ItemData *itemData = m_hlDict[m_schema]->find(m_hl)->first();
00533         itemData != 0L;
00534         itemData = m_hlDict[m_schema]->find(m_hl)->next())
00535   {
00536     kdDebug () << "insert items " << itemData->name << endl;
00537 
00538     m_styles->insertItem( new KateStyleListItem( m_styles, itemData->name,
00539                           l->at(itemData->defStyleNum), itemData ) );
00540 
00541   }
00542 }
00543 
00544 void KateSchemaConfigHighlightTab::reload ()
00545 {
00546   m_styles->clear ();
00547   m_hlDict.clear ();
00548 
00549   hlChanged (0);
00550 }
00551 
00552 void KateSchemaConfigHighlightTab::apply ()
00553 {
00554   for ( QIntDictIterator< QIntDict<ItemDataList> > it( m_hlDict ); it.current(); ++it )
00555     for ( QIntDictIterator< ItemDataList > it2( *it.current() ); it2.current(); ++it2 )
00556        HlManager::self()->getHl( it2.currentKey() )->setItemDataList (it.currentKey(), *(it2.current()));
00557 }
00558 
00559 
00560 
00561 KateSchemaConfigPage::KateSchemaConfigPage( QWidget *parent )
00562   : KateConfigPage( parent ),
00563     m_lastSchema (-1)
00564 {
00565   QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() );
00566 
00567   QHBox *hbHl = new QHBox( this );
00568   layout->add (hbHl);
00569   hbHl->setSpacing( KDialog::spacingHint() );
00570   QLabel *lHl = new QLabel( i18n("&Schema:"), hbHl );
00571   schemaCombo = new QComboBox( false, hbHl );
00572   lHl->setBuddy( schemaCombo );
00573   connect( schemaCombo, SIGNAL(activated(int)),
00574            this, SLOT(schemaChanged(int)) );
00575 
00576   btndel = new QPushButton( i18n("&Delete"), hbHl );
00577   connect( btndel, SIGNAL(clicked()), this, SLOT(deleteSchema()) );
00578 
00579   QPushButton *btnnew = new QPushButton( i18n("&New..."), hbHl );
00580   connect( btnnew, SIGNAL(clicked()), this, SLOT(newSchema()) );
00581 
00582   m_tabWidget = new QTabWidget ( this );
00583   m_tabWidget->setMargin (KDialog::marginHint());
00584   layout->add (m_tabWidget);
00585 
00586   connect (m_tabWidget, SIGNAL (currentChanged (QWidget *)), this, SLOT (newCurrentPage (QWidget *)));
00587 
00588   m_colorTab = new KateSchemaConfigColorTab (m_tabWidget);
00589   m_tabWidget->addTab (m_colorTab, i18n("Colors"));
00590 
00591   m_fontTab = new KateSchemaConfigFontTab (m_tabWidget);
00592   m_tabWidget->addTab (m_fontTab, i18n("Font"));
00593 
00594   m_fontColorTab = new KateSchemaConfigFontColorTab (m_tabWidget);
00595   m_tabWidget->addTab (m_fontColorTab, i18n("Normal Text Styles"));
00596 
00597   m_highlightTab = new KateSchemaConfigHighlightTab (m_tabWidget, "", m_fontColorTab);
00598   m_tabWidget->addTab (m_highlightTab, i18n("Highlighting Text Styles"));
00599 
00600   hbHl = new QHBox( this );
00601   layout->add (hbHl);
00602   hbHl->setSpacing( KDialog::spacingHint() );
00603   lHl = new QLabel( i18n("&Default schema for %1:").arg(KApplication::kApplication()->aboutData()->programName ()), hbHl );
00604   defaultSchemaCombo = new QComboBox( false, hbHl );
00605   lHl->setBuddy( defaultSchemaCombo );
00606   
00607   reload();
00608   
00609   connect( defaultSchemaCombo, SIGNAL(activated(int)),
00610            this, SLOT(slotChanged()) );
00611 }
00612 
00613 KateSchemaConfigPage::~KateSchemaConfigPage ()
00614 {
00615   
00616   KateFactory::self()->schemaManager()->update ();
00617 }
00618 
00619 void KateSchemaConfigPage::apply()
00620 {
00621   if (m_lastSchema > -1)
00622   {
00623     m_colorTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema));
00624     m_fontTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema));
00625   }
00626 
00627   
00628   KateFactory::self()->schemaManager()->schema (0)->sync();
00629   KateFactory::self()->schemaManager()->update ();
00630 
00631   KateRendererConfig::global()->setSchema (defaultSchemaCombo->currentItem());
00632 
00633   
00634   m_fontColorTab->apply ();
00635   m_highlightTab->apply ();
00636 
00637   
00638   HlManager::self()->getKConfig()->sync ();
00639 }
00640 
00641 void KateSchemaConfigPage::reload()
00642 {
00643   
00644   KateFactory::self()->schemaManager()->update ();
00645 
00646   
00647   m_fontColorTab->reload ();
00648 
00649   update ();
00650 
00651   defaultSchemaCombo->setCurrentItem (KateRendererConfig::global()->schema());
00652 }
00653 
00654 void KateSchemaConfigPage::reset()
00655 {
00656   reload ();
00657 }
00658 
00659 void KateSchemaConfigPage::defaults()
00660 {
00661   reload ();
00662 }
00663 
00664 void KateSchemaConfigPage::update ()
00665 {
00666   
00667   KateFactory::self()->schemaManager()->update (false);
00668 
00669   schemaCombo->clear ();
00670   schemaCombo->insertStringList (KateFactory::self()->schemaManager()->list ());
00671 
00672   defaultSchemaCombo->clear ();
00673   defaultSchemaCombo->insertStringList (KateFactory::self()->schemaManager()->list ());
00674 
00675   schemaCombo->setCurrentItem (0);
00676   schemaChanged (0);
00677 
00678   schemaCombo->setEnabled (schemaCombo->count() > 0);
00679 }
00680 
00681 void KateSchemaConfigPage::deleteSchema ()
00682 {
00683   int t = schemaCombo->currentItem ();
00684 
00685   KateFactory::self()->schemaManager()->removeSchema (t);
00686 
00687   update ();
00688 }
00689 
00690 void KateSchemaConfigPage::newSchema ()
00691 {
00692   QString t = KInputDialog::getText (i18n("Name for New Schema"), i18n ("Name:"), i18n("New Schema"), 0, this);
00693 
00694   KateFactory::self()->schemaManager()->addSchema (t);
00695 
00696   
00697   KateFactory::self()->schemaManager()->update (false);
00698   int i = KateFactory::self()->schemaManager()->list ().findIndex (t);
00699 
00700   update ();
00701   if (i > -1)
00702   {
00703     schemaCombo->setCurrentItem (i);
00704     schemaChanged (i);
00705   }
00706 }
00707 
00708 void KateSchemaConfigPage::schemaChanged (int schema)
00709 {
00710   if (schema < 2)
00711   {
00712     btndel->setEnabled (false);
00713   }
00714   else
00715   {
00716     btndel->setEnabled (true);
00717   }
00718 
00719   if (m_lastSchema > -1)
00720   {
00721     m_colorTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema));
00722     m_fontTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema));
00723   }
00724 
00725   m_colorTab->readConfig (KateFactory::self()->schemaManager()->schema(schema));
00726   m_fontTab->readConfig (KateFactory::self()->schemaManager()->schema(schema));
00727   m_fontColorTab->schemaChanged (schema);
00728   m_highlightTab->schemaChanged (schema);
00729 
00730   m_lastSchema = schema;
00731 }
00732 
00733 void KateSchemaConfigPage::newCurrentPage (QWidget *w)
00734 {
00735   if (w == m_highlightTab)
00736     m_highlightTab->schemaChanged (m_lastSchema);
00737 }
00738 
00739 
00740 void KateViewSchemaAction::init()
00741 {
00742   m_view = 0;
00743   last = 0;
00744 
00745   connect(popupMenu(),SIGNAL(aboutToShow()),this,SLOT(slotAboutToShow()));
00746 }
00747 
00748 void KateViewSchemaAction::updateMenu (KateView *view)
00749 {
00750   m_view = view;
00751 }
00752 
00753 void KateViewSchemaAction::slotAboutToShow()
00754 {
00755   KateView *view=m_view;
00756   int count = KateFactory::self()->schemaManager()->list().count();
00757 
00758   for (int z=0; z<count; z++)
00759   {
00760     QString hlName = KateFactory::self()->schemaManager()->list().operator[](z);
00761 
00762     if (names.contains(hlName) < 1)
00763     {
00764       names << hlName;
00765       popupMenu()->insertItem ( hlName, this, SLOT(setSchema(int)), 0,  z+1);
00766     }
00767   }
00768 
00769   if (!view) return;
00770 
00771   popupMenu()->setItemChecked (last, false);
00772   popupMenu()->setItemChecked (view->renderer()->config()->schema()+1, true);
00773 
00774   last = view->renderer()->config()->schema()+1;
00775 }
00776 
00777 void KateViewSchemaAction::setSchema (int mode)
00778 {
00779   KateView *view=m_view;
00780 
00781   if (view)
00782     view->renderer()->config()->setSchema (mode-1);
00783 }
00784 
00785 
00786 
00787 KateStyleListView::KateStyleListView( QWidget *parent, bool showUseDefaults )
00788     : QListView( parent )
00789 {
00790   addColumn( i18n("Context") );
00791   addColumn( SmallIconSet("text_bold"), QString::null );
00792   addColumn( SmallIconSet("text_italic"), QString::null );
00793   addColumn( SmallIconSet("text_under"), QString::null );
00794   addColumn( SmallIconSet("text_strike"), QString::null );
00795   addColumn( i18n("Normal") );
00796   addColumn( i18n("Selected") );
00797   addColumn( i18n("Background") );
00798   addColumn( i18n("Background Selected") );
00799   if ( showUseDefaults )
00800     addColumn( i18n("Use Default Style") );
00801   connect( this, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)),
00802            this, SLOT(slotMousePressed(int, QListViewItem*, const QPoint&, int)) );
00803   connect( this, SIGNAL(spacePressed(QListViewItem*)),
00804            this, SLOT(showPopupMenu(QListViewItem*)) );
00805   
00806   normalcol = KGlobalSettings::textColor();
00807   bgcol = *KateRendererConfig::global()->backgroundColor();
00808   selcol = *KateRendererConfig::global()->selectionColor();
00809   docfont = *KateRendererConfig::global()->font();
00810 
00811   viewport()->setPaletteBackgroundColor( bgcol );
00812 }
00813 
00814 void KateStyleListView::showPopupMenu( KateStyleListItem *i, const QPoint &globalPos, bool showtitle )
00815 {
00816   KPopupMenu m( this );
00817   KateAttribute *is = i->style();
00818   int id;
00819   
00820   
00821   QPixmap cl(16,16);
00822   cl.fill( i->style()->textColor() );
00823   QPixmap scl(16,16);
00824   scl.fill( i->style()->selectedTextColor() );
00825   if ( showtitle )
00826     m.insertTitle( i->contextName(), KateStyleListItem::ContextName );
00827   id = m.insertItem( i18n("&Bold"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Bold );
00828   m.setItemChecked( id, is->bold() );
00829   id = m.insertItem( i18n("&Italic"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Italic );
00830   m.setItemChecked( id, is->italic() );
00831   m.insertItem( QIconSet(cl), i18n("Normal &Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Color );
00832   m.insertItem( QIconSet(scl), i18n("&Selected Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::SelColor );
00833   if ( ! i->isDefault() ) {
00834     id = m.insertItem( i18n("Use &Default Style"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::UseDefStyle );
00835     m.setItemChecked( id, i->defStyle() );
00836   }
00837   m.exec( globalPos );
00838 }
00839 
00840 void KateStyleListView::showPopupMenu( QListViewItem *i )
00841 {
00842   showPopupMenu( (KateStyleListItem*)i, viewport()->mapToGlobal(itemRect(i).topLeft()), true );
00843 }
00844 
00845 void KateStyleListView::mSlotPopupHandler( int z )
00846 {
00847   ((KateStyleListItem*)currentItem())->changeProperty( (KateStyleListItem::Property)z );
00848 }
00849 
00850 
00851 
00852 void KateStyleListView::slotMousePressed(int btn, QListViewItem* i, const QPoint& pos, int c)
00853 {
00854   if ( i ) {
00855     if ( btn == Qt::RightButton ) {
00856       showPopupMenu( (KateStyleListItem*)i, pos );
00857     }
00858     else if ( btn == Qt::LeftButton && c > 0 ) {
00859       
00860       ((KateStyleListItem*)i)->activate( c, viewport()->mapFromGlobal( pos ) - QPoint( 0, itemRect(i).top() ) );
00861     }
00862   }
00863 }
00864 
00865 
00866 
00867 
00868 static const int BoxSize = 16;
00869 static const int ColorBtnWidth = 32;
00870 
00871 KateStyleListItem::KateStyleListItem( QListView *parent, const QString & stylename,
00872                               KateAttribute *style, ItemData *data )
00873         : QListViewItem( parent, stylename ),
00874           ds( style ),
00875           st( data )
00876 {
00877   if (!st)
00878     is = ds;
00879   else
00880   {
00881     is = new KateAttribute (*style);
00882 
00883     if (data->isSomethingSet())
00884       *is += *data;
00885   }
00886 }
00887 
00888 void KateStyleListItem::updateStyle()
00889 {
00890   
00891   if (!st)
00892     return;
00893 
00894   if ( is->itemSet(KateAttribute::Weight) )
00895   {
00896     if ( is->weight() != st->weight())
00897       st->setWeight( is->weight() );
00898   }
00899 
00900   if ( is->itemSet(KateAttribute::Italic) )
00901   {
00902     if ( is->italic() != st->italic())
00903       st->setItalic( is->italic() );
00904   }
00905 
00906   if ( is->itemSet(KateAttribute::StrikeOut) )
00907   {
00908     if ( is->strikeOut() != st->strikeOut())
00909 
00910       st->setStrikeOut( is->strikeOut() );
00911   }
00912 
00913   if ( is->itemSet(KateAttribute::Underline) )
00914   {
00915     if ( is->underline() != st->underline())
00916       st->setUnderline( is->underline() );
00917   }
00918 
00919   if ( is->itemSet(KateAttribute::Outline) )
00920   {
00921     if ( is->outline() != st->outline())
00922       st->setOutline( is->outline() );
00923   }
00924 
00925   if ( is->itemSet(KateAttribute::TextColor) )
00926   {
00927     if ( is->textColor() != st->textColor())
00928       st->setTextColor( is->textColor() );
00929   }
00930 
00931   if ( is->itemSet(KateAttribute::SelectedTextColor) )
00932   {
00933     if ( is->selectedTextColor() != st->selectedTextColor())
00934       st->setSelectedTextColor( is->selectedTextColor() );
00935   }
00936 
00937   if ( is->itemSet(KateAttribute::BGColor) )
00938   {
00939     if ( is->bgColor() != st->bgColor())
00940       st->setBGColor( is->bgColor() );
00941   }
00942 
00943   if ( is->itemSet(KateAttribute::SelectedBGColor) )
00944   {
00945     if ( is->selectedBGColor() != st->selectedBGColor())
00946       st->setSelectedBGColor( is->selectedBGColor() );
00947   }
00948   
00949   
00950 }
00951 
00952 
00953 bool KateStyleListItem::defStyle() { return st && st->isSomethingSet(); }
00954 
00955 
00956 bool KateStyleListItem::isDefault() { return st ? false : true; }
00957 
00958 int KateStyleListItem::width( const QFontMetrics & , const QListView * lv, int col ) const
00959 {
00960   int m = lv->itemMargin() * 2;
00961   switch ( col ) {
00962     case ContextName:
00963       
00964       
00965       return QFontMetrics( ((KateStyleListView*)lv)->docfont).width( text(0) ) + m;
00966     case Bold:
00967     case Italic:
00968     case UseDefStyle:
00969       return BoxSize + m;
00970     case Color:
00971     case SelColor:
00972     case BgColor:
00973     case SelBgColor:
00974       return ColorBtnWidth +m;
00975     default:
00976       return 0;
00977   }
00978 }
00979 
00980 void KateStyleListItem::activate( int column, const QPoint &localPos )
00981 {
00982   QListView *lv = listView();
00983   int x = 0;
00984   for( int c = 0; c < column-1; c++ )
00985     x += lv->columnWidth( c );
00986   int w;
00987   switch( column ) {
00988     case Bold:
00989     case Italic:
00990     case Underline:
00991     case Strikeout:
00992     case UseDefStyle:
00993       w = BoxSize;
00994       break;
00995     case Color:
00996     case SelColor:
00997     case BgColor:
00998     case SelBgColor:
00999       w = ColorBtnWidth;
01000       break;
01001     default:
01002       return;
01003   }
01004   if ( !QRect( x, 0, w, BoxSize ).contains( localPos ) )
01005   changeProperty( (Property)column );
01006 }
01007 
01008 void KateStyleListItem::changeProperty( Property p )
01009 {
01010   if ( p == Bold )
01011     is->setBold( ! is->bold() );
01012   else if ( p == Italic )
01013     is->setItalic( ! is->italic() );
01014   else if ( p == Underline )
01015     is->setUnderline( ! is->underline() );
01016   else if ( p == Strikeout )
01017     is->setStrikeOut( ! is->strikeOut() );
01018   else if ( p == UseDefStyle )
01019     toggleDefStyle();
01020   else
01021     setColor( p );
01022 
01023   updateStyle ();
01024 
01025   ((KateStyleListView*)listView())->emitChanged();
01026 }
01027 
01028 void KateStyleListItem::toggleDefStyle()
01029 {
01030   if ( *is == *ds ) {
01031     KMessageBox::information( listView(),
01032          i18n("\"Use Default Style\" will be automatically unset when you change any style properties."),
01033          i18n("Kate Styles"),
01034          "Kate hl config use defaults" );
01035   }
01036   else {
01037     delete is;
01038     is = new KateAttribute( *ds );
01039     repaint();
01040   }
01041 }
01042 
01043 void KateStyleListItem::setColor( int column )
01044 {
01045   QColor c;
01046   if ( column == Color) c = is->textColor();
01047   else if ( column == SelColor ) c = is->selectedTextColor();
01048   else if ( column == BgColor ) c = is->bgColor();
01049   else if ( column == SelBgColor ) c = is->selectedBGColor();
01050 
01051   if ( KColorDialog::getColor( c, listView() ) != QDialog::Accepted) return;
01052 
01053   if (st && st->isSomethingSet()) setCustStyle();
01054 
01055   if ( column == Color) is->setTextColor( c );
01056   else if ( column == SelColor ) is->setSelectedTextColor( c );
01057   else if ( column == BgColor ) is->setBGColor( c );
01058   else if ( column == SelBgColor ) is->setSelectedBGColor( c );
01059 
01060   repaint();
01061 }
01062 
01063 void KateStyleListItem::setCustStyle()
01064 {
01065 
01066 
01067 
01068 }
01069 
01070 void KateStyleListItem::paintCell( QPainter *p, const QColorGroup& cg, int col, int width, int align )
01071 {
01072 
01073   if ( !p )
01074     return;
01075 
01076   QListView *lv = listView();
01077   if ( !lv )
01078     return;
01079   Q_ASSERT( lv ); 
01080 
01081   p->fillRect( 0, 0, width, height(), QBrush( ((KateStyleListView*)lv)->bgcol ) );
01082   int marg = lv->itemMargin();
01083 
01084   
01085   QColorGroup mcg = cg;
01086   QColor c;
01087 
01088   switch ( col )
01089   {
01090     case ContextName:
01091     {
01092       mcg.setColor(QColorGroup::Text, is->textColor());
01093       mcg.setColor(QColorGroup::HighlightedText, is->selectedTextColor());
01094       QFont f ( ((KateStyleListView*)lv)->docfont );
01095       p->setFont( is->font(f) );
01096       
01097       
01098       QListViewItem::paintCell( p, mcg, col, width, align );
01099     }
01100     break;
01101     case Bold:
01102     case Italic:
01103     case Underline:
01104     case Strikeout:
01105     case UseDefStyle:
01106     {
01107       
01108       
01109       
01110       mcg.setColor( QColorGroup::Text, ((KateStyleListView*)lv)->normalcol );
01111       int x = 0;
01112       if ( align == AlignCenter ) {
01113         QFontMetrics fm( lv->font() );
01114         x = (width - BoxSize - fm.width(text(0)))/2;
01115       }
01116       int y = (height() - BoxSize) / 2;
01117 
01118       if ( isEnabled() )
01119         p->setPen( QPen( mcg.text(), 2 ) );
01120       else
01121         p->setPen( QPen( lv->palette().color( QPalette::Disabled, QColorGroup::Text ), 2 ) );
01122 
01123       if ( isSelected() && lv->header()->mapToSection( 0 ) != 0 )
01124       {
01125         p->fillRect( 0, 0, x + marg + BoxSize + 4, height(),
01126               mcg.brush( QColorGroup::Highlight ) );
01127         if ( isEnabled() )
01128           p->setPen( QPen( mcg.highlightedText(), 2 ) ); 
01129       }
01130       p->drawRect( x+marg, y+2, BoxSize-4, BoxSize-4 );
01131       x++;
01132       y++;
01133       if ( (col == Bold && is->bold()) ||
01134           (col == Italic && is->italic()) ||
01135           (col == Underline && is->underline()) ||
01136           (col == Strikeout && is->strikeOut()) ||
01137           (col == UseDefStyle && *is == *ds ) )
01138       {
01139         QPointArray a( 7*2 );
01140         int i, xx, yy;
01141         xx = x+1+marg;
01142         yy = y+5;
01143         for ( i=0; i<3; i++ ) {
01144           a.setPoint( 2*i,   xx, yy );
01145           a.setPoint( 2*i+1, xx, yy+2 );
01146           xx++; yy++;
01147         }
01148         yy -= 2;
01149         for ( i=3; i<7; i++ ) {
01150           a.setPoint( 2*i,   xx, yy );
01151           a.setPoint( 2*i+1, xx, yy+2 );
01152           xx++; yy--;
01153         }
01154         p->drawLineSegments( a );
01155       }
01156     }
01157     break;
01158     case Color:
01159     case SelColor:
01160     case BgColor:
01161     case SelBgColor:
01162     {
01163       if ( col == Color) c = is->textColor();
01164       else if ( col == SelColor ) c = is->selectedTextColor();
01165       else if ( col == BgColor ) c = is->itemSet(KateAttribute::BGColor) ? is->bgColor() : ((KateStyleListView*)lv)->bgcol;
01166       else if ( col == SelBgColor ) c = is->itemSet(KateAttribute::SelectedBGColor) ? is->selectedBGColor(): ((KateStyleListView*)lv)->bgcol;
01167       
01168       mcg.setColor( QColorGroup::Text, ((KateStyleListView*)lv)->normalcol );
01169       int x = 0;
01170       int y = (height() - BoxSize) / 2;
01171       if ( isEnabled() )
01172         p->setPen( QPen( mcg.text(), 2 ) );
01173       else
01174         p->setPen( QPen( lv->palette().color( QPalette::Disabled, QColorGroup::Text ), 2 ) );
01175 
01176       p->drawRect( x+marg, y+2, ColorBtnWidth-4, BoxSize-4 );
01177       p->fillRect( x+marg+1,y+3,ColorBtnWidth-7,BoxSize-7,QBrush( c ) );
01178     }
01179     
01180   }
01181 }
01182 
01183 
01184