# HG changeset patch # User Torsten # Date 1364766971 -7200 # Node ID 7fa90eb412402cfb40dae8adc508c4cdf9107945 # Parent 2a294ee8efc0fd6409a8cc2f9b88d07312c79583 gui: individually configurable styles of the editor lexers * color-picker.cc/.h: new class for a simple color picker * libgui/src/module.mk: added color-picker.cc/.h * file-editor-tab.cc(update_lexer): read config of the lexer from settings file * file-editor-tab(notice_settings): editor font not directly configured from the settings file but via the lexer settings in update_lexer * lexer-octave-gui.cc(constructor): inherits from QsciLexerOctave instaead of QsciLexer, code cleanup * lexer-octave-gui.cc(language,lexer,defaultFont,defaultColor,description): removed, original methods used and configured via settings * lexer-octave-gui.h: removed functions language,lexer,defaultFont,defaultColor, description and enum; using methods of octave lexer instead * settings-dialog.cc(constructor): editor font not configured from settings, defining lexers for supported languages and reading their actual settings from the settings file (via read_lexer_settings) * settings-dialog.cc(read_lexer_settings): new function for reading the lexers settings from settings file and creating the tabs with interactive elements for configuring these settings * settings-dialog.cc(write_changed_settings): remove settings for editor font, defining lexers for supported languages and writing the settings from the dialog into the settings file (via write_lexer_settings) * settings-dialog.cc(write_lexer_settings): new funciton for getting the new lexer settings from the interactive elements and write them into the settings file * settings-dialog.h: new functiond read_lexer_settings, write_lexer_settings * settings-dialog.ui: new tab for the editor styles which contents is dynamically created in settings_dialog () diff --git a/libgui/src/color-picker.cc b/libgui/src/color-picker.cc new file mode 100644 --- /dev/null +++ b/libgui/src/color-picker.cc @@ -0,0 +1,55 @@ +// +// This class provides a simple color picker based on tQColorButton +// by Harald Jedele, 23.03.01, GPL version 2 or any later version. +// +// Copyright (C) FZI Forschungszentrum Informatik Karlsruhe +// Copyright (C) 2013 Torsten +// +// This file is part of Octave. +// +// Octave is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3 of the License, or (at your +// option) any later version. +// +// Octave is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License +// along with Octave; see the file COPYING. If not, see +// . +// + +#include "color-picker.h" + +// constuctor with initial color as parameter +color_picker::color_picker (QColor old_color, QWidget* p) : QPushButton (p) +{ + _color = old_color; + setFlat (true); + update_button (); + connect(this, SIGNAL (clicked ()), SLOT (select_color ())); +} + +// slot for bitton clicked: selct a new color using QColorDialog +void +color_picker::select_color () +{ + QColor new_color = QColorDialog::getColor (_color); + if (new_color.isValid () && new_color != _color) + { + _color = new_color; + update_button (); + } +} + +// draw the button with the actual color (using a stylesheet) +void color_picker::update_button () +{ + QString css = QString("background-color: %1; border: none;" ) + .arg(_color.name()); + setStyleSheet(css); + repaint (); +} diff --git a/libgui/src/color-picker.h b/libgui/src/color-picker.h new file mode 100644 --- /dev/null +++ b/libgui/src/color-picker.h @@ -0,0 +1,47 @@ +// +// This class provides a simple color picker based on tQColorButton +// by Harald Jedele, 23.03.01, GPL version 2 or any later version. +// +// Copyright (C) FZI Forschungszentrum Informatik Karlsruhe +// Copyright (C) 2013 Torsten +// +// This file is part of Octave. +// +// Octave is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3 of the License, or (at your +// option) any later version. +// +// Octave is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License +// along with Octave; see the file COPYING. If not, see +// . +// + +#ifndef COLORPICKER_H +#define COLORPICKER_H + +#include +#include + +class color_picker: public QPushButton +{ + Q_OBJECT + +public: + color_picker (QColor color = QColor(0,0,0), QWidget *parent = 0); + QColor color () const { return _color; } + +private slots: + void select_color (); + +private: + virtual void update_button (); + QColor _color; +}; + +#endif diff --git a/libgui/src/m-editor/file-editor-tab.cc b/libgui/src/m-editor/file-editor-tab.cc --- a/libgui/src/m-editor/file-editor-tab.cc +++ b/libgui/src/m-editor/file-editor-tab.cc @@ -242,21 +242,9 @@ } QSettings *settings = resource_manager::get_settings (); + lexer->readSettings (*settings); + _edit_area->setLexer (lexer); - // Editor font (default or from settings) - if (settings) - lexer->setDefaultFont (QFont ( - settings->value ("editor/fontName", - "Courier New").toString (), - settings->value ("editor/fontSize", - 10).toInt ())); - - // TODO: Autoindent not working as it should - lexer->setAutoIndentStyle (QsciScintilla::AiMaintain || - QsciScintilla::AiOpening || - QsciScintilla::AiClosing); - - _edit_area->setLexer (lexer); } // slot for fetab_set_focus: sets the focus to the current edit area @@ -1017,6 +1005,8 @@ void file_editor_tab::notice_settings () { + update_lexer (); + QFontMetrics lexer_font_metrics (_edit_area->lexer ()->defaultFont (0)); QSettings *settings = resource_manager::get_settings (); if (settings==NULL) @@ -1029,15 +1019,10 @@ else _edit_area->setAutoCompletionThreshold (-1); - QFont xfont (settings->value ("editor/fontName","Courier New").toString (), - settings->value ("editor/fontSize",10).toInt ()); - if (settings->value ("editor/showLineNumbers",true).toBool ()) { _edit_area->setMarginLineNumbers (2, true); - _edit_area->setMarginsFont (xfont); - QFontMetrics metrics (xfont); - _edit_area->setMarginWidth(2, metrics.width("9999")); + _edit_area->setMarginWidth(2, lexer_font_metrics.width("9999")); } else { @@ -1045,8 +1030,6 @@ _edit_area->setMarginWidth(2, 0); } - update_lexer (); - _long_title = settings->value ("editor/longWindowTitle",false).toBool (); update_window_title (false); diff --git a/libgui/src/m-editor/lexer-octave-gui.cc b/libgui/src/m-editor/lexer-octave-gui.cc --- a/libgui/src/m-editor/lexer-octave-gui.cc +++ b/libgui/src/m-editor/lexer-octave-gui.cc @@ -34,33 +34,27 @@ // Some basic functions // ----------------------------------------------------- lexer_octave_gui::lexer_octave_gui (QObject *p) - : QsciLexer (p) + : QsciLexerOctave (p) { // The API info that is used for auto completion // TODO: Where to store a file with API info (raw or prepared?)? // TODO: Also provide infos on octave-forge functions? // TODO: Also provide infos on function parameters? // By now, use the keywords-list from syntax highlighting - QString keyword; QStringList keywordList; - - // get whole string with all keywords - keyword = this->keywords (1); - // split into single strings - keywordList = keyword.split (QRegExp ("\\s+")); - + keyword = this->keywords (1); // get whole string with all keywords + keywordList = keyword.split (QRegExp ("\\s+")); // split into single strings lexer_api = new QsciAPIs (this); if (lexer_api) { - for (int i = 0; i < keywordList.size (); i++) - { - // add single strings to the API - lexer_api->add (keywordList.at (i)); - } - // prepare API info ... this may take some time - lexer_api->prepare (); + for (int i = 0; i < keywordList.size (); i++) // add all keywords to API + lexer_api->add (keywordList.at (i)); + lexer_api->prepare (); // prepare API info ... this may take some time } + + // get the settings from the settings file + QSettings *settings = resource_manager::get_settings (); } lexer_octave_gui::~lexer_octave_gui() @@ -69,111 +63,14 @@ delete lexer_api; } -const char *lexer_octave_gui::language() const -{ - return "Octave"; // return the name of the language -} - -const char *lexer_octave_gui::lexer() const -{ - return "octave"; // return the name of the lexer -} - -// ----------------------------------------------------- -// The colors for syntax highlighting -// ----------------------------------------------------- -QColor lexer_octave_gui::defaultColor(int style) const -{ - switch (style) - { - case Default: // black - return QColor(0x00,0x00,0x00); - case Operator: // red - return QColor(0xef,0x00,0x00); - case Comment: // gray - return QColor(0x7f,0x7f,0x7f); - case Command: // blue-green - return QColor(0x00,0x7f,0x7f); - case Number: // orange - return QColor(0x7f,0x7f,0x00); - case Keyword: // blue - return QColor(0x00,0x00,0xbf); - case SingleQuotedString: // green - return QColor(0x00,0x7f,0x00); - case DoubleQuotedString: // green-yellow - return QColor(0x4f,0x7f,0x00); - } - return QsciLexer::defaultColor(style); -} - - -// ----------------------------------------------------- -// The font decorations for highlighting -// ----------------------------------------------------- -QFont lexer_octave_gui::defaultFont(int style) const -{ - QFont f; - - switch (style) - { - case Comment: // default but italic - f = QsciLexer::defaultFont(style); - f.setItalic(true); - break; - case Keyword: // default - f = QsciLexer::defaultFont(style); - break; - case Operator: // default - f = QsciLexer::defaultFont(style); - break; - default: // default - f = QsciLexer::defaultFont(style); - break; - } - return f; // return the selected font -} - - -// ----------------------------------------------------- -// Style names -// ----------------------------------------------------- -QString lexer_octave_gui::description(int style) const -{ - switch (style) - { - case Default: - return tr("Default"); - case Comment: - return tr("Comment"); - case Command: - return tr("Command"); - case Number: - return tr("Number"); - case Keyword: - return tr("Keyword"); - case SingleQuotedString: - return tr("Single-quoted string"); - case Operator: - return tr("Operator"); - case Identifier: - return tr("Identifier"); - case DoubleQuotedString: - return tr("Double-quoted string"); - } - return QString(); -} - - // ----------------------------------------------------- // The set of keywords for highlighting -// TODO: How to define a second set? // ----------------------------------------------------- const char *lexer_octave_gui::keywords(int set) const { if (set == 1) - { return resource_manager::octave_keywords (); - } + return 0; } diff --git a/libgui/src/m-editor/lexer-octave-gui.h b/libgui/src/m-editor/lexer-octave-gui.h --- a/libgui/src/m-editor/lexer-octave-gui.h +++ b/libgui/src/m-editor/lexer-octave-gui.h @@ -26,36 +26,18 @@ #include "resource-manager.h" #include #include -#include +#include #include -class lexer_octave_gui : public QsciLexer +class lexer_octave_gui : public QsciLexerOctave { Q_OBJECT - public: - // the used styles - enum - { - Default = 0, - Comment = 1, - Command = 2, - Number = 3, - Keyword = 4, - SingleQuotedString = 5, - Operator = 6, - Identifier = 7, - DoubleQuotedString = 8 - }; +public: lexer_octave_gui (QObject *parent = 0); ~lexer_octave_gui (); - const char *language () const; - const char *lexer () const; - QColor defaultColor (int style) const; - QFont defaultFont (int style) const; - const char *keywords (int set) const; - QString description (int style) const; + virtual const char *keywords (int set) const; private: lexer_octave_gui (const lexer_octave_gui &); diff --git a/libgui/src/module.mk b/libgui/src/module.mk --- a/libgui/src/module.mk +++ b/libgui/src/module.mk @@ -74,6 +74,7 @@ src/moc-octave-qt-event-listener.cc \ src/moc-settings-dialog.cc \ src/moc-terminal-dockwidget.cc \ + src/moc-color-picker.cc \ src/moc-welcome-wizard.cc \ src/moc-workspace-model.cc \ src/moc-workspace-view.cc \ @@ -114,6 +115,7 @@ src/settings-dialog.h \ src/symbol-information.h \ src/terminal-dockwidget.h \ + src/color-picker.h \ src/welcome-wizard.h \ src/workspace-model.h \ src/workspace-view.h @@ -137,6 +139,7 @@ src/settings-dialog.cc \ src/symbol-information.cc \ src/terminal-dockwidget.cc \ + src/color-picker.cc \ src/welcome-wizard.cc \ src/workspace-model.cc \ src/workspace-view.cc diff --git a/libgui/src/settings-dialog.cc b/libgui/src/settings-dialog.cc --- a/libgui/src/settings-dialog.cc +++ b/libgui/src/settings-dialog.cc @@ -31,6 +31,16 @@ #include #include +#ifdef HAVE_QSCINTILLA +#include +#include "color-picker.h" +#include +#include +#include +#include +#include +#endif + settings_dialog::settings_dialog (QWidget *p): QDialog (p), ui (new Ui::settings_dialog) { @@ -71,8 +81,6 @@ ui->editor_showLineNumbers->setChecked (settings->value ("editor/showLineNumbers",true).toBool () ); ui->editor_highlightCurrentLine->setChecked (settings->value ("editor/highlightCurrentLine",true).toBool () ); ui->editor_codeCompletion->setChecked (settings->value ("editor/codeCompletion",true).toBool () ); - ui->editor_fontName->setCurrentFont (QFont (settings->value ("editor/fontName","Courier New").toString()) ); - ui->editor_fontSize->setValue (settings->value ("editor/fontSize",10).toInt ()); ui->editor_longWindowTitle->setChecked (settings->value ("editor/longWindowTitle",false).toBool ()); ui->editor_restoreSession->setChecked (settings->value ("editor/restoreSession",true).toBool ()); ui->terminal_fontName->setCurrentFont (QFont (settings->value ("terminal/fontName","Courier New").toString()) ); @@ -114,6 +122,29 @@ ui->proxyPort->setText (settings->value ("proxyPort").toString ()); ui->proxyUserName->setText (settings->value ("proxyUserName").toString ()); ui->proxyPassword->setText (settings->value ("proxyPassword").toString ()); + +#ifdef HAVE_QSCINTILLA + // editor styles: create lexer, read settings, and create dialog elements + QsciLexer *lexer; + lexer = new lexer_octave_gui (); + read_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerCPP (); + read_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerPerl (); + read_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerBatch (); + read_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerDiff (); + read_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerBash (); + read_lexer_settings (lexer,settings); + delete lexer; +#endif } settings_dialog::~settings_dialog () @@ -121,6 +152,80 @@ delete ui; } + +#ifdef HAVE_QSCINTILLA +void +settings_dialog::read_lexer_settings (QsciLexer *lexer, QSettings *settings) +{ + lexer->readSettings (*settings); + int styles = 0; + while (lexer->description(styles) != "") + styles++; + QGridLayout *style_grid = new QGridLayout (); + QLabel *description[styles]; + QFontComboBox *select_font[styles]; + QSpinBox *font_size[styles]; + QCheckBox *attrib_font[3][styles]; + color_picker *color[styles]; + int default_size = 10; + QFont default_font = QFont (); + for (int i = 0; i < styles; i++) // create dialog elements for all styles + { + QString actual_name = lexer->description (i); + QFont actual_font = lexer->font (i); + description[i] = new QLabel (actual_name); + select_font[i] = new QFontComboBox (); + select_font[i]->setObjectName (actual_name+"_font"); + font_size[i] = new QSpinBox (); + font_size[i]->setObjectName (actual_name+"_size"); + if (i == 0) // the default + { + select_font[i]->setCurrentFont (actual_font); + default_font = actual_font; + font_size[i]->setRange (6,24); + default_size = actual_font.pointSize (); + font_size[i]->setValue (default_size); + } + else // other styles + { + select_font[i]->setCurrentFont (actual_font); + if (actual_font.family () == default_font.family ()) + select_font[i]->setEditText (lexer->description (0)); + font_size[i]->setRange (-4,4); + font_size[i]->setValue (actual_font.pointSize ()-default_size); + font_size[i]->setToolTip ("Difference to the defalt size"); + } + attrib_font[0][i] = new QCheckBox (tr("b")); + attrib_font[1][i] = new QCheckBox (tr("i")); + attrib_font[2][i] = new QCheckBox (tr("u")); + attrib_font[0][i]->setChecked(Qt::Checked && actual_font.bold ()); + attrib_font[0][i]->setObjectName (actual_name+"_bold"); + attrib_font[1][i]->setChecked(Qt::Checked && actual_font.italic ()); + attrib_font[1][i]->setObjectName (actual_name+"_italic"); + attrib_font[2][i]->setChecked(Qt::Checked && actual_font.underline ()); + attrib_font[2][i]->setObjectName (actual_name+"_underline"); + color[i] = new color_picker (lexer->color (i)); + color[i]->setObjectName (actual_name+"_color"); + int column = 1; + style_grid->addWidget (description[i], i,column++); + style_grid->addWidget (select_font[i], i,column++); + style_grid->addWidget (font_size[i], i,column++); + style_grid->addWidget (attrib_font[0][i],i,column++); + style_grid->addWidget (attrib_font[1][i],i,column++); + style_grid->addWidget (attrib_font[2][i],i,column++); + style_grid->addWidget (color[i], i,column++); + } + // place grid with elements into the tab + QScrollArea *scroll_area = new QScrollArea (); + QWidget *scroll_area_contents = new QWidget (); + scroll_area_contents->setObjectName (QString (lexer->language ())+"_styles"); + scroll_area_contents->setLayout (style_grid); + scroll_area->setWidget (scroll_area_contents); + ui->tabs_editor_styles->addTab (scroll_area,lexer->language ()); +} +#endif + + void settings_dialog::write_changed_settings () { @@ -147,8 +252,6 @@ settings->setValue ("editor/showLineNumbers", ui->editor_showLineNumbers->isChecked ()); settings->setValue ("editor/highlightCurrentLine", ui->editor_highlightCurrentLine->isChecked ()); settings->setValue ("editor/codeCompletion", ui->editor_codeCompletion->isChecked ()); - settings->setValue ("editor/fontName", ui->editor_fontName->currentFont().family()); - settings->setValue ("editor/fontSize", ui->editor_fontSize->value()); settings->setValue ("editor/longWindowTitle", ui->editor_longWindowTitle->isChecked()); settings->setValue ("editor/restoreSession", ui->editor_restoreSession->isChecked ()); settings->setValue ("terminal/fontSize", ui->terminal_fontSize->value()); @@ -177,4 +280,87 @@ } settings->setValue ("terminal/cursorType", cursorType); settings->sync (); + +#ifdef HAVE_QSCINTILLA + // editor styles: create lexer, get dialog contents, and write settings + QsciLexer *lexer; + lexer = new lexer_octave_gui (); + write_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerCPP (); + write_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerPerl (); + write_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerBatch (); + write_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerDiff (); + write_lexer_settings (lexer,settings); + delete lexer; + lexer = new QsciLexerBash (); + write_lexer_settings (lexer,settings); + delete lexer; +#endif } + +#ifdef HAVE_QSCINTILLA +void +settings_dialog::write_lexer_settings (QsciLexer *lexer, QSettings *settings) +{ + QWidget *tab = ui->tabs_editor_styles-> + findChild (QString (lexer->language ())+"_styles"); + int styles = 0; + while (lexer->description(styles) != "") + styles++; + QFontComboBox *select_font; + QSpinBox *font_size; + QCheckBox *attrib_font[3]; + color_picker *color; + int default_size = 10; + QFont default_font = QFont ("Courier New",10,-1,0); + for (int i = 0; i < styles; i++) // get dialog elements and their contents + { + QString actual_name = lexer->description (i); + select_font = tab->findChild (actual_name+"_font"); + font_size = tab->findChild (actual_name+"_size"); + attrib_font[0] = tab->findChild (actual_name+"_bold"); + attrib_font[1] = tab->findChild (actual_name+"_italic"); + attrib_font[2] = tab->findChild (actual_name+"_underline"); + color = tab->findChild (actual_name+"_color"); + QFont new_font = default_font; + if (select_font) + { + new_font = select_font->currentFont (); + if (i == 0) + default_font = new_font; + else + if (select_font->currentText () == lexer->description (0)) + new_font = default_font; + } + if (font_size) + { + if (i == 0) + { + default_size = font_size->value (); + new_font.setPointSize (font_size->value ()); + } + else + new_font.setPointSize (font_size->value ()+default_size); + } + if (attrib_font[0]) + new_font.setBold (attrib_font[0]->isChecked ()); + if (attrib_font[1]) + new_font.setItalic (attrib_font[1]->isChecked ()); + if (attrib_font[2]) + new_font.setUnderline (attrib_font[2]->isChecked ()); + lexer->setFont (new_font,i); + if (i == 0) + lexer->setDefaultFont (new_font); + if (color) + lexer->setColor (color->color (),i); + } + lexer->writeSettings (*settings); +} +#endif diff --git a/libgui/src/settings-dialog.h b/libgui/src/settings-dialog.h --- a/libgui/src/settings-dialog.h +++ b/libgui/src/settings-dialog.h @@ -24,6 +24,9 @@ #define SETTINGSDIALOG_H #include +#ifdef HAVE_QSCINTILLA +#include "lexer-octave-gui.h" +#endif namespace Ui { @@ -39,6 +42,10 @@ private: Ui::settings_dialog * ui; +#ifdef HAVE_QSCINTILLA + void read_lexer_settings (QsciLexer *lexer, QSettings *settings); + void write_lexer_settings (QsciLexer *lexer, QSettings *settings); +#endif }; #endif // SETTINGSDIALOG_H diff --git a/libgui/src/settings-dialog.ui b/libgui/src/settings-dialog.ui --- a/libgui/src/settings-dialog.ui +++ b/libgui/src/settings-dialog.ui @@ -9,20 +9,20 @@ 0 0 - 600 - 400 + 700 + 480 - 600 - 400 + 700 + 480 - 600 - 400 + 700 + 480 @@ -157,58 +157,7 @@ - - - - - Font - - - - - - - false - - - QFontComboBox::MonospacedFonts - - - - - - - Font Size - - - - - - - 2 - - - 96 - - - 10 - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - + @@ -298,6 +247,21 @@ + + + Editor Styles + + + + + 2 + 9 + 671 + 381 + + + + Terminal