summaryrefslogtreecommitdiff
path: root/apps/opencs/view/world/table.cpp
blob: 69242fbca3750e46486897f01ea59d9737bf3dcb (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
#include "table.hpp"

#include <QHeaderView>
#include <QAction>
#include <QMenu>
#include <QContextMenuEvent>
#include <QString>
#include <QMetaObject>
#include <QtCore/qnamespace.h>

#include <components/debug/debuglog.hpp>
#include <components/misc/helpviewer.hpp>
#include <components/misc/stringops.hpp>

#include "../../model/doc/document.hpp"

#include "../../model/world/commands.hpp"
#include "../../model/world/infotableproxymodel.hpp"
#include "../../model/world/idtablebase.hpp"
#include "../../model/world/idtable.hpp"
#include "../../model/world/landtexturetableproxymodel.hpp"
#include "../../model/world/commanddispatcher.hpp"

#include "../../model/prefs/state.hpp"
#include "../../model/prefs/shortcut.hpp"

#include "tableeditidaction.hpp"
#include "tableheadermouseeventhandler.hpp"
#include "util.hpp"

void CSVWorld::Table::contextMenuEvent (QContextMenuEvent *event)
{
    // configure dispatcher
    mDispatcher->setSelection (getSelectedIds());

    std::vector<CSMWorld::UniversalId> extendedTypes = mDispatcher->getExtendedTypes();

    mDispatcher->setExtendedTypes (extendedTypes);

    // create context menu
    QModelIndexList selectedRows = selectionModel()->selectedRows();
    QMenu menu (this);

    ///  \todo add menu items for select all and clear selection

    int currentRow = rowAt(event->y());
    int currentColumn = columnAt(event->x());
    if (mEditIdAction->isValidIdCell(currentRow, currentColumn))
    {
        mEditIdAction->setCell(currentRow, currentColumn);
        menu.addAction(mEditIdAction);
        menu.addSeparator();
    }

    if (!mEditLock && !(mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Constant))
    {
        if (selectedRows.size()==1)
        {
            menu.addAction (mEditAction);

            if (mCreateAction)
                menu.addAction(mCloneAction);
        }

        if (mTouchAction)
            menu.addAction (mTouchAction);

        if (mCreateAction)
            menu.addAction (mCreateAction);

        if (mDispatcher->canRevert())
        {
            menu.addAction (mRevertAction);

            if (!extendedTypes.empty())
                menu.addAction (mExtendedRevertAction);
        }

        if (mDispatcher->canDelete())
        {
            menu.addAction (mDeleteAction);

            if (!extendedTypes.empty())
                menu.addAction (mExtendedDeleteAction);
        }

        if (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_ReorderWithinTopic)
        {
            /// \todo allow reordering of multiple rows
            if (selectedRows.size()==1)
            {
                int column = mModel->searchColumnIndex (CSMWorld::Columns::ColumnId_Topic);

                if (column==-1)
                    column = mModel->searchColumnIndex (CSMWorld::Columns::ColumnId_Journal);

                if (column!=-1)
                {
                    int row = mProxyModel->mapToSource (
                        mProxyModel->index (selectedRows.begin()->row(), 0)).row();
                    QString curData = mModel->data(mModel->index(row, column)).toString();

                    if (row > 0)
                    {
                        QString prevData = mModel->data(mModel->index(row - 1, column)).toString();
                        if (Misc::StringUtils::ciEqual(curData.toStdString(), prevData.toStdString()))
                        {
                            menu.addAction(mMoveUpAction);
                        }
                    }

                    if (row < mModel->rowCount() - 1)
                    {
                        QString nextData = mModel->data(mModel->index(row + 1, column)).toString();
                        if (Misc::StringUtils::ciEqual(curData.toStdString(), nextData.toStdString()))
                        {
                            menu.addAction(mMoveDownAction);
                        }
                    }
                }
            }
        }
    }

    if (selectedRows.size()==1)
    {
        int row = selectedRows.begin()->row();

        row = mProxyModel->mapToSource (mProxyModel->index (row, 0)).row();

        if (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_View)
        {
            CSMWorld::UniversalId id = mModel->view (row).first;

            int index = mDocument.getData().getCells().searchId (id.getId());
            // index==-1: the ID references a worldspace instead of a cell (ignore for now and go
            // ahead)

            if (index==-1 || !mDocument.getData().getCells().getRecord (index).isDeleted())
                menu.addAction (mViewAction);
        }

        if (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Preview)
        {
            const CSMWorld::UniversalId id = getUniversalId(currentRow);
            const CSMWorld::UniversalId::Type type = id.getType();

            QModelIndex index = mModel->index (row,
                mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Modification));

            CSMWorld::RecordBase::State state = static_cast<CSMWorld::RecordBase::State> (
                mModel->data (index).toInt());

            if (state!=CSMWorld::RecordBase::State_Deleted && type != CSMWorld::UniversalId::Type_ItemLevelledList)
                menu.addAction (mPreviewAction);
        }
    }

    if (mHelpAction)
        menu.addAction (mHelpAction);

    menu.exec (event->globalPos());
}

