Added dynamic option menus to viewer

This commit is contained in:
Mark Long 2016-10-15 10:13:53 -05:00
parent 2a1c2586c3
commit abe2f08e79
15 changed files with 102 additions and 74 deletions

View File

@ -18,22 +18,6 @@ CatalogWidget::CatalogWidget(QWidget *parent) :
connect(ui->catalog_list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), connect(ui->catalog_list, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
SLOT(itemClicked(QListWidgetItem*))); SLOT(itemClicked(QListWidgetItem*)));
// ui->catalog_list->setContextMenuPolicy(Qt::CustomContextMenu);
// connect(ui->catalog_list, SIGNAL(customContextMenuRequested(const QPoint &)),
// SLOT(showContextMenuForWidget(const QPoint &)));
}
void CatalogWidget::showContextMenuForWidget(const QPoint &point) {
QListWidgetItem *selectedItem = ui->catalog_list->itemAt(point);
QMenu contextMenu("Context menu",this);
QAction *viewAction = new QAction("View",this);
connect(viewAction, &QAction::triggered,
[=](){ this->itemClicked(selectedItem); viewAction->deleteLater();});
contextMenu.addAction(viewAction);
contextMenu.exec(mapToGlobal(point));
} }
CatalogWidget::~CatalogWidget() CatalogWidget::~CatalogWidget()
@ -49,7 +33,7 @@ void CatalogWidget::prepForNewDisk(QString filename, DiskFile *disk)
QString CatalogWidget::createToolTip(FileDescriptiveEntry &fde) { QString CatalogWidget::createToolTip(FileDescriptiveEntry &fde) {
QString retval; QString retval;
qDebug() << AppleString(fde.filename).printable().trimmed(); //qDebug() << AppleString(fde.filename).printable().trimmed();
retval += AppleString(fde.filename).printable().trimmed() + "\n"; retval += AppleString(fde.filename).printable().trimmed() + "\n";
retval += QString("Type: %1\n").arg(fde.fileType()); retval += QString("Type: %1\n").arg(fde.fileType());
retval += QString("Sectors: %1 (%2 bytes)\n").arg(fde.lengthInSectors).arg(fde.lengthInSectors*256); retval += QString("Sectors: %1 (%2 bytes)\n").arg(fde.lengthInSectors).arg(fde.lengthInSectors*256);
@ -111,7 +95,6 @@ void CatalogWidget::processNewlyLoadedDisk(QString diskfilename, DiskFile *disk)
idx++; idx++;
} }
ui->catalog_list->resize(maxrect.width(),ui->catalog_list->size().height()); ui->catalog_list->resize(maxrect.width(),ui->catalog_list->size().height());
} }
} }
@ -124,15 +107,10 @@ void CatalogWidget::unloadDisk(DiskFile *disk)
ui->volume_label->clear(); ui->volume_label->clear();
} }
void CatalogWidget::itemClicked(QListWidgetItem *item) void CatalogWidget::itemClicked(QListWidgetItem *item)
{ {
int idx = item->data(0x0100).toInt(); int idx = item->data(0x0100).toInt();
FileDescriptiveEntry fde = m_disk->getAllFDEs()[idx]; FileDescriptiveEntry fde = m_disk->getAllFDEs()[idx];
qDebug() << "Default File " << AppleString(fde.filename).printable().trimmed(); // qDebug() << "Default File " << AppleString(fde.filename).printable().trimmed();
emit openWithDefaultViewer(m_disk,fde); emit openWithDefaultViewer(m_disk,fde);
} }

View File

@ -25,7 +25,6 @@ public slots:
void processNewlyLoadedDisk(QString filename, DiskFile *disk); void processNewlyLoadedDisk(QString filename, DiskFile *disk);
void unloadDisk(DiskFile *disk); void unloadDisk(DiskFile *disk);
void showContextMenuForWidget(const QPoint &);
signals: signals:
void newFileSelected(FileDescriptiveEntry *entry); void newFileSelected(FileDescriptiveEntry *entry);
void openWithDefaultViewer(DiskFile *disk, FileDescriptiveEntry fde); void openWithDefaultViewer(DiskFile *disk, FileDescriptiveEntry fde);

View File

