From ab0d80d349cf6dda2da00cdbf38d73874b4de08b Mon Sep 17 00:00:00 2001 From: Matthias Pohl Date: Fri, 1 Apr 2016 17:12:55 +0200 Subject: [PATCH 1/3] Add option to insert spaces instead of tabs --- app/mainwindow.cpp | 3 +++ app/markdowneditor.cpp | 5 +++++ app/markdowneditor.h | 2 ++ app/options.cpp | 15 +++++++++++++++ app/options.h | 5 +++++ app/optionsdialog.cpp | 2 ++ app/optionsdialog.ui | 23 +++++++++++++++++++++++ 7 files changed, 55 insertions(+) diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index 0b7f059b..ba75f7a3 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -138,6 +138,7 @@ void MainWindow::initializeApp() lastUsedTheme(); ui->plainTextEdit->tabWidthChanged(options->tabWidth()); + ui->plainTextEdit->spacesForTabsChanged(options->isSpacesForTabsEnabled()); ui->plainTextEdit->rulerEnabledChanged(options->isRulerEnabled()); ui->plainTextEdit->rulerPosChanged(options->rulerPos()); @@ -1055,6 +1056,8 @@ void MainWindow::setupMarkdownEditor() ui->plainTextEdit, &MarkdownEditor::editorFontChanged); connect(options, &Options::tabWidthChanged, ui->plainTextEdit, &MarkdownEditor::tabWidthChanged); + connect(options, &Options::spacesForTabsChanged, + ui->plainTextEdit, &MarkdownEditor::spacesForTabsChanged); connect(options, &Options::rulerEnabledChanged, ui->plainTextEdit, &MarkdownEditor::rulerEnabledChanged); connect(options, &Options::rulerPosChanged, diff --git a/app/markdowneditor.cpp b/app/markdowneditor.cpp index 3ea9e4bd..bddaf5c0 100644 --- a/app/markdowneditor.cpp +++ b/app/markdowneditor.cpp @@ -257,6 +257,11 @@ void MarkdownEditor::tabWidthChanged(int tabWidth) setTabStopWidth(tabWidth*fm.width(' ')); } +void MarkdownEditor::spacesForTabsChanged(bool enabled) +{ + insertSpacesForTabs = enabled; +} + void MarkdownEditor::rulerEnabledChanged(bool enabled) { rulerEnabled = enabled; diff --git a/app/markdowneditor.h b/app/markdowneditor.h index 88d5b513..fe4aaa65 100644 --- a/app/markdowneditor.h +++ b/app/markdowneditor.h @@ -63,6 +63,7 @@ class MarkdownEditor : public QPlainTextEdit public slots: void tabWidthChanged(int tabWidth); + void spacesForTabsChanged(bool enabled); void editorFontChanged(const QFont &font); void rulerEnabledChanged(bool enabled); void rulerPosChanged(int pos); @@ -95,6 +96,7 @@ private slots: bool showHardLinebreaks; bool rulerEnabled; int rulerPos; + bool insertSpacesForTabs; }; #endif // MARKDOWNEDITOR_H diff --git a/app/options.cpp b/app/options.cpp index 14ba8da0..09046212 100644 --- a/app/options.cpp +++ b/app/options.cpp @@ -26,6 +26,7 @@ static const char* FONT_FAMILY_DEFAULT = "Monospace"; static const char* FONT_FAMILY = "editor/font/family"; static const char* FONT_SIZE = "editor/font/size"; static const char* TAB_WIDTH = "editor/tabwidth"; +static const char* SPACESFORTABS_ENABLED = "editor/spacesfortabs/enabled"; static const char* LINECOLUMN_ENABLED = "editor/linecolumn/enabled"; static const char* RULER_ENABLED = "editor/ruler/enabled"; static const char* RULER_POS = "editor/ruler/pos"; @@ -80,6 +81,7 @@ Options::Options(QObject *parent) : m_sourceAtSingleSizeEnabled(true), m_spellingCheckEnabled(true), m_diagramSupportEnabled(false), + m_spacesForTabsEnabled(false), m_lineColumnEnabled(true), m_rulerEnabled(false), m_rulerPos(80), @@ -127,6 +129,17 @@ void Options::setTabWidth(int width) emit tabWidthChanged(width); } +bool Options::isSpacesForTabsEnabled() const +{ + return m_spacesForTabsEnabled; +} + +void Options::setSpacesForTabsEnabled(bool enabled) +{ + m_spacesForTabsEnabled = enabled; + emit spacesForTabsChanged(enabled); +} + bool Options::isLineColumnEnabled() const { return m_lineColumnEnabled; @@ -493,6 +506,7 @@ void Options::readSettings() int fontSize = settings.value(FONT_SIZE, 10).toInt(); m_tabWidth = settings.value(TAB_WIDTH, 8).toInt(); + m_spacesForTabsEnabled = settings.value(SPACESFORTABS_ENABLED, false).toBool(); m_lineColumnEnabled = settings.value(LINECOLUMN_ENABLED, false).toBool(); m_rulerEnabled = settings.value(RULER_ENABLED, false).toBool(); m_rulerPos = settings.value(RULER_POS, 80).toInt(); @@ -567,6 +581,7 @@ void Options::writeSettings() settings.setValue(FONT_FAMILY, font.family()); settings.setValue(FONT_SIZE, font.pointSize()); settings.setValue(TAB_WIDTH, m_tabWidth); + settings.setValue(SPACESFORTABS_ENABLED, m_spacesForTabsEnabled); settings.setValue(LINECOLUMN_ENABLED, m_lineColumnEnabled); settings.setValue(RULER_ENABLED, m_rulerEnabled); settings.setValue(RULER_POS, m_rulerPos); diff --git a/app/options.h b/app/options.h index 68d26307..76ff7860 100644 --- a/app/options.h +++ b/app/options.h @@ -45,6 +45,9 @@ class Options : public QObject int tabWidth() const; void setTabWidth(int width); + bool isSpacesForTabsEnabled() const; + void setSpacesForTabsEnabled(bool enabled); + bool isLineColumnEnabled() const; void setLineColumnEnabled(bool enabled); @@ -159,6 +162,7 @@ class Options : public QObject void editorFontChanged(const QFont &font); void editorStyleChanged(); void tabWidthChanged(int tabWidth); + void spacesForTabsChanged(bool enabled); void lineColumnEnabledChanged(bool enabled); void rulerEnabledChanged(bool enabled); void rulerPosChanged(int pos); @@ -193,6 +197,7 @@ class Options : public QObject bool m_spellingCheckEnabled; bool m_yamlHeaderSupportEnabled; bool m_diagramSupportEnabled; + bool m_spacesForTabsEnabled; bool m_lineColumnEnabled; bool m_rulerEnabled; int m_rulerPos; diff --git a/app/optionsdialog.cpp b/app/optionsdialog.cpp index 21508aa2..074431a2 100644 --- a/app/optionsdialog.cpp +++ b/app/optionsdialog.cpp @@ -287,6 +287,7 @@ void OptionsDialog::readState() ui->sizeComboBox->setCurrentText(QString().setNum(font.pointSize())); ui->sourceSingleSizedCheckBox->setChecked(options->isSourceAtSingleSizeEnabled()); ui->tabWidthSpinBox->setValue(options->tabWidth()); + ui->spacesForTabsCheckBox->setChecked(options->isSpacesForTabsEnabled()); ui->lineColumnCheckBox->setChecked(options->isLineColumnEnabled()); ui->rulerEnableCheckBox->setChecked(options->isRulerEnabled()); ui->rulerPosSpinBox->setValue(options->rulerPos()); @@ -337,6 +338,7 @@ void OptionsDialog::saveState() options->setEditorFont(font); options->setSourceAtSingleSizeEnabled(ui->sourceSingleSizedCheckBox->isChecked()); options->setTabWidth(ui->tabWidthSpinBox->value()); + options->setSpacesForTabsEnabled(ui->spacesForTabsCheckBox->isChecked()); options->setLineColumnEnabled(ui->lineColumnCheckBox->isChecked()); options->setRulerEnabled(ui->rulerEnableCheckBox->isChecked()); options->setRulerPos(ui->rulerPosSpinBox->value()); diff --git a/app/optionsdialog.ui b/app/optionsdialog.ui index c95be62d..b58e8409 100644 --- a/app/optionsdialog.ui +++ b/app/optionsdialog.ui @@ -193,6 +193,29 @@ + + + + Qt::Horizontal + + + QSizePolicy::Minimum + + + + 40 + 6 + + + + + + + + Insert Spacesfor Tabs + + + From 65f8d3cedadf28239498ad539d534eb6fcdd0afb Mon Sep 17 00:00:00 2001 From: Matthias Pohl Date: Fri, 1 Apr 2016 18:29:15 +0200 Subject: [PATCH 2/3] Crude implementation for indent with spaces --- app/markdowneditor.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/markdowneditor.cpp b/app/markdowneditor.cpp index bddaf5c0..f9df4ef5 100644 --- a/app/markdowneditor.cpp +++ b/app/markdowneditor.cpp @@ -189,6 +189,31 @@ void MarkdownEditor::resizeEvent(QResizeEvent *event) void MarkdownEditor::keyPressEvent(QKeyEvent *e) { + if(insertSpacesForTabs) + { + if(Qt::Key_Tab == e->key()) + { + QTextCursor cursor = textCursor(); + QFontMetrics fm(font()); + int tabwidth = this->tabStopWidth() / fm.width(' '); + + cursor.beginEditBlock(); + int currentPosition = cursor.position(); + cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); + int indentToAdd = cursor.selectedText().length() % tabwidth; + indentToAdd = (0 == indentToAdd) ? tabwidth : tabwidth - indentToAdd; + cursor.setPosition(currentPosition); + for(int i = 0; i < indentToAdd; ++i) + { + cursor.insertText(" "); + } + cursor.endEditBlock(); + + e->accept(); + return; + } + } + if (completer && completer->isPopupVisible()) { // The following keys are forwarded by the completer to the widget switch (e->key()) { From 5a10f57edbe8f98ad68bfdbf2e95f0853fd349ff Mon Sep 17 00:00:00 2001 From: Matthias Pohl Date: Fri, 1 Apr 2016 18:44:26 +0200 Subject: [PATCH 3/3] Fix spelling --- app/optionsdialog.ui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/optionsdialog.ui b/app/optionsdialog.ui index b58e8409..71cb9782 100644 --- a/app/optionsdialog.ui +++ b/app/optionsdialog.ui @@ -212,7 +212,7 @@ - Insert Spacesfor Tabs + Insert Spaces for Tabs