void CSVWorld::Table::mouseDoubleClickEvent (QMouseEvent *event)
{
    Qt::KeyboardModifiers modifiers =
        event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier);

    QModelIndex index = currentIndex();

    selectionModel()->select (index,
        QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);

    std::map<Qt::KeyboardModifiers, DoubleClickAction>::iterator iter =
        mDoubleClickActions.find (modifiers);

    if (iter==mDoubleClickActions.end())
    {
        event->accept();
        return;
    }

    switch (iter->second)
    {
        case Action_None:

            event->accept();
            break;

        case Action_InPlaceEdit:

            DragRecordTable::mouseDoubleClickEvent (event);
            break;

        case Action_EditRecord:

            event->accept();
            editRecord();
            break;

        case Action_View:

            event->accept();
            viewRecord();
            break;

        case Action_Revert:

            event->accept();
            if (mDispatcher->canRevert())
                mDispatcher->executeRevert();
            break;

        case Action_Delete:

            event->accept();
            if (mDispatcher->canDelete())
                mDispatcher->executeDelete();
            break;

        case Action_EditRecordAndClose:

            event->accept();
            editRecord();
            emit closeRequest();
            break;

        case Action_ViewAndClose:

            event->accept();
            viewRecord();
            emit closeRequest();
            break;
    }
}

