Skip to content

Commit

Permalink
feat: add recent imports to File menu for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
hello-adam committed Mar 24, 2020
1 parent b9e0ceb commit fc2fba7
Show file tree
Hide file tree
Showing 19 changed files with 391 additions and 89 deletions.
2 changes: 2 additions & 0 deletions src/hobbits-core/hobbits-core.pro
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ SOURCES += \
displayhelper.cpp \
frame.cpp \
hobbitscoreinfo.cpp \
importexportresult.cpp \
mathparser.cpp \
operatoractor.cpp \
operatorresult.cpp \
Expand Down Expand Up @@ -86,6 +87,7 @@ HEADERS += \
hobbits-core_global.h \
hobbitscoreinfo.h \
importexportinterface.h \
importexportresult.h \
operatoractor.h \
operatorinterface.h \
mathparser.h \
Expand Down
10 changes: 7 additions & 3 deletions src/hobbits-core/importexportinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define IMPORTEXPORTINTERFACE_H

#include "bitcontainer.h"
#include "importexportresult.h"
#include <QSharedPointer>

#include "hobbits-core_global.h"
Expand All @@ -20,10 +21,13 @@ class HOBBITSCORESHARED_EXPORT ImportExportInterface
virtual bool canImport() = 0;
virtual bool canExport() = 0;

virtual QSharedPointer<BitContainer> importBits(QMap<QString, QString> args, QWidget *parent) = 0;
virtual void exportBits(
virtual QString getImportLabelForState(QJsonObject pluginState) = 0;
virtual QString getExportLabelForState(QJsonObject pluginState) = 0;

virtual QSharedPointer<ImportExportResult> importBits(QJsonObject pluginState, QWidget *parent) = 0;
virtual QSharedPointer<ImportExportResult> exportBits(
QSharedPointer<const BitContainer> container,
QMap<QString, QString> args,
QJsonObject pluginState,
QWidget *parent) = 0;

};
Expand Down
51 changes: 51 additions & 0 deletions src/hobbits-core/importexportresult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "importexportresult.h"

ImportExportResult::ImportExportResult()
{

}

ImportExportResult* ImportExportResult::setContainer(QSharedPointer<BitContainer> container)
{
m_container = container;
return this;
}

QSharedPointer<BitContainer> ImportExportResult::getContainer() const
{
return m_container;
}

ImportExportResult* ImportExportResult::setPluginState(QJsonObject pluginState)
{
m_pluginState = pluginState;
return this;
}

const QJsonObject ImportExportResult::getPluginState() const
{
return m_pluginState;
}


QSharedPointer<ImportExportResult> ImportExportResult::nullResult()
{
return QSharedPointer<ImportExportResult>();
}

QSharedPointer<ImportExportResult> ImportExportResult::error(QString error)
{
QJsonObject pluginState;
pluginState.insert("error", error);
return QSharedPointer<ImportExportResult>((new ImportExportResult())->setPluginState(pluginState));
}

QSharedPointer<ImportExportResult> ImportExportResult::create(QSharedPointer<BitContainer> container, QJsonObject pluginState)
{
return QSharedPointer<ImportExportResult>((new ImportExportResult())->setContainer(container)->setPluginState(pluginState));
}

QSharedPointer<ImportExportResult> ImportExportResult::create(QJsonObject pluginState)
{
return QSharedPointer<ImportExportResult>((new ImportExportResult())->setPluginState(pluginState));
}
28 changes: 28 additions & 0 deletions src/hobbits-core/importexportresult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef IMPORTEXPORTRESULT_H
#define IMPORTEXPORTRESULT_H

#include "bitcontainer.h"
#include <QJsonObject>

class ImportExportResult
{
public:
ImportExportResult();

ImportExportResult* setContainer(QSharedPointer<BitContainer> container);
QSharedPointer<BitContainer> getContainer() const;

ImportExportResult* setPluginState(QJsonObject pluginState);
const QJsonObject getPluginState() const;

static QSharedPointer<ImportExportResult> nullResult();
static QSharedPointer<ImportExportResult> error(QString error);
static QSharedPointer<ImportExportResult> create(QSharedPointer<BitContainer> container, QJsonObject pluginState);
static QSharedPointer<ImportExportResult> create(QJsonObject pluginState);

private:
QSharedPointer<BitContainer> m_container;
QJsonObject m_pluginState;
};

