Skip to content

Commit

Permalink
Toggle highlighted lines in editor #417
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorsandy committed Mar 12, 2021
1 parent 2d4d7c1 commit 2dd613f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 60 deletions.
2 changes: 1 addition & 1 deletion builds/linux/obs/alldeps/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Last Update: July 19, 2020
# Copyright (c) 2017 - 2020 by Trevor SANDY
pkgname=lpub3d
pkgver=2.4.1.2035
pkgver=2.4.1.2036
pkgrel=1
pkgdesc="An LDraw Building Instruction Editor"
url="https://github.com/trevorsandy/lpub3d.git"
Expand Down
6 changes: 3 additions & 3 deletions builds/linux/obs/alldeps/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lpub3d (2.4.1.2035) debian; urgency=medium
lpub3d (2.4.1.2036) debian; urgency=medium

* LPub3D version 2.4.1.1.2035_20210312 for Linux
* LPub3D version 2.4.1.1.2036_20210312 for Linux

-- Trevor SANDY <trevor.sandy@gmail.com> Fri, 12 Mar 2021 06:32:42 +0100
-- Trevor SANDY <trevor.sandy@gmail.com> Fri, 12 Mar 2021 06:33:20 +0100
2 changes: 1 addition & 1 deletion builds/linux/obs/alldeps/debian/lpub3d.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Format: 1.0
Source: lpub3d
Binary: lpub3d
Architecture: any
Version: 2.4.1.2035
Version: 2.4.1.2036
Maintainer: Trevor SANDY <trevor.sandy@gmail.com>
Homepage: https://trevorsandy.github.io/lpub3d/
Standards-Version: 3.9.7
Expand Down
4 changes: 2 additions & 2 deletions builds/linux/obs/alldeps/lpub3d.spec
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ BuildRequires: fdupes
Summary: An LDraw Building Instruction Editor
Name: lpub3d
Icon: lpub3d.xpm
Version: 2.4.1.2035
Version: 2.4.1.2036
Release: <B_CNT>%{?dist}
URL: https://trevorsandy.github.io/lpub3d
Vendor: Trevor SANDY
Expand Down Expand Up @@ -731,5 +731,5 @@ update-mime-database /usr/share/mime >/dev/null || true
update-desktop-database || true
%endif

* Fri Mar 12 2021 - trevor.dot.sandy.at.gmail.dot.com 2.4.1.2035
* Fri Mar 12 2021 - trevor.dot.sandy.at.gmail.dot.com 2.4.1.2036
- LPub3D Linux package (rpm) release
Binary file modified builds/utilities/ci/secure/.secrets.tar.enc
Binary file not shown.
2 changes: 1 addition & 1 deletion builds/utilities/version.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2 4 1 1 2035 7cc2501
2 4 1 1 2036 2d4d7c1
2 changes: 1 addition & 1 deletion mainApp/docs/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LPub3D 2.4.1.1.2035 (12 03 2021 06:32:42)
LPub3D 2.4.1.1.2036 (12 03 2021 06:33:20)

Features, enhancements, fixes and changes
------------
Expand Down
2 changes: 1 addition & 1 deletion mainApp/docs/RELEASE_NOTES.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<table class="tg">
<tr>
<th class="tg-0pky">
<h4><a id="LPub3D_0"></a>LPub3D 2.4.1.1.2035 (12 03 2021 06:32:42)</h4>
<h4><a id="LPub3D_0"></a>LPub3D 2.4.1.1.2036 (12 03 2021 06:33:20)</h4>
<hr>
<p>
LPub3D Official release.<br>
Expand Down
137 changes: 88 additions & 49 deletions mainApp/editwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,63 +945,90 @@ bool EditWindow::saveFile()

void EditWindow::highlightSelectedLines(QVector<int> &lines, bool clear)
{
disconnect(_textEdit->document(), SIGNAL(contentsChange(int,int,int)),
this, SLOT( contentsChange(int,int,int)));
highlightSelectedLines(lines, clear, false/*editorSelection*/);
}