CSVWorld::Table::Table (const CSMWorld::UniversalId& id,
    bool createAndDelete, bool sorting, CSMDoc::Document& document)
    : DragRecordTable(document), mCreateAction (nullptr), mCloneAction(nullptr), mTouchAction(nullptr),
    mRecordStatusDisplay (0), mJumpToAddedRecord(false), mUnselectAfterJump(false), mAutoJump (false)
{
    mModel = &dynamic_cast<CSMWorld::IdTableBase&> (*mDocument.getData().getTableModel (id));

    bool isInfoTable = id.getType() == CSMWorld::UniversalId::Type_TopicInfos ||
                       id.getType() == CSMWorld::UniversalId::Type_JournalInfos;
    bool isLtexTable = (id.getType() == CSMWorld::UniversalId::Type_LandTextures);
    if (isInfoTable)
    {
        mProxyModel = new CSMWorld::InfoTableProxyModel(id.getType(), this);
        connect (this, &CSVWorld::DragRecordTable::moveRecordsFromSameTable, this, &CSVWorld::Table::moveRecords);
    }
    else if (isLtexTable)
    {
        mProxyModel = new CSMWorld::LandTextureTableProxyModel (this);
    }
    else
    {
        mProxyModel = new CSMWorld::IdTableProxyModel (this);
    }
    mProxyModel->setSourceModel (mModel);

    mDispatcher = new CSMWorld::CommandDispatcher (document, id, this);

    setModel (mProxyModel);
    horizontalHeader()->setSectionResizeMode (QHeaderView::Interactive);
    verticalHeader()->hide();
    setSelectionBehavior (QAbstractItemView::SelectRows);
    setSelectionMode (QAbstractItemView::ExtendedSelection);

    int columns = mModel->columnCount();
    for (int i=0; i<columns; ++i)
    {
        int flags = mModel->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Flags).toInt();

        if (flags & CSMWorld::ColumnBase::Flag_Table)
        {
            CSMWorld::ColumnBase::Display display = static_cast<CSMWorld::ColumnBase::Display> (
                mModel->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Display).toInt());

            CommandDelegate *delegate = CommandDelegateFactoryCollection::get().makeDelegate (display,
                mDispatcher, document, this);

            mDelegates.push_back (delegate);
            setItemDelegateForColumn (i, delegate);
        }
        else
            hideColumn (i);
    }

    if (sorting)
    {
        // FIXME: some tables (e.g. CellRef) have this column hidden, which makes it confusing
        sortByColumn (mModel->findColumnIndex(CSMWorld::Columns::ColumnId_Id), Qt::AscendingOrder);
    }
    setSortingEnabled (sorting);

    mEditAction = new QAction (tr ("Edit Record"), this);
    connect (mEditAction, SIGNAL (triggered()), this, SLOT (editRecord()));
    mEditAction->setIcon(QIcon(":edit-edit"));
    addAction (mEditAction);
    CSMPrefs::Shortcut* editShortcut = new CSMPrefs::Shortcut("table-edit", this);
    editShortcut->associateAction(mEditAction);

    if (createAndDelete)
    {
        mCreateAction = new QAction (tr ("Add Record"), this);
        connect (mCreateAction, SIGNAL (triggered()), this, SIGNAL (createRequest()));
        mCreateAction->setIcon(QIcon(":edit-add"));
        addAction (mCreateAction);
        CSMPrefs::Shortcut* createShortcut = new CSMPrefs::Shortcut("table-add", this);
        createShortcut->associateAction(mCreateAction);

        mCloneAction = new QAction (tr ("Clone Record"), this);
        connect(mCloneAction, SIGNAL (triggered()), this, SLOT (cloneRecord()));
        mCloneAction->setIcon(QIcon(":edit-clone"));
        addAction(mCloneAction);
        CSMPrefs::Shortcut* cloneShortcut = new CSMPrefs::Shortcut("table-clone", this);
        cloneShortcut->associateAction(mCloneAction);
    }

    if (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_AllowTouch)
    {
        mTouchAction = new QAction(tr("Touch Record"), this);
        connect(mTouchAction, SIGNAL(triggered()), this, SLOT(touchRecord()));
        mTouchAction->setIcon(QIcon(":edit-touch"));
        addAction(mTouchAction);
        CSMPrefs::Shortcut* touchShortcut = new CSMPrefs::Shortcut("table-touch", this);
        touchShortcut->associateAction(mTouchAction);
    }

    mRevertAction = new QAction (tr ("Revert Record"), this);
    connect (mRevertAction, SIGNAL (triggered()), mDispatcher, SLOT (executeRevert()));
    mRevertAction->setIcon(QIcon(":edit-undo"));
    addAction (mRevertAction);
    CSMPrefs::Shortcut* revertShortcut = new CSMPrefs::Shortcut("table-revert", this);
    revertShortcut->associateAction(mRevertAction);

    mDeleteAction = new QAction (tr ("Delete Record"), this);
    connect (mDeleteAction, SIGNAL (triggered()), mDispatcher, SLOT (executeDelete()));
    mDeleteAction->setIcon(QIcon(":edit-delete"));
    addAction (mDeleteAction);
    CSMPrefs::Shortcut* deleteShortcut = new CSMPrefs::Shortcut("table-remove", this);
    deleteShortcut->associateAction(mDeleteAction);

    mMoveUpAction = new QAction (tr ("Move Up"), this);
    connect (mMoveUpAction, SIGNAL (triggered()), this, SLOT (moveUpRecord()));
    mMoveUpAction->setIcon(QIcon(":record-up"));
    addAction (mMoveUpAction);
    CSMPrefs::Shortcut* moveUpShortcut = new CSMPrefs::Shortcut("table-moveup", this);
    moveUpShortcut->associateAction(mMoveUpAction);

    mMoveDownAction = new QAction (tr ("Move Down"), this);
    connect (mMoveDownAction, SIGNAL (triggered()), this, SLOT (moveDownRecord()));
    mMoveDownAction->setIcon(QIcon(":record-down"));
    addAction (mMoveDownAction);
    CSMPrefs::Shortcut* moveDownShortcut = new CSMPrefs::Shortcut("table-movedown", this);
    moveDownShortcut->associateAction(mMoveDownAction);

    mViewAction = new QAction (tr ("View"), this);
    connect (mViewAction, SIGNAL (triggered()), this, SLOT (viewRecord()));
    mViewAction->setIcon(QIcon(":/cell.png"));
    addAction (mViewAction);
    CSMPrefs::Shortcut* viewShortcut = new CSMPrefs::Shortcut("table-view", this);
    viewShortcut->associateAction(mViewAction);

    mPreviewAction = new QAction (tr ("Preview"), this);
    connect (mPreviewAction, SIGNAL (triggered()), this, SLOT (previewRecord()));
    mPreviewAction->setIcon(QIcon(":edit-preview"));
    addAction (mPreviewAction);
    CSMPrefs::Shortcut* previewShortcut = new CSMPrefs::Shortcut("table-preview", this);
    previewShortcut->associateAction(mPreviewAction);

    mExtendedDeleteAction = new QAction (tr ("Extended Delete Record"), this);
    connect (mExtendedDeleteAction, SIGNAL (triggered()), this, SLOT (executeExtendedDelete()));
    mExtendedDeleteAction->setIcon(QIcon(":edit-delete"));
    addAction (mExtendedDeleteAction);
    CSMPrefs::Shortcut* extendedDeleteShortcut = new CSMPrefs::Shortcut("table-extendeddelete", this);
    extendedDeleteShortcut->associateAction(mExtendedDeleteAction);

    mExtendedRevertAction = new QAction (tr ("Extended Revert Record"), this);
    connect (mExtendedRevertAction, SIGNAL (triggered()), this, SLOT (executeExtendedRevert()));
    mExtendedRevertAction->setIcon(QIcon(":edit-undo"));
    addAction (mExtendedRevertAction);
    CSMPrefs::Shortcut* extendedRevertShortcut = new CSMPrefs::Shortcut("table-extendedrevert", this);
    extendedRevertShortcut->associateAction(mExtendedRevertAction);

    mEditIdAction = new TableEditIdAction (*this, this);
    connect (mEditIdAction, SIGNAL (triggered()), this, SLOT (editCell()));
    addAction (mEditIdAction);

    mHelpAction = new QAction (tr ("Help"), this);
    connect (mHelpAction, SIGNAL (triggered()), this, SLOT (openHelp()));
    mHelpAction->setIcon(QIcon(":/info.png"));
    addAction (mHelpAction);
    CSMPrefs::Shortcut* openHelpShortcut = new CSMPrefs::Shortcut("help", this);
    openHelpShortcut->associateAction(mHelpAction);

    connect (mProxyModel, SIGNAL (rowsRemoved (const QModelIndex&, int, int)),
        this, SLOT (tableSizeUpdate()));

    connect (mProxyModel, SIGNAL (rowAdded (const std::string &)),
        this, SLOT (rowAdded (const std::string &)));

    /// \note This signal could instead be connected to a slot that filters out changes not affecting
    /// the records status column (for permanence reasons)
    connect (mProxyModel, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (dataChangedEvent(const QModelIndex&, const QModelIndex&)));

    connect (selectionModel(), SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
        this, SLOT (selectionSizeUpdate ()));

    setAcceptDrops(true);

    mDoubleClickActions.insert (std::make_pair (Qt::NoModifier, Action_InPlaceEdit));
    mDoubleClickActions.insert (std::make_pair (Qt::ShiftModifier, Action_EditRecord));
    mDoubleClickActions.insert (std::make_pair (Qt::ControlModifier, Action_View));
    mDoubleClickActions.insert (std::make_pair (Qt::ShiftModifier | Qt::ControlModifier, Action_EditRecordAndClose));

    connect (&CSMPrefs::State::get(), SIGNAL (settingChanged (const CSMPrefs::Setting *)),
        this, SLOT (settingChanged (const CSMPrefs::Setting *)));
    CSMPrefs::get()["ID Tables"].update();

    new TableHeaderMouseEventHandler(this);
}

