File: searchsubview.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (161 lines) | stat: -rw-r--r-- 4,852 bytes parent folder | download
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
#include "searchsubview.hpp"

#include <QVBoxLayout>

#include "../../model/doc/document.hpp"
#include "../../model/doc/state.hpp"
#include "../../model/prefs/state.hpp"
#include "../../model/tools/reportmodel.hpp"
#include "../../model/tools/search.hpp"
#include "../../model/world/idtablebase.hpp"

#include "../world/creator.hpp"
#include "../world/tablebottombox.hpp"

#include <apps/opencs/model/prefs/category.hpp>
#include <apps/opencs/model/prefs/setting.hpp>
#include <apps/opencs/model/world/data.hpp>
#include <apps/opencs/view/doc/subview.hpp>

#include "reporttable.hpp"
#include "searchbox.hpp"

void CSVTools::SearchSubView::replace(bool selection)
{
    if (mLocked)
        return;

    std::vector<int> indices = mTable->getReplaceIndices(selection);

    std::string replace = mSearchBox.getReplaceText();

    const CSMTools::ReportModel& model = dynamic_cast<const CSMTools::ReportModel&>(*mTable->model());

    bool autoDelete = CSMPrefs::get()["Search & Replace"]["auto-delete"].isTrue();

    CSMTools::Search search(mSearch);
    CSMWorld::IdTableBase* currentTable = nullptr;

    // We are running through the indices in reverse order to avoid messing up multiple results
    // in a single string.
    for (std::vector<int>::const_reverse_iterator iter(indices.rbegin()); iter != indices.rend(); ++iter)
    {
        const CSMWorld::UniversalId& id = model.getUniversalId(*iter);

        CSMWorld::UniversalId::Type type = CSMWorld::UniversalId::getParentType(id.getType());

        CSMWorld::IdTableBase* table = &dynamic_cast<CSMWorld::IdTableBase&>(*mDocument.getData().getTableModel(type));

        if (table != currentTable)
        {
            search.configure(table);
            currentTable = table;
        }

        std::string hint = model.getHint(*iter);

        if (search.verify(mDocument, table, id, hint))
        {
            search.replace(mDocument, table, id, hint, replace);
            mTable->flagAsReplaced(*iter);

            if (autoDelete)
                mTable->model()->removeRows(*iter, 1);
        }
    }
}

void CSVTools::SearchSubView::showEvent(QShowEvent* event)
{
    CSVDoc::SubView::showEvent(event);
    mSearchBox.focus();
}

CSVTools::SearchSubView::SearchSubView(const CSMWorld::UniversalId& id, CSMDoc::Document& document)
    : CSVDoc::SubView(id)
    , mDocument(document)
    , mLocked(false)
{
    QVBoxLayout* layout = new QVBoxLayout;

    layout->addWidget(&mSearchBox);

    layout->addWidget(mTable = new ReportTable(document, id, true), 2);

    layout->addWidget(mBottom = new CSVWorld::TableBottomBox(CSVWorld::NullCreatorFactory(), document, id, this), 0);

    QWidget* widget = new QWidget;

    widget->setLayout(layout);

    setWidget(widget);

    stateChanged(document.getState(), &document);

    connect(mTable, &ReportTable::editRequest, this, &SearchSubView::focusId);

    connect(mTable, &ReportTable::replaceRequest, this, &SearchSubView::replaceRequest);

    connect(&document, &CSMDoc::Document::stateChanged, this, &SearchSubView::stateChanged);

    connect(
        &mSearchBox, qOverload<const CSMTools::Search&>(&SearchBox::startSearch), this, &SearchSubView::startSearch);

    connect(&mSearchBox, qOverload<>(&SearchBox::replaceAll), this, &SearchSubView::replaceAllRequest);

    connect(document.getReport(id), &CSMTools::ReportModel::rowsRemoved, this, &SearchSubView::tableSizeUpdate);

    connect(document.getReport(id), &CSMTools::ReportModel::rowsInserted, this, &SearchSubView::tableSizeUpdate);

    connect(&document, &CSMDoc::Document::operationDone, this, &SearchSubView::operationDone);
}

void CSVTools::SearchSubView::setEditLock(bool locked)
{
    mLocked = locked;
    mSearchBox.setEditLock(locked);
}

void CSVTools::SearchSubView::setStatusBar(bool show)
{
    mBottom->setStatusBar(show);
}

void CSVTools::SearchSubView::stateChanged(int state, CSMDoc::Document* document)
{
    mSearchBox.setSearchMode(!(state & CSMDoc::State_Searching));
}

void CSVTools::SearchSubView::startSearch(const CSMTools::Search& search)
{
    CSMPrefs::Category& settings = CSMPrefs::get()["Search & Replace"];

    mSearch = search;
    mSearch.setPadding(settings["char-before"].toInt(), settings["char-after"].toInt());

    mTable->clear();
    mDocument.runSearch(getUniversalId(), mSearch);
}

void CSVTools::SearchSubView::replaceRequest()
{
    replace(true);
}

void CSVTools::SearchSubView::replaceAllRequest()
{
    replace(false);
}

void CSVTools::SearchSubView::tableSizeUpdate()
{
    mBottom->tableSizeChanged(mDocument.getReport(getUniversalId())->rowCount(), 0, 0);
}

void CSVTools::SearchSubView::operationDone(int type, bool failed)
{
    if (type == CSMDoc::State_Searching && !failed && !mDocument.getReport(getUniversalId())->rowCount())
    {
        mBottom->setStatusMessage("No Results");
    }
}