⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwindow.cpp

📁 编译器 用C语言对下述文法和单词表定义的语言设计编制一个编译器
💻 CPP
字号:
/*************************************************
 * mainwindow.cpp
 *
 * 2008-03-19 21:19
 * 周鑫(zhouxin63766@yahoo.com.cn)
 *
 * Make a GUI for parser.
 *****************************************************************************/

#include <QtGui>
#include "mainwindow.h"

MainWindow::MainWindow()
{
	textEdit = new QTextEdit;
	parser = new Parser;
	QFont font( "Times New Roman", 12, QFont::Bold );
	textEdit->setFont(font);
	//textEdit->setReadOnly( true );
	setCentralWidget( textEdit );

	createActions();
	createMenus();
	createDockWindows();

	connect( textEdit, SIGNAL( textChanged() ), this, SLOT( setFileChanged() ) );
	setWindowIcon( QIcon( ":/images/icon.png" ) );
	setWindowTitle( tr("parser") );
}

MainWindow::~MainWindow()
{
	delete textEdit;
	delete parser;
}

void MainWindow::createMenus()
{
	fileMenu = menuBar()->addMenu( tr("&File" ) );
	fileMenu->addAction( newAction );
	fileMenu->addAction( openAction );
	fileMenu->addAction( reloadAction );
	fileMenu->addAction( saveAction );
	fileMenu->addAction( saveAsAction );
	fileMenu->addSeparator();
	fileMenu->addAction( exitAction );

	buildMenu = menuBar()->addMenu( tr("&Build") );
	buildMenu->addAction( compileAction );

	viewMenu = menuBar()->addMenu( tr("&View") );

	helpMenu = menuBar()->addMenu( tr("&Help") );
	helpMenu->addAction( aboutAction );
}

void MainWindow::createActions()
{
	newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this );
	newAction->setShortcut( tr("Ctrl+N") );
	connect( newAction, SIGNAL( activated() ), this, SLOT( newFile() ) );

	openAction = new QAction( QIcon(":/images/open.png"), tr("&Open"), this );
	openAction->setShortcut( tr("Ctrl+O") );
	connect( openAction, SIGNAL( activated() ), this, SLOT( openFile() ) );

	reloadAction = new QAction( QIcon(":/images/reload.png"), tr("&Reload"), this );
	reloadAction->setShortcut( tr("Ctrl+R") );
	connect( reloadAction, SIGNAL( activated() ), this, SLOT( reloadFile() ) );

	saveAction = new QAction( QIcon(":/images/save.png"), tr("&Save"), this );
	saveAction->setShortcut( tr("Ctrl+S") );
	connect( saveAction, SIGNAL( activated() ), this, SLOT( saveFile() ) );

	saveAsAction = new QAction( QIcon(":/images/saveas.png"), tr("Save &As..."), this );
	saveAsAction->setShortcut( tr("Ctrl+SHIFT+S") );
	connect( saveAsAction, SIGNAL( activated() ), this, SLOT( saveAs() ) );

	compileAction = new QAction( QIcon(":/images/compile.png"), tr("&Compile"), this );
	compileAction->setShortcut( QKeySequence( Qt::Key_F5 ) );
	connect( compileAction, SIGNAL( activated() ), this, SLOT( compile() ) );

	exitAction = new QAction( QIcon(":/images/exit.png"), tr("&Exit"), this );
	exitAction->setShortcut( tr("Ctrl+W") );
	connect( exitAction, SIGNAL( activated() ), qApp, SLOT( quit() ) );

	aboutAction = new QAction( QIcon(":/images/about.png"), tr("&About"), this );
	aboutAction->setShortcut( tr("Ctrl+A") );
	connect( aboutAction, SIGNAL( activated() ), this, SLOT( about() ) );
}

void MainWindow::createDockWindows()
{
	QDockWidget *dock = new QDockWidget( tr("Output" ), this );
	dock->setAllowedAreas( Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea );
	debugList = new QListWidget( dock );
	debugList->addItems( QStringList()
		<< "Line 1\t Harmony Enterprises, 12 Lakeside, Ambleton"
		<< "Line 2\t Memorabilia, 23 Watersedge, Beaton"
		<< "Line 3\t Tiblanka, 38 Sea Views, Carlton"
		<< "Line 4\t Tiroli Tea, 67 Long River, Fedula" );
	dock->setWidget( debugList );
	addDockWidget( Qt::BottomDockWidgetArea, dock );
	viewMenu->addAction( dock->toggleViewAction() );
}

