Skip to content

Commit

Permalink
add a "page guide" pref in SLiMgui, at a specified column
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaller committed Jan 19, 2025
1 parent 0caf465 commit 05487d3
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
42 changes: 42 additions & 0 deletions QtSLiM/QtSLiMPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static const char *QtSLiMDisplayFontSize = "QtSLiMDisplayFontSize";
static const char *QtSLiMSyntaxHighlightScript = "QtSLiMSyntaxHighlightScript";
static const char *QtSLiMSyntaxHighlightOutput = "QtSLiMSyntaxHighlightOutput";
static const char *QtSLiMShowLineNumbers = "QtSLiMShowLineNumbers";
static const char *QtSLiMShowPageGuide = "QtSLiMShowPageGuide";
static const char *QtSLiMPageGuideColumn = "QtSLiMPageGuideColumn";
static const char *QtSLiMHighlightCurrentLine = "QtSLiMHighlightCurrentLine";
static const char *QtSLiMAutosaveOnRecycle = "QtSLiMAutosaveOnRecycle";
static const char *QtSLiMShowSaveInUntitled = "QtSLiMShowSaveInUntitled";
Expand Down Expand Up @@ -194,6 +196,20 @@ bool QtSLiMPreferencesNotifier::highlightCurrentLinePref(void) const
return settings.value(QtSLiMHighlightCurrentLine, QVariant(true)).toBool();
}

bool QtSLiMPreferencesNotifier::showPageGuidePref(void) const
{
QSettings settings;

return settings.value(QtSLiMShowPageGuide, QVariant(false)).toBool();
}

int QtSLiMPreferencesNotifier::pageGuideColumnPref(void) const
{
QSettings settings;

return settings.value(QtSLiMPageGuideColumn, QVariant(80)).toInt();
}

bool QtSLiMPreferencesNotifier::autosaveOnRecyclePref(void) const
{
QSettings settings;
Expand Down Expand Up @@ -368,6 +384,25 @@ void QtSLiMPreferencesNotifier::highlightCurrentLineToggled()
emit highlightCurrentLinePrefChanged();
}

void QtSLiMPreferencesNotifier::showPageGuideToggled()
{
QtSLiMPreferences &prefsUI = QtSLiMPreferences::instance();
QSettings settings;

settings.setValue(QtSLiMShowPageGuide, QVariant(prefsUI.ui->showPageGuide->isChecked()));

emit pageGuidePrefsChanged();
}

void QtSLiMPreferencesNotifier::pageGuideColumnChanged(int newColumn)
{
QSettings settings;

settings.setValue(QtSLiMPageGuideColumn, QVariant(newColumn));

emit pageGuidePrefsChanged();
}

void QtSLiMPreferencesNotifier::autosaveOnRecycleToggled()
{
QtSLiMPreferences &prefsUI = QtSLiMPreferences::instance();
Expand Down Expand Up @@ -454,6 +489,11 @@ QtSLiMPreferences::QtSLiMPreferences(QWidget *p_parent) : QDialog(p_parent), ui(
ui->showLineNumbers->setChecked(notifier->showLineNumbersPref());
ui->highlightCurrentLine->setChecked(notifier->highlightCurrentLinePref());

// the presence of this hidden widget fixes a padding bug; see https://forum.qt.io/topic/10757/unwanted-padding-around-qhboxlayout
ui->pageGuideNoPadWidget->hide();
ui->showPageGuide->setChecked(notifier->showPageGuidePref());
ui->pageGuideSpinBox->setValue(notifier->pageGuideColumnPref());

ui->autosaveOnRecycle->setChecked(notifier->autosaveOnRecyclePref());
ui->showSaveIfUntitled->setChecked(notifier->showSaveIfUntitledPref());
ui->showSaveIfUntitled->setEnabled(notifier->autosaveOnRecyclePref());
Expand All @@ -472,6 +512,8 @@ QtSLiMPreferences::QtSLiMPreferences(QWidget *p_parent) : QDialog(p_parent), ui(

connect(ui->showLineNumbers, &QCheckBox::toggled, notifier, &QtSLiMPreferencesNotifier::showLineNumbersToggled);
connect(ui->highlightCurrentLine, &QCheckBox::toggled, notifier, &QtSLiMPreferencesNotifier::highlightCurrentLineToggled);
connect(ui->showPageGuide, &QCheckBox::toggled, notifier, &QtSLiMPreferencesNotifier::showPageGuideToggled);
connect(ui->pageGuideSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), notifier, &QtSLiMPreferencesNotifier::pageGuideColumnChanged);

connect(ui->autosaveOnRecycle, &QCheckBox::toggled, notifier, &QtSLiMPreferencesNotifier::autosaveOnRecycleToggled);
connect(ui->showSaveIfUntitled, &QCheckBox::toggled, notifier, &QtSLiMPreferencesNotifier::showSaveIfUntitledToggled);
Expand Down
5 changes: 5 additions & 0 deletions QtSLiM/QtSLiMPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class QtSLiMPreferencesNotifier : public QObject
bool scriptSyntaxHighlightPref(void) const;
bool outputSyntaxHighlightPref(void) const;
bool showLineNumbersPref(void) const;
bool showPageGuidePref(void) const;
int pageGuideColumnPref(void) const;
bool highlightCurrentLinePref(void) const;
bool autosaveOnRecyclePref(void) const;
bool reloadOnSafeExternalEditsPref(void) const;
Expand All @@ -59,6 +61,7 @@ class QtSLiMPreferencesNotifier : public QObject
void scriptSyntaxHighlightPrefChanged(void);
void outputSyntaxHighlightPrefChanged(void);
void showLineNumbersPrefChanged(void);
void pageGuidePrefsChanged(void);
void highlightCurrentLinePrefChanged(void);
void autosaveOnRecyclePrefChanged(void);
void reloadOnSafeExternalEditsChanged(void);
Expand All @@ -81,6 +84,8 @@ private slots:
void syntaxHighlightScriptToggled();
void syntaxHighlightOutputToggled();
void showLineNumbersToggled();
void showPageGuideToggled();
void pageGuideColumnChanged(int newColumn);
void highlightCurrentLineToggled();
void autosaveOnRecycleToggled();
void reloadOnSafeExternalEditsToggled();
Expand Down
60 changes: 56 additions & 4 deletions QtSLiM/QtSLiMPreferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>303</width>
<height>747</height>
<height>780</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -279,7 +279,7 @@
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
<enum>Qt::StrongFocus</enum>
</property>
<property name="toolTip">
<string>the font size to use for script and output views</string>
Expand Down Expand Up @@ -419,9 +419,9 @@
<string notr="true">QGroupBox { font-weight: bold; }</string>
</property>
<property name="title">
<string>Line numbering:</string>
<string>Script view appearance:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5" stretch="0,0">
<layout class="QVBoxLayout" name="verticalLayout_5" stretch="0,0,0">
<property name="spacing">
<number>6</number>
</property>
Expand Down Expand Up @@ -457,6 +457,58 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="pageGuideLayout" stretch="0,0,0,1">
<item>
<widget class="QWidget" name="pageGuideNoPadWidget" native="true"/>
</item>
<item>
<widget class="QCheckBox" name="showPageGuide">
<property name="text">
<string>Page guide at column: </string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="pageGuideSpinBox">
<property name="minimumSize">
<size>
<width>55</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>999</number>
</property>
<property name="value">
<number>80</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>5</width>
<height>5</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down
57 changes: 57 additions & 0 deletions QtSLiM/QtSLiMScriptTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,11 @@ void QtSLiMScriptTextEdit::sharedInit(void)
setCenterOnScroll(true);

initializeLineNumbers();

// set up to listen to changes to page guide prefs
QtSLiMPreferencesNotifier &prefsNotifier = QtSLiMPreferencesNotifier::instance();

connect(&prefsNotifier, &QtSLiMPreferencesNotifier::pageGuidePrefsChanged, this, [this]() { viewport()->update(); });
}

void QtSLiMScriptTextEdit::initializeLineNumbers(void)
Expand Down Expand Up @@ -3391,6 +3396,58 @@ QString QtSLiMScriptTextEdit::exportAsHtml(void)
return html;
}

