update main widget to CodeEditor with line numbers and highlight. Open file from command line.
This commit is contained in:
23
tools/lammps-gui/.clang-format
Normal file
23
tools/lammps-gui/.clang-format
Normal file
@ -0,0 +1,23 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
AccessModifierOffset: -4
|
||||
AlignConsecutiveAssignments: true
|
||||
AlignEscapedNewlines: Left
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortLambdasOnASingleLine: None
|
||||
AllowShortIfStatementsOnASingleLine: WithoutElse
|
||||
BraceWrapping:
|
||||
AfterFunction: true
|
||||
BreakBeforeBraces: Custom
|
||||
BreakInheritanceList: AfterColon
|
||||
BreakConstructorInitializers: AfterColon
|
||||
ColumnLimit: 80
|
||||
IndentCaseLabels: true
|
||||
IndentWidth: 4
|
||||
ObjCBlockIndentWidth: 4
|
||||
PenaltyBreakAssignment: 4
|
||||
Standard: Cpp11
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
...
|
||||
@ -53,6 +53,9 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
|
||||
|
||||
set(PROJECT_SOURCES
|
||||
main.cpp
|
||||
codeeditor.cpp
|
||||
codeeditor.h
|
||||
linenumberarea.h
|
||||
lammpsgui.cpp
|
||||
lammpsgui.h
|
||||
lammpsgui.ui
|
||||
|
||||
102
tools/lammps-gui/codeeditor.cpp
Normal file
102
tools/lammps-gui/codeeditor.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "codeeditor.h"
|
||||
#include "linenumberarea.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QTextBlock>
|
||||
|
||||
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
||||
{
|
||||
lineNumberArea = new LineNumberArea(this);
|
||||
|
||||
connect(this, &CodeEditor::blockCountChanged, this,
|
||||
&CodeEditor::updateLineNumberAreaWidth);
|
||||
connect(this, &CodeEditor::updateRequest, this,
|
||||
&CodeEditor::updateLineNumberArea);
|
||||
connect(this, &CodeEditor::cursorPositionChanged, this,
|
||||
&CodeEditor::highlightCurrentLine);
|
||||
|
||||
updateLineNumberAreaWidth(0);
|
||||
highlightCurrentLine();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
QPlainTextEdit::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);
|
||||
}
|
||||
|
||||
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(lineNumberArea);
|
||||
painter.fillRect(event->rect(), Qt::lightGray);
|
||||
QTextBlock block = firstVisibleBlock();
|
||||
int blockNumber = block.blockNumber();
|
||||
int top =
|
||||
qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
|
||||
int bottom = top + qRound(blockBoundingRect(block).height());
|
||||
while (block.isValid() && top <= event->rect().bottom()) {
|
||||
if (block.isVisible() && bottom >= event->rect().top()) {
|
||||
QString number = QString::number(blockNumber + 1);
|
||||
painter.setPen(Qt::black);
|
||||
painter.drawText(0, top, lineNumberArea->width(),
|
||||
fontMetrics().height(), Qt::AlignRight, number);
|
||||
}
|
||||
|
||||
block = block.next();
|
||||
top = bottom;
|
||||
bottom = top + qRound(blockBoundingRect(block).height());
|
||||
++blockNumber;
|
||||
}
|
||||
}
|
||||
40
tools/lammps-gui/codeeditor.h
Normal file
40
tools/lammps-gui/codeeditor.h
Normal file
@ -0,0 +1,40 @@
|
||||
/* -*- c++ -*- ----------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
https://www.lammps.org/, Sandia National Laboratories
|
||||
LAMMPS development team: developers@lammps.org
|
||||
|
||||
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
||||
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
||||
certain rights in this software. This software is distributed under
|
||||
the GNU General Public License.
|
||||
|
||||
See the README file in the top-level LAMMPS directory.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#ifndef CODEEDITOR_H
|
||||
#define CODEEDITOR_H
|
||||
|
||||
#include <QPlainTextEdit>
|
||||
|
||||
class CodeEditor : public QPlainTextEdit {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CodeEditor(QWidget *parent = nullptr);
|
||||
|
||||
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||
int lineNumberAreaWidth();
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void updateLineNumberAreaWidth(int newBlockCount);
|
||||
void highlightCurrentLine();
|
||||
void updateLineNumberArea(const QRect &rect, int dy);
|
||||
|
||||
private:
|
||||
QWidget *lineNumberArea;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,25 +1,26 @@
|
||||
#include "lammpsgui.h"
|
||||
#include "./ui_lammpsgui.h"
|
||||
#include "library.h"
|
||||
#include "ui_lammpsgui.h"
|
||||
|
||||
#include <string>
|
||||
#include <QFile>
|
||||
#include <QFileDialog>
|
||||
#include <QFont>
|
||||
#include <QTextStream>
|
||||
#include <QMessageBox>
|
||||
#include <QTextStream>
|
||||
#include <string>
|
||||
|
||||
LammpsGui::LammpsGui(QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::LammpsGui), lammps_handle(nullptr)
|
||||
#include "library.h"
|
||||
|
||||
LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
|
||||
QMainWindow(parent), ui(new Ui::LammpsGui), lammps_handle(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->setCentralWidget(ui->textEdit);
|
||||
current_file.clear();
|
||||
current_line = 0;
|
||||
|
||||
QFont text_font;
|
||||
text_font.setFamily("monospace");
|
||||
text_font.setFixedPitch(true);
|
||||
text_font.setStyleHint(QFont::TypeWriter);
|
||||
ui->textEdit->setCurrentFont(text_font);
|
||||
ui->textEdit->document()->setDefaultFont(text_font);
|
||||
|
||||
connect(ui->actionNew, &QAction::triggered, this, &LammpsGui::new_document);
|
||||
connect(ui->actionOpen, &QAction::triggered, this, &LammpsGui::open);
|
||||
@ -32,15 +33,23 @@ LammpsGui::LammpsGui(QWidget *parent)
|
||||
connect(ui->actionUndo, &QAction::triggered, this, &LammpsGui::undo);
|
||||
connect(ui->actionRedo, &QAction::triggered, this, &LammpsGui::redo);
|
||||
connect(ui->actionClear, &QAction::triggered, this, &LammpsGui::clear);
|
||||
connect(ui->actionRun_Buffer, &QAction::triggered, this, &LammpsGui::run_buffer);
|
||||
connect(ui->actionExecute_Line, &QAction::triggered, this, &LammpsGui::run_line);
|
||||
// connect(ui->actionAbout, &QAction::triggered, this, &LammpsGui::about);
|
||||
connect(ui->actionRun_Buffer, &QAction::triggered, this,
|
||||
&LammpsGui::run_buffer);
|
||||
connect(ui->actionExecute_Line, &QAction::triggered, this,
|
||||
&LammpsGui::run_line);
|
||||
// connect(ui->actionAbout, &QAction::triggered, this,
|
||||
// &LammpsGui::about);
|
||||
|
||||
#if !QT_CONFIG(clipboard)
|
||||
ui->actionCut->setEnabled(false);
|
||||
ui->actionCopy->setEnabled(false);
|
||||
ui->actionPaste->setEnabled(false);
|
||||
#endif
|
||||
|
||||
if (filename)
|
||||
open_file(filename);
|
||||
else
|
||||
setWindowTitle(QString("LAMMPS-GUI - *unknown*"));
|
||||
}
|
||||
|
||||
LammpsGui::~LammpsGui()
|
||||
@ -51,26 +60,32 @@ LammpsGui::~LammpsGui()
|
||||
void LammpsGui::new_document()
|
||||
{
|
||||
current_file.clear();
|
||||
current_line = 0;
|
||||
ui->textEdit->setText(QString());
|
||||
ui->textEdit->document()->setPlainText(QString());
|
||||
if (lammps_handle) lammps_close(lammps_handle);
|
||||
lammps_handle = nullptr;
|
||||
setWindowTitle(QString("LAMMPS-GUI - *unknown*"));
|
||||
}
|
||||
|
||||
void LammpsGui::open()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
|
||||
open_file(fileName);
|
||||
}
|
||||
|
||||
void LammpsGui::open_file(const QString &fileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
current_file = fileName;
|
||||
current_line = 0;
|
||||
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
|
||||
QMessageBox::warning(this, "Warning",
|
||||
"Cannot open file: " + file.errorString());
|
||||
return;
|
||||
}
|
||||
setWindowTitle(fileName);
|
||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
||||
QTextStream in(&file);
|
||||
QString text = in.readAll();
|
||||
ui->textEdit->setText(text);
|
||||
ui->textEdit->document()->setPlainText(text);
|
||||
ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
|
||||
file.close();
|
||||
}
|
||||
|
||||
@ -86,10 +101,11 @@ void LammpsGui::save()
|
||||
}
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
||||
QMessageBox::warning(this, "Warning",
|
||||
"Cannot save file: " + file.errorString());
|
||||
return;
|
||||
}
|
||||
setWindowTitle(fileName);
|
||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
||||
QTextStream out(&file);
|
||||
QString text = ui->textEdit->toPlainText();
|
||||
out << text;
|
||||
@ -102,11 +118,12 @@ void LammpsGui::save_as()
|
||||
QFile file(fileName);
|
||||
|
||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
||||
QMessageBox::warning(this, "Warning",
|
||||
"Cannot save file: " + file.errorString());
|
||||
return;
|
||||
}
|
||||
current_file = fileName;
|
||||
setWindowTitle(fileName);
|
||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
||||
QTextStream out(&file);
|
||||
QString text = ui->textEdit->toPlainText();
|
||||
out << text;
|
||||
@ -157,8 +174,12 @@ void LammpsGui::redo()
|
||||
|
||||
void LammpsGui::run_buffer()
|
||||
{
|
||||
char *args[] = {(char *)"LAMMPS GUI", (char *)"-log", (char *)"none"};
|
||||
int nargs = sizeof(args) / sizeof(char *);
|
||||
|
||||
clear();
|
||||
if (!lammps_handle) lammps_handle = lammps_open_no_mpi(0, nullptr, nullptr);
|
||||
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());
|
||||
@ -166,15 +187,23 @@ void LammpsGui::run_buffer()
|
||||
|
||||
void LammpsGui::run_line()
|
||||
{
|
||||
// dummy
|
||||
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()
|
||||
{
|
||||
if (lammps_handle) {
|
||||
lammps_command(lammps_handle, "clear");
|
||||
current_line = 0;
|
||||
}
|
||||
ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
|
||||
}
|
||||
|
||||
void LammpsGui::about()
|
||||
|
||||
@ -1,22 +1,38 @@
|
||||
/* -*- c++ -*- ----------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
https://www.lammps.org/, Sandia National Laboratories
|
||||
LAMMPS development team: developers@lammps.org
|
||||
|
||||
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
||||
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
||||
certain rights in this software. This software is distributed under
|
||||
the GNU General Public License.
|
||||
|
||||
See the README file in the top-level LAMMPS directory.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#ifndef LAMMPSGUI_H
|
||||
#define LAMMPSGUI_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QFont>
|
||||
#include <QString>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class LammpsGui; }
|
||||
namespace Ui {
|
||||
class LammpsGui;
|
||||
}
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class LammpsGui : public QMainWindow
|
||||
{
|
||||
class LammpsGui : public QMainWindow {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
LammpsGui(QWidget *parent = nullptr);
|
||||
LammpsGui(QWidget *parent = nullptr, const char *filename = nullptr);
|
||||
~LammpsGui();
|
||||
|
||||
protected:
|
||||
void open_file(const QString &filename);
|
||||
|
||||
private slots:
|
||||
void new_document();
|
||||
void open();
|
||||
@ -35,9 +51,9 @@ private slots:
|
||||
|
||||
private:
|
||||
Ui::LammpsGui *ui;
|
||||
|
||||
QString current_file;
|
||||
QFont text_font;
|
||||
int current_line;
|
||||
void *lammps_handle;
|
||||
};
|
||||
|
||||
#endif // LAMMPSGUI_H
|
||||
|
||||
@ -25,7 +25,7 @@
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QTextEdit" name="textEdit"/>
|
||||
<widget class="CodeEditor" name="textEdit"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
|
||||
58
tools/lammps-gui/linenumberarea.cpp
Normal file
58
tools/lammps-gui/linenumberarea.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#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);
|
||||
}
|
||||
38
tools/lammps-gui/linenumberarea.h
Normal file
38
tools/lammps-gui/linenumberarea.h
Normal file
@ -0,0 +1,38 @@
|
||||
/* -*- c++ -*- ----------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
https://www.lammps.org/, Sandia National Laboratories
|
||||
LAMMPS development team: developers@lammps.org
|
||||
|
||||
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
||||
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
||||
certain rights in this software. This software is distributed under
|
||||
the GNU General Public License.
|
||||
|
||||
See the README file in the top-level LAMMPS directory.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#ifndef LINENUMBERAREA_H
|
||||
#define LINENUMBERAREA_H
|
||||
|
||||
#include "codeeditor.h"
|
||||
#include <QWidget>
|
||||
|
||||
class LineNumberArea : public QWidget {
|
||||
public:
|
||||
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor) {}
|
||||
|
||||
QSize sizeHint() const override
|
||||
{
|
||||
return QSize(codeEditor->lineNumberAreaWidth(), 0);
|
||||
}
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override
|
||||
{
|
||||
codeEditor->lineNumberAreaPaintEvent(event);
|
||||
}
|
||||
|
||||
private:
|
||||
CodeEditor *codeEditor;
|
||||
};
|
||||
#endif
|
||||
@ -5,7 +5,11 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
LammpsGui w;
|
||||
|
||||
const char *infile = nullptr;
|
||||
if (argc > 1) infile = argv[1];
|
||||
|
||||
LammpsGui w(nullptr, infile);
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user