1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "appoutputpane.h"
27 
28 #include "projectexplorer.h"
29 #include "projectexplorerconstants.h"
30 #include "projectexplorericons.h"
31 #include "runcontrol.h"
32 #include "session.h"
33 #include "windebuginterface.h"
34 
35 #include <coreplugin/actionmanager/actionmanager.h>
36 #include <coreplugin/actionmanager/command.h>
37 #include <coreplugin/coreconstants.h>
38 #include <coreplugin/find/basetextfind.h>
39 #include <coreplugin/icore.h>
40 #include <coreplugin/outputwindow.h>
41 #include <texteditor/behaviorsettings.h>
42 #include <texteditor/fontsettings.h>
43 #include <texteditor/texteditorsettings.h>
44 
45 #include <extensionsystem/invoker.h>
46 #include <extensionsystem/pluginmanager.h>
47 #include <utils/algorithm.h>
48 #include <utils/outputformatter.h>
49 #include <utils/qtcassert.h>
50 #include <utils/utilsicons.h>
51 
52 #include <QAction>
53 #include <QCheckBox>
54 #include <QComboBox>
55 #include <QFormLayout>
56 #include <QHBoxLayout>
57 #include <QLabel>
58 #include <QLoggingCategory>
59 #include <QMenu>
60 #include <QSpinBox>
61 #include <QTabBar>
62 #include <QTabWidget>
63 #include <QTimer>
64 #include <QToolButton>
65 #include <QVBoxLayout>
66 
67 static Q_LOGGING_CATEGORY(appOutputLog, "qtc.projectexplorer.appoutput", QtWarningMsg);
68 
69 using namespace ProjectExplorer;
70 using namespace ProjectExplorer::Internal;
71 
72 const char OPTIONS_PAGE_ID[] = "B.ProjectExplorer.AppOutputOptions";
73 
74 
debuggerPlugin()75 static QObject *debuggerPlugin()
76 {
77     return ExtensionSystem::PluginManager::getObjectByName("DebuggerPlugin");
78 }
79 
msgAttachDebuggerTooltip(const QString & handleDescription=QString ())80 static QString msgAttachDebuggerTooltip(const QString &handleDescription = QString())
81 {
82     return handleDescription.isEmpty() ?
83            AppOutputPane::tr("Attach debugger to this process") :
84            AppOutputPane::tr("Attach debugger to %1").arg(handleDescription);
85 }
86 
87 namespace {
88 const char SETTINGS_KEY[] = "ProjectExplorer/AppOutput/Zoom";
89 const char C_APP_OUTPUT[] = "ProjectExplorer.ApplicationOutput";
90 const char POP_UP_FOR_RUN_OUTPUT_KEY[] = "ProjectExplorer/Settings/ShowRunOutput";
91 const char POP_UP_FOR_DEBUG_OUTPUT_KEY[] = "ProjectExplorer/Settings/ShowDebugOutput";
92 const char CLEAN_OLD_OUTPUT_KEY[] = "ProjectExplorer/Settings/CleanOldAppOutput";
93 const char MERGE_CHANNELS_KEY[] = "ProjectExplorer/Settings/MergeStdErrAndStdOut";
94 const char WRAP_OUTPUT_KEY[] = "ProjectExplorer/Settings/WrapAppOutput";
95 const char MAX_LINES_KEY[] = "ProjectExplorer/Settings/MaxAppOutputLines";
96 }
97 
98 namespace ProjectExplorer {
99 namespace Internal {
100 
101 class TabWidget : public QTabWidget
102 {
103     Q_OBJECT
104 public:
105     TabWidget(QWidget *parent = nullptr);
106 signals:
107     void contextMenuRequested(const QPoint &pos, int index);
108 protected:
109     bool eventFilter(QObject *object, QEvent *event) override;
110 private:
111     void slotContextMenuRequested(const QPoint &pos);
112     int m_tabIndexForMiddleClick = -1;
113 };
114 
TabWidget(QWidget * parent)115 TabWidget::TabWidget(QWidget *parent)
116     : QTabWidget(parent)
117 {
118     tabBar()->installEventFilter(this);
119     setContextMenuPolicy(Qt::CustomContextMenu);
120     connect(this, &QWidget::customContextMenuRequested,
121             this, &TabWidget::slotContextMenuRequested);
122 }
123 
eventFilter(QObject * object,QEvent * event)124 bool TabWidget::eventFilter(QObject *object, QEvent *event)
125 {
126     if (object == tabBar()) {
127         if (event->type() == QEvent::MouseButtonPress) {
128             auto *me = static_cast<QMouseEvent *>(event);
129             if (me->button() == Qt::MiddleButton) {
130                 m_tabIndexForMiddleClick = tabBar()->tabAt(me->pos());
131                 event->accept();
132                 return true;
133             }
134         } else if (event->type() == QEvent::MouseButtonRelease) {
135             auto *me = static_cast<QMouseEvent *>(event);
136             if (me->button() == Qt::MiddleButton) {
137                 int tab = tabBar()->tabAt(me->pos());
138                 if (tab != -1 && tab == m_tabIndexForMiddleClick)
139                     emit tabCloseRequested(tab);
140                 m_tabIndexForMiddleClick = -1;
141                 event->accept();
142                 return true;
143             }
144         }
145     }
146     return QTabWidget::eventFilter(object, event);
147 }
148 
slotContextMenuRequested(const QPoint & pos)149 void TabWidget::slotContextMenuRequested(const QPoint &pos)
150 {
151     emit contextMenuRequested(pos, tabBar()->tabAt(pos));
152 }
153 
RunControlTab(RunControl * runControl,Core::OutputWindow * w)154 AppOutputPane::RunControlTab::RunControlTab(RunControl *runControl, Core::OutputWindow *w) :
155     runControl(runControl), window(w)
156 {
157     if (runControl && w) {
158         w->reset();
159         runControl->setupFormatter(w->outputFormatter());
160     }
161 }
162 
AppOutputPane()163 AppOutputPane::AppOutputPane() :
164     m_mainWidget(new QWidget),
165     m_tabWidget(new TabWidget),
166     m_stopAction(new QAction(tr("Stop"), this)),
167     m_closeCurrentTabAction(new QAction(tr("Close Tab"), this)),
168     m_closeAllTabsAction(new QAction(tr("Close All Tabs"), this)),
169     m_closeOtherTabsAction(new QAction(tr("Close Other Tabs"), this)),
170     m_reRunButton(new QToolButton),
171     m_stopButton(new QToolButton),
172     m_attachButton(new QToolButton),
173     m_settingsButton(new QToolButton),
174     m_formatterWidget(new QWidget)
175 {
176     setObjectName("AppOutputPane"); // Used in valgrind engine
177     loadSettings();
178 
179     // Rerun
180     m_reRunButton->setIcon(Utils::Icons::RUN_SMALL_TOOLBAR.icon());
181     m_reRunButton->setToolTip(tr("Re-run this run-configuration."));
182     m_reRunButton->setEnabled(false);
183     connect(m_reRunButton, &QToolButton::clicked,
184             this, &AppOutputPane::reRunRunControl);
185 
186     // Stop
187     m_stopAction->setIcon(Utils::Icons::STOP_SMALL_TOOLBAR.icon());
188     m_stopAction->setToolTip(tr("Stop running program."));
189     m_stopAction->setEnabled(false);
190 
191     Core::Command *cmd = Core::ActionManager::registerAction(m_stopAction, Constants::STOP);
192     cmd->setDescription(m_stopAction->toolTip());
193 
194     m_stopButton->setDefaultAction(cmd->action());
195 
196     connect(m_stopAction, &QAction::triggered,
197             this, &AppOutputPane::stopRunControl);
198 
199     // Attach
200     m_attachButton->setToolTip(msgAttachDebuggerTooltip());
201     m_attachButton->setEnabled(false);
202     m_attachButton->setIcon(Icons::DEBUG_START_SMALL_TOOLBAR.icon());
203 
204     connect(m_attachButton, &QToolButton::clicked,
205             this, &AppOutputPane::attachToRunControl);
206 
207     connect(this, &IOutputPane::zoomInRequested, this, &AppOutputPane::zoomIn);
208     connect(this, &IOutputPane::zoomOutRequested, this, &AppOutputPane::zoomOut);
209     connect(this, &IOutputPane::resetZoomRequested, this, &AppOutputPane::resetZoom);
210 
211     m_settingsButton->setToolTip(tr("Open Settings Page"));
212     m_settingsButton->setIcon(Utils::Icons::SETTINGS_TOOLBAR.icon());
213     connect(m_settingsButton, &QToolButton::clicked, this, [] {
214         Core::ICore::showOptionsDialog(OPTIONS_PAGE_ID);
215     });
216 
217     auto formatterWidgetsLayout = new QHBoxLayout;
218     formatterWidgetsLayout->setContentsMargins(QMargins());
219     m_formatterWidget->setLayout(formatterWidgetsLayout);
220 
221     // Spacer (?)
222 
223     auto *layout = new QVBoxLayout;
224     layout->setContentsMargins(0, 0, 0, 0);
225     m_tabWidget->setDocumentMode(true);
226     m_tabWidget->setTabsClosable(true);
227     m_tabWidget->setMovable(true);
228     connect(m_tabWidget, &QTabWidget::tabCloseRequested,
229             this, [this](int index) { closeTab(index); });
230     layout->addWidget(m_tabWidget);
231 
232     connect(m_tabWidget, &QTabWidget::currentChanged, this, &AppOutputPane::tabChanged);
233     connect(m_tabWidget, &TabWidget::contextMenuRequested,
234             this, &AppOutputPane::contextMenuRequested);
235 
236     m_mainWidget->setLayout(layout);
237 
238     connect(SessionManager::instance(), &SessionManager::aboutToUnloadSession,
239             this, &AppOutputPane::aboutToUnloadSession);
240 
241     setupFilterUi("AppOutputPane.Filter");
242     setFilteringEnabled(false);
243     setZoomButtonsEnabled(false);
244     setupContext("Core.AppOutputPane", m_mainWidget);
245 }
246 
~AppOutputPane()247 AppOutputPane::~AppOutputPane()
248 {
249     qCDebug(appOutputLog) << "AppOutputPane::~AppOutputPane: Entries left" << m_runControlTabs.size();
250 
251     for (const RunControlTab &rt : qAsConst(m_runControlTabs)) {
252         delete rt.window;
253         delete rt.runControl;
254     }
255     delete m_mainWidget;
256 }
257 
currentIndex() const258 int AppOutputPane::currentIndex() const
259 {
260     if (const QWidget *w = m_tabWidget->currentWidget())
261         return indexOf(w);
262     return -1;
263 }
264 
currentRunControl() const265 RunControl *AppOutputPane::currentRunControl() const
266 {
267     const int index = currentIndex();
268     if (index != -1)
269         return m_runControlTabs.at(index).runControl;
270     return nullptr;
271 }
272 
indexOf(const RunControl * rc) const273 int AppOutputPane::indexOf(const RunControl *rc) const
274 {
275     for (int i = m_runControlTabs.size() - 1; i >= 0; i--)
276         if (m_runControlTabs.at(i).runControl == rc)
277             return i;
278     return -1;
279 }
280 
indexOf(const QWidget * outputWindow) const281 int AppOutputPane::indexOf(const QWidget *outputWindow) const
282 {
283     for (int i = m_runControlTabs.size() - 1; i >= 0; i--)
284         if (m_runControlTabs.at(i).window == outputWindow)
285             return i;
286     return -1;
287 }
288 
tabWidgetIndexOf(int runControlIndex) const289 int AppOutputPane::tabWidgetIndexOf(int runControlIndex) const
290 {
291     if (runControlIndex >= 0 && runControlIndex < m_runControlTabs.size())
292         return m_tabWidget->indexOf(m_runControlTabs.at(runControlIndex).window);
293     return -1;
294 }
295 
updateCloseActions()296 void AppOutputPane::updateCloseActions()
297 {
298     const int tabCount = m_tabWidget->count();
299     m_closeCurrentTabAction->setEnabled(tabCount > 0);
300     m_closeAllTabsAction->setEnabled(tabCount > 0);
301     m_closeOtherTabsAction->setEnabled(tabCount > 1);
302 }
303 
aboutToClose() const304 bool AppOutputPane::aboutToClose() const
305 {
306     return Utils::allOf(m_runControlTabs, [](const RunControlTab &rt) {
307         return !rt.runControl || !rt.runControl->isRunning() || rt.runControl->promptToStop();
308     });
309 }
310 
aboutToUnloadSession()311 void AppOutputPane::aboutToUnloadSession()
312 {
313     closeTabs(CloseTabWithPrompt);
314 }
315 
outputWidget(QWidget *)316 QWidget *AppOutputPane::outputWidget(QWidget *)
317 {
318     return m_mainWidget;
319 }
320 
toolBarWidgets() const321 QList<QWidget*> AppOutputPane::toolBarWidgets() const
322 {
323     return QList<QWidget *>{m_reRunButton, m_stopButton, m_attachButton, m_settingsButton,
324                 m_formatterWidget} + IOutputPane::toolBarWidgets();
325 }
326 
displayName() const327 QString AppOutputPane::displayName() const
328 {
329     return tr("Application Output");
330 }
331 
priorityInStatusBar() const332 int AppOutputPane::priorityInStatusBar() const
333 {
334     return 60;
335 }
336 
clearContents()337 void AppOutputPane::clearContents()
338 {
339     auto *currentWindow = qobject_cast<Core::OutputWindow *>(m_tabWidget->currentWidget());
340     if (currentWindow)
341         currentWindow->clear();
342 }
343 
hasFocus() const344 bool AppOutputPane::hasFocus() const
345 {
346     QWidget *widget = m_tabWidget->currentWidget();
347     if (!widget)
348         return false;
349     return widget->window()->focusWidget() == widget;
350 }
351 
canFocus() const352 bool AppOutputPane::canFocus() const
353 {
354     return m_tabWidget->currentWidget();
355 }
356 
setFocus()357 void AppOutputPane::setFocus()
358 {
359     if (m_tabWidget->currentWidget())
360         m_tabWidget->currentWidget()->setFocus();
361 }
362 
updateFilter()363 void AppOutputPane::updateFilter()
364 {
365     const int index = currentIndex();
366     if (index != -1) {
367         m_runControlTabs.at(index).window->updateFilterProperties(
368                     filterText(), filterCaseSensitivity(), filterUsesRegexp(), filterIsInverted());
369     }
370 }
371 
createNewOutputWindow(RunControl * rc)372 void AppOutputPane::createNewOutputWindow(RunControl *rc)
373 {
374     QTC_ASSERT(rc, return);
375 
376     connect(rc, &RunControl::aboutToStart,
377             this, &AppOutputPane::slotRunControlChanged);
378     connect(rc, &RunControl::started,
379             this, &AppOutputPane::slotRunControlChanged);
380     connect(rc, &RunControl::stopped,
381             this, &AppOutputPane::slotRunControlFinished);
382     connect(rc, &RunControl::applicationProcessHandleChanged,
383             this, &AppOutputPane::enableDefaultButtons);
384     connect(rc, &RunControl::appendMessage,
385             this, [this, rc](const QString &out, Utils::OutputFormat format) {
386                 appendMessage(rc, out, format);
387             });
388 
389     // First look if we can reuse a tab
390     const Runnable thisRunnable = rc->runnable();
391     const int tabIndex = Utils::indexOf(m_runControlTabs, [&](const RunControlTab &tab) {
392         if (!tab.runControl || tab.runControl->isRunning())
393             return false;
394         const Runnable otherRunnable = tab.runControl->runnable();
395         return thisRunnable.executable == otherRunnable.executable
396                 && thisRunnable.commandLineArguments == otherRunnable.commandLineArguments
397                 && thisRunnable.workingDirectory == otherRunnable.workingDirectory
398                 && thisRunnable.environment == otherRunnable.environment;
399     });
400     if (tabIndex != -1) {
401         RunControlTab &tab = m_runControlTabs[tabIndex];
402         // Reuse this tab
403         if (tab.runControl)
404             tab.runControl->initiateFinish();
405         tab.runControl = rc;
406         tab.window->reset();
407         rc->setupFormatter(tab.window->outputFormatter());
408 
409         handleOldOutput(tab.window);
410 
411         // Update the title.
412         m_tabWidget->setTabText(tabIndex, rc->displayName());
413 
414         tab.window->scrollToBottom();
415         qCDebug(appOutputLog) << "AppOutputPane::createNewOutputWindow: Reusing tab"
416                               << tabIndex << "for" << rc;
417         return;
418     }
419     // Create new
420     static int counter = 0;
421     Utils::Id contextId = Utils::Id(C_APP_OUTPUT).withSuffix(counter++);
422     Core::Context context(contextId);
423     Core::OutputWindow *ow = new Core::OutputWindow(context, SETTINGS_KEY, m_tabWidget);
424     ow->setWindowTitle(tr("Application Output Window"));
425     ow->setWindowIcon(Icons::WINDOW.icon());
426     ow->setWordWrapEnabled(m_settings.wrapOutput);
427     ow->setMaxCharCount(m_settings.maxCharCount);
428 
429     auto updateFontSettings = [ow] {
430         ow->setBaseFont(TextEditor::TextEditorSettings::fontSettings().font());
431     };
432 
433     auto updateBehaviorSettings = [ow] {
434         ow->setWheelZoomEnabled(
435                     TextEditor::TextEditorSettings::behaviorSettings().m_scrollWheelZooming);
436     };
437 
438     updateFontSettings();
439     updateBehaviorSettings();
440 
441     connect(ow, &Core::OutputWindow::wheelZoom, this, [this, ow]() {
442         float fontZoom = ow->fontZoom();
443         for (const RunControlTab &tab : qAsConst(m_runControlTabs))
444             tab.window->setFontZoom(fontZoom);
445     });
446     connect(TextEditor::TextEditorSettings::instance(), &TextEditor::TextEditorSettings::fontSettingsChanged,
447             ow, updateFontSettings);
448     connect(TextEditor::TextEditorSettings::instance(), &TextEditor::TextEditorSettings::behaviorSettingsChanged,
449             ow, updateBehaviorSettings);
450 
451     auto *agg = new Aggregation::Aggregate;
452     agg->add(ow);
453     agg->add(new Core::BaseTextFind(ow));
454     m_runControlTabs.push_back(RunControlTab(rc, ow));
455     m_tabWidget->addTab(ow, rc->displayName());
456     qCDebug(appOutputLog) << "AppOutputPane::createNewOutputWindow: Adding tab for" << rc;
457     updateCloseActions();
458     setFilteringEnabled(m_tabWidget->count() > 0);
459 }
460 
handleOldOutput(Core::OutputWindow * window) const461 void AppOutputPane::handleOldOutput(Core::OutputWindow *window) const
462 {
463     if (m_settings.cleanOldOutput)
464         window->clear();
465     else
466         window->grayOutOldContent();
467 }
468 
updateFromSettings()469 void AppOutputPane::updateFromSettings()
470 {
471     for (const RunControlTab &tab : qAsConst(m_runControlTabs)) {
472         tab.window->setWordWrapEnabled(m_settings.wrapOutput);
473         tab.window->setMaxCharCount(m_settings.maxCharCount);
474     }
475 }
476 
appendMessage(RunControl * rc,const QString & out,Utils::OutputFormat format)477 void AppOutputPane::appendMessage(RunControl *rc, const QString &out, Utils::OutputFormat format)
478 {
479     const int index = indexOf(rc);
480     if (index != -1) {
481         Core::OutputWindow *window = m_runControlTabs.at(index).window;
482         QString stringToWrite;
483         if (format == Utils::NormalMessageFormat || format == Utils::ErrorMessageFormat) {
484             stringToWrite = QTime::currentTime().toString();
485             stringToWrite += ": ";
486         }
487         stringToWrite += out;
488         window->appendMessage(stringToWrite, format);
489         if (format != Utils::NormalMessageFormat) {
490             RunControlTab &tab = m_runControlTabs[index];
491             switch (tab.behaviorOnOutput) {
492             case AppOutputPaneMode::FlashOnOutput:
493                 flash();
494                 break;
495             case AppOutputPaneMode::PopupOnFirstOutput:
496                 tab.behaviorOnOutput = AppOutputPaneMode::FlashOnOutput;
497                 Q_FALLTHROUGH();
498             case AppOutputPaneMode::PopupOnOutput:
499                 popup(NoModeSwitch);
500                 break;
501             }
502         }
503     }
504 }
505 
setSettings(const AppOutputSettings & settings)506 void AppOutputPane::setSettings(const AppOutputSettings &settings)
507 {
508     m_settings = settings;
509     storeSettings();
510     updateFromSettings();
511 }
512 
513 const AppOutputPaneMode kRunOutputModeDefault = AppOutputPaneMode::PopupOnFirstOutput;
514 const AppOutputPaneMode kDebugOutputModeDefault = AppOutputPaneMode::FlashOnOutput;
515 const bool kCleanOldOutputDefault = false;
516 const bool kMergeChannelsDefault = false;
517 const bool kWrapOutputDefault = true;
518 
storeSettings() const519 void AppOutputPane::storeSettings() const
520 {
521     Utils::QtcSettings *const s = Core::ICore::settings();
522     s->setValueWithDefault(POP_UP_FOR_RUN_OUTPUT_KEY,
523                            int(m_settings.runOutputMode),
524                            int(kRunOutputModeDefault));
525     s->setValueWithDefault(POP_UP_FOR_DEBUG_OUTPUT_KEY,
526                            int(m_settings.debugOutputMode),
527                            int(kDebugOutputModeDefault));
528     s->setValueWithDefault(CLEAN_OLD_OUTPUT_KEY, m_settings.cleanOldOutput, kCleanOldOutputDefault);
529     s->setValueWithDefault(MERGE_CHANNELS_KEY, m_settings.mergeChannels, kMergeChannelsDefault);
530     s->setValueWithDefault(WRAP_OUTPUT_KEY, m_settings.wrapOutput, kWrapOutputDefault);
531     s->setValueWithDefault(MAX_LINES_KEY,
532                            m_settings.maxCharCount / 100,
533                            Core::Constants::DEFAULT_MAX_CHAR_COUNT);
534 }
535 
loadSettings()536 void AppOutputPane::loadSettings()
537 {
538     QSettings * const s = Core::ICore::settings();
539     const auto modeFromSettings = [s](const QString key, AppOutputPaneMode defaultValue) {
540         return static_cast<AppOutputPaneMode>(s->value(key, int(defaultValue)).toInt());
541     };
542     m_settings.runOutputMode = modeFromSettings(POP_UP_FOR_RUN_OUTPUT_KEY, kRunOutputModeDefault);
543     m_settings.debugOutputMode = modeFromSettings(POP_UP_FOR_DEBUG_OUTPUT_KEY,
544                                                   kDebugOutputModeDefault);
545     m_settings.cleanOldOutput = s->value(CLEAN_OLD_OUTPUT_KEY, kCleanOldOutputDefault).toBool();
546     m_settings.mergeChannels = s->value(MERGE_CHANNELS_KEY, kMergeChannelsDefault).toBool();
547     m_settings.wrapOutput = s->value(WRAP_OUTPUT_KEY, kWrapOutputDefault).toBool();
548     m_settings.maxCharCount = s->value(MAX_LINES_KEY,
549                                        Core::Constants::DEFAULT_MAX_CHAR_COUNT).toInt() * 100;
550 }
551 
showTabFor(RunControl * rc)552 void AppOutputPane::showTabFor(RunControl *rc)
553 {
554     m_tabWidget->setCurrentIndex(tabWidgetIndexOf(indexOf(rc)));
555 }
556 
setBehaviorOnOutput(RunControl * rc,AppOutputPaneMode mode)557 void AppOutputPane::setBehaviorOnOutput(RunControl *rc, AppOutputPaneMode mode)
558 {
559     const int index = indexOf(rc);
560     if (index != -1)
561         m_runControlTabs[index].behaviorOnOutput = mode;
562 }
563 
reRunRunControl()564 void AppOutputPane::reRunRunControl()
565 {
566     const int index = currentIndex();
567     const RunControlTab &tab = m_runControlTabs.at(index);
568     QTC_ASSERT(tab.runControl, return);
569     QTC_ASSERT(index != -1 && !tab.runControl->isRunning(), return);
570 
571     handleOldOutput(tab.window);
572     tab.window->scrollToBottom();
573     tab.runControl->initiateReStart();
574 }
575 
attachToRunControl()576 void AppOutputPane::attachToRunControl()
577 {
578     const int index = currentIndex();
579     QTC_ASSERT(index != -1, return);
580     RunControl *rc = m_runControlTabs.at(index).runControl;
581     QTC_ASSERT(rc && rc->isRunning(), return);
582     ExtensionSystem::Invoker<void>(debuggerPlugin(), "attachExternalApplication", rc);
583 }
584 
stopRunControl()585 void AppOutputPane::stopRunControl()
586 {
587     const int index = currentIndex();
588     QTC_ASSERT(index != -1, return);
589     RunControl *rc = m_runControlTabs.at(index).runControl;
590     QTC_ASSERT(rc, return);
591 
592     if (rc->isRunning() && optionallyPromptToStop(rc))
593         rc->initiateStop();
594     else {
595         QTC_CHECK(false);
596         rc->forceStop();
597     }
598 
599     qCDebug(appOutputLog) << "AppOutputPane::stopRunControl" << rc;
600 }
601 
closeTabs(CloseTabMode mode)602 void AppOutputPane::closeTabs(CloseTabMode mode)
603 {
604     for (int t = m_tabWidget->count() - 1; t >= 0; t--)
605         closeTab(t, mode);
606 }
607 
allRunControls() const608 QList<RunControl *> AppOutputPane::allRunControls() const
609 {
610     const QList<RunControl *> list = Utils::transform<QList>(m_runControlTabs,[](const RunControlTab &tab) {
611         return tab.runControl.data();
612     });
613     return Utils::filtered(list, [](RunControl *rc) { return rc; });
614 }
615 
closeTab(int tabIndex,CloseTabMode closeTabMode)616 void AppOutputPane::closeTab(int tabIndex, CloseTabMode closeTabMode)
617 {
618     int index = indexOf(m_tabWidget->widget(tabIndex));
619     QTC_ASSERT(index != -1, return);
620 
621     RunControl *runControl = m_runControlTabs[index].runControl;
622     Core::OutputWindow *window = m_runControlTabs[index].window;
623     qCDebug(appOutputLog) << "AppOutputPane::closeTab tab" << tabIndex << runControl << window;
624     // Prompt user to stop
625     if (closeTabMode == CloseTabWithPrompt) {
626         QWidget *tabWidget = m_tabWidget->widget(tabIndex);
627         if (runControl && runControl->isRunning() && !runControl->promptToStop())
628             return;
629         // The event loop has run, thus the ordering might have changed, a tab might
630         // have been closed, so do some strange things...
631         tabIndex = m_tabWidget->indexOf(tabWidget);
632         index = indexOf(tabWidget);
633         if (tabIndex == -1 || index == -1)
634             return;
635     }
636 
637     m_tabWidget->removeTab(tabIndex);
638     delete window;
639 
640     if (runControl)
641         runControl->initiateFinish(); // Will self-destruct.
642     m_runControlTabs.removeAt(index);
643     updateCloseActions();
644     setFilteringEnabled(m_tabWidget->count() > 0);
645 
646     if (m_runControlTabs.isEmpty())
647         hide();
648 }
649 
optionallyPromptToStop(RunControl * runControl)650 bool AppOutputPane::optionallyPromptToStop(RunControl *runControl)
651 {
652     ProjectExplorerSettings settings = ProjectExplorerPlugin::projectExplorerSettings();
653     if (!runControl->promptToStop(&settings.prompToStopRunControl))
654         return false;
655     ProjectExplorerPlugin::setProjectExplorerSettings(settings);
656     return true;
657 }
658 
projectRemoved()659 void AppOutputPane::projectRemoved()
660 {
661     tabChanged(m_tabWidget->currentIndex());
662 }
663 
enableDefaultButtons()664 void AppOutputPane::enableDefaultButtons()
665 {
666     enableButtons(currentRunControl());
667 }
668 
zoomIn(int range)669 void AppOutputPane::zoomIn(int range)
670 {
671     for (const RunControlTab &tab : qAsConst(m_runControlTabs))
672         tab.window->zoomIn(range);
673 }
674 
zoomOut(int range)675 void AppOutputPane::zoomOut(int range)
676 {
677     for (const RunControlTab &tab : qAsConst(m_runControlTabs))
678         tab.window->zoomOut(range);
679 }
680 
resetZoom()681 void AppOutputPane::resetZoom()
682 {
683     for (const RunControlTab &tab : qAsConst(m_runControlTabs))
684         tab.window->resetZoom();
685 }
686 
enableButtons(const RunControl * rc)687 void AppOutputPane::enableButtons(const RunControl *rc)
688 {
689     if (rc) {
690         const bool isRunning = rc->isRunning();
691         m_reRunButton->setEnabled(rc->isStopped() && rc->supportsReRunning());
692         m_reRunButton->setIcon(rc->icon().icon());
693         m_stopAction->setEnabled(isRunning);
694         if (isRunning && debuggerPlugin() && rc->applicationProcessHandle().isValid()) {
695             m_attachButton->setEnabled(true);
696             Utils::ProcessHandle h = rc->applicationProcessHandle();
697             QString tip = h.isValid() ? RunControl::tr("PID %1").arg(h.pid())
698                                       : RunControl::tr("Invalid");
699             m_attachButton->setToolTip(msgAttachDebuggerTooltip(tip));
700         } else {
701             m_attachButton->setEnabled(false);
702             m_attachButton->setToolTip(msgAttachDebuggerTooltip());
703         }
704         setZoomButtonsEnabled(true);
705     } else {
706         m_reRunButton->setEnabled(false);
707         m_reRunButton->setIcon(Utils::Icons::RUN_SMALL_TOOLBAR.icon());
708         m_attachButton->setEnabled(false);
709         m_attachButton->setToolTip(msgAttachDebuggerTooltip());
710         m_stopAction->setEnabled(false);
711         setZoomButtonsEnabled(false);
712     }
713     m_formatterWidget->setVisible(m_formatterWidget->layout()->count());
714 }
715 
tabChanged(int i)716 void AppOutputPane::tabChanged(int i)
717 {
718     const int index = indexOf(m_tabWidget->widget(i));
719     if (i != -1 && index != -1) {
720         const RunControlTab &controlTab = m_runControlTabs[index];
721         controlTab.window->updateFilterProperties(filterText(), filterCaseSensitivity(),
722                                                   filterUsesRegexp(), filterIsInverted());
723         enableButtons(controlTab.runControl);
724     } else {
725         enableDefaultButtons();
726     }
727 }
728 
contextMenuRequested(const QPoint & pos,int index)729 void AppOutputPane::contextMenuRequested(const QPoint &pos, int index)
730 {
731     const QList<QAction *> actions = {m_closeCurrentTabAction, m_closeAllTabsAction, m_closeOtherTabsAction};
732     QAction *action = QMenu::exec(actions, m_tabWidget->mapToGlobal(pos), nullptr, m_tabWidget);
733     const int currentIdx = index != -1 ? index : currentIndex();
734     if (action == m_closeCurrentTabAction) {
735         if (currentIdx >= 0)
736             closeTab(currentIdx);
737     } else if (action == m_closeAllTabsAction) {
738         closeTabs(AppOutputPane::CloseTabWithPrompt);
739     } else if (action == m_closeOtherTabsAction) {
740         for (int t = m_tabWidget->count() - 1; t >= 0; t--)
741             if (t != currentIdx)
742                 closeTab(t);
743     }
744 }
745 
slotRunControlChanged()746 void AppOutputPane::slotRunControlChanged()
747 {
748     RunControl *current = currentRunControl();
749     if (current && current == sender())
750         enableButtons(current); // RunControl::isRunning() cannot be trusted in signal handler.
751 }
752 
slotRunControlFinished()753 void AppOutputPane::slotRunControlFinished()
754 {
755     auto *rc = qobject_cast<RunControl *>(sender());
756     QTimer::singleShot(0, this, [this, rc]() { slotRunControlFinished2(rc); });
757     for (const RunControlTab &t : qAsConst(m_runControlTabs)) {
758         if (t.runControl == rc) {
759             t.window->flush();
760             break;
761         }
762     }
763 }
764 
slotRunControlFinished2(RunControl * sender)765 void AppOutputPane::slotRunControlFinished2(RunControl *sender)
766 {
767     const int senderIndex = indexOf(sender);
768 
769     // This slot is queued, so the stop() call in closeTab might lead to this slot, after closeTab already cleaned up
770     if (senderIndex == -1)
771         return;
772 
773     // Enable buttons for current
774     RunControl *current = currentRunControl();
775 
776     qCDebug(appOutputLog) << "AppOutputPane::runControlFinished"  << sender << senderIndex
777                           << "current" << current << m_runControlTabs.size();
778 
779     if (current && current == sender)
780         enableButtons(current);
781 
782     ProjectExplorerPlugin::updateRunActions();
783 
784 #ifdef Q_OS_WIN
785     const bool isRunning = Utils::anyOf(m_runControlTabs, [](const RunControlTab &rt) {
786         return rt.runControl && rt.runControl->isRunning();
787     });
788     if (!isRunning)
789         WinDebugInterface::instance()->stop();
790 #endif
791 
792 }
793 
canNext() const794 bool AppOutputPane::canNext() const
795 {
796     return false;
797 }
798 
canPrevious() const799 bool AppOutputPane::canPrevious() const
800 {
801     return false;
802 }
803 
goToNext()804 void AppOutputPane::goToNext()
805 {
806 
807 }
808 
goToPrev()809 void AppOutputPane::goToPrev()
810 {
811 
812 }
813 
canNavigate() const814 bool AppOutputPane::canNavigate() const
815 {
816     return false;
817 }
818 
819 class AppOutputSettingsWidget : public Core::IOptionsPageWidget
820 {
821     Q_DECLARE_TR_FUNCTIONS(ProjectExplorer::Internal::AppOutputSettingsPage)
822 public:
AppOutputSettingsWidget()823     AppOutputSettingsWidget()
824     {
825         const AppOutputSettings &settings = ProjectExplorerPlugin::appOutputSettings();
826         m_wrapOutputCheckBox.setText(tr("Word-wrap output"));
827         m_wrapOutputCheckBox.setChecked(settings.wrapOutput);
828         m_cleanOldOutputCheckBox.setText(tr("Clear old output on a new run"));
829         m_cleanOldOutputCheckBox.setChecked(settings.cleanOldOutput);
830         m_mergeChannelsCheckBox.setText(tr("Merge stderr and stdout"));
831         m_mergeChannelsCheckBox.setChecked(settings.mergeChannels);
832         for (QComboBox * const modeComboBox
833              : {&m_runOutputModeComboBox, &m_debugOutputModeComboBox}) {
834             modeComboBox->addItem(tr("Always"), int(AppOutputPaneMode::PopupOnOutput));
835             modeComboBox->addItem(tr("Never"), int(AppOutputPaneMode::FlashOnOutput));
836             modeComboBox->addItem(tr("On First Output Only"),
837                                   int(AppOutputPaneMode::PopupOnFirstOutput));
838         }
839         m_runOutputModeComboBox.setCurrentIndex(m_runOutputModeComboBox
840                                                 .findData(int(settings.runOutputMode)));
841         m_debugOutputModeComboBox.setCurrentIndex(m_debugOutputModeComboBox
842                                                   .findData(int(settings.debugOutputMode)));
843         m_maxCharsBox.setMaximum(100000000);
844         m_maxCharsBox.setValue(settings.maxCharCount);
845         const auto layout = new QVBoxLayout(this);
846         layout->addWidget(&m_wrapOutputCheckBox);
847         layout->addWidget(&m_cleanOldOutputCheckBox);
848         layout->addWidget(&m_mergeChannelsCheckBox);
849         const auto maxCharsLayout = new QHBoxLayout;
850         const QString msg = tr("Limit output to %1 characters");
851         const QStringList parts = msg.split("%1") << QString() << QString();
852         maxCharsLayout->addWidget(new QLabel(parts.at(0).trimmed()));
853         maxCharsLayout->addWidget(&m_maxCharsBox);
854         maxCharsLayout->addWidget(new QLabel(parts.at(1).trimmed()));
855         maxCharsLayout->addStretch(1);
856         const auto outputModeLayout = new QFormLayout;
857         outputModeLayout->addRow(tr("Open pane on output when running:"), &m_runOutputModeComboBox);
858         outputModeLayout->addRow(tr("Open pane on output when debugging:"),
859                                  &m_debugOutputModeComboBox);
860         layout->addLayout(outputModeLayout);
861         layout->addLayout(maxCharsLayout);
862         layout->addStretch(1);
863     }
864 
apply()865     void apply() final
866     {
867         AppOutputSettings s;
868         s.wrapOutput = m_wrapOutputCheckBox.isChecked();
869         s.cleanOldOutput = m_cleanOldOutputCheckBox.isChecked();
870         s.mergeChannels = m_mergeChannelsCheckBox.isChecked();
871         s.runOutputMode = static_cast<AppOutputPaneMode>(
872                     m_runOutputModeComboBox.currentData().toInt());
873         s.debugOutputMode = static_cast<AppOutputPaneMode>(
874                     m_debugOutputModeComboBox.currentData().toInt());
875         s.maxCharCount = m_maxCharsBox.value();
876 
877         ProjectExplorerPlugin::setAppOutputSettings(s);
878     }
879 
880 private:
881     QCheckBox m_wrapOutputCheckBox;
882     QCheckBox m_cleanOldOutputCheckBox;
883     QCheckBox m_mergeChannelsCheckBox;
884     QComboBox m_runOutputModeComboBox;
885     QComboBox m_debugOutputModeComboBox;
886     QSpinBox m_maxCharsBox;
887 };
888 
AppOutputSettingsPage()889 AppOutputSettingsPage::AppOutputSettingsPage()
890 {
891     setId(OPTIONS_PAGE_ID);
892     setDisplayName(AppOutputSettingsWidget::tr("Application Output"));
893     setCategory(Constants::BUILD_AND_RUN_SETTINGS_CATEGORY);
894     setWidgetCreator([] { return new AppOutputSettingsWidget; });
895 }
896 
897 } // namespace Internal
898 } // namespace ProjectExplorer
899 
900 #include "appoutputpane.moc"
901 
902