#endif // IMPORTEXPORTRESULT_H
120 changes: 102 additions & 18 deletions src/hobbits-gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ MainWindow::MainWindow(QString extraPluginPath, QString configFilePath, QWidget
initializeImporterExporters();
initializeDisplays();

// Import menu initialization
populateRecentImportsMenu();

// create an initial state
checkOperatorInput();
activateBitContainer();
Expand Down Expand Up @@ -292,21 +295,15 @@ void MainWindow::initializeImporterExporters()
ui->menu_Import_Bits_From->addAction(
plugin->getName(),
[this, plugin]() {
QSharedPointer<BitContainer> container = plugin->importBits(QMap<QString, QString>(), this);
if (!container.isNull()) {
QModelIndex addedIndex = this->m_bitContainerManager->getTreeModel()->addContainer(container);
this->m_bitContainerManager->getCurrSelectionModel()->setCurrentIndex(
addedIndex,
QItemSelectionModel::ClearAndSelect);
}
this->requestImportRun(plugin->getName());
});
}
if (plugin->canExport()) {
ui->menu_Export_Bits_To->setEnabled(true);
ui->menu_Export_Bits_To->addAction(
plugin->getName(),
[this, plugin]() {
plugin->exportBits(this->currContainer(), QMap<QString, QString>(), this);
this->requestExportRun(plugin->getName());
});
}
}
Expand Down Expand Up @@ -355,21 +352,14 @@ QSharedPointer<BitContainer> MainWindow::currContainer()

void MainWindow::importBitfile(QString file)
{
QMap<QString, QString> args;
args.insert("filename", file);

QSharedPointer<ImportExportInterface> fileDataImporter = m_pluginManager->getImporterExporter("File Data");
if (fileDataImporter.isNull()) {
warningMessage("Could not import bit file without 'File Data' plugin");
return;
}
QSharedPointer<BitContainer> container = fileDataImporter->importBits(args, this);
if (!container.isNull()) {
QModelIndex addedIndex = this->m_bitContainerManager->getTreeModel()->addContainer(container);
this->m_bitContainerManager->getCurrSelectionModel()->setCurrentIndex(
addedIndex,
QItemSelectionModel::ClearAndSelect);
}
QJsonObject pluginState;
pluginState.insert("filename", file);
requestImportRun("File Data", pluginState);
}

void MainWindow::importBytes(QByteArray rawBytes, QString name)
Expand Down Expand Up @@ -700,6 +690,33 @@ void MainWindow::requestOperatorRun(QString pluginName, QJsonObject pluginState)
}
}

void MainWindow::requestImportRun(QString pluginName, QJsonObject pluginState)
{
QSharedPointer<ImportExportInterface> plugin = m_pluginManager->getImporterExporter(pluginName);
auto result = plugin->importBits(pluginState, this);
if (result.isNull()) {
return;
}
if (!result->getPluginState().isEmpty()) {
this->populateRecentImportsMenu({plugin->getName(), result->getPluginState()});
}
if (!result->getContainer().isNull()) {
QModelIndex addedIndex = this->m_bitContainerManager->getTreeModel()->addContainer(result->getContainer());
this->m_bitContainerManager->getCurrSelectionModel()->setCurrentIndex(
addedIndex,
QItemSelectionModel::ClearAndSelect);
}
}

void MainWindow::requestExportRun(QString pluginName, QJsonObject pluginState)
{
if (currContainer().isNull()) {
warningMessage("Cannot export without a selected bit container");
}
QSharedPointer<ImportExportInterface> plugin = m_pluginManager->getImporterExporter(pluginName);
plugin->exportBits(currContainer(), pluginState, this);
}

