diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index fde268670..2d648a6cf 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -41,22 +41,22 @@ MainWindow::MainWindow(QWidget *parent, const QString &filename) : QMainWindow(parent) - , ui(new Ui::MainWindow) - , editor(new Editor(this)) - , undoView(nullptr) - , firstResult(nullptr) - , loadedAutosave(false) - , autosaveFilename("") - , dolphinFilename("none") - , bd(nullptr) - , translator(nullptr) + , m_ui(new Ui::MainWindow) + , m_editor(new Editor(this)) + , m_undoView(nullptr) + , m_firstResult(nullptr) + , m_loadedAutosave(false) + , m_autosaveFilename("") + , m_dolphinFilename("none") + , m_bd(nullptr) + , m_translator(nullptr) , m_settings(new WPandaSettings) { COMMENT("WIRED PANDA Version = " << APP_VERSION << " OR " << GlobalProperties::version, 0); - ui->setupUi(this); + m_ui->setupUi(this); ThemeManager::globalMngr = new ThemeManager(this); buildFullScreenDialog(); - ui->graphicsView->setScene(editor->getScene()); + m_ui->graphicsView->setScene(m_editor->getScene()); m_settings->load(); @@ -65,51 +65,52 @@ MainWindow::MainWindow(QWidget *parent, const QString &filename) loadTranslation(m_settings->language()); - if (autosaveFilename != "") { + if (m_autosaveFilename != "") { // If autosaveFile was found within the config. file, then WiredPanda probably didn't save its last project correctly, perhaps due to a crash. - if ((QFile(autosaveFilename).exists()) && (autosaveFilename != filename)) { - int ret = recoverAutoSaveFile(autosaveFilename); + if ((QFile(m_autosaveFilename).exists()) && (m_autosaveFilename != filename)) { + int ret = recoverAutoSaveFile(m_autosaveFilename); if (ret == QMessageBox::Yes) { auto *wPanda = new QProcess(nullptr); QStringList args; - args << autosaveFilename; + args << m_autosaveFilename; wPanda->start(QCoreApplication::applicationFilePath(), args); COMMENT("Reloaded autosave: " << args[0].toStdString(), 0); } - } else if ((QFile(autosaveFilename).exists()) && (autosaveFilename == filename)) { + } else if ((QFile(m_autosaveFilename).exists()) && (m_autosaveFilename == filename)) { COMMENT("Loading autosave!", 0); - loadedAutosave = true; + m_loadedAutosave = true; } } + // TODO: remove geometry from settings save restoreGeometry(GlobalProperties::settingToByteArray(m_settings->windowGeometry())); restoreState(GlobalProperties::settingToByteArray(m_settings->windowState())); - ui->splitter->restoreGeometry(GlobalProperties::settingToByteArray(m_settings->splitterGeometry())); - ui->splitter->restoreState(GlobalProperties::settingToByteArray(m_settings->splitterState())); + m_ui->splitter->restoreGeometry(GlobalProperties::settingToByteArray(m_settings->splitterGeometry())); + m_ui->splitter->restoreState(GlobalProperties::settingToByteArray(m_settings->splitterState())); - ui->actionExport_to_Arduino->setEnabled(false); + m_ui->actionExport_to_Arduino->setEnabled(false); QList zoom_in_shortcuts; zoom_in_shortcuts << QKeySequence("Ctrl++") << QKeySequence("Ctrl+="); - ui->actionZoom_in->setShortcuts(zoom_in_shortcuts); + m_ui->actionZoom_in->setShortcuts(zoom_in_shortcuts); /* THEME */ auto *themeGroup = new QActionGroup(this); - auto const actions = ui->menuTheme->actions(); + auto const actions = m_ui->menuTheme->actions(); for (QAction *action : actions) { themeGroup->addAction(action); } themeGroup->setExclusive(true); connect(ThemeManager::globalMngr, &ThemeManager::themeChanged, this, &MainWindow::updateTheme); - connect(ThemeManager::globalMngr, &ThemeManager::themeChanged, editor, &Editor::updateTheme); + connect(ThemeManager::globalMngr, &ThemeManager::themeChanged, m_editor, &Editor::updateTheme); ThemeManager::globalMngr->initialize(); - /* ui->graphicsView->setBackgroundBrush(QBrush(QColor(Qt::gray))); */ + /* m_ui->graphicsView->setBackgroundBrush(QBrush(QColor(Qt::gray))); */ setFastMode(m_settings->fastMode()); - editor->setElementEditor(ui->widgetElementEditor); - ui->searchScrollArea->hide(); + m_editor->setElementEditor(m_ui->widgetElementEditor); + m_ui->searchScrollArea->hide(); setCurrentFile(QFileInfo()); @@ -119,78 +120,77 @@ MainWindow::MainWindow(QWidget *parent, const QString &filename) * #endif */ - connect(ui->actionCopy, &QAction::triggered, editor, &Editor::copyAction); - connect(ui->actionCut, &QAction::triggered, editor, &Editor::cutAction); - connect(ui->actionPaste, &QAction::triggered, editor, &Editor::pasteAction); - connect(ui->actionDelete, &QAction::triggered, editor, &Editor::deleteAction); + connect(m_ui->actionCopy, &QAction::triggered, m_editor, &Editor::copyAction); + connect(m_ui->actionCut, &QAction::triggered, m_editor, &Editor::cutAction); + connect(m_ui->actionPaste, &QAction::triggered, m_editor, &Editor::pasteAction); + connect(m_ui->actionDelete, &QAction::triggered, m_editor, &Editor::deleteAction); - undoAction = editor->getUndoStack()->createUndoAction(this, tr("&Undo")); - undoAction->setIcon(QIcon(QPixmap(":/toolbar/undo.png"))); - undoAction->setShortcuts(QKeySequence::Undo); + m_undoAction = m_editor->getUndoStack()->createUndoAction(this, tr("&Undo")); + m_undoAction->setIcon(QIcon(QPixmap(":/toolbar/undo.png"))); + m_undoAction->setShortcuts(QKeySequence::Undo); - redoAction = editor->getUndoStack()->createRedoAction(this, tr("&Redo")); - redoAction->setIcon(QIcon(QPixmap(":/toolbar/redo.png"))); - redoAction->setShortcuts(QKeySequence::Redo); + m_redoAction = m_editor->getUndoStack()->createRedoAction(this, tr("&Redo")); + m_redoAction->setIcon(QIcon(QPixmap(":/toolbar/redo.png"))); + m_redoAction->setShortcuts(QKeySequence::Redo); - ui->menuEdit->insertAction(ui->menuEdit->actions().at(0), undoAction); - ui->menuEdit->insertAction(undoAction, redoAction); + m_ui->menuEdit->insertAction(m_ui->menuEdit->actions().at(0), m_undoAction); + m_ui->menuEdit->insertAction(m_undoAction, m_redoAction); - connect(ui->graphicsView->gvzoom(), &GraphicsViewZoom::zoomed, this, &MainWindow::zoomChanged); - connect(editor, &Editor::scroll, this, &MainWindow::scrollView); - connect(editor, &Editor::circuitHasChanged, this, &MainWindow::autoSave); + connect(m_ui->graphicsView->gvzoom(), &GraphicsViewZoom::zoomed, this, &MainWindow::zoomChanged); + connect(m_editor, &Editor::scroll, this, &MainWindow::scrollView); + connect(m_editor, &Editor::circuitHasChanged, this, &MainWindow::autoSave); - rfController = new RecentFilesController(this, true); - ricController = new RecentFilesController(this, false); - connect(this, &MainWindow::addRecentFile, rfController, &RecentFilesController::addRecentFile); - connect(this, &MainWindow::addRecentIcFile, ricController, &RecentFilesController::addRecentFile); + m_rfController = new RecentFilesController(this, true); + m_ricController = new RecentFilesController(this, false); + connect(this, &MainWindow::addRecentFile, m_rfController, &RecentFilesController::addRecentFile); + connect(this, &MainWindow::addRecentIcFile, m_ricController, &RecentFilesController::addRecentFile); auto *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F), this); - connect(shortcut, &QShortcut::activated, ui->lineEdit, QOverload<>::of(&QWidget::setFocus)); - ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground); + connect(shortcut, &QShortcut::activated, m_ui->lineEdit, QOverload<>::of(&QWidget::setFocus)); + m_ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground); updateRecentICs(); createRecentFileActions(); updateRecentFileActions(); - connect(rfController, &RecentFilesController::recentFilesUpdated, this, &MainWindow::updateRecentFileActions); - connect(ricController, &RecentFilesController::recentFilesUpdated, this, &MainWindow::updateRecentICs); + connect(m_rfController, &RecentFilesController::recentFilesUpdated, this, &MainWindow::updateRecentFileActions); + connect(m_ricController, &RecentFilesController::recentFilesUpdated, this, &MainWindow::updateRecentICs); /* QApplication::setStyle( QStyleFactory::create( "Fusion" ) ); */ - ui->actionPlay->setChecked(true); + m_ui->actionPlay->setChecked(true); populateLeftMenu(); } -WPandaSettings *MainWindow::settings() { - return m_settings; -} - void MainWindow::setFastMode(bool fastModeEnabled) { - ui->graphicsView->setRenderHint(QPainter::Antialiasing, !fastModeEnabled); - ui->graphicsView->setRenderHint(QPainter::SmoothPixmapTransform, !fastModeEnabled); + m_ui->graphicsView->setRenderHint(QPainter::Antialiasing, !fastModeEnabled); + m_ui->graphicsView->setRenderHint(QPainter::SmoothPixmapTransform, !fastModeEnabled); - fullscreenView->setRenderHint(QPainter::Antialiasing, !fastModeEnabled); - fullscreenView->setRenderHint(QPainter::SmoothPixmapTransform, !fastModeEnabled); + m_fullscreenView->setRenderHint(QPainter::Antialiasing, !fastModeEnabled); + m_fullscreenView->setRenderHint(QPainter::SmoothPixmapTransform, !fastModeEnabled); - ui->actionFast_Mode->setChecked(fastModeEnabled); + m_ui->actionFast_Mode->setChecked(fastModeEnabled); } void MainWindow::createUndoView() { - undoView = new QUndoView(editor->getUndoStack()); - undoView->setWindowTitle(tr("Command List")); - undoView->show(); - undoView->setAttribute(Qt::WA_QuitOnClose, false); + m_undoView = new QUndoView(m_editor->getUndoStack()); + m_undoView->setWindowTitle(tr("Command List")); + m_undoView->show(); + m_undoView->setAttribute(Qt::WA_QuitOnClose, false); } MainWindow::~MainWindow() { + if (m_bd) { + delete m_bd; + } + if (m_undoView) { + delete m_undoView; + } + delete m_editor; m_settings->save(); - m_settings->deleteLater(); - if (undoView) { - delete undoView; + delete m_ui; } - delete ui; -} void MainWindow::on_actionExit_triggered() { @@ -199,11 +199,11 @@ void MainWindow::on_actionExit_triggered() bool MainWindow::save(QString fname) { - COMMENT("fname: " << fname.toStdString() << ", autosave: " << autosaveFilename.toStdString(), 0); - if ((fname.isEmpty()) || (loadedAutosave)) { - fname = currentFile.absoluteFilePath(); - if ((currentFile.fileName().isEmpty()) || (loadedAutosave)) { - fname = QFileDialog::getSaveFileName(this, tr("Save File"), defaultDirectory.absolutePath(), tr("Panda files (*.panda)")); + COMMENT("fname: " << fname.toStdString() << ", autosave: " << m_autosaveFilename.toStdString(), 0); + if ((fname.isEmpty()) || (m_loadedAutosave)) { + fname = m_currentFile.absoluteFilePath(); + if ((m_currentFile.fileName().isEmpty()) || (m_loadedAutosave)) { + fname = QFileDialog::getSaveFileName(this, tr("Save File"), m_defaultDirectory.absolutePath(), tr("Panda files (*.panda)")); } } if (fname.isEmpty()) { @@ -216,19 +216,19 @@ bool MainWindow::save(QString fname) if (fl.open(QFile::WriteOnly)) { QDataStream ds(&fl); try { - editor->save(ds, dolphinFilename); + m_editor->save(ds, m_dolphinFilename); } catch (std::runtime_error &e) { std::cerr << tr("Error saving project: ").toStdString() << e.what() << std::endl; return false; } } if (fl.commit()) { - loadedAutosave = false; + m_loadedAutosave = false; setCurrentFile(QFileInfo(fname)); - ui->statusBar->showMessage(tr("Saved file successfully."), 2000); - editor->getUndoStack()->setClean(); - if (autosaveFile.isOpen()) { - autosaveFile.remove(); + m_ui->statusBar->showMessage(tr("Saved file successfully."), 2000); + m_editor->getUndoStack()->setClean(); + if (m_autosaveFile.isOpen()) { + m_autosaveFile.remove(); m_settings->setAutoSaveFile(QStringLiteral("")); } return true; @@ -240,13 +240,13 @@ bool MainWindow::save(QString fname) void MainWindow::show() { QMainWindow::show(); - editor->clear(); + m_editor->clear(); } void MainWindow::clear() { - editor->clear(); - dolphinFilename = "none"; + m_editor->clear(); + m_dolphinFilename = "none"; setCurrentFile(QFileInfo()); } @@ -283,17 +283,17 @@ void MainWindow::on_actionNew_triggered() void MainWindow::on_actionWires_triggered(bool checked) { - editor->showWires(checked); + m_editor->showWires(checked); } void MainWindow::on_actionRotate_right_triggered() { - editor->rotate(true); + m_editor->rotate(true); } void MainWindow::on_actionRotate_left_triggered() { - editor->rotate(false); + m_editor->rotate(false); } bool MainWindow::loadPandaFile(const QString &fname) @@ -312,9 +312,9 @@ bool MainWindow::loadPandaFile(const QString &fname) COMMENT("Current file set.", 0); try { COMMENT("Loading in editor.", 0); - editor->load(ds); + m_editor->load(ds); COMMENT("Loaded. Emitting changed signal.", 0); - emit editor->circuitHasChanged(); + emit m_editor->circuitHasChanged(); COMMENT("Finished updating changed by signal.", 0); } catch (std::runtime_error &e) { std::cerr << tr("Error loading project: ").toStdString() << e.what() << std::endl; @@ -330,19 +330,19 @@ bool MainWindow::loadPandaFile(const QString &fname) fl.close(); // COMMENT( "Adding to controller.", 0 ); // emit addRecentFile( fname ); - ui->statusBar->showMessage(tr("File loaded successfully."), 2000); + m_ui->statusBar->showMessage(tr("File loaded successfully."), 2000); /* on_actionWaveform_triggered( ); */ return true; } void MainWindow::scrollView(int dx, int dy) { - ui->graphicsView->scroll(dx, dy); + m_ui->graphicsView->scroll(dx, dy); } void MainWindow::on_actionOpen_triggered() { - QString fname = QFileDialog::getOpenFileName(this, tr("Open File"), defaultDirectory.absolutePath(), tr("Panda files (*.panda)")); + QString fname = QFileDialog::getOpenFileName(this, tr("Open File"), m_defaultDirectory.absolutePath(), tr("Panda files (*.panda)")); if (fname.isEmpty()) { return; } @@ -382,15 +382,15 @@ void MainWindow::on_actionAbout_Qt_triggered() bool MainWindow::closeFile() { bool ok = true; - if (!editor->getUndoStack()->isClean()) { + if (!m_editor->getUndoStack()->isClean()) { int ret = confirmSave(); if (ret == QMessageBox::Save) { ok = save(); } else if (ret == QMessageBox::Cancel) { ok = false; } else { // Close without saving. Deleting autosave if it was opened. - if (loadedAutosave) { - autosaveFile.remove(); + if (m_loadedAutosave) { + m_autosaveFile.remove(); m_settings->setAutoSaveFile(QStringLiteral("")); } } @@ -402,10 +402,11 @@ void MainWindow::closeEvent(QCloseEvent *e) { Q_UNUSED(e); + // TODO: remove geometry from settings save m_settings->setWindowGeometry(GlobalProperties::settingToIntList(saveGeometry())); m_settings->setWindowState(GlobalProperties::settingToIntList(saveState())); - m_settings->setSplitterGeometry(GlobalProperties::settingToIntList(ui->splitter->saveGeometry())); - m_settings->setSplitterState(GlobalProperties::settingToIntList(ui->splitter->saveState())); + m_settings->setSplitterGeometry(GlobalProperties::settingToIntList(m_ui->splitter->saveGeometry())); + m_settings->setSplitterState(GlobalProperties::settingToIntList(m_ui->splitter->saveState())); m_settings->save(); if (closeFile()) { @@ -417,10 +418,10 @@ void MainWindow::closeEvent(QCloseEvent *e) void MainWindow::on_actionSave_As_triggered() { - QString fname = currentFile.absoluteFilePath(); - QString path = defaultDirectory.absolutePath(); - if (!currentFile.fileName().isEmpty()) { - path = currentFile.absoluteFilePath(); + QString fname = m_currentFile.absoluteFilePath(); + QString path = m_defaultDirectory.absolutePath(); + if (!m_currentFile.fileName().isEmpty()) { + path = m_currentFile.absoluteFilePath(); } fname = QFileDialog::getSaveFileName(this, tr("Save File as ..."), path, tr("Panda files (*.panda)")); if (fname.isEmpty()) { @@ -435,7 +436,7 @@ void MainWindow::on_actionSave_As_triggered() QFileInfo MainWindow::getCurrentFile() const { - return currentFile; + return m_currentFile; } void MainWindow::setCurrentFile(const QFileInfo &file) @@ -446,62 +447,62 @@ void MainWindow::setCurrentFile(const QFileInfo &file) COMMENT("Autosave path set to the current file's directory, if there is one.", 0); autosavePath = file.dir(); COMMENT("Autosavepath: " << autosavePath.absolutePath().toStdString(), 0); - autosaveFile.setFileTemplate(autosavePath.absoluteFilePath(file.baseName() + "XXXXXX.panda")); + m_autosaveFile.setFileTemplate(autosavePath.absoluteFilePath(file.baseName() + "XXXXXX.panda")); COMMENT("Setting current file to: " << file.absoluteFilePath().toStdString(), 0); } else { COMMENT("Default file does not exist: " << file.absoluteFilePath().toStdString(), 0); QDir autosavePath(QDir::temp()); COMMENT("Autosavepath: " << autosavePath.absolutePath().toStdString(), 0); - autosaveFile.setFileTemplate(autosavePath.absoluteFilePath("XXXXXX.panda")); + m_autosaveFile.setFileTemplate(autosavePath.absoluteFilePath("XXXXXX.panda")); COMMENT("Setting current file to random file in tmp.", 0); } - currentFile = file; + m_currentFile = file; if (file.fileName().isEmpty()) { setWindowTitle("wiRED PANDA v" + QString(APP_VERSION)); } else { setWindowTitle(QString("wiRED PANDA v%1 [%2]").arg(APP_VERSION, file.fileName())); } - if (!loadedAutosave) { + if (!m_loadedAutosave) { COMMENT("Adding file to controller.", 0); emit addRecentFile(file.absoluteFilePath()); } - GlobalProperties::currentFile = currentFile.absoluteFilePath(); + GlobalProperties::currentFile = m_currentFile.absoluteFilePath(); COMMENT("Setting global current file.", 0); - if (currentFile.exists()) { - defaultDirectory = currentFile.dir(); + if (m_currentFile.exists()) { + m_defaultDirectory = m_currentFile.dir(); } else { - defaultDirectory = QDir::home(); + m_defaultDirectory = QDir::home(); } } void MainWindow::on_actionSelect_all_triggered() { - editor->selectAll(); + m_editor->selectAll(); } void MainWindow::updateRecentICs() { - ui->scrollAreaWidgetContents_Box->layout()->removeItem(ui->verticalSpacer_BOX); - for (ListItemWidget *item : qAsConst(boxItemWidgets)) { + m_ui->scrollAreaWidgetContents_Box->layout()->removeItem(m_ui->verticalSpacer_BOX); + for (ListItemWidget *item : qAsConst(m_boxItemWidgets)) { item->deleteLater(); } /* qDeleteAll( boxItemWidgets ); */ - boxItemWidgets.clear(); + m_boxItemWidgets.clear(); //! Show recent files - const QStringList files = ricController->getRecentFiles(); + const QStringList files = m_ricController->getRecentFiles(); for (const QString &file : files) { QPixmap pixmap(QString::fromUtf8(":/basic/box.png")); auto *item = new ListItemWidget(pixmap, ElementType::IC, file, this); - boxItemWidgets.append(item); - ui->scrollAreaWidgetContents_Box->layout()->addWidget(item); + m_boxItemWidgets.append(item); + m_ui->scrollAreaWidgetContents_Box->layout()->addWidget(item); } - ui->scrollAreaWidgetContents_Box->layout()->addItem(ui->verticalSpacer_BOX); + m_ui->scrollAreaWidgetContents_Box->layout()->addItem(m_ui->verticalSpacer_BOX); } QString MainWindow::getOpenICFile() { - return QFileDialog::getOpenFileName(this, tr("Open File as IC"), defaultDirectory.absolutePath(), tr("Panda files (*.panda)")); + return QFileDialog::getOpenFileName(this, tr("Open File as IC"), m_defaultDirectory.absolutePath(), tr("Panda files (*.panda)")); } void MainWindow::on_actionOpen_IC_triggered() @@ -524,29 +525,29 @@ void MainWindow::on_actionOpen_IC_triggered() } fl.close(); - ui->statusBar->showMessage(tr("Loaded ic successfully."), 2000); + m_ui->statusBar->showMessage(tr("Loaded ic successfully."), 2000); } void MainWindow::on_lineEdit_textChanged(const QString &text) { - ui->searchLayout->removeItem(ui->VSpacer); - for (ListItemWidget *item : qAsConst(searchItemWidgets)) { + m_ui->searchLayout->removeItem(m_ui->VSpacer); + for (ListItemWidget *item : qAsConst(m_searchItemWidgets)) { item->deleteLater(); } - searchItemWidgets.clear(); - firstResult = nullptr; + m_searchItemWidgets.clear(); + m_firstResult = nullptr; if (text.isEmpty()) { - ui->searchScrollArea->hide(); - ui->tabWidget->show(); + m_ui->searchScrollArea->hide(); + m_ui->tabWidget->show(); } else { - ui->searchScrollArea->show(); - ui->tabWidget->hide(); - QList