QTextCursor highlightCursor(_textEdit->document());
void EditWindow::highlightSelectedLines(QVector<int> &lines, bool clear, bool editorSelection)
{
emit lpubAlert->messageSig(LOG_TRACE, QString("HighlightSelectedLines Sender: %1")
.arg(sender()->metaObject()->className()));

QTextCursor textCursor(_textEdit->document());
// Remove duplicate editor (saved) lines and viewer lines
if (!editorSelection && savedSelection.size()) {
for (int line : lines) {
if (savedSelection.contains(line))
savedSelection.removeAll(line);
}
}

textCursor.beginEditBlock();
auto highlightLines = [this, &editorSelection] (QVector<int> &linesToFormat, bool clear)
{
QTextCursor highlightCursor(_textEdit->document());

if (!_textEdit->isReadOnly()) {
QTextCursor textCursor(_textEdit->document());

QColor lineColor = QColor(Qt::transparent);
bool applyFormat = lines.size();
if (applyFormat) {
if (clear) {
if (Preferences::displayTheme == THEME_DEFAULT) {
lineColor = QColor(Qt::white);
} else if (Preferences::displayTheme == THEME_DARK) {
lineColor = QColor(THEME_SCENE_BGCOLOR_DARK);
}
} else {
if (Preferences::displayTheme == THEME_DEFAULT) {
lineColor = QColor(Qt::yellow).lighter(180);
} else if (Preferences::displayTheme == THEME_DARK) {
lineColor = QColor(Qt::yellow).lighter(180);
lineColor.setAlpha(100); // make 60% transparent
textCursor.beginEditBlock();

if (!_textEdit->isReadOnly()) {

QColor lineColor = QColor(Qt::transparent);

bool applyFormat = linesToFormat.size() || (editorSelection && savedSelection.size());
if (applyFormat) {
if (clear) {
if (Preferences::displayTheme == THEME_DEFAULT) {
lineColor = QColor(Qt::white);
} else if (Preferences::displayTheme == THEME_DARK) {
lineColor = QColor(THEME_SCENE_BGCOLOR_DARK);
}
} else {
if (Preferences::displayTheme == THEME_DEFAULT) {
lineColor = QColor(Qt::yellow).lighter(180);
} else if (Preferences::displayTheme == THEME_DARK) {
lineColor = QColor(Qt::yellow).lighter(180);
lineColor.setAlpha(100); // make 60% transparent
}
}
}
}
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(lineColor);

if (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
if (applyFormat) {
for (int i = 0; i < lines.size(); ++i) {
QTextBlock block = _textEdit->document()->findBlockByLineNumber(lines.at(i));
int blockPos = block.position();
highlightCursor.setPosition(blockPos);

if (!highlightCursor.isNull()) {
highlightCursor.select(QTextCursor::LineUnderCursor);
highlightCursor.mergeCharFormat(colorFormat);
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(lineColor);

if (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
if (applyFormat) {
for (int i = 0; i < linesToFormat.size(); ++i) {
QTextBlock block = _textEdit->document()->findBlockByLineNumber(linesToFormat.at(i));
int blockPos = block.position();
highlightCursor.setPosition(blockPos);

if (!highlightCursor.isNull()) {
highlightCursor.select(QTextCursor::LineUnderCursor);
highlightCursor.mergeCharFormat(colorFormat);
}
}
} else {
highlightCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
}
} else {
highlightCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
}
}
}

textCursor.endEditBlock();
textCursor.endEditBlock();
};

disconnect(_textEdit->document(), SIGNAL(contentsChange(int,int,int)),
this, SLOT( contentsChange(int,int,int)));

// apply previously selected lines if any
if (savedSelection.size())
highlightLines(savedSelection, false/*clear*/);

// apply highlighting, toggle on from editor or selection from viewer
highlightLines(lines, clear);

connect(_textEdit->document(), SIGNAL(contentsChange(int,int,int)),
this, SLOT( contentsChange(int,int,int)));

}

void EditWindow::highlightCurrentLine()
Expand Down Expand Up @@ -1099,34 +1126,36 @@ void EditWindow::updateSelectedParts() {

int currentLine = 0;
int selectedLines = 0;
bool hasSelection = false;
bool clearSelection = false;

QVector<TypeLine> lineTypeIndexes;
QVector<int> toggleLines;

QTextCursor cursor = _textEdit->textCursor();

QTextCursor saveCursor = cursor;

if(!(hasSelection = cursor.hasSelection()))
if(!cursor.hasSelection())
cursor.select(QTextCursor::LineUnderCursor);

QString content = cursor.selection().toPlainText();
selectedLines = content.count("\n")+1;

auto getSelectedLineNumber = [&cursor] () {
int lines = 0;
int lineNumber = 0;

while(cursor.positionInBlock()>0) {
cursor.movePosition(QTextCursor::Up);
lines++;
lineNumber++;
}

QTextBlock block = cursor.block().previous();

while(block.isValid()) {
lines += block.lineCount();
lineNumber += block.lineCount();
block = block.previous();
}
return lines;
return lineNumber;
};

cursor.beginEditBlock();
Expand All @@ -1139,9 +1168,18 @@ void EditWindow::updateSelectedParts() {

if (!selection.isEmpty())
{
if (selection.startsWith("1")) {
TypeLine typeLine(fileOrderIndex,getSelectedLineNumber());
if (selection.startsWith("1") || selection.contains(" PLI BEGIN SUB ")) {
int lineNumber = getSelectedLineNumber();
TypeLine typeLine(fileOrderIndex,lineNumber);
lineTypeIndexes.append(typeLine);
toggleLines.append(lineNumber);
clearSelection = savedSelection.contains(lineNumber);
highlightSelectedLines(toggleLines, clearSelection); // toggle On/Off
toggleLines.clear();
if (clearSelection)
savedSelection.removeAll(lineNumber);
else
savedSelection.append(lineNumber);
}

// go to next selected line
Expand Down Expand Up @@ -1219,6 +1257,7 @@ void EditWindow::displayFile(
isIncludeFile = ldrawFile->isIncludeFile(_fileName);
_contentLoaded = false;
_subFileListPending = true;
savedSelection.clear();

if (modelFileEdit() && !fileName.isEmpty())
fileWatcher.removePath(fileName);
Expand Down
2 changes: 2 additions & 0 deletions mainApp/editwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private slots:
void readSettings();
void writeSettings();
void openFolderSelect(const QString& absoluteFilePath);
void highlightSelectedLines(QVector<int> &lines, bool clear, bool editorSelection);

QAbstractItemModel *modelFromFile(const QString& fileName);
void openWithProgramAndArgs(QString &program, QStringList &arguments);
Expand All @@ -165,6 +166,7 @@ private slots:
HighlighterSimple *highlighterSimple;
QComboBox *mpdCombo;
QFileSystemWatcher fileWatcher;
QVector<int> savedSelection;
QString fileName; // of model file currently being displayed
int numOpenWithPrograms;
int showLineType;
Expand Down
2 changes: 1 addition & 1 deletion mainApp/org.trevorsandy.lpub3d.appdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<binary>lpub3d24</binary>
​</provides>
<releases>
<release version="2.4.1.2035" date="2021-03-12">
<release version="2.4.1.2036" date="2021-03-12">
<description>
<p>LPub3D Linux AppImage package</p>
</description>
Expand Down

0 comments on commit 2dd613f

Please sign in to comment.