@ -35,8 +35,6 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->action_Unload_Disk_Image, SIGNAL(triggered()), SLOT(unloadDiskFile())); connect(ui->action_Unload_Disk_Image, SIGNAL(triggered()), SLOT(unloadDiskFile()));
connect(ui->catalogWidget,SIGNAL(openWithDefaultViewer(DiskFile*,FileDescriptiveEntry)), connect(ui->catalogWidget,SIGNAL(openWithDefaultViewer(DiskFile*,FileDescriptiveEntry)),
SLOT(handleDiskItemSelectedDefaultOpen(DiskFile*,FileDescriptiveEntry))); SLOT(handleDiskItemSelectedDefaultOpen(DiskFile*,FileDescriptiveEntry)));
connect(ui->catalogWidget,SIGNAL(openWithHexViewer(DiskFile*,FileDescriptiveEntry)),
SLOT(handleDiskItemSelectedHexViewOpen(DiskFile*,FileDescriptiveEntry)));
connect(this, SIGNAL(diskFileLoading(QString, DiskFile*)), connect(this, SIGNAL(diskFileLoading(QString, DiskFile*)),

View File

@ -25,19 +25,6 @@ ApplesoftFileViewer::ApplesoftFileViewer(QWidget *parent) :
ui->textArea->setUndoRedoEnabled(false); ui->textArea->setUndoRedoEnabled(false);
ui->textArea->setUndoRedoEnabled(true); ui->textArea->setUndoRedoEnabled(true);
setIntsAsHex(settings.value("ASViewer.intsAsHex",false).toBool());
ui->intHexCB->setChecked(settings.value("ASViewer.intsAsHex",false).toBool());
setIndentCode(settings.value("ASViewer.indentCode",false).toBool());
ui->indentCode->setChecked(settings.value("ASViewer.indentCode",false).toBool());
connect(ui->intHexCB, SIGNAL(toggled(bool)), SLOT(setIntsAsHex(bool)));
connect(ui->intHexCB, SIGNAL(toggled(bool)), ui->findText,SLOT(clear()));
connect(ui->indentCode, SIGNAL(toggled(bool)), SLOT(setIndentCode(bool)));
connect(ui->indentCode, SIGNAL(toggled(bool)), ui->findText,SLOT(clear()));
connect(ui->varBrowserButton, SIGNAL(clicked(bool)), SLOT(launchVarBrowser()));
} }
ApplesoftFileViewer::~ApplesoftFileViewer() ApplesoftFileViewer::~ApplesoftFileViewer()
@ -45,9 +32,43 @@ ApplesoftFileViewer::~ApplesoftFileViewer()
delete ui; delete ui;
} }
QMenu *ApplesoftFileViewer::optionsMenuItems() const bool ApplesoftFileViewer::makeMenuOptions(QMenu *menu)
{ {
return Q_NULLPTR; qDebug() << "makeMenuOptions()";
QSettings settings;
QAction *action = new QAction("Show &Ints as Hex",menu);
action->setCheckable(true);
action->setChecked(settings.value("ASViewer.intsAsHex",false).toBool());
setIntsAsHex(settings.value("ASViewer.intsAsHex",false).toBool());
connect(action, SIGNAL(toggled(bool)), ui->findText,SLOT(clear()));
connect(action, SIGNAL(toggled(bool)),SLOT(setIntsAsHex(bool)));
menu->addAction(action);
action = new QAction("&Reindent code",menu);
action->setCheckable(true);
action->setChecked(settings.value("ASViewer.indentCode",false).toBool());
setIndentCode(settings.value("ASViewer.indentCode",false).toBool());
connect(action, SIGNAL(toggled(bool)), ui->findText,SLOT(clear()));
connect(action, SIGNAL(toggled(bool)),SLOT(setIndentCode(bool)));
menu->addAction(action);
menu->addSeparator();
action = new QAction("Show &Variable Explorer...",menu);
action->setCheckable(false);
connect(action, SIGNAL(triggered(bool)), SLOT(launchVarBrowser()));
menu->addAction(action);
return true;
}
bool ApplesoftFileViewer::optionsMenuItems(QMenu *menu)
{
qDebug() << "AFV::optionMenuItems()";
return makeMenuOptions(menu);
} }
void ApplesoftFileViewer::setIndentCode(bool enabled) void ApplesoftFileViewer::setIndentCode(bool enabled)

View File