void CSVWorld::Table::setEditLock (bool locked)
{
    for (std::vector<CommandDelegate *>::iterator iter (mDelegates.begin()); iter!=mDelegates.end(); ++iter)
        (*iter)->setEditLock (locked);

    DragRecordTable::setEditLock(locked);
    mDispatcher->setEditLock (locked);
}

CSMWorld::UniversalId CSVWorld::Table::getUniversalId (int row) const
{
    row = mProxyModel->mapToSource (mProxyModel->index (row, 0)).row();

    int idColumn = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Id);
    int typeColumn = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_RecordType);

    return CSMWorld::UniversalId (
        static_cast<CSMWorld::UniversalId::Type> (mModel->data (mModel->index (row, typeColumn)).toInt()),
        mModel->data (mModel->index (row, idColumn)).toString().toUtf8().constData());
}

std::vector<std::string> CSVWorld::Table::getSelectedIds() const
{
    std::vector<std::string> ids;
    QModelIndexList selectedRows = selectionModel()->selectedRows();
    int columnIndex = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Id);

    for (QModelIndexList::const_iterator iter (selectedRows.begin());
         iter != selectedRows.end();
         ++iter)
    {
        int row = mProxyModel->mapToSource (mProxyModel->index (iter->row(), 0)).row();
        ids.emplace_back(mModel->data (mModel->index (row, columnIndex)).toString().toUtf8().constData());
    }
    return ids;
}

