213 lines
8 KiB
C++
213 lines
8 KiB
C++
#include "numberator.h"
|
|
#include "ui_numberator.h"
|
|
#include "ui_TagListDock.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <QToolbar>
|
|
|
|
Numberator::Numberator(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::Numberator)
|
|
, tagsDockUi(new Ui::TagListDock)
|
|
, tagEditUi(new Ui::TagEditDialog)
|
|
, settings("jaseg.de", "Numberator")
|
|
, loadImageDialog(this)
|
|
, proj()
|
|
, tagListModel(proj)
|
|
, tagPropTableModel(&proj)
|
|
, dialogTagPropTableModel(nullptr, false)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
connect(&proj, &SQLiteSaveFile::fileIOError, [=](auto e, QString errorName, QString description) {
|
|
Q_UNUSED(e);
|
|
qDebug() << errorName << ": " << description;
|
|
QMessageBox::critical(this, errorName, description);
|
|
});
|
|
|
|
QDialog *tagEditDialog = new QDialog(this);
|
|
tagEditUi->setupUi(tagEditDialog);
|
|
tagEditUi->tagPropertiesView->setModel(&dialogTagPropTableModel);
|
|
connect(tagEditUi->buttonBox, &QDialogButtonBox::accepted, [=]() {
|
|
Tag newTag(m_tagBeingEdited);
|
|
newTag.name = tagEditUi->tagNameEdit->text();
|
|
newTag.metadata = dialogTagPropTableModel.metadata();
|
|
proj.updateTag(newTag);
|
|
tagEditDialog->hide();
|
|
});
|
|
|
|
connect(tagEditUi->buttonBox, &QDialogButtonBox::rejected, tagEditDialog, &QDialog::hide);
|
|
|
|
connect(ui->graphicsView->scene(), &TagScene::selectionChanged, [=](){
|
|
auto dbg = qDebug() << "tag view selection changed";
|
|
auto items = ui->graphicsView->scene()->selectedItems();
|
|
if (items.isEmpty()) {
|
|
dbg << "<empty>";
|
|
return;
|
|
}
|
|
QGraphicsItem *first = items.first();
|
|
TagItem *it = qgraphicsitem_cast<TagItem *>(first);
|
|
dbg << first << it << first->type() << TagItem::Type;
|
|
if (!it) {
|
|
dbg << "<no tagitem>";
|
|
return;
|
|
}
|
|
dbg << it->tag().id << it->tag().name;
|
|
QSignalBlocker(tagsDockUi->tagList->selectionModel());
|
|
tagsDockUi->tagList->selectionModel()->select(tagListModel.indexOf(it->tag()), QItemSelectionModel::ClearAndSelect);
|
|
tagPropTableModel.showTag(it->tag());
|
|
});
|
|
|
|
/*
|
|
m_tagBeingEdited = it->tag();
|
|
tagEditUi->tagNameEdit->setText(m_tagBeingEdited.name);
|
|
tagEditUi->tagIdLabel->setText(QString::number(m_tagBeingEdited.id));
|
|
tagEditUi->tagLocationLabel->setText(m_tagBeingEdited.humanReadableAnchor());
|
|
dialogTagPropTableModel.showTag(m_tagBeingEdited);
|
|
*/
|
|
|
|
QDockWidget *dock = new QDockWidget(this);
|
|
tagsDockUi->setupUi(dock);
|
|
addDockWidget(Qt::LeftDockWidgetArea, dock);
|
|
tagsDockUi->tagList->setModel(&tagListModel);
|
|
qDebug() << "connecting up model" << tagsDockUi->tagList->selectionModel();
|
|
connect(tagsDockUi->tagList->selectionModel(), &QItemSelectionModel::selectionChanged,
|
|
[=](const QItemSelection &selected, const QItemSelection &deselected) {
|
|
Q_UNUSED(deselected);
|
|
auto dbg = qDebug() << "tagList...selectionChanged";
|
|
if (selected.indexes().isEmpty()) {
|
|
dbg << "<empty>";
|
|
ui->graphicsView->scene()->clearSelection();
|
|
tagPropTableModel.showTag(Tag());
|
|
|
|
} else {
|
|
Tag tag(tagListModel.getTag(selected.indexes().first()));
|
|
qDebug() << tag.id << tag.name;
|
|
tagPropTableModel.showTag(tag);
|
|
QSignalBlocker(ui->graphicsView->scene());
|
|
ui->graphicsView->scene()->selectTag(tag);
|
|
}
|
|
});
|
|
|
|
QToolBar *tools_tb = new QToolBar("Tools", this);
|
|
struct tool_def {
|
|
ToolType type;
|
|
QString filename;
|
|
QString name;
|
|
};
|
|
|
|
std::initializer_list<struct tool_def> tool_defs = {
|
|
{SELECTION_TOOL, ":/icons/selection_tool.png", "Select"},
|
|
{TAG_TOOL, ":/icons/tag.png", "Add Tag"},
|
|
{MOVE_TOOL, ":/icons/move_tool.png", "Move Tag"},
|
|
{EDIT_TOOL, ":/icons/edit_tool.png", "Edit Tag"},
|
|
};
|
|
QActionGroup toolsActionGroup(this);
|
|
for (auto tool : tool_defs) {
|
|
auto action = tools_tb->addAction(QIcon(tool.filename), tool.name, [=](){
|
|
setTool(tool.type);
|
|
});
|
|
action->setCheckable(true);
|
|
toolsActionGroup.addAction(action);
|
|
}
|
|
toolsActionGroup.actions().first()->setChecked(true);
|
|
this->addToolBar(Qt::TopToolBarArea, tools_tb);
|
|
|
|
ui->menuView->addAction(dock->toggleViewAction());
|
|
connect(ui->actionReload_Image, &QAction::triggered, &proj, &SQLiteSaveFile::reloadImageFromDisk);
|
|
|
|
ui->graphicsView->setProject(&proj);
|
|
|
|
tagsDockUi->propertyTable->setModel(&tagPropTableModel);
|
|
|
|
loadImageDialog.setWindowModality(Qt::ApplicationModal);
|
|
loadImageDialog.setWindowTitle("Load Image...");
|
|
loadImageDialog.setNameFilter("Images (*.png, *.xpm, *.jpg)");
|
|
loadImageDialog.setFileMode(QFileDialog::ExistingFile);
|
|
loadImageDialog.restoreState(settings.value("MainWindow/LoadImageFileDialogState").toByteArray());
|
|
connect(&loadImageDialog, &QFileDialog::accepted, [=]() {
|
|
settings.setValue("MainWindow/LoadImageFileDialogState", this->loadImageDialog.saveState());
|
|
});
|
|
connect(&loadImageDialog, &QFileDialog::fileSelected, &proj, &SQLiteSaveFile::loadImageFromDisk);
|
|
connect(ui->actionImport_Image, &QAction::triggered, [=](bool checked){
|
|
Q_UNUSED(checked);
|
|
this->loadImageDialog.open();
|
|
});
|
|
|
|
saveOpenDialog.setWindowModality(Qt::ApplicationModal);
|
|
saveOpenDialog.setNameFilter("Project Files (*.npr);;Any File (*)");
|
|
saveOpenDialog.setFileMode(QFileDialog::AnyFile);
|
|
saveOpenDialog.restoreState(settings.value("MainWindow/SaveAsFileDialogState").toByteArray());
|
|
connect(&saveOpenDialog, &QFileDialog::accepted, [=]() {
|
|
settings.setValue("MainWindow/SaveAsFileDialogState", this->saveOpenDialog.saveState());
|
|
});
|
|
connect(ui->actionSave_Project, &QAction::triggered, this, &Numberator::showSaveDialog);
|
|
connect(ui->actionOpen_Project, &QAction::triggered, [=](){
|
|
if (!showConfirmDiscardDialog())
|
|
return;
|
|
|
|
saveOpenDialog.setWindowTitle("Open Project...");
|
|
saveOpenDialog.setAcceptMode(QFileDialog::AcceptOpen);
|
|
if (saveOpenDialog.exec() == QDialog::Accepted)
|
|
proj.open(saveOpenDialog.selectedFiles().value(0));
|
|
});
|
|
|
|
connect(ui->actionNew_Project, &QAction::triggered, [=]() {
|
|
if (!showConfirmDiscardDialog())
|
|
return;
|
|
|
|
proj.clearNew();
|
|
});
|
|
connect(ui->actionQuit, &QAction::triggered, [=]() {
|
|
if (!showConfirmDiscardDialog())
|
|
return;
|
|
|
|
QApplication::quit();
|
|
});
|
|
connect(ui->actionAbout, &QAction::triggered, &aboutDialog, &AboutDialog::open);
|
|
|
|
connect(ui->actionZoom_to_fit, &QAction::triggered, ui->graphicsView, &TagView::zoomToFit);
|
|
connect(ui->actionZoom_in, &QAction::triggered, ui->graphicsView, &TagView::zoomIn);
|
|
connect(ui->actionZoom_out, &QAction::triggered, ui->graphicsView, &TagView::zoomOut);
|
|
}
|
|
|
|
Numberator::~Numberator()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void Numberator::setTool(Numberator::ToolType tool)
|
|
{
|
|
ui->graphicsView->scene()->setTool(tool);
|
|
}
|
|
|
|
bool Numberator::showConfirmDiscardDialog()
|
|
{
|
|
if (!proj.isMemory() || !proj.isDirty())
|
|
return true;
|
|
|
|
auto btn = QMessageBox::warning(this, "Discard unsaved changes?", "This document contains unsaved changes. Do you want to save these changes?",
|
|
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
|
|
|
|
if (btn == QMessageBox::Cancel)
|
|
return false;
|
|
|
|
if (btn == QMessageBox::Save)
|
|
return showSaveDialog();
|
|
|
|
/* else, the discard button was clicked */
|
|
return true;
|
|
}
|
|
|
|
bool Numberator::showSaveDialog()
|
|
{
|
|
saveOpenDialog.setWindowTitle("Save Project as...");
|
|
saveOpenDialog.setAcceptMode(QFileDialog::AcceptSave);
|
|
if (saveOpenDialog.exec() == QDialog::Accepted) {
|
|
QString fn = this->saveOpenDialog.selectedFiles().value(0);
|
|
qDebug() << QString("Calling saveas(%1)").arg(fn);
|
|
return proj.saveAs(fn);
|
|
}
|
|
|
|
return false;
|
|
}
|