summaryrefslogtreecommitdiff
path: root/numberator.cpp
blob: 3fe64973cd71c6094bca81c4c6bbea50a9331736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#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;
}