void QtSLiMScriptTextEdit::paintEvent(QPaintEvent *event)
{
// If the user wants a "page guide", show a slightly dimmed margin beyond a threshold column
// Note that Qt has already cleared to the white background of the QTextEdit
QtSLiMPreferencesNotifier &prefs = QtSLiMPreferencesNotifier::instance();
bool showPageGuide = prefs.showPageGuidePref();

if (showPageGuide)
{
QFont displayFont = prefs.displayFontPref();
QFontMetricsF fm(displayFont);
double marginStart;
QString marginString(prefs.pageGuideColumnPref(), ' ');

#if (QT_VERSION < QT_VERSION_CHECK(5, 11, 0))
marginStart = fm.width(marginString); // deprecated in 5.11
#else
marginStart = fm.horizontalAdvance(marginString); // added in Qt 5.11
#endif

// adjust by the document margin, which is built into QTextDocument; this took a while to find!
// this is an inset of the QTextEdit's contents, on all four sides; it defaults to 4, which we do not change
QTextDocument *doc = document();
double docMargin = doc->documentMargin();

marginStart += docMargin;
marginStart = round(marginStart);

QRect bounds = rect();

if (bounds.width() >= marginStart)
{
// Because QTextEdit's display lives inside a scrollable area, we use viewport()
QPainter painter(this->viewport());

QRect margin = bounds.adjusted(marginStart, 0, 0, 0);
QRect marginEdge = margin;

// draw a one-pixel darker line at the border
marginEdge.setWidth(1);
margin.adjust(1, 0, 0, 0);

painter.fillRect(marginEdge, QtSLiMColorWithWhite(0.918, 1.0));
painter.fillRect(margin, QtSLiMColorWithWhite(0.980, 1.0));
}
}

// call super to have it paint; this draws all the text and everything
QtSLiMTextEdit::paintEvent(event);
}





Expand Down
1 change: 1 addition & 0 deletions QtSLiM/QtSLiMScriptTextEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public slots:
void sharedInit(void);
void initializeLineNumbers(void);
virtual void resizeEvent(QResizeEvent *p_event) override;
virtual void paintEvent(QPaintEvent *event) override;

protected slots:
virtual void displayFontPrefChanged() override;
Expand Down
1 change: 1 addition & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Note that not every commit will be logged here; that is what the Github commit h

development head (in the master branch):
fix the wiring for calcPi() and calcTajimasD() to call the correct code; they were broken in SLiM 4.3
add a "page guide at column" pref to SLiMgui, for those trying to lay out code to a maximum width
multi-chromosome transition:
API changes:
x class Genome -> class Haplosome
Expand Down

0 comments on commit 05487d3

Please sign in to comment.