void CSVWorld::Table::editRecord()
{
    if (!mEditLock || (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Constant))
    {
        QModelIndexList selectedRows = selectionModel()->selectedRows();

        if (selectedRows.size()==1)
            emit editRequest (getUniversalId (selectedRows.begin()->row()), "");
    }
}

void CSVWorld::Table::cloneRecord()
{
    if (!mEditLock || (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Constant))
    {
        QModelIndexList selectedRows = selectionModel()->selectedRows();
        const CSMWorld::UniversalId& toClone = getUniversalId(selectedRows.begin()->row());
        if (selectedRows.size() == 1)
        {
            emit cloneRequest (toClone);
        }
    }
}

void CSVWorld::Table::touchRecord()
{
    if (!mEditLock && mModel->getFeatures() & CSMWorld::IdTableBase::Feature_AllowTouch)
    {
        std::vector<CSMWorld::UniversalId> touchIds;

        QModelIndexList selectedRows = selectionModel()->selectedRows();
        for (auto it = selectedRows.begin(); it != selectedRows.end(); ++it)
        {
            touchIds.push_back(getUniversalId(it->row()));
        }

        emit touchRequest(touchIds);
    }
}

void CSVWorld::Table::moveUpRecord()
{
    if (mEditLock || (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Constant))
        return;

    QModelIndexList selectedRows = selectionModel()->selectedRows();

    if (selectedRows.size()==1)
    {
        int row2 =selectedRows.begin()->row();

        if (row2>0)
        {
            int row = row2-1;

            row = mProxyModel->mapToSource (mProxyModel->index (row, 0)).row();
            row2 = mProxyModel->mapToSource (mProxyModel->index (row2, 0)).row();

            if (row2<=row)
                throw std::runtime_error ("Inconsistent row order");

            std::vector<int> newOrder (row2-row+1);
            newOrder[0] = row2-row;
            newOrder[row2-row] = 0;
            for (int i=1; i<row2-row; ++i)
                newOrder[i] = i;

            mDocument.getUndoStack().push (new CSMWorld::ReorderRowsCommand (
                dynamic_cast<CSMWorld::IdTable&> (*mModel), row, newOrder));
        }
    }
}

