simplify, move redundant operations to functions, update coding style
This commit is contained in:
@ -12,7 +12,7 @@ BraceWrapping:
|
||||
BreakBeforeBraces: Custom
|
||||
BreakInheritanceList: AfterColon
|
||||
BreakConstructorInitializers: AfterColon
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 100
|
||||
IndentCaseLabels: true
|
||||
IndentWidth: 4
|
||||
ObjCBlockIndentWidth: 4
|
||||
|
||||
@ -8,12 +8,9 @@ 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);
|
||||
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
|
||||
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
|
||||
connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
|
||||
|
||||
updateLineNumberAreaWidth(0);
|
||||
highlightCurrentLine();
|
||||
@ -43,8 +40,7 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
|
||||
if (dy)
|
||||
lineNumberArea->scroll(0, dy);
|
||||
else
|
||||
lineNumberArea->update(0, rect.y(), lineNumberArea->width(),
|
||||
rect.height());
|
||||
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
|
||||
|
||||
if (rect.contains(viewport()->rect())) updateLineNumberAreaWidth(0);
|
||||
}
|
||||
@ -54,8 +50,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
|
||||
QPlainTextEdit::resizeEvent(e);
|
||||
|
||||
QRect cr = contentsRect();
|
||||
lineNumberArea->setGeometry(
|
||||
QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
||||
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
||||
}
|
||||
|
||||
void CodeEditor::highlightCurrentLine()
|
||||
@ -83,15 +78,14 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
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());
|
||||
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);
|
||||
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
|
||||
Qt::AlignRight, number);
|
||||
}
|
||||
|
||||
block = block.next();
|
||||
@ -100,3 +94,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
++blockNumber;
|
||||
}
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -38,3 +38,6 @@ private:
|
||||
};
|
||||
|
||||
#endif
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#include "ui_lammpsgui.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QFileInfo>
|
||||
#include <QFont>
|
||||
#include <QMessageBox>
|
||||
#include <QTextStream>
|
||||
@ -33,14 +34,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
|
||||
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_LAMMPS_GUI, &QAction::triggered, this,
|
||||
&LammpsGui::about);
|
||||
connect(ui->actionLAMMPS_Info, &QAction::triggered, this,
|
||||
&LammpsGui::about_lammps);
|
||||
connect(ui->actionRun_Buffer, &QAction::triggered, this, &LammpsGui::run_buffer);
|
||||
connect(ui->actionAbout_LAMMPS_GUI, &QAction::triggered, this, &LammpsGui::about);
|
||||
|
||||
#if !QT_CONFIG(clipboard)
|
||||
ui->actionCut->setEnabled(false);
|
||||
@ -74,13 +69,17 @@ void LammpsGui::open()
|
||||
open_file(fileName);
|
||||
}
|
||||
|
||||
// open file and switch CWD to path of file
|
||||
void LammpsGui::open_file(const QString &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)) {
|
||||
QMessageBox::warning(this, "Warning",
|
||||
"Cannot open file: " + file.errorString());
|
||||
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
|
||||
return;
|
||||
}
|
||||
setWindowTitle(QString("LAMMPS-GUI - " + fileName));
|
||||
@ -91,20 +90,13 @@ void LammpsGui::open_file(const QString &fileName)
|
||||
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);
|
||||
QFileInfo path(file);
|
||||
current_file = path.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(QString("LAMMPS-GUI - " + fileName));
|
||||
@ -114,22 +106,19 @@ void LammpsGui::save()
|
||||
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()
|
||||
{
|
||||
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
|
||||
QFile 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();
|
||||
write_file(fileName);
|
||||
}
|
||||
|
||||
void LammpsGui::quit()
|
||||
@ -176,30 +165,13 @@ 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(nargs, args, nullptr);
|
||||
start_lammps();
|
||||
if (!lammps_handle) return;
|
||||
clear();
|
||||
std::string buffer = ui->textEdit->toPlainText().toStdString();
|
||||
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()
|
||||
{
|
||||
if (lammps_handle) {
|
||||
@ -208,21 +180,26 @@ void LammpsGui::clear()
|
||||
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"};
|
||||
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)
|
||||
lammps_handle = lammps_open_no_mpi(nargs, args, nullptr);
|
||||
|
||||
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);
|
||||
QMessageBox::warning(this, "LAMMPS Error", "Internal error launching LAMMPS");
|
||||
}
|
||||
|
||||
void LammpsGui::about()
|
||||
{
|
||||
QMessageBox::information(this, "About LAMMPS-GUI",
|
||||
"This is LAMMPS-GUI version 0.1");
|
||||
}
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -32,6 +32,8 @@ public:
|
||||
|
||||
protected:
|
||||
void open_file(const QString &filename);
|
||||
void write_file(const QString &filename);
|
||||
void start_lammps();
|
||||
|
||||
private slots:
|
||||
void new_document();
|
||||
@ -46,15 +48,17 @@ private slots:
|
||||
void redo();
|
||||
void clear();
|
||||
void run_buffer();
|
||||
void run_line();
|
||||
void about();
|
||||
void about_lammps();
|
||||
|
||||
private:
|
||||
Ui::LammpsGui *ui;
|
||||
|
||||
QString current_file;
|
||||
QString current_dir;
|
||||
void *lammps_handle;
|
||||
};
|
||||
|
||||
#endif // LAMMPSGUI_H
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -68,19 +68,11 @@
|
||||
</property>
|
||||
<addaction name="actionClear"/>
|
||||
<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>
|
||||
<addaction name="menuFile"/>
|
||||
<addaction name="menuEdit"/>
|
||||
<addaction name="menuRun"/>
|
||||
<addaction name="menuAbout"/>
|
||||
<addaction name="actionAbout_LAMMPS_GUI"/>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
<action name="actionNew">
|
||||
@ -143,19 +135,9 @@
|
||||
<string>Execute Buffer</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionExecute_Line">
|
||||
<property name="text">
|
||||
<string>Execute Line</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionAbout_LAMMPS_GUI">
|
||||
<property name="text">
|
||||
<string>About LAMMPS-GUI</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionLAMMPS_Info">
|
||||
<property name="text">
|
||||
<string>LAMMPS Info</string>
|
||||
<string>About</string>
|
||||
</property>
|
||||
</action>
|
||||
</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;
|
||||
};
|
||||
#endif
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
@ -13,3 +13,7 @@ int main(int argc, char *argv[])
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
|
||||
Reference in New Issue
Block a user