<commit_before>#include "mainwindow.h"

#include <string>

#include <QDesktopServices>

#include "query_widget.h"
#include "codeview_widget.h"
#include "session.h"

MainWindow::MainWindow() {
	setWindowTitle(tr("CppQuery"));

	QDockWidget *searchResultDock = new QDockWidget(tr("Search Results"), this);
	QDockWidget *queryTextDock = new QDockWidget(tr("Query"), this);

	searchResultDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
	queryTextDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);

	searchResults = new QTableView(searchResultDock);
	queryWidget = new QueryWidget(queryTextDock);

	searchResults->setSelectionMode(QAbstractItemView::NoSelection);
	QStandardItemModel* model = new QStandardItemModel(0, 6, searchResults);
	QStringList headers;
	headers << tr("id") << tr("Filename") << tr("Start Line") << tr("Start Col") << tr("End Line") << tr("End Col");
	model->setHorizontalHeaderLabels(headers);

	searchResults->setModel(model);
	searchResults->setGridStyle(Qt::NoPen);
	searchResults->setAlternatingRowColors(true);
	searchResults->verticalHeader()->hide();
	searchResults->setEditTriggers(QAbstractItemView::NoEditTriggers);
	searchResults->setSelectionMode(QAbstractItemView::NoSelection);
	searchResults->setDragEnabled(false);
	searchResults->setTextElideMode(Qt::ElideLeft);
	searchResults->resizeColumnsToContents();
	searchResults->horizontalHeader()->setStretchLastSection(true);

	searchResultDock->setWidget(searchResults);
	queryTextDock->setWidget(queryWidget);

	addDockWidget(Qt::LeftDockWidgetArea, searchResultDock);
	addDockWidget(Qt::BottomDockWidgetArea, queryTextDock);

	resultText = new CodeViewArea(this);
	setCentralWidget(resultText);
	resultText->setReadOnly(true);
	resultText->setLineWrapMode(QPlainTextEdit::NoWrap);

	createMenuBar();

	statusBar()->showMessage(tr("Ready"));

	parser = new ParserWorker(this);
	parseProgress = new QProgressDialog(tr("Parsing files..."), tr("Cancel"), 0, 0, this);
	parseProgress->setWindowModality(Qt::WindowModal);
	parseProgress->hide();

	connect(queryWidget, &QueryWidget::executeQuery, this, &MainWindow::executeQuery);
	connect(searchResults, &QTableView::doubleClicked, this, &MainWindow::openResult);

	// Connect the options to the worker thread;
	connect(parser, &ParserWorker::filesDone,[this](int i) {
		parseProgress->setValue(i);
	});

	connect(parser, &ParserWorker::parseDone,[this]() {
		parseProgress->hide();
	});

	// Cancel button terminates the parser thread
	connect(parseProgress, &QProgressDialog::canceled, [this](){
		parser->terminate();
		session.reset(nullptr);
	});
}

MainWindow::~MainWindow() {
	delete searchResults->model();
	delete parser;
}

void MainWindow::createMenuBar() {
	openAct = new QAction(tr("&Open..."), this);
	exitAct = new QAction(tr("E&xit"), this);
	matcherHelpAct = new QAction(tr("Matcher reference"), this);
	aboutAct = new QAction(tr("About"), this);
	aboutQtAct = new QAction(tr("About Qt"), this);

	openAct->setShortcuts(QKeySequence::Open);
	openAct->setStatusTip(tr("Open a JSON compilation database."));

	exitAct->setShortcuts(QKeySequence::Quit);

	connect(openAct, &QAction::triggered, this, &MainWindow::open);
	connect(exitAct, &QAction::triggered, this, &MainWindow::close);
	connect(matcherHelpAct, &QAction::triggered, this, &MainWindow::openMatcherReference);
	connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
	connect(aboutQtAct, &QAction::triggered, this, &QApplication::aboutQt);

	fileMenu = menuBar()->addMenu(tr("&File"));
	helpMenu = menuBar()->addMenu(tr("&Help"));

	fileMenu->addAction(openAct);
	fileMenu->addAction(exitAct);

	helpMenu->addAction(matcherHelpAct);
	helpMenu->addAction(aboutAct);
	helpMenu->addAction(aboutQtAct);
}

void MainWindow::open() {
	// TODO: move parsing to other thread!

	std::string fileName = QFileDialog::getOpenFileName(this,
    	tr("Open Compilation Database"), "", tr("JSON files (*.json)")).toStdString();

	if (fileName.empty()) return;

	try {
		session.reset(new Session(fileName));

		parseProgress->show();
		parseProgress->setMaximum(session->getFileCount());
		parseProgress->setValue(0);

		parser->setSession(session.get());
		parser->start();
	}
	catch (DatabaseError& e) {
		QMessageBox::critical(this, tr("Error"), tr("Unable to open compilation database: ") + QString::fromStdString(e.getReason()));
	}
}

void MainWindow::about() {
    QMessageBox::about(this, tr("About Menu"),
            tr("A query language based on Clang's ASTMatcher library, to provide the programmers with a more efficient way to navigate in huge codebases than simple text searches."));
}

void MainWindow::openMatcherReference() {
	QDesktopServices::openUrl(QUrl("http://clang.llvm.org/docs/LibASTMatchersReference.html"));
}
<commit_msg>The results from former session are no longer preserved when opening a new compilation database.<commit_after>#include "mainwindow.h"

#include <string>

#include <QDesktopServices>

#include "query_widget.h"
#include "codeview_widget.h"
#include "session.h"