void CSVWorld::Table::moveDownRecord()
{
    if (mEditLock || (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Constant))
        return;

    QModelIndexList selectedRows = selectionModel()->selectedRows();

    if (selectedRows.size()==1)
    {
        int row =selectedRows.begin()->row();

        if (row<mProxyModel->rowCount()-1)
        {
            int row2 = row+1;

            row = mProxyModel->mapToSource (mProxyModel->index (row, 0)).row();
            row2 = mProxyModel->mapToSource (mProxyModel->index (row2, 0)).row();

            if (row2<=row)
                throw std::runtime_error ("Inconsistent row order");

            std::vector<int> newOrder (row2-row+1);
            newOrder[0] = row2-row;
            newOrder[row2-row] = 0;
            for (int i=1; i<row2-row; ++i)
                newOrder[i] = i;

            mDocument.getUndoStack().push (new CSMWorld::ReorderRowsCommand (
                dynamic_cast<CSMWorld::IdTable&> (*mModel), row, newOrder));
        }
    }
}

void CSVWorld::Table::moveRecords(QDropEvent *event)
{
    if (mEditLock || (mModel->getFeatures() & CSMWorld::IdTableBase::Feature_Constant))
        return;

    QModelIndex targedIndex = indexAt(event->pos());

    QModelIndexList selectedRows = selectionModel()->selectedRows();
    int targetRowRaw = targedIndex.row();
    int targetRow = mProxyModel->mapToSource (mProxyModel->index (targetRowRaw, 0)).row();
    int baseRowRaw = targedIndex.row() - 1;
    int baseRow = mProxyModel->mapToSource (mProxyModel->index (baseRowRaw, 0)).row();
    int highestDifference = 0;

    for (const auto& thisRowData : selectedRows)
    {
        int thisRow = mProxyModel->mapToSource (mProxyModel->index (thisRowData.row(), 0)).row();
        if (std::abs(targetRow - thisRow) > highestDifference) highestDifference = std::abs(targetRow - thisRow);
        if (thisRow - 1 < baseRow) baseRow = thisRow - 1;
    }

    std::vector<int> newOrder (highestDifference + 1);

    for (long unsigned int i = 0; i < newOrder.size(); ++i)
    {
        newOrder[i] = i;
    }

    if (selectedRows.size() > 1)
    {
        Log(Debug::Warning) << "Move operation failed: Moving multiple selections isn't implemented.";
        return;
    }

    for (const auto& thisRowData : selectedRows)
    {
        /*
            Moving algorithm description
            a) Remove the (ORIGIN + 1)th list member.
            b) Add (ORIGIN+1)th list member with value TARGET
            c) If ORIGIN > TARGET,d_INC; ELSE d_DEC
            d_INC) increase all members after (and including) the TARGET by one, stop before hitting ORIGINth address
            d_DEC)  decrease all members after the ORIGIN by one, stop after hitting address TARGET
        */

        int originRowRaw = thisRowData.row();
        int originRow = mProxyModel->mapToSource (mProxyModel->index (originRowRaw, 0)).row();

        newOrder.erase(newOrder.begin() +  originRow - baseRow - 1);
        newOrder.emplace(newOrder.begin() + originRow - baseRow - 1, targetRow - baseRow - 1);

        if (originRow > targetRow)
        {
            for (int i = targetRow - baseRow - 1; i < originRow - baseRow - 1; ++i)
            {
                ++newOrder[i];
            }
        }
        else
        {
            for (int i = originRow - baseRow; i <= targetRow - baseRow - 1; ++i)
            {
                --newOrder[i];
            }
        }

    }
    mDocument.getUndoStack().push (new CSMWorld::ReorderRowsCommand (
        dynamic_cast<CSMWorld::IdTable&> (*mModel), baseRow + 1, newOrder));
}

void CSVWorld::Table::editCell()
{
    emit editRequest(mEditIdAction->getCurrentId(), "");
}

void CSVWorld::Table::openHelp()
{
    Misc::HelpViewer::openHelp("manuals/openmw-cs/tables.html");
}

