diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-06-15 19:43:36 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-07-02 10:10:17 -0400 |
| commit | fc8d5c1708aa126bb3301079d15787943a0f47c9 (patch) | |
| tree | 574a75115823cbdb9e9dfad67c3acd6761028dab /src | |
| parent | 9f509f9aa2a1066a223bfd23fb54862f9c988b78 (diff) | |
checkable wordwrap on toolbar
Diffstat (limited to 'src')
| -rw-r--r-- | src/texteditor.cpp | 25 | ||||
| -rw-r--r-- | src/texteditor.h | 5 |
2 files changed, 19 insertions, 11 deletions
diff --git a/src/texteditor.cpp b/src/texteditor.cpp index 9de2b9c1..56ccfd2c 100644 --- a/src/texteditor.cpp +++ b/src/texteditor.cpp @@ -10,6 +10,8 @@ TextEditor::TextEditor(QPlainTextEdit* edit) m_edit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); wordWrap(true); + emit modified(false); + QObject::connect( m_edit->document(), &QTextDocument::modificationChanged, [&](bool b){ onModified(b); }); @@ -55,6 +57,13 @@ void TextEditor::wordWrap(bool b) } else { m_edit->setLineWrapMode(QPlainTextEdit::NoWrap); } + + emit wordWrapChanged(b); +} + +void TextEditor::toggleWordWrap() +{ + wordWrap(!wordWrap()); } bool TextEditor::wordWrap() const @@ -131,8 +140,10 @@ TextEditorToolbar::TextEditorToolbar(TextEditor& editor) : m_save(new QAction(QObject::tr("&Save"))), m_wordWrap(new QAction(QObject::tr("&Word wrap"))) { - QObject::connect(m_save, &QAction::triggered, [&]{ onSave(); }); - QObject::connect(m_wordWrap, &QAction::triggered, [&]{ onWordWrap(); }); + QObject::connect(m_save, &QAction::triggered, [&]{ m_editor.save(); }); + + m_wordWrap->setCheckable(true); + QObject::connect(m_wordWrap, &QAction::triggered, [&]{ m_editor.toggleWordWrap(); }); auto* layout = new QHBoxLayout(m_widget); layout->setContentsMargins(0, 0, 0, 0); @@ -147,6 +158,7 @@ TextEditorToolbar::TextEditorToolbar(TextEditor& editor) : layout->addWidget(b); QObject::connect(&m_editor, &TextEditor::modified, [&](bool b){ onTextModified(b); }); + QObject::connect(&m_editor, &TextEditor::wordWrapChanged, [&](bool b){ onWordWrap(b); }); } QWidget* TextEditorToolbar::widget() @@ -159,12 +171,7 @@ void TextEditorToolbar::onTextModified(bool b) m_save->setEnabled(b); } -void TextEditorToolbar::onSave() -{ - m_editor.save(); -} - -void TextEditorToolbar::onWordWrap() +void TextEditorToolbar::onWordWrap(bool b) { - m_editor.wordWrap(!m_editor.wordWrap()); + m_wordWrap->setChecked(b); } diff --git a/src/texteditor.h b/src/texteditor.h index 10f6c4de..c196b7a4 100644 --- a/src/texteditor.h +++ b/src/texteditor.h @@ -19,8 +19,7 @@ private: QAction* m_wordWrap; void onTextModified(bool b); - void onSave(); - void onWordWrap(); + void onWordWrap(bool b); }; @@ -37,12 +36,14 @@ public: const QString& filename() const; void wordWrap(bool b); + void toggleWordWrap(); bool wordWrap() const; bool dirty() const; signals: void modified(bool b); + void wordWrapChanged(bool b); private: QPlainTextEdit* m_edit; |
