-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrepoutputview.cpp
More file actions
375 lines (311 loc) · 12.6 KB
/
grepoutputview.cpp
File metadata and controls
375 lines (311 loc) · 12.6 KB
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
/**************************************************************************
* Copyright 2010 Silvère Lestang <silvere.lestang@gmail.com> *
* Copyright 2010 Julien Desgats <julien.desgats@gmail.com> *
* *
* 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 "grepoutputview.h"
#include "grepoutputmodel.h"
#include "grepoutputdelegate.h"
#include "ui_grepoutputview.h"
#include "grepviewplugin.h"
#include "grepdialog.h"
#include "greputil.h"
#include "grepjob.h"
#include <QtGui/QAction>
#include <QtGui/QStringListModel>
#include <KMessageBox>
#include <kdebug.h>
#include <QMenu>
#include <interfaces/icore.h>
#include <interfaces/isession.h>
#include <QWidgetAction>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iplugincontroller.h>
using namespace KDevelop;
GrepOutputViewFactory::GrepOutputViewFactory(GrepViewPlugin* plugin)
: m_plugin(plugin)
{}
QWidget* GrepOutputViewFactory::create(QWidget* parent)
{
return new GrepOutputView(parent, m_plugin);
}
Qt::DockWidgetArea GrepOutputViewFactory::defaultPosition()
{
return Qt::BottomDockWidgetArea;
}
QString GrepOutputViewFactory::id() const
{
return "org.kdevelop.GrepOutputView";
}
const int GrepOutputView::HISTORY_SIZE = 5;
GrepOutputView::GrepOutputView(QWidget* parent, GrepViewPlugin* plugin)
: QWidget(parent)
, m_next(0)
, m_prev(0)
, m_collapseAll(0)
, m_expandAll(0)
, m_clearSearchHistory(0)
, m_statusLabel(0)
, m_plugin(plugin)
{
Ui::GrepOutputView::setupUi(this);
setWindowTitle(i18nc("@title:window", "Replace output view"));
setWindowIcon(SmallIcon("edit-find"));
m_prev = new QAction(KIcon("go-previous"), i18n("&Previous item"), this);
m_prev->setEnabled(false);
m_next = new QAction(KIcon("go-next"), i18n("&Next item"), this);
m_next->setEnabled(false);
m_collapseAll = new QAction(KIcon("arrow-left-double"), i18n("C&ollapse all"), this); // TODO change icon
m_collapseAll->setEnabled(false);
m_expandAll = new QAction(KIcon("arrow-right-double"), i18n("&Expand all"), this); // TODO change icon
m_expandAll->setEnabled(false);
QAction *separator = new QAction(this);
separator->setSeparator(true);
QAction *change_criteria = new QAction(KIcon("configure"), i18n("&Change criteria"), this);
m_clearSearchHistory = new QAction(KIcon("edit-clear-list"), i18n("Clear search history"), this);
addAction(m_prev);
addAction(m_next);
addAction(m_collapseAll);
addAction(m_expandAll);
addAction(separator);
addAction(change_criteria);
addAction(m_clearSearchHistory);
separator = new QAction(this);
separator->setSeparator(true);
addAction(separator);
QWidgetAction *statusWidget = new QWidgetAction(this);
m_statusLabel = new QLabel(this);
statusWidget->setDefaultWidget(m_statusLabel);
addAction(statusWidget);
modelSelector->setEditable(false);
modelSelector->setContextMenuPolicy(Qt::CustomContextMenu);
connect(modelSelector, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(modelSelectorContextMenu(QPoint)));
connect(modelSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(changeModel(int)));
resultsTreeView->setItemDelegate(GrepOutputDelegate::self());
resultsTreeView->setHeaderHidden(true);
resultsTreeView->setUniformRowHeights(false);
connect(m_prev, SIGNAL(triggered(bool)), this, SLOT(selectPreviousItem()));
connect(m_next, SIGNAL(triggered(bool)), this, SLOT(selectNextItem()));
connect(m_collapseAll, SIGNAL(triggered(bool)), this, SLOT(collapseAllItems()));
connect(m_expandAll, SIGNAL(triggered(bool)), this, SLOT(expandAllItems()));
connect(applyButton, SIGNAL(clicked()), this, SLOT(onApply()));
connect(m_clearSearchHistory, SIGNAL(triggered(bool)), this, SLOT(clearSearchHistory()));
connect(resultsTreeView, SIGNAL(collapsed(QModelIndex)), this, SLOT(updateScrollArea(QModelIndex)));
connect(resultsTreeView, SIGNAL(expanded(QModelIndex)), this, SLOT(updateScrollArea(QModelIndex)));
IPlugin *outputView = ICore::self()->pluginController()->pluginForExtension("org.kdevelop.IOutputView");
connect(outputView, SIGNAL(selectPrevItem()), this, SLOT(selectPreviousItem()));
connect(outputView, SIGNAL(selectNextItem()), this, SLOT(selectNextItem()));
KConfigGroup cg = ICore::self()->activeSession()->config()->group( "GrepDialog" );
replacementCombo->addItems( cg.readEntry("LastReplacementItems", QStringList()) );
replacementCombo->setInsertPolicy(QComboBox::InsertAtTop);
applyButton->setIcon(KIcon("dialog-ok-apply"));
connect(replacementCombo, SIGNAL(editTextChanged(QString)), SLOT(replacementTextChanged(QString)));
connect(change_criteria, SIGNAL(triggered(bool)), this, SLOT(showDialog()));
updateCheckable();
}
void GrepOutputView::replacementTextChanged(QString)
{
updateCheckable();
if (model()) {
// see https://bugs.kde.org/show_bug.cgi?id=274902 - renewModel can trigger a call here without an active model
updateApplyState(model()->index(0, 0), model()->index(0, 0));
}
}
GrepOutputView::~GrepOutputView()
{
KConfigGroup cg = ICore::self()->activeSession()->config()->group( "GrepDialog" );
cg.writeEntry("LastReplacementItems", qCombo2StringList(replacementCombo, true));
emit outputViewIsClosed();
}
GrepOutputModel* GrepOutputView::renewModel(QString name, QString descriptionOrUrl)
{
// Crear oldest model
while(modelSelector->count() > GrepOutputView::HISTORY_SIZE) {
QVariant var = modelSelector->itemData(GrepOutputView::HISTORY_SIZE - 1);
qvariant_cast<QObject*>(var)->deleteLater();
modelSelector->removeItem(GrepOutputView::HISTORY_SIZE - 1);
}
replacementCombo->clearEditText();
GrepOutputModel* newModel = new GrepOutputModel(resultsTreeView);
applyButton->setEnabled(false);
// text may be already present
newModel->setReplacement(replacementCombo->currentText());
connect(newModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(rowsRemoved()));
connect(resultsTreeView, SIGNAL(activated(QModelIndex)), newModel, SLOT(activate(QModelIndex)));
connect(replacementCombo, SIGNAL(editTextChanged(QString)), newModel, SLOT(setReplacement(QString)));
connect(newModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(expandElements(QModelIndex)));
connect(newModel, SIGNAL(showErrorMessage(QString,int)), this, SLOT(showErrorMessage(QString)));
QString prettyUrl = descriptionOrUrl;
if(descriptionOrUrl.startsWith("/"))
prettyUrl = ICore::self()->projectController()->prettyFileName(descriptionOrUrl, KDevelop::IProjectController::FormatPlain);
// appends new model to history
QString displayName = QString("Search %1 in %2 (at time %3)")
.arg(name)
.arg(prettyUrl)
.arg(QTime::currentTime().toString("hh:mm"));
modelSelector->insertItem(0, displayName, qVariantFromValue<QObject*>(newModel));
modelSelector->setCurrentIndex(0);//setCurrentItem(displayName);
updateCheckable();
return newModel;
}
GrepOutputModel* GrepOutputView::model()
{
return static_cast<GrepOutputModel*>(resultsTreeView->model());
}
void GrepOutputView::changeModel(int index)
{
disconnect(model(), SIGNAL(showMessage(KDevelop::IStatus*,QString)),
this, SLOT(showMessage(KDevelop::IStatus*,QString)));
disconnect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(updateApplyState(QModelIndex,QModelIndex)));
replacementCombo->clearEditText();
//after deleting the whole search history, index is -1
if(index >= 0)
{
QVariant var = modelSelector->itemData(index);
GrepOutputModel *resultModel = static_cast<GrepOutputModel *>(qvariant_cast<QObject*>(var));
resultsTreeView->setModel(resultModel);
connect(model(), SIGNAL(showMessage(KDevelop::IStatus*,QString)),
this, SLOT(showMessage(KDevelop::IStatus*,QString)));
connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(updateApplyState(QModelIndex,QModelIndex)));
model()->showMessageEmit();
applyButton->setEnabled(model()->hasResults() &&
model()->getRootItem() &&
model()->getRootItem()->checkState() != Qt::Unchecked &&
!replacementCombo->currentText().isEmpty());
if(model()->hasResults())
expandElements(QModelIndex());
}
updateCheckable();
updateApplyState(model()->index(0, 0), model()->index(0, 0));
}
void GrepOutputView::setMessage(const QString& msg)
{
m_statusLabel->setText(msg);
}
void GrepOutputView::showErrorMessage( const QString& errorMessage )
{
setStyleSheet("QLabel { color : red; }");
setMessage(errorMessage);
}
void GrepOutputView::showMessage( KDevelop::IStatus* , const QString& message )
{
setStyleSheet("");
setMessage(message);
}
void GrepOutputView::onApply()
{
if(model())
{
Q_ASSERT(model()->rowCount());
// ask a confirmation before an empty string replacement
if(replacementCombo->currentText().length() == 0 &&
KMessageBox::questionYesNo(this, i18n("Do you want to replace with an empty string?"),
i18n("Start replacement")) == KMessageBox::No)
{
return;
}
setEnabled(false);
model()->doReplacements();
setEnabled(true);
}
}
void GrepOutputView::showDialog()
{
m_plugin->showDialog(true);
}
void GrepOutputView::expandElements(const QModelIndex&)
{
m_prev->setEnabled(true);
m_next->setEnabled(true);
m_collapseAll->setEnabled(true);
m_expandAll->setEnabled(true);
resultsTreeView->expandAll();
}
void GrepOutputView::selectPreviousItem()
{
if (!model()) {
return;
}
QModelIndex prev_idx = model()->previousItemIndex(resultsTreeView->currentIndex());
if (prev_idx.isValid()) {
resultsTreeView->setCurrentIndex(prev_idx);
model()->activate(prev_idx);
}
}
void GrepOutputView::selectNextItem()
{
if (!model()) {
return;
}
QModelIndex next_idx = model()->nextItemIndex(resultsTreeView->currentIndex());
if (next_idx.isValid()) {
resultsTreeView->setCurrentIndex(next_idx);
model()->activate(next_idx);
}
}
void GrepOutputView::collapseAllItems()
{
// Collapse everything
resultsTreeView->collapseAll();
// Now reopen the first children, which correspond to the files.
resultsTreeView->expand(resultsTreeView->model()->index(0, 0));
}
void GrepOutputView::expandAllItems()
{
resultsTreeView->expandAll();
}
void GrepOutputView::rowsRemoved()
{
m_prev->setEnabled(model()->rowCount());
m_next->setEnabled(model()->rowCount());
}
void GrepOutputView::updateApplyState(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
Q_UNUSED(bottomRight);
// we only care about root item
if(!topLeft.parent().isValid() && model())
{
applyButton->setEnabled(topLeft.data(Qt::CheckStateRole) != Qt::Unchecked && model()->itemsCheckable());
}
}
void GrepOutputView::updateCheckable()
{
if(model())
model()->makeItemsCheckable(!replacementCombo->currentText().isEmpty() || model()->itemsCheckable());
}
void GrepOutputView::clearSearchHistory()
{
GrepJob *runningJob = m_plugin->grepJob();
if(runningJob)
{
runningJob->kill();
}
while(modelSelector->count() > 0)
{
QVariant var = modelSelector->itemData(0);
qvariant_cast<QObject*>(var)->deleteLater();
modelSelector->removeItem(0);
}
applyButton->setEnabled(false);
m_statusLabel->setText(QString());
}
void GrepOutputView::modelSelectorContextMenu(const QPoint& pos)
{
QPoint globalPos = modelSelector->mapToGlobal(pos);
QMenu myMenu;
myMenu.addAction(m_clearSearchHistory);
myMenu.exec(globalPos);
}
void GrepOutputView::updateScrollArea(const QModelIndex& index)
{
resultsTreeView->resizeColumnToContents( index.column() );
}