void MainWindow::newFile()
{
	currentFile = QString();
	setWindowTitle( tr("Parser *") );
	textEdit->clear();
	debugList->clear();
}

void MainWindow::openFile()
{
	QString fileName = QFileDialog::getOpenFileName( this, "Open File", "./",
			"PASCAL Files( *.pas *.txt )"); 
	if ( fileName.isEmpty() )
	{
		return;
	}
	else
	{
		loadFile( fileName );
	}
}

void MainWindow::reloadFile()
{
	if ( currentFile.isEmpty() )
	{
		QMessageBox::information( this, tr("Reload File Error!"),
				tr("Please open a file first!") );
	}
	else
	{
		loadFile( currentFile );
	}
}

void MainWindow::loadFile( const QString &fileName )
{
	QFile file( fileName );
	if ( !file.open( QFile::ReadOnly | QFile::Text ) )
	{
		QMessageBox::warning( this, tr("Load Files"),
			tr("Cannot read file %1:\n%2.").
			arg(fileName).arg(file.errorString()) );
		return;
	}

	QTextStream in( &file );
	QApplication::setOverrideCursor( Qt::WaitCursor );
	textEdit->setPlainText( in.readAll() );
	QApplication::restoreOverrideCursor();

	currentFile = fileName;
	if ( currentFile.isEmpty() )
	{
		setWindowTitle( tr("Parser") );
	}
	else
	{
		setWindowTitle( tr("%1 - %2").arg( tr("Parser File") )
				.arg( QFileInfo( currentFile ).fileName() ) );
	}

	debugList->clear();
}

void MainWindow::saveFile()
{
	if ( currentFile.isEmpty() )
	{
		saveAs();
	}
	else
	{
		saveFile( currentFile );
	}
}

void MainWindow::saveAs()
{
	QString fileName = QFileDialog::getSaveFileName( this );
	if ( fileName.isEmpty() )
	{
		return;
	}
	else
	{
		saveFile( fileName );
	}
}

void MainWindow::saveFile( const QString &fileName )
{
	QFile file( fileName );
	if ( !file.open( QFile::WriteOnly | QFile::Text ) )
	{
		QMessageBox::warning( this, tr("Recent Files"),
				tr("Cannot write file %1:\n%2.")
				.arg( fileName ).arg( file.errorString() ) );
		return;
	}

	QTextStream out( &file );
	QApplication::setOverrideCursor( Qt::WaitCursor );
	out << textEdit->toPlainText();
	QApplication::restoreOverrideCursor();

	currentFile = fileName;
	setWindowTitle( tr("%1 - %2").arg( tr("Parser File") )
				.arg( QFileInfo( fileName ).fileName() ) );
}

void MainWindow::compile()
{
	debugList->clear();

	if ( currentFile.isEmpty() )
	{
		QMessageBox::information( this, tr("Compile File Error!"),
				tr("Please open or save a file first!") );
	}
	else
	{
		if ( parser->build( currentFile ) )
		{
			debugList->addItems( parser->errorList() );
			QMessageBox::information( this, tr("Compile Successfully!"),
					QString("File %1 compiled successfully.").arg( currentFile ) );
		}
		else
		{
			debugList->addItems( parser->errorList() );
			QMessageBox::information( this, tr("Compile Error!"),
					QString("Error occure when compile file %1.\n"
						"Details description are in dock window.")
					.arg( currentFile ) );
		}
	}
}

void MainWindow::about()
{
	QMessageBox::about( this, tr("About parser!"),
			tr( "<h2><center>parser 1.0</center></h2>"
				"<p>Copyright &copy; 2008 xidian University.</p>"
				"parser is an application that read "
				"<b><font color=red>PASCAL source file</font></b> "
				"then analyse the glossary and the syntax.<br>"
				"Contact to:<a href='mailto:zhouxin63766@yahoo.com.cn'>"
				"zhouxin63766@yahoo.com.cn</a>" ) );
}

void MainWindow::setFileChanged()
{
	setWindowTitle( tr("%1 - %2 *").arg( tr("Parser File") )
				.arg( QFileInfo( currentFile ).fileName() ) );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -