nextpnr/gui/basewindow.cc

467 lines
17 KiB
C++
Raw Normal View History

2018-06-22 22:21:20 +08:00
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.com>
2018-07-27 09:28:01 +08:00
* Copyright (C) 2018 Serge Bazanski <q3k@symbioticeda.com>
2018-06-22 22:21:20 +08:00
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <QAction>
2018-07-16 02:31:42 +08:00
#include <QCoreApplication>
#include <QFileDialog>
#include <QGridLayout>
#include <QIcon>
2018-08-03 00:50:08 +08:00
#include <QInputDialog>
#include <QSplitter>
2018-08-03 01:25:20 +08:00
#include <fstream>
2018-06-15 02:03:59 +08:00
#include "designwidget.h"
#include "fpgaviewwidget.h"
2018-08-03 01:25:20 +08:00
#include "jsonparse.h"
#include "log.h"
2018-06-21 19:55:36 +08:00
#include "mainwindow.h"
2018-08-09 00:14:22 +08:00
#include "project.h"
2018-06-15 02:03:59 +08:00
#include "pythontab.h"
2018-06-22 19:10:27 +08:00
static void initBasenameResource() { Q_INIT_RESOURCE(base); }
NEXTPNR_NAMESPACE_BEGIN
2018-08-04 17:52:07 +08:00
BaseMainWindow::BaseMainWindow(std::unique_ptr<Context> context, ArchArgs args, QWidget *parent)
: QMainWindow(parent), chipArgs(args), ctx(std::move(context)), timing_driven(false)
2018-06-15 00:37:57 +08:00
{
2018-06-22 19:10:27 +08:00
initBasenameResource();
qRegisterMetaType<std::string>();
log_files.clear();
log_streams.clear();
2018-07-14 23:58:58 +08:00
setObjectName("BaseMainWindow");
resize(1024, 768);
2018-06-15 00:37:57 +08:00
2018-08-03 00:50:08 +08:00
task = new TaskManager();
2018-08-03 01:21:25 +08:00
// Create and deploy widgets on main screen
QWidget *centralWidget = new QWidget(this);
QGridLayout *gridLayout = new QGridLayout(centralWidget);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(11, 11, 11, 11);
QSplitter *splitter_h = new QSplitter(Qt::Horizontal, centralWidget);
QSplitter *splitter_v = new QSplitter(Qt::Vertical, splitter_h);
splitter_h->addWidget(splitter_v);
gridLayout->addWidget(splitter_h, 0, 0, 1, 1);
setCentralWidget(centralWidget);
2018-06-15 00:37:57 +08:00
2018-07-16 02:31:42 +08:00
designview = new DesignWidget();
2018-06-15 02:24:05 +08:00
designview->setMinimumWidth(300);
splitter_h->addWidget(designview);
2018-06-29 00:06:31 +08:00
tabWidget = new QTabWidget();
2018-07-14 20:06:05 +08:00
console = new PythonTab();
tabWidget->addTab(console, "Console");
2018-06-21 20:12:02 +08:00
centralTabWidget = new QTabWidget();
centralTabWidget->setTabsClosable(true);
2018-07-26 19:21:46 +08:00
fpgaView = new FPGAViewWidget();
centralTabWidget->addTab(fpgaView, "Device");
2018-08-02 23:05:55 +08:00
centralTabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, 0);
centralTabWidget->tabBar()->setTabButton(0, QTabBar::LeftSide, 0);
2018-06-26 21:47:22 +08:00
2018-08-03 01:21:25 +08:00
splitter_v->addWidget(centralTabWidget);
splitter_v->addWidget(tabWidget);
2018-08-03 01:25:20 +08:00
// Connect Worker
2018-08-03 16:53:38 +08:00
connect(task, &TaskManager::log, this, &BaseMainWindow::writeInfo);
connect(task, &TaskManager::pack_finished, this, &BaseMainWindow::pack_finished);
connect(task, &TaskManager::budget_finish, this, &BaseMainWindow::budget_finish);
connect(task, &TaskManager::place_finished, this, &BaseMainWindow::place_finished);
connect(task, &TaskManager::route_finished, this, &BaseMainWindow::route_finished);
connect(task, &TaskManager::taskCanceled, this, &BaseMainWindow::taskCanceled);
connect(task, &TaskManager::taskStarted, this, &BaseMainWindow::taskStarted);
connect(task, &TaskManager::taskPaused, this, &BaseMainWindow::taskPaused);
2018-08-03 01:21:25 +08:00
2018-08-03 01:25:20 +08:00
// Events for context change
2018-08-03 16:53:38 +08:00
connect(this, &BaseMainWindow::contextChanged, task, &TaskManager::contextChanged);
connect(this, &BaseMainWindow::contextChanged, console, &PythonTab::newContext);
connect(this, &BaseMainWindow::contextChanged, fpgaView, &FPGAViewWidget::newContext);
connect(this, &BaseMainWindow::contextChanged, designview, &DesignWidget::newContext);
2018-08-03 01:21:25 +08:00
// Catch close tab events
2018-08-03 16:53:38 +08:00
connect(centralTabWidget, &QTabWidget::tabCloseRequested, this, &BaseMainWindow::closeTab);
2018-08-03 01:21:25 +08:00
2018-08-03 01:25:20 +08:00
// Propagate events from design view to device view
2018-08-03 16:53:38 +08:00
connect(designview, &DesignWidget::selected, fpgaView, &FPGAViewWidget::onSelectedArchItem);
connect(designview, &DesignWidget::zoomSelected, fpgaView, &FPGAViewWidget::zoomSelected);
connect(designview, &DesignWidget::highlight, fpgaView, &FPGAViewWidget::onHighlightGroupChanged);
2018-07-15 23:50:58 +08:00
2018-08-03 01:21:25 +08:00
// Click event on device view
2018-08-03 16:53:38 +08:00
connect(fpgaView, &FPGAViewWidget::clickedBel, designview, &DesignWidget::onClickedBel);
connect(fpgaView, &FPGAViewWidget::clickedWire, designview, &DesignWidget::onClickedWire);
connect(fpgaView, &FPGAViewWidget::clickedPip, designview, &DesignWidget::onClickedPip);
2018-07-16 02:31:42 +08:00
2018-08-03 01:21:25 +08:00
// Update tree event
2018-08-03 16:53:38 +08:00
connect(this, &BaseMainWindow::updateTreeView, designview, &DesignWidget::updateTree);
2018-08-03 00:50:08 +08:00
createMenusAndBars();
2018-06-15 00:37:57 +08:00
}
2018-08-03 00:50:08 +08:00
BaseMainWindow::~BaseMainWindow() { delete task; }
2018-06-15 17:10:11 +08:00
void BaseMainWindow::closeTab(int index) { delete centralTabWidget->widget(index); }
2018-07-14 20:06:05 +08:00
void BaseMainWindow::writeInfo(std::string text) { console->info(text); }
2018-06-21 19:41:16 +08:00
void BaseMainWindow::createMenusAndBars()
{
2018-08-03 01:21:25 +08:00
// File menu / project toolbar actions
actionNew = new QAction("New", this);
2018-07-14 23:58:58 +08:00
actionNew->setIcon(QIcon(":/icons/resources/new.png"));
2018-06-26 20:17:17 +08:00
actionNew->setShortcuts(QKeySequence::New);
actionNew->setStatusTip("New project file");
2018-08-03 16:53:38 +08:00
connect(actionNew, &QAction::triggered, this, &BaseMainWindow::new_proj);
2018-06-26 20:17:17 +08:00
actionOpen = new QAction("Open", this);
2018-07-14 23:58:58 +08:00
actionOpen->setIcon(QIcon(":/icons/resources/open.png"));
actionOpen->setShortcuts(QKeySequence::Open);
2018-06-26 20:17:17 +08:00
actionOpen->setStatusTip("Open an existing project file");
2018-08-03 16:53:38 +08:00
connect(actionOpen, &QAction::triggered, this, &BaseMainWindow::open_proj);
2018-07-21 18:15:50 +08:00
actionSave = new QAction("Save", this);
2018-07-14 23:58:58 +08:00
actionSave->setIcon(QIcon(":/icons/resources/save.png"));
actionSave->setShortcuts(QKeySequence::Save);
2018-06-26 20:17:17 +08:00
actionSave->setStatusTip("Save existing project to disk");
actionSave->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionSave, &QAction::triggered, this, &BaseMainWindow::save_proj);
QAction *actionExit = new QAction("Exit", this);
2018-07-14 23:58:58 +08:00
actionExit->setIcon(QIcon(":/icons/resources/exit.png"));
actionExit->setShortcuts(QKeySequence::Quit);
actionExit->setStatusTip("Exit the application");
2018-08-03 16:53:38 +08:00
connect(actionExit, &QAction::triggered, this, &BaseMainWindow::close);
2018-08-03 01:21:25 +08:00
// Help menu actions
QAction *actionAbout = new QAction("About", this);
2018-08-03 01:21:25 +08:00
// Design menu options
2018-08-03 00:50:08 +08:00
actionLoadJSON = new QAction("Open JSON", this);
actionLoadJSON->setIcon(QIcon(":/icons/resources/open_json.png"));
actionLoadJSON->setStatusTip("Open an existing JSON file");
actionLoadJSON->setEnabled(true);
2018-08-03 16:53:38 +08:00
connect(actionLoadJSON, &QAction::triggered, this, &BaseMainWindow::open_json);
2018-08-03 00:50:08 +08:00
actionPack = new QAction("Pack", this);
actionPack->setIcon(QIcon(":/icons/resources/pack.png"));
actionPack->setStatusTip("Pack current design");
actionPack->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionPack, &QAction::triggered, task, &TaskManager::pack);
2018-08-03 00:50:08 +08:00
actionAssignBudget = new QAction("Assign Budget", this);
actionAssignBudget->setIcon(QIcon(":/icons/resources/time_add.png"));
actionAssignBudget->setStatusTip("Assign time budget for current design");
actionAssignBudget->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionAssignBudget, &QAction::triggered, this, &BaseMainWindow::budget);
2018-08-03 00:50:08 +08:00
actionPlace = new QAction("Place", this);
actionPlace->setIcon(QIcon(":/icons/resources/place.png"));
actionPlace->setStatusTip("Place current design");
actionPlace->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionPlace, &QAction::triggered, this, &BaseMainWindow::place);
2018-08-03 00:50:08 +08:00
actionRoute = new QAction("Route", this);
actionRoute->setIcon(QIcon(":/icons/resources/route.png"));
actionRoute->setStatusTip("Route current design");
actionRoute->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionRoute, &QAction::triggered, task, &TaskManager::route);
2018-08-03 00:50:08 +08:00
2018-08-03 01:21:25 +08:00
// Worker control toolbar actions
2018-08-03 00:50:08 +08:00
actionPlay = new QAction("Play", this);
actionPlay->setIcon(QIcon(":/icons/resources/control_play.png"));
actionPlay->setStatusTip("Continue running task");
actionPlay->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionPlay, &QAction::triggered, task, &TaskManager::continue_thread);
2018-08-03 00:50:08 +08:00
actionPause = new QAction("Pause", this);
actionPause->setIcon(QIcon(":/icons/resources/control_pause.png"));
actionPause->setStatusTip("Pause running task");
actionPause->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionPause, &QAction::triggered, task, &TaskManager::pause_thread);
2018-08-03 00:50:08 +08:00
actionStop = new QAction("Stop", this);
actionStop->setIcon(QIcon(":/icons/resources/control_stop.png"));
actionStop->setStatusTip("Stop running task");
actionStop->setEnabled(false);
2018-08-03 16:53:38 +08:00
connect(actionStop, &QAction::triggered, task, &TaskManager::terminate_thread);
2018-08-03 00:50:08 +08:00
2018-08-03 01:21:25 +08:00
// Device view control toolbar actions
2018-07-26 19:21:46 +08:00
QAction *actionZoomIn = new QAction("Zoom In", this);
actionZoomIn->setIcon(QIcon(":/icons/resources/zoom_in.png"));
2018-08-03 16:53:38 +08:00
connect(actionZoomIn, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomIn);
2018-07-26 19:21:46 +08:00
QAction *actionZoomOut = new QAction("Zoom Out", this);
actionZoomOut->setIcon(QIcon(":/icons/resources/zoom_out.png"));
2018-08-03 16:53:38 +08:00
connect(actionZoomOut, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomOut);
2018-07-26 19:21:46 +08:00
QAction *actionZoomSelected = new QAction("Zoom Selected", this);
actionZoomSelected->setIcon(QIcon(":/icons/resources/shape_handles.png"));
2018-08-03 16:53:38 +08:00
connect(actionZoomSelected, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomSelected);
2018-07-26 19:21:46 +08:00
QAction *actionZoomOutbound = new QAction("Zoom Outbound", this);
actionZoomOutbound->setIcon(QIcon(":/icons/resources/shape_square.png"));
2018-08-03 16:53:38 +08:00
connect(actionZoomOutbound, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomOutbound);
2018-07-26 19:21:46 +08:00
2018-08-03 01:21:25 +08:00
// Add main menu
menuBar = new QMenuBar();
menuBar->setGeometry(QRect(0, 0, 1024, 27));
setMenuBar(menuBar);
QMenu *menuFile = new QMenu("&File", menuBar);
QMenu *menuHelp = new QMenu("&Help", menuBar);
menuDesign = new QMenu("&Design", menuBar);
menuBar->addAction(menuFile->menuAction());
menuBar->addAction(menuDesign->menuAction());
menuBar->addAction(menuHelp->menuAction());
// Add File menu actions
menuFile->addAction(actionNew);
menuFile->addAction(actionOpen);
menuFile->addAction(actionSave);
menuFile->addSeparator();
menuFile->addAction(actionExit);
// Add Design menu actions
menuDesign->addAction(actionLoadJSON);
menuDesign->addAction(actionPack);
menuDesign->addAction(actionAssignBudget);
menuDesign->addAction(actionPlace);
menuDesign->addAction(actionRoute);
// Add Help menu actions
menuHelp->addAction(actionAbout);
// Project toolbar
2018-08-03 01:24:05 +08:00
QToolBar *projectToolBar = new QToolBar("Project");
2018-08-03 01:21:25 +08:00
addToolBar(Qt::TopToolBarArea, projectToolBar);
projectToolBar->addAction(actionNew);
projectToolBar->addAction(actionOpen);
projectToolBar->addAction(actionSave);
// Main action bar
2018-08-03 01:24:05 +08:00
mainActionBar = new QToolBar("Main");
2018-08-03 01:21:25 +08:00
addToolBar(Qt::TopToolBarArea, mainActionBar);
mainActionBar->addAction(actionLoadJSON);
mainActionBar->addAction(actionPack);
mainActionBar->addAction(actionAssignBudget);
mainActionBar->addAction(actionPlace);
mainActionBar->addAction(actionRoute);
// Add worker control toolbar
2018-08-03 01:24:05 +08:00
QToolBar *workerControlToolBar = new QToolBar("Worker");
2018-08-03 01:21:25 +08:00
addToolBar(Qt::TopToolBarArea, workerControlToolBar);
workerControlToolBar->addAction(actionPlay);
workerControlToolBar->addAction(actionPause);
workerControlToolBar->addAction(actionStop);
// Add device view control toolbar
2018-08-03 01:24:05 +08:00
QToolBar *deviceViewToolBar = new QToolBar("Device");
2018-08-03 01:21:25 +08:00
addToolBar(Qt::TopToolBarArea, deviceViewToolBar);
deviceViewToolBar->addAction(actionZoomIn);
deviceViewToolBar->addAction(actionZoomOut);
deviceViewToolBar->addAction(actionZoomSelected);
deviceViewToolBar->addAction(actionZoomOutbound);
// Add status bar with progress bar
statusBar = new QStatusBar();
progressBar = new QProgressBar(statusBar);
progressBar->setAlignment(Qt::AlignRight);
progressBar->setMaximumSize(180, 19);
statusBar->addPermanentWidget(progressBar);
progressBar->setValue(0);
progressBar->setEnabled(false);
setStatusBar(statusBar);
2018-07-26 19:21:46 +08:00
}
2018-08-03 00:50:08 +08:00
void BaseMainWindow::load_json(std::string filename)
{
disableActions();
currentJson = filename;
std::ifstream f(filename);
if (parse_json_file(f, filename, ctx.get())) {
log("Loading design successful.\n");
Q_EMIT updateTreeView();
2018-08-06 00:02:33 +08:00
updateJsonLoaded();
2018-08-03 00:50:08 +08:00
} else {
actionLoadJSON->setEnabled(true);
log("Loading design failed.\n");
}
}
void BaseMainWindow::open_json()
{
QString fileName = QFileDialog::getOpenFileName(this, QString("Open JSON"), QString(), QString("*.json"));
if (!fileName.isEmpty()) {
load_json(fileName.toStdString());
}
}
void BaseMainWindow::pack_finished(bool status)
{
disableActions();
if (status) {
log("Packing design successful.\n");
Q_EMIT updateTreeView();
actionPlace->setEnabled(true);
actionAssignBudget->setEnabled(true);
onPackFinished();
} else {
log("Packing design failed.\n");
}
}
void BaseMainWindow::budget_finish(bool status)
{
disableActions();
if (status) {
log("Assigning timing budget successful.\n");
actionPlace->setEnabled(true);
onBudgetFinished();
} else {
log("Assigning timing budget failed.\n");
}
}
void BaseMainWindow::place_finished(bool status)
{
disableActions();
if (status) {
log("Placing design successful.\n");
Q_EMIT updateTreeView();
actionRoute->setEnabled(true);
onPlaceFinished();
} else {
log("Placing design failed.\n");
}
}
void BaseMainWindow::route_finished(bool status)
{
disableActions();
if (status) {
log("Routing design successful.\n");
Q_EMIT updateTreeView();
onRouteFinished();
} else
log("Routing design failed.\n");
}
void BaseMainWindow::taskCanceled()
{
log("CANCELED\n");
disableActions();
}
void BaseMainWindow::taskStarted()
{
disableActions();
actionPause->setEnabled(true);
actionStop->setEnabled(true);
actionNew->setEnabled(false);
actionOpen->setEnabled(false);
}
void BaseMainWindow::taskPaused()
{
disableActions();
actionPlay->setEnabled(true);
actionStop->setEnabled(true);
actionNew->setEnabled(false);
actionOpen->setEnabled(false);
}
void BaseMainWindow::budget()
{
bool ok;
double freq = QInputDialog::getDouble(this, "Assign timing budget", "Frequency [MHz]:", 50, 0, 250, 2, &ok);
if (ok) {
freq *= 1e6;
timing_driven = true;
Q_EMIT task->budget(freq);
}
}
void BaseMainWindow::place() { Q_EMIT task->place(timing_driven); }
void BaseMainWindow::disableActions()
{
actionLoadJSON->setEnabled(false);
actionPack->setEnabled(false);
actionAssignBudget->setEnabled(false);
actionPlace->setEnabled(false);
actionRoute->setEnabled(false);
actionPlay->setEnabled(false);
actionPause->setEnabled(false);
actionStop->setEnabled(false);
actionNew->setEnabled(true);
actionOpen->setEnabled(true);
actionSave->setEnabled(!currentJson.empty());
onDisableActions();
}
2018-08-06 00:02:33 +08:00
void BaseMainWindow::updateJsonLoaded()
{
disableActions();
actionPack->setEnabled(true);
onJsonLoaded();
}
2018-08-09 00:14:22 +08:00
void BaseMainWindow::open_proj()
{
QString fileName = QFileDialog::getOpenFileName(this, QString("Open Project"), QString(), QString("*.proj"));
if (!fileName.isEmpty()) {
try {
ProjectHandler proj;
disableActions();
ctx = proj.load(fileName.toStdString());
Q_EMIT contextChanged(ctx.get());
log_info("Loaded project %s...\n", fileName.toStdString().c_str());
updateJsonLoaded();
onProjectLoaded();
} catch (log_execution_error_exception) {
}
}
}
void BaseMainWindow::save_proj()
{
if (currentProj.empty()) {
QString fileName = QFileDialog::getSaveFileName(this, QString("Save Project"), QString(), QString("*.proj"));
if (fileName.isEmpty())
return;
currentProj = fileName.toStdString();
}
if (!currentProj.empty()) {
ProjectHandler proj;
proj.save(ctx.get(), currentProj);
}
}
2018-06-22 19:10:27 +08:00
NEXTPNR_NAMESPACE_END