void CSVWorld::Table::viewRecord()
{
    if (!(mModel->getFeatures() & CSMWorld::IdTableBase::Feature_View))
        return;

    QModelIndexList selectedRows = selectionModel()->selectedRows();

    if (selectedRows.size()==1)
    {
        int row = selectedRows.begin()->row();

        row = mProxyModel->mapToSource (mProxyModel->index (row, 0)).row();

        std::pair<CSMWorld::UniversalId, std::string> params = mModel->view (row);

        if (params.first.getType()!=CSMWorld::UniversalId::Type_None)
            emit editRequest (params.first, params.second);
    }
}

void CSVWorld::Table::previewRecord()
{
    QModelIndexList selectedRows = selectionModel()->selectedRows();

    if (selectedRows.size()==1)
    {
        std::string id = getUniversalId (selectedRows.begin()->row()).getId();

        QModelIndex index = mModel->getModelIndex (id,
            mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Modification));

        if (mModel->data (index)!=CSMWorld::RecordBase::State_Deleted)
            emit editRequest (CSMWorld::UniversalId (CSMWorld::UniversalId::Type_Preview, id),
                "");
    }
}

void CSVWorld::Table::executeExtendedDelete()
{
    if (CSMPrefs::get()["ID Tables"]["extended-config"].isTrue())
    {
        emit extendedDeleteConfigRequest(getSelectedIds());
    }
    else
    {
        QMetaObject::invokeMethod(mDispatcher, "executeExtendedDelete", Qt::QueuedConnection);
    }
}

void CSVWorld::Table::executeExtendedRevert()
{
    if (CSMPrefs::get()["ID Tables"]["extended-config"].isTrue())
    {
        emit extendedRevertConfigRequest(getSelectedIds());
    }
    else
    {
        QMetaObject::invokeMethod(mDispatcher, "executeExtendedRevert", Qt::QueuedConnection);
    }
}

void CSVWorld::Table::settingChanged (const CSMPrefs::Setting *setting)
{
    if (*setting=="ID Tables/jump-to-added")
    {
        if (setting->toString()=="Jump and Select")
        {
            mJumpToAddedRecord = true;
            mUnselectAfterJump = false;
        }
        else if (setting->toString()=="Jump Only")
        {
            mJumpToAddedRecord = true;
            mUnselectAfterJump = true;
        }
        else // No Jump
        {
            mJumpToAddedRecord = false;
            mUnselectAfterJump = false;
        }
    }
    else if (*setting=="Records/type-format" || *setting=="Records/status-format")
    {
        int columns = mModel->columnCount();

        for (int i=0; i<columns; ++i)
            if (QAbstractItemDelegate *delegate = itemDelegateForColumn (i))
            {
                dynamic_cast<CommandDelegate&> (*delegate).settingChanged (setting);
                emit dataChanged (mModel->index (0, i),
                    mModel->index (mModel->rowCount()-1, i));
            }
    }
    else if (setting->getParent()->getKey()=="ID Tables" &&
        setting->getKey().substr (0, 6)=="double")
    {
        std::string modifierString = setting->getKey().substr (6);

        Qt::KeyboardModifiers modifiers;

        if (modifierString=="-s")
            modifiers = Qt::ShiftModifier;
        else if (modifierString=="-c")
            modifiers = Qt::ControlModifier;
        else if (modifierString=="-sc")
            modifiers = Qt::ShiftModifier | Qt::ControlModifier;

        DoubleClickAction action = Action_None;

        std::string value = setting->toString();

        if (value=="Edit in Place")
            action = Action_InPlaceEdit;
        else if (value=="Edit Record")
            action = Action_EditRecord;
        else if (value=="View")
            action = Action_View;
        else if (value=="Revert")
            action = Action_Revert;
        else if (value=="Delete")
            action = Action_Delete;
        else if (value=="Edit Record and Close")
            action = Action_EditRecordAndClose;
        else if (value=="View and Close")
            action = Action_ViewAndClose;

        mDoubleClickActions[modifiers] = action;
    }
}

