Kate
kateschema.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2007, 2008 Matthew Woehlke <mw_triad@users.sourceforge.net> 00003 Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org> 00004 Copyright (C) 2002, 2003 Anders Lund <anders.lund@lund.tdcadsl.dk> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 //BEGIN Includes 00022 #include "kateschema.h" 00023 #include "kateschema.moc" 00024 00025 #include "kateconfig.h" 00026 #include "katedocument.h" 00027 #include "kateglobal.h" 00028 #include "kateview.h" 00029 #include "katerenderer.h" 00030 #include "kateextendedattribute.h" 00031 #include "katestyletreewidget.h" 00032 00033 #include "ui_schemaconfigcolortab.h" 00034 00035 #include <kcolorscheme.h> 00036 #include <kcolorutils.h> 00037 #include <klocale.h> 00038 #include <kdialog.h> 00039 #include <kcolorbutton.h> 00040 #include <kcombobox.h> 00041 #include <kinputdialog.h> 00042 #include <kfontdialog.h> 00043 #include <kdebug.h> 00044 #include <kiconloader.h> 00045 #include <kmessagebox.h> 00046 #include <kmenu.h> 00047 #include <kcolordialog.h> 00048 #include <kapplication.h> 00049 #include <kaboutdata.h> 00050 #include <ktexteditor/markinterface.h> 00051 #include <khbox.h> 00052 #include <ktabwidget.h> 00053 00054 #include <QtGui/QCheckBox> 00055 #include <QtGui/QDialog> 00056 #include <QtGui/QLabel> 00057 #include <QtCore/QTextCodec> 00058 #include <QtGui/QLayout> 00059 #include <QtGui/QPainter> 00060 #include <QtCore/QObject> 00061 #include <QtGui/QPixmap> 00062 #include <QtGui/QPushButton> 00063 #include <QtGui/QRadioButton> 00064 #include <QtGui/QSpinBox> 00065 #include <QtCore/QStringList> 00066 #include <QtGui/QPolygon> 00067 #include <QtGui/QGroupBox> 00068 #include <QtGui/QTreeWidget> 00069 //END 00070 00071 00072 //BEGIN KateSchemaManager 00073 QString KateSchemaManager::normalSchema () 00074 { 00075 return KGlobal::mainComponent().aboutData()->appName () + QString (" - Normal"); 00076 } 00077 00078 QString KateSchemaManager::printingSchema () 00079 { 00080 return KGlobal::mainComponent().aboutData()->appName () + QString (" - Printing"); 00081 } 00082 00083 KateSchemaManager::KateSchemaManager () 00084 : m_config ("kateschemarc", KConfig::NoGlobals) 00085 { 00086 update (); 00087 } 00088 00089 KateSchemaManager::~KateSchemaManager () 00090 { 00091 } 00092 00093 // 00094 // read the types from config file and update the internal list 00095 // 00096 void KateSchemaManager::update (bool readfromfile) 00097 { 00098 if (readfromfile) 00099 m_config.reparseConfiguration (); 00100 00101 m_schemas = m_config.groupList(); 00102 m_schemas.sort (); 00103 00104 m_schemas.removeAll (printingSchema()); 00105 m_schemas.removeAll (normalSchema()); 00106 m_schemas.prepend (printingSchema()); 00107 m_schemas.prepend (normalSchema()); 00108 } 00109 00110 // 00111 // get the right group 00112 // special handling of the default schemas ;) 00113 // 00114 KConfigGroup KateSchemaManager::schema (uint number) 00115 { 00116 if ((number>1) && (number < (uint)m_schemas.count())) 00117 return m_config.group (m_schemas[number]); 00118 else if (number == 1) 00119 return m_config.group (printingSchema()); 00120 else 00121 return m_config.group (normalSchema()); 00122 } 00123 00124 void KateSchemaManager::addSchema (const QString &t) 00125 { 00126 m_config.group(t).writeEntry("Color Background", KColorScheme(QPalette::Active, KColorScheme::View).background().color()); 00127 00128 update (false); 00129 } 00130 00131 void KateSchemaManager::removeSchema (uint number) 00132 { 00133 if (number >= (uint)m_schemas.count()) 00134 return; 00135 00136 if (number < 2) 00137 return; 00138 00139 m_config.deleteGroup (name (number)); 00140 00141 update (false); 00142 } 00143 00144 bool KateSchemaManager::validSchema (uint number) 00145 { 00146 if (number < (uint)m_schemas.count()) 00147 return true; 00148 00149 return false; 00150 } 00151 00152 bool KateSchemaManager::validSchema (const QString &name) 00153 { 00154 if (name == normalSchema() || name == printingSchema()) 00155 return true; 00156 00157 for (int i = 0; i < m_schemas.size(); ++i) 00158 if (m_schemas[i] == name) 00159 return true; 00160 00161 return false; 00162 } 00163 00164 uint KateSchemaManager::number (const QString &name) 00165 { 00166 if (name == normalSchema()) 00167 return 0; 00168 00169 if (name == printingSchema()) 00170 return 1; 00171 00172 int i; 00173 if ((i = m_schemas.indexOf(name)) > -1) 00174 return i; 00175 00176 return 0; 00177 } 00178 00179 QString KateSchemaManager::name (uint number) 00180 { 00181 if ((number>1) && (number < (uint)m_schemas.count())) 00182 return m_schemas[number]; 00183 else if (number == 1) 00184 return printingSchema(); 00185 00186 return normalSchema(); 00187 } 00188 //END 00189 00190 // 00191 // DIALOGS !!! 00192 // 00193 00194 //BEGIN KateSchemaConfigColorTab -- 'Colors' tab 00195 KateSchemaConfigColorTab::KateSchemaConfigColorTab() 00196 : ui(new Ui::SchemaConfigColorTab()) 00197 { 00198 m_schema = -1; 00199 00200 ui->setupUi(this); 00201 00202 // Markers from kdelibs/interfaces/ktextinterface/markinterface.h 00203 // add the predefined mark types as defined in markinterface.h 00204 ui->combobox->addItem(i18n("Bookmark")); // markType01 00205 ui->combobox->addItem(i18n("Active Breakpoint")); // markType02 00206 ui->combobox->addItem(i18n("Reached Breakpoint")); // markType03 00207 ui->combobox->addItem(i18n("Disabled Breakpoint")); // markType04 00208 ui->combobox->addItem(i18n("Execution")); // markType05 00209 ui->combobox->addItem(i18n("Warning")); // markType06 00210 ui->combobox->addItem(i18n("Error")); // markType07 00211 ui->combobox->addItem(i18n("Template Background")); 00212 ui->combobox->addItem(i18n("Template Editable Placeholder")); 00213 ui->combobox->addItem(i18n("Template Focused Editable Placeholder")); 00214 ui->combobox->addItem(i18n("Template Not Editable Placeholder")); 00215 ui->combobox->setCurrentIndex(0); 00216 00217 connect( ui->combobox , SIGNAL( activated( int ) ) , SLOT( slotComboBoxChanged( int ) ) ); 00218 connect( ui->back , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00219 connect( ui->selected , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00220 connect( ui->current , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00221 connect( ui->bracket , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00222 connect( ui->wwmarker , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00223 connect( ui->iconborder, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00224 connect( ui->tmarker , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00225 connect( ui->linenumber, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00226 connect( ui->markers , SIGNAL( changed( const QColor& ) ), SLOT( slotMarkerColorChanged( const QColor& ) ) ); 00227 connect( ui->spellingmistakeline, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00228 } 00229 00230 KateSchemaConfigColorTab::~KateSchemaConfigColorTab() 00231 { 00232 delete ui; 00233 } 00234 00235 void KateSchemaConfigColorTab::schemaChanged ( int newSchema ) 00236 { 00237 // save curent schema 00238 if ( m_schema > -1 ) 00239 { 00240 m_schemas[ m_schema ].back = ui->back->color(); 00241 m_schemas[ m_schema ].selected = ui->selected->color(); 00242 m_schemas[ m_schema ].current = ui->current->color(); 00243 m_schemas[ m_schema ].bracket = ui->bracket->color(); 00244 m_schemas[ m_schema ].wwmarker = ui->wwmarker->color(); 00245 m_schemas[ m_schema ].iconborder = ui->iconborder->color(); 00246 m_schemas[ m_schema ].tmarker = ui->tmarker->color(); 00247 m_schemas[ m_schema ].linenumber = ui->linenumber->color(); 00248 m_schemas[ m_schema ].spellingmistakeline = ui->spellingmistakeline->color(); 00249 } 00250 00251 if ( newSchema == m_schema ) return; 00252 00253 // switch 00254 m_schema = newSchema; 00255 00256 // first block signals otherwise setColor emits changed 00257 bool blocked = blockSignals(true); 00258 00259 // If we havent this schema, read in from config file 00260 if ( ! m_schemas.contains( newSchema ) ) 00261 { 00262 // fallback defaults 00263 // NOTE keep in sync with KateRendererConfig::setSchemaInternal 00264 KColorScheme schemeView(QPalette::Active, KColorScheme::View); 00265 KColorScheme schemeWindow(QPalette::Active, KColorScheme::Window); 00266 KColorScheme schemeSelection(QPalette::Active, KColorScheme::Selection); 00267 QColor tmp0( schemeView.background().color() ); 00268 QColor tmp1( schemeSelection.background().color() ); 00269 QColor tmp2( schemeView.background(KColorScheme::AlternateBackground).color() ); 00270 // using KColorUtils::shade wasn't working really well 00271 qreal bgLuma = KColorUtils::luma( tmp0 ); 00272 QColor tmp3( KColorUtils::tint(tmp0, schemeView.decoration(KColorScheme::HoverColor).color()) ); 00273 QColor tmp4( KColorUtils::shade( tmp0, bgLuma > 0.3 ? -0.15 : 0.03 ) ); 00274 QColor tmp5( KColorUtils::shade( tmp0, bgLuma > 0.7 ? -0.35 : 0.3 ) ); 00275 QColor tmp6( schemeWindow.background().color() ); 00276 QColor tmp7( schemeWindow.foreground().color() ); 00277 QColor tmp8( Qt::red ); 00278 00279 // same std colors like in KateDocument::markColor 00280 QVector <QColor> mark(KTextEditor::MarkInterface::reservedMarkersCount()); 00281 Q_ASSERT(mark.size() > 6); 00282 mark[0] = Qt::blue; 00283 mark[1] = Qt::red; 00284 mark[2] = Qt::yellow; 00285 mark[3] = Qt::magenta; 00286 mark[4] = Qt::gray; 00287 mark[5] = Qt::green; 00288 mark[6] = Qt::red; 00289 00290 SchemaColors c; 00291 KConfigGroup config = KateGlobal::self()->schemaManager()->schema(newSchema); 00292 00293 c.back= config.readEntry("Color Background", tmp0); 00294 c.selected = config.readEntry("Color Selection", tmp1); 00295 c.current = config.readEntry("Color Highlighted Line", tmp2); 00296 c.bracket = config.readEntry("Color Highlighted Bracket", tmp3); 00297 c.wwmarker = config.readEntry("Color Word Wrap Marker", tmp4); 00298 c.tmarker = config.readEntry("Color Tab Marker", tmp5); 00299 c.iconborder = config.readEntry("Color Icon Bar", tmp6); 00300 c.linenumber = config.readEntry("Color Line Number", tmp7); 00301 c.spellingmistakeline = config.readEntry("Color Spelling Mistake Line", tmp8); 00302 00303 for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++) 00304 c.markerColors[i] = config.readEntry( QString("Color MarkType%1").arg(i+1), mark[i] ); 00305 00306 c.templateColors[0] = config.readEntry(QString("Color Template Background"),QColor(0xcc,0xcc,0xcc)); 00307 c.templateColors[1] = config.readEntry(QString("Color Template Editable Placeholder"),QColor(0xcc,0xff,0xcc)); 00308 c.templateColors[2] = config.readEntry(QString("Color Template Focused Editable Placeholder"),QColor(0x66,0xff,0x66)); 00309 c.templateColors[3] = config.readEntry(QString("Color Template Not Editable Placeholder"),QColor(0xff,0xcc,0xcc)); 00310 00311 m_schemas[ newSchema ] = c; 00312 } 00313 00314 ui->back->setColor( m_schemas[ newSchema ].back); 00315 ui->selected->setColor( m_schemas [ newSchema ].selected ); 00316 ui->current->setColor( m_schemas [ newSchema ].current ); 00317 ui->bracket->setColor( m_schemas [ newSchema ].bracket ); 00318 ui->wwmarker->setColor( m_schemas [ newSchema ].wwmarker ); 00319 ui->tmarker->setColor( m_schemas [ newSchema ].tmarker ); 00320 ui->iconborder->setColor( m_schemas [ newSchema ].iconborder ); 00321 ui->linenumber->setColor( m_schemas [ newSchema ].linenumber ); 00322 ui->spellingmistakeline->setColor( m_schemas [ newSchema ].spellingmistakeline ); 00323 00324 // map from 0..reservedMarkersCount()-1 - the same index as in markInterface 00325 for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++) 00326 { 00327 QPixmap pix(16, 16); 00328 pix.fill( m_schemas [ newSchema ].markerColors[i]); 00329 ui->combobox->setItemIcon(i, QIcon(pix)); 00330 } 00331 for (int i = 0; i < 4; i++) 00332 { 00333 QPixmap pix(16, 16); 00334 pix.fill( m_schemas [ newSchema ].templateColors[i]); 00335 ui->combobox->setItemIcon(i+KTextEditor::MarkInterface::reservedMarkersCount(), QIcon(pix)); 00336 } 00337 00338 ui->markers->setColor( m_schemas [ newSchema ].markerColors[ ui->combobox->currentIndex() ] ); 00339 00340 blockSignals(blocked); 00341 } 00342 00343 void KateSchemaConfigColorTab::apply () 00344 { 00345 schemaChanged( m_schema ); 00346 QMap<int,SchemaColors>::Iterator it; 00347 for ( it = m_schemas.begin(); it != m_schemas.end(); ++it ) 00348 { 00349 kDebug(13030)<<"APPLY scheme = "<<it.key(); 00350 KConfigGroup config = KateGlobal::self()->schemaManager()->schema( it.key() ); 00351 kDebug(13030)<<"Using config group "<<config.name(); 00352 SchemaColors c = it.value(); 00353 00354 // TODO - don't save if using defaults, so that changing the color scheme 00355 // lets colors track the new scheme if they haven't been customized 00356 // Although, KColorScheme should handle this eventually... 00357 config.writeEntry("Color Background", c.back); 00358 config.writeEntry("Color Selection", c.selected); 00359 config.writeEntry("Color Highlighted Line", c.current); 00360 config.writeEntry("Color Highlighted Bracket", c.bracket); 00361 config.writeEntry("Color Word Wrap Marker", c.wwmarker); 00362 config.writeEntry("Color Tab Marker", c.tmarker); 00363 config.writeEntry("Color Icon Bar", c.iconborder); 00364 config.writeEntry("Color Line Number", c.linenumber); 00365 config.writeEntry("Color Spelling Mistake Line", c.spellingmistakeline); 00366 00367 for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++) 00368 { 00369 config.writeEntry(QString("Color MarkType%1").arg(i + 1), c.markerColors[i]); 00370 } 00371 00372 config.writeEntry(QString("Color Template Background"),c.templateColors[0]); 00373 config.writeEntry(QString("Color Template Editable Placeholder"),c.templateColors[1]); 00374 config.writeEntry(QString("Color Template Focused Editable Placeholder"),c.templateColors[2]); 00375 config.writeEntry(QString("Color Template Not Editable Placeholder"),c.templateColors[3]); 00376 00377 } 00378 } 00379 00380 void KateSchemaConfigColorTab::slotMarkerColorChanged( const QColor& color) 00381 { 00382 int index = ui->combobox->currentIndex(); 00383 if (index<7) 00384 m_schemas[ m_schema ].markerColors[ index ] = color; 00385 else 00386 m_schemas[m_schema].templateColors[index-7] = color; 00387 QPixmap pix(16, 16); 00388 pix.fill(color); 00389 ui->combobox->setItemIcon(index, QIcon(pix)); 00390 00391 emit changed(); 00392 } 00393 00394 void KateSchemaConfigColorTab::slotComboBoxChanged(int index) 00395 { 00396 // temporarily block signals because setColor emits changed as well 00397 bool blocked = ui->markers->blockSignals(true); 00398 if (index<7) 00399 ui->markers->setColor( m_schemas[m_schema].markerColors[index] ); 00400 else 00401 ui->markers->setColor( m_schemas[m_schema].templateColors[index-7] ); 00402 ui->markers->blockSignals(blocked); 00403 } 00404 00405 //END KateSchemaConfigColorTab 00406 00407 //BEGIN FontConfig -- 'Fonts' tab 00408 KateSchemaConfigFontTab::KateSchemaConfigFontTab() 00409 { 00410 // sizemanagment 00411 QGridLayout *grid = new QGridLayout( this ); 00412 00413 m_fontchooser = new KFontChooser ( this, KFontChooser::NoDisplayFlags ); 00414 grid->addWidget( m_fontchooser, 0, 0); 00415 00416 m_schema = -1; 00417 } 00418 00419 KateSchemaConfigFontTab::~KateSchemaConfigFontTab() 00420 { 00421 } 00422 00423 void KateSchemaConfigFontTab::slotFontSelected( const QFont &font ) 00424 { 00425 if ( m_schema > -1 ) 00426 { 00427 m_fonts[m_schema] = font; 00428 emit changed(); 00429 } 00430 } 00431 00432 void KateSchemaConfigFontTab::apply() 00433 { 00434 FontMap::Iterator it; 00435 for ( it = m_fonts.begin(); it != m_fonts.end(); ++it ) 00436 { 00437 KateGlobal::self()->schemaManager()->schema( it.key() ).writeEntry( "Font", it.value() ); 00438 } 00439 } 00440 00441 void KateSchemaConfigFontTab::schemaChanged( int newSchema ) 00442 { 00443 if ( m_schema > -1 ) 00444 m_fonts[ m_schema ] = m_fontchooser->font(); 00445 00446 m_schema = newSchema; 00447 00448 QFont f (KGlobalSettings::fixedFont()); 00449 00450 m_fontchooser->disconnect ( this ); 00451 m_fontchooser->setFont ( KateGlobal::self()->schemaManager()->schema( newSchema ).readEntry("Font", f) ); 00452 m_fonts[ newSchema ] = m_fontchooser->font(); 00453 connect (m_fontchooser, SIGNAL (fontSelected( const QFont & )), this, SLOT (slotFontSelected( const QFont & ))); 00454 } 00455 //END FontConfig 00456 00457 //BEGIN FontColorConfig -- 'Normal Text Styles' tab 00458 KateSchemaConfigFontColorTab::KateSchemaConfigFontColorTab() 00459 { 00460 // sizemanagment 00461 QGridLayout *grid = new QGridLayout( this ); 00462 00463 m_defaultStyles = new KateStyleTreeWidget( this ); 00464 m_defaultStyles->setRootIsDecorated(false); 00465 connect(m_defaultStyles, SIGNAL(changed()), this, SIGNAL(changed())); 00466 grid->addWidget( m_defaultStyles, 0, 0); 00467 00468 m_defaultStyles->setWhatsThis(i18n( 00469 "<p>This list displays the default styles for the current schema and " 00470 "offers the means to edit them. The style name reflects the current " 00471 "style settings.</p>" 00472 "<p>To edit the colors, click the colored squares, or select the color " 00473 "to edit from the popup menu.</p><p>You can unset the Background and Selected " 00474 "Background colors from the popup menu when appropriate.</p>") ); 00475 } 00476 00477 KateSchemaConfigFontColorTab::~KateSchemaConfigFontColorTab() 00478 { 00479 qDeleteAll(m_defaultStyleLists); 00480 } 00481 00482 KateAttributeList *KateSchemaConfigFontColorTab::attributeList (uint schema) 00483 { 00484 if (!m_defaultStyleLists.contains(schema)) 00485 { 00486 KateAttributeList *list = new KateAttributeList (); 00487 KateHlManager::self()->getDefaults(KateGlobal::self()->schemaManager()->name (schema), *list); 00488 00489 m_defaultStyleLists.insert (schema, list); 00490 } 00491 00492 return m_defaultStyleLists[schema]; 00493 } 00494 00495 void KateSchemaConfigFontColorTab::schemaChanged (uint schema) 00496 { 00497 m_defaultStyles->clear (); 00498 00499 KateAttributeList *l = attributeList (schema); 00500 00501 // set colors 00502 QPalette p ( m_defaultStyles->palette() ); 00503 KColorScheme s ( QPalette::Active, KColorScheme::View ); 00504 QColor _c ( s.background().color() ); 00505 p.setColor( QPalette::Base, 00506 KateGlobal::self()->schemaManager()->schema(schema). 00507 readEntry( "Color Background", _c ) ); 00508 _c = KColorScheme(QPalette::Active, KColorScheme::Selection).background().color(); 00509 p.setColor( QPalette::Highlight, 00510 KateGlobal::self()->schemaManager()->schema(schema). 00511 readEntry( "Color Selection", _c ) ); 00512 _c = l->at(0)->foreground().color(); // not quite as much of an assumption ;) 00513 p.setColor( QPalette::Text, _c ); 00514 m_defaultStyles->viewport()->setPalette( p ); 00515 00516 for ( uint i = 0; i < KateHlManager::self()->defaultStyles(); i++ ) 00517 { 00518 m_defaultStyles->addItem( KateHlManager::self()->defaultStyleName(i, true), l->at( i ) ); 00519 } 00520 } 00521 00522 void KateSchemaConfigFontColorTab::reload () 00523 { 00524 m_defaultStyles->clear (); 00525 qDeleteAll(m_defaultStyleLists); 00526 m_defaultStyleLists.clear (); 00527 } 00528 00529 void KateSchemaConfigFontColorTab::apply () 00530 { 00531 QHashIterator<int,KateAttributeList*> it = m_defaultStyleLists; 00532 while (it.hasNext()) { 00533 it.next(); 00534 KateHlManager::self()->setDefaults(KateGlobal::self()->schemaManager()->name (it.key()), *it.value()); 00535 } 00536 } 00537 00538 //END FontColorConfig 00539 00540 //BEGIN KateSchemaConfigHighlightTab -- 'Highlighting Text Styles' tab 00541 KateSchemaConfigHighlightTab::KateSchemaConfigHighlightTab(KateSchemaConfigFontColorTab *page) 00542 { 00543 m_defaults = page; 00544 00545 m_schema = 0; 00546 m_hl = 0; 00547 00548 QVBoxLayout *layout = new QVBoxLayout(this); 00549 00550 // hl chooser 00551 KHBox *hbHl = new KHBox( this ); 00552 layout->addWidget (hbHl); 00553 00554 hbHl->setSpacing( -1 ); 00555 QLabel *lHl = new QLabel( i18n("H&ighlight:"), hbHl ); 00556 hlCombo = new KComboBox( hbHl ); 00557 hlCombo->setEditable( false ); 00558 lHl->setBuddy( hlCombo ); 00559 connect( hlCombo, SIGNAL(activated(int)), 00560 this, SLOT(hlChanged(int)) ); 00561 00562 for( int i = 0; i < KateHlManager::self()->highlights(); i++) { 00563 if (KateHlManager::self()->hlSection(i).length() > 0) 00564 hlCombo->addItem(KateHlManager::self()->hlSection(i) + QString ("/") + KateHlManager::self()->hlNameTranslated(i)); 00565 else 00566 hlCombo->addItem(KateHlManager::self()->hlNameTranslated(i)); 00567 } 00568 hlCombo->setCurrentIndex(0); 00569 00570 // styles listview 00571 m_styles = new KateStyleTreeWidget( this, true ); 00572 connect(m_styles, SIGNAL(changed()), this, SIGNAL(changed())); 00573 layout->addWidget (m_styles, 999); 00574 00575 // get current highlighting from the host application 00576 int hl = 0; 00577 KTextEditor::MdiContainer *iface = qobject_cast<KTextEditor::MdiContainer*>(KateGlobal::self()->container()); 00578 if (iface) { 00579 KateView *kv = qobject_cast<KateView*>(iface->activeView()); 00580 if (kv) { 00581 const QString hlName = kv->doc()->highlight()->name(); 00582 hl = KateHlManager::self()->nameFind(hlName); 00583 } 00584 } 00585 hlCombo->setCurrentIndex ( hl ); 00586 hlChanged ( hl ); 00587 00588 m_styles->setWhatsThis(i18n( 00589 "<p>This list displays the contexts of the current syntax highlight mode and " 00590 "offers the means to edit them. The context name reflects the current " 00591 "style settings.</p><p>To edit using the keyboard, press " 00592 "<strong><SPACE></strong> and choose a property from the popup menu.</p>" 00593 "<p>To edit the colors, click the colored squares, or select the color " 00594 "to edit from the popup menu.</p><p>You can unset the Background and Selected " 00595 "Background colors from the context menu when appropriate.</p>") ); 00596 } 00597 00598 KateSchemaConfigHighlightTab::~KateSchemaConfigHighlightTab() 00599 { 00600 } 00601 00602 void KateSchemaConfigHighlightTab::hlChanged(int z) 00603 { 00604 m_hl = z; 00605 00606 schemaChanged (m_schema); 00607 } 00608 00609 void KateSchemaConfigHighlightTab::schemaChanged (int schema) 00610 { 00611 m_schema = schema; 00612 00613 kDebug(13030) << "NEW SCHEMA: " << m_schema << " NEW HL: " << m_hl; 00614 00615 m_styles->clear (); 00616 00617 if (!m_hlDict.contains(m_schema)) 00618 { 00619 kDebug(13030) << "NEW SCHEMA, create dict"; 00620 00621 m_hlDict.insert (schema, QHash<int, QList<KateExtendedAttribute::Ptr> >()); 00622 } 00623 00624 if (!m_hlDict[m_schema].contains(m_hl)) 00625 { 00626 kDebug(13030) << "NEW HL, create list"; 00627 00628 QList<KateExtendedAttribute::Ptr> list; 00629 KateHlManager::self()->getHl( m_hl )->getKateExtendedAttributeListCopy(KateGlobal::self()->schemaManager()->name (m_schema), list); 00630 m_hlDict[m_schema].insert (m_hl, list); 00631 } 00632 00633 KateAttributeList *l = m_defaults->attributeList (schema); 00634 00635 // Set listview colors 00636 // We do that now, because we can now get the "normal text" color. 00637 // TODO this reads of the KConfig object, which should be changed when 00638 // the color tab is fixed. 00639 QPalette p ( m_styles->palette() ); 00640 KColorScheme s ( QPalette::Active, KColorScheme::View ); 00641 QColor _c ( s.background().color() ); 00642 p.setColor( QPalette::Base, 00643 KateGlobal::self()->schemaManager()->schema(m_schema). 00644 readEntry( "Color Background", _c ) ); 00645 _c = KColorScheme(QPalette::Active, KColorScheme::Selection).background().color(); 00646 p.setColor( QPalette::Highlight, 00647 KateGlobal::self()->schemaManager()->schema(m_schema). 00648 readEntry( "Color Selection", _c ) ); 00649 _c = l->at(0)->foreground().color(); // not quite as much of an assumption ;) 00650 p.setColor( QPalette::Text, _c ); 00651 m_styles->viewport()->setPalette( p ); 00652 00653 QHash<QString, QTreeWidgetItem*> prefixes; 00654 QList<KateExtendedAttribute::Ptr>::ConstIterator it = m_hlDict[m_schema][m_hl].constBegin(); 00655 while (it != m_hlDict[m_schema][m_hl].constEnd()) 00656 { 00657 const KateExtendedAttribute::Ptr itemData = *it; 00658 Q_ASSERT(itemData); 00659 00660 kDebug(13030) << "insert items " << itemData->name(); 00661 00662 // All stylenames have their language mode prefixed, e.g. HTML:Comment 00663 // split them and put them into nice substructures. 00664 int c = itemData->name().indexOf(':'); 00665 if ( c > 0 ) { 00666 QString prefix = itemData->name().left(c); 00667 QString name = itemData->name().mid(c+1); 00668 00669 QTreeWidgetItem *parent = prefixes[prefix]; 00670 if ( ! parent ) 00671 { 00672 parent = new QTreeWidgetItem( m_styles, QStringList() << prefix ); 00673 m_styles->expandItem(parent); 00674 prefixes.insert( prefix, parent ); 00675 } 00676 m_styles->addItem( parent, name, l->at(itemData->defaultStyleIndex()), itemData ); 00677 } else { 00678 m_styles->addItem( itemData->name(), l->at(itemData->defaultStyleIndex()), itemData ); 00679 } 00680 ++it; 00681 } 00682 00683 m_styles->resizeColumns(); 00684 } 00685 00686 void KateSchemaConfigHighlightTab::reload () 00687 { 00688 m_styles->clear (); 00689 00690 m_hlDict.clear (); 00691 00692 hlChanged (0); 00693 } 00694 00695 void KateSchemaConfigHighlightTab::apply () 00696 { 00697 QMutableHashIterator<int, QHash<int, QList<KateExtendedAttribute::Ptr> > > it = m_hlDict; 00698 while (it.hasNext()) { 00699 it.next(); 00700 QMutableHashIterator<int, QList<KateExtendedAttribute::Ptr> > it2 = it.value(); 00701 while (it2.hasNext()) { 00702 it2.next(); 00703 KateHlManager::self()->getHl( it2.key() )->setKateExtendedAttributeList (it.key(), it2.value()); 00704 } 00705 } 00706 } 00707 00708 //END KateSchemaConfigHighlightTab 00709 00710 //BEGIN KateSchemaConfigPage -- Main dialog page 00711 KateSchemaConfigPage::KateSchemaConfigPage( QWidget *parent) 00712 : KateConfigPage( parent ), 00713 m_lastSchema (-1) 00714 { 00715 QVBoxLayout *layout = new QVBoxLayout(this); 00716 layout->setMargin(0); 00717 00718 KHBox *hbHl = new KHBox( this ); 00719 layout->addWidget(hbHl); 00720 hbHl->setSpacing( -1 ); 00721 QLabel *lHl = new QLabel( i18n("&Schema:"), hbHl ); 00722 schemaCombo = new KComboBox( hbHl ); 00723 schemaCombo->setEditable( false ); 00724 lHl->setBuddy( schemaCombo ); 00725 connect( schemaCombo, SIGNAL(activated(int)), 00726 this, SLOT(schemaChanged(int)) ); 00727 00728 QPushButton *btnnew = new QPushButton( i18n("&New..."), hbHl ); 00729 connect( btnnew, SIGNAL(clicked()), this, SLOT(newSchema()) ); 00730 00731 btndel = new QPushButton( i18n("&Delete"), hbHl ); 00732 connect( btndel, SIGNAL(clicked()), this, SLOT(deleteSchema()) ); 00733 00734 qobject_cast<QBoxLayout *>(hbHl->layout())->addStretch(); 00735 00736 m_tabWidget = new KTabWidget ( this ); 00737 layout->addWidget (m_tabWidget); 00738 00739 m_colorTab = new KateSchemaConfigColorTab(); 00740 m_tabWidget->addTab (m_colorTab, i18n("Colors")); 00741 connect(m_colorTab, SIGNAL(changed()), SLOT(slotChanged())); 00742 00743 m_fontTab = new KateSchemaConfigFontTab(); 00744 m_tabWidget->addTab (m_fontTab, i18n("Font")); 00745 connect(m_fontTab, SIGNAL(changed()), SLOT(slotChanged())); 00746 00747 m_fontColorTab = new KateSchemaConfigFontColorTab(); 00748 m_tabWidget->addTab (m_fontColorTab, i18n("Normal Text Styles")); 00749 connect(m_fontColorTab, SIGNAL(changed()), SLOT(slotChanged())); 00750 00751 m_highlightTab = new KateSchemaConfigHighlightTab(m_fontColorTab); 00752 m_tabWidget->addTab(m_highlightTab, i18n("Highlighting Text Styles")); 00753 connect(m_highlightTab, SIGNAL(changed()), SLOT(slotChanged())); 00754 00755 connect (m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT (newCurrentPage(int))); 00756 00757 hbHl = new KHBox( this ); 00758 layout->addWidget (hbHl); 00759 hbHl->setSpacing( -1 ); 00760 lHl = new QLabel( i18n("&Default schema for %1:", KGlobal::mainComponent().aboutData()->programName ()), hbHl ); 00761 defaultSchemaCombo = new KComboBox( hbHl ); 00762 defaultSchemaCombo->setEditable( false ); 00763 lHl->setBuddy( defaultSchemaCombo ); 00764 00765 m_defaultSchema = KateGlobal::self()->schemaManager()->number (KateRendererConfig::global()->schema()); 00766 00767 reload(); 00768 00769 connect( defaultSchemaCombo, SIGNAL(activated(int)), 00770 this, SLOT(slotChanged()) ); 00771 } 00772 00773 KateSchemaConfigPage::~KateSchemaConfigPage () 00774 { 00775 // just reload config from disc 00776 KateGlobal::self()->schemaManager()->update (); 00777 } 00778 00779 void KateSchemaConfigPage::apply() 00780 { 00781 m_colorTab->apply(); 00782 m_fontTab->apply(); 00783 m_fontColorTab->apply (); 00784 m_highlightTab->apply (); 00785 00786 // just sync the config 00787 KateGlobal::self()->schemaManager()->schema (0).sync(); 00788 00789 KateGlobal::self()->schemaManager()->update (); 00790 00791 // clear all attributes 00792 for (int i = 0; i < KateHlManager::self()->highlights(); ++i) 00793 KateHlManager::self()->getHl (i)->clearAttributeArrays(); 00794 00795 // than reload the whole stuff 00796 KateRendererConfig::global()->setSchema (KateGlobal::self()->schemaManager()->name (defaultSchemaCombo->currentIndex())); 00797 KateRendererConfig::global()->reloadSchema(); 00798 00799 // sync the hl config for real 00800 KateHlManager::self()->getKConfig()->sync (); 00801 } 00802 00803 void KateSchemaConfigPage::reload() 00804 { 00805 // just reload the config from disc 00806 KateGlobal::self()->schemaManager()->update (); 00807 00808 // special for the highlighting stuff 00809 m_fontColorTab->reload (); 00810 00811 update (); 00812 00813 defaultSchemaCombo->setCurrentIndex (KateGlobal::self()->schemaManager()->number (KateRendererConfig::global()->schema())); 00814 00815 // initialize to the schema in the current document, or default schema 00816 schemaCombo->setCurrentIndex( m_defaultSchema ); 00817 schemaChanged( m_defaultSchema ); 00818 } 00819 00820 void KateSchemaConfigPage::reset() 00821 { 00822 reload (); 00823 } 00824 00825 void KateSchemaConfigPage::defaults() 00826 { 00827 reload (); 00828 } 00829 00830 void KateSchemaConfigPage::update () 00831 { 00832 // soft update, no load from disk 00833 KateGlobal::self()->schemaManager()->update (false); 00834 00835 schemaCombo->clear (); 00836 schemaCombo->addItems (KateGlobal::self()->schemaManager()->list ()); 00837 00838 defaultSchemaCombo->clear (); 00839 defaultSchemaCombo->addItems (KateGlobal::self()->schemaManager()->list ()); 00840 00841 schemaCombo->setCurrentIndex (0); 00842 schemaChanged (0); 00843 00844 schemaCombo->setEnabled (schemaCombo->count() > 0); 00845 } 00846 00847 void KateSchemaConfigPage::deleteSchema () 00848 { 00849 int t = schemaCombo->currentIndex (); 00850 00851 KateGlobal::self()->schemaManager()->removeSchema (t); 00852 00853 update (); 00854 } 00855 00856 void KateSchemaConfigPage::newSchema () 00857 { 00858 QString t = KInputDialog::getText (i18n("Name for New Schema"), i18n ("Name:"), i18n("New Schema"), 0, this); 00859 00860 KateGlobal::self()->schemaManager()->addSchema (t); 00861 00862 // soft update, no load from disk 00863 KateGlobal::self()->schemaManager()->update (false); 00864 int i = KateGlobal::self()->schemaManager()->list ().indexOf (t); 00865 00866 update (); 00867 if (i > -1) 00868 { 00869 schemaCombo->setCurrentIndex (i); 00870 schemaChanged (i); 00871 } 00872 } 00873 00874 void KateSchemaConfigPage::schemaChanged (int schema) 00875 { 00876 btndel->setEnabled( schema > 1 ); 00877 00878 m_colorTab->schemaChanged( schema ); 00879 m_fontTab->schemaChanged( schema ); 00880 m_fontColorTab->schemaChanged (schema); 00881 m_highlightTab->schemaChanged (schema); 00882 00883 m_lastSchema = schema; 00884 } 00885 00886 void KateSchemaConfigPage::newCurrentPage (int index) 00887 { 00888 QWidget *w = m_tabWidget->widget(index); 00889 if (w == m_highlightTab) 00890 m_highlightTab->schemaChanged (m_lastSchema); 00891 } 00892 //END KateSchemaConfigPage 00893 00894 //BEGIN SCHEMA ACTION -- the 'View->Schema' menu action 00895 void KateViewSchemaAction::init() 00896 { 00897 m_group=0; 00898 m_view = 0; 00899 last = 0; 00900 00901 connect(menu(),SIGNAL(aboutToShow()),this,SLOT(slotAboutToShow())); 00902 } 00903 00904 void KateViewSchemaAction::updateMenu (KateView *view) 00905 { 00906 m_view = view; 00907 } 00908 00909 void KateViewSchemaAction::slotAboutToShow() 00910 { 00911 KateView *view=m_view; 00912 int count = KateGlobal::self()->schemaManager()->list().count(); 00913 00914 if (!m_group) { 00915 m_group=new QActionGroup(menu()); 00916 m_group->setExclusive(true); 00917 00918 } 00919 00920 for (int z=0; z<count; z++) 00921 { 00922 QString hlName = KateGlobal::self()->schemaManager()->list().operator[](z); 00923 00924 if (!names.contains(hlName)) 00925 { 00926 names << hlName; 00927 QAction *a=menu()->addAction ( hlName, this, SLOT(setSchema())); 00928 a->setData(hlName); 00929 a->setCheckable(true); 00930 a->setActionGroup(m_group); 00931 //FIXME EXCLUSIVE 00932 } 00933 } 00934 00935 if (!view) return; 00936 00937 QString id=view->renderer()->config()->schema(); 00938 foreach(QAction *a,menu()->actions()) { 00939 a->setChecked(a->data().toString()==id); 00940 00941 } 00942 //FIXME 00943 #if 0 00944 popupMenu()->setItemChecked (last, false); 00945 popupMenu()->setItemChecked (view->renderer()->config()->schema()+1, true); 00946 00947 last = view->renderer()->config()->schema()+1; 00948 #endif 00949 } 00950 00951 void KateViewSchemaAction::setSchema () { 00952 QAction *action = qobject_cast<QAction*>(sender()); 00953 00954 if (!action) return; 00955 QString mode=action->data().toString(); 00956 00957 KateView *view=m_view; 00958 00959 if (view) 00960 view->renderer()->config()->setSchema (mode); 00961 } 00962 //END SCHEMA ACTION 00963 00964 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference