simplify, move redundant operations to functions, update coding style
This commit is contained in:
@ -12,7 +12,7 @@ BraceWrapping:
|
|||||||
BreakBeforeBraces: Custom
|
BreakBeforeBraces: Custom
|
||||||
BreakInheritanceList: AfterColon
|
BreakInheritanceList: AfterColon
|
||||||
BreakConstructorInitializers: AfterColon
|
BreakConstructorInitializers: AfterColon
|
||||||
ColumnLimit: 80
|
ColumnLimit: 100
|
||||||
IndentCaseLabels: true
|
IndentCaseLabels: true
|
||||||
IndentWidth: 4
|
IndentWidth: 4
|
||||||
ObjCBlockIndentWidth: 4
|
ObjCBlockIndentWidth: 4
|
||||||
|
|||||||
@ -8,12 +8,9 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
|||||||
{
|
{
|
||||||
lineNumberArea = new LineNumberArea(this);
|
lineNumberArea = new LineNumberArea(this);
|
||||||
|
|
||||||
connect(this, &CodeEditor::blockCountChanged, this,
|
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
|
||||||
&CodeEditor::updateLineNumberAreaWidth);
|
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
|
||||||
connect(this, &CodeEditor::updateRequest, this,
|
connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
|
||||||
&CodeEditor::updateLineNumberArea);
|
|
||||||
connect(this, &CodeEditor::cursorPositionChanged, this,
|
|
||||||
&CodeEditor::highlightCurrentLine);
|
|
||||||
|
|
||||||
updateLineNumberAreaWidth(0);
|
updateLineNumberAreaWidth(0);
|
||||||
highlightCurrentLine();
|
highlightCurrentLine();
|
||||||
@ -43,8 +40,7 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
|
|||||||
if (dy)
|
if (dy)
|
||||||
lineNumberArea->scroll(0, dy);
|
lineNumberArea->scroll(0, dy);
|
||||||
else
|
else
|
||||||
lineNumberArea->update(0, rect.y(), lineNumberArea->width(),
|
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
|
||||||
rect.height());
|
|
||||||
|
|
||||||
if (rect.contains(viewport()->rect())) updateLineNumberAreaWidth(0);
|
if (rect.contains(viewport()->rect())) updateLineNumberAreaWidth(0);
|
||||||
}
|
}
|
||||||
@ -54,8 +50,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
|
|||||||
QPlainTextEdit::resizeEvent(e);
|
QPlainTextEdit::resizeEvent(e);
|
||||||
|
|
||||||
QRect cr = contentsRect();
|
QRect cr = contentsRect();
|
||||||
lineNumberArea->setGeometry(
|
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
||||||
QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeEditor::highlightCurrentLine()
|
void CodeEditor::highlightCurrentLine()
|
||||||
@ -83,15 +78,14 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
|||||||
painter.fillRect(event->rect(), Qt::lightGray);
|
painter.fillRect(event->rect(), Qt::lightGray);
|
||||||
QTextBlock block = firstVisibleBlock();
|
QTextBlock block = firstVisibleBlock();
|
||||||
int blockNumber = block.blockNumber();
|
int blockNumber = block.blockNumber();
|
||||||
int top =
|
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
|
||||||
qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
|
int bottom = top + qRound(blockBoundingRect(block).height());
|
||||||
int bottom = top + qRound(blockBoundingRect(block).height());
|
|
||||||
while (block.isValid() && top <= event->rect().bottom()) {
|
while (block.isValid() && top <= event->rect().bottom()) {
|
||||||
if (block.isVisible() && bottom >= event->rect().top()) {
|
if (block.isVisible() && bottom >= event->rect().top()) {
|
||||||
QString number = QString::number(blockNumber + 1);
|
QString number = QString::number(blockNumber + 1);
|
||||||
painter.setPen(Qt::black);
|
painter.setPen(Qt::black);
|
||||||
painter.drawText(0, top, lineNumberArea->width(),
|
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
|
||||||
fontMetrics().height(), Qt::AlignRight, number);
|
Qt::AlignRight, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
block = block.next();
|
block = block.next();
|
||||||
@ -100,3 +94,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
|||||||
++blockNumber;
|
++blockNumber;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Local Variables:
|
||||||
|
// c-basic-offset: 4
|
||||||
|
// End:
|
||||||
|
|||||||
@ -38,3 +38,6 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
// Local Variables:
|
||||||
|
// c-basic-offset: 4
|
||||||
|
// End:
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#include "ui_lammpsgui.h"
|
#include "ui_lammpsgui.h"
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QFont>
|
#include <QFont>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
@ -33,14 +34,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
|
|||||||
connect(ui->actionUndo, &QAction::triggered, this, &LammpsGui::undo);
|
connect(ui->actionUndo, &QAction::triggered, this, &LammpsGui::undo);
|
||||||
connect(ui->actionRedo, &QAction::triggered, this, &LammpsGui::redo);
|
connect(ui->actionRedo, &QAction::triggered, this, &LammpsGui::redo);
|
||||||
connect(ui->actionClear, &QAction::triggered, this, &LammpsGui::clear);
|
connect(ui->actionClear, &QAction::triggered, this, &LammpsGui::clear);
|
||||||
connect(ui->actionRun_Buffer, &QAction::triggered, this,
|
connect(ui->actionRun_Buffer, &QAction::triggered, this, &LammpsGui::run_buffer);
|
||||||
&LammpsGui::run_buffer);
|
connect(ui->actionAbout_LAMMPS_GUI, &QAction::triggered, this, &LammpsGui::about);
|
||||||
connect(ui->actionExecute_Line, &QAction::triggered, this,
|
|
||||||
&LammpsGui::run_line);
|
|
||||||
connect(ui->actionAbout_LAMMPS_GUI, &QAction::triggered, this,
|
|
||||||
&LammpsGui::about);
|
|
||||||
connect(ui->actionLAMMPS_Info, &QAction::triggered, this,
|
|
||||||
&LammpsGui::about_lammps);
|
|
||||||
|
|
||||||
#if !QT_CONFIG(clipboard)
|
#if !QT_CONFIG(clipboard)
|
||||||
ui->actionCut->setEnabled(false);
|
ui->actionCut->setEnabled(false);
|
||||||
@ -74,13 +69,17 @@ void LammpsGui::open()
|
|||||||
open_file(fileName);
|
open_file(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// open file and switch CWD to path of file
|
||||||
void LammpsGui::open_file(const QString &fileName)
|
void LammpsGui::open_file(const QString &fileName)
|
||||||
{
|
{
|
||||||
|
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
current_file = fileName;
|
QFileInfo path(file);
|
||||||
|
current_file = path.fileName();
|
||||||
|
current_dir = path.absolutePath();
|
||||||
|
QDir::setCurrent(current_dir);
|
||||||
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
|
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
|
||||||
QMessageBox::warning(this, "Warning",
|
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
|
||||||
"Cannot open file: " + file.errorString());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
||||||
@ -91,20 +90,13 @@ void LammpsGui::open_file(const QString &fileName)
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LammpsGui::save()
|
void LammpsGui::write_file(const QString &fileName)
|
||||||
{
|
{
|
||||||
QString fileName;
|
|
||||||
// If we don't have a filename from before, get one.
|
|
||||||
if (current_file.isEmpty()) {
|
|
||||||
fileName = QFileDialog::getSaveFileName(this, "Save");
|
|
||||||
current_file = fileName;
|
|
||||||
} else {
|
|
||||||
fileName = current_file;
|
|
||||||
}
|
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
|
QFileInfo path(file);
|
||||||
|
current_file = path.fileName();
|
||||||
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
||||||
QMessageBox::warning(this, "Warning",
|
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
||||||
"Cannot save file: " + file.errorString());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
||||||
@ -114,22 +106,19 @@ void LammpsGui::save()
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LammpsGui::save()
|
||||||
|
{
|
||||||
|
QString fileName = current_file;
|
||||||
|
// If we don't have a filename from before, get one.
|
||||||
|
if (fileName.isEmpty()) fileName = QFileDialog::getSaveFileName(this, "Save");
|
||||||
|
|
||||||
|
write_file(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
void LammpsGui::save_as()
|
void LammpsGui::save_as()
|
||||||
{
|
{
|
||||||
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
|
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
|
||||||
QFile file(fileName);
|
write_file(fileName);
|
||||||
|
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
|
||||||
QMessageBox::warning(this, "Warning",
|
|
||||||
"Cannot save file: " + file.errorString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
current_file = fileName;
|
|
||||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
|
||||||
QTextStream out(&file);
|
|
||||||
QString text = ui->textEdit->toPlainText();
|
|
||||||
out << text;
|
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LammpsGui::quit()
|
void LammpsGui::quit()
|
||||||
@ -176,30 +165,13 @@ void LammpsGui::redo()
|
|||||||
|
|
||||||
void LammpsGui::run_buffer()
|
void LammpsGui::run_buffer()
|
||||||
{
|
{
|
||||||
char *args[] = {(char *)"LAMMPS GUI", (char *)"-log", (char *)"none"};
|
start_lammps();
|
||||||
int nargs = sizeof(args) / sizeof(char *);
|
|
||||||
|
|
||||||
clear();
|
|
||||||
if (!lammps_handle)
|
|
||||||
lammps_handle = lammps_open_no_mpi(nargs, args, nullptr);
|
|
||||||
if (!lammps_handle) return;
|
if (!lammps_handle) return;
|
||||||
|
clear();
|
||||||
std::string buffer = ui->textEdit->toPlainText().toStdString();
|
std::string buffer = ui->textEdit->toPlainText().toStdString();
|
||||||
lammps_commands_string(lammps_handle, buffer.c_str());
|
lammps_commands_string(lammps_handle, buffer.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void LammpsGui::run_line()
|
|
||||||
{
|
|
||||||
char *args[] = {(char *)"LAMMPS GUI", (char *)"-log", (char *)"none"};
|
|
||||||
int nargs = sizeof(args) / sizeof(char *);
|
|
||||||
|
|
||||||
if (!lammps_handle)
|
|
||||||
lammps_handle = lammps_open_no_mpi(nargs, args, nullptr);
|
|
||||||
if (!lammps_handle) return;
|
|
||||||
|
|
||||||
// std::string buffer = ui->textEdit->toPlainText().toStdString();
|
|
||||||
// lammps_commands_string(lammps_handle, buffer.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void LammpsGui::clear()
|
void LammpsGui::clear()
|
||||||
{
|
{
|
||||||
if (lammps_handle) {
|
if (lammps_handle) {
|
||||||
@ -208,21 +180,26 @@ void LammpsGui::clear()
|
|||||||
ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
|
ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LammpsGui::about_lammps()
|
void LammpsGui::about()
|
||||||
|
{
|
||||||
|
start_lammps();
|
||||||
|
|
||||||
|
std::string version = "This is LAMMPS-GUI version 0.1\n";
|
||||||
|
if (lammps_handle) version += "LAMMPS Version " + std::to_string(lammps_version(lammps_handle));
|
||||||
|
QMessageBox::information(this, "About LAMMPS-GUI", version.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void LammpsGui::start_lammps()
|
||||||
{
|
{
|
||||||
char *args[] = {(char *)"LAMMPS GUI", (char *)"-log", (char *)"none"};
|
char *args[] = {(char *)"LAMMPS GUI", (char *)"-log", (char *)"none"};
|
||||||
int nargs = sizeof(args) / sizeof(char *);
|
int nargs = sizeof(args) / sizeof(char *);
|
||||||
|
|
||||||
|
// Create LAMMPS instance if not already present
|
||||||
|
if (!lammps_handle) lammps_handle = lammps_open_no_mpi(nargs, args, nullptr);
|
||||||
if (!lammps_handle)
|
if (!lammps_handle)
|
||||||
lammps_handle = lammps_open_no_mpi(nargs, args, nullptr);
|
QMessageBox::warning(this, "LAMMPS Error", "Internal error launching LAMMPS");
|
||||||
|
|
||||||
std::string version = "LAMMPS Version " + std::to_string(lammps_version(lammps_handle));
|
|
||||||
QString lammps_info(version.c_str());
|
|
||||||
QMessageBox::information(this, "About LAMMPS", lammps_info);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LammpsGui::about()
|
// Local Variables:
|
||||||
{
|
// c-basic-offset: 4
|
||||||
QMessageBox::information(this, "About LAMMPS-GUI",
|
// End:
|
||||||
"This is LAMMPS-GUI version 0.1");
|
|
||||||
}
|
|
||||||
|
|||||||
@ -32,6 +32,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void open_file(const QString &filename);
|
void open_file(const QString &filename);
|
||||||
|
void write_file(const QString &filename);
|
||||||
|
void start_lammps();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void new_document();
|
void new_document();
|
||||||
@ -46,15 +48,17 @@ private slots:
|
|||||||
void redo();
|
void redo();
|
||||||
void clear();
|
void clear();
|
||||||
void run_buffer();
|
void run_buffer();
|
||||||
void run_line();
|
|
||||||
void about();
|
void about();
|
||||||
void about_lammps();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::LammpsGui *ui;
|
Ui::LammpsGui *ui;
|
||||||
|
|
||||||
QString current_file;
|
QString current_file;
|
||||||
|
QString current_dir;
|
||||||
void *lammps_handle;
|
void *lammps_handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LAMMPSGUI_H
|
#endif // LAMMPSGUI_H
|
||||||
|
// Local Variables:
|
||||||
|
// c-basic-offset: 4
|
||||||
|
// End:
|
||||||
|
|||||||
@ -68,19 +68,11 @@
|
|||||||
</property>
|
</property>
|
||||||
<addaction name="actionClear"/>
|
<addaction name="actionClear"/>
|
||||||
<addaction name="actionRun_Buffer"/>
|
<addaction name="actionRun_Buffer"/>
|
||||||
<addaction name="actionExecute_Line"/>
|
|
||||||
</widget>
|
|
||||||
<widget class="QMenu" name="menuAbout">
|
|
||||||
<property name="title">
|
|
||||||
<string>About</string>
|
|
||||||
</property>
|
|
||||||
<addaction name="actionAbout_LAMMPS_GUI"/>
|
|
||||||
<addaction name="actionLAMMPS_Info"/>
|
|
||||||
</widget>
|
</widget>
|
||||||
<addaction name="menuFile"/>
|
<addaction name="menuFile"/>
|
||||||
<addaction name="menuEdit"/>
|
<addaction name="menuEdit"/>
|
||||||
<addaction name="menuRun"/>
|
<addaction name="menuRun"/>
|
||||||
<addaction name="menuAbout"/>
|
<addaction name="actionAbout_LAMMPS_GUI"/>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QStatusBar" name="statusbar"/>
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
<action name="actionNew">
|
<action name="actionNew">
|
||||||
@ -143,19 +135,9 @@
|
|||||||
<string>Execute Buffer</string>
|
<string>Execute Buffer</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
<action name="actionExecute_Line">
|
|
||||||
<property name="text">
|
|
||||||
<string>Execute Line</string>
|
|
||||||
</property>
|
|
||||||
</action>
|
|
||||||
<action name="actionAbout_LAMMPS_GUI">
|
<action name="actionAbout_LAMMPS_GUI">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>About LAMMPS-GUI</string>
|
<string>About</string>
|
||||||
</property>
|
|
||||||
</action>
|
|
||||||
<action name="actionLAMMPS_Info">
|
|
||||||
<property name="text">
|
|
||||||
<string>LAMMPS Info</string>
|
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
</widget>
|
</widget>
|
||||||
|
|||||||
@ -1,58 +0,0 @@
|
|||||||
#include "codeeditor.h"
|
|
||||||
|
|
||||||
int CodeEditor::lineNumberAreaWidth()
|
|
||||||
{
|
|
||||||
int digits = 1;
|
|
||||||
int max = qMax(1, blockCount());
|
|
||||||
while (max >= 10) {
|
|
||||||
max /= 10;
|
|
||||||
++digits;
|
|
||||||
}
|
|
||||||
|
|
||||||
int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
|
|
||||||
|
|
||||||
return space;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
|
|
||||||
{
|
|
||||||
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
|
|
||||||
{
|
|
||||||
if (dy)
|
|
||||||
lineNumberArea->scroll(0, dy);
|
|
||||||
else
|
|
||||||
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
|
|
||||||
|
|
||||||
if (rect.contains(viewport()->rect()))
|
|
||||||
updateLineNumberAreaWidth(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CodeEditor::resizeEvent(QResizeEvent *e)
|
|
||||||
{
|
|
||||||
QTextEdit::resizeEvent(e);
|
|
||||||
|
|
||||||
QRect cr = contentsRect();
|
|
||||||
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void CodeEditor::highlightCurrentLine()
|
|
||||||
{
|
|
||||||
QList<QTextEdit::ExtraSelection> extraSelections;
|
|
||||||
|
|
||||||
if (!isReadOnly()) {
|
|
||||||
QTextEdit::ExtraSelection selection;
|
|
||||||
|
|
||||||
QColor lineColor = QColor(Qt::yellow).lighter(160);
|
|
||||||
|
|
||||||
selection.format.setBackground(lineColor);
|
|
||||||
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
|
||||||
selection.cursor = textCursor();
|
|
||||||
selection.cursor.clearSelection();
|
|
||||||
extraSelections.append(selection);
|
|
||||||
}
|
|
||||||
|
|
||||||
setExtraSelections(extraSelections);
|
|
||||||
}
|
|
||||||
@ -36,3 +36,6 @@ private:
|
|||||||
CodeEditor *codeEditor;
|
CodeEditor *codeEditor;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
// Local Variables:
|
||||||
|
// c-basic-offset: 4
|
||||||
|
// End:
|
||||||
|
|||||||
@ -13,3 +13,7 @@ int main(int argc, char *argv[])
|
|||||||
w.show();
|
w.show();
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Local Variables:
|
||||||
|
// c-basic-offset: 4
|
||||||
|
// End:
|
||||||
|
|||||||
Reference in New Issue
Block a user