RedPanda-CPP/RedPandaIDE/mainwindow.cpp

170 lines
4.3 KiB
C++
Raw Normal View History

2021-04-06 23:10:57 +08:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
2021-04-07 21:13:15 +08:00
#include "editorlist.h"
#include "editor.h"
2021-04-11 21:33:08 +08:00
#include "systemconsts.h"
#include "settings.h"
2021-04-09 17:48:25 +08:00
#include <QCloseEvent>
2021-04-18 11:41:41 +08:00
#include <QComboBox>
2021-04-11 21:33:08 +08:00
#include <QFileDialog>
#include <QLabel>
2021-04-16 22:04:48 +08:00
#include <settingsdialog/settingsdialog.h>
MainWindow* pMainWindow;
2021-04-06 23:10:57 +08:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// status bar
2021-04-18 11:41:41 +08:00
mFileInfoStatus=new QLabel();
mFileEncodingStatus = new QLabel();
mFileInfoStatus->setStyleSheet("margin-left:10px; margin-right:10px");
mFileEncodingStatus->setStyleSheet("margin-left:10px; margin-right:10px");
ui->statusbar->addWidget(mFileInfoStatus);
ui->statusbar->addWidget(mFileEncodingStatus);
2021-04-07 21:13:15 +08:00
mEditorList = new EditorList(ui->EditorTabsLeft,
ui->EditorTabsRight,
ui->EditorPanelSplitter,
ui->EditorPanel);
setupActions();
2021-04-07 22:44:08 +08:00
ui->EditorTabsRight->setVisible(false);
2021-04-18 11:41:41 +08:00
mCompilerSet = new QComboBox();
ui->toolbarCompilerSet->addWidget(mCompilerSet);
connect(mCompilerSet,QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &MainWindow::onCompilerSetChanged);
updateCompilerSet();
2021-04-06 23:10:57 +08:00
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateStatusBarForEncoding() {
Editor * editor = mEditorList->getEditor();
if (editor!=NULL) {
mFileEncodingStatus->setText(editor->fileEncoding());
}
2021-04-11 13:55:31 +08:00
}
void MainWindow::updateStatusBarForEditingInfo(int line,int col,int lines,int charCount)
{
Editor * editor = mEditorList->getEditor();
if (editor!=NULL) {
mFileInfoStatus->setText(
QString(tr("Line: %1 Col: %2 Lines: %3 Chars: %4")).arg(line)
.arg(col).arg(lines).arg(charCount));
}
}
2021-04-11 21:33:08 +08:00
void MainWindow::openFiles(const QStringList &files)
{
mEditorList->beginUpdate();
auto end = finally([this] {
this->mEditorList->endUpdate();
});
2021-04-11 21:33:08 +08:00
for (QString file:files) {
openFile(file);
}
mEditorList->endUpdate();
}
void MainWindow::openFile(const QString &filename)
{
Editor* editor = mEditorList->findOpenedEditor(filename);
if (editor!=nullptr) {
editor->activate();
2021-04-11 21:33:08 +08:00
return;
}
editor = mEditorList->newEditor(filename,ENCODING_AUTO_DETECT,
false,false);
editor->activate();
2021-04-11 21:33:08 +08:00
this->updateStatusBarForEncoding();
}
2021-04-07 21:13:15 +08:00
void MainWindow::setupActions() {
}
2021-04-18 11:41:41 +08:00
void MainWindow::updateCompilerSet()
{
mCompilerSet->clear();
int index=pSettings->compilerSets().defaultIndex();
for (int i=0;i<pSettings->compilerSets().list().size();i++) {
mCompilerSet->addItem(pSettings->compilerSets().list()[i]->name());
}
if (index < 0 || index>=mCompilerSet->count()) {
index = 0;
}
mCompilerSet->setCurrentIndex(index);
}
2021-04-07 21:13:15 +08:00
void MainWindow::on_actionNew_triggered()
{
Editor * editor=mEditorList->newEditor("",ENCODING_AUTO_DETECT,false,true);
editor->activate();
updateStatusBarForEncoding();
2021-04-07 21:13:15 +08:00
}
2021-04-07 22:44:08 +08:00
void MainWindow::on_EditorTabsLeft_tabCloseRequested(int index)
{
Editor* editor = mEditorList->getEditor(index);
mEditorList->closeEditor(editor);
}
void MainWindow::on_actionOpen_triggered()
{
2021-04-11 21:33:08 +08:00
QString selectedFileFilter = pSystemConsts->defaultFileFilter();
QStringList files = QFileDialog::getOpenFileNames(pMainWindow,
tr("Open"), QString(), pSystemConsts->defaultFileFilters().join(";;"),
&selectedFileFilter);
openFiles(files);
2021-04-07 22:44:08 +08:00
}
2021-04-09 17:48:25 +08:00
void MainWindow::closeEvent(QCloseEvent *event) {
if (!mEditorList->closeAll(true)) {
event->ignore();
return ;
}
delete mEditorList;
event->accept();
return;
}
void MainWindow::on_actionSave_triggered()
{
Editor * editor = mEditorList->getEditor();
if (editor != NULL) {
editor->save();
}
}
void MainWindow::on_actionSaveAs_triggered()
{
Editor * editor = mEditorList->getEditor();
if (editor != NULL) {
editor->saveAs();
}
}
2021-04-16 22:04:48 +08:00
void MainWindow::on_actionOptions_triggered()
{
SettingsDialog settingsDialog;
settingsDialog.exec();
}
2021-04-18 11:41:41 +08:00
void MainWindow::onCompilerSetChanged(int index)
{
if (index<0)
return;
pSettings->compilerSets().setDefaultIndex(index);
pSettings->compilerSets().saveDefaultIndex();
}