void MainWindow::on_pb_analyze_clicked()
{
if (!currContainer().isNull()) {
Expand Down Expand Up @@ -1043,6 +1060,73 @@ void MainWindow::populateRecentTemplatesMenu(QString addition, QString removal)
ui->menuApply_Recent_Template->setEnabled(recentlyUsed.length() > 0);
}


void MainWindow::populateRecentImportsMenu(QPair<QString, QJsonObject> addition, QPair<QString, QJsonObject> removal)
{
QString key = "recently_imported";
QString separator = "/[]\"[]/";

QString additionString;
if (!addition.first.isEmpty() && !addition.second.isEmpty()) {
QJsonDocument doc(addition.second);
QString additionJson(doc.toJson(QJsonDocument::Compact));
additionString = QString("%1%2%3").arg(addition.first).arg(separator).arg(additionJson);
}
QString removalString;
if (!removal.first.isEmpty() && !removal.second.isEmpty()) {
QJsonDocument doc(removal.second);
QString removalJson(doc.toJson(QJsonDocument::Compact));
removalString = QString("%1%2%3").arg(removal.first).arg(separator).arg(removalJson);
}

QStringList recentlyImported;
QVariant currentSetting = SettingsManager::getInstance().getPrivateSetting(key);
if (!currentSetting.isNull() && currentSetting.canConvert<QStringList>()) {
recentlyImported = currentSetting.toStringList();
}

if (!removalString.isEmpty()) {
recentlyImported.removeAll(removalString);
}

if (!additionString.isEmpty()) {
recentlyImported.removeAll(additionString);
recentlyImported.insert(0, additionString);
}

recentlyImported = recentlyImported.mid(0, 10);

SettingsManager::getInstance().setPrivateSetting(key, recentlyImported);

ui->menuImport_Recent->clear();
for (QString importString : recentlyImported) {
QStringList importParts = importString.split(separator);
if (importParts.size() != 2) {
continue;
}
QString pluginName = importParts.at(0);
QSharedPointer<ImportExportInterface> plugin = m_pluginManager->getImporterExporter(pluginName);
if (plugin.isNull()) {
continue;
}

QByteArray pluginStateString = importParts.at(1).toUtf8();
QJsonObject pluginState = QJsonDocument::fromJson(pluginStateString).object();

QString menuLabel = plugin->getImportLabelForState(pluginState);
if (menuLabel.isEmpty()) {
continue;
}
ui->menuImport_Recent->addAction(
menuLabel,
[this, pluginName, pluginState]() {
this->requestImportRun(pluginName, pluginState);
});
}

ui->menuImport_Recent->setEnabled(recentlyImported.length() > 0);
}

void MainWindow::on_tb_scrollReset_clicked()
{
m_displayHandle->setOffsets(0, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/hobbits-gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public slots:

void requestAnalyzerRun(QString pluginName, QJsonObject pluginState);
void requestOperatorRun(QString pluginName, QJsonObject pluginState);
void requestImportRun(QString pluginName, QJsonObject pluginState = QJsonObject());
void requestExportRun(QString pluginName, QJsonObject pluginState = QJsonObject());

QSharedPointer<BitContainer> currContainer();

Expand Down Expand Up @@ -86,6 +88,7 @@ private slots:

void on_actionPreferences_triggered();

void populateRecentImportsMenu(QPair<QString, QJsonObject> addition = QPair<QString, QJsonObject>(), QPair<QString, QJsonObject> removal = QPair<QString, QJsonObject>());
void populateRecentTemplatesMenu(QString addition = QString(), QString removal = QString());

void on_tb_scrollReset_clicked();
Expand Down
20 changes: 17 additions & 3 deletions src/hobbits-gui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
<x>0</x>
<y>0</y>
<width>1286</width>
<height>29</height>
<height>27</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand All @@ -315,15 +315,24 @@
<string>Apply Recent Template</string>
</property>
</widget>
<widget class="QMenu" name="menuImport_Recent">
<property name="enabled">
<bool>false</bool>
</property>
<property name="title">
<string>Import Recent</string>
</property>
</widget>
<addaction name="action_Save_Current_Container"/>
<addaction name="actionOpen_Container"/>
<addaction name="separator"/>
<addaction name="menuImport_Recent"/>
<addaction name="menu_Import_Bits_From"/>
<addaction name="menu_Export_Bits_To"/>
<addaction name="separator"/>
<addaction name="action_Export_Template"/>
<addaction name="actionApply_Template"/>
<addaction name="menuApply_Recent_Template"/>
<addaction name="actionApply_Template"/>
<addaction name="action_Export_Template"/>
</widget>
<widget class="QMenu" name="menu_View">
<property name="title">
Expand Down Expand Up @@ -427,6 +436,11 @@
<string>blah</string>
</property>
</action>
<action name="actiona">
<property name="text">
<string>a</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
Expand Down
Loading

0 comments on commit fc2fba7

Please sign in to comment.