Permalink
Browse files

Implement indent and unindent buttons

  • Loading branch information...
ShiraNai7 committed Feb 6, 2017
1 parent 6b9e22b commit 170103586bde2d766a8378526e3355d3987d0cb7
View
Binary file not shown.
View
Binary file not shown.
View
@@ -44,5 +44,7 @@
<file>syntax-help/directives.html</file>
<file>syntax-help/html4css1.css</file>
<file>icons/qt.png</file>
<file>icons/indent.png</file>
<file>icons/unindent.png</file>
</qresource>
</RCC>
@@ -14,13 +14,18 @@ namespace RstPad {
}
void EditManipulator::indent()
{
indent(indentSize);
}
void EditManipulator::indent(int numChars)
{
auto cursor = edit->textCursor();
if (cursor.hasSelection()) {
// indent selection (one or more lines)
auto blocks = getSelectedBlocks(cursor.selectionStart(), cursor.selectionEnd());
auto indentation = QString(" ").repeated(indentSize);
auto indentation = QString(" ").repeated(numChars);
cursor.beginEditBlock();
@@ -33,13 +38,18 @@ namespace RstPad {
} else {
// indent from cursor position
int pos = cursor.positionInBlock();
int numSpaces = qCeil((float) pos / (float) indentSize) * indentSize - pos;
int numSpaces = qCeil((float) pos / (float) numChars) * numChars - pos;
cursor.insertText(QString(" ").repeated(0 == numSpaces ? indentSize : numSpaces));
cursor.insertText(QString(" ").repeated(0 == numSpaces ? numChars : numSpaces));
}
}
void EditManipulator::unindent()
{
unindent(indentSize);
}
void EditManipulator::unindent(int numChars)
{
auto cursor = edit->textCursor();
@@ -51,10 +61,10 @@ namespace RstPad {
for (int i = 0; i < blocks.length(); ++i) {
auto text = blocks[i].text();
auto numCharsToRemove = indentSize;
auto numCharsToRemove = numChars;
// stop on first non-space character
for (int j = 0; j < indentSize; ++j) {
for (int j = 0; j < numChars; ++j) {
if (text[j] != QLatin1Char(' ')) {
numCharsToRemove = j;
break;
@@ -13,7 +13,9 @@ namespace RstPad {
public:
EditManipulator(QPlainTextEdit *edit);
void indent();
void indent(int numChars);
void unindent();
void unindent(int numChars);
void indentNewLine();
void jumpToStartOfLine(bool anchored = false);
void setIndentSize(int size);
View
@@ -5,6 +5,7 @@
#include "WebPage.h"
#include "SettingsDlg.h"
#include "PygmentLexersDlg.h"
#include <Qt>
#include <QTextOption>
#include <QFontDatabase>
#include <QMessageBox>
@@ -529,3 +530,21 @@ void RstPad::MainWindow::on_ActionGotoLine_triggered()
}
}
}
void RstPad::MainWindow::on_ActionIndent_triggered()
{
if (APP->keyboardModifiers() & Qt::ShiftModifier) {
ui->CodeEditor->manipulator()->indent(1);
} else {
ui->CodeEditor->manipulator()->indent();
}
}
void RstPad::MainWindow::on_ActionUnindent_triggered()
{
if (APP->keyboardModifiers() & Qt::ShiftModifier) {
ui->CodeEditor->manipulator()->unindent(1);
} else {
ui->CodeEditor->manipulator()->unindent();
}
}
View
@@ -96,6 +96,8 @@ namespace RstPad {
void on_ActionFindNext_triggered();
void on_ActionFindPrev_triggered();
void on_ActionGotoLine_triggered();
void on_ActionIndent_triggered();
void on_ActionUnindent_triggered();
};
}
View
@@ -138,17 +138,20 @@ QWebEngineView, QPlainTextEdit {
<addaction name="ActionBold"/>
<addaction name="ActionItalic"/>
<addaction name="separator"/>
<addaction name="ActionInlineCode"/>
<addaction name="ActionCodeBlock"/>
<addaction name="ActionPygmentsCodeBlock"/>
<addaction name="ActionHorizontalRule"/>
<addaction name="separator"/>
<addaction name="ActionUL"/>
<addaction name="ActionOL"/>
<addaction name="ActionHorizontalRule"/>
<addaction name="separator"/>
<addaction name="ActionUnindent"/>
<addaction name="ActionIndent"/>
<addaction name="separator"/>
<addaction name="ActionLink"/>
<addaction name="ActionImage"/>
<addaction name="separator"/>
<addaction name="ActionInlineCode"/>
<addaction name="ActionCodeBlock"/>
<addaction name="ActionPygmentsCodeBlock"/>
<addaction name="separator"/>
<addaction name="ActionSyntaxHelp"/>
</widget>
<action name="ActionNew">
@@ -504,6 +507,30 @@ QWebEngineView, QPlainTextEdit {
<string>Ctrl+G</string>
</property>
</action>
<action name="ActionIndent">
<property name="icon">
<iconset resource="../../resources/resources.qrc">
<normaloff>:/icons/indent.png</normaloff>:/icons/indent.png</iconset>
</property>
<property name="text">
<string>Indent</string>
</property>
<property name="toolTip">
<string>Indent (hold shift for 1-char indent)</string>
</property>
</action>
<action name="ActionUnindent">
<property name="icon">
<iconset resource="../../resources/resources.qrc">
<normaloff>:/icons/unindent.png</normaloff>:/icons/unindent.png</iconset>
</property>
<property name="text">
<string>Unindent</string>
</property>
<property name="toolTip">
<string>Unindent (hold shift for 1-char unindent)</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

0 comments on commit 1701035

Please sign in to comment.