Skip to content

Commit

Permalink
feat: add word wrap setting options.
Browse files Browse the repository at this point in the history
Change-Id: I6121f830c2ed47346b095d587a1c367d5bdb6bfd
  • Loading branch information
rekols committed Nov 27, 2018
1 parent 0eec631 commit b6b871a
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 68 deletions.
10 changes: 9 additions & 1 deletion src/dtextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ DTextEdit::DTextEdit(QPlainTextEdit *parent)

// configure content area
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &DTextEdit::adjustScrollbarMargins, Qt::QueuedConnection);

Expand Down Expand Up @@ -2001,6 +2001,14 @@ void DTextEdit::removeHighlightWordUnderCursor()
renderAllSelections();
}

void DTextEdit::setWordWrapMode(QTextOption::WrapMode policy)
{
setHorizontalScrollBarPolicy(policy == QTextOption::WordWrap ? Qt::ScrollBarAlwaysOff
: Qt::ScrollBarAlwaysOn);

QPlainTextEdit::setWordWrapMode(policy);
}

void DTextEdit::setSettings(Settings *keySettings)
{
m_settings = keySettings;
Expand Down
1 change: 1 addition & 0 deletions src/dtextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class DTextEdit : public QPlainTextEdit
bool highlightWordUnderMouse(QPoint pos);
void removeHighlightWordUnderCursor();

void setWordWrapMode(QTextOption::WrapMode policy);
void setSettings(Settings *settings);
void setModified(bool modified);

Expand Down
7 changes: 7 additions & 0 deletions src/resources/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
"default": 12,
"max": 50,
"min": 8
},
{
"key": "wordwrap",
"name": "",
"type": "checkbox",
"text": "Word Wrap",
"default": "true"
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Settings::Settings(QWidget *parent)
settings = DSettings::fromJsonFile(":/resources/settings.json");
settings->setBackend(m_backend);

auto wordWrap = settings->option("base.font.wordwrap");
connect(wordWrap, &Dtk::Core::DSettingsOption::valueChanged, this, [=] (QVariant value) {
emit adjustWordWrap(value.toBool());
});

auto fontSize = settings->option("base.font.size");
connect(fontSize, &Dtk::Core::DSettingsOption::valueChanged, this, [=] (QVariant value) {
emit adjustFontSize(value.toInt());
Expand Down
1 change: 1 addition & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Settings : public QObject
void adjustFont(QString name);
void adjustFontSize(int fontSize);
void adjustTabSpaceNumber(int number);
void adjustWordWrap(bool enable);

private:
void updateAllKeysWithKeymap(QString keymap);
Expand Down
2 changes: 2 additions & 0 deletions src/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ void GenerateSettingTranslate()
auto advance_windowName = QObject::tr("Window");
auto advance_window_windowstateName = QObject::tr("Window size");
auto advance_editor_tabspacenumberName = QObject::tr("Tab space");

auto base_font_wordwrapText = QObject::tr("Word Wrap");
}
8 changes: 8 additions & 0 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ Window::Window(DMainWindow *parent)
connect(m_settings, &Settings::adjustFont, this, &Window::updateFont);
connect(m_settings, &Settings::adjustFontSize, this, &Window::updateFontSize);
connect(m_settings, &Settings::adjustTabSpaceNumber, this, &Window::updateTabSpaceNumber);
connect(m_settings, &Settings::adjustWordWrap, this, [=] (bool enable) {
for (EditWrapper *wrapper : m_wrappers.values()) {
DTextEdit *textedit = wrapper->textEditor();
textedit->setWordWrapMode(enable ? QTextOption::WordWrap : QTextOption::NoWrap);
}
});

// Init layout and editor.
m_centralLayout->setMargin(0);
Expand Down Expand Up @@ -367,11 +373,13 @@ void Window::restoreTab()
EditWrapper* Window::createEditor()
{
EditWrapper *wrapper = new EditWrapper();
bool wordWrap = m_settings->settings->option("base.font.wordwrap")->value().toBool();
wrapper->textEditor()->setThemeWithPath(m_themePath);
wrapper->textEditor()->setSettings(m_settings);
wrapper->textEditor()->setTabSpaceNumber(m_settings->settings->option("advance.editor.tabspacenumber")->value().toInt());
wrapper->textEditor()->setFontFamily(m_settings->settings->option("base.font.family")->value().toString());
wrapper->textEditor()->setModified(false);
wrapper->textEditor()->setWordWrapMode(wordWrap ? QTextOption::WordWrap : QTextOption::NoWrap);
setFontSizeWithConfig(wrapper);

connect(wrapper->textEditor(), &DTextEdit::clickFindAction, this, &Window::popupFindBar, Qt::QueuedConnection);
Expand Down
2 changes: 1 addition & 1 deletion src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public slots:
int m_remberPositionScrollOffset;

QString m_blankFileDir;
int m_fontSize;
int m_fontSize = 0;

QString m_titlebarStyleSheet;

Expand Down
Loading

0 comments on commit b6b871a

Please sign in to comment.