@ -2,6 +2,9 @@
#define APPLESOFTFILEVIEWER_H #define APPLESOFTFILEVIEWER_H
#include <QWidget> #include <QWidget>
#include <QMenu>
#include <QAction>
#include "applesoftfile.h" #include "applesoftfile.h"
#include "applesoftformatter.h" #include "applesoftformatter.h"
#include "viewerbase.h" #include "viewerbase.h"
@ -20,7 +23,7 @@ public:
// void setFormatter(ApplesoftFormatter *formatter); // void setFormatter(ApplesoftFormatter *formatter);
virtual QMenu* optionsMenuItems() const; virtual bool optionsMenuItems(QMenu *menu);
public slots: public slots:
void setFile(GenericFile *file); void setFile(GenericFile *file);
@ -38,6 +41,8 @@ protected slots:
void reformatText(); void reformatText();
private: private:
bool makeMenuOptions(QMenu *menu);
ApplesoftFile *m_file; ApplesoftFile *m_file;
ApplesoftFormatter *m_formatter; ApplesoftFormatter *m_formatter;
bool m_isFirstFind; bool m_isFirstFind;

View File

@ -21,7 +21,7 @@
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0"> <item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,1,0,0,0"> <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,1">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
@ -47,27 +47,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="indentCode">
<property name="text">
<string>Indent Code</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="intHexCB">
<property name="text">
<string>Ints as Hex</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="varBrowserButton">
<property name="text">
<string>Var Broswer</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item row="1" column="0"> <item row="1" column="0">
@ -88,6 +67,14 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>FileViewerInterface</class>
<extends>QWidget</extends>
<header>viewerbase.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View File

@ -16,6 +16,8 @@ public:
void setFile(GenericFile *file); void setFile(GenericFile *file);
void setFile(BinaryFile *file); void setFile(BinaryFile *file);
virtual bool optionsMenuItems(QMenu *) { return false; }
signals: signals:
private: private:

View File

@ -26,6 +26,7 @@ public:
void setText(QString text); void setText(QString text);
QString getPotentialLabel(quint16 address); QString getPotentialLabel(quint16 address);
virtual bool optionsMenuItems(QMenu *) { return false; }
public slots: public slots:
void setFile(GenericFile *file); void setFile(GenericFile *file);

View File

@ -14,7 +14,7 @@ class FileViewerInterface : public QWidget
public: public:
FileViewerInterface(QWidget *parent = Q_NULLPTR) : QWidget(parent) { } FileViewerInterface(QWidget *parent = Q_NULLPTR) : QWidget(parent) { }
virtual QMenu *optionsMenuItems() const { return Q_NULLPTR; } virtual bool optionsMenuItems(QMenu *) = 0;
bool canPrint() const { return false; } bool canPrint() const { return false; }

View File

@ -24,7 +24,7 @@ public:
void setFile(GenericFile *file) { setFile(file,0); } void setFile(GenericFile *file) { setFile(file,0); }
void setFile(GenericFile *file, quint16 offset); void setFile(GenericFile *file, quint16 offset);
virtual bool optionsMenuItems(QMenu *) { return false; }
public slots: public slots:
void showHexAndAsciiValues(); void showHexAndAsciiValues();

View File

@ -51,6 +51,8 @@ public:
static QBitArray byteToBits(quint8 byte); static QBitArray byteToBits(quint8 byte);
void contextMenuEvent(QContextMenuEvent *); void contextMenuEvent(QContextMenuEvent *);
virtual bool optionsMenuItems(QMenu *) { return false; }
signals: signals:
public slots: public slots:

View File

@ -26,6 +26,8 @@ public:
void setFile(BinaryFile *file); void setFile(BinaryFile *file);
virtual bool optionsMenuItems(QMenu *) { return false; }
private: private:
QPixmap m_maze; QPixmap m_maze;
BinaryFile *m_file; BinaryFile *m_file;

View File

@ -26,6 +26,8 @@ public:
void setData(QByteArray data); void setData(QByteArray data);
void setText(QString text); void setText(QString text);
virtual bool optionsMenuItems(QMenu *) { return false; }
protected: protected:
QString makeTextStr(QByteArray data); QString makeTextStr(QByteArray data);
QString makeHexStr(QByteArray data); QString makeHexStr(QByteArray data);

View File

