Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle for whitespace #138

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/textviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ TextViewer::TextViewer(const QString& title, QWidget* parent)
ui->setupUi(this);
setWindowTitle(title);
m_EditorTabs = findChild<QTabWidget*>("editorTabs");
connect(ui->showWhitespace, SIGNAL(stateChanged(int)), this,
SLOT(showWhitespaceChanged(int)));
}

TextViewer::~TextViewer()
Expand Down Expand Up @@ -120,6 +122,25 @@ void TextViewer::findNext()
}
}

void TextViewer::showWhitespaceChanged(int state)
{
for (int i = 0; i < m_EditorTabs->count(); ++i) {
QTextEdit* editor = m_EditorTabs->widget(i)->findChild<QTextEdit*>();
if (editor != nullptr) {
auto document = editor->document();
auto textOption = document->defaultTextOption();
auto flags = textOption.flags();
if (state == Qt::Unchecked)
flags = flags & (~QTextOption::ShowTabsAndSpaces);
else
flags = flags | QTextOption::ShowTabsAndSpaces;
textOption.setFlags(flags);
document->setDefaultTextOption(textOption);
editor->setDocument(document);
}
}
}

bool TextViewer::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::ShortcutOverride) {
Expand Down Expand Up @@ -213,9 +234,11 @@ void TextViewer::addFile(const QString& fileName, bool writable)
QVBoxLayout* layout = new QVBoxLayout(page);
QTextEdit* editor = new QTextEdit(page);
QTextDocument* document = new QTextDocument(page);
QTextOption option;
option.setFlags(QTextOption::ShowTabsAndSpaces);
document->setDefaultTextOption(option);
if (ui->showWhitespace->isChecked()) {
QTextOption option;
option.setFlags(QTextOption::ShowTabsAndSpaces);
document->setDefaultTextOption(option);
}
editor->setDocument(document);
editor->setAcceptRichText(false);
editor->setPlainText(QString(temp));
Expand Down
1 change: 1 addition & 0 deletions src/textviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private slots:
void modified();
void patternChanged(QString newPattern);
void findNext();
void showWhitespaceChanged(int state);

private:
void saveFile(const QTextEdit* editor);
Expand Down
30 changes: 25 additions & 5 deletions src/textviewer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,34 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>Placeholder</string>
</property>
</widget>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>Placeholder</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="showWhitespace">
<property name="text">
<string>Show Whitespace</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTabWidget" name="editorTabs">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>-1</number>
</property>
Expand Down