void CSVWorld::Table::tableSizeUpdate()
{
    int size = 0;
    int deleted = 0;
    int modified = 0;

    if (mProxyModel->columnCount()>0)
    {
        int rows = mProxyModel->rowCount();

        int columnIndex = mModel->searchColumnIndex (CSMWorld::Columns::ColumnId_Modification);

        if (columnIndex!=-1)
        {
            for (int i=0; i<rows; ++i)
            {
                QModelIndex index = mProxyModel->mapToSource (mProxyModel->index (i, 0));

                int state = mModel->data (mModel->index (index.row(), columnIndex)).toInt();

                switch (state)
                {
                    case CSMWorld::RecordBase::State_BaseOnly: ++size; break;
                    case CSMWorld::RecordBase::State_Modified: ++size; ++modified; break;
                    case CSMWorld::RecordBase::State_ModifiedOnly: ++size; ++modified; break;
                    case CSMWorld::RecordBase::State_Deleted: ++deleted; ++modified; break;
                }
            }
        }
        else
            size = rows;
    }

    emit tableSizeChanged (size, deleted, modified);
}

void CSVWorld::Table::selectionSizeUpdate()
{
    emit selectionSizeChanged (selectionModel()->selectedRows().size());
}

void CSVWorld::Table::requestFocus (const std::string& id)
{
    QModelIndex index = mProxyModel->getModelIndex (id, 0);

    if (index.isValid())
    {
        // This will scroll to the row.
        selectRow (index.row());
        // This will actually select it.
        selectionModel()->select (index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
    }
}

void CSVWorld::Table::recordFilterChanged (std::shared_ptr<CSMFilter::Node> filter)
{
    mProxyModel->setFilter (filter);
    tableSizeUpdate();
    selectionSizeUpdate();
}

void CSVWorld::Table::mouseMoveEvent (QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        startDragFromTable(*this);
    }
}

std::vector<std::string> CSVWorld::Table::getColumnsWithDisplay(CSMWorld::ColumnBase::Display display) const
{
    const int count = mModel->columnCount();

    std::vector<std::string> titles;
    for (int i = 0; i < count; ++i)
    {
        CSMWorld::ColumnBase::Display columndisplay = static_cast<CSMWorld::ColumnBase::Display>
                                                     (mModel->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Display).toInt());

        if (display == columndisplay)
        {
            titles.emplace_back(mModel->headerData (i, Qt::Horizontal).toString().toUtf8().constData());
        }
    }
    return titles;
}

std::vector< CSMWorld::UniversalId > CSVWorld::Table::getDraggedRecords() const
{
    QModelIndexList selectedRows = selectionModel()->selectedRows();
    std::vector<CSMWorld::UniversalId> idToDrag;

    for (QModelIndex& it : selectedRows)
        idToDrag.push_back (getUniversalId (it.row()));

    return idToDrag;
}

// parent, start and end depend on the model sending the signal, in this case mProxyModel
//
// If, for example, mModel was used instead, then scrolTo() should use the index
//   mProxyModel->mapFromSource(mModel->index(end, 0))
void CSVWorld::Table::rowAdded(const std::string &id)
{
    tableSizeUpdate();
    if(mJumpToAddedRecord)
    {
        int idColumn = mModel->findColumnIndex(CSMWorld::Columns::ColumnId_Id);
        int end = mProxyModel->getModelIndex(id, idColumn).row();
        selectRow(end);

        // without this delay the scroll works but goes to top for add/clone
        QMetaObject::invokeMethod(this, "queuedScrollTo", Qt::QueuedConnection, Q_ARG(int, end));

        if(mUnselectAfterJump)
            clearSelection();
    }
}

void CSVWorld::Table::queuedScrollTo(int row)
{
    scrollTo(mProxyModel->index(row, 0), QAbstractItemView::PositionAtCenter);
}

void CSVWorld::Table::dataChangedEvent(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    tableSizeUpdate();

    if (mAutoJump)
    {
        selectRow(bottomRight.row());
        scrollTo(bottomRight, QAbstractItemView::PositionAtCenter);
    }
}

void CSVWorld::Table::jumpAfterModChanged(int state)
{
    if(state == Qt::Checked)
        mAutoJump = true;
    else
        mAutoJump = false;
}