@ -5,6 +5,7 @@
#include <QToolBar> #include <QToolBar>
#include <QComboBox> #include <QComboBox>
#include <QLabel> #include <QLabel>
#include <QDebug>
#include "applesoftfileviewer.h" #include "applesoftfileviewer.h"
#include "hexdumpviewer.h" #include "hexdumpviewer.h"
@ -33,7 +34,10 @@ ViewerBase::ViewerBase(QWidget *parent) :
m_viewercombo = new QComboBox(m_toolbar); m_viewercombo = new QComboBox(m_toolbar);
m_toolbar->addWidget(m_viewercombo); m_toolbar->addWidget(m_viewercombo);
connect(m_viewercombo, SIGNAL(currentIndexChanged(QString)), SLOT(showViewer(QString))); m_optionMenu = new QMenu("&Viewer");
menuBar()->addMenu(m_optionMenu);
m_optionMenu->setEnabled(false);
} }
ViewerBase::~ViewerBase() ViewerBase::~ViewerBase()
@ -52,14 +56,12 @@ void ViewerBase::setFile(GenericFile *file)
descriptor = ("Hex Dump Viewer"); descriptor = ("Hex Dump Viewer");
addViewer(descriptor,hdv); addViewer(descriptor,hdv);
if (dynamic_cast<ApplesoftFile*>(file)) if (dynamic_cast<ApplesoftFile*>(file))
{ {
ApplesoftFileViewer *afv = new ApplesoftFileViewer(0); ApplesoftFileViewer *afv = new ApplesoftFileViewer(0);
afv->setFile(file); afv->setFile(file);
descriptor="Applesoft File Viewer"; descriptor="Applesoft File Viewer";
addViewer(descriptor,afv); addViewer(descriptor,afv);
m_stack->setCurrentWidget(afv);
showViewer(descriptor); showViewer(descriptor);
} }
else if (dynamic_cast<BinaryFile*>(file)) else if (dynamic_cast<BinaryFile*>(file))
@ -96,6 +98,7 @@ void ViewerBase::setFile(GenericFile *file)
thdv->setFile(bf); thdv->setFile(bf);
descriptor = QString("Text/Hex Dump Viewer"); descriptor = QString("Text/Hex Dump Viewer");
addViewer(descriptor,thdv); addViewer(descriptor,thdv);
showViewer(descriptor); showViewer(descriptor);
} }
else if (dynamic_cast<RelocatableFile*>(file)) else if (dynamic_cast<RelocatableFile*>(file))
@ -104,11 +107,15 @@ void ViewerBase::setFile(GenericFile *file)
dv->setFile(file); dv->setFile(file);
descriptor = "Relocatable Disassembler Viewer"; descriptor = "Relocatable Disassembler Viewer";
addViewer(descriptor,dv); addViewer(descriptor,dv);
showViewer(descriptor);
} }
else else
{ {
showViewer(descriptor); showViewer(descriptor);
} }
connect(m_viewercombo, SIGNAL(currentIndexChanged(QString)), SLOT(showViewer(QString)));
} }
void ViewerBase::closeEvent(QCloseEvent *event) void ViewerBase::closeEvent(QCloseEvent *event)
@ -120,16 +127,38 @@ void ViewerBase::addViewer(QString descriptor, FileViewerInterface *viewer)
{ {
if (!m_viewers.contains(descriptor)) if (!m_viewers.contains(descriptor))
{ {
m_stack->addWidget(viewer);
m_viewers[descriptor] = viewer; m_viewers[descriptor] = viewer;
m_viewercombo->addItem(descriptor); m_viewercombo->addItem(descriptor);
m_stack->addWidget(viewer);
} }
} }
void ViewerBase::showViewer(QString descriptor) void ViewerBase::showViewer(QString descriptor)
{ {
m_viewercombo->setCurrentText(descriptor); qDebug() << "showViewer";
FileViewerInterface *fvi = m_viewers[descriptor]; FileViewerInterface *fvi = m_viewers[descriptor];
m_stack->setCurrentWidget(fvi); if (fvi)
setWindowTitle(fvi->windowTitle()); {
m_optionMenu->clear();
m_viewercombo->setCurrentText(descriptor);
m_stack->setCurrentWidget(fvi);
setWindowTitle(fvi->windowTitle());
if (m_optionMenu)
{
if (!fvi->optionsMenuItems(m_optionMenu))
{
m_optionMenu->setEnabled(false);
}
else
{
m_optionMenu->setEnabled(true);
}
}
}
else
{
qDebug() << "Could not find widget for descriptor " << descriptor;
}
} }

View File

@ -25,6 +25,7 @@ public:
public slots: public slots:
void showViewer(QString descriptor); void showViewer(QString descriptor);
protected: protected:
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
void addViewer(QString descriptor, FileViewerInterface *viewer); void addViewer(QString descriptor, FileViewerInterface *viewer);
@ -37,6 +38,7 @@ private:
QMap<QString,FileViewerInterface *> m_viewers; QMap<QString,FileViewerInterface *> m_viewers;
GenericFile *m_file; GenericFile *m_file;
QMenu *m_optionMenu;
}; };