MainWindow::MainWindow() {
	setWindowTitle(tr("CppQuery"));

	QDockWidget *searchResultDock = new QDockWidget(tr("Search Results"), this);
	QDockWidget *queryTextDock = new QDockWidget(tr("Query"), this);

	searchResultDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
	queryTextDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);

	searchResults = new QTableView(searchResultDock);
	queryWidget = new QueryWidget(queryTextDock);

	searchResults->setSelectionMode(QAbstractItemView::NoSelection);
	QStandardItemModel* model = new QStandardItemModel(0, 6, searchResults);
	QStringList headers;
	headers << tr("id") << tr("Filename") << tr("Start Line") << tr("Start Col") << tr("End Line") << tr("End Col");
	model->setHorizontalHeaderLabels(headers);

	searchResults->setModel(model);
	searchResults->setGridStyle(Qt::NoPen);
	searchResults->setAlternatingRowColors(true);
	searchResults->verticalHeader()->hide();
	searchResults->setEditTriggers(QAbstractItemView::NoEditTriggers);
	searchResults->setSelectionMode(QAbstractItemView::NoSelection);
	searchResults->setDragEnabled(false);
	searchResults->setTextElideMode(Qt::ElideLeft);
	searchResults->resizeColumnsToContents();
	searchResults->horizontalHeader()->setStretchLastSection(true);

	searchResultDock->setWidget(searchResults);
	queryTextDock->setWidget(queryWidget);

	addDockWidget(Qt::LeftDockWidgetArea, searchResultDock);
	addDockWidget(Qt::BottomDockWidgetArea, queryTextDock);

	resultText = new CodeViewArea(this);
	setCentralWidget(resultText);
	resultText->setReadOnly(true);
	resultText->setLineWrapMode(QPlainTextEdit::NoWrap);

	createMenuBar();

	statusBar()->showMessage(tr("Ready"));

	parser = new ParserWorker(this);
	parseProgress = new QProgressDialog(tr("Parsing files..."), tr("Cancel"), 0, 0, this);
	parseProgress->setWindowModality(Qt::WindowModal);
	parseProgress->hide();

	connect(queryWidget, &QueryWidget::executeQuery, this, &MainWindow::executeQuery);
	connect(searchResults, &QTableView::doubleClicked, this, &MainWindow::openResult);

	// Connect the options to the worker thread;
	connect(parser, &ParserWorker::filesDone,[this](int i) {
		parseProgress->setValue(i);
	});

	connect(parser, &ParserWorker::parseDone,[this]() {
		parseProgress->hide();
	});

	// Cancel button terminates the parser thread
	connect(parseProgress, &QProgressDialog::canceled, [this](){
		parser->terminate();
		session.reset(nullptr);
	});
}

MainWindow::~MainWindow() {
	delete searchResults->model();
	delete parser;
}

void MainWindow::createMenuBar() {
	openAct = new QAction(tr("&Open..."), this);
	exitAct = new QAction(tr("E&xit"), this);
	matcherHelpAct = new QAction(tr("Matcher reference"), this);
	aboutAct = new QAction(tr("About"), this);
	aboutQtAct = new QAction(tr("About Qt"), this);

	openAct->setShortcuts(QKeySequence::Open);
	openAct->setStatusTip(tr("Open a JSON compilation database."));

	exitAct->setShortcuts(QKeySequence::Quit);

	connect(openAct, &QAction::triggered, this, &MainWindow::open);
	connect(exitAct, &QAction::triggered, this, &MainWindow::close);
	connect(matcherHelpAct, &QAction::triggered, this, &MainWindow::openMatcherReference);
	connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
	connect(aboutQtAct, &QAction::triggered, this, &QApplication::aboutQt);

	fileMenu = menuBar()->addMenu(tr("&File"));
	helpMenu = menuBar()->addMenu(tr("&Help"));

	fileMenu->addAction(openAct);
	fileMenu->addAction(exitAct);

	helpMenu->addAction(matcherHelpAct);
	helpMenu->addAction(aboutAct);
	helpMenu->addAction(aboutQtAct);
}

void MainWindow::open() {
	// TODO: move parsing to other thread!

	std::string fileName = QFileDialog::getOpenFileName(this,
    	tr("Open Compilation Database"), "", tr("JSON files (*.json)")).toStdString();

	if (fileName.empty()) return;

	try {
		session.reset(new Session(fileName));

		parseProgress->show();
		parseProgress->setMaximum(session->getFileCount());
		parseProgress->setValue(0);

		parser->setSession(session.get());
		parser->start();


		// Remove the results of the session that was open earlier
		QAbstractItemModel* model = searchResults->model();
		model->removeRows(0, model->rowCount());
		searchResults->setModel(model);
	}
	catch (DatabaseError& e) {
		QMessageBox::critical(this, tr("Error"), tr("Unable to open compilation database: ") + QString::fromStdString(e.getReason()));
	}
}

void MainWindow::about() {
    QMessageBox::about(this, tr("About Menu"),
            tr("A query language based on Clang's ASTMatcher library, to provide the programmers with a more efficient way to navigate in huge codebases than simple text searches."));
}

void MainWindow::openMatcherReference() {
	QDesktopServices::openUrl(QUrl("http://clang.llvm.org/docs/LibASTMatchersReference.html"));
}

{'fraction_non_alphanumeric': 0.15422584869547698, 'fraction_numerical': 0.001108089050065478, 'mean_word_length': 7.724077328646748, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1156649', 'n_tokens_mistral': 3164, 'n_tokens_neox': 3072, 'n_words': 558}