[PythonEditor] Make converting tabs to spaces the default

This commit is contained in:
mwganson
2024-12-16 17:14:33 -06:00
committed by Chris Hennes
parent 02f41bbe60
commit 40571eb683
7 changed files with 115 additions and 88 deletions

View File

@@ -205,7 +205,7 @@
<string>Keep tabs</string>
</property>
<property name="checked">
<bool>true</bool>
<bool>false</bool>
</property>
<property name="prefEntry" stdset="0">
<cstring>Tabs</cstring>
@@ -226,6 +226,9 @@
<property name="prefEntry" stdset="0">
<cstring>Spaces</cstring>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="prefPath" stdset="0">
<cstring>Editor</cstring>
</property>

View File

@@ -435,7 +435,7 @@ void InteractiveInterpreter::clearBuffer()
* Constructs a PythonConsole which is a child of 'parent'.
*/
PythonConsole::PythonConsole(QWidget *parent)
: TextEdit(parent), WindowParameter( "Editor" ), _sourceDrain(nullptr)
: PythonTextEditor(parent), _sourceDrain(nullptr)
{
d = new PythonConsoleP();
d->interactive = false;
@@ -467,7 +467,6 @@ PythonConsole::PythonConsole(QWidget *parent)
// set colors and font from settings
ParameterGrp::handle hPrefGrp = getWindowParameter();
hPrefGrp->Attach(this);
hPrefGrp->NotifyAll();
d->hGrpSettings = WindowParameter::getDefaultParameter()->GetGroup("PythonConsole");
@@ -512,7 +511,6 @@ PythonConsole::~PythonConsole()
saveHistory();
Base::PyGILStateLocker lock;
d->hGrpSettings->Detach(this);
getWindowParameter()->Detach(this);
delete pythonSyntax;
Py_XDECREF(d->_stdoutPy);
Py_XDECREF(d->_stderrPy);
@@ -613,13 +611,13 @@ void PythonConsole::keyPressEvent(QKeyEvent * e)
if (e->text().isEmpty() ||
e->matches(QKeySequence::Copy) ||
e->matches(QKeySequence::SelectAll)) {
TextEdit::keyPressEvent(e);
PythonTextEditor::keyPressEvent(e);
}
else if (!e->text().isEmpty() &&
(e->modifiers() == Qt::NoModifier ||
e->modifiers() == Qt::ShiftModifier)) {
this->moveCursor(QTextCursor::End);
TextEdit::keyPressEvent(e);
PythonTextEditor::keyPressEvent(e);
}
break;
}
@@ -671,11 +669,11 @@ void PythonConsole::keyPressEvent(QKeyEvent * e)
if (e->text() == QLatin1String(".")) {
// analyse context and show available call tips
int contextLength = cursor.position() - inputLineBegin.position();
TextEdit::keyPressEvent(e);
PythonTextEditor::keyPressEvent(e);
d->callTipsList->showTips( inputStrg.left( contextLength ) );
}
else {
TextEdit::keyPressEvent(e);
PythonTextEditor::keyPressEvent(e);
}
} break;
@@ -707,25 +705,25 @@ void PythonConsole::keyPressEvent(QKeyEvent * e)
case Qt::Key_Left:
{
if (cursor > inputLineBegin)
{ TextEdit::keyPressEvent(e); }
{ PythonTextEditor::keyPressEvent(e); }
restartHistory = false;
} break;
case Qt::Key_Right:
{
TextEdit::keyPressEvent(e);
PythonTextEditor::keyPressEvent(e);
restartHistory = false;
} break;
case Qt::Key_Backspace:
{
if (cursorBeyond( cursor, inputLineBegin, +1 ))
{ TextEdit::keyPressEvent(e); }
{ PythonTextEditor::keyPressEvent(e); }
} break;
default:
{
TextEdit::keyPressEvent(e);
PythonTextEditor::keyPressEvent(e);
} break;
}
// This can't be done in CallTipsList::eventFilter() because we must first perform

View File

@@ -97,7 +97,7 @@ private:
* @author Werner Mayer
*/
class PythonConsoleHighlighter;
class GuiExport PythonConsole : public TextEdit, public WindowParameter
class GuiExport PythonConsole : public PythonTextEditor
{
Q_OBJECT

View File

@@ -65,7 +65,7 @@ struct PythonEditorP
* syntax highlighting for the Python language.
*/
PythonEditor::PythonEditor(QWidget* parent)
: TextEditor(parent)
: PythonTextEditor(parent)
{
d = new PythonEditorP();
this->setSyntaxHighlighter(new PythonSyntaxHighlighter(this));
@@ -205,7 +205,7 @@ void PythonEditor::keyPressEvent(QKeyEvent* e)
setTextCursor(cursor);
return; //skip default handler
}
TextEditor::keyPressEvent(e); //wasn't enter key, so let base class handle it
PythonTextEditor::keyPressEvent(e); //wasn't enter key, so let base class handle it
}
void PythonEditor::onComment()

View File

@@ -36,7 +36,7 @@ class PythonSyntaxHighlighterP;
* Python text editor with syntax highlighting.
* \author Werner Mayer
*/
class GuiExport PythonEditor : public TextEditor
class GuiExport PythonEditor : public PythonTextEditor
{
Q_OBJECT

View File

@@ -257,6 +257,11 @@ TextEditor::TextEditor(QWidget* parent)
highlightCurrentLine();
}
void TextEditor::keyPressEvent(QKeyEvent *e)
{
TextEdit::keyPressEvent( e );
}
/** Destroys the object and frees any allocated resources */
TextEditor::~TextEditor()
{
@@ -359,12 +364,85 @@ void TextEditor::setSyntaxHighlighter(SyntaxHighlighter* sh)
this->highlighter = sh;
}
void TextEditor::keyPressEvent (QKeyEvent * e)
/** Sets the font, font size and tab size of the editor. */
void TextEditor::OnChange(Base::Subject<const char*> &rCaller,const char* sReason)
{
Q_UNUSED(rCaller);
ParameterGrp::handle hPrefGrp = getWindowParameter();
if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) {
#ifdef FC_OS_LINUX
int fontSize = hPrefGrp->GetInt("FontSize", 15);
#else
int fontSize = hPrefGrp->GetInt("FontSize", 10);
#endif
QString fontFamily = QString::fromLatin1(hPrefGrp->GetASCII( "Font", "Courier" ).c_str());
QFont font(fontFamily, fontSize);
setFont(font);
lineNumberArea->setFont(font);
}
else {
QMap<QString, QColor>::Iterator it = d->colormap.find(QString::fromLatin1(sReason));
if (it != d->colormap.end()) {
QColor color = it.value();
unsigned int col = App::Color::asPackedRGB<QColor>(color);
auto value = static_cast<unsigned long>(col);
value = hPrefGrp->GetUnsigned(sReason, value);
col = static_cast<unsigned int>(value);
color.setRgb((col>>24)&0xff, (col>>16)&0xff, (col>>8)&0xff);
if (this->highlighter)
this->highlighter->setColor(QLatin1String(sReason), color);
}
}
if (strcmp(sReason, "TabSize") == 0 || strcmp(sReason, "FontSize") == 0) {
int tabWidth = hPrefGrp->GetInt("TabSize", 4);
QFontMetrics metric(font());
int fontSize = QtTools::horizontalAdvance(metric, QLatin1Char('0'));
setTabStopDistance(tabWidth * fontSize);
}
// Enables/Disables Line number in the Macro Editor from Edit->Preferences->Editor menu.
if (strcmp(sReason, "EnableLineNumber") == 0) {
QRect cr = contentsRect();
bool show = hPrefGrp->GetBool("EnableLineNumber", true);
if(show)
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
else
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), 0, cr.height()));
}
if (strcmp(sReason, "EnableBlockCursor") == 0 ||
strcmp(sReason, "FontSize") == 0 ||
strcmp(sReason, "Font") == 0) {
bool block = hPrefGrp->GetBool("EnableBlockCursor", false);
if (block)
setCursorWidth(QFontMetrics(font()).averageCharWidth());
else
setCursorWidth(1);
}
}
void TextEditor::paintEvent (QPaintEvent * e)
{
TextEdit::paintEvent( e );
}
// ------------------------------------------------------------------------------
PythonTextEditor::PythonTextEditor(QWidget *parent)
: TextEditor(parent)
{
}
PythonTextEditor::~PythonTextEditor() = default;
void PythonTextEditor::keyPressEvent (QKeyEvent * e)
{
if ( e->key() == Qt::Key_Tab ) {
ParameterGrp::handle hPrefGrp = getWindowParameter();
bool space = hPrefGrp->GetBool("Spaces", true);
int indent = hPrefGrp->GetInt( "IndentSize", 4 );
bool space = hPrefGrp->GetBool( "Spaces", false );
QString ch = space ? QString(indent, QLatin1Char(' '))
: QString::fromLatin1("\t");
@@ -389,7 +467,7 @@ void TextEditor::keyPressEvent (QKeyEvent * e)
break; // end of selection reached
cursor.setPosition(block.position());
cursor.insertText(ch);
selEnd += ch.length();
selEnd += ch.length();
}
}
@@ -442,78 +520,10 @@ void TextEditor::keyPressEvent (QKeyEvent * e)
return;
}
TextEdit::keyPressEvent( e );
TextEditor::keyPressEvent( e );
}
/** Sets the font, font size and tab size of the editor. */
void TextEditor::OnChange(Base::Subject<const char*> &rCaller,const char* sReason)
{
Q_UNUSED(rCaller);
ParameterGrp::handle hPrefGrp = getWindowParameter();
if (strcmp(sReason, "FontSize") == 0 || strcmp(sReason, "Font") == 0) {
#ifdef FC_OS_LINUX
int fontSize = hPrefGrp->GetInt("FontSize", 15);
#else
int fontSize = hPrefGrp->GetInt("FontSize", 10);
#endif
QString fontFamily = QString::fromLatin1(hPrefGrp->GetASCII( "Font", "Courier" ).c_str());
QFont font(fontFamily, fontSize);
setFont(font);
lineNumberArea->setFont(font);
}
else {
QMap<QString, QColor>::Iterator it = d->colormap.find(QString::fromLatin1(sReason));
if (it != d->colormap.end()) {
QColor color = it.value();
unsigned int col = App::Color::asPackedRGB<QColor>(color);
auto value = static_cast<unsigned long>(col);
value = hPrefGrp->GetUnsigned(sReason, value);
col = static_cast<unsigned int>(value);
color.setRgb((col>>24)&0xff, (col>>16)&0xff, (col>>8)&0xff);
if (this->highlighter)
this->highlighter->setColor(QLatin1String(sReason), color);
}
}
if (strcmp(sReason, "TabSize") == 0 || strcmp(sReason, "FontSize") == 0) {
int tabWidth = hPrefGrp->GetInt("TabSize", 4);
QFontMetrics metric(font());
int fontSize = QtTools::horizontalAdvance(metric, QLatin1Char('0'));
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
setTabStopWidth(tabWidth * fontSize);
#else
setTabStopDistance(tabWidth * fontSize);
#endif
}
// Enables/Disables Line number in the Macro Editor from Edit->Preferences->Editor menu.
if (strcmp(sReason, "EnableLineNumber") == 0) {
QRect cr = contentsRect();
bool show = hPrefGrp->GetBool("EnableLineNumber", true);
if(show)
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
else
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), 0, cr.height()));
}
if (strcmp(sReason, "EnableBlockCursor") == 0 ||
strcmp(sReason, "FontSize") == 0 ||
strcmp(sReason, "Font") == 0) {
bool block = hPrefGrp->GetBool("EnableBlockCursor", false);
if (block)
setCursorWidth(QFontMetrics(font()).averageCharWidth());
else
setCursorWidth(1);
}
}
void TextEditor::paintEvent (QPaintEvent * e)
{
TextEdit::paintEvent( e );
}
// ------------------------------------------------------------------------------
LineMarker::LineMarker(TextEditor* editor)
: QWidget(editor), textEditor(editor)

View File

@@ -118,6 +118,22 @@ private:
friend class SyntaxHighlighter;
};
/** subclass of TextEditor that serves as base class for the
* python editor and the python console where we handle
* the tab key conversion to spaces, depending on user settings
*/
class GuiExport PythonTextEditor : public TextEditor
{
Q_OBJECT
public:
explicit PythonTextEditor(QWidget *parent = nullptr);
~PythonTextEditor() override;
protected:
void keyPressEvent(QKeyEvent *) override;
};
class LineMarker : public QWidget
{
Q_OBJECT