kate Library API Documentation

kateschema.cpp

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., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, 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=0, 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 // 00266 // 00267 // DIALOGS !!! 00268 // 00269 // 00270 00271 //BEGIN KateSchemaConfigColorTab 00272 KateSchemaConfigColorTab::KateSchemaConfigColorTab( QWidget *parent, const char * ) 00273 : QWidget (parent) 00274 { 00275 QHBox *b; 00276 QLabel *label; 00277 00278 QVBoxLayout *blay=new QVBoxLayout(this, 0, KDialog::spacingHint()); 00279 00280 QVGroupBox *gbTextArea = new QVGroupBox(i18n("Text Area Background"), this); 00281 00282 b = new QHBox (gbTextArea); 00283 b->setSpacing(KDialog::spacingHint()); 00284 label = new QLabel( i18n("Normal text:"), b); 00285 label->setAlignment( AlignLeft|AlignVCenter); 00286 m_back = new KColorButton(b); 00287 00288 b = new QHBox (gbTextArea); 00289 b->setSpacing(KDialog::spacingHint()); 00290 label = new QLabel( i18n("Selected text:"), b); 00291 label->setAlignment( AlignLeft|AlignVCenter); 00292 m_selected = new KColorButton(b); 00293 00294 b = new QHBox (gbTextArea); 00295 b->setSpacing(KDialog::spacingHint()); 00296 label = new QLabel( i18n("Current line:"), b); 00297 label->setAlignment( AlignLeft|AlignVCenter); 00298 m_current = new KColorButton(b); 00299 00300 // Markers from kdelibs/interfaces/ktextinterface/markinterface.h 00301 b = new QHBox (gbTextArea); 00302 b->setSpacing(KDialog::spacingHint()); 00303 m_combobox = new KComboBox(b, "color_combo_box"); 00304 // add the predefined mark types as defined in markinterface.h 00305 m_combobox->insertItem(i18n("Bookmark")); // markType01 00306 m_combobox->insertItem(i18n("Active Breakpoint")); // markType02 00307 m_combobox->insertItem(i18n("Reached Breakpoint")); // markType03 00308 m_combobox->insertItem(i18n("Disabled Breakpoint")); // markType04 00309 m_combobox->insertItem(i18n("Execution")); // markType05 00310 m_combobox->insertItem(i18n("Warning")); // markType06 00311 m_combobox->insertItem(i18n("Error")); // markType07 00312 m_combobox->setCurrentItem(0); 00313 m_markers = new KColorButton(b, "marker_color_button"); 00314 connect( m_combobox, SIGNAL( activated( int ) ), SLOT( slotComboBoxChanged( int ) ) ); 00315 00316 blay->addWidget(gbTextArea); 00317 00318 QVGroupBox *gbBorder = new QVGroupBox(i18n("Additional Elements"), this); 00319 00320 b = new QHBox (gbBorder); 00321 b->setSpacing(KDialog::spacingHint()); 00322 label = new QLabel( i18n("Left border background:"), b); 00323 label->setAlignment( AlignLeft|AlignVCenter); 00324 m_iconborder = new KColorButton(b); 00325 00326 b = new QHBox (gbBorder); 00327 b->setSpacing(KDialog::spacingHint()); 00328 label = new QLabel( i18n("Line numbers:"), b); 00329 label->setAlignment( AlignLeft|AlignVCenter); 00330 m_linenumber = new KColorButton(b); 00331 00332 b = new QHBox (gbBorder); 00333 b->setSpacing(KDialog::spacingHint()); 00334 label = new QLabel( i18n("Bracket highlight:"), b); 00335 label->setAlignment( AlignLeft|AlignVCenter); 00336 m_bracket = new KColorButton(b); 00337 00338 b = new QHBox (gbBorder); 00339 b->setSpacing(KDialog::spacingHint()); 00340 label = new QLabel( i18n("Word wrap markers:"), b); 00341 label->setAlignment( AlignLeft|AlignVCenter); 00342 m_wwmarker = new KColorButton(b); 00343 00344 b = new QHBox (gbBorder); 00345 b->setSpacing(KDialog::spacingHint()); 00346 label = new QLabel( i18n("Tab markers:"), b); 00347 label->setAlignment( AlignLeft|AlignVCenter); 00348 m_tmarker = new KColorButton(b); 00349 00350 blay->addWidget(gbBorder); 00351 00352 blay->addStretch(); 00353 00354 // connect signal changed(); changed is emitted by a ColorButton change! 00355 connect( this, SIGNAL( changed() ), parent->parentWidget(), SLOT( slotChanged() ) ); 00356 00357 // QWhatsThis help 00358 QWhatsThis::add(m_back, i18n("<p>Sets the background color of the editing area.</p>")); 00359 QWhatsThis::add(m_selected, i18n("<p>Sets the background color of the selection.</p>" 00360 "<p>To set the text color for selected text, use the \"<b>Configure " 00361 "Highlighting</b>\" dialog.</p>")); 00362 QWhatsThis::add(m_markers, i18n("<p>Sets the background color of the selected " 00363 "marker type.</p><p><b>Note</b>: The marker color is displayed lightly because " 00364 "of transparency.</p>")); 00365 QWhatsThis::add(m_combobox, i18n("<p>Select the marker type you want to change.</p>")); 00366 QWhatsThis::add(m_current, i18n("<p>Sets the background color of the currently " 00367 "active line, which means the line where your cursor is positioned.</p>")); 00368 QWhatsThis::add( m_linenumber, i18n( 00369 "<p>This color will be used to draw the line numbers (if enabled) and the " 00370 "lines in the code-folding pane.</p>" ) ); 00371 QWhatsThis::add(m_bracket, i18n("<p>Sets the bracket matching color. This means, " 00372 "if you place the cursor e.g. at a <b>(</b>, the matching <b>)</b> will " 00373 "be highlighted with this color.</p>")); 00374 QWhatsThis::add(m_wwmarker, i18n( 00375 "<p>Sets the color of Word Wrap-related markers:</p>" 00376 "<dl><dt>Static Word Wrap</dt><dd>A vertical line which shows the column where " 00377 "text is going to be wrapped</dd>" 00378 "<dt>Dynamic Word Wrap</dt><dd>An arrow shown to the left of " 00379 "visually-wrapped lines</dd></dl>")); 00380 QWhatsThis::add(m_tmarker, i18n( 00381 "<p>Sets the color of the tabulator marks:</p>")); 00382 } 00383 00384 KateSchemaConfigColorTab::~KateSchemaConfigColorTab() 00385 { 00386 } 00387 00388 void KateSchemaConfigColorTab::readConfig (KConfig *config) 00389 { 00390 // first disconnect all signals otherwise setColor emits changed 00391 m_back ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00392 m_selected ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00393 m_current ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00394 m_bracket ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00395 m_wwmarker ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00396 m_iconborder->disconnect( SIGNAL( changed( const QColor & ) ) ); 00397 m_tmarker ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00398 m_markers ->disconnect( SIGNAL( changed( const QColor & ) ) ); 00399 m_linenumber->disconnect( SIGNAL( changed( const QColor & ) ) ); 00400 00401 QColor tmp0 (KGlobalSettings::baseColor()); 00402 QColor tmp1 (KGlobalSettings::highlightColor()); 00403 QColor tmp2 (KGlobalSettings::alternateBackgroundColor()); 00404 QColor tmp3 ( "#FFFF99" ); 00405 QColor tmp4 (tmp2.dark()); 00406 QColor tmp5 ( KGlobalSettings::textColor() ); 00407 QColor tmp6 ( "#EAE9E8" ); 00408 QColor tmp7 ( "#000000" ); 00409 00410 m_back->setColor(config->readColorEntry("Color Background", &tmp0)); 00411 m_selected->setColor(config->readColorEntry("Color Selection", &tmp1)); 00412 m_current->setColor(config->readColorEntry("Color Highlighted Line", &tmp2)); 00413 m_bracket->setColor(config->readColorEntry("Color Highlighted Bracket", &tmp3)); 00414 m_wwmarker->setColor(config->readColorEntry("Color Word Wrap Marker", &tmp4)); 00415 m_tmarker->setColor(config->readColorEntry("Color Tab Marker", &tmp5)); 00416 m_iconborder->setColor(config->readColorEntry("Color Icon Bar", &tmp6)); 00417 m_linenumber->setColor(config->readColorEntry("Color Line Number", &tmp7)); 00418 00419 // same std colors like in KateDocument::markColor 00420 QValueVector <QColor> mark(KTextEditor::MarkInterface::reservedMarkersCount()); 00421 Q_ASSERT(mark.size() > 6); 00422 mark[0] = Qt::blue; 00423 mark[1] = Qt::red; 00424 mark[2] = Qt::yellow; 00425 mark[3] = Qt::magenta; 00426 mark[4] = Qt::gray; 00427 mark[5] = Qt::green; 00428 mark[6] = Qt::red; 00429 00430 // map from 0..reservedMarkersCount()-1 - the same index as in markInterface 00431 for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++) 00432 { 00433 // arg(i+1) to match the number conventions in markinterface.h 00434 m_markerColors[i] = config->readColorEntry(QString("Color MarkType%1").arg(i + 1), &mark[i]); 00435 QPixmap pix(16, 16); 00436 pix.fill(m_markerColors[i]); 00437 m_combobox->changeItem(pix, m_combobox->text(i), i); 00438 } 00439 m_markers->setColor( m_markerColors[ m_combobox->currentItem() ] ); 00440 00441 connect( m_back , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00442 connect( m_selected , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00443 connect( m_current , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00444 connect( m_bracket , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00445 connect( m_wwmarker , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00446 connect( m_iconborder, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00447 connect( m_tmarker , SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00448 connect( m_linenumber, SIGNAL( changed( const QColor& ) ), SIGNAL( changed() ) ); 00449 connect( m_markers , SIGNAL( changed( const QColor& ) ), SLOT( slotMarkerColorChanged( const QColor& ) ) ); 00450 } 00451 00452 void KateSchemaConfigColorTab::writeConfig (KConfig *config) 00453 { 00454 config->writeEntry("Color Background", m_back->color()); 00455 config->writeEntry("Color Selection", m_selected->color()); 00456 config->writeEntry("Color Highlighted Line", m_current->color()); 00457 config->writeEntry("Color Highlighted Bracket", m_bracket->color()); 00458 config->writeEntry("Color Word Wrap Marker", m_wwmarker->color()); 00459 config->writeEntry("Color Tab Marker", m_tmarker->color()); 00460 config->writeEntry("Color Icon Bar", m_iconborder->color()); 00461 config->writeEntry("Color Line Number", m_linenumber->color()); 00462 00463 for (int i = 0; i < KTextEditor::MarkInterface::reservedMarkersCount(); i++) 00464 { 00465 config->writeEntry(QString("Color MarkType%1").arg(i + 1), m_markerColors[i]); 00466 } 00467 } 00468 00469 void KateSchemaConfigColorTab::slotMarkerColorChanged( const QColor& color) 00470 { 00471 int index = m_combobox->currentItem(); 00472 m_markerColors[ index ] = color; 00473 QPixmap pix(16, 16); 00474 pix.fill(color); 00475 m_combobox->changeItem(pix, m_combobox->text(index), index); 00476 00477 emit changed(); 00478 } 00479 00480 void KateSchemaConfigColorTab::slotComboBoxChanged(int index) 00481 { 00482 // temporarily disconnect the changed-signal because setColor emits changed as well 00483 m_markers->disconnect( SIGNAL( changed( const QColor& ) ) ); 00484 m_markers->setColor( m_markerColors[index] ); 00485 connect( m_markers, SIGNAL( changed( const QColor& ) ), SLOT( slotMarkerColorChanged( const QColor& ) ) ); 00486 } 00487 00488 //END KateSchemaConfigColorTab 00489 00490 //BEGIN FontConfig 00491 KateSchemaConfigFontTab::KateSchemaConfigFontTab( QWidget *parent, const char * ) 00492 : QWidget (parent) 00493 { 00494 // sizemanagment 00495 QGridLayout *grid = new QGridLayout( this, 1, 1 ); 00496 00497 m_fontchooser = new KFontChooser ( this, 0L, false, QStringList(), false ); 00498 m_fontchooser->enableColumn(KFontChooser::StyleList, false); 00499 grid->addWidget( m_fontchooser, 0, 0); 00500 00501 connect (this, SIGNAL( changed()), parent->parentWidget(), SLOT (slotChanged())); 00502 } 00503 00504 KateSchemaConfigFontTab::~KateSchemaConfigFontTab() 00505 { 00506 } 00507 00508 void KateSchemaConfigFontTab::slotFontSelected( const QFont &font ) 00509 { 00510 myFont = font; 00511 00512 emit changed(); 00513 } 00514 00515 void KateSchemaConfigFontTab::readConfig (KConfig *config) 00516 { 00517 QFont f (KGlobalSettings::fixedFont()); 00518 00519 m_fontchooser->disconnect ( this ); 00520 m_fontchooser->setFont (config->readFontEntry("Font", &f)); 00521 myFont = m_fontchooser->font(); 00522 connect (m_fontchooser, SIGNAL (fontSelected( const QFont & )), this, SLOT (slotFontSelected( const QFont & ))); 00523 } 00524 00525 void KateSchemaConfigFontTab::writeConfig (KConfig *config) 00526 { 00527 config->writeEntry("Font", myFont); 00528 } 00529 00530 //END FontConfig 00531 00532 //BEGIN FontColorConfig 00533 KateSchemaConfigFontColorTab::KateSchemaConfigFontColorTab( QWidget *parent, const char * ) 00534 : QWidget (parent) 00535 { 00536 m_defaultStyleLists.setAutoDelete(true); 00537 00538 // sizemanagment 00539 QGridLayout *grid = new QGridLayout( this, 1, 1 ); 00540 00541 m_defaultStyles = new KateStyleListView( this, false ); 00542 grid->addWidget( m_defaultStyles, 0, 0); 00543 00544 connect (m_defaultStyles, SIGNAL (changed()), parent->parentWidget(), SLOT (slotChanged())); 00545 } 00546 00547 KateSchemaConfigFontColorTab::~KateSchemaConfigFontColorTab() 00548 { 00549 } 00550 00551 KateAttributeList *KateSchemaConfigFontColorTab::attributeList (uint schema) 00552 { 00553 if (!m_defaultStyleLists[schema]) 00554 { 00555 KateAttributeList *list = new KateAttributeList (); 00556 KateHlManager::self()->getDefaults(schema, *list); 00557 00558 m_defaultStyleLists.insert (schema, list); 00559 } 00560 00561 return m_defaultStyleLists[schema]; 00562 } 00563 00564 void KateSchemaConfigFontColorTab::schemaChanged (uint schema) 00565 { 00566 m_defaultStyles->clear (); 00567 00568 KateAttributeList *l = attributeList (schema); 00569 00570 // set colors 00571 QPalette p ( m_defaultStyles->palette() ); 00572 QColor _c ( KGlobalSettings::baseColor() ); 00573 p.setColor( QColorGroup::Base, 00574 KateFactory::self()->schemaManager()->schema(schema)-> 00575 readColorEntry( "Color Background", &_c ) ); 00576 _c = KGlobalSettings::highlightColor(); 00577 p.setColor( QColorGroup::Highlight, 00578 KateFactory::self()->schemaManager()->schema(schema)-> 00579 readColorEntry( "Color Selection", &_c ) ); 00580 _c = l->at(0)->textColor(); // not quite as much of an assumption ;) 00581 p.setColor( QColorGroup::Text, _c ); 00582 m_defaultStyles->viewport()->setPalette( p ); 00583 00584 // insert the default styles backwards to get them in the right order 00585 for ( int i = KateHlManager::self()->defaultStyles() - 1; i >= 0; i-- ) 00586 { 00587 new KateStyleListItem( m_defaultStyles, KateHlManager::self()->defaultStyleName(i), l->at( i ) ); 00588 } 00589 00590 QWhatsThis::add( m_defaultStyles, i18n( 00591 "This list displays the default styles for the current schema and " 00592 "offers the means to edit them. The style name reflects the current " 00593 "style settings.<p>To edit using the keyboard, press " 00594 "<strong>&lt;SPACE&gt;</strong> and choose a property from the popup menu." 00595 "<p>To edit the colors, click the colored squares, or select the color " 00596 "to edit from the popup menu.<p>You can unset the Background and Selected " 00597 "Background colors from the popup menu when appropriate.") ); 00598 00599 } 00600 00601 void KateSchemaConfigFontColorTab::reload () 00602 { 00603 m_defaultStyles->clear (); 00604 m_defaultStyleLists.clear (); 00605 } 00606 00607 void KateSchemaConfigFontColorTab::apply () 00608 { 00609 for ( QIntDictIterator<KateAttributeList> it( m_defaultStyleLists ); it.current(); ++it ) 00610 KateHlManager::self()->setDefaults(it.currentKey(), *(it.current())); 00611 } 00612 00613 //END FontColorConfig 00614 00615 //BEGIN KateSchemaConfigHighlightTab 00616 KateSchemaConfigHighlightTab::KateSchemaConfigHighlightTab( QWidget *parent, const char *, KateSchemaConfigFontColorTab *page, uint hl ) 00617 : QWidget (parent) 00618 { 00619 m_defaults = page; 00620 00621 m_schema = 0; 00622 m_hl = 0; 00623 00624 m_hlDict.setAutoDelete (true); 00625 00626 QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() ); 00627 00628 // hl chooser 00629 QHBox *hbHl = new QHBox( this ); 00630 layout->add (hbHl); 00631 00632 hbHl->setSpacing( KDialog::spacingHint() ); 00633 QLabel *lHl = new QLabel( i18n("H&ighlight:"), hbHl ); 00634 hlCombo = new QComboBox( false, hbHl ); 00635 lHl->setBuddy( hlCombo ); 00636 connect( hlCombo, SIGNAL(activated(int)), 00637 this, SLOT(hlChanged(int)) ); 00638 00639 for( int i = 0; i < KateHlManager::self()->highlights(); i++) { 00640 if (KateHlManager::self()->hlSection(i).length() > 0) 00641 hlCombo->insertItem(KateHlManager::self()->hlSection(i) + QString ("/") + KateHlManager::self()->hlNameTranslated(i)); 00642 else 00643 hlCombo->insertItem(KateHlManager::self()->hlNameTranslated(i)); 00644 } 00645 hlCombo->setCurrentItem(0); 00646 00647 // styles listview 00648 m_styles = new KateStyleListView( this, true ); 00649 layout->addWidget (m_styles, 999); 00650 00651 hlCombo->setCurrentItem ( hl ); 00652 hlChanged ( hl ); 00653 00654 QWhatsThis::add( m_styles, i18n( 00655 "This list displays the contexts of the current syntax highlight mode and " 00656 "offers the means to edit them. The context name reflects the current " 00657 "style settings.<p>To edit using the keyboard, press " 00658 "<strong>&lt;SPACE&gt;</strong> and choose a property from the popup menu." 00659 "<p>To edit the colors, click the colored squares, or select the color " 00660 "to edit from the popup menu.<p>You can unset the Background and Selected " 00661 "Background colors from the context menu when appropriate.") ); 00662 00663 connect (m_styles, SIGNAL (changed()), parent->parentWidget(), SLOT (slotChanged())); 00664 } 00665 00666 KateSchemaConfigHighlightTab::~KateSchemaConfigHighlightTab() 00667 { 00668 } 00669 00670 void KateSchemaConfigHighlightTab::hlChanged(int z) 00671 { 00672 m_hl = z; 00673 00674 schemaChanged (m_schema); 00675 } 00676 00677 void KateSchemaConfigHighlightTab::schemaChanged (uint schema) 00678 { 00679 m_schema = schema; 00680 00681 kdDebug () << "NEW SCHEMA: " << m_schema << " NEW HL: " << m_hl << endl; 00682 00683 m_styles->clear (); 00684 00685 if (!m_hlDict[m_schema]) 00686 { 00687 kdDebug () << "NEW SCHEMA, create dict" << endl; 00688 00689 m_hlDict.insert (schema, new QIntDict<KateHlItemDataList>); 00690 m_hlDict[m_schema]->setAutoDelete (true); 00691 } 00692 00693 if (!m_hlDict[m_schema]->find(m_hl)) 00694 { 00695 kdDebug () << "NEW HL, create list" << endl; 00696 00697 KateHlItemDataList *list = new KateHlItemDataList (); 00698 KateHlManager::self()->getHl( m_hl )->getKateHlItemDataListCopy (m_schema, *list); 00699 m_hlDict[m_schema]->insert (m_hl, list); 00700 } 00701 00702 KateAttributeList *l = m_defaults->attributeList (schema); 00703 00704 // Set listview colors 00705 // We do that now, because we can now get the "normal text" color. 00706 // TODO this reads of the KConfig object, which should be changed when 00707 // the color tab is fixed. 00708 QPalette p ( m_styles->palette() ); 00709 QColor _c ( KGlobalSettings::baseColor() ); 00710 p.setColor( QColorGroup::Base, 00711 KateFactory::self()->schemaManager()->schema(m_schema)-> 00712 readColorEntry( "Color Background", &_c ) ); 00713 _c = KGlobalSettings::highlightColor(); 00714 p.setColor( QColorGroup::Highlight, 00715 KateFactory::self()->schemaManager()->schema(m_schema)-> 00716 readColorEntry( "Color Selection", &_c ) ); 00717 _c = l->at(0)->textColor(); // not quite as much of an assumption ;) 00718 p.setColor( QColorGroup::Text, _c ); 00719 m_styles->viewport()->setPalette( p ); 00720 00721 QDict<KateStyleListCaption> prefixes; 00722 for ( KateHlItemData *itemData = m_hlDict[m_schema]->find(m_hl)->last(); 00723 itemData != 0L; 00724 itemData = m_hlDict[m_schema]->find(m_hl)->prev()) 00725 { 00726 kdDebug () << "insert items " << itemData->name << endl; 00727 00728 // All stylenames have their language mode prefixed, e.g. HTML:Comment 00729 // split them and put them into nice substructures. 00730 int c = itemData->name.find(':'); 00731 if ( c > 0 ) { 00732 QString prefix = itemData->name.left(c); 00733 QString name = itemData->name.mid(c+1); 00734 00735 KateStyleListCaption *parent = prefixes.find( prefix ); 00736 if ( ! parent ) 00737 { 00738 parent = new KateStyleListCaption( m_styles, prefix ); 00739 parent->setOpen(true); 00740 prefixes.insert( prefix, parent ); 00741 } 00742 new KateStyleListItem( parent, name, l->at(itemData->defStyleNum), itemData ); 00743 } else { 00744 new KateStyleListItem( m_styles, itemData->name, l->at(itemData->defStyleNum), itemData ); 00745 } 00746 } 00747 } 00748 00749 void KateSchemaConfigHighlightTab::reload () 00750 { 00751 m_styles->clear (); 00752 m_hlDict.clear (); 00753 00754 hlChanged (0); 00755 } 00756 00757 void KateSchemaConfigHighlightTab::apply () 00758 { 00759 for ( QIntDictIterator< QIntDict<KateHlItemDataList> > it( m_hlDict ); it.current(); ++it ) 00760 for ( QIntDictIterator< KateHlItemDataList > it2( *it.current() ); it2.current(); ++it2 ) 00761 KateHlManager::self()->getHl( it2.currentKey() )->setKateHlItemDataList (it.currentKey(), *(it2.current())); 00762 } 00763 00764 //END KateSchemaConfigHighlightTab 00765 00766 //BEGIN KateSchemaConfigPage 00767 KateSchemaConfigPage::KateSchemaConfigPage( QWidget *parent, KateDocument *doc ) 00768 : KateConfigPage( parent ), 00769 m_lastSchema (-1) 00770 { 00771 QVBoxLayout *layout = new QVBoxLayout(this, 0, KDialog::spacingHint() ); 00772 00773 QHBox *hbHl = new QHBox( this ); 00774 layout->add (hbHl); 00775 hbHl->setSpacing( KDialog::spacingHint() ); 00776 QLabel *lHl = new QLabel( i18n("&Schema:"), hbHl ); 00777 schemaCombo = new QComboBox( false, hbHl ); 00778 lHl->setBuddy( schemaCombo ); 00779 connect( schemaCombo, SIGNAL(activated(int)), 00780 this, SLOT(schemaChanged(int)) ); 00781 00782 QPushButton *btnnew = new QPushButton( i18n("&New..."), hbHl ); 00783 connect( btnnew, SIGNAL(clicked()), this, SLOT(newSchema()) ); 00784 00785 btndel = new QPushButton( i18n("&Delete"), hbHl ); 00786 connect( btndel, SIGNAL(clicked()), this, SLOT(deleteSchema()) ); 00787 00788 m_tabWidget = new QTabWidget ( this ); 00789 m_tabWidget->setMargin (KDialog::marginHint()); 00790 layout->add (m_tabWidget); 00791 00792 connect (m_tabWidget, SIGNAL (currentChanged (QWidget *)), this, SLOT (newCurrentPage (QWidget *))); 00793 00794 m_colorTab = new KateSchemaConfigColorTab (m_tabWidget); 00795 m_tabWidget->addTab (m_colorTab, i18n("Colors")); 00796 00797 m_fontTab = new KateSchemaConfigFontTab (m_tabWidget); 00798 m_tabWidget->addTab (m_fontTab, i18n("Font")); 00799 00800 m_fontColorTab = new KateSchemaConfigFontColorTab (m_tabWidget); 00801 m_tabWidget->addTab (m_fontColorTab, i18n("Normal Text Styles")); 00802 00803 uint hl = doc ? doc->hlMode() : 0; 00804 m_highlightTab = new KateSchemaConfigHighlightTab (m_tabWidget, "", m_fontColorTab, hl ); 00805 m_tabWidget->addTab (m_highlightTab, i18n("Highlighting Text Styles")); 00806 00807 hbHl = new QHBox( this ); 00808 layout->add (hbHl); 00809 hbHl->setSpacing( KDialog::spacingHint() ); 00810 lHl = new QLabel( i18n("&Default schema for %1:").arg(KApplication::kApplication()->aboutData()->programName ()), hbHl ); 00811 defaultSchemaCombo = new QComboBox( false, hbHl ); 00812 lHl->setBuddy( defaultSchemaCombo ); 00813 00814 00815 m_defaultSchema = (doc && doc->activeView()) ? doc->activeView()->renderer()->config()->schema() : KateRendererConfig::global()->schema(); 00816 00817 reload(); 00818 00819 connect( defaultSchemaCombo, SIGNAL(activated(int)), 00820 this, SLOT(slotChanged()) ); 00821 } 00822 00823 KateSchemaConfigPage::~KateSchemaConfigPage () 00824 { 00825 // just reload config from disc 00826 KateFactory::self()->schemaManager()->update (); 00827 } 00828 00829 void KateSchemaConfigPage::apply() 00830 { 00831 if (m_lastSchema > -1) 00832 { 00833 m_colorTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema)); 00834 m_fontTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema)); 00835 } 00836 00837 // just sync the config 00838 KateFactory::self()->schemaManager()->schema (0)->sync(); 00839 KateFactory::self()->schemaManager()->update (); 00840 00841 KateRendererConfig::global()->setSchema (defaultSchemaCombo->currentItem()); 00842 00843 // special for the highlighting stuff 00844 m_fontColorTab->apply (); 00845 m_highlightTab->apply (); 00846 00847 // sync the hl config for real 00848 KateHlManager::self()->getKConfig()->sync (); 00849 } 00850 00851 void KateSchemaConfigPage::reload() 00852 { 00853 // just reload the config from disc 00854 KateFactory::self()->schemaManager()->update (); 00855 00856 // special for the highlighting stuff 00857 m_fontColorTab->reload (); 00858 00859 update (); 00860 00861 defaultSchemaCombo->setCurrentItem (KateRendererConfig::global()->schema()); 00862 00863 // initialize to the schema in the current document, or default schema 00864 schemaCombo->setCurrentItem( m_defaultSchema ); 00865 schemaChanged( m_defaultSchema ); 00866 } 00867 00868 void KateSchemaConfigPage::reset() 00869 { 00870 reload (); 00871 } 00872 00873 void KateSchemaConfigPage::defaults() 00874 { 00875 reload (); 00876 } 00877 00878 void KateSchemaConfigPage::update () 00879 { 00880 // soft update, no load from disk 00881 KateFactory::self()->schemaManager()->update (false); 00882 00883 schemaCombo->clear (); 00884 schemaCombo->insertStringList (KateFactory::self()->schemaManager()->list ()); 00885 00886 defaultSchemaCombo->clear (); 00887 defaultSchemaCombo->insertStringList (KateFactory::self()->schemaManager()->list ()); 00888 00889 schemaCombo->setCurrentItem (0); 00890 schemaChanged (0); 00891 00892 schemaCombo->setEnabled (schemaCombo->count() > 0); 00893 } 00894 00895 void KateSchemaConfigPage::deleteSchema () 00896 { 00897 int t = schemaCombo->currentItem (); 00898 00899 KateFactory::self()->schemaManager()->removeSchema (t); 00900 00901 update (); 00902 } 00903 00904 void KateSchemaConfigPage::newSchema () 00905 { 00906 QString t = KInputDialog::getText (i18n("Name for New Schema"), i18n ("Name:"), i18n("New Schema"), 0, this); 00907 00908 KateFactory::self()->schemaManager()->addSchema (t); 00909 00910 // soft update, no load from disk 00911 KateFactory::self()->schemaManager()->update (false); 00912 int i = KateFactory::self()->schemaManager()->list ().findIndex (t); 00913 00914 update (); 00915 if (i > -1) 00916 { 00917 schemaCombo->setCurrentItem (i); 00918 schemaChanged (i); 00919 } 00920 } 00921 00922 void KateSchemaConfigPage::schemaChanged (int schema) 00923 { 00924 if (schema < 2) 00925 { 00926 btndel->setEnabled (false); 00927 } 00928 else 00929 { 00930 btndel->setEnabled (true); 00931 } 00932 00933 if (m_lastSchema > -1) // so, pressing "Cancel" is no good here? 00934 { 00935 m_colorTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema)); 00936 m_fontTab->writeConfig (KateFactory::self()->schemaManager()->schema(m_lastSchema)); 00937 } 00938 00939 m_colorTab->readConfig (KateFactory::self()->schemaManager()->schema(schema)); 00940 m_fontTab->readConfig (KateFactory::self()->schemaManager()->schema(schema)); 00941 m_fontColorTab->schemaChanged (schema); 00942 m_highlightTab->schemaChanged (schema); 00943 00944 m_lastSchema = schema; 00945 } 00946 00947 void KateSchemaConfigPage::newCurrentPage (QWidget *w) 00948 { 00949 if (w == m_highlightTab) 00950 m_highlightTab->schemaChanged (m_lastSchema); 00951 } 00952 //END KateSchemaConfigPage 00953 00954 // BEGIN SCHEMA ACTION 00955 void KateViewSchemaAction::init() 00956 { 00957 m_view = 0; 00958 last = 0; 00959 00960 connect(popupMenu(),SIGNAL(aboutToShow()),this,SLOT(slotAboutToShow())); 00961 } 00962 00963 void KateViewSchemaAction::updateMenu (KateView *view) 00964 { 00965 m_view = view; 00966 } 00967 00968 void KateViewSchemaAction::slotAboutToShow() 00969 { 00970 KateView *view=m_view; 00971 int count = KateFactory::self()->schemaManager()->list().count(); 00972 00973 for (int z=0; z<count; z++) 00974 { 00975 QString hlName = KateFactory::self()->schemaManager()->list().operator[](z); 00976 00977 if (names.contains(hlName) < 1) 00978 { 00979 names << hlName; 00980 popupMenu()->insertItem ( hlName, this, SLOT(setSchema(int)), 0, z+1); 00981 } 00982 } 00983 00984 if (!view) return; 00985 00986 popupMenu()->setItemChecked (last, false); 00987 popupMenu()->setItemChecked (view->renderer()->config()->schema()+1, true); 00988 00989 last = view->renderer()->config()->schema()+1; 00990 } 00991 00992 void KateViewSchemaAction::setSchema (int mode) 00993 { 00994 KateView *view=m_view; 00995 00996 if (view) 00997 view->renderer()->config()->setSchema (mode-1); 00998 } 00999 // END SCHEMA ACTION 01000 01001 //BEGIN KateStyleListView 01002 KateStyleListView::KateStyleListView( QWidget *parent, bool showUseDefaults ) 01003 : QListView( parent ) 01004 { 01005 setSorting( -1 ); // disable sorting, let the styles appear in their defined order 01006 addColumn( i18n("Context") ); 01007 addColumn( SmallIconSet("text_bold"), QString::null ); 01008 addColumn( SmallIconSet("text_italic"), QString::null ); 01009 addColumn( SmallIconSet("text_under"), QString::null ); 01010 addColumn( SmallIconSet("text_strike"), QString::null ); 01011 addColumn( i18n("Normal") ); 01012 addColumn( i18n("Selected") ); 01013 addColumn( i18n("Background") ); 01014 addColumn( i18n("Background Selected") ); 01015 if ( showUseDefaults ) 01016 addColumn( i18n("Use Default Style") ); 01017 connect( this, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)), 01018 this, SLOT(slotMousePressed(int, QListViewItem*, const QPoint&, int)) ); 01019 connect( this, SIGNAL(spacePressed(QListViewItem*)), 01020 this, SLOT(showPopupMenu(QListViewItem*)) ); 01021 // grap the bg color, selected color and default font 01022 normalcol = KGlobalSettings::textColor(); 01023 bgcol = KateRendererConfig::global()->backgroundColor(); 01024 selcol = KateRendererConfig::global()->selectionColor(); 01025 docfont = *KateRendererConfig::global()->font(); 01026 01027 viewport()->setPaletteBackgroundColor( bgcol ); 01028 } 01029 01030 void KateStyleListView::showPopupMenu( KateStyleListItem *i, const QPoint &globalPos, bool showtitle ) 01031 { 01032 if ( !dynamic_cast<KateStyleListItem*>(i) ) return; 01033 01034 KPopupMenu m( this ); 01035 KateAttribute *is = i->style(); 01036 int id; 01037 // the title is used, because the menu obscures the context name when 01038 // displayed on behalf of spacePressed(). 01039 QPixmap cl(16,16); 01040 cl.fill( i->style()->textColor() ); 01041 QPixmap scl(16,16); 01042 scl.fill( i->style()->selectedTextColor() ); 01043 QPixmap bgcl(16,16); 01044 bgcl.fill( i->style()->itemSet(KateAttribute::BGColor) ? i->style()->bgColor() : viewport()->colorGroup().base() ); 01045 QPixmap sbgcl(16,16); 01046 sbgcl.fill( i->style()->itemSet(KateAttribute::SelectedBGColor) ? i->style()->selectedBGColor() : viewport()->colorGroup().base() ); 01047 01048 if ( showtitle ) 01049 m.insertTitle( i->contextName(), KateStyleListItem::ContextName ); 01050 id = m.insertItem( i18n("&Bold"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Bold ); 01051 m.setItemChecked( id, is->bold() ); 01052 id = m.insertItem( i18n("&Italic"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Italic ); 01053 m.setItemChecked( id, is->italic() ); 01054 id = m.insertItem( i18n("&Underline"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Underline ); 01055 m.setItemChecked( id, is->underline() ); 01056 id = m.insertItem( i18n("S&trikeout"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Strikeout ); 01057 m.setItemChecked( id, is->strikeOut() ); 01058 01059 m.insertSeparator(); 01060 01061 m.insertItem( QIconSet(cl), i18n("Normal &Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::Color ); 01062 m.insertItem( QIconSet(scl), i18n("&Selected Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::SelColor ); 01063 m.insertItem( QIconSet(bgcl), i18n("&Background Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::BgColor ); 01064 m.insertItem( QIconSet(sbgcl), i18n("S&elected Background Color..."), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::SelBgColor ); 01065 01066 // Unset [some] colors. I could show one only if that button was clicked, but that 01067 // would disable setting this with the keyboard (how many aren't doing just 01068 // that every day? ;) 01069 // ANY ideas for doing this in a nicer way will be warmly wellcomed. 01070 KateAttribute *style = i->style(); 01071 if ( style->itemSet( KateAttribute::BGColor) || style->itemSet( KateAttribute::SelectedBGColor ) ) 01072 { 01073 m.insertSeparator(); 01074 if ( style->itemSet( KateAttribute::BGColor) ) 01075 m.insertItem( i18n("Unset Background Color"), this, SLOT(unsetColor(int)), 0, 100 ); 01076 if ( style->itemSet( KateAttribute::SelectedBGColor ) ) 01077 m.insertItem( i18n("Unset Selected Background Color"), this, SLOT(unsetColor(int)), 0, 101 ); 01078 } 01079 01080 if ( ! i->isDefault() && ! i->defStyle() ) { 01081 m.insertSeparator(); 01082 id = m.insertItem( i18n("Use &Default Style"), this, SLOT(mSlotPopupHandler(int)), 0, KateStyleListItem::UseDefStyle ); 01083 m.setItemChecked( id, i->defStyle() ); 01084 } 01085 m.exec( globalPos ); 01086 } 01087 01088 void KateStyleListView::showPopupMenu( QListViewItem *i ) 01089 { 01090 if ( dynamic_cast<KateStyleListItem*>(i) ) 01091 showPopupMenu( (KateStyleListItem*)i, viewport()->mapToGlobal(itemRect(i).topLeft()), true ); 01092 } 01093 01094 void KateStyleListView::mSlotPopupHandler( int z ) 01095 { 01096 ((KateStyleListItem*)currentItem())->changeProperty( (KateStyleListItem::Property)z ); 01097 } 01098 01099 void KateStyleListView::unsetColor( int c ) 01100 { 01101 ((KateStyleListItem*)currentItem())->unsetColor( c ); 01102 } 01103 01104 // Because QListViewItem::activatePos() is going to become deprecated, 01105 // and also because this attempt offers more control, I connect mousePressed to this. 01106 void KateStyleListView::slotMousePressed(int btn, QListViewItem* i, const QPoint& pos, int c) 01107 { 01108 if ( dynamic_cast<KateStyleListItem*>(i) ) { 01109 if ( btn == Qt::RightButton ) { 01110 showPopupMenu( (KateStyleListItem*)i, /*mapToGlobal(*/pos/*)*/ ); 01111 } 01112 else if ( btn == Qt::LeftButton && c > 0 ) { 01113 // map pos to item/column and call KateStyleListItem::activate(col, pos) 01114 ((KateStyleListItem*)i)->activate( c, viewport()->mapFromGlobal( pos ) - QPoint( 0, itemRect(i).top() ) ); 01115 } 01116 } 01117 } 01118 01119 //END 01120 01121 //BEGIN KateStyleListItem 01122 static const int BoxSize = 16; 01123 static const int ColorBtnWidth = 32; 01124 01125 KateStyleListItem::KateStyleListItem( QListViewItem *parent, const QString & stylename, 01126 KateAttribute *style, KateHlItemData *data ) 01127 : QListViewItem( parent, stylename ), 01128 ds( style ), 01129 st( data ) 01130 { 01131 initStyle(); 01132 } 01133 01134 KateStyleListItem::KateStyleListItem( QListView *parent, const QString & stylename, 01135 KateAttribute *style, KateHlItemData *data ) 01136 : QListViewItem( parent, stylename ), 01137 ds( style ), 01138 st( data ) 01139 { 01140 initStyle(); 01141 } 01142 01143 void KateStyleListItem::initStyle() 01144 { 01145 if (!st) 01146 is = ds; 01147 else 01148 { 01149 is = new KateAttribute (*ds); 01150 01151 if (st->isSomethingSet()) 01152 *is += *st; 01153 } 01154 } 01155 01156 void KateStyleListItem::updateStyle() 01157 { 01158 // nothing there, not update it, will crash 01159 if (!st) 01160 return; 01161 01162 if ( is->itemSet(KateAttribute::Weight) ) 01163 { 01164 if ( is->weight() != st->weight()) 01165 st->setWeight( is->weight() ); 01166 } 01167 01168 if ( is->itemSet(KateAttribute::Italic) ) 01169 { 01170 if ( is->italic() != st->italic()) 01171 st->setItalic( is->italic() ); 01172 } 01173 01174 if ( is->itemSet(KateAttribute::StrikeOut) ) 01175 { 01176 if ( is->strikeOut() != st->strikeOut()) 01177 01178 st->setStrikeOut( is->strikeOut() ); 01179 } 01180 01181 if ( is->itemSet(KateAttribute::Underline) ) 01182 { 01183 if ( is->underline() != st->underline()) 01184 st->setUnderline( is->underline() ); 01185 } 01186 01187 if ( is->itemSet(KateAttribute::Outline) ) 01188 { 01189 if ( is->outline() != st->outline()) 01190 st->setOutline( is->outline() ); 01191 } 01192 01193 if ( is->itemSet(KateAttribute::TextColor) ) 01194 { 01195 if ( is->textColor() != st->textColor()) 01196 st->setTextColor( is->textColor() ); 01197 } 01198 01199 if ( is->itemSet(KateAttribute::SelectedTextColor) ) 01200 { 01201 if ( is->selectedTextColor() != st->selectedTextColor()) 01202 st->setSelectedTextColor( is->selectedTextColor() ); 01203 } 01204 01205 if ( is->itemSet(KateAttribute::BGColor) ) 01206 { 01207 if ( is->bgColor() != st->bgColor()) 01208 st->setBGColor( is->bgColor() ); 01209 } 01210 01211 if ( is->itemSet(KateAttribute::SelectedBGColor) ) 01212 { 01213 if ( is->selectedBGColor() != st->selectedBGColor()) 01214 st->setSelectedBGColor( is->selectedBGColor() ); 01215 } 01216 } 01217 01218 /* only true for a hl mode item using it's default style */ 01219 bool KateStyleListItem::defStyle() { return st && st->itemsSet() != ds->itemsSet(); } 01220 01221 /* true for default styles */ 01222 bool KateStyleListItem::isDefault() { return st ? false : true; } 01223 01224 int KateStyleListItem::width( const QFontMetrics & /*fm*/, const QListView * lv, int col ) const 01225 { 01226 int m = lv->itemMargin() * 2; 01227 switch ( col ) { 01228 case ContextName: 01229 // FIXME: width for name column should reflect bold/italic 01230 // (relevant for non-fixed fonts only - nessecary?) 01231 return QListViewItem::width( QFontMetrics( ((KateStyleListView*)lv)->docfont), lv, col); 01232 case Bold: 01233 case Italic: 01234 case UseDefStyle: 01235 return BoxSize + m; 01236 case Color: 01237 case SelColor: 01238 case BgColor: 01239 case SelBgColor: 01240 return ColorBtnWidth +m; 01241 default: 01242 return 0; 01243 } 01244 } 01245 01246 void KateStyleListItem::activate( int column, const QPoint &localPos ) 01247 { 01248 QListView *lv = listView(); 01249 int x = 0; 01250 for( int c = 0; c < column-1; c++ ) 01251 x += lv->columnWidth( c ); 01252 int w; 01253 switch( column ) { 01254 case Bold: 01255 case Italic: 01256 case Underline: 01257 case Strikeout: 01258 case UseDefStyle: 01259 w = BoxSize; 01260 break; 01261 case Color: 01262 case SelColor: 01263 case BgColor: 01264 case SelBgColor: 01265 w = ColorBtnWidth; 01266 break; 01267 default: 01268 return; 01269 } 01270 if ( !QRect( x, 0, w, BoxSize ).contains( localPos ) ) 01271 changeProperty( (Property)column ); 01272 } 01273 01274 void KateStyleListItem::changeProperty( Property p ) 01275 { 01276 if ( p == Bold ) 01277 is->setBold( ! is->bold() ); 01278 else if ( p == Italic ) 01279 is->setItalic( ! is->italic() ); 01280 else if ( p == Underline ) 01281 is->setUnderline( ! is->underline() ); 01282 else if ( p == Strikeout ) 01283 is->setStrikeOut( ! is->strikeOut() ); 01284 else if ( p == UseDefStyle ) 01285 toggleDefStyle(); 01286 else 01287 setColor( p ); 01288 01289 updateStyle (); 01290 01291 ((KateStyleListView*)listView())->emitChanged(); 01292 } 01293 01294 void KateStyleListItem::toggleDefStyle() 01295 { 01296 if ( *is == *ds ) { 01297 KMessageBox::information( listView(), 01298 i18n("\"Use Default Style\" will be automatically unset when you change any style properties."), 01299 i18n("Kate Styles"), 01300 "Kate hl config use defaults" ); 01301 } 01302 else { 01303 delete is; 01304 is = new KateAttribute( *ds ); 01305 repaint(); 01306 } 01307 } 01308 01309 void KateStyleListItem::setColor( int column ) 01310 { 01311 QColor c; // use this 01312 QColor d; // default color 01313 if ( column == Color) 01314 { 01315 c = is->textColor(); 01316 d = ds->textColor(); 01317 } 01318 else if ( column == SelColor ) 01319 { 01320 c = is->selectedTextColor(); 01321 d = is->selectedTextColor(); 01322 } 01323 else if ( column == BgColor ) 01324 { 01325 c = is->bgColor(); 01326 d = ds->bgColor(); 01327 } 01328 else if ( column == SelBgColor ) 01329 { 01330 c = is->selectedBGColor(); 01331 d = ds->selectedBGColor(); 01332 } 01333 01334 if ( KColorDialog::getColor( c, d, listView() ) != QDialog::Accepted) return; 01335 01336 bool def = ! c.isValid(); 01337 01338 // if set default, and the attrib is set in the default style use it 01339 // else if set default, unset it 01340 // else set the selected color 01341 switch (column) 01342 { 01343 case Color: 01344 if ( def ) 01345 { 01346 if ( ds->itemSet(KateAttribute::TextColor) ) 01347 is->setTextColor( ds->textColor()); 01348 else 01349 is->clearAttribute(KateAttribute::TextColor); 01350 } 01351 else 01352 is->setTextColor( c ); 01353 break; 01354 case SelColor: 01355 if ( def ) 01356 { 01357 if ( ds->itemSet(KateAttribute::SelectedTextColor) ) 01358 is->setSelectedTextColor( ds->selectedTextColor()); 01359 else 01360 is->clearAttribute(KateAttribute::SelectedTextColor); 01361 } 01362 else 01363 is->setSelectedTextColor( c ); 01364 break; 01365 case BgColor: 01366 if ( def ) 01367 { 01368 if ( ds->itemSet(KateAttribute::BGColor) ) 01369 is->setBGColor( ds->bgColor()); 01370 else 01371 is->clearAttribute(KateAttribute::BGColor); 01372 } 01373 else 01374 is->setBGColor( c ); 01375 break; 01376 case SelBgColor: 01377 if ( def ) 01378 { 01379 if ( ds->itemSet(KateAttribute::SelectedBGColor) ) 01380 is->setSelectedBGColor( ds->selectedBGColor()); 01381 else 01382 is->clearAttribute(KateAttribute::SelectedBGColor); 01383 } 01384 else 01385 is->setSelectedBGColor( c ); 01386 break; 01387 } 01388 01389 repaint(); 01390 } 01391 01392 void KateStyleListItem::unsetColor( int c ) 01393 { 01394 if ( c == 100 && is->itemSet(KateAttribute::BGColor) ) 01395 is->clearAttribute(KateAttribute::BGColor); 01396 else if ( c == 101 && is->itemSet(KateAttribute::SelectedBGColor) ) 01397 is->clearAttribute(KateAttribute::SelectedBGColor); 01398 } 01399 01400 void KateStyleListItem::paintCell( QPainter *p, const QColorGroup& /*cg*/, int col, int width, int align ) 01401 { 01402 01403 if ( !p ) 01404 return; 01405 01406 QListView *lv = listView(); 01407 if ( !lv ) 01408 return; 01409 Q_ASSERT( lv ); //### 01410 01411 // use a private color group and set the text/highlighted text colors 01412 QColorGroup mcg = lv->viewport()->colorGroup(); 01413 01414 if ( col ) // col 0 is drawn by the superclass method 01415 p->fillRect( 0, 0, width, height(), QBrush( mcg.base() ) ); 01416 01417 int marg = lv->itemMargin(); 01418 01419 QColor c; 01420 01421 switch ( col ) 01422 { 01423 case ContextName: 01424 { 01425 mcg.setColor(QColorGroup::Text, is->textColor()); 01426 mcg.setColor(QColorGroup::HighlightedText, is->selectedTextColor()); 01427 // text background color 01428 c = is->bgColor(); 01429 if ( c.isValid() && is->itemSet(KateAttribute::BGColor) ) 01430 mcg.setColor( QColorGroup::Base, c ); 01431 if ( isSelected() && is->itemSet(KateAttribute::SelectedBGColor) ) 01432 { 01433 c = is->selectedBGColor(); 01434 if ( c.isValid() ) 01435 mcg.setColor( QColorGroup::Highlight, c ); 01436 } 01437 QFont f ( ((KateStyleListView*)lv)->docfont ); 01438 p->setFont( is->font(f) ); 01439 // FIXME - repainting when text is cropped, and the column is enlarged is buggy. 01440 // Maybe I need painting the string myself :( 01441 // (wilbert) it depends on the font used 01442 QListViewItem::paintCell( p, mcg, col, width, align ); 01443 } 01444 break; 01445 case Bold: 01446 case Italic: 01447 case Underline: 01448 case Strikeout: 01449 case UseDefStyle: 01450 { 01451 // Bold/Italic/use default checkboxes 01452 // code allmost identical to QCheckListItem 01453 int x = 0; 01454 int y = (height() - BoxSize) / 2; 01455 01456 if ( isEnabled() ) 01457 p->setPen( QPen( mcg.text(), 2 ) ); 01458 else 01459 p->setPen( QPen( lv->palette().color( QPalette::Disabled, QColorGroup::Text ), 2 ) ); 01460 01461 p->drawRect( x+marg, y+2, BoxSize-4, BoxSize-4 ); 01462 x++; 01463 y++; 01464 if ( (col == Bold && is->bold()) || 01465 (col == Italic && is->italic()) || 01466 (col == Underline && is->underline()) || 01467 (col == Strikeout && is->strikeOut()) || 01468 (col == UseDefStyle && *is == *ds ) ) 01469 { 01470 QPointArray a( 7*2 ); 01471 int i, xx, yy; 01472 xx = x+1+marg; 01473 yy = y+5; 01474 for ( i=0; i<3; i++ ) { 01475 a.setPoint( 2*i, xx, yy ); 01476 a.setPoint( 2*i+1, xx, yy+2 ); 01477 xx++; yy++; 01478 } 01479 yy -= 2; 01480 for ( i=3; i<7; i++ ) { 01481 a.setPoint( 2*i, xx, yy ); 01482 a.setPoint( 2*i+1, xx, yy+2 ); 01483 xx++; yy--; 01484 } 01485 p->drawLineSegments( a ); 01486 } 01487 } 01488 break; 01489 case Color: 01490 case SelColor: 01491 case BgColor: 01492 case SelBgColor: 01493 { 01494 bool set( false ); 01495 if ( col == Color) 01496 { 01497 c = is->textColor(); 01498 set = is->itemSet(KateAttribute::TextColor); 01499 } 01500 else if ( col == SelColor ) 01501 { 01502 c = is->selectedTextColor(); 01503 set = is->itemSet( KateAttribute::SelectedTextColor); 01504 } 01505 else if ( col == BgColor ) 01506 { 01507 set = is->itemSet(KateAttribute::BGColor); 01508 c = set ? is->bgColor() : mcg.base(); 01509 } 01510 else if ( col == SelBgColor ) 01511 { 01512 set = is->itemSet(KateAttribute::SelectedBGColor); 01513 c = set ? is->selectedBGColor(): mcg.base(); 01514 } 01515 01516 // color "buttons" 01517 int x = 0; 01518 int y = (height() - BoxSize) / 2; 01519 if ( isEnabled() ) 01520 p->setPen( QPen( mcg.text(), 2 ) ); 01521 else 01522 p->setPen( QPen( lv->palette().color( QPalette::Disabled, QColorGroup::Text ), 2 ) ); 01523 01524 p->drawRect( x+marg, y+2, ColorBtnWidth-4, BoxSize-4 ); 01525 p->fillRect( x+marg+1,y+3,ColorBtnWidth-7,BoxSize-7,QBrush( c ) ); 01526 // if this item is unset, draw a diagonal line over the button 01527 if ( ! set ) 01528 p->drawLine( x+marg-1, BoxSize-3, ColorBtnWidth-4, y+1 ); 01529 } 01530 //case default: // no warning... 01531 } 01532 } 01533 //END 01534 01535 //BEGIN KateStyleListCaption 01536 KateStyleListCaption::KateStyleListCaption( QListView *parent, const QString & name ) 01537 : QListViewItem( parent, name ) 01538 { 01539 } 01540 01541 void KateStyleListCaption::paintCell( QPainter *p, const QColorGroup& /*cg*/, int col, int width, int align ) 01542 { 01543 QListView *lv = listView(); 01544 if ( !lv ) 01545 return; 01546 Q_ASSERT( lv ); //### 01547 01548 // use the same colorgroup as the other items in the viewport 01549 QColorGroup mcg = lv->viewport()->colorGroup(); 01550 01551 QListViewItem::paintCell( p, mcg, col, width, align ); 01552 } 01553 //END 01554 01555 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:45:09 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003