summaryrefslogtreecommitdiffstats
path: root/mappingtreewidget.cpp
blob: c4872bc2009bf2a09556f409203e91915d5ca1bd (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QSortFilterProxyModel>
#include <QComboBox>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QCheckBox>
#include <QLabel>
#include <QStringListModel>
#include <QMessageBox>
#include <QSettings>
#include <QHideEvent>
#include <QAction>
#include <QMenu>
#include <QSqlError>
#include <QApplication>
#include <QToolBar>

#include "mappingtreewidget.h"
#include "mappingtreemodel.h"
#include "mappingtreeview.h"
#include "mappingtreeproxy.h"
#include "mappinginputdialog.h"
#include "mappingdata.h"
#include "smglobals.h"
#include "helper.h"

MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){
    //setup model
    mModel = static_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree"));
    mProxy = new MappingTreeProxy(this);
    mProxy->setSourceModel(mModel);
    mTypesModel = new QStringListModel(this);
    mTypesModel->setStringList(mModel->mappingTypeNames());

    //setup gui
    mTree = new MappingTreeView;
    mTree->setModel(mProxy);
    mTree->setColumnHidden(1, true);
    mTree->setColumnHidden(2, true);
    mTree->setColumnHidden(3, true);
    mTree->setColumnHidden(4, true);
    mTree->setAlternatingRowColors(true);
    mTree->expandAll();
    connect(mModel, &MappingTreeModel::needExpansion, mTree, &MappingTreeView::expandAll);
    connect(mTree->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MappingTreeWidget::selectionChanged);
    /* Hide buttons, this doesn't really work,
     * but keep them for future use :) */
    mTypeBox = new QComboBox;
    mTypeBox->setModel(mTypesModel);
    connect(mTypeBox, QOverload<const QString &>::of(&QComboBox::currentIndexChanged), this, &MappingTreeWidget::typeChanged);
    mTypeBox->setHidden(true);
    mAddType = new QPushButton(tr("Add &type"));
    connect(mAddType, &QPushButton::clicked, this, &MappingTreeWidget::addType);
    mAddType->setHidden(true);
    mDeleteType = new QPushButton(tr("Delete type"));
    connect(mDeleteType, &QPushButton::clicked, this, &MappingTreeWidget::deleteType);
    mDeleteType->setHidden(true);
    QHBoxLayout *typesButtonLayout = new QHBoxLayout;
    typesButtonLayout->addWidget(mDeleteType);
    typesButtonLayout->addWidget(mAddType);

    //select type and populate model
    QSettings s;
    QString lastType = s.value("ui/mappingtreetype").toString();
    if(lastType.isEmpty()){
        if(!mTypesModel->stringList().isEmpty()){
            lastType = mTypesModel->stringList().at(0);
        }
    }
    int typeId = mModel->mappingTypeIdFromName(lastType);
    if(typeId != -1){
        mModel->setType(typeId);
        mTypeBox->setCurrentIndex(mTypeBox->findText(lastType));
        typeChanged(lastType);
    }

    // filter + refresh
    mFilter = new QLineEdit;
    connect(mFilter, &QLineEdit::returnPressed, this, &MappingTreeWidget::filter);
    QToolBar *filterTB = new QToolBar;
    QAction *doFilterA = new QAction(Helper::icon(Qt::transparent, qApp->palette().color(QPalette::Text), QChar(0x2245), true, false), tr("Filter"), this);
    connect(doFilterA, &QAction::triggered, this, &MappingTreeWidget::filter);
    filterTB->addAction(doFilterA);
    QAction *clearFilterA = new QAction(Helper::icon(Qt::transparent, qApp->palette().color(QPalette::Text), QChar(0x2694), true, false), tr("Clear"), this);
    connect(clearFilterA, &QAction::triggered, this, &MappingTreeWidget::clearFilter);
    filterTB->addAction(clearFilterA);
    QAction *refreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh"), this);
    connect(refreshA, &QAction::triggered, mModel, &MappingTreeModel::populate);
    filterTB->addAction(refreshA);
    QLabel *filterLabel = new QLabel(tr("Filter"));
    QHBoxLayout *filterLayout = new QHBoxLayout;
    filterLayout->addWidget(filterLabel);
    filterLayout->addWidget(mFilter);
    filterLayout->addWidget(filterTB);

    //setup actions
    mAddChildA = new QAction(tr("Add child..."), this);
    connect(mAddChildA, &QAction::triggered, this, &MappingTreeWidget::addChild);
    mTree->addAction(mAddChildA);
    mAddActorA = new QAction(tr("Add actor..."), this);
    mAddActorA->setShortcut(Qt::CTRL + Qt::Key_A);
    connect(mAddActorA, &QAction::triggered, this, &MappingTreeWidget::addActor);
    mTree->addAction(mAddActorA);
    mDeleteChildA = new QAction(tr("Delete..."), this);
    connect(mDeleteChildA, &QAction::triggered, this, &MappingTreeWidget::deleteChild);
    mTree->addAction(mDeleteChildA);
    mEditChildA = new QAction(tr("Edit..."), this);
    connect(mEditChildA, &QAction::triggered, this, &MappingTreeWidget::editChild);
    mTree->addAction(mEditChildA);

    //widget layout and misc
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(mTypeBox);
    mainLayout->addLayout(filterLayout);
    mainLayout->addLayout(typesButtonLayout);
    mainLayout->addWidget(mTree);
    setLayout(mainLayout);
}

MappingData MappingTreeWidget::selectedItem() const {
    QModelIndex sel = selected();
    if(!sel.isValid()){
        return MappingData();
    }
    QModelIndex real = mProxy->mapToSource(sel);
    return mModel->mappingDataFromIndex(real);
}

QList<MappingData> MappingTreeWidget::selectedTree() const {
    QModelIndex start = selected();
    QList<MappingData> retval;
    retval << selectedTreeRecursive(start);
    return retval;
}

QList<MappingData> MappingTreeWidget::selectedTreeRecursive(const QModelIndex &start) const{
    QList<MappingData> retval;
    const QAbstractItemModel *startModel = start.model();
    QModelIndex curChild = startModel->index(0, 0, start);
    if(!curChild.isValid()){
        QModelIndex real = mProxy->mapToSource(start);
        retval << mModel->mappingDataFromIndex(real);
        return retval;
    }
    int row = 0;
    while(curChild.isValid()){
        retval << selectedTreeRecursive(curChild);
        ++row;
        curChild = startModel->index(row, 0, start);
    }
    return retval;
}

void MappingTreeWidget::addChild(){
    MappingInputDialog dlg(this);
    int retval = dlg.exec();
    if(retval != QDialog::Accepted){
        return;
    }
    QString value = dlg.mappingName();
    if(value.isEmpty()){
        return;
    }
    QModelIndex parent = mModel->rootIndex();
    if(!dlg.createRoot()){
        QModelIndex sel = selected();
        parent = mProxy->mapToSource(sel);
    }
    int parentId = parent.data(MappingTreeModel::MappingIdRole).toInt();
    if(!mModel->addChild(value, parent)){
        QString err = QString(tr("<p>Failed to create child! Database said:</p><p>%1</p>")).arg(mModel->lastError().text());
        QMessageBox::critical(this, tr("Error"), err);
    }else{
        // we repopulated the model, so all indexes are invalid!
        QModelIndex newPIdx = mModel->findRecursive(parentId, MappingTreeModel::MappingId, mModel->rootIndex());
        if(newPIdx.isValid()){
            QModelIndex newSIdx = mModel->find(value, MappingTreeModel::Name, newPIdx);
            if(newSIdx.isValid()){
                QModelIndex newSIdxP = mProxy->mapFromSource(newSIdx);
                mTree->scrollTo(newSIdxP, QAbstractItemView::EnsureVisible);
                mTree->selectionModel()->setCurrentIndex(newSIdxP, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current | QItemSelectionModel::Rows);
            }
        }
    }
}

void MappingTreeWidget::addActor(){
    QModelIndex actorIndex = mModel->find("actors", MappingTreeModel::Name);
    if(!actorIndex.isValid()){
        QMessageBox::critical(this, tr("Error"), tr("Could not find actors!"));
        return;
    }
    QString newActorName = QInputDialog::getText(this, tr("New actor"), tr("Actor"));
    if(!newActorName.isEmpty()){
        QModelIndex newActorIdx = mModel->find(newActorName, MappingTreeModel::Name, actorIndex);
        if(newActorIdx.isValid()){
            QModelIndex newIdxProxy = mProxy->mapFromSource(newActorIdx);
            mTree->scrollTo(newIdxProxy, QAbstractItemView::EnsureVisible);
            mTree->selectionModel()->setCurrentIndex(newIdxProxy, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current | QItemSelectionModel::Rows);
        }else{
            mModel->addChild(newActorName, actorIndex);
            actorIndex = mModel->find("actors", MappingTreeModel::Name);
            QModelIndex newIdx = mModel->find(newActorName, MappingTreeModel::Name, actorIndex);
            if(newIdx.isValid()){
                QModelIndex newIdxProxy = mProxy->mapFromSource(newIdx);
                mTree->scrollTo(newIdxProxy, QAbstractItemView::EnsureVisible);
                mTree->selectionModel()->setCurrentIndex(newIdxProxy, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current | QItemSelectionModel::Rows);
            }
        }
    }
}


void MappingTreeWidget::addType(){
    QString typeName = QInputDialog::getText(this, tr("Enter type name"), tr("Name")).toLower().trimmed();
    if(typeName.isEmpty()){
        return;
    }
    if(mModel->addMappingType(typeName)){
        mTypesModel->setStringList(mModel->mappingTypeNames());
        int idxOf = mTypeBox->findText(typeName);
        if(idxOf != -1){
            mTypeBox->setCurrentIndex(idxOf);
        }
    }else{
        QMessageBox::critical(this, tr("Error"), tr("Failed to add type. Most likely it already exists!"));
    }
}

void MappingTreeWidget::deleteChild(){
    QModelIndex sel = selected();
    QModelIndex real = mProxy->mapToSource(sel);
    if(mModel->rowCount(real) > 0){
        QString msg = QString(tr("Cannot delete item %1, because it has %2 children!")).arg(real.data().toString()).arg(QString::number(mModel->rowCount(real)));
        QMessageBox::critical(this, tr("Error"), msg);
        return;
    }
    QString question = QString(tr("Really delete %1?")).arg(real.data(MappingTreeModel::NameRole).toString());
    int retval = QMessageBox::question(this, tr("Question"), question, QMessageBox::Yes | QMessageBox::No);
    if(retval == QMessageBox::Yes){
        if(!mModel->deleteChild(real)){
            QSqlError last = mModel->lastError();
            QString msg = QString(tr("Failed to delete %1. Database said: %2")).arg(real.data().toString()).arg(last.text());
            QMessageBox::critical(this, tr("Error"), msg);
        }
    }
}

void MappingTreeWidget::deleteType(){
    QString curText = mTypeBox->currentText();
    if(curText.isEmpty()){
        return;
    }
    QString msg = QString(tr("Really delete %1?")).arg(curText);
    int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
    if(retval == QMessageBox::Yes){
        int typeId = mModel->mappingTypeIdFromName(curText);
        if(typeId != -1){
            if(mModel->deleteMappingType(typeId)){
                mTypesModel->setStringList(mModel->mappingTypeNames());
                if(mTypesModel->stringList().count() > 0){
                    mTypeBox->setCurrentIndex(0);
                }
            }
        }
    }
}

void MappingTreeWidget::hideEvent(QHideEvent *event){
    QString type = mTypeBox->currentText();
    if(!type.isEmpty()){
        QSettings s;
        s.setValue("ui/mappingtreetype", type);
    }
    event->accept();
}

void MappingTreeWidget::typeChanged(QString type){
    int typeId = mModel->mappingTypeIdFromName(type);
    if(typeId != -1){
        mModel->setType(typeId);
        mModel->populate();
        mTree->expandAll();
    }
}

void MappingTreeWidget::editChild(){
    QModelIndex sel = selected();
    QString question = QString("Rename %1").arg(sel.data(MappingTreeModel::NameRole).toString());
    bool ok = false;
    QString newName = QInputDialog::getText(mTree, tr("Rename"), question, QLineEdit::Normal, sel.data(MappingTreeModel::NameRole).toString(), &ok);
    if(ok){
        QModelIndex real = mProxy->mapToSource(sel);
        if(real.isValid()){
            mModel->renameChild(real, newName);
        }
    }
}

void MappingTreeWidget::selectionChanged(){
    QModelIndex sel = selected();
    QModelIndex real = mProxy->mapToSource(sel);
    emit mappingChanged(real.data(MappingTreeModel::MappingIdRole).toInt());
}

void MappingTreeWidget::filter(){
    QString filter = mFilter->text();
    mProxy->setFilter(filter);
    mTree->expandAll();
}

void MappingTreeWidget::clearFilter(){
    mFilter->clear();
    mProxy->setFilter(QString());
    mTree->expandAll();
}

const QModelIndex MappingTreeWidget::selected() const{
    QModelIndexList sel = mTree->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return QModelIndex();
    }
    return sel.first();
}