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
|
/*
SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
SPDX-FileCopyrightText: 2007 Dukju Ahn <dukjuahn@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "outputwidget.h"
#include "standardoutputview.h"
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QIcon>
#include <QLineEdit>
#include <QRegularExpression>
#include <QSortFilterProxyModel>
#include <QStackedWidget>
#include <QTabWidget>
#include <QToolButton>
#include <QTreeView>
#include <QVBoxLayout>
#include <QWidgetAction>
#include <KActionCollection>
#include <KColorScheme>
#include <KLocalizedString>
#include <KStandardAction>
#include <KToggleAction>
#include <outputview/ioutputviewmodel.h>
#include <util/focusedtreeview.h>
#include <util/expandablelineedit.h>
#include "outputmodel.h"
#include "outputwidgetconfig.h"
#include "toolviewdata.h"
#include <debug.h>
namespace {
QString validFilterInputToolTip()
{
return i18nc("@info:tooltip", "Enter a case-insensitive regular expression to filter the output view");
}
}
OutputWidget::OutputWidget(QWidget* parent, const ToolViewData* tvdata)
: QWidget(parent)
, m_tabwidget(nullptr)
, m_stackwidget(nullptr)
, data(tvdata)
, m_closeButton(nullptr)
, m_closeOthersAction(nullptr)
, m_nextAction(nullptr)
, m_previousAction(nullptr)
, m_activateOnSelect(nullptr)
, m_focusOnSelect(nullptr)
, m_filterInput(nullptr)
, m_filterAction(nullptr)
, m_outputWidgetConfig(nullptr)
{
setWindowTitle(i18nc("@title:window", "Output View"));
setWindowIcon(tvdata->icon);
auto* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
if( data->type & KDevelop::IOutputView::MultipleView )
{
m_tabwidget = new QTabWidget(this);
layout->addWidget( m_tabwidget );
m_closeButton = new QToolButton( this );
connect( m_closeButton, &QToolButton::clicked, this, &OutputWidget::closeActiveView );
m_closeButton->setIcon( QIcon::fromTheme( QStringLiteral( "tab-close") ) );
m_closeButton->setToolTip(i18nc("@info:tooltip", "Close the currently active output view"));
m_closeButton->setAutoRaise(true);
m_closeOthersAction = new QAction( this );
connect(m_closeOthersAction, &QAction::triggered, this, &OutputWidget::closeOtherViews);
m_closeOthersAction->setIcon(QIcon::fromTheme(QStringLiteral("tab-close-other")));
m_closeOthersAction->setToolTip(i18nc("@info:tooltip", "Close all other output views"));
m_closeOthersAction->setText( m_closeOthersAction->toolTip() );
addAction(m_closeOthersAction);
m_tabwidget->setCornerWidget(m_closeButton, Qt::TopRightCorner);
m_tabwidget->setDocumentMode(true);
} else if ( data->type == KDevelop::IOutputView::HistoryView )
{
m_stackwidget = new QStackedWidget( this );
layout->addWidget( m_stackwidget );
m_previousAction = new QAction(QIcon::fromTheme(QStringLiteral("arrow-left")), i18nc("@action", "Previous Output"), this);
connect(m_previousAction, &QAction::triggered, this, &OutputWidget::previousOutput);
addAction(m_previousAction);
m_nextAction = new QAction(QIcon::fromTheme(QStringLiteral("arrow-right")), i18nc("@action", "Next Output"), this);
connect(m_nextAction, &QAction::triggered, this, &OutputWidget::nextOutput);
addAction(m_nextAction);
}
m_activateOnSelect = new KToggleAction( QIcon(), i18nc("@action", "Select Activated Item"), this );
m_activateOnSelect->setChecked( true );
m_focusOnSelect = new KToggleAction( QIcon(), i18nc("@action", "Focus when Selecting Item"), this );
m_focusOnSelect->setChecked( false );
if( data->option & KDevelop::IOutputView::ShowItemsButton )
{
addAction(m_activateOnSelect);
addAction(m_focusOnSelect);
}
QAction* action;
action = new QAction(QIcon::fromTheme(QStringLiteral("text-wrap")), i18nc("@action", "Word Wrap"), this);
action->setCheckable(true);
connect(action, &QAction::toggled, this, &OutputWidget::setWordWrap);
addAction(action);
auto *separator = new QAction(this);
separator->setSeparator(true);
addAction(separator);
action = new QAction(QIcon::fromTheme(QStringLiteral("go-first")), i18nc("@action", "First Item"), this);
connect(action, &QAction::triggered, this, &OutputWidget::selectFirstItem);
addAction(action);
action = new QAction(QIcon::fromTheme(QStringLiteral("go-previous")), i18nc("@action", "Previous Item"), this);
connect(action, &QAction::triggered, this, &OutputWidget::selectPreviousItem);
addAction(action);
action = new QAction(QIcon::fromTheme(QStringLiteral("go-next")), i18nc("@action", "Next Item"), this);
connect(action, &QAction::triggered, this, &OutputWidget::selectNextItem);
addAction(action);
action = new QAction(QIcon::fromTheme(QStringLiteral("go-last")), i18nc("@action", "Last Item"), this);
connect(action, &QAction::triggered, this, &OutputWidget::selectLastItem);
addAction(action);
QAction* selectAllAction = KStandardAction::selectAll(this, SLOT(selectAll()), this);
selectAllAction->setShortcut(QKeySequence()); //FIXME: why does CTRL-A conflict with Katepart (while CTRL-Cbelow doesn't) ?
selectAllAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
addAction(selectAllAction);
QAction* copyAction = KStandardAction::copy(this, SLOT(copySelection()), this);
copyAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
addAction(copyAction);
auto* clearAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear-list")), i18nc("@action", "Clear"), this);
connect(clearAction, &QAction::triggered, this, &OutputWidget::clearModel);
addAction(clearAction);
if (data->type & KDevelop::IOutputView::MultipleView) {
connect(m_tabwidget, &QTabWidget::currentChanged, this, &OutputWidget::currentViewChanged);
} else if (data->type == KDevelop::IOutputView::HistoryView) {
connect(m_stackwidget, &QStackedWidget::currentChanged, this, &OutputWidget::currentViewChanged);
}
if( data->option & KDevelop::IOutputView::AddFilterAction )
{
auto *separator = new QAction(this);
separator->setSeparator(true);
addAction(separator);
m_filterInput = new KExpandableLineEdit(this);
m_filterInput->setPlaceholderText(i18nc("@info:placeholder", "Search..."));
m_filterInput->setClearButtonEnabled(true);
m_filterInput->setToolTip(validFilterInputToolTip());
m_filterAction = new QWidgetAction(this);
m_filterAction->setText(m_filterInput->placeholderText());
connect(m_filterAction, &QAction::triggered, this, [this]() {m_filterInput->setFocus();});
m_filterAction->setDefaultWidget(m_filterInput);
addAction(m_filterAction);
connect(m_filterInput, &QLineEdit::textEdited,
this, &OutputWidget::outputFilter );
}
if (!data->configSubgroupName.isEmpty()
&& (data->type & KDevelop::IOutputView::MultipleView || data->type & KDevelop::IOutputView::HistoryView)) {
m_outputWidgetConfig = new OutputWidgetConfig(data->configSubgroupName, data->title, this);
connect(m_outputWidgetConfig, &OutputWidgetConfig::settingsChanged, this, [this]() {
const auto maxViewCount = m_outputWidgetConfig->maxViewCount();
// don't close views when view limit is not enabled
if (!maxViewCount.has_value()) {
return;
}
if (data->type & KDevelop::IOutputView::MultipleView) {
closeFirstViewsWhileTooMany(*m_tabwidget, *maxViewCount);
} else {
closeFirstViewsWhileTooMany(*m_stackwidget, *maxViewCount);
}
});
QAction* const openConfigAction = KStandardAction::preferences(
m_outputWidgetConfig,
[this]() {
m_outputWidgetConfig->openDialog(this);
},
this);
openConfigAction->setText(
i18nc("@action %1: output type, e.g. Build or Run", "Configure %1 Output", data->title));
openConfigAction->setShortcut(QKeySequence());
openConfigAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
insertAction(separator, openConfigAction);
}
addActions(data->actionList);
connect( data, &ToolViewData::outputAdded,
this, &OutputWidget::addOutput );
connect( this, &OutputWidget::outputRemoved,
data->plugin, &StandardOutputView::outputRemoved );
for (auto it = data->outputdata.keyBegin(), end =data->outputdata.keyEnd(); it != end; ++it) {
int id = *it;
changeModel( id );
changeDelegate( id );
}
enableActions();
}
OutputWidget::~OutputWidget()
{
// Disconnect our widget to prevent updateFilter() slot calling from parent's destructor,
// which leads to segfault since m_views hash will be destroyed before.
if (m_tabwidget) {
m_tabwidget->disconnect(this);
} else if (m_stackwidget) {
m_stackwidget->disconnect(this);
}
}
void OutputWidget::clearModel()
{
auto view = qobject_cast<QAbstractItemView*>(currentWidget());
if( !view || !view->isVisible())
return;
KDevelop::OutputModel *outputModel = nullptr;
if (auto proxy = qobject_cast<QAbstractProxyModel*>(view->model())) {
outputModel = qobject_cast<KDevelop::OutputModel*>(proxy->sourceModel());
} else {
outputModel = qobject_cast<KDevelop::OutputModel*>(view->model());
}
outputModel->clear();
}
void OutputWidget::addOutput( int id )
{
QTreeView* listview = createListView(id);
setCurrentWidget( listview );
connect( data->outputdata.value(id), &OutputData::modelChanged, this, &OutputWidget::changeModel);
connect( data->outputdata.value(id), &OutputData::delegateChanged, this, &OutputWidget::changeDelegate);
enableActions();
}
void OutputWidget::setCurrentWidget( QTreeView* view )
{
if( data->type & KDevelop::IOutputView::MultipleView )
{
m_tabwidget->setCurrentWidget( view );
} else if( data->type & KDevelop::IOutputView::HistoryView )
{
m_stackwidget->setCurrentWidget( view );
}
}
void OutputWidget::changeDelegate( int id )
{
const auto viewIt = m_views.constFind(id);
const auto dataIt = data->outputdata.constFind(id);
if (dataIt != data->outputdata.constEnd() && viewIt != m_views.constEnd()) {
(*viewIt).view->setItemDelegate((*dataIt)->delegate);
} else {
addOutput(id);
}
}
void OutputWidget::changeModel( int id )
{
const auto viewIt = m_views.constFind(id);
const auto dataIt = data->outputdata.constFind(id);
if (dataIt != data->outputdata.constEnd() && viewIt != m_views.constEnd()) {
(*viewIt).view->setModel((*dataIt)->model);
}
else
{
addOutput( id );
}
}
void OutputWidget::removeOutput( int id )
{
const auto viewIt = m_views.constFind(id);
if (data->outputdata.contains(id) && (viewIt != m_views.constEnd())) {
auto view = viewIt->view;
if( data->type & KDevelop::IOutputView::MultipleView || data->type & KDevelop::IOutputView::HistoryView )
{
if( data->type & KDevelop::IOutputView::MultipleView )
{
int idx = m_tabwidget->indexOf( view );
if (idx != -1)
{
m_tabwidget->removeTab( idx );
}
} else
{
int idx = m_stackwidget->indexOf( view );
if (idx != -1)
{
m_stackwidget->removeWidget(view);
}
}
} else {
// KDevelop::IOutputView::OneView case
// Do nothig here since our single view will be automatically removed from layout
// during it's destroy
}
m_views.erase(viewIt);
// remove our view with proxy model which is view's child (see outputFilter() method).
delete view;
emit outputRemoved( data->toolViewId, id );
}
enableActions();
}
bool OutputWidget::closeView(const QWidget* view)
{
Q_ASSERT(view);
Q_ASSERT(qobject_cast<const QAbstractItemView*>(view));
const auto fvIt = constFindFilteredView(static_cast<const QAbstractItemView*>(view));
if (fvIt == m_views.cend()) {
return false;
}
const auto id = fvIt.key();
if (!(data->outputdata.value(id)->behaviour & KDevelop::IOutputView::AllowUserClose)) {
return false;
}
data->plugin->removeOutput(id);
enableActions();
return true;
}
template<class ViewContainer>
void OutputWidget::closeFirstViewIfTooMany(const ViewContainer& viewContainer)
{
if (!m_outputWidgetConfig) {
return;
}
const auto maxViewCount = m_outputWidgetConfig->maxViewCount();
Q_ASSERT(!maxViewCount.has_value() || *maxViewCount > 0);
if (maxViewCount.has_value() && viewContainer.count() > *maxViewCount) {
closeView(viewContainer.widget(0));
}
}
template<class ViewContainer>
void OutputWidget::closeFirstViewsWhileTooMany(const ViewContainer& viewContainer, int maxViewCount)
{
Q_ASSERT(maxViewCount > 0);
while (viewContainer.count() > maxViewCount) {
if (!closeView(viewContainer.widget(0))) {
break; // Closing a view usually succeeds. Prevent endless loop if it fails.
}
}
}
void OutputWidget::closeActiveView()
{
Q_ASSERT(m_tabwidget);
if (const auto* view = m_tabwidget->currentWidget()) {
closeView(view);
}
}
void OutputWidget::closeOtherViews()
{
Q_ASSERT(m_tabwidget);
QWidget* widget = m_tabwidget->currentWidget();
if (!widget)
return;
const auto ids = m_views.keys();
for (int id : ids) {
if (m_views.value(id).view == widget) {
continue; // leave the active view open
}
OutputData* od = data->outputdata.value(id);
if (od->behaviour & KDevelop::IOutputView::AllowUserClose) {
data->plugin->removeOutput( id );
}
}
enableActions();
}
QWidget* OutputWidget::currentWidget() const
{
QWidget* widget;
if( data->type & KDevelop::IOutputView::MultipleView )
{
widget = m_tabwidget->currentWidget();
} else if( data->type & KDevelop::IOutputView::HistoryView )
{
widget = m_stackwidget->currentWidget();
} else
{
widget = m_views.begin()->view;
}
return widget;
}
KDevelop::IOutputViewModel *OutputWidget::outputViewModel() const
{
auto view = qobject_cast<QAbstractItemView*>(currentWidget());
if( !view || !view->isVisible())
return nullptr;
QAbstractItemModel *absmodel = view->model();
auto* iface = qobject_cast<KDevelop::IOutputViewModel*>(absmodel);
if ( ! iface )
{
// try if it's a proxy model?
if ( auto* proxy = qobject_cast<QAbstractProxyModel*>(absmodel) )
{
iface = qobject_cast<KDevelop::IOutputViewModel*>(proxy->sourceModel());
}
}
return iface;
}
void OutputWidget::eventuallyDoFocus()
{
QWidget* widget = currentWidget();
if( m_focusOnSelect->isChecked() && !widget->hasFocus() ) {
widget->setFocus( Qt::OtherFocusReason );
}
}
QAbstractItemView *OutputWidget::outputView() const
{
return qobject_cast<QAbstractItemView*>(currentWidget());
}
void OutputWidget::activateIndex(const QModelIndex &index, QAbstractItemView *view, KDevelop::IOutputViewModel *iface)
{
if( ! index.isValid() )
return;
QModelIndex sourceIndex = index;
QModelIndex viewIndex = index;
const auto fvIt = constFindFilteredView(view);
if (fvIt != m_views.cend() && fvIt->proxyModel) {
auto proxy = fvIt->proxyModel;
if ( index.model() == proxy ) {
// index is from the proxy, map it to the source
sourceIndex = proxy->mapToSource(index);
} else if (proxy == view->model()) {
// index is from the source, map it to the proxy
viewIndex = proxy->mapFromSource(index);
}
}
view->setCurrentIndex( viewIndex );
view->scrollTo( viewIndex );
if( m_activateOnSelect->isChecked() ) {
iface->activate( sourceIndex );
}
}
void OutputWidget::selectFirstItem()
{
selectItem(First);
}
void OutputWidget::selectNextItem()
{
selectItem(Next);
}
void OutputWidget::selectPreviousItem()
{
selectItem(Previous);
}
void OutputWidget::selectLastItem()
{
selectItem(Last);
}
void OutputWidget::selectItem(SelectionMode selectionMode)
{
auto view = outputView();
auto iface = outputViewModel();
if ( ! view || ! iface )
return;
eventuallyDoFocus();
auto index = view->currentIndex();
const auto fvIt = constFindFilteredView(view);
if (fvIt != m_views.cend() && fvIt->proxyModel) {
auto proxy = fvIt->proxyModel;
if ( index.model() == proxy ) {
// index is from the proxy, map it to the source
index = proxy->mapToSource(index);
}
}
QModelIndex newIndex;
switch (selectionMode) {
case First:
newIndex = iface->firstHighlightIndex();
break;
case Next:
newIndex = iface->nextHighlightIndex( index );
break;
case Previous:
newIndex = iface->previousHighlightIndex( index );
break;
case Last:
newIndex = iface->lastHighlightIndex();
break;
}
qCDebug(PLUGIN_STANDARDOUTPUTVIEW) << "old:" << index << "- new:" << newIndex;
activateIndex(newIndex, view, iface);
}
void OutputWidget::activate(const QModelIndex& index)
{
auto iface = outputViewModel();
auto view = outputView();
if( ! view || ! iface )
return;
activateIndex(index, view, iface);
}
QTreeView* OutputWidget::createListView(int id)
{
auto createHelper = [&]() -> QTreeView* {
auto* listview = new KDevelop::FocusedTreeView(this);
listview->setEditTriggers( QAbstractItemView::NoEditTriggers );
listview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); //Always enable the scrollbar, so it doesn't flash around
listview->setHeaderHidden(true);
listview->setRootIsDecorated(false);
listview->setUniformRowHeights(!m_wordWrap);
listview->setWordWrap(m_wordWrap);
listview->setSelectionMode( QAbstractItemView::ContiguousSelection );
if (data->outputdata.value(id)->behaviour & KDevelop::IOutputView::AutoScroll) {
listview->setAutoScrollAtEnd(true);
}
connect(listview, &QTreeView::activated, this, &OutputWidget::activate);
connect(listview, &QTreeView::clicked, this, &OutputWidget::activate);
return listview;
};
QTreeView* listview = nullptr;
const auto viewIt = m_views.constFind(id);
if (viewIt != m_views.constEnd()) {
listview = viewIt->view;
} else {
bool newView = true;
if( data->type & KDevelop::IOutputView::MultipleView || data->type & KDevelop::IOutputView::HistoryView )
{
qCDebug(PLUGIN_STANDARDOUTPUTVIEW) << "creating listview";
listview = createHelper();
if( data->type & KDevelop::IOutputView::MultipleView )
{
m_tabwidget->addTab(listview, data->outputdata.value(id)->title);
closeFirstViewIfTooMany(*m_tabwidget);
} else
{
const int index = m_stackwidget->addWidget(listview);
m_stackwidget->setCurrentIndex(index);
closeFirstViewIfTooMany(*m_stackwidget);
}
} else
{
if( m_views.isEmpty() )
{
listview = createHelper();
layout()->addWidget(listview);
} else
{
listview = m_views.cbegin().value().view;
newView = false;
}
}
m_views[id].view = listview;
changeModel( id );
changeDelegate( id );
if (newView)
listview->scrollToBottom();
}
enableActions();
return listview;
}
void OutputWidget::raiseOutput(int id)
{
const auto viewIt = m_views.constFind(id);
if (viewIt != m_views.constEnd()) {
auto view = viewIt->view;
if( data->type & KDevelop::IOutputView::MultipleView )
{
int idx = m_tabwidget->indexOf(view);
if( idx >= 0 )
{
m_tabwidget->setCurrentIndex( idx );
}
} else if( data->type & KDevelop::IOutputView::HistoryView )
{
int idx = m_stackwidget->indexOf(view);
if( idx >= 0 )
{
m_stackwidget->setCurrentIndex( idx );
}
}
}
enableActions();
}
void OutputWidget::nextOutput()
{
if( m_stackwidget && m_stackwidget->currentIndex() < m_stackwidget->count()-1 )
{
m_stackwidget->setCurrentIndex( m_stackwidget->currentIndex()+1 );
}
enableActions();
}
void OutputWidget::previousOutput()
{
if( m_stackwidget && m_stackwidget->currentIndex() > 0 )
{
m_stackwidget->setCurrentIndex( m_stackwidget->currentIndex()-1 );
}
enableActions();
}
void OutputWidget::setWordWrap(bool enable)
{
m_wordWrap = enable;
auto* const widget = currentWidget();
if (!widget) {
return; // this happens in test_standardoutputview when the last view is removed
}
auto* const view = qobject_cast<KDevelop::FocusedTreeView*>(widget);
if (!view) {
qCWarning(PLUGIN_STANDARDOUTPUTVIEW) << "current widget is not a FocusedTreeView:" << widget;
return;
}
if (view->wordWrap() == m_wordWrap) {
Q_ASSERT(view->uniformRowHeights() == !m_wordWrap);
return; // nothing changed
}
view->setUniformRowHeights(!m_wordWrap);
view->setWordWrap(m_wordWrap);
view->fitColumns();
}
void OutputWidget::enableActions()
{
if( data->type == KDevelop::IOutputView::HistoryView )
{
Q_ASSERT(m_stackwidget);
Q_ASSERT(m_nextAction);
Q_ASSERT(m_previousAction);
m_previousAction->setEnabled( ( m_stackwidget->currentIndex() > 0 ) );
m_nextAction->setEnabled( ( m_stackwidget->currentIndex() < m_stackwidget->count() - 1 ) );
}
}
void OutputWidget::scrollToIndex( const QModelIndex& idx )
{
QWidget* w = currentWidget();
if( !w )
return;
auto *view = static_cast<QAbstractItemView*>(w);
view->scrollTo( idx );
}
void OutputWidget::copySelection()
{
QWidget* widget = currentWidget();
if( !widget )
return;
auto* view = qobject_cast<QAbstractItemView*>(widget);
if( !view )
return;
QClipboard *cb = QApplication::clipboard();
const QModelIndexList indexes = view->selectionModel()->selectedRows();
QStringList content;
content.reserve(indexes.size());
for (const QModelIndex& index : indexes) {
content += index.data().toString();
}
cb->setText(content.join(QLatin1Char('\n')));
}
void OutputWidget::selectAll()
{
if (auto *view = qobject_cast<QAbstractItemView*>(currentWidget()))
view->selectAll();
}
int OutputWidget::currentOutputIndex()
{
int index = 0;
if( data->type & KDevelop::IOutputView::MultipleView )
{
index = m_tabwidget->currentIndex();
} else if( data->type & KDevelop::IOutputView::HistoryView )
{
index = m_stackwidget->currentIndex();
}
return index;
}
void OutputWidget::outputFilter(const QString& filter)
{
auto *view = qobject_cast<QAbstractItemView*>(currentWidget());
if( !view )
return;
auto fvIt = findFilteredView(view);
auto proxyModel = qobject_cast<QSortFilterProxyModel*>(view->model());
if( !proxyModel )
{
// create new proxy model and make view it's parent. This allows us destroy view and
// it's model with "one shot" (see removeOutput() method).
fvIt->proxyModel = proxyModel = new QSortFilterProxyModel(view);
proxyModel->setDynamicSortFilter(true);
proxyModel->setSourceModel(view->model());
view->setModel(proxyModel);
}
// Don't capture anything as the captures are not used anyway.
QRegularExpression regex(filter, QRegularExpression::CaseInsensitiveOption | QRegularExpression::DontCaptureOption);
fvIt->filter = regex; // store away the original regex to be restored when the current tab/widget changes
if (!regex.isValid()) {
// Setting an invalid regex as the model filter hides all output as expected, but also causes runtime warnings:
// "QString::contains: invalid QRegularExpression object".
// Set a valid regex that matches nothing to hide all output and avoid the warnings.
static const QRegularExpression matchNothing(QStringLiteral("$z"));
regex = matchNothing;
}
proxyModel->setFilterRegularExpression(regex);
updateFilterInputAppearance(constIterator(fvIt));
}
void OutputWidget::updateFilter(int index)
{
QWidget *view = (data->type & KDevelop::IOutputView::MultipleView)
? m_tabwidget->widget(index) : m_stackwidget->widget(index);
const auto fvIt = constFindFilteredView(qobject_cast<QAbstractItemView*>(view));
const QString filterText = fvIt == m_views.cend() ? QString{} : fvIt->filter.pattern();
if (filterText.isEmpty()) {
m_filterInput->clear();
} else {
m_filterInput->setText(filterText);
}
updateFilterInputAppearance(fvIt);
}
void OutputWidget::currentViewChanged(int index)
{
if (data->option & KDevelop::IOutputView::AddFilterAction) {
updateFilter(index);
}
// Update wordwrap status for new view
setWordWrap(m_wordWrap);
}
void OutputWidget::setTitle(int outputId, const QString& title)
{
auto fview = m_views.value(outputId, FilteredView{});
if (fview.view && (data->type & KDevelop::IOutputView::MultipleView)) {
const int idx = m_tabwidget->indexOf(fview.view);
if (idx >= 0) {
m_tabwidget->setTabText(idx, title);
}
}
}
auto OutputWidget::constIterator(FilteredViews::iterator it) -> FilteredViews::const_iterator
{
return static_cast<FilteredViews::const_iterator>(it);
}
template<typename ForwardIt>
ForwardIt OutputWidget::findFilteredView(ForwardIt first, ForwardIt last, const QAbstractItemView* view)
{
return std::find_if(first, last, [view](const FilteredView& filteredView) {
return filteredView.view == view;
});
}
auto OutputWidget::findFilteredView(const QAbstractItemView* view) -> FilteredViews::iterator
{
return findFilteredView(m_views.begin(), m_views.end(), view);
}
auto OutputWidget::constFindFilteredView(const QAbstractItemView* view) const -> FilteredViews::const_iterator
{
return findFilteredView(m_views.cbegin(), m_views.cend(), view);
}
void OutputWidget::updateFilterInputAppearance(FilteredViews::const_iterator currentView)
{
if (currentView == m_views.cend() || currentView->filter.isValid()) {
m_filterInput->setPalette(QPalette{});
m_filterInput->setToolTip(validFilterInputToolTip());
} else {
QPalette background(m_filterInput->palette());
KColorScheme::adjustBackground(background, KColorScheme::NegativeBackground);
m_filterInput->setPalette(background);
m_filterInput->setToolTip(
i18nc("@info:tooltip %1 - position in the pattern, %2 - textual description of the error",
"Filter regular expression pattern error at offset %1: %2", currentView->filter.patternErrorOffset(),
currentView->filter.errorString()));
}
}
#include "moc